Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 14349 was 9615, checked in by mkommend, 12 years ago

#1734: Updated copyright information in all DataImporter classes.

File size: 3.2 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.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    [StorableConstructor]
32    protected AddNewColumnCommandBase(bool deserializing) : base(deserializing) { }
33
34    protected AddNewColumnCommandBase(DataSet dataSet, string columnGroupName, string columnName, Type columnType)
35      : base(dataSet, columnGroupName) {
36      this.ColumnName = columnName;
37      if (!columnType.IsSubclassOf(typeof(ColumnBase)) || columnType == null)
38        throw new CommandExecutionException("Added Column must be a subtype of ColumnBase", this);
39      this.ColumnType = columnType;
40    }
41
42    private ColumnBase newColumn;
43    protected ColumnBase NewColumn {
44      get { return this.newColumn; }
45      set { this.newColumn = value; }
46    }
47
48    [Storable]
49    private int columnIndex = -1;
50    protected int ColumnIndex {
51      get { return this.columnIndex; }
52      set { this.columnIndex = value; }
53    }
54
55    [Storable]
56    private string columnName;
57    protected string ColumnName {
58      get { return this.columnName; }
59      set { this.columnName = value; }
60    }
61
62    [Storable]
63    private Type columnType;
64    protected Type ColumnType {
65      get { return this.columnType; }
66      set { this.columnType = value; }
67    }
68
69    public override void Execute() {
70      base.Execute();
71      newColumn = (ColumnBase)Activator.CreateInstance(ColumnType, new object[] { this.ColumnName });
72      newColumn.Resize(ColumnGroup.RowCount);
73
74      if (ColumnIndex == -1) {
75        ColumnBase cb = ColumnGroup.Columns.FirstOrDefault(x => x.Selected);
76        if (cb != null) ColumnIndex = ColumnGroup.IndexOfColumn(cb);
77      }
78      if (ColumnIndex != -1) {
79        ColumnGroup.InsertColumn(ColumnIndex + 1, NewColumn);
80      } else {
81        ColumnGroup.AddColumn(NewColumn);
82      }
83      ColumnGroup.FireChanged();
84      ColumnGroup = null;
85    }
86
87    public override void UndoExecute() {
88      base.UndoExecute();
89      ColumnGroup.RemoveColumn(NewColumn);
90      newColumn = null;
91      ColumnGroup.FireChanged();
92      ColumnGroup = null;
93    }
94  }
95}
Note: See TracBrowser for help on using the repository browser.