Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Data/View/CommandListViewItem.cs @ 14781

Last change on this file since 14781 was 9615, checked in by mkommend, 11 years ago

#1734: Updated copyright information in all DataImporter classes.

File size: 3.6 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.Collections.Generic;
24using System.Linq;
25using System.Text;
26using System.Windows.Forms;
27using System.Reflection;
28using HeuristicLab.DataImporter.Data.CommandBase;
29using HeuristicLab.DataImporter.Data.Model;
30
31
32namespace HeuristicLab.DataImporter.Data.View {
33  public class CommandListViewItem<T> : ListViewItem, ICommandGenerator where T : ICommand {
34    private ViewableCommandInfoAttribute commandInfo;
35    public CommandListViewItem() {
36      commandInfo = (ViewableCommandInfoAttribute)Attribute.GetCustomAttribute(typeof(T), typeof(ViewableCommandInfoAttribute));
37      if (commandInfo == null)
38        throw new ArgumentException("ViewableCommandInfoAttribute must be specified!");
39      this.Text = commandInfo.Name;
40      this.Group = new ListViewGroup(commandInfo.GroupName, commandInfo.GroupName);
41    }
42
43    public virtual ICommand GenerateCommand(DataSet dataSet) {
44      T cmd;
45      ColumnGroup activeColumnGroup = null;
46      int[] selectedColumnIndizes = new int[0];
47      if (dataSet.ActiveColumnGroups.Count() > 0)
48        activeColumnGroup = dataSet.ActiveColumnGroups.ElementAt(0);
49      if (activeColumnGroup != null)
50        selectedColumnIndizes = activeColumnGroup.SelectedColumnIndexes;
51      //call ctor according to super class
52      if (typeof(T).IsSubclassOf(typeof(ColumnCommandBase)))
53        cmd = (T)Activator.CreateInstance(typeof(T), new object[] { dataSet, activeColumnGroup.Name, activeColumnGroup.SelectedColumnIndexes[0] });
54      else if (typeof(T).IsSubclassOf(typeof(ColumnGroupCommandWithAffectedColumnsBase)))
55        cmd = (T)Activator.CreateInstance(typeof(T), new object[] { dataSet, activeColumnGroup.Name, selectedColumnIndizes });
56      else if (typeof(T).IsSubclassOf(typeof(ColumnGroupCommandBase)))
57        cmd = (T)Activator.CreateInstance(typeof(T), new object[] { dataSet, activeColumnGroup.Name });
58      else if (typeof(T).IsSubclassOf(typeof(DataSetCommandWithAffectedColumnGroupsBase)))
59        cmd = (T)Activator.CreateInstance(typeof(T), new object[] { dataSet, dataSet.ActiveColumnGroups.Select(x =>x.Name).ToList() });
60      else
61        cmd = (T)Activator.CreateInstance(typeof(T), new object[] { dataSet });
62
63      if (commandInfo.OptionsView != null) {
64        CommandViewDialog dlg = new CommandViewDialog();
65        dlg.StartPosition = FormStartPosition.CenterParent;
66        UserControl ctrl = (UserControl)Activator.CreateInstance(commandInfo.OptionsView,cmd);
67        dlg.Panel.Controls.Add(ctrl);
68        dlg.ShowDialog(this.ListView.TopLevelControl);
69        if (dlg.DialogResult == DialogResult.Cancel)
70          return null;
71      }
72      return cmd;
73    }
74
75    public bool Enabled(DataSetState state) {
76      return commandInfo.State.Enabled(state);
77    }
78  }
79}
Note: See TracBrowser for help on using the repository browser.