Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Operator Architecture Refactoring/HeuristicLab.Core/OperatorGraphView.cs @ 771

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

Added HeuristicLab 3.0 sources from former SVN repository at revision 52

File size: 19.8 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.Generic;
24using System.ComponentModel;
25using System.Drawing;
26using System.Data;
27using System.Text;
28using System.Windows.Forms;
29using HeuristicLab.PluginInfrastructure;
30
31namespace HeuristicLab.Core {
32  public partial class OperatorGraphView : ViewBase {
33    private ChooseOperatorDialog chooseOperatorDialog;
34    private Dictionary<IOperator, IList<TreeNode>> operatorNodeTable;
35
36    public IOperatorGraph OperatorGraph {
37      get { return (IOperatorGraph)Item; }
38      set { base.Item = value; }
39    }
40
41    public OperatorGraphView() {
42      InitializeComponent();
43      operatorNodeTable = new Dictionary<IOperator, IList<TreeNode>>();
44      operatorsListView.Columns[0].Width = Math.Max(0, operatorsListView.Width - 25);
45      Caption = "Operator Graph";
46    }
47    public OperatorGraphView(IOperatorGraph operatorGraph)
48      : this() {
49      OperatorGraph = operatorGraph;
50    }
51
52    protected override void RemoveItemEvents() {
53      OperatorGraph.OperatorAdded -= new EventHandler<OperatorEventArgs>(OperatorGraph_OperatorAdded);
54      OperatorGraph.OperatorRemoved -= new EventHandler<OperatorEventArgs>(OperatorGraph_OperatorRemoved);
55      OperatorGraph.InitialOperatorChanged -= new EventHandler(OperatorGraph_InitialOperatorChanged);
56      base.RemoveItemEvents();
57    }
58    protected override void AddItemEvents() {
59      base.AddItemEvents();
60      OperatorGraph.OperatorAdded += new EventHandler<OperatorEventArgs>(OperatorGraph_OperatorAdded);
61      OperatorGraph.OperatorRemoved += new EventHandler<OperatorEventArgs>(OperatorGraph_OperatorRemoved);
62      OperatorGraph.InitialOperatorChanged += new EventHandler(OperatorGraph_InitialOperatorChanged);
63    }
64
65    protected override void UpdateControls() {
66      base.UpdateControls();
67      if (graphTreeView.Nodes.Count > 0)
68        RemoveTreeNode(graphTreeView.Nodes[0]);
69      graphTreeView.SelectedNode = null;
70      graphTreeView.Nodes.Clear();
71      graphTreeView.Enabled = false;
72      operatorsListView.Items.Clear();
73      removeButton.Enabled = false;
74      if (OperatorGraph == null) {
75        Caption = "Operator Graph";
76        operatorsListView.Enabled = false;
77        addOperatorButton.Enabled = false;
78      } else {
79        Caption = "Operator Graph (" + OperatorGraph.GetType().Name + ")";
80        foreach (IOperator op in OperatorGraph.Operators) {
81          operatorsListView.Items.Add(CreateListViewItem(op));
82        }
83        operatorsListView.Enabled = true;
84        addOperatorButton.Enabled = true;
85        if (OperatorGraph.InitialOperator != null) {
86          graphTreeView.Nodes.Add(CreateTreeNode(OperatorGraph.InitialOperator));
87          graphTreeView.Enabled = true;
88        }
89      }
90    }
91
92    private ListViewItem CreateListViewItem(IOperator op) {
93      ListViewItem item = new ListViewItem();
94      item.Text = op.Name;
95      item.Tag = op;
96      item.ImageIndex = 0;
97      if (op.GetType().Name == "CombinedOperator")
98        item.ImageIndex = 1;
99      else if (op.GetType().Name == "ProgrammableOperator")
100        item.ImageIndex = 2;
101      if (op == OperatorGraph.InitialOperator)
102        item.Font = new Font(operatorsListView.Font, FontStyle.Bold);
103      return item;
104    }
105
106    private TreeNode CreateTreeNode(IOperator op) {
107      TreeNode node = new TreeNode();
108      node.Text = op.Name;
109      node.Tag = op;
110      if (op.Breakpoint)
111        node.ForeColor = Color.Red;
112
113      if (!operatorNodeTable.ContainsKey(op)) {
114        operatorNodeTable.Add(op, new List<TreeNode>());
115        op.NameChanged += new EventHandler(Operator_NameChanged);
116        op.BreakpointChanged += new EventHandler(Operator_BreakpointChanged);
117        op.SubOperatorAdded += new EventHandler<OperatorIndexEventArgs>(Operator_SubOperatorAdded);
118        op.SubOperatorRemoved += new EventHandler<OperatorIndexEventArgs>(Operator_SubOperatorRemoved);
119      }
120      operatorNodeTable[op].Add(node);
121
122      for (int i = 0; i < op.SubOperators.Count; i++)
123        node.Nodes.Add(new TreeNode());
124      return node;
125    }
126
127    private void RemoveTreeNode(TreeNode node) {
128      foreach (TreeNode child in node.Nodes)
129        RemoveTreeNode(child);
130
131      IOperator op = (IOperator)node.Tag;
132      if (op != null) {
133        operatorNodeTable[op].Remove(node);
134        if (operatorNodeTable[op].Count == 0) {
135          op.NameChanged -= new EventHandler(Operator_NameChanged);
136          op.BreakpointChanged -= new EventHandler(Operator_BreakpointChanged);
137          op.SubOperatorAdded -= new EventHandler<OperatorIndexEventArgs>(Operator_SubOperatorAdded);
138          op.SubOperatorRemoved -= new EventHandler<OperatorIndexEventArgs>(Operator_SubOperatorRemoved);
139          operatorNodeTable.Remove(op);
140        }
141      }
142    }
143
144    #region ListView Events
145    private void operatorsListView_SelectedIndexChanged(object sender, EventArgs e) {
146      removeButton.Enabled = false;
147      if (operatorsListView.SelectedItems.Count > 0) {
148        removeButton.Enabled = true;
149      }
150    }
151    private void operatorsListView_DoubleClick(object sender, EventArgs e) {
152      if (operatorsListView.SelectedItems.Count == 1) {
153        IOperator op = (IOperator)operatorsListView.SelectedItems[0].Tag;
154        IView view = op.CreateView();
155        if (view != null)
156          PluginManager.ControlManager.ShowControl(view);
157      }
158    }
159    #endregion
160
161    #region TreeView Events
162    private void graphTreeView_BeforeExpand(object sender, TreeViewCancelEventArgs e) {
163      TreeNode node = e.Node;
164      IOperator op = (IOperator)node.Tag;
165      for (int i = 0; i < node.Nodes.Count; i++) {
166        if (node.Nodes[i].Tag == null) {
167          node.Nodes[i].Remove();
168          node.Nodes.Insert(i, CreateTreeNode(op.SubOperators[i]));
169        }
170      }
171    }
172    private void graphTreeView_AfterSelect(object sender, TreeViewEventArgs e) {
173      IOperator op = (IOperator)e.Node.Tag;
174      foreach (ListViewItem item in operatorsListView.Items)
175        item.Selected = item.Tag == op;
176    }
177    #endregion
178
179    #region Size Changed Events
180    private void operatorsListView_SizeChanged(object sender, EventArgs e) {
181      if (operatorsListView.Columns.Count > 0)
182        operatorsListView.Columns[0].Width = Math.Max(0, operatorsListView.Width - 25);
183    }
184    #endregion
185
186    #region Key Events
187    private void operatorsListView_KeyDown(object sender, KeyEventArgs e) {
188      if (e.KeyCode == Keys.Delete) {
189        if (operatorsListView.SelectedItems.Count > 0) {
190          foreach (ListViewItem item in operatorsListView.SelectedItems)
191            OperatorGraph.RemoveOperator(((IOperator)item.Tag).Guid);
192        }
193      }
194      if (e.KeyCode == Keys.F2) {
195        if (operatorsListView.SelectedItems.Count == 1)
196          operatorsListView.SelectedItems[0].BeginEdit();
197      }
198    }
199    private void graphTreeView_KeyDown(object sender, KeyEventArgs e) {
200      if (e.KeyCode == Keys.Delete) {
201        TreeNode node = graphTreeView.SelectedNode;
202        if ((node != null) && (node.Parent != null)) {
203          IOperator parent = (IOperator)node.Parent.Tag;
204          parent.RemoveSubOperator(node.Index);
205        }
206      }
207    }
208    #endregion
209
210    #region Edit Events
211    private void operatorsListView_AfterLabelEdit(object sender, LabelEditEventArgs e) {
212      e.CancelEdit = false;
213
214      string name = e.Label;
215      if (name != null) {
216        IOperator op = (IOperator)operatorsListView.Items[e.Item].Tag;
217        op.Name = name;
218      }
219    }
220    #endregion
221
222    #region Button Events
223    private void addOperatorButton_Click(object sender, EventArgs e) {
224      if (chooseOperatorDialog == null) chooseOperatorDialog = new ChooseOperatorDialog();
225      if (chooseOperatorDialog.ShowDialog(this) == DialogResult.OK)
226        OperatorGraph.AddOperator(chooseOperatorDialog.Operator);
227    }
228    private void removeButton_Click(object sender, EventArgs e) {
229      if (operatorsListView.SelectedItems.Count > 0) {
230        foreach (ListViewItem item in operatorsListView.SelectedItems)
231          OperatorGraph.RemoveOperator(((IOperator)item.Tag).Guid);
232      }
233    }
234    #endregion
235
236    #region Drag and Drop Events
237    private void operatorsListView_ItemDrag(object sender, ItemDragEventArgs e) {
238      ListViewItem item = (ListViewItem)e.Item;
239      IOperator op = (IOperator)item.Tag;
240      DataObject data = new DataObject();
241      data.SetData("IOperator", op);
242      data.SetData("DragSource", operatorsListView);
243      DoDragDrop(data, DragDropEffects.Copy);
244    }
245    private void operatorsListView_DragEnter(object sender, DragEventArgs e) {
246      e.Effect = DragDropEffects.None;
247      if (e.Data.GetDataPresent("IOperator"))
248        e.Effect = DragDropEffects.Copy;
249    }
250    private void operatorsListView_DragOver(object sender, DragEventArgs e) {
251      e.Effect = DragDropEffects.None;
252      if (e.Data.GetDataPresent("IOperator"))
253        e.Effect = DragDropEffects.Copy;
254    }
255    private void operatorsListView_DragDrop(object sender, DragEventArgs e) {
256      if (e.Effect != DragDropEffects.None) {
257        if (e.Data.GetDataPresent("IOperator")) {
258          IOperator op = (IOperator)e.Data.GetData("IOperator");
259          op = (IOperator)op.Clone();
260          OperatorGraph.AddOperator(op);
261        }
262      }
263    }
264    private void graphTreeView_ItemDrag(object sender, ItemDragEventArgs e) {
265      TreeNode node = (TreeNode)e.Item;
266      if (node.Parent != null) {
267        IOperator op = (IOperator)node.Tag;
268        IOperator parent = (IOperator)node.Parent.Tag;
269        int index = node.Index;
270        DataObject data = new DataObject();
271        data.SetData("IOperator", op);
272        data.SetData("DragSource", graphTreeView);
273        data.SetData("ParentOperator", parent);
274        data.SetData("Index", index);
275        DoDragDrop(data, DragDropEffects.Move);
276      }
277    }
278    private void graphTreeView_DragEnter(object sender, DragEventArgs e) {
279      e.Effect = DragDropEffects.None;
280      if (e.Data.GetDataPresent("IOperator")) {
281        Point p = graphTreeView.PointToClient(new Point(e.X, e.Y));
282        TreeNode node = graphTreeView.GetNodeAt(p);
283        if (node != null) {
284          if ((e.Data.GetDataPresent("ParentOperator")) && (node.Parent != null)) {
285            if ((e.Data.GetDataPresent("DragSource")) && (e.Data.GetData("DragSource") == graphTreeView))
286              e.Effect = DragDropEffects.Move;
287          } else {
288            if ((e.Data.GetDataPresent("DragSource")) && (e.Data.GetData("DragSource") == operatorsListView))
289              e.Effect = DragDropEffects.Copy;
290          }
291        }
292      }
293    }
294    private void graphTreeView_DragOver(object sender, DragEventArgs e) {
295      e.Effect = DragDropEffects.None;
296      if (e.Data.GetDataPresent("IOperator")) {
297        Point p = graphTreeView.PointToClient(new Point(e.X, e.Y));
298        TreeNode node = graphTreeView.GetNodeAt(p);
299        if (node != null) {
300          if ((e.Data.GetDataPresent("ParentOperator")) && (node.Parent != null)) {
301            if ((e.Data.GetDataPresent("DragSource")) && (e.Data.GetData("DragSource") == graphTreeView))
302              e.Effect = DragDropEffects.Move;
303          } else {
304            if ((e.Data.GetDataPresent("DragSource")) && (e.Data.GetData("DragSource") == operatorsListView))
305              e.Effect = DragDropEffects.Copy;
306          }
307        }
308      }
309    }
310    private void graphTreeView_DragDrop(object sender, DragEventArgs e) {
311      if (e.Effect != DragDropEffects.None) {
312        if (e.Data.GetDataPresent("IOperator")) {
313          IOperator op = (IOperator)e.Data.GetData("IOperator");
314          Point p = graphTreeView.PointToClient(new Point(e.X, e.Y));
315          TreeNode node = graphTreeView.GetNodeAt(p);
316          if (e.Data.GetDataPresent("ParentOperator")) {
317            if (node.Parent != null) {
318              TreeNode parentNode = node.Parent;
319              IOperator oldParent = (IOperator)e.Data.GetData("ParentOperator");
320              int oldIndex = (int)e.Data.GetData("Index");
321              IOperator newParent = (IOperator)node.Parent.Tag;
322              int newIndex = node.Index;
323              ICollection<IConstraint> violatedConstraints;
324              ICollection<IConstraint> violatedConstraints2;
325              oldParent.TryRemoveSubOperator(oldIndex, out violatedConstraints);
326              newParent.TryAddSubOperator(op, newIndex, out violatedConstraints2);
327              if ((violatedConstraints.Count == 0) && (violatedConstraints2.Count == 0)) {
328                graphTreeView.SelectedNode = parentNode.Nodes[newIndex];
329              } else {
330                List<IConstraint> allViolatedConstraints = new List<IConstraint>(violatedConstraints);
331                allViolatedConstraints.AddRange(violatedConstraints2);
332                if (Auxiliary.ShowIgnoreConstraintViolationMessageBox(allViolatedConstraints) == DialogResult.Yes) {
333                  if (violatedConstraints.Count > 0)
334                    oldParent.RemoveSubOperator(oldIndex);
335                  if (violatedConstraints2.Count > 0)
336                    newParent.AddSubOperator(op, newIndex);
337                  graphTreeView.SelectedNode = parentNode.Nodes[newIndex];
338                } else {
339                  if (violatedConstraints.Count == 0)
340                    oldParent.AddSubOperator(op, oldIndex);
341                  if (violatedConstraints2.Count == 0)
342                    newParent.RemoveSubOperator(newIndex);
343                }
344              }
345            }
346          } else {
347            if (node != null) {
348              IOperator parent = (IOperator)node.Tag;
349              ICollection<IConstraint> violatedConstraints;
350              if (parent.TryAddSubOperator(op, out violatedConstraints)) {
351                graphTreeView.SelectedNode = node.Nodes[node.Nodes.Count - 1];
352              } else if (Auxiliary.ShowIgnoreConstraintViolationMessageBox(violatedConstraints) == DialogResult.Yes) {
353                parent.AddSubOperator(op);
354                graphTreeView.SelectedNode = node.Nodes[node.Nodes.Count - 1];
355              }
356            }
357          }
358        }
359      }
360    }
361    #endregion
362
363    #region Context Menu Events
364    private void operatorsContextMenuStrip_Opening(object sender, CancelEventArgs e) {
365      viewToolStripMenuItem.Enabled = false;
366      initialOperatorToolStripMenuItem.Enabled = false;
367      initialOperatorToolStripMenuItem.Checked = false;
368      if (operatorsListView.SelectedItems.Count == 1) {
369        IOperator op = (IOperator)operatorsListView.SelectedItems[0].Tag;
370        IView view = op.CreateView();
371        if (view != null) {
372          viewToolStripMenuItem.Enabled = true;
373          viewToolStripMenuItem.Tag = view;
374        }
375        initialOperatorToolStripMenuItem.Enabled = true;
376        initialOperatorToolStripMenuItem.Tag = op;
377        if (op == OperatorGraph.InitialOperator)
378          initialOperatorToolStripMenuItem.Checked = true;
379      }
380    }
381    private void viewToolStripMenuItem_Click(object sender, EventArgs e) {
382      IView view = (IView)((ToolStripMenuItem)sender).Tag;
383      PluginManager.ControlManager.ShowControl(view);
384    }
385    private void initialOperatorToolStripMenuItem_Click(object sender, EventArgs e) {
386      if (initialOperatorToolStripMenuItem.Checked) {
387        foreach (ListViewItem item in operatorsListView.Items)
388          item.Font = operatorsListView.Font;
389        operatorsListView.SelectedItems[0].Font = new Font(operatorsListView.Font, FontStyle.Bold);
390        OperatorGraph.InitialOperator = (IOperator)initialOperatorToolStripMenuItem.Tag;
391      } else {
392        operatorsListView.SelectedItems[0].Font = operatorsListView.Font;
393        OperatorGraph.InitialOperator = null;
394      }
395    }
396    private void graphContextMenuStrip_Opening(object sender, CancelEventArgs e) {
397      viewToolStripMenuItem1.Enabled = false;
398      breakpointToolStripMenuItem.Enabled = false;
399      breakpointToolStripMenuItem.Checked = false;
400      if (graphTreeView.SelectedNode != null) {
401        IOperator op = (IOperator)graphTreeView.SelectedNode.Tag;
402        IView view = op.CreateView();
403        if (view != null) {
404          viewToolStripMenuItem1.Enabled = true;
405          viewToolStripMenuItem1.Tag = view;
406        }
407        breakpointToolStripMenuItem.Enabled = true;
408        breakpointToolStripMenuItem.Tag = op;
409        if (op.Breakpoint)
410          breakpointToolStripMenuItem.Checked = true;
411      }
412    }
413    private void breakpointToolStripMenuItem_Click(object sender, EventArgs e) {
414      IOperator op = (IOperator)breakpointToolStripMenuItem.Tag;
415      op.Breakpoint = breakpointToolStripMenuItem.Checked;
416    }
417    #endregion
418
419    #region OperatorGraph Events
420    private void OperatorGraph_OperatorAdded(object sender, OperatorEventArgs e) {
421      operatorsListView.Items.Add(CreateListViewItem(e.Operator));
422    }
423    private void OperatorGraph_OperatorRemoved(object sender, OperatorEventArgs e) {
424      ListViewItem itemToDelete = null;
425      foreach (ListViewItem item in operatorsListView.Items) {
426        if (item.Tag == e.Operator)
427          itemToDelete = item;
428      }
429      itemToDelete.Remove();
430    }
431    private void OperatorGraph_InitialOperatorChanged(object sender, EventArgs e) {
432      Refresh();
433    }
434    #endregion
435
436    #region Operator Events
437    private void Operator_NameChanged(object sender, EventArgs e) {
438      IOperator op = (IOperator)sender;
439      foreach (TreeNode node in operatorNodeTable[op])
440        node.Text = op.Name;
441    }
442    private void Operator_BreakpointChanged(object sender, EventArgs e) {
443      IOperator op = (IOperator)sender;
444      foreach (TreeNode node in operatorNodeTable[op]) {
445        if (op.Breakpoint)
446          node.ForeColor = Color.Red;
447        else
448          node.ForeColor = graphTreeView.ForeColor;
449      }
450    }
451    private void Operator_SubOperatorAdded(object sender, OperatorIndexEventArgs e) {
452      IOperator op = (IOperator)sender;
453      if (operatorNodeTable.ContainsKey(op)) {
454        TreeNode[] nodes = new TreeNode[operatorNodeTable[op].Count];
455        operatorNodeTable[op].CopyTo(nodes, 0);
456        foreach (TreeNode node in nodes)
457          node.Nodes.Insert(e.Index, CreateTreeNode(e.Operator));
458      }
459    }
460    private void Operator_SubOperatorRemoved(object sender, OperatorIndexEventArgs e) {
461      IOperator op = (IOperator)sender;
462      if (operatorNodeTable.ContainsKey(op)) {
463        TreeNode[] nodes = new TreeNode[operatorNodeTable[op].Count];
464        operatorNodeTable[op].CopyTo(nodes, 0);
465        foreach (TreeNode node in nodes) {
466          RemoveTreeNode(node.Nodes[e.Index]);
467          node.Nodes.RemoveAt(e.Index);
468        }
469      }
470    }
471    #endregion
472
473    #region Mouse Events
474    private void graphTreeView_MouseDown(object sender, MouseEventArgs e) {
475      if (e.Button != MouseButtons.Right) return;
476      TreeNode clickedNode = graphTreeView.GetNodeAt(e.X, e.Y);
477      if (clickedNode != null) {
478        graphTreeView.SelectedNode = clickedNode;
479        graphTreeView.Refresh();
480      }
481    }
482    #endregion
483
484  }
485}
Note: See TracBrowser for help on using the repository browser.