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 | /// <summary>
|
---|
34 | /// A dialog to select an operator out of a library.
|
---|
35 | /// </summary>
|
---|
36 | public partial class ChooseOperatorDialog : 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 | private IOperatorLibrary operatorLibrary;
|
---|
52 |
|
---|
53 | private IOperator myOperator;
|
---|
54 | /// <summary>
|
---|
55 | /// Gets the selected operator.
|
---|
56 | /// </summary>
|
---|
57 | public IOperator Operator {
|
---|
58 | get { return myOperator; }
|
---|
59 | }
|
---|
60 |
|
---|
61 | /// <summary>
|
---|
62 | /// Initializes a new instance of <see cref="ChooseOperatorDialog"/>.
|
---|
63 | /// </summary>
|
---|
64 | public ChooseOperatorDialog() {
|
---|
65 | InitializeComponent();
|
---|
66 | operatorLibrary = null;
|
---|
67 | myOperator = null;
|
---|
68 | TreeNodeSorter nodeSorter = new TreeNodeSorter();
|
---|
69 | operatorLibraryOperatorsTreeView.TreeViewNodeSorter = nodeSorter;
|
---|
70 | builtinOperatorsTreeView.TreeViewNodeSorter = nodeSorter;
|
---|
71 |
|
---|
72 | foreach (IPluginDescription plugin in ApplicationManager.Manager.Plugins) {
|
---|
73 | TreeNode pluginItem = new TreeNode();
|
---|
74 | pluginItem.Text = plugin.Name;
|
---|
75 | pluginItem.Tag = plugin;
|
---|
76 |
|
---|
77 | IEnumerable<Type> operators = ApplicationManager.Manager.GetTypes(typeof(IOperator), plugin);
|
---|
78 | foreach (Type type in operators) {
|
---|
79 | if (!type.IsAbstract) {
|
---|
80 | TreeNode operatorItem = new TreeNode();
|
---|
81 | operatorItem.Text = type.Name;
|
---|
82 | operatorItem.Tag = type;
|
---|
83 | pluginItem.Nodes.Add(operatorItem);
|
---|
84 | }
|
---|
85 | }
|
---|
86 | // add plugin node only if it contains operators
|
---|
87 | if (pluginItem.Nodes.Count > 0) {
|
---|
88 | builtinOperatorsTreeView.Nodes.Add(pluginItem);
|
---|
89 | }
|
---|
90 | }
|
---|
91 | if (builtinOperatorsTreeView.Nodes.Count == 0) {
|
---|
92 | builtinOperatorsTreeView.Enabled = false;
|
---|
93 | builtinOperatorsTreeView.Nodes.Add("No operators available");
|
---|
94 | }
|
---|
95 | builtinOperatorsTreeView.Sort();
|
---|
96 | }
|
---|
97 |
|
---|
98 | private void UpdateOperatorsTreeView() {
|
---|
99 | operatorLibraryOperatorsTreeView.Nodes.Clear();
|
---|
100 | operatorLibraryOperatorsTreeView.Nodes.Add(CreateTreeNode(operatorLibrary.Group));
|
---|
101 | operatorLibraryOperatorsTreeView.Sort();
|
---|
102 | }
|
---|
103 |
|
---|
104 | private TreeNode CreateTreeNode(IOperatorGroup group) {
|
---|
105 | TreeNode node = new TreeNode();
|
---|
106 | node.Text = group.Name;
|
---|
107 | node.ForeColor = Color.LightGray;
|
---|
108 | node.Tag = group;
|
---|
109 |
|
---|
110 | foreach (IOperator op in group.Operators)
|
---|
111 | node.Nodes.Add(CreateTreeNode(op));
|
---|
112 | foreach (IOperatorGroup subGroup in group.SubGroups)
|
---|
113 | node.Nodes.Add(CreateTreeNode(subGroup));
|
---|
114 | return node;
|
---|
115 | }
|
---|
116 | private TreeNode CreateTreeNode(IOperator op) {
|
---|
117 | TreeNode node = new TreeNode();
|
---|
118 | node.Text = op.Name;
|
---|
119 | node.ToolTipText = op.GetType().Name;
|
---|
120 | node.Tag = op;
|
---|
121 | return node;
|
---|
122 | }
|
---|
123 |
|
---|
124 | #region Form Events
|
---|
125 | private void ChooseOperatorDialog_Shown(object sender, EventArgs e) {
|
---|
126 | operatorLibraryOperatorsTreeView.SelectedNode = null;
|
---|
127 | operatorLibraryOperatorsDescriptionTextBox.Text = "";
|
---|
128 | myOperator = null;
|
---|
129 | builtinOperatorsTreeView.SelectedNode = null;
|
---|
130 | builtinOperatorsDescriptionTextBox.Text = "";
|
---|
131 | okButton.Enabled = false;
|
---|
132 | }
|
---|
133 | #endregion
|
---|
134 |
|
---|
135 | #region Selection Events
|
---|
136 | private void operatorsTreeView_AfterSelect(object sender, TreeViewEventArgs e) {
|
---|
137 | okButton.Enabled = false;
|
---|
138 | myOperator = null;
|
---|
139 | operatorLibraryOperatorsDescriptionTextBox.Text = "";
|
---|
140 | if ((operatorLibraryOperatorsTreeView.SelectedNode != null) && (operatorLibraryOperatorsTreeView.SelectedNode.Tag is IOperator)) {
|
---|
141 | myOperator = (IOperator)operatorLibraryOperatorsTreeView.SelectedNode.Tag;
|
---|
142 | myOperator = (IOperator)myOperator.Clone();
|
---|
143 | operatorLibraryOperatorsDescriptionTextBox.Text = myOperator.Description;
|
---|
144 | okButton.Enabled = true;
|
---|
145 | }
|
---|
146 | }
|
---|
147 | private void builtinOperatorsTreeView_AfterSelect(object sender, TreeViewEventArgs e) {
|
---|
148 | okButton.Enabled = false;
|
---|
149 | myOperator = null;
|
---|
150 | builtinOperatorsDescriptionTextBox.Text = "";
|
---|
151 | if ((builtinOperatorsTreeView.SelectedNode != null) && (builtinOperatorsTreeView.SelectedNode.Tag is Type)) {
|
---|
152 | okButton.Enabled = true;
|
---|
153 | myOperator = (IOperator)Activator.CreateInstance((Type)builtinOperatorsTreeView.SelectedNode.Tag);
|
---|
154 | builtinOperatorsDescriptionTextBox.Text = myOperator.Description;
|
---|
155 | }
|
---|
156 | }
|
---|
157 | #endregion
|
---|
158 |
|
---|
159 | #region Click Events
|
---|
160 | private void loadButton_Click(object sender, EventArgs e) {
|
---|
161 | if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
|
---|
162 | operatorLibrary = (IOperatorLibrary)PersistenceManager.Load(openFileDialog.FileName);
|
---|
163 | operatorLibraryTextBox.Text = openFileDialog.FileName;
|
---|
164 | toolTip.SetToolTip(operatorLibraryTextBox, openFileDialog.FileName);
|
---|
165 | operatorLibraryOperatorsGroupBox.Enabled = true;
|
---|
166 | UpdateOperatorsTreeView();
|
---|
167 | }
|
---|
168 | }
|
---|
169 | private void okButton_Click(object sender, EventArgs e) {
|
---|
170 | if (Operator != null) {
|
---|
171 | this.DialogResult = DialogResult.OK;
|
---|
172 | this.Close();
|
---|
173 | }
|
---|
174 | }
|
---|
175 | private void treeView_DoubleClick(object sender, EventArgs e) {
|
---|
176 | if (Operator != null) {
|
---|
177 | this.DialogResult = DialogResult.OK;
|
---|
178 | this.Close();
|
---|
179 | }
|
---|
180 | }
|
---|
181 | #endregion
|
---|
182 | }
|
---|
183 | }
|
---|