Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6377 was 6134, checked in by gkronber, 14 years ago

#1471: added plugin for DbExplorer interfaces, deleted .resx files, set svn:ignore properties, and added license header

File size: 3.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Text;
26using System.Windows.Forms;
27using System.Xml;
28using HeuristicLab.DataImporter.Data;
29using HeuristicLab.DataImporter.Data.CommandBase;
30using HeuristicLab.DataImporter.Data.Model;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace 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}
Note: See TracBrowser for help on using the repository browser.