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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections;
|
---|
24 | using System.Collections.Generic;
|
---|
25 | using System.ComponentModel;
|
---|
26 | using System.Data;
|
---|
27 | using System.Drawing;
|
---|
28 | using System.Text;
|
---|
29 | using System.Windows.Forms;
|
---|
30 | using HeuristicLab.PluginInfrastructure;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Core {
|
---|
33 | public partial class ChooseTypeDialog : 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 Type myType;
|
---|
54 | public Type Type {
|
---|
55 | get { return myType; }
|
---|
56 | }
|
---|
57 |
|
---|
58 | public ChooseTypeDialog()
|
---|
59 | : this(typeof(IItem)) {
|
---|
60 | }
|
---|
61 |
|
---|
62 | public ChooseTypeDialog(Type baseType) {
|
---|
63 | InitializeComponent();
|
---|
64 |
|
---|
65 | treeView.TreeViewNodeSorter = new TreeNodeSorter();
|
---|
66 |
|
---|
67 | if (baseType == null) {
|
---|
68 | baseType = 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(baseType, plugin);
|
---|
77 | foreach (Type type in types) {
|
---|
78 | TreeNode itemNode = new TreeNode();
|
---|
79 | itemNode.Text = type.Name;
|
---|
80 | itemNode.Tag = type;
|
---|
81 | pluginNode.Nodes.Add(itemNode);
|
---|
82 | }
|
---|
83 | if (pluginNode.Nodes.Count > 0) {
|
---|
84 | treeView.Nodes.Add(pluginNode);
|
---|
85 | }
|
---|
86 | }
|
---|
87 | if (treeView.Nodes.Count == 0) {
|
---|
88 | treeView.Enabled = false;
|
---|
89 | treeView.Nodes.Add(new TreeNode("No item types available"));
|
---|
90 | }
|
---|
91 | okButton.Enabled = false;
|
---|
92 |
|
---|
93 | if (treeView.Nodes.Count == 1) {
|
---|
94 | TreeNodeCollection items = treeView.Nodes[0].Nodes;
|
---|
95 | treeView.Nodes.Clear();
|
---|
96 | foreach (TreeNode i in items) {
|
---|
97 | treeView.Nodes.Add(i);
|
---|
98 | }
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | private void okButton_Click(object sender, EventArgs e) {
|
---|
103 | myType = (Type)treeView.SelectedNode.Tag;
|
---|
104 | this.DialogResult = DialogResult.OK;
|
---|
105 | this.Close();
|
---|
106 | }
|
---|
107 |
|
---|
108 | private void treeView_DoubleClick(object sender, EventArgs e) {
|
---|
109 | // plugin groups have a null-tag
|
---|
110 | if ((treeView.SelectedNode != null) && (treeView.SelectedNode.Tag != null)) {
|
---|
111 | myType = (Type)treeView.SelectedNode.Tag;
|
---|
112 | this.DialogResult = DialogResult.OK;
|
---|
113 | this.Close();
|
---|
114 | }
|
---|
115 | }
|
---|
116 |
|
---|
117 | private void treeView_AfterSelect(object sender, TreeViewEventArgs e) {
|
---|
118 | okButton.Enabled = false;
|
---|
119 | if ((treeView.SelectedNode != null) && (treeView.SelectedNode.Tag != null))
|
---|
120 | okButton.Enabled = true;
|
---|
121 | }
|
---|
122 |
|
---|
123 | private void ChooseTypeDialog_Load(object sender, EventArgs e) {
|
---|
124 | myType = null;
|
---|
125 | if (treeView.Nodes.Count == 1) {
|
---|
126 | myType = (Type)treeView.Nodes[0].Tag;
|
---|
127 | this.DialogResult = DialogResult.OK;
|
---|
128 | this.Close();
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|
132 | }
|
---|