Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Operator Architecture Refactoring/HeuristicLab.AdvancedOptimizationFrontend/AvailableOperatorsForm.cs @ 630

Last change on this file since 630 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 WeifenLuo.WinFormsUI.Docking;
31using HeuristicLab.PluginInfrastructure;
32using HeuristicLab.Core;
33
34namespace HeuristicLab.AdvancedOptimizationFrontend {
35  public partial class AvailableOperatorsForm : DockContent {
36    #region Inner Class TreeNodeSorter
37    private class TreeNodeSorter : IComparer {
38      public int Compare(object x, object y) {
39        TreeNode tx = x as TreeNode;
40        TreeNode ty = y as TreeNode;
41
42        int result = string.Compare(tx.Text, ty.Text);
43        if (result == 0)
44          result = tx.Index.CompareTo(ty.Index);
45        return result;
46      }
47    }
48    #endregion
49
50    private IOperatorLibrary operatorLibrary;
51
52    public AvailableOperatorsForm() {
53      InitializeComponent();
54      operatorLibrary = null;
55      TreeNodeSorter nodeSorter = new TreeNodeSorter();
56      operatorLibraryOperatorsTreeView.TreeViewNodeSorter = nodeSorter;
57      builtinOperatorsTreeView.TreeViewNodeSorter = nodeSorter;
58
59      DiscoveryService discoveryService = new DiscoveryService();
60      PluginInfo[] plugins = discoveryService.Plugins;
61      foreach(PluginInfo plugin in plugins) {
62        TreeNode pluginItem = new TreeNode();
63        pluginItem.Text = plugin.Name;
64        pluginItem.Tag = plugin;
65
66        Type[] operators = discoveryService.GetTypes(typeof(IOperator), plugin);
67        foreach(Type type in operators) {
68          if(!type.IsAbstract) {
69            TreeNode operatorItem = new TreeNode();
70            operatorItem.Text = type.Name;
71            operatorItem.Tag = type;
72            pluginItem.Nodes.Add(operatorItem);
73          }
74        }
75        // add plugin node only if it contains operators
76        if(pluginItem.Nodes.Count > 0) {
77          builtinOperatorsTreeView.Nodes.Add(pluginItem);
78        }
79      }
80      if (builtinOperatorsTreeView.Nodes.Count == 0) {
81        builtinOperatorsTreeView.Enabled = false;
82        builtinOperatorsTreeView.Nodes.Add("No operators available");
83      }
84      builtinOperatorsTreeView.Sort();
85    }
86
87    private void UpdateOperatorsTreeView() {
88      operatorLibraryOperatorsTreeView.Nodes.Clear();
89      operatorLibraryOperatorsTreeView.Nodes.Add(CreateTreeNode(operatorLibrary.Group));
90      operatorLibraryOperatorsTreeView.Sort();
91    }
92
93    private TreeNode CreateTreeNode(IOperatorGroup group) {
94      TreeNode node = new TreeNode();
95      node.Text = group.Name;
96      node.ForeColor = Color.LightGray;
97      node.Tag = group;
98
99      foreach (IOperator op in group.Operators)
100        node.Nodes.Add(CreateTreeNode(op));
101      foreach (IOperatorGroup subGroup in group.SubGroups)
102        node.Nodes.Add(CreateTreeNode(subGroup));
103      return node;
104    }
105    private TreeNode CreateTreeNode(IOperator op) {
106      TreeNode node = new TreeNode();
107      node.Text = op.Name;
108      node.ToolTipText = op.GetType().Name;
109      node.Tag = op;
110      return node;
111    }
112
113    #region Selection Events
114    private void operatorsTreeView_AfterSelect(object sender, TreeViewEventArgs e) {
115      operatorLibraryOperatorsDescriptionTextBox.Text = "";
116      if ((operatorLibraryOperatorsTreeView.SelectedNode != null) && (operatorLibraryOperatorsTreeView.SelectedNode.Tag is IOperator))
117        operatorLibraryOperatorsDescriptionTextBox.Text = ((IOperator)operatorLibraryOperatorsTreeView.SelectedNode.Tag).Description;
118    }
119    private void builtinOperatorsTreeView_AfterSelect(object sender, TreeViewEventArgs e) {
120      builtinOperatorsDescriptionTextBox.Text = "";
121      if ((builtinOperatorsTreeView.SelectedNode != null) && (builtinOperatorsTreeView.SelectedNode.Tag is Type))
122        builtinOperatorsDescriptionTextBox.Text = ((IOperator)Activator.CreateInstance((Type)builtinOperatorsTreeView.SelectedNode.Tag)).Description;
123    }
124    #endregion
125
126    #region Click Events
127    private void loadButton_Click(object sender, EventArgs e) {
128      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
129        operatorLibrary = (IOperatorLibrary)PersistenceManager.Load(openFileDialog.FileName);
130        operatorLibraryTextBox.Text = openFileDialog.FileName;
131        toolTip.SetToolTip(operatorLibraryTextBox, openFileDialog.FileName);
132        operatorLibraryOperatorsGroupBox.Enabled = true;
133        UpdateOperatorsTreeView();
134      }
135    }
136    private void closeButton_Click(object sender, EventArgs e) {
137      this.Close();
138    }
139    #endregion
140
141    #region Drag and Drop Events
142    private void operatorLibraryOperatorsTreeView_ItemDrag(object sender, ItemDragEventArgs e) {
143      TreeNode node = e.Item as TreeNode;
144      if ((node != null) && (node.Tag is IOperator)) {
145        IOperator op = (IOperator)node.Tag;
146        op = (IOperator)op.Clone();
147        DataObject data = new DataObject();
148        data.SetData("IOperator", op);
149        data.SetData("DragSource", operatorLibraryOperatorsTreeView);
150        DoDragDrop(data, DragDropEffects.Copy);
151      }
152    }
153    private void builtinOperatorsTreeView_ItemDrag(object sender, ItemDragEventArgs e) {
154      TreeNode node = e.Item as TreeNode;
155      if ((node != null) && (node.Tag is Type)) {
156        IOperator op = (IOperator)Activator.CreateInstance((Type)node.Tag);
157        DataObject data = new DataObject();
158        data.SetData("IOperator", op);
159        data.SetData("DragSource", builtinOperatorsTreeView);
160        DoDragDrop(data, DragDropEffects.Copy);
161      }
162    }
163    #endregion
164  }
165}
Note: See TracBrowser for help on using the repository browser.