#region License Information
/* HeuristicLab
* Copyright (C) 2002-2011 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.Collections.Generic;
using System.Linq;
using System.Text;
using HeuristicLab.DataImporter.Data;
using HeuristicLab.DataImporter.Data.CommandBase;
using HeuristicLab.DataImporter.Data.Model;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using System.Windows.Forms;
namespace HeuristicLab.DataImporter.Command {
[StorableClass]
[ViewableCommandInfo("Merge ColumnGroups", 2, ColumnGroupState.Active, "ColumnGroup Commands", Position = 5, MinActiveColumnGroups = 2)]
public class MergeColumnGroupsCommand : DataSetCommandWithAffectedColumnGroupsBase {
private SortedDictionary oldColumnGroups;
private List oldRowCounts;
private ColumnGroup newColumnGroup;
private MergeColumnGroupsCommand()
: base(null, null) {
oldColumnGroups = new SortedDictionary();
oldRowCounts = new List();
}
public MergeColumnGroupsCommand(DataSet dataSet, List affectedColumnGroupNames)
: base(dataSet, affectedColumnGroupNames) {
oldColumnGroups = new SortedDictionary();
oldRowCounts = new List();
}
public override void Execute() {
base.Execute();
string name = "";
ColumnGroup columnGroup;
foreach (string columnGroupName in AffectedColumnGroupNames) {
columnGroup = this.DataSet.GetColumnGroup(columnGroupName);
oldColumnGroups.Add(this.DataSet.IndexOfColumnGroup(columnGroup), columnGroup);
oldRowCounts.Add(columnGroup.RowCount);
name = name + " & " + columnGroup.Name;
}
name = name.Remove(0, 3);
this.newColumnGroup = new ColumnGroup(name);
foreach (ColumnGroup grp in oldColumnGroups.Values) {
newColumnGroup.AddColumns(grp.Columns);
DataSet.RemoveColumnGroup(grp);
}
foreach (ColumnBase col in this.newColumnGroup.Columns) {
col.Resize(oldRowCounts.Max());
col.SortOrder = SortOrder.None;
}
DataSet.InsertColumnGroup(oldColumnGroups.Keys.ElementAt(0), this.newColumnGroup);
DataSet.FireChanged();
}
public override void UndoExecute() {
base.UndoExecute();
DataSet.RemoveColumnGroup(newColumnGroup);
int col;
for (int i = 0; i < oldColumnGroups.Count; i++) {
col = oldColumnGroups.Keys.ElementAt(i);
DataSet.InsertColumnGroup(col, oldColumnGroups[col]);
foreach (ColumnBase column in oldColumnGroups[col].Columns)
column.Resize(oldRowCounts[i]);
}
oldColumnGroups.Clear();
newColumnGroup = null;
oldRowCounts.Clear();
DataSet.FireChanged();
}
public override string Description {
get { return "Merge column groups"; }
}
}
}