Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.0/sources/HeuristicLab.OptimizationFrontend/AvailableOperatorsForm.cs @ 5306

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

Added HeuristicLab 3.0 sources from former SVN repository at revision 52

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