Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.Core/3.3/ChooseItemDialog.cs @ 3744

Last change on this file since 3744 was 2744, checked in by epitzer, 15 years ago

update to new PluginInfrastructure (#802)

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