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 @ 6004

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

#1233

  • ItemTreeView robustifications
  • compactified the layout in HiveJobView
File size: 9.8 KB
RevLine 
[5637]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
[5675]22using System;
[5637]23using System.Windows.Forms;
[5675]24using HeuristicLab.Collections;
[5637]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() {
[5675]44      foreach (TreeNode node in treeView.Nodes) {
45        DeregisterContentEvents((IItemTree)node.Tag, node);
46      }
[5637]47      base.DeregisterContentEvents();
48    }
49
[5675]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);
[5793]54      item.ToStringChanged -= new EventHandler(item_ToStringChanged);
[5675]55      item.ItemImageChanged -= new EventHandler(item_ItemImageChanged);
56      foreach (TreeNode childNode in node.Nodes) {
57        DeregisterContentEvents((IItemTree)childNode.Tag, childNode);
58      }
59    }
60
[5637]61    protected override void RegisterContentEvents() {
62      base.RegisterContentEvents();
[5675]63      foreach (TreeNode node in treeView.Nodes) {
64        RegisterContentEvents((IItemTree)node.Tag, node);
65      }
[5637]66    }
67
[5675]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);
[5793]72      item.ToStringChanged += new EventHandler(item_ToStringChanged);
[5675]73      item.ItemImageChanged += new EventHandler(item_ItemImageChanged);
74      foreach (TreeNode childNode in node.Nodes) {
75        RegisterContentEvents((IItemTree)childNode.Tag, childNode);
76      }
77    }
78
[5637]79    #region Event Handlers (Content)
[5793]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);
[5797]85        if (node != null) {
86          foreach (IItemTree item in e.Items) {
87            AddChildNodes(item, node.Nodes, true);
88            node.ExpandAll();
89            UpdateNodeItemImage(node);
90          }
[5793]91        }
92      }
93    }
94
95    private void item_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IItemTree> e) {
96      if (InvokeRequired) {
97        Invoke(new CollectionItemsChangedEventHandler<IItemTree>(item_ItemsRemoved), sender, e);
98      } else {
99        TreeNode node = GetNodeByItem(sender as IItemTree);
[5797]100        if (node != null) {
101          foreach (IItemTree item in e.Items) {
102            RemoveChildNodes(item, node.Nodes);
103          }
104          RebuildImageList();
[5793]105        }
106      }
107    }
108
109    private void item_CollectionReset(object sender, CollectionItemsChangedEventArgs<IItemTree> e) {
110      if (InvokeRequired) {
111        Invoke(new CollectionItemsChangedEventHandler<IItemTree>(item_CollectionReset), sender, e);
112      } else {
113        TreeNode node = GetNodeByItem(sender as IItemTree);
[5797]114        if (node != null) {
115          foreach (IItemTree item in e.OldItems) {
116            RemoveChildNodes(item, node.Nodes);
117          }
118          RebuildImageList();
119          foreach (IItemTree item in e.Items) {
120            AddChildNodes(item, node.Nodes, true);
121          }
[5793]122        }
123      }
124    }
125
126    private void item_ToStringChanged(object sender, EventArgs e) {
127      if (InvokeRequired) {
128        Invoke(new EventHandler(item_ToStringChanged), sender, e);
129      } else {
130        var item = sender as IItemTree;
131        TreeNode node = GetNodeByItem(item);
[5797]132        if(node != null)
133          node.Text = item.ToString();
[5793]134      }
135    }
136
137    private void item_ItemImageChanged(object sender, EventArgs e) {
138      if (InvokeRequired) {
139        Invoke(new EventHandler(item_ItemImageChanged), sender, e);
140      } else {
141        var item = sender as IItemTree;
142        TreeNode node = GetNodeByItem(item);
[5797]143        if(node != null)
144          UpdateNodeItemImage(node);
[5793]145      }
146    }
147
148    private void treeView_DoubleClick(object sender, EventArgs e) {
149      if (InvokeRequired) {
150        Invoke(new EventHandler(treeView_DoubleClick), sender, e);
151      } else {
152        if (treeView.SelectedNode != null) {
153          var item = treeView.SelectedNode.Tag as IItemTree;
154          if (item != null) {
155            IContentView view = MainFormManager.MainForm.ShowContent(item);
156            if (view != null) {
157              view.ReadOnly = ReadOnly;
158              view.Locked = Locked;
159            }
160          }
161        }
162      }
163    }
[5637]164    #endregion
165
166    protected override void OnContentChanged() {
167      base.OnContentChanged();
168      if (Content == null) {
169        // Add code when content has been changed and is null
[5675]170        ClearNodes();
[5793]171        detailsViewHost.Content = null;
[5637]172      } else {
173        // Add code when content has been changed and is not null
[5675]174        ClearNodes();
175        AddChildNodes(Content, treeView.Nodes, false);
176        foreach (TreeNode node in treeView.Nodes) {
177          UpdateNodeItemImage(node);
178        }
[5637]179      }
180    }
181
[5675]182    private void ClearNodes() {
183      treeView.Nodes.Clear();
184    }
185
186    private void AddChildNodes(IItemTree item, TreeNodeCollection nodes, bool registerEvents) {
187      TreeNode node = CreateTreeViewNode(item);
[5637]188      nodes.Add(node);
[5675]189      RebuildImageList(node);
190      RegisterContentEvents(item, node);
191      var childItems = item.GetChildItems();
192      foreach (var childItem in childItems) {
193        AddChildNodes(childItem, node.Nodes, registerEvents);
[5637]194      }
[5675]195      node.ExpandAll();
[5637]196    }
197
[5675]198    private void RemoveChildNodes(IItemTree item, TreeNodeCollection treeNodeCollection) {
199      int idx = -1;
200      for (int i = 0; i < treeNodeCollection.Count; i++) {
201        if (treeNodeCollection[i].Tag == item) {
202          idx = i; break;
203        }
204      }
205      DeregisterContentEvents(item, treeNodeCollection[idx]);
206      treeNodeCollection.RemoveAt(idx);
207    }
[5793]208   
[5675]209    private TreeNode GetNodeByItem(IItemTree item) {
210      TreeNode found = null;
211      foreach (TreeNode node in treeView.Nodes) {
212        found = GetNodeByItem(node, item);
213        if (found != null)
214          return found;
215      }
216      return null;
217    }
218
219    private TreeNode GetNodeByItem(TreeNode node, IItemTree item) {
220      if (node.Tag == item)
221        return node;
222      TreeNode found = null;
223      foreach (TreeNode childNode in node.Nodes) {
224        found = GetNodeByItem(childNode, item);
225        if (found != null)
226          return found;
227      }
228      return null;
229    }
230
[5637]231    private TreeNode CreateTreeViewNode(IItemTree item) {
232      var node = new TreeNode(item.ToString());
233      node.Tag = item;
234      return node;
235    }
236
[5675]237    private void UpdateNodeItemImage(TreeNode node) {
238      if (node == null) throw new ArgumentNullException();
239      var item = node.Tag as IItemTree;
240      int i = node.ImageIndex;
241      this.imageList.Images[i] = item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage;
242      node.ImageIndex = -1;
243      node.ImageIndex = i;
244      node.SelectedImageIndex = node.ImageIndex;
245      foreach (TreeNode childNode in node.Nodes) {
246        UpdateNodeItemImage(childNode);
247      }
248    }
249
[5637]250    protected override void SetEnabledStateOfControls() {
251      base.SetEnabledStateOfControls();
252      // Enable or disable controls based on whether the content is null or the view is set readonly
253    }
254
255    #region Event Handlers (child controls)
256    // Put event handlers of child controls here.
257    private void treeView_AfterSelect(object sender, TreeViewEventArgs e) {
[5793]258      detailsViewHost.Content = (IContent)treeView.SelectedNode.Tag;
[5637]259    }
260    #endregion
261
[5675]262    private void RebuildImageList() {
263      treeView.ImageList.Images.Clear();
264      foreach (TreeNode node in treeView.Nodes) {
265        RebuildImageList(node);
266      }
267    }
[5637]268
[5675]269    private void RebuildImageList(TreeNode node) {
270      var item = node.Tag as IItemTree;
271      treeView.ImageList.Images.Add(item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage);
272      node.ImageIndex = treeView.ImageList.Images.Count - 1;
273      node.SelectedImageIndex = node.ImageIndex;
274      foreach (TreeNode childNode in node.Nodes) {
275        RebuildImageList(childNode);
276      }
277    }
[5637]278  }
279}
Note: See TracBrowser for help on using the repository browser.