Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Command/ChangeColumnGroup/ChangeColumnCommandBase.cs @ 17020

Last change on this file since 17020 was 16994, checked in by gkronber, 6 years ago

#2520 Update plugin dependencies and references for HL.DataImporter for new persistence

File size: 3.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.DataImporter.Data.CommandBase;
27using HeuristicLab.DataImporter.Data.Model;
28using HEAL.Attic;
29
30namespace HeuristicLab.DataImporter.Command {
31  [StorableType("A67CC5A7-4C36-418A-BC86-59DA37E38531")]
32  public abstract class ChangeColumnCommandBase : ColumnGroupCommandWithAffectedColumnsBase {
33    [Storable]
34    protected Type columnType;
35
36    protected List<ColumnBase> newColumns;
37    protected List<ColumnBase> oldColumns;
38    protected List<int> positions;
39    protected ICollection<int> oldSortedColumnIndexes;
40    protected IEnumerable<SortOrder> oldSortOrder;
41
42    [StorableConstructor]
43    protected ChangeColumnCommandBase(StorableConstructorFlag _) : base(_) {
44      newColumns = new List<ColumnBase>();
45      oldColumns = new List<ColumnBase>();
46      positions = new List<int>();
47    }
48
49    protected ChangeColumnCommandBase(DataSet dataSet, string columnGroupName, int[] affectedColumns, Type newColumnType) :
50      base(dataSet, columnGroupName, affectedColumns) {
51      if (!newColumnType.IsSubclassOf(typeof(ColumnBase)))
52        throw new CommandExecutionException("Given columntype no subtype of ColumnBase.", this);
53
54      this.columnType = newColumnType;
55      newColumns = new List<ColumnBase>();
56      oldColumns = new List<ColumnBase>();
57      positions = new List<int>();
58    }
59
60    public override void Execute() {
61      base.Execute();
62      ColumnBase newCol;
63      ColumnBase oldCol;
64      oldSortOrder = ColumnGroup.SortOrdersForColumns.ToList();
65      oldSortedColumnIndexes = new List<int>(ColumnGroup.SortedColumnIndexes);
66      for (int j = 0; j < AffectedColumns.Length; j++) {
67        oldCol = ColumnGroup.Columns.ElementAt(AffectedColumns[j]);
68        if (oldCol.GetType() != columnType) {
69          positions.Add(AffectedColumns[j]);
70          oldColumns.Add(oldCol);
71          newCol = (ColumnBase)Activator.CreateInstance(columnType, new object[] { oldCol.Name, oldCol.TotalValuesCount });
72          for (int i = 0; i < oldCol.TotalValuesCount; i++)
73            newCol.AddValueOrNull(oldCol.GetValue(i));
74          newColumns.Add(newCol);
75        }
76      }
77
78      for (int i = 0; i < positions.Count; i++)
79        ColumnGroup.ReplaceColumn(positions[i], newColumns[i]);
80      ColumnGroup.FireChanged();
81    }
82
83    public override void UndoExecute() {
84      base.UndoExecute();
85      for (int i = 0; i < positions.Count; i++)
86        ColumnGroup.ReplaceColumn(positions[i], oldColumns[i]);
87      ColumnGroup.SortOrdersForColumns = oldSortOrder;
88      ColumnGroup.SortedColumnIndexes = oldSortedColumnIndexes;
89      oldSortedColumnIndexes = null;
90      oldSortOrder = null;
91      oldColumns.Clear();
92      newColumns.Clear();
93      positions.Clear();
94      ColumnGroup.FireChanged();
95    }
96  }
97}
Note: See TracBrowser for help on using the repository browser.