Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.OptimizationFrontend/3.3/AvailableOperatorsForm.cs @ 4062

Last change on this file since 4062 was 2744, checked in by epitzer, 15 years ago

update to new PluginInfrastructure (#802)

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.Linq;
29using System.Text;
30using System.Windows.Forms;
31using HeuristicLab.PluginInfrastructure;
32using HeuristicLab.Core;
33using HeuristicLab.PluginInfrastructure.Manager;
34
35namespace HeuristicLab.OptimizationFrontend {
36  public partial class AvailableOperatorsForm : 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    public AvailableOperatorsForm() {
54      InitializeComponent();
55      operatorLibrary = null;
56      TreeNodeSorter nodeSorter = new TreeNodeSorter();
57      operatorLibraryOperatorsTreeView.TreeViewNodeSorter = nodeSorter;
58      builtinOperatorsTreeView.TreeViewNodeSorter = nodeSorter;
59
60      foreach(PluginDescription plugin in ApplicationManager.Manager.Plugins) {
61        TreeNode pluginItem = new TreeNode();
62        pluginItem.Text = plugin.Name;
63        pluginItem.Tag = plugin;
64
65        Type[] operators = ApplicationManager.Manager.GetTypes(typeof(IOperator), plugin).ToArray();
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.