Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Data/View/CommandListViewItem.cs @ 17104

Last change on this file since 17104 was 16994, checked in by gkronber, 6 years ago

#2520 Update plugin dependencies and references for HL.DataImporter for new persistence

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