[6134] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[7267] | 3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[6134] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
[6133] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Text;
|
---|
| 26 | using System.Windows.Forms;
|
---|
| 27 | using System.Xml;
|
---|
| 28 | using HeuristicLab.DataImporter.Data;
|
---|
| 29 | using HeuristicLab.DataImporter.Data.CommandBase;
|
---|
| 30 | using HeuristicLab.DataImporter.Data.Model;
|
---|
| 31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 32 |
|
---|
| 33 | namespace HeuristicLab.DataImporter.Command {
|
---|
| 34 | [StorableClass]
|
---|
| 35 | public abstract class ChangeColumnCommandBase : ColumnGroupCommandWithAffectedColumnsBase {
|
---|
| 36 | [Storable]
|
---|
| 37 | protected Type columnType;
|
---|
| 38 |
|
---|
| 39 | protected List<ColumnBase> newColumns;
|
---|
| 40 | protected List<ColumnBase> oldColumns;
|
---|
| 41 | protected List<int> positions;
|
---|
| 42 | protected ICollection<int> oldSortedColumnIndexes;
|
---|
| 43 | protected IEnumerable<SortOrder> oldSortOrder;
|
---|
| 44 |
|
---|
| 45 | private ChangeColumnCommandBase()
|
---|
| 46 | : base(null, string.Empty, null) {
|
---|
| 47 | newColumns = new List<ColumnBase>();
|
---|
| 48 | oldColumns = new List<ColumnBase>();
|
---|
| 49 | positions = new List<int>();
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | protected ChangeColumnCommandBase(DataSet dataSet, string columnGroupName, int[] affectedColumns, Type newColumnType) :
|
---|
| 53 | base(dataSet, columnGroupName, affectedColumns) {
|
---|
| 54 | if (!newColumnType.IsSubclassOf(typeof(ColumnBase)))
|
---|
| 55 | throw new CommandExecutionException("Given columntype no subtype of ColumnBase.", this);
|
---|
| 56 |
|
---|
| 57 | this.columnType = newColumnType;
|
---|
| 58 | newColumns = new List<ColumnBase>();
|
---|
| 59 | oldColumns = new List<ColumnBase>();
|
---|
| 60 | positions = new List<int>();
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | public override void Execute() {
|
---|
| 64 | base.Execute();
|
---|
| 65 | ColumnBase newCol;
|
---|
| 66 | ColumnBase oldCol;
|
---|
| 67 | oldSortOrder = ColumnGroup.SortOrdersForColumns.ToList();
|
---|
| 68 | oldSortedColumnIndexes = new List<int>(ColumnGroup.SortedColumnIndexes);
|
---|
| 69 | for (int j = 0; j < AffectedColumns.Length; j++) {
|
---|
| 70 | oldCol = ColumnGroup.Columns.ElementAt(AffectedColumns[j]);
|
---|
| 71 | if (oldCol.GetType() != columnType) {
|
---|
| 72 | positions.Add(AffectedColumns[j]);
|
---|
| 73 | oldColumns.Add(oldCol);
|
---|
| 74 | newCol = (ColumnBase)Activator.CreateInstance(columnType, new object[] { oldCol.Name, oldCol.TotalValuesCount });
|
---|
| 75 | for (int i = 0; i < oldCol.TotalValuesCount; i++)
|
---|
| 76 | newCol.AddValueOrNull(oldCol.GetValue(i));
|
---|
| 77 | newColumns.Add(newCol);
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | for (int i = 0; i < positions.Count; i++)
|
---|
| 82 | ColumnGroup.ReplaceColumn(positions[i], newColumns[i]);
|
---|
| 83 | ColumnGroup.FireChanged();
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | public override void UndoExecute() {
|
---|
| 87 | base.UndoExecute();
|
---|
| 88 | for (int i = 0; i < positions.Count; i++)
|
---|
| 89 | ColumnGroup.ReplaceColumn(positions[i], oldColumns[i]);
|
---|
| 90 | ColumnGroup.SortOrdersForColumns = oldSortOrder;
|
---|
| 91 | ColumnGroup.SortedColumnIndexes = oldSortedColumnIndexes;
|
---|
| 92 | oldSortedColumnIndexes = null;
|
---|
| 93 | oldSortOrder = null;
|
---|
| 94 | oldColumns.Clear();
|
---|
| 95 | newColumns.Clear();
|
---|
| 96 | positions.Clear();
|
---|
| 97 | ColumnGroup.FireChanged();
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
| 100 | }
|
---|