Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.0/sources/HeuristicLab.Core/ChooseItemDialog.cs @ 17607

Last change on this file since 17607 was 42, checked in by swagner, 17 years ago

Closed ticket #41

  • finished implementing ItemList<T>
File size: 5.5 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  public partial class ChooseItemDialog : Form {
34    #region Inner Class TreeNodeSorter
35    private class TreeNodeSorter : IComparer {
36      public int Compare(object x, object y) {
37        TreeNode tx = x as TreeNode;
38        TreeNode ty = y as TreeNode;
39
40        int result = string.Compare(tx.Text, ty.Text);
41        if (result == 0)
42          result = tx.Index.CompareTo(ty.Index);
43        return result;
44      }
45    }
46    #endregion
47
48    public string Caption {
49      get { return Text; }
50      set { Text = value; }
51    }
52
53    private IItem myItem;
54    public IItem Item {
55      get { return myItem; }
56    }
57
58    public ChooseItemDialog()
59      : this(typeof(IItem)) {
60    }
61
62    public ChooseItemDialog(Type itemType) {
63      InitializeComponent();
64
65      treeView.TreeViewNodeSorter = new TreeNodeSorter();
66
67      if (itemType == null) {
68        itemType = typeof(IItem);
69      }
70
71      DiscoveryService discoveryService = new DiscoveryService();
72      foreach (PluginInfo plugin in discoveryService.Plugins) {
73        TreeNode pluginNode = new TreeNode(plugin.Name);
74        pluginNode.Tag = null;
75
76        Type[] types = discoveryService.GetTypes(itemType, plugin);
77        foreach (Type type in types) {
78          if (!type.IsAbstract) {
79            TreeNode itemNode = new TreeNode();
80            itemNode.Text = type.Name;
81            itemNode.Tag = type;
82            pluginNode.Nodes.Add(itemNode);
83          }
84        }
85        if (pluginNode.Nodes.Count > 0) {
86          treeView.Nodes.Add(pluginNode);
87        }
88      }
89      if (treeView.Nodes.Count == 0) {
90        treeView.Enabled = false;
91        treeView.Nodes.Add(new TreeNode("No item types available"));
92      }
93      okButton.Enabled = false;
94
95      if (treeView.Nodes.Count == 1) {
96        TreeNodeCollection items = treeView.Nodes[0].Nodes;
97        treeView.Nodes.Clear();
98        foreach (TreeNode i in items) {
99          treeView.Nodes.Add(i);
100        }
101      }
102    }
103
104    private void okButton_Click(object sender, EventArgs e) {
105      Type type = (Type)treeView.SelectedNode.Tag;
106
107      if (type.ContainsGenericParameters) {
108        type = type.GetGenericTypeDefinition();
109        Type[] args = new Type[type.GetGenericArguments().Length];
110        ChooseTypeDialog dialog = new ChooseTypeDialog();
111        for (int i = 0; i < args.Length; i++) {
112          dialog.Caption = "Choose type of generic parameter " + (i + 1).ToString();
113          if (dialog.ShowDialog(this) == DialogResult.OK)
114            args[i] = dialog.Type;
115          else
116            args[i] = null;
117        }
118        dialog.Dispose();
119        type = type.MakeGenericType(args);
120      }
121
122      myItem = (IItem)Activator.CreateInstance(type);
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        Type type = (Type)treeView.SelectedNode.Tag;
131
132        if (type.ContainsGenericParameters) {
133          type = type.GetGenericTypeDefinition();
134          Type[] args = new Type[type.GetGenericArguments().Length];
135          ChooseTypeDialog dialog = new ChooseTypeDialog();
136          for (int i = 0; i < args.Length; i++) {
137            dialog.Caption = "Choose type of generic parameter " + (i + 1).ToString();
138            if (dialog.ShowDialog(this) == DialogResult.OK)
139              args[i] = dialog.Type;
140            else
141              args[i] = null;
142          }
143          dialog.Dispose();
144          type = type.MakeGenericType(args);
145        }
146
147        myItem = (IItem)Activator.CreateInstance(type);
148        this.DialogResult = DialogResult.OK;
149        this.Close();
150      }
151    }
152
153    private void treeView_AfterSelect(object sender, TreeViewEventArgs e) {
154      okButton.Enabled = false;
155      if ((treeView.SelectedNode != null) && (treeView.SelectedNode.Tag != null))
156        okButton.Enabled = true;
157    }
158
159    private void ChooseItemDialog_Load(object sender, EventArgs e) {
160      myItem = null;
161      if (treeView.Nodes.Count == 1) {
162        Type type = (Type)treeView.Nodes[0].Tag;
163        if (!type.ContainsGenericParameters) {
164          myItem = (IItem)Activator.CreateInstance(type);
165          this.DialogResult = DialogResult.OK;
166          this.Close();
167        }
168      }
169    }
170  }
171}
Note: See TracBrowser for help on using the repository browser.