Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HL-3.2-MonoMigration/HeuristicLab.Core/ChooseOperatorDialog.cs @ 2396

Last change on this file since 2396 was 2, checked in by swagner, 17 years ago

Added HeuristicLab 3.0 sources from former SVN repository at revision 52

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