Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.AdvancedOptimizationFrontend/3.3/AvailableOperatorsForm.cs @ 2906

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

update to new PluginInfrastructure (#802)

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