Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 16566 was 16566, checked in by gkronber, 5 years ago

#2520: updated HeuristicLab.DataImporter addon for new Persistence (introduced in 3.3.16)

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