Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.AdvancedOptimizationFrontend/3.3/AvailableOperatorsForm.cs @ 2257

Last change on this file since 2257 was 1529, checked in by gkronber, 16 years ago

Moved source files of plugins AdvancedOptimizationFrontEnd ... Grid into version-specific sub-folders. #576

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.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      DiscoveryService discoveryService = new DiscoveryService();
66      PluginInfo[] plugins = discoveryService.Plugins;
67      foreach(PluginInfo plugin in plugins) {
68        TreeNode pluginItem = new TreeNode();
69        pluginItem.Text = plugin.Name;
70        pluginItem.Tag = plugin;
71
72        Type[] operators = discoveryService.GetTypes(typeof(IOperator), plugin);
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.