Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/3.2/ChooseTypeDialog.cs @ 2688

Last change on this file since 2688 was 2591, checked in by gkronber, 15 years ago

Copied refactored plugin infrastructure from branch and merged changeset r2586:2589 from branch into the trunk. #799

File size: 4.8 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 a specific type.
35  /// </summary>
36  public partial class ChooseTypeDialog : 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="Form"/>.
55    /// No own data storage present.</remarks>
56    public string Caption {
57      get { return Text; }
58      set { Text = value; }
59    }
60
61    private Type myType;
62    /// <summary>
63    /// Gets the selected type.
64    /// </summary>
65    public Type Type {
66      get { return myType; }
67    }
68
69    /// <summary>
70    /// Initializes a new instance of <see cref="ChooseTypeDialog"/>.
71    /// </summary>
72    /// <remarks>Calls <see cref="ChooseTypeDialog(Type)"/> with the type of <see cref="IItem"/> as
73    /// parameter.</remarks>
74    public ChooseTypeDialog()
75      : this(typeof(IItem)) {
76    }
77
78    /// <summary>
79    /// Initializes a new instance of <see cref="ChooseTypeDialog"/> with the given
80    /// <paramref name="baseType"/>.
81    /// </summary>
82    /// <param name="baseType">The base type of all chooseable types.</param>
83    public ChooseTypeDialog(Type baseType) {
84      InitializeComponent();
85
86      treeView.TreeViewNodeSorter = new TreeNodeSorter();
87
88      if (baseType == null) {
89        baseType = typeof(IItem);
90      }
91
92      foreach (IPluginDescription 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(baseType, plugin)) {
97          TreeNode itemNode = new TreeNode();
98          itemNode.Text = type.Name;
99          itemNode.Tag = type;
100          pluginNode.Nodes.Add(itemNode);
101        }
102        if (pluginNode.Nodes.Count > 0) {
103          treeView.Nodes.Add(pluginNode);
104        }
105      }
106      if (treeView.Nodes.Count == 0) {
107        treeView.Enabled = false;
108        treeView.Nodes.Add(new TreeNode("No item types available"));
109      }
110      okButton.Enabled = false;
111
112      if (treeView.Nodes.Count == 1) {
113        TreeNodeCollection items = treeView.Nodes[0].Nodes;
114        treeView.Nodes.Clear();
115        foreach (TreeNode i in items) {
116          treeView.Nodes.Add(i);
117        }
118      }
119    }
120
121    private void okButton_Click(object sender, EventArgs e) {
122      myType = (Type)treeView.SelectedNode.Tag;
123      this.DialogResult = DialogResult.OK;
124      this.Close();
125    }
126
127    private void treeView_DoubleClick(object sender, EventArgs e) {
128      // plugin groups have a null-tag
129      if ((treeView.SelectedNode != null) && (treeView.SelectedNode.Tag != null)) {
130        myType = (Type)treeView.SelectedNode.Tag;
131        this.DialogResult = DialogResult.OK;
132        this.Close();
133      }
134    }
135
136    private void treeView_AfterSelect(object sender, TreeViewEventArgs e) {
137      okButton.Enabled = false;
138      if ((treeView.SelectedNode != null) && (treeView.SelectedNode.Tag != null))
139        okButton.Enabled = true;
140    }
141
142    private void ChooseTypeDialog_Load(object sender, EventArgs e) {
143      myType = null;
144      if (treeView.Nodes.Count == 1) {
145        myType = (Type)treeView.Nodes[0].Tag;
146        this.DialogResult = DialogResult.OK;
147        this.Close();
148      }
149    }
150  }
151}
Note: See TracBrowser for help on using the repository browser.