Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.0/sources/HeuristicLab.Core/OperatorLibraryEditor.cs @ 12093

Last change on this file since 12093 was 2, checked in by swagner, 17 years ago

Added HeuristicLab 3.0 sources from former SVN repository at revision 52

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
165    #region TreeView Events
166    private void operatorsTreeView_AfterSelect(object sender, TreeViewEventArgs e) {
167      EnableDisableButtons();
168    }
169    private void operatorsTreeView_AfterLabelEdit(object sender, NodeLabelEditEventArgs e) {
170      e.CancelEdit = false;
171
172      string name = e.Label;
173      if (name != null) {
174        if (e.Node.Tag is IOperatorGroup)
175          ((IOperatorGroup)e.Node.Tag).Name = name;
176        else if (e.Node.Tag is IOperator)
177          ((IOperator)e.Node.Tag).Name = name;
178      }
179    }
180    private void operatorsTreeView_DoubleClick(object sender, EventArgs e) {
181      if (operatorsTreeView.SelectedNode != null) {
182        IOperator op = operatorsTreeView.SelectedNode.Tag as IOperator;
183        if (op != null) {
184          IView view = op.CreateView();
185          if (view != null)
186            PluginManager.ControlManager.ShowControl(view);
187        }
188      }
189    }
190    #endregion
191
192    #region Button Events
193    private void addOperatorButton_Click(object sender, EventArgs e) {
194      if (chooseOperatorDialog == null) chooseOperatorDialog = new ChooseOperatorDialog();
195      if (chooseOperatorDialog.ShowDialog(this) == DialogResult.OK) {
196        TreeNode node = operatorsTreeView.SelectedNode;
197        IOperatorGroup group = node.Tag as IOperatorGroup;
198        group.AddOperator(chooseOperatorDialog.Operator);
199        node.Nodes.Add(CreateTreeNode(chooseOperatorDialog.Operator));
200        operatorsTreeView.Sort();
201        EnableDisableButtons();
202      }
203    }
204    private void addGroupButton_Click(object sender, EventArgs e) {
205      TreeNode node = operatorsTreeView.SelectedNode;
206      IOperatorGroup newGroup = new OperatorGroup();
207      IOperatorGroup group = node.Tag as OperatorGroup;
208      group.AddSubGroup(newGroup);
209      node.Nodes.Add(CreateTreeNode(newGroup));
210      operatorsTreeView.Sort();
211      EnableDisableButtons();
212    }
213    private void removeButton_Click(object sender, EventArgs e) {
214      TreeNode node = operatorsTreeView.SelectedNode;
215      RemoveTreeNode(node);
216    }
217    private void importButton_Click(object sender, EventArgs e) {
218      if(openFileDialog.ShowDialog() == DialogResult.OK) {
219        IOperatorLibrary loadedLibrary = (PersistenceManager.Load(openFileDialog.FileName) as IOperatorLibrary);
220        if(loadedLibrary == null) {
221          Auxiliary.ShowErrorMessageBox("The selected file does not contain an operator library");
222        } else {
223          MergeOperatorLibrary(loadedLibrary.Group, OperatorLibrary.Group);
224          Refresh();
225        }
226      }
227    }
228    #endregion
229
230    #region Key Events
231    private void operatorsTreeView_KeyDown(object sender, KeyEventArgs e) {
232      TreeNode node = operatorsTreeView.SelectedNode;
233      if ((e.KeyCode == Keys.Delete) && (node != null) && (node.Parent != null))
234        RemoveTreeNode(operatorsTreeView.SelectedNode);
235      if ((e.KeyCode == Keys.F2) && (node != null))
236        node.BeginEdit();
237    }
238    #endregion
239
240
241    #region Mouse Events
242    private void operatorsTreeView_MouseDown(object sender, MouseEventArgs e) {
243      if (e.Button != MouseButtons.Right) return;
244      TreeNode clickedNode = operatorsTreeView.GetNodeAt(e.X, e.Y);
245      if (clickedNode != null) {
246        operatorsTreeView.SelectedNode = clickedNode;
247        operatorsTreeView.Refresh();
248      }
249    }
250    #endregion
251
252
253    #region Context Menu Events
254    private void contextMenuStrip_Opening(object sender, CancelEventArgs e) {
255      viewToolStripMenuItem.Enabled = false;
256      if (operatorsTreeView.SelectedNode != null) {
257        IOperator op = operatorsTreeView.SelectedNode.Tag as IOperator;
258        if (op != null) {
259          IView view = op.CreateView();
260          if (view != null) {
261            viewToolStripMenuItem.Enabled = true;
262            viewToolStripMenuItem.Tag = view;
263          }
264        }
265      }
266    }
267    private void viewToolStripMenuItem_Click(object sender, EventArgs e) {
268      IView view = (IView)viewToolStripMenuItem.Tag;
269      PluginManager.ControlManager.ShowControl(view);
270    }
271    #endregion
272
273    #region Drag and Drop Events
274    private void operatorsTreeView_ItemDrag(object sender, ItemDragEventArgs e) {
275      TreeNode node = (TreeNode)e.Item;
276      IOperator op = node.Tag as IOperator;
277      if (op != null) {
278        DataObject data = new DataObject();
279        data.SetData("IOperator", op);
280        data.SetData("DragSource", operatorsTreeView);
281        DoDragDrop(data, DragDropEffects.Copy);
282      }
283    }
284    private void operatorsTreeView_DragEnter(object sender, DragEventArgs e) {
285      e.Effect = DragDropEffects.None;
286      if (e.Data.GetDataPresent("IOperator")) {
287        Point p = operatorsTreeView.PointToClient(new Point(e.X, e.Y));
288        TreeNode node = operatorsTreeView.GetNodeAt(p);
289        if ((node != null) && (node.Tag is IOperatorGroup))
290          e.Effect = DragDropEffects.Copy;
291      }
292    }
293    private void operatorsTreeView_DragOver(object sender, DragEventArgs e) {
294      e.Effect = DragDropEffects.None;
295      if (e.Data.GetDataPresent("IOperator")) {
296        Point p = operatorsTreeView.PointToClient(new Point(e.X, e.Y));
297        TreeNode node = operatorsTreeView.GetNodeAt(p);
298        if ((node != null) && (node.Tag is IOperatorGroup))
299          e.Effect = DragDropEffects.Copy;
300      }
301    }
302    private void operatorsTreeView_DragDrop(object sender, DragEventArgs e) {
303      if (e.Effect != DragDropEffects.None) {
304        if (e.Data.GetDataPresent("IOperator")) {
305          IOperator op = (IOperator)e.Data.GetData("IOperator");
306          Point p = operatorsTreeView.PointToClient(new Point(e.X, e.Y));
307          TreeNode node = operatorsTreeView.GetNodeAt(p);
308          if (node != null) {
309            op = (IOperator)op.Clone();
310
311            while (op.SubOperators.Count > 0)
312              op.RemoveSubOperator(0);
313
314            IOperatorGroup group = (IOperatorGroup)node.Tag;
315            group.AddOperator(op);
316            node.Nodes.Add(CreateTreeNode(op));
317            operatorsTreeView.Sort();
318            EnableDisableButtons();
319          }
320        }
321      }
322    }
323    #endregion
324
325  }
326}
Note: See TracBrowser for help on using the repository browser.