Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.AdvancedOptimizationFrontend/3.2/AvailableOperatorsForm.cs @ 3193

Last change on this file since 3193 was 2591, checked in by gkronber, 15 years ago

Copied refactored plugin infrastructure from branch and merged changeset r2586:2589 from branch into the trunk. #799

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