#region License Information /* HeuristicLab * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Linq; using HeuristicLab.DataImporter.Data.CommandBase; using HeuristicLab.DataImporter.Data.Model; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.DataImporter.Command { [StorableClass] public abstract class AddNewColumnCommandBase : ColumnGroupCommandBase { private AddNewColumnCommandBase() : base(null, string.Empty) { } protected AddNewColumnCommandBase(DataSet dataSet, string columnGroupName, string columnName, Type columnType) : base(dataSet, columnGroupName) { this.ColumnName = columnName; if (!columnType.IsSubclassOf(typeof(ColumnBase)) || columnType == null) throw new CommandExecutionException("Added Column must be a subtype of ColumnBase", this); this.ColumnType = columnType; } private ColumnBase newColumn; protected ColumnBase NewColumn { get { return this.newColumn; } set { this.newColumn = value; } } [Storable] private string columnName; protected string ColumnName { get { return this.columnName; } set { this.columnName = value; } } [Storable] private Type columnType; protected Type ColumnType { get { return this.columnType; } set { this.columnType = value; } } public override void Execute() { base.Execute(); newColumn = (ColumnBase)Activator.CreateInstance(ColumnType, new object[] { this.ColumnName }); newColumn.Resize(ColumnGroup.RowCount); ColumnBase cb = ColumnGroup.Columns.Where(x => x.Selected).FirstOrDefault(); if (cb != null) { ColumnGroup.InsertColumn(ColumnGroup.IndexOfColumn(cb) + 1, NewColumn); } else { ColumnGroup.AddColumn(NewColumn); } ColumnGroup.FireChanged(); ColumnGroup = null; } public override void UndoExecute() { base.UndoExecute(); ColumnGroup.RemoveColumn(NewColumn); newColumn = null; ColumnGroup.FireChanged(); ColumnGroup = null; } } }