Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/OperatorLibraryEditor.cs @ 833

Last change on this file since 833 was 776, checked in by vdorfer, 16 years ago

Created API documentation for HeuristicLab.Core namespace (#331)

File size: 12.4 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.Drawing;
27using System.Data;
28using System.Text;
29using System.Windows.Forms;
30using HeuristicLab.PluginInfrastructure;
31
32namespace HeuristicLab.Core {
33  /// <summary>
34  /// Visual representation of the class <see cref="IOperatorLibrary"/>.
35  /// </summary>
36  public partial class OperatorLibraryEditor : EditorBase {
37    private ChooseOperatorDialog chooseOperatorDialog;
38
39    /// <summary>
40    /// Gets or sets the operator library that should be displayed.
41    /// </summary>
42    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="EditorBase"/>.</remarks>
43    public IOperatorLibrary OperatorLibrary {
44      get { return (IOperatorLibrary)Item; }
45      set { base.Item = value; }
46    }
47
48    /// <summary>
49    /// Initializes a new instance of <see cref="OperatorLibraryEditor"/> with caption
50    /// <c>Operator Library</c>.
51    /// </summary>
52    public OperatorLibraryEditor() {
53      InitializeComponent();
54      operatorsTreeView.TreeViewNodeSorter = new NodeSorter();
55      Caption = "Operator Library";
56    }
57    /// <summary>
58    /// Initializes a new instance of <see cref="OperatorLibraryEditor"/> with the
59    /// specified <paramref name="operatorLibrary"/>.
60    /// </summary>
61    /// <param name="operatorLibrary">The operator library to display.</param>
62    public OperatorLibraryEditor(IOperatorLibrary operatorLibrary)
63      : this() {
64      OperatorLibrary = operatorLibrary;
65    }
66
67    private class NodeSorter : IComparer {
68      public int Compare(object x, object y) {
69        TreeNode tx = x as TreeNode;
70        TreeNode ty = y as TreeNode;
71
72        int result = string.Compare(tx.Text, ty.Text);
73        if (result == 0)
74          result = tx.Index.CompareTo(ty.Index);
75        return result;
76      }
77    }
78
79    /// <summary>
80    /// Updates all controls with the latest data of the model.
81    /// </summary>
82    /// <remarks>Calls <see cref="EditorBase.UpdateControls"/> of base class <see cref="EditorBase"/>.</remarks>
83    protected override void UpdateControls() {
84      base.UpdateControls();
85      operatorsTreeView.Nodes.Clear();
86      addGroupButton.Enabled = false;
87      addOperatorButton.Enabled = false;
88      removeButton.Enabled = false;
89      if (OperatorLibrary == null) {
90        Caption = "Operator Library";
91        operatorsGroupBox.Enabled = false;
92      } else {
93        Caption = OperatorLibrary.Group.Name + " (" + OperatorLibrary.GetType().Name + ")";
94        operatorsGroupBox.Enabled = true;
95        operatorsTreeView.Nodes.Add(CreateTreeNode(OperatorLibrary.Group));
96        operatorsTreeView.Sort();
97      }
98    }
99
100    private TreeNode CreateTreeNode(IOperatorGroup group) {
101      TreeNode node = new TreeNode();
102      node.Text = group.Name;
103      node.ForeColor = Color.LightGray;
104      node.Tag = group;
105
106      foreach (IOperator op in group.Operators)
107        node.Nodes.Add(CreateTreeNode(op));
108      foreach (IOperatorGroup subGroup in group.SubGroups)
109        node.Nodes.Add(CreateTreeNode(subGroup));
110      return node;
111    }
112    private TreeNode CreateTreeNode(IOperator op) {
113      TreeNode node = new TreeNode();
114      node.Text = op.Name;
115      node.ToolTipText = op.GetType().Name;
116      node.Tag = op;
117      return node;
118    }
119    private void RemoveTreeNode(TreeNode node) {
120      if (node.Tag is IOperatorGroup) {
121        IOperatorGroup group = node.Tag as IOperatorGroup;
122        IOperatorGroup parent = node.Parent.Tag as IOperatorGroup;
123        parent.RemoveSubGroup(group);
124      } else if (node.Tag is IOperator) {
125        IOperator op = node.Tag as IOperator;
126        IOperatorGroup group = node.Parent.Tag as IOperatorGroup;
127        group.RemoveOperator(op);
128      }
129      node.Remove();
130      EnableDisableButtons();
131    }
132
133    private void EnableDisableButtons() {
134      addGroupButton.Enabled = false;
135      addOperatorButton.Enabled = false;
136      removeButton.Enabled = false;
137      operatorsTreeView.Focus();
138
139      if (operatorsTreeView.SelectedNode != null) {
140        if (operatorsTreeView.SelectedNode.Parent != null)
141          removeButton.Enabled = true;
142        if (operatorsTreeView.SelectedNode.Tag is IOperatorGroup) {
143          addGroupButton.Enabled = true;
144          addOperatorButton.Enabled = true;
145        }
146      }
147    }
148
149    private void MergeOperatorLibrary(IOperatorGroup src, IOperatorGroup dest) {
150      foreach(IOperator op in src.Operators) {
151        if(!Contains(dest, op)) {
152          dest.AddOperator(op);
153        }
154      }
155
156      foreach(OperatorGroup group in src.SubGroups) {
157        bool mergedIntoExistingGroup = false;
158        // try to find a group in dest with matching name
159        foreach(OperatorGroup destGroup in dest.SubGroups) {
160          if(group.Name == destGroup.Name) {
161            MergeOperatorLibrary(group, destGroup);
162            mergedIntoExistingGroup = true;
163            break;
164          }
165        }
166        if(!mergedIntoExistingGroup) {
167          OperatorGroup newGroup = new OperatorGroup();
168          newGroup.Name = group.Name;
169          dest.AddSubGroup(newGroup);
170          MergeOperatorLibrary(group, newGroup);
171        }
172      }
173    }
174
175    private bool Contains(IOperatorGroup group, IOperator op) {
176      foreach(IOperator destOp in group.Operators) {
177        if(op.Name == destOp.Name) {
178          return true;
179        }
180      }
181      return false;
182    }
183
184
185    #region TreeView Events
186    private void operatorsTreeView_AfterSelect(object sender, TreeViewEventArgs e) {
187      EnableDisableButtons();
188    }
189    private void operatorsTreeView_AfterLabelEdit(object sender, NodeLabelEditEventArgs e) {
190      e.CancelEdit = false;
191
192      string name = e.Label;
193      if (name != null) {
194        if (e.Node.Tag is IOperatorGroup)
195          ((IOperatorGroup)e.Node.Tag).Name = name;
196        else if (e.Node.Tag is IOperator)
197          ((IOperator)e.Node.Tag).Name = name;
198      }
199    }
200    private void operatorsTreeView_DoubleClick(object sender, EventArgs e) {
201      if (operatorsTreeView.SelectedNode != null) {
202        IOperator op = operatorsTreeView.SelectedNode.Tag as IOperator;
203        if (op != null) {
204          IView view = op.CreateView();
205          if (view != null)
206            PluginManager.ControlManager.ShowControl(view);
207        }
208      }
209    }
210    #endregion
211
212    #region Button Events
213    private void addOperatorButton_Click(object sender, EventArgs e) {
214      if (chooseOperatorDialog == null) chooseOperatorDialog = new ChooseOperatorDialog();
215      if (chooseOperatorDialog.ShowDialog(this) == DialogResult.OK) {
216        TreeNode node = operatorsTreeView.SelectedNode;
217        IOperatorGroup group = node.Tag as IOperatorGroup;
218        group.AddOperator(chooseOperatorDialog.Operator);
219        node.Nodes.Add(CreateTreeNode(chooseOperatorDialog.Operator));
220        operatorsTreeView.Sort();
221        EnableDisableButtons();
222      }
223    }
224    private void addGroupButton_Click(object sender, EventArgs e) {
225      TreeNode node = operatorsTreeView.SelectedNode;
226      IOperatorGroup newGroup = new OperatorGroup();
227      IOperatorGroup group = node.Tag as OperatorGroup;
228      group.AddSubGroup(newGroup);
229      node.Nodes.Add(CreateTreeNode(newGroup));
230      operatorsTreeView.Sort();
231      EnableDisableButtons();
232    }
233    private void removeButton_Click(object sender, EventArgs e) {
234      TreeNode node = operatorsTreeView.SelectedNode;
235      RemoveTreeNode(node);
236    }
237    private void importButton_Click(object sender, EventArgs e) {
238      if(openFileDialog.ShowDialog() == DialogResult.OK) {
239        IOperatorLibrary loadedLibrary = (PersistenceManager.Load(openFileDialog.FileName) as IOperatorLibrary);
240        if(loadedLibrary == null) {
241          Auxiliary.ShowErrorMessageBox("The selected file does not contain an operator library");
242        } else {
243          MergeOperatorLibrary(loadedLibrary.Group, OperatorLibrary.Group);
244          Refresh();
245        }
246      }
247    }
248    #endregion
249
250    #region Key Events
251    private void operatorsTreeView_KeyDown(object sender, KeyEventArgs e) {
252      TreeNode node = operatorsTreeView.SelectedNode;
253      if ((e.KeyCode == Keys.Delete) && (node != null) && (node.Parent != null))
254        RemoveTreeNode(operatorsTreeView.SelectedNode);
255      if ((e.KeyCode == Keys.F2) && (node != null))
256        node.BeginEdit();
257    }
258    #endregion
259
260
261    #region Mouse Events
262    private void operatorsTreeView_MouseDown(object sender, MouseEventArgs e) {
263      if (e.Button != MouseButtons.Right) return;
264      TreeNode clickedNode = operatorsTreeView.GetNodeAt(e.X, e.Y);
265      if (clickedNode != null) {
266        operatorsTreeView.SelectedNode = clickedNode;
267        operatorsTreeView.Refresh();
268      }
269    }
270    #endregion
271
272
273    #region Context Menu Events
274    private void contextMenuStrip_Opening(object sender, CancelEventArgs e) {
275      viewToolStripMenuItem.Enabled = false;
276      if (operatorsTreeView.SelectedNode != null) {
277        IOperator op = operatorsTreeView.SelectedNode.Tag as IOperator;
278        if (op != null) {
279          IView view = op.CreateView();
280          if (view != null) {
281            viewToolStripMenuItem.Enabled = true;
282            viewToolStripMenuItem.Tag = view;
283          }
284        }
285      }
286    }
287    private void viewToolStripMenuItem_Click(object sender, EventArgs e) {
288      IView view = (IView)viewToolStripMenuItem.Tag;
289      PluginManager.ControlManager.ShowControl(view);
290    }
291    #endregion
292
293    #region Drag and Drop Events
294    private void operatorsTreeView_ItemDrag(object sender, ItemDragEventArgs e) {
295      TreeNode node = (TreeNode)e.Item;
296      IOperator op = node.Tag as IOperator;
297      if (op != null) {
298        DataObject data = new DataObject();
299        data.SetData("IOperator", op);
300        data.SetData("DragSource", operatorsTreeView);
301        DoDragDrop(data, DragDropEffects.Copy);
302      }
303    }
304    private void operatorsTreeView_DragEnter(object sender, DragEventArgs e) {
305      e.Effect = DragDropEffects.None;
306      if (e.Data.GetDataPresent("IOperator")) {
307        Point p = operatorsTreeView.PointToClient(new Point(e.X, e.Y));
308        TreeNode node = operatorsTreeView.GetNodeAt(p);
309        if ((node != null) && (node.Tag is IOperatorGroup))
310          e.Effect = DragDropEffects.Copy;
311      }
312    }
313    private void operatorsTreeView_DragOver(object sender, DragEventArgs e) {
314      e.Effect = DragDropEffects.None;
315      if (e.Data.GetDataPresent("IOperator")) {
316        Point p = operatorsTreeView.PointToClient(new Point(e.X, e.Y));
317        TreeNode node = operatorsTreeView.GetNodeAt(p);
318        if ((node != null) && (node.Tag is IOperatorGroup))
319          e.Effect = DragDropEffects.Copy;
320      }
321    }
322    private void operatorsTreeView_DragDrop(object sender, DragEventArgs e) {
323      if (e.Effect != DragDropEffects.None) {
324        if (e.Data.GetDataPresent("IOperator")) {
325          IOperator op = (IOperator)e.Data.GetData("IOperator");
326          Point p = operatorsTreeView.PointToClient(new Point(e.X, e.Y));
327          TreeNode node = operatorsTreeView.GetNodeAt(p);
328          if (node != null) {
329            op = (IOperator)op.Clone();
330
331            while (op.SubOperators.Count > 0)
332              op.RemoveSubOperator(0);
333
334            IOperatorGroup group = (IOperatorGroup)node.Tag;
335            group.AddOperator(op);
336            node.Nodes.Add(CreateTreeNode(op));
337            operatorsTreeView.Sort();
338            EnableDisableButtons();
339          }
340        }
341      }
342    }
343    #endregion
344
345  }
346}
Note: See TracBrowser for help on using the repository browser.