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