Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Views/3.4/ExperimentManager/TreeView/ItemTreeView.cs @ 6033

Last change on this file since 6033 was 6033, checked in by cneumuel, 14 years ago

#1233

  • created baseclass for jobs (ItemJob) which derives OperatorJobs and EngineJobs
  • created special view for OptimizerJobs which derives from a more general view
  • removed logic from domain class HiveExperiment and moved it into RefreshableHiveExperiment
  • improved ItemTreeView
  • corrected plugin dependencies
  • fixed bug in database trigger when deleting HiveExperiments
  • added delete cascade for Plugin and PluginData
  • lots of fixes
File size: 15.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Windows.Forms;
27using HeuristicLab.Collections;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Core.Views;
31using HeuristicLab.MainForm;
32using HeuristicLab.MainForm.WindowsForms;
33using HeuristicLab.PluginInfrastructure;
34
35namespace HeuristicLab.Clients.Hive.Views {
36  [View("ItemTree View")]
37  [Content(typeof(ItemCollection<>), IsDefaultView = false)]
38  public partial class ItemTreeView<T> : ItemView where T : class, IItemTree<T> {
39    public new ItemCollection<T> Content {
40      get { return (ItemCollection<T>)base.Content; }
41      set { base.Content = value; }
42    }
43
44    public T SelectedItem {
45      get {
46        if (treeView.SelectedNode != null)
47          return (T)treeView.SelectedNode.Tag;
48        return null;
49      }
50    }
51
52    public ItemTreeView() {
53      InitializeComponent();
54    }
55
56    protected override void DeregisterContentEvents() {
57      Content.ItemsAdded -= new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded);
58      Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved);
59      Content.CollectionReset -= new CollectionItemsChangedEventHandler<T>(Content_CollectionReset);
60      foreach (TreeNode node in treeView.Nodes) {
61        DeregisterContentEvents((T)node.Tag, node);
62      }
63      base.DeregisterContentEvents();
64    }
65
66    protected virtual void DeregisterContentEvents(T item, TreeNode node) {
67      item.ItemsAdded -= new CollectionItemsChangedEventHandler<IItemTree<T>>(item_ItemsAdded);
68      item.ItemsRemoved -= new CollectionItemsChangedEventHandler<IItemTree<T>>(item_ItemsRemoved);
69      item.CollectionReset -= new CollectionItemsChangedEventHandler<IItemTree<T>>(item_CollectionReset);
70      item.ToStringChanged -= new EventHandler(item_ToStringChanged);
71      item.ItemImageChanged -= new EventHandler(item_ItemImageChanged);
72      foreach (TreeNode childNode in node.Nodes) {
73        DeregisterContentEvents((T)childNode.Tag, childNode);
74      }
75    }
76
77    protected override void RegisterContentEvents() {
78      base.RegisterContentEvents();
79      Content.ItemsAdded += new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded);
80      Content.ItemsRemoved += new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved);
81      Content.CollectionReset += new CollectionItemsChangedEventHandler<T>(Content_CollectionReset);
82      foreach (TreeNode node in treeView.Nodes) {
83        RegisterContentEvents((T)node.Tag, node);
84      }
85    }
86
87    protected virtual void RegisterContentEvents(T item, TreeNode node) {
88      item.ItemsAdded += new CollectionItemsChangedEventHandler<IItemTree<T>>(item_ItemsAdded);
89      item.ItemsRemoved += new CollectionItemsChangedEventHandler<IItemTree<T>>(item_ItemsRemoved);
90      item.CollectionReset += new CollectionItemsChangedEventHandler<IItemTree<T>>(item_CollectionReset);
91      item.ToStringChanged += new EventHandler(item_ToStringChanged);
92      item.ItemImageChanged += new EventHandler(item_ItemImageChanged);
93      foreach (TreeNode childNode in node.Nodes) {
94        RegisterContentEvents((T)childNode.Tag, childNode);
95      }
96    }
97
98    #region Event Handlers (Content)
99    protected virtual void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
100      if (InvokeRequired) {
101        Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded), sender, e);
102      } else {
103        foreach (T item in e.Items) {
104          var node = AddChildNodes(item, treeView.Nodes, true);
105          UpdateNodeItemImage(node);
106        }
107      }
108    }
109    protected virtual void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
110      if (InvokeRequired) {
111        Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved), sender, e);
112      } else {
113        foreach (T item in e.Items) {
114          RemoveChildNodes(item, treeView.Nodes);
115        }
116        RebuildImageList();
117      }
118    }
119    protected virtual void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
120      if (InvokeRequired) {
121        Invoke(new CollectionItemsChangedEventHandler<T>(Content_CollectionReset), sender, e);
122      } else {
123        foreach (T item in e.OldItems) {
124          RemoveChildNodes(item, treeView.Nodes);
125        }
126        RebuildImageList();
127        foreach (T item in e.Items) {
128          AddChildNodes(item, treeView.Nodes, true);
129        }
130      }
131    }
132
133    protected virtual void item_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IItemTree<T>> e) {
134      if (InvokeRequired) {
135        Invoke(new CollectionItemsChangedEventHandler<IItemTree<T>>(item_ItemsAdded), sender, e);
136      } else {
137        TreeNode node = GetNodeByItem(sender as T);
138        if (node != null) {
139          foreach (T item in e.Items) {
140            AddChildNodes(item, node.Nodes, true);
141            node.ExpandAll();
142            UpdateNodeItemImage(node);
143          }
144        }
145      }
146    }
147
148    protected virtual void item_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IItemTree<T>> e) {
149      if (InvokeRequired) {
150        Invoke(new CollectionItemsChangedEventHandler<IItemTree<T>>(item_ItemsRemoved), sender, e);
151      } else {
152        TreeNode node = GetNodeByItem(sender as T);
153        if (node != null) {
154          foreach (T item in e.Items) {
155            RemoveChildNodes(item, node.Nodes);
156          }
157          RebuildImageList();
158        }
159      }
160    }
161
162    protected virtual void item_CollectionReset(object sender, CollectionItemsChangedEventArgs<IItemTree<T>> e) {
163      if (InvokeRequired) {
164        Invoke(new CollectionItemsChangedEventHandler<IItemTree<T>>(item_CollectionReset), sender, e);
165      } else {
166        TreeNode node = GetNodeByItem(sender as T);
167        if (node != null) {
168          foreach (T item in e.OldItems) {
169            RemoveChildNodes(item, node.Nodes);
170          }
171          RebuildImageList();
172          foreach (T item in e.Items) {
173            AddChildNodes(item, node.Nodes, true);
174          }
175        }
176      }
177    }
178
179    protected virtual void item_ToStringChanged(object sender, EventArgs e) {
180      if (InvokeRequired) {
181        Invoke(new EventHandler(item_ToStringChanged), sender, e);
182      } else {
183        var item = sender as T;
184        TreeNode node = GetNodeByItem(item);
185        if (node != null)
186          node.Text = item.ToString();
187      }
188    }
189
190    protected virtual void item_ItemImageChanged(object sender, EventArgs e) {
191      if (InvokeRequired) {
192        Invoke(new EventHandler(item_ItemImageChanged), sender, e);
193      } else {
194        var item = sender as T;
195        TreeNode node = GetNodeByItem(item);
196        if (node != null)
197          UpdateNodeItemImage(node);
198      }
199    }
200
201    protected virtual void treeView_DoubleClick(object sender, EventArgs e) {
202      if (InvokeRequired) {
203        Invoke(new EventHandler(treeView_DoubleClick), sender, e);
204      } else {
205        if (treeView.SelectedNode != null) {
206          var item = treeView.SelectedNode.Tag as T;
207          if (item != null) {
208            IContentView view = MainFormManager.MainForm.ShowContent(item);
209            if (view != null) {
210              view.ReadOnly = ReadOnly;
211              view.Locked = Locked;
212            }
213          }
214        }
215      }
216    }
217
218    protected virtual void treeView_MouseDown(object sender, MouseEventArgs e) {
219      if (e.Button == System.Windows.Forms.MouseButtons.Left) {
220        // enables deselection of treeNodes
221        if (treeView.SelectedNode == null)
222          return;
223        treeView.SelectedNode = null;
224        SetEnabledStateOfControls();
225      }
226    }
227    private void treeView_MouseClick(object sender, MouseEventArgs e) {
228
229    }
230    private void treeView_Click(object sender, EventArgs e) {
231
232    }
233    #endregion
234
235    protected override void OnContentChanged() {
236      base.OnContentChanged();
237      if (Content == null) {
238        ClearNodes();
239        detailsViewHost.Content = null;
240      } else {
241        ClearNodes();
242        foreach (T itemTree in Content) {
243          AddChildNodes(itemTree, treeView.Nodes, false);
244        }
245        foreach (TreeNode node in treeView.Nodes) {
246          UpdateNodeItemImage(node);
247        }
248        detailsViewHost.Content = null;
249      }
250    }
251
252    protected virtual void ClearNodes() {
253      treeView.Nodes.Clear();
254    }
255
256    protected virtual TreeNode AddChildNodes(T item, TreeNodeCollection nodes, bool registerEvents) {
257      TreeNode node = CreateTreeViewNode(item);
258      nodes.Add(node);
259      RebuildImageList(node);
260      RegisterContentEvents(item, node);
261      var childItems = item.GetChildItems();
262      foreach (T childItem in childItems) {
263        AddChildNodes(childItem, node.Nodes, registerEvents);
264      }
265      node.ExpandAll();
266      return node;
267    }
268
269    protected virtual void RemoveChildNodes(T item, TreeNodeCollection treeNodeCollection) {
270      int idx = -1;
271      for (int i = 0; i < treeNodeCollection.Count; i++) {
272        if (treeNodeCollection[i].Tag == item) {
273          idx = i; break;
274        }
275      }
276      if (idx > 0) {
277        DeregisterContentEvents(item, treeNodeCollection[idx]);
278        treeNodeCollection.RemoveAt(idx);
279      }
280    }
281
282    protected virtual TreeNode GetNodeByItem(T item) {
283      TreeNode found = null;
284      foreach (TreeNode node in treeView.Nodes) {
285        found = GetNodeByItem(node, item);
286        if (found != null)
287          return found;
288      }
289      return null;
290    }
291
292    protected virtual TreeNode GetNodeByItem(TreeNode node, T item) {
293      if (node.Tag == item)
294        return node;
295      TreeNode found = null;
296      foreach (TreeNode childNode in node.Nodes) {
297        found = GetNodeByItem(childNode, item);
298        if (found != null)
299          return found;
300      }
301      return null;
302    }
303
304    protected virtual TreeNode CreateTreeViewNode(T item) {
305      var node = new TreeNode(item.ToString());
306      node.Tag = item;
307      return node;
308    }
309
310    protected virtual void UpdateNodeItemImage(TreeNode node) {
311      if (node == null) throw new ArgumentNullException();
312      var item = node.Tag as T;
313      int i = node.ImageIndex;
314      this.imageList.Images[i] = item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage;
315      node.ImageIndex = -1;
316      node.ImageIndex = i;
317      node.SelectedImageIndex = node.ImageIndex;
318      foreach (TreeNode childNode in node.Nodes) {
319        UpdateNodeItemImage(childNode);
320      }
321    }
322
323    protected virtual T GetParentItem(T selectedItem) {
324      var node = GetNodeByItem(selectedItem);
325      if (node == null || node.Parent == null)
326        return null;
327      else
328        return (T)node.Parent.Tag;
329    }
330
331    protected override void SetEnabledStateOfControls() {
332      base.SetEnabledStateOfControls();
333      addButton.Enabled = !this.ReadOnly;
334      removeButton.Enabled = !this.ReadOnly && treeView.SelectedNode != null;
335    }
336
337    #region Event Handlers (child controls)
338    // Put event handlers of child controls here.
339    protected virtual void treeView_AfterSelect(object sender, TreeViewEventArgs e) {
340      detailsViewHost.Content = (IContent)treeView.SelectedNode.Tag;
341      SetEnabledStateOfControls();
342    }
343
344    protected virtual void contextMenuStrip_Opening(object sender, CancelEventArgs e) {
345      if (treeView.SelectedNode != null) {
346        T selectedItem = treeView.SelectedNode.Tag as T;
347        T parentItem = GetParentItem(selectedItem);
348        var actions = GetTreeNodeItemActions(selectedItem);
349        contextMenuStrip.Items.Clear();
350        foreach (var action in actions) {
351          contextMenuStrip.Items.Add(new DelegateMenuItem<T>(action.Name, action.Image, new Action<T, T>(action.Execute), selectedItem, parentItem));
352        }
353      }
354    }
355
356    protected virtual void addButton_Click(object sender, EventArgs e) {
357      T item = CreateItem<T>();
358      if (item != null)
359        Content.Add(item);
360    }
361
362    protected virtual void removeButton_Click(object sender, EventArgs e) {
363      if (treeView.SelectedNode != null) {
364
365      }
366    }
367
368    protected virtual void showDetailsCheckBox_CheckedChanged(object sender, EventArgs e) {
369      if (showDetailsCheckBox.Checked) {
370        splitContainer.Panel2Collapsed = false;
371        detailsGroupBox.Enabled = treeView.SelectedNode != null;
372        detailsViewHost.Content = treeView.SelectedNode != null ? (T)treeView.SelectedNode.Tag : null;
373      } else {
374        splitContainer.Panel2Collapsed = true;
375        detailsViewHost.Content = null;
376      }
377    }
378    #endregion
379
380    protected virtual void RebuildImageList() {
381      treeView.ImageList.Images.Clear();
382      foreach (TreeNode node in treeView.Nodes) {
383        RebuildImageList(node);
384      }
385    }
386
387    protected virtual void RebuildImageList(TreeNode node) {
388      var item = node.Tag as T;
389      treeView.ImageList.Images.Add(item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage);
390      node.ImageIndex = treeView.ImageList.Images.Count - 1;
391      node.SelectedImageIndex = node.ImageIndex;
392      foreach (TreeNode childNode in node.Nodes) {
393        RebuildImageList(childNode);
394      }
395    }
396
397    protected virtual ICollection<IItemTreeNodeAction<T>> GetTreeNodeItemActions(T selectedItem) {
398      return selectedItem.Actions;
399    }
400
401    protected virtual X CreateItem<X>() where X : class {
402      var typeSelectorDialog = new TypeSelectorDialog();
403      typeSelectorDialog.Caption = "Select Item";
404      typeSelectorDialog.TypeSelector.Caption = "Available Items";
405      typeSelectorDialog.TypeSelector.Configure(typeof(X), false, true);
406
407      if (typeSelectorDialog.ShowDialog(this) == DialogResult.OK) {
408        try {
409          return (X)typeSelectorDialog.TypeSelector.CreateInstanceOfSelectedType();
410        }
411        catch (Exception ex) {
412          ErrorHandling.ShowErrorDialog(this, ex);
413        }
414      }
415      return null;
416    }
417
418
419  }
420
421  public class DelegateMenuItem<T> : ToolStripMenuItem {
422    private Action<T, T> actionDelegate;
423    private T node;
424    private T parentNode;
425
426    public DelegateMenuItem(string name, Image image, Action<T, T> actionDelegate, T node, T parentNode) {
427      this.Name = name;
428      this.Text = name;
429      this.actionDelegate = actionDelegate;
430      this.node = node;
431      this.parentNode = parentNode;
432      this.Image = image;
433    }
434
435    protected override void OnClick(EventArgs e) {
436      base.OnClick(e);
437      actionDelegate.Invoke(node, parentNode);
438    }
439  }
440}
Note: See TracBrowser for help on using the repository browser.