Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5675 was 5675, checked in by cneumuel, 12 years ago

#1233

  • improved treeview for hive jobs
File size: 8.3 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 System.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 System.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    // Put event handlers of the content here
81    #endregion
82
83    protected override void OnContentChanged() {
84      base.OnContentChanged();
85      if (Content == null) {
86        // Add code when content has been changed and is null
87        ClearNodes();
88      } else {
89        // Add code when content has been changed and is not null
90        ClearNodes();
91        AddChildNodes(Content, treeView.Nodes, false);
92        foreach (TreeNode node in treeView.Nodes) {
93          UpdateNodeItemImage(node);
94        }
95      }
96    }
97
98    private void ClearNodes() {
99      treeView.Nodes.Clear();
100    }
101
102    private void AddChildNodes(IItemTree item, TreeNodeCollection nodes, bool registerEvents) {
103      TreeNode node = CreateTreeViewNode(item);
104      nodes.Add(node);
105      RebuildImageList(node);
106      RegisterContentEvents(item, node);
107      var childItems = item.GetChildItems();
108      foreach (var childItem in childItems) {
109        AddChildNodes(childItem, node.Nodes, registerEvents);
110      }
111      node.ExpandAll();
112    }
113
114    private void RemoveChildNodes(IItemTree item, TreeNodeCollection treeNodeCollection) {
115      int idx = -1;
116      for (int i = 0; i < treeNodeCollection.Count; i++) {
117        if (treeNodeCollection[i].Tag == item) {
118          idx = i; break;
119        }
120      }
121      DeregisterContentEvents(item, treeNodeCollection[idx]);
122      treeNodeCollection.RemoveAt(idx);
123    }
124
125    private void item_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IItemTree> e) {
126      TreeNode node = GetNodeByItem(sender as IItemTree);
127      foreach (IItemTree item in e.Items) {
128        AddChildNodes(item, node.Nodes, true);
129        node.ExpandAll();
130        UpdateNodeItemImage(node);
131      }
132    }
133
134    private void item_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IItemTree> e) {
135      TreeNode node = GetNodeByItem(sender as IItemTree);
136      foreach (IItemTree item in e.Items) {
137        RemoveChildNodes(item, node.Nodes);
138      }
139      RebuildImageList();
140    }
141
142    private void item_CollectionReset(object sender, CollectionItemsChangedEventArgs<IItemTree> e) {
143      TreeNode node = GetNodeByItem(sender as IItemTree);
144      foreach (IItemTree item in e.OldItems) {
145        RemoveChildNodes(item, node.Nodes);
146      }
147      RebuildImageList();
148      foreach (IItemTree item in e.Items) {
149        AddChildNodes(item, node.Nodes, true);
150      }
151    }
152
153    private void item_ToStringChanged(object sender, EventArgs e) {
154      var item = sender as IItemTree;
155      TreeNode node = GetNodeByItem(item);
156      node.Text = item.ToString();
157    }
158
159    private void item_ItemImageChanged(object sender, EventArgs e) {
160      var item = sender as IItemTree;
161      TreeNode node = GetNodeByItem(item);
162      UpdateNodeItemImage(node);
163    }
164
165    private TreeNode GetNodeByItem(IItemTree item) {
166      TreeNode found = null;
167      foreach (TreeNode node in treeView.Nodes) {
168        found = GetNodeByItem(node, item);
169        if (found != null)
170          return found;
171      }
172      return null;
173    }
174
175    private TreeNode GetNodeByItem(TreeNode node, IItemTree item) {
176      if (node.Tag == item)
177        return node;
178      TreeNode found = null;
179      foreach (TreeNode childNode in node.Nodes) {
180        found = GetNodeByItem(childNode, item);
181        if (found != null)
182          return found;
183      }
184      return null;
185    }
186
187    private TreeNode CreateTreeViewNode(IItemTree item) {
188      var node = new TreeNode(item.ToString());
189      node.Tag = item;
190      return node;
191    }
192
193    private void UpdateNodeItemImage(TreeNode node) {
194      if (node == null) throw new ArgumentNullException();
195      var item = node.Tag as IItemTree;
196      int i = node.ImageIndex;
197      this.imageList.Images[i] = item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage;
198      node.ImageIndex = -1;
199      node.ImageIndex = i;
200      node.SelectedImageIndex = node.ImageIndex;
201      foreach (TreeNode childNode in node.Nodes) {
202        UpdateNodeItemImage(childNode);
203      }
204    }
205
206    protected override void SetEnabledStateOfControls() {
207      base.SetEnabledStateOfControls();
208      // Enable or disable controls based on whether the content is null or the view is set readonly
209    }
210
211    #region Event Handlers (child controls)
212    // Put event handlers of child controls here.
213    private void treeView_AfterSelect(object sender, TreeViewEventArgs e) {
214      viewHost.Content = (IContent)treeView.SelectedNode.Tag;
215    }
216    #endregion
217
218    private void RebuildImageList() {
219      treeView.ImageList.Images.Clear();
220      foreach (TreeNode node in treeView.Nodes) {
221        RebuildImageList(node);
222      }
223    }
224
225    private void RebuildImageList(TreeNode node) {
226      var item = node.Tag as IItemTree;
227      treeView.ImageList.Images.Add(item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage);
228      node.ImageIndex = treeView.ImageList.Images.Count - 1;
229      node.SelectedImageIndex = node.ImageIndex;
230      foreach (TreeNode childNode in node.Nodes) {
231        RebuildImageList(childNode);
232      }
233    }
234  }
235}
Note: See TracBrowser for help on using the repository browser.