#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.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using HeuristicLab.DataImporter.Data.CommandBase;
using HeuristicLab.DataImporter.Data.Model;
namespace HeuristicLab.DataImporter.Data.View {
public class CommandListViewItem : ListViewItem, ICommandGenerator where T : ICommand {
private ViewableCommandInfoAttribute commandInfo;
public CommandListViewItem() {
commandInfo = (ViewableCommandInfoAttribute)Attribute.GetCustomAttribute(typeof(T), typeof(ViewableCommandInfoAttribute));
if (commandInfo == null)
throw new ArgumentException("ViewableCommandInfoAttribute must be specified!");
this.Text = commandInfo.Name;
this.Group = new ListViewGroup(commandInfo.GroupName, commandInfo.GroupName);
}
public virtual ICommand GenerateCommand(DataSet dataSet) {
T cmd;
ColumnGroup activeColumnGroup = null;
int[] selectedColumnIndizes = new int[0];
if (dataSet.ActiveColumnGroups.Count() > 0)
activeColumnGroup = dataSet.ActiveColumnGroups.ElementAt(0);
if (activeColumnGroup != null)
selectedColumnIndizes = activeColumnGroup.SelectedColumnIndexes;
//call ctor according to super class
if (typeof(T).IsSubclassOf(typeof(ColumnCommandBase)))
cmd = (T)Activator.CreateInstance(typeof(T), new object[] { dataSet, activeColumnGroup.Name, activeColumnGroup.SelectedColumnIndexes[0] });
else if (typeof(T).IsSubclassOf(typeof(ColumnGroupCommandWithAffectedColumnsBase)))
cmd = (T)Activator.CreateInstance(typeof(T), new object[] { dataSet, activeColumnGroup.Name, selectedColumnIndizes });
else if (typeof(T).IsSubclassOf(typeof(ColumnGroupCommandBase)))
cmd = (T)Activator.CreateInstance(typeof(T), new object[] { dataSet, activeColumnGroup.Name });
else if (typeof(T).IsSubclassOf(typeof(DataSetCommandWithAffectedColumnGroupsBase)))
cmd = (T)Activator.CreateInstance(typeof(T), new object[] { dataSet, dataSet.ActiveColumnGroups.Select(x =>x.Name).ToList() });
else
cmd = (T)Activator.CreateInstance(typeof(T), new object[] { dataSet });
if (commandInfo.OptionsView != null) {
CommandViewDialog dlg = new CommandViewDialog();
dlg.StartPosition = FormStartPosition.CenterParent;
UserControl ctrl = (UserControl)Activator.CreateInstance(commandInfo.OptionsView,cmd);
dlg.Panel.Controls.Add(ctrl);
dlg.ShowDialog(this.ListView.TopLevelControl);
if (dlg.DialogResult == DialogResult.Cancel)
return null;
}
return cmd;
}
public bool Enabled(DataSetState state) {
return commandInfo.State.Enabled(state);
}
}
}