1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using System.Windows.Forms;
|
---|
6 | using System.Xml;
|
---|
7 | using HeuristicLab.DataImporter.Data;
|
---|
8 | using HeuristicLab.DataImporter.Data.CommandBase;
|
---|
9 | using HeuristicLab.DataImporter.Data.Model;
|
---|
10 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
11 |
|
---|
12 | namespace 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 | }
|
---|