Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ItemTreeViews/3.3/Views/NamedItemCollectionTreeView.cs @ 15356

Last change on this file since 15356 was 14335, checked in by bwerth, 8 years ago

#2684 initial commit of TreeView

File size: 3.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Windows.Forms;
23using HeuristicLab.Collections;
24using HeuristicLab.Core;
25using HeuristicLab.MainForm;
26
27namespace HeuristicLab.ItemTreeViews.Views {
28  [View("NamedItemCollection Tree View")]
29  [Content(typeof(NamedItemCollection<>), false)]
30  public partial class NamedItemCollectionTreeView<T> : ItemCollectionTreeView<T> where T : class, INamedItem {
31    public new IKeyedItemCollection<string, T> Content
32    {
33      get { return (IKeyedItemCollection<string, T>)base.Content; }
34      set { base.Content = value; }
35    }
36
37    public NamedItemCollectionTreeView() {
38      InitializeComponent();
39    }
40
41    protected override void DeregisterContentEvents() {
42      Content.ItemsReplaced -= Content_ItemsReplaced;
43      base.DeregisterContentEvents();
44    }
45    protected override void RegisterContentEvents() {
46      base.RegisterContentEvents();
47      Content.ItemsReplaced += Content_ItemsReplaced;
48    }
49
50    protected override void OnContentChanged() {
51      string selectedName = null;
52      if ((ItemsTreeView.SelectedNode != null ? ItemsTreeView.SelectedNode.Tag as T : null) is T)
53        selectedName = ((T)ItemsTreeView.SelectedNode.Tag).Name;
54
55      base.OnContentChanged();
56
57      if (selectedName == null) return;
58      foreach (TreeNode item in ItemsTreeView.Nodes)
59        if ((item.Tag != null) && ((T)item.Tag).Name.Equals(selectedName))
60          ItemsTreeView.SelectedNode = item;
61    }
62
63    protected override T CreateItem() {
64      T item = base.CreateItem();
65      if (item != null) {
66        item.Name = GetUniqueName(item.Name);
67      }
68      return item;
69    }
70
71    protected override TreeNode CreateTreeViewNode(T item) {
72      var listViewItem = base.CreateTreeViewNode(item);
73      if (!string.IsNullOrEmpty(item != null ? item.Description : null)) {
74        listViewItem.ToolTipText = item.ItemName + ": " + item.Description;
75      }
76      return listViewItem;
77    }
78    protected override void UpdateTreeViewItemText(TreeNode treeViewNode) {
79      base.UpdateTreeViewItemText(treeViewNode);
80      T item = treeViewNode.Tag as T;
81      if (!string.IsNullOrEmpty(item != null ? item.Description : null)) {
82        treeViewNode.ToolTipText = item.ItemName + ": " + item.Description;
83      }
84    }
85
86    #region Content Events
87    protected virtual void Content_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<T> e) {
88      if (InvokeRequired)
89        Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsReplaced), sender, e);
90      else {
91        ItemsTreeView.BeginUpdate();
92        foreach (T item in e.Items)
93          UpdateTreeViewItemText(GetTreeNodeForItem(item));
94        ItemsTreeView.EndUpdate();
95      }
96    }
97    #endregion
98
99    #region Helpers
100    protected virtual string GetUniqueName(string originalName) {
101      if (!Content.ContainsKey(originalName))
102        return originalName;
103
104      string name;
105      int index = 0;
106      do {
107        index++;
108        name = originalName + index;
109      } while (Content.ContainsKey(name));
110      return name;
111
112    }
113    #endregion
114  }
115}
Note: See TracBrowser for help on using the repository browser.