Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/3.2/ChooseItemDialog.cs @ 2491

Last change on this file since 2491 was 1529, checked in by gkronber, 16 years ago

Moved source files of plugins AdvancedOptimizationFrontEnd ... Grid into version-specific sub-folders. #576

File size: 6.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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;
24using System.Collections.Generic;
25using System.ComponentModel;
26using System.Data;
27using System.Drawing;
28using System.Text;
29using System.Windows.Forms;
30using HeuristicLab.PluginInfrastructure;
31
32namespace HeuristicLab.Core {
33  /// <summary>
34  /// A dialog to select an item.
35  /// </summary>
36  public partial class ChooseItemDialog : Form {
37    #region Inner Class TreeNodeSorter
38    private class TreeNodeSorter : IComparer {
39      public int Compare(object x, object y) {
40        TreeNode tx = x as TreeNode;
41        TreeNode ty = y as TreeNode;
42
43        int result = string.Compare(tx.Text, ty.Text);
44        if (result == 0)
45          result = tx.Index.CompareTo(ty.Index);
46        return result;
47      }
48    }
49    #endregion
50
51    /// <summary>
52    /// Gets or sets the caption of the dialog.
53    /// </summary>
54    /// <remarks>Uses property <see cref="Form.Text"/> of base class <see cref="System.Windows.Forms.Form"/>.
55    /// No own data storage present.</remarks>
56    public string Caption {
57      get { return Text; }
58      set { Text = value; }
59    }
60
61    private IItem myItem;
62    /// <summary>
63    /// Gets the selected item.
64    /// </summary>
65    public IItem Item {
66      get { return myItem; }
67    }
68
69    /// <summary>
70    /// Initializes a new instance of <see cref="ChooseItemDialog"/>.
71    /// </summary>
72    /// <remarks>Calls <see cref="ChooseItemDialog(System.Type)"/> with the type of <see cref="IItem"/>
73    /// as parameter.</remarks>
74    public ChooseItemDialog()
75      : this(typeof(IItem)) {
76    }
77    /// <summary>
78    /// Initializes a new instance of <see cref="ChooseItemDialog"/> with
79    /// the given <paramref name="itemType"/>.
80    /// </summary>
81    /// <param name="itemType">The type of the items to choose from.</param>
82    public ChooseItemDialog(Type itemType) {
83      InitializeComponent();
84
85      treeView.TreeViewNodeSorter = new TreeNodeSorter();
86
87      if (itemType == null) {
88        itemType = typeof(IItem);
89      }
90
91      DiscoveryService discoveryService = new DiscoveryService();
92      foreach (PluginInfo plugin in discoveryService.Plugins) {
93        TreeNode pluginNode = new TreeNode(plugin.Name);
94        pluginNode.Tag = null;
95
96        Type[] types = discoveryService.GetTypes(itemType, plugin);
97        foreach (Type type in types) {
98          if (!type.IsAbstract) {
99            TreeNode itemNode = new TreeNode();
100            itemNode.Text = type.Name;
101            itemNode.Tag = type;
102            pluginNode.Nodes.Add(itemNode);
103          }
104        }
105        if (pluginNode.Nodes.Count > 0) {
106          treeView.Nodes.Add(pluginNode);
107        }
108      }
109      if (treeView.Nodes.Count == 0) {
110        treeView.Enabled = false;
111        treeView.Nodes.Add(new TreeNode("No item types available"));
112      }
113      okButton.Enabled = false;
114
115      if (treeView.Nodes.Count == 1) {
116        TreeNodeCollection items = treeView.Nodes[0].Nodes;
117        treeView.Nodes.Clear();
118        foreach (TreeNode i in items) {
119          treeView.Nodes.Add(i);
120        }
121      }
122    }
123
124    private void okButton_Click(object sender, EventArgs e) {
125      Type type = (Type)treeView.SelectedNode.Tag;
126
127      if (type.ContainsGenericParameters) {
128        type = type.GetGenericTypeDefinition();
129        Type[] args = new Type[type.GetGenericArguments().Length];
130        ChooseTypeDialog dialog = new ChooseTypeDialog();
131        for (int i = 0; i < args.Length; i++) {
132          dialog.Caption = "Choose type of generic parameter " + (i + 1).ToString();
133          if (dialog.ShowDialog(this) == DialogResult.OK)
134            args[i] = dialog.Type;
135          else
136            args[i] = null;
137        }
138        dialog.Dispose();
139        type = type.MakeGenericType(args);
140      }
141
142      myItem = (IItem)Activator.CreateInstance(type);
143      this.DialogResult = DialogResult.OK;
144      this.Close();
145    }
146
147    private void treeView_DoubleClick(object sender, EventArgs e) {
148      // plugin groups have a null-tag
149      if ((treeView.SelectedNode != null) && (treeView.SelectedNode.Tag != null)) {
150        Type type = (Type)treeView.SelectedNode.Tag;
151
152        if (type.ContainsGenericParameters) {
153          type = type.GetGenericTypeDefinition();
154          Type[] args = new Type[type.GetGenericArguments().Length];
155          ChooseTypeDialog dialog = new ChooseTypeDialog();
156          for (int i = 0; i < args.Length; i++) {
157            dialog.Caption = "Choose type of generic parameter " + (i + 1).ToString();
158            if (dialog.ShowDialog(this) == DialogResult.OK)
159              args[i] = dialog.Type;
160            else
161              args[i] = null;
162          }
163          dialog.Dispose();
164          type = type.MakeGenericType(args);
165        }
166
167        myItem = (IItem)Activator.CreateInstance(type);
168        this.DialogResult = DialogResult.OK;
169        this.Close();
170      }
171    }
172
173    private void treeView_AfterSelect(object sender, TreeViewEventArgs e) {
174      okButton.Enabled = false;
175      if ((treeView.SelectedNode != null) && (treeView.SelectedNode.Tag != null))
176        okButton.Enabled = true;
177    }
178
179    private void ChooseItemDialog_Load(object sender, EventArgs e) {
180      myItem = null;
181      if (treeView.Nodes.Count == 1) {
182        Type type = (Type)treeView.Nodes[0].Tag;
183        if (!type.ContainsGenericParameters) {
184          myItem = (IItem)Activator.CreateInstance(type);
185          this.DialogResult = DialogResult.OK;
186          this.Close();
187        }
188      }
189    }
190  }
191}
Note: See TracBrowser for help on using the repository browser.