Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1471: imported generic parts of DataImporter from private code base

File size: 2.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Windows.Forms;
6using System.Xml;
7using HeuristicLab.DataImporter.Data;
8using HeuristicLab.DataImporter.Data.CommandBase;
9using HeuristicLab.DataImporter.Data.Model;
10using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
11
12namespace HeuristicLab.DataImporter.Command {
13  [StorableClass]
14  public abstract class ChangeColumnCommandBase : ColumnGroupCommandWithAffectedColumnsBase {
15    [Storable]
16    protected Type columnType;
17
18    protected List<ColumnBase> newColumns;
19    protected List<ColumnBase> oldColumns;
20    protected List<int> positions;
21    protected ICollection<int> oldSortedColumnIndexes;
22    protected IEnumerable<SortOrder> oldSortOrder;
23
24    private ChangeColumnCommandBase()
25      : base(null, string.Empty, null) {
26      newColumns = new List<ColumnBase>();
27      oldColumns = new List<ColumnBase>();
28      positions = new List<int>();
29    }
30
31    protected ChangeColumnCommandBase(DataSet dataSet, string columnGroupName, int[] affectedColumns, Type newColumnType) :
32      base(dataSet, columnGroupName, affectedColumns) {
33      if (!newColumnType.IsSubclassOf(typeof(ColumnBase)))
34        throw new CommandExecutionException("Given columntype no subtype of ColumnBase.", this);
35
36      this.columnType = newColumnType;
37      newColumns = new List<ColumnBase>();
38      oldColumns = new List<ColumnBase>();
39      positions = new List<int>();
40    }
41
42    public override void Execute() {
43      base.Execute();
44      ColumnBase newCol;
45      ColumnBase oldCol;
46      oldSortOrder = ColumnGroup.SortOrdersForColumns.ToList();
47      oldSortedColumnIndexes = new List<int>(ColumnGroup.SortedColumnIndexes);
48      for (int j = 0; j < AffectedColumns.Length; j++) {
49        oldCol = ColumnGroup.Columns.ElementAt(AffectedColumns[j]);
50        if (oldCol.GetType() != columnType) {
51          positions.Add(AffectedColumns[j]);
52          oldColumns.Add(oldCol);
53          newCol = (ColumnBase)Activator.CreateInstance(columnType, new object[] { oldCol.Name, oldCol.TotalValuesCount });
54          for (int i = 0; i < oldCol.TotalValuesCount; i++)
55            newCol.AddValueOrNull(oldCol.GetValue(i));
56          newColumns.Add(newCol);
57        }
58      }
59
60      for (int i = 0; i < positions.Count; i++)
61        ColumnGroup.ReplaceColumn(positions[i], newColumns[i]);
62      ColumnGroup.FireChanged();
63    }
64
65    public override void UndoExecute() {
66      base.UndoExecute();
67      for (int i = 0; i < positions.Count; i++)
68        ColumnGroup.ReplaceColumn(positions[i], oldColumns[i]);
69      ColumnGroup.SortOrdersForColumns = oldSortOrder;
70      ColumnGroup.SortedColumnIndexes = oldSortedColumnIndexes;
71      oldSortedColumnIndexes = null;
72      oldSortOrder = null;
73      oldColumns.Clear();
74      newColumns.Clear();
75      positions.Clear();
76      ColumnGroup.FireChanged();
77    }
78  }
79}
Note: See TracBrowser for help on using the repository browser.