Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Command/ChangeColumnGroup/ChangeColumnCommandBase.cs @ 10743

Last change on this file since 10743 was 9615, checked in by mkommend, 11 years ago

#1734: Updated copyright information in all DataImporter classes.

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