Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Command/ChangeColumnGroup/AddNewColumnCommandBase.cs @ 8614

Last change on this file since 8614 was 7732, checked in by sforsten, 13 years ago

#1825: new columns are now inserted next to the selected one. If no column is selected, the new column is inserted at the end

File size: 2.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Linq;
24using HeuristicLab.DataImporter.Data.CommandBase;
25using HeuristicLab.DataImporter.Data.Model;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.DataImporter.Command {
29  [StorableClass]
30  public abstract class AddNewColumnCommandBase : ColumnGroupCommandBase {
31    private AddNewColumnCommandBase()
32      : base(null, string.Empty) {
33    }
34
35    protected AddNewColumnCommandBase(DataSet dataSet, string columnGroupName, string columnName, Type columnType)
36      : base(dataSet, columnGroupName) {
37      this.ColumnName = columnName;
38      if (!columnType.IsSubclassOf(typeof(ColumnBase)) || columnType == null)
39        throw new CommandExecutionException("Added Column must be a subtype of ColumnBase", this);
40      this.ColumnType = columnType;
41    }
42
43    private ColumnBase newColumn;
44    protected ColumnBase NewColumn {
45      get { return this.newColumn; }
46      set { this.newColumn = value; }
47    }
48
49    [Storable]
50    private string columnName;
51    protected string ColumnName {
52      get { return this.columnName; }
53      set { this.columnName = value; }
54    }
55
56    [Storable]
57    private Type columnType;
58    protected Type ColumnType {
59      get { return this.columnType; }
60      set { this.columnType = value; }
61    }
62
63    public override void Execute() {
64      base.Execute();
65      newColumn = (ColumnBase)Activator.CreateInstance(ColumnType, new object[] { this.ColumnName });
66      newColumn.Resize(ColumnGroup.RowCount);
67
68      ColumnBase cb = ColumnGroup.Columns.Where(x => x.Selected).FirstOrDefault();
69      if (cb != null) {
70        ColumnGroup.InsertColumn(ColumnGroup.IndexOfColumn(cb) + 1, NewColumn);
71      } else {
72        ColumnGroup.AddColumn(NewColumn);
73      }
74      ColumnGroup.FireChanged();
75      ColumnGroup = null;
76    }
77
78    public override void UndoExecute() {
79      base.UndoExecute();
80      ColumnGroup.RemoveColumn(NewColumn);
81      newColumn = null;
82      ColumnGroup.FireChanged();
83      ColumnGroup = null;
84    }
85  }
86}
Note: See TracBrowser for help on using the repository browser.