Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HL-3.2-MonoMigration/HeuristicLab.Core/OperatorLibraryEditor.cs @ 1104

Last change on this file since 1104 was 638, checked in by gkronber, 16 years ago

created a branch for changes needed to run HL3 on Mono 2.0
(ticket #298)

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