Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PluginInfrastructure Refactoring/HeuristicLab.Core/3.2/ChooseItemDialog.cs @ 2484

Last change on this file since 2484 was 2484, checked in by gkronber, 14 years ago

Copied main HL plugins from trunk for testing. #799

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;
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
92      foreach (var plugin in ApplicationManager.Manager.Plugins) {
93        TreeNode pluginNode = new TreeNode(plugin.Name);
94        pluginNode.Tag = null;
95
96        foreach (Type type in ApplicationManager.Manager.GetTypes(itemType, plugin)) {
97          if (!type.IsAbstract) {
98            TreeNode itemNode = new TreeNode();
99            itemNode.Text = type.Name;
100            itemNode.Tag = type;
101            pluginNode.Nodes.Add(itemNode);
102          }
103        }
104        if (pluginNode.Nodes.Count > 0) {
105          treeView.Nodes.Add(pluginNode);
106        }
107      }
108      if (treeView.Nodes.Count == 0) {
109        treeView.Enabled = false;
110        treeView.Nodes.Add(new TreeNode("No item types available"));
111      }
112      okButton.Enabled = false;
113
114      if (treeView.Nodes.Count == 1) {
115        TreeNodeCollection items = treeView.Nodes[0].Nodes;
116        treeView.Nodes.Clear();
117        foreach (TreeNode i in items) {
118          treeView.Nodes.Add(i);
119        }
120      }
121    }
122
123    private void okButton_Click(object sender, EventArgs e) {
124      Type type = (Type)treeView.SelectedNode.Tag;
125
126      if (type.ContainsGenericParameters) {
127        type = type.GetGenericTypeDefinition();
128        Type[] args = new Type[type.GetGenericArguments().Length];
129        ChooseTypeDialog dialog = new ChooseTypeDialog();
130        for (int i = 0; i < args.Length; i++) {
131          dialog.Caption = "Choose type of generic parameter " + (i + 1).ToString();
132          if (dialog.ShowDialog(this) == DialogResult.OK)
133            args[i] = dialog.Type;
134          else
135            args[i] = null;
136        }
137        dialog.Dispose();
138        type = type.MakeGenericType(args);
139      }
140
141      myItem = (IItem)Activator.CreateInstance(type);
142      this.DialogResult = DialogResult.OK;
143      this.Close();
144    }
145
146    private void treeView_DoubleClick(object sender, EventArgs e) {
147      // plugin groups have a null-tag
148      if ((treeView.SelectedNode != null) && (treeView.SelectedNode.Tag != null)) {
149        Type type = (Type)treeView.SelectedNode.Tag;
150
151        if (type.ContainsGenericParameters) {
152          type = type.GetGenericTypeDefinition();
153          Type[] args = new Type[type.GetGenericArguments().Length];
154          ChooseTypeDialog dialog = new ChooseTypeDialog();
155          for (int i = 0; i < args.Length; i++) {
156            dialog.Caption = "Choose type of generic parameter " + (i + 1).ToString();
157            if (dialog.ShowDialog(this) == DialogResult.OK)
158              args[i] = dialog.Type;
159            else
160              args[i] = null;
161          }
162          dialog.Dispose();
163          type = type.MakeGenericType(args);
164        }
165
166        myItem = (IItem)Activator.CreateInstance(type);
167        this.DialogResult = DialogResult.OK;
168        this.Close();
169      }
170    }
171
172    private void treeView_AfterSelect(object sender, TreeViewEventArgs e) {
173      okButton.Enabled = false;
174      if ((treeView.SelectedNode != null) && (treeView.SelectedNode.Tag != null))
175        okButton.Enabled = true;
176    }
177
178    private void ChooseItemDialog_Load(object sender, EventArgs e) {
179      myItem = null;
180      if (treeView.Nodes.Count == 1) {
181        Type type = (Type)treeView.Nodes[0].Tag;
182        if (!type.ContainsGenericParameters) {
183          myItem = (IItem)Activator.CreateInstance(type);
184          this.DialogResult = DialogResult.OK;
185          this.Close();
186        }
187      }
188    }
189  }
190}
Note: See TracBrowser for help on using the repository browser.