Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.OptimizationFrontend/3.2/AvailableOperatorsForm.cs @ 2806

Last change on this file since 2806 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.2 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      foreach(IPluginDescription plugin in ApplicationManager.Manager.Plugins) {
59        TreeNode pluginItem = new TreeNode();
60        pluginItem.Text = plugin.Name;
61        pluginItem.Tag = plugin;
62
63        foreach(Type type in ApplicationManager.Manager.GetTypes(typeof(IOperator), plugin)) {
64          if(!type.IsAbstract) {
65            TreeNode operatorItem = new TreeNode();
66            operatorItem.Text = type.Name;
67            operatorItem.Tag = type;
68            pluginItem.Nodes.Add(operatorItem);
69          }
70        }
71        // add plugin node only if it contains operators
72        if(pluginItem.Nodes.Count > 0) {
73          builtinOperatorsTreeView.Nodes.Add(pluginItem);
74        }
75      }
76      if (builtinOperatorsTreeView.Nodes.Count == 0) {
77        builtinOperatorsTreeView.Enabled = false;
78        builtinOperatorsTreeView.Nodes.Add("No operators available");
79      }
80      builtinOperatorsTreeView.Sort();
81    }
82
83    private void UpdateOperatorsTreeView() {
84      operatorLibraryOperatorsTreeView.Nodes.Clear();
85      operatorLibraryOperatorsTreeView.Nodes.Add(CreateTreeNode(operatorLibrary.Group));
86      operatorLibraryOperatorsTreeView.Sort();
87    }
88
89    private TreeNode CreateTreeNode(IOperatorGroup group) {
90      TreeNode node = new TreeNode();
91      node.Text = group.Name;
92      node.ForeColor = Color.LightGray;
93      node.Tag = group;
94
95      foreach (IOperator op in group.Operators)
96        node.Nodes.Add(CreateTreeNode(op));
97      foreach (IOperatorGroup subGroup in group.SubGroups)
98        node.Nodes.Add(CreateTreeNode(subGroup));
99      return node;
100    }
101    private TreeNode CreateTreeNode(IOperator op) {
102      TreeNode node = new TreeNode();
103      node.Text = op.Name;
104      node.ToolTipText = op.GetType().Name;
105      node.Tag = op;
106      return node;
107    }
108
109    #region Selection Events
110    private void operatorsTreeView_AfterSelect(object sender, TreeViewEventArgs e) {
111      operatorLibraryOperatorsDescriptionTextBox.Text = "";
112      if ((operatorLibraryOperatorsTreeView.SelectedNode != null) && (operatorLibraryOperatorsTreeView.SelectedNode.Tag is IOperator))
113        operatorLibraryOperatorsDescriptionTextBox.Text = ((IOperator)operatorLibraryOperatorsTreeView.SelectedNode.Tag).Description;
114    }
115    private void builtinOperatorsTreeView_AfterSelect(object sender, TreeViewEventArgs e) {
116      builtinOperatorsDescriptionTextBox.Text = "";
117      if ((builtinOperatorsTreeView.SelectedNode != null) && (builtinOperatorsTreeView.SelectedNode.Tag is Type))
118        builtinOperatorsDescriptionTextBox.Text = ((IOperator)Activator.CreateInstance((Type)builtinOperatorsTreeView.SelectedNode.Tag)).Description;
119    }
120    #endregion
121
122    #region Click Events
123    private void loadButton_Click(object sender, EventArgs e) {
124      if (openFileDialog.ShowDialog(this) == DialogResult.OK) {
125        operatorLibrary = (IOperatorLibrary)PersistenceManager.Load(openFileDialog.FileName);
126        operatorLibraryTextBox.Text = openFileDialog.FileName;
127        toolTip.SetToolTip(operatorLibraryTextBox, openFileDialog.FileName);
128        operatorLibraryOperatorsGroupBox.Enabled = true;
129        UpdateOperatorsTreeView();
130      }
131    }
132    private void closeButton_Click(object sender, EventArgs e) {
133      this.Close();
134    }
135    #endregion
136
137    #region Drag and Drop Events
138    private void operatorLibraryOperatorsTreeView_ItemDrag(object sender, ItemDragEventArgs e) {
139      TreeNode node = e.Item as TreeNode;
140      if ((node != null) && (node.Tag is IOperator)) {
141        IOperator op = (IOperator)node.Tag;
142        op = (IOperator)op.Clone();
143        DataObject data = new DataObject();
144        data.SetData("IOperator", op);
145        data.SetData("DragSource", operatorLibraryOperatorsTreeView);
146        DoDragDrop(data, DragDropEffects.Copy);
147      }
148    }
149    private void builtinOperatorsTreeView_ItemDrag(object sender, ItemDragEventArgs e) {
150      TreeNode node = e.Item as TreeNode;
151      if ((node != null) && (node.Tag is Type)) {
152        IOperator op = (IOperator)Activator.CreateInstance((Type)node.Tag);
153        DataObject data = new DataObject();
154        data.SetData("IOperator", op);
155        data.SetData("DragSource", builtinOperatorsTreeView);
156        DoDragDrop(data, DragDropEffects.Copy);
157      }
158    }
159    #endregion
160  }
161}
Note: See TracBrowser for help on using the repository browser.