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

Last change on this file since 5793 was 5793, checked in by cneumuel, 11 years ago

#1233

  • implemented correct downloading of paused jobs. its now also possible to change parameters and resume a algorithm
  • removed Prepare() calls in ExperimentManager and in slave, as it prevents corrent resuming of paused jobs
  • made events in ItemTreeView be invoked in the correct thread
  • reduced log output in ExperimentManager
File size: 9.6 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.Windows.Forms;
24using HeuristicLab.Collections;
25using HeuristicLab.Common;
26using HeuristicLab.Core.Views;
27using HeuristicLab.MainForm;
28using HeuristicLab.MainForm.WindowsForms;
29
30namespace HeuristicLab.Clients.Hive.Views {
31  [View("ItemTree View")]
32  [Content(typeof(IItemTree), IsDefaultView = false)]
33  public sealed partial class ItemTreeView : ItemView {
34    public new IItemTree Content {
35      get { return (IItemTree)base.Content; }
36      set { base.Content = value; }
37    }
38
39    public ItemTreeView() {
40      InitializeComponent();
41    }
42
43    protected override void DeregisterContentEvents() {
44      foreach (TreeNode node in treeView.Nodes) {
45        DeregisterContentEvents((IItemTree)node.Tag, node);
46      }
47      base.DeregisterContentEvents();
48    }
49
50    private void DeregisterContentEvents(IItemTree item, TreeNode node) {
51      item.ItemsAdded -= new CollectionItemsChangedEventHandler<IItemTree>(item_ItemsAdded);
52      item.ItemsRemoved -= new CollectionItemsChangedEventHandler<IItemTree>(item_ItemsRemoved);
53      item.CollectionReset -= new CollectionItemsChangedEventHandler<IItemTree>(item_CollectionReset);
54      item.ToStringChanged -= new EventHandler(item_ToStringChanged);
55      item.ItemImageChanged -= new EventHandler(item_ItemImageChanged);
56      foreach (TreeNode childNode in node.Nodes) {
57        DeregisterContentEvents((IItemTree)childNode.Tag, childNode);
58      }
59    }
60
61    protected override void RegisterContentEvents() {
62      base.RegisterContentEvents();
63      foreach (TreeNode node in treeView.Nodes) {
64        RegisterContentEvents((IItemTree)node.Tag, node);
65      }
66    }
67
68    private void RegisterContentEvents(IItemTree item, TreeNode node) {
69      item.ItemsAdded += new CollectionItemsChangedEventHandler<IItemTree>(item_ItemsAdded);
70      item.ItemsRemoved += new CollectionItemsChangedEventHandler<IItemTree>(item_ItemsRemoved);
71      item.CollectionReset += new CollectionItemsChangedEventHandler<IItemTree>(item_CollectionReset);
72      item.ToStringChanged += new EventHandler(item_ToStringChanged);
73      item.ItemImageChanged += new EventHandler(item_ItemImageChanged);
74      foreach (TreeNode childNode in node.Nodes) {
75        RegisterContentEvents((IItemTree)childNode.Tag, childNode);
76      }
77    }
78
79    #region Event Handlers (Content)
80    private void item_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IItemTree> e) {
81      if (InvokeRequired) {
82        Invoke(new CollectionItemsChangedEventHandler<IItemTree>(item_ItemsAdded), sender, e);
83      } else {
84        TreeNode node = GetNodeByItem(sender as IItemTree);
85        foreach (IItemTree item in e.Items) {
86          AddChildNodes(item, node.Nodes, true);
87          node.ExpandAll();
88          UpdateNodeItemImage(node);
89        }
90      }
91    }
92
93    private void item_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IItemTree> e) {
94      if (InvokeRequired) {
95        Invoke(new CollectionItemsChangedEventHandler<IItemTree>(item_ItemsRemoved), sender, e);
96      } else {
97        TreeNode node = GetNodeByItem(sender as IItemTree);
98        foreach (IItemTree item in e.Items) {
99          RemoveChildNodes(item, node.Nodes);
100        }
101        RebuildImageList();
102      }
103    }
104
105    private void item_CollectionReset(object sender, CollectionItemsChangedEventArgs<IItemTree> e) {
106      if (InvokeRequired) {
107        Invoke(new CollectionItemsChangedEventHandler<IItemTree>(item_CollectionReset), sender, e);
108      } else {
109        TreeNode node = GetNodeByItem(sender as IItemTree);
110        foreach (IItemTree item in e.OldItems) {
111          RemoveChildNodes(item, node.Nodes);
112        }
113        RebuildImageList();
114        foreach (IItemTree item in e.Items) {
115          AddChildNodes(item, node.Nodes, true);
116        }
117      }
118    }
119
120    private void item_ToStringChanged(object sender, EventArgs e) {
121      if (InvokeRequired) {
122        Invoke(new EventHandler(item_ToStringChanged), sender, e);
123      } else {
124        var item = sender as IItemTree;
125        TreeNode node = GetNodeByItem(item);
126        node.Text = item.ToString();
127      }
128    }
129
130    private void item_ItemImageChanged(object sender, EventArgs e) {
131      if (InvokeRequired) {
132        Invoke(new EventHandler(item_ItemImageChanged), sender, e);
133      } else {
134        var item = sender as IItemTree;
135        TreeNode node = GetNodeByItem(item);
136        UpdateNodeItemImage(node);
137      }
138    }
139
140    private void treeView_DoubleClick(object sender, EventArgs e) {
141      if (InvokeRequired) {
142        Invoke(new EventHandler(treeView_DoubleClick), sender, e);
143      } else {
144        if (treeView.SelectedNode != null) {
145          var item = treeView.SelectedNode.Tag as IItemTree;
146          if (item != null) {
147            IContentView view = MainFormManager.MainForm.ShowContent(item);
148            if (view != null) {
149              view.ReadOnly = ReadOnly;
150              view.Locked = Locked;
151            }
152          }
153        }
154      }
155    }
156    #endregion
157
158    protected override void OnContentChanged() {
159      base.OnContentChanged();
160      if (Content == null) {
161        // Add code when content has been changed and is null
162        ClearNodes();
163        detailsViewHost.Content = null;
164      } else {
165        // Add code when content has been changed and is not null
166        ClearNodes();
167        AddChildNodes(Content, treeView.Nodes, false);
168        foreach (TreeNode node in treeView.Nodes) {
169          UpdateNodeItemImage(node);
170        }
171      }
172    }
173
174    private void ClearNodes() {
175      treeView.Nodes.Clear();
176    }
177
178    private void AddChildNodes(IItemTree item, TreeNodeCollection nodes, bool registerEvents) {
179      TreeNode node = CreateTreeViewNode(item);
180      nodes.Add(node);
181      RebuildImageList(node);
182      RegisterContentEvents(item, node);
183      var childItems = item.GetChildItems();
184      foreach (var childItem in childItems) {
185        AddChildNodes(childItem, node.Nodes, registerEvents);
186      }
187      node.ExpandAll();
188    }
189
190    private void RemoveChildNodes(IItemTree item, TreeNodeCollection treeNodeCollection) {
191      int idx = -1;
192      for (int i = 0; i < treeNodeCollection.Count; i++) {
193        if (treeNodeCollection[i].Tag == item) {
194          idx = i; break;
195        }
196      }
197      DeregisterContentEvents(item, treeNodeCollection[idx]);
198      treeNodeCollection.RemoveAt(idx);
199    }
200   
201    private TreeNode GetNodeByItem(IItemTree item) {
202      TreeNode found = null;
203      foreach (TreeNode node in treeView.Nodes) {
204        found = GetNodeByItem(node, item);
205        if (found != null)
206          return found;
207      }
208      return null;
209    }
210
211    private TreeNode GetNodeByItem(TreeNode node, IItemTree item) {
212      if (node.Tag == item)
213        return node;
214      TreeNode found = null;
215      foreach (TreeNode childNode in node.Nodes) {
216        found = GetNodeByItem(childNode, item);
217        if (found != null)
218          return found;
219      }
220      return null;
221    }
222
223    private TreeNode CreateTreeViewNode(IItemTree item) {
224      var node = new TreeNode(item.ToString());
225      node.Tag = item;
226      return node;
227    }
228
229    private void UpdateNodeItemImage(TreeNode node) {
230      if (node == null) throw new ArgumentNullException();
231      var item = node.Tag as IItemTree;
232      int i = node.ImageIndex;
233      this.imageList.Images[i] = item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage;
234      node.ImageIndex = -1;
235      node.ImageIndex = i;
236      node.SelectedImageIndex = node.ImageIndex;
237      foreach (TreeNode childNode in node.Nodes) {
238        UpdateNodeItemImage(childNode);
239      }
240    }
241
242    protected override void SetEnabledStateOfControls() {
243      base.SetEnabledStateOfControls();
244      // Enable or disable controls based on whether the content is null or the view is set readonly
245    }
246
247    #region Event Handlers (child controls)
248    // Put event handlers of child controls here.
249    private void treeView_AfterSelect(object sender, TreeViewEventArgs e) {
250      detailsViewHost.Content = (IContent)treeView.SelectedNode.Tag;
251    }
252    #endregion
253
254    private void RebuildImageList() {
255      treeView.ImageList.Images.Clear();
256      foreach (TreeNode node in treeView.Nodes) {
257        RebuildImageList(node);
258      }
259    }
260
261    private void RebuildImageList(TreeNode node) {
262      var item = node.Tag as IItemTree;
263      treeView.ImageList.Images.Add(item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage);
264      node.ImageIndex = treeView.ImageList.Images.Count - 1;
265      node.SelectedImageIndex = node.ImageIndex;
266      foreach (TreeNode childNode in node.Nodes) {
267        RebuildImageList(childNode);
268      }
269    }
270  }
271}
Note: See TracBrowser for help on using the repository browser.