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 | using HeuristicLab.PluginInfrastructure.Manager;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Core {
|
---|
34 | /// <summary>
|
---|
35 | /// A dialog to select an operator out of a library.
|
---|
36 | /// </summary>
|
---|
37 | public partial class ChooseOperatorDialog : Form {
|
---|
38 | #region Inner Class TreeNodeSorter
|
---|
39 | private class TreeNodeSorter : IComparer {
|
---|
40 | public int Compare(object x, object y) {
|
---|
41 | TreeNode tx = x as TreeNode;
|
---|
42 | TreeNode ty = y as TreeNode;
|
---|
43 |
|
---|
44 | int result = string.Compare(tx.Text, ty.Text);
|
---|
45 | if (result == 0)
|
---|
46 | result = tx.Index.CompareTo(ty.Index);
|
---|
47 | return result;
|
---|
48 | }
|
---|
49 | }
|
---|
50 | #endregion
|
---|
51 |
|
---|
52 | private IOperatorLibrary operatorLibrary;
|
---|
53 |
|
---|
54 | private IOperator myOperator;
|
---|
55 | /// <summary>
|
---|
56 | /// Gets the selected operator.
|
---|
57 | /// </summary>
|
---|
58 | public IOperator Operator {
|
---|
59 | get { return myOperator; }
|
---|
60 | }
|
---|
61 |
|
---|
62 | /// <summary>
|
---|
63 | /// Initializes a new instance of <see cref="ChooseOperatorDialog"/>.
|
---|
64 | /// </summary>
|
---|
65 | public ChooseOperatorDialog() {
|
---|
66 | InitializeComponent();
|
---|
67 | operatorLibrary = null;
|
---|
68 | myOperator = null;
|
---|
69 | TreeNodeSorter nodeSorter = new TreeNodeSorter();
|
---|
70 | operatorLibraryOperatorsTreeView.TreeViewNodeSorter = nodeSorter;
|
---|
71 | builtinOperatorsTreeView.TreeViewNodeSorter = nodeSorter;
|
---|
72 |
|
---|
73 | foreach(PluginDescription plugin in ApplicationManager.Manager.Plugins) {
|
---|
74 | TreeNode pluginItem = new TreeNode();
|
---|
75 | pluginItem.Text = plugin.Name;
|
---|
76 | pluginItem.Tag = plugin;
|
---|
77 |
|
---|
78 | foreach(Type type in ApplicationManager.Manager.GetTypes(typeof(IOperator), plugin)) {
|
---|
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 | }
|
---|