[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] | 22 | using System;
|
---|
[5637] | 23 | using System.Windows.Forms;
|
---|
[5675] | 24 | using HeuristicLab.Collections;
|
---|
[5637] | 25 | using HeuristicLab.Common;
|
---|
[6006] | 26 | using HeuristicLab.Core;
|
---|
[5637] | 27 | using HeuristicLab.Core.Views;
|
---|
| 28 | using HeuristicLab.MainForm;
|
---|
| 29 | using HeuristicLab.MainForm.WindowsForms;
|
---|
| 30 |
|
---|
| 31 | namespace HeuristicLab.Clients.Hive.Views {
|
---|
| 32 | [View("ItemTree View")]
|
---|
[6006] | 33 | [Content(typeof(ItemCollection<>), IsDefaultView = false)]
|
---|
| 34 | [Content(typeof(ItemCollection<IItemTree>), IsDefaultView = true)]
|
---|
| 35 | public sealed partial class ItemTreeView<T> : ItemView where T : class, IItemTree {
|
---|
| 36 | public new ItemCollection<T> Content {
|
---|
| 37 | get { return (ItemCollection<T>)base.Content; }
|
---|
[5637] | 38 | set { base.Content = value; }
|
---|
| 39 | }
|
---|
| 40 |
|
---|
[6006] | 41 | public T SelectedItem {
|
---|
| 42 | get {
|
---|
| 43 | if (treeView.SelectedNode != null)
|
---|
| 44 | return (T)treeView.SelectedNode.Tag;
|
---|
| 45 | return null;
|
---|
| 46 | }
|
---|
| 47 | }
|
---|
| 48 |
|
---|
[5637] | 49 | public ItemTreeView() {
|
---|
| 50 | InitializeComponent();
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | protected override void DeregisterContentEvents() {
|
---|
[6006] | 54 | Content.ItemsAdded -= new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded);
|
---|
| 55 | Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved);
|
---|
| 56 | Content.CollectionReset -= new CollectionItemsChangedEventHandler<T>(Content_CollectionReset);
|
---|
[5675] | 57 | foreach (TreeNode node in treeView.Nodes) {
|
---|
[6006] | 58 | DeregisterContentEvents((T)node.Tag, node);
|
---|
[5675] | 59 | }
|
---|
[5637] | 60 | base.DeregisterContentEvents();
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[6006] | 63 | private void DeregisterContentEvents(T item, TreeNode node) {
|
---|
[5675] | 64 | item.ItemsAdded -= new CollectionItemsChangedEventHandler<IItemTree>(item_ItemsAdded);
|
---|
| 65 | item.ItemsRemoved -= new CollectionItemsChangedEventHandler<IItemTree>(item_ItemsRemoved);
|
---|
| 66 | item.CollectionReset -= new CollectionItemsChangedEventHandler<IItemTree>(item_CollectionReset);
|
---|
[5793] | 67 | item.ToStringChanged -= new EventHandler(item_ToStringChanged);
|
---|
[5675] | 68 | item.ItemImageChanged -= new EventHandler(item_ItemImageChanged);
|
---|
| 69 | foreach (TreeNode childNode in node.Nodes) {
|
---|
[6006] | 70 | DeregisterContentEvents((T)childNode.Tag, childNode);
|
---|
[5675] | 71 | }
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[5637] | 74 | protected override void RegisterContentEvents() {
|
---|
| 75 | base.RegisterContentEvents();
|
---|
[6006] | 76 | Content.ItemsAdded += new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded);
|
---|
| 77 | Content.ItemsRemoved += new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved);
|
---|
| 78 | Content.CollectionReset += new CollectionItemsChangedEventHandler<T>(Content_CollectionReset);
|
---|
[5675] | 79 | foreach (TreeNode node in treeView.Nodes) {
|
---|
[6006] | 80 | RegisterContentEvents((T)node.Tag, node);
|
---|
[5675] | 81 | }
|
---|
[5637] | 82 | }
|
---|
| 83 |
|
---|
[6006] | 84 | private void RegisterContentEvents(T item, TreeNode node) {
|
---|
[5675] | 85 | item.ItemsAdded += new CollectionItemsChangedEventHandler<IItemTree>(item_ItemsAdded);
|
---|
| 86 | item.ItemsRemoved += new CollectionItemsChangedEventHandler<IItemTree>(item_ItemsRemoved);
|
---|
| 87 | item.CollectionReset += new CollectionItemsChangedEventHandler<IItemTree>(item_CollectionReset);
|
---|
[5793] | 88 | item.ToStringChanged += new EventHandler(item_ToStringChanged);
|
---|
[5675] | 89 | item.ItemImageChanged += new EventHandler(item_ItemImageChanged);
|
---|
| 90 | foreach (TreeNode childNode in node.Nodes) {
|
---|
[6006] | 91 | RegisterContentEvents((T)childNode.Tag, childNode);
|
---|
[5675] | 92 | }
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[5637] | 95 | #region Event Handlers (Content)
|
---|
[6006] | 96 | private void Content_ItemsAdded(object sender, CollectionItemsChangedEventArgs<T> e) {
|
---|
| 97 | if (InvokeRequired) {
|
---|
| 98 | Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded), sender, e);
|
---|
| 99 | } else {
|
---|
| 100 | foreach (T item in e.Items) {
|
---|
| 101 | var node = AddChildNodes(item, treeView.Nodes, true);
|
---|
| 102 | UpdateNodeItemImage(node);
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
| 106 | private void Content_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<T> e) {
|
---|
| 107 | if (InvokeRequired) {
|
---|
| 108 | Invoke(new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved), sender, e);
|
---|
| 109 | } else {
|
---|
| 110 | foreach (T item in e.Items) {
|
---|
| 111 | RemoveChildNodes(item, treeView.Nodes);
|
---|
| 112 | }
|
---|
| 113 | RebuildImageList();
|
---|
| 114 | }
|
---|
| 115 | }
|
---|
| 116 | private void Content_CollectionReset(object sender, CollectionItemsChangedEventArgs<T> e) {
|
---|
| 117 | if (InvokeRequired) {
|
---|
| 118 | Invoke(new CollectionItemsChangedEventHandler<T>(Content_CollectionReset), sender, e);
|
---|
| 119 | } else {
|
---|
| 120 | foreach (T item in e.OldItems) {
|
---|
| 121 | RemoveChildNodes(item, treeView.Nodes);
|
---|
| 122 | }
|
---|
| 123 | RebuildImageList();
|
---|
| 124 | foreach (T item in e.Items) {
|
---|
| 125 | AddChildNodes(item, treeView.Nodes, true);
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
| 129 |
|
---|
[5793] | 130 | private void item_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IItemTree> e) {
|
---|
| 131 | if (InvokeRequired) {
|
---|
| 132 | Invoke(new CollectionItemsChangedEventHandler<IItemTree>(item_ItemsAdded), sender, e);
|
---|
| 133 | } else {
|
---|
[6006] | 134 | TreeNode node = GetNodeByItem(sender as T);
|
---|
[5797] | 135 | if (node != null) {
|
---|
[6006] | 136 | foreach (T item in e.Items) {
|
---|
[5797] | 137 | AddChildNodes(item, node.Nodes, true);
|
---|
| 138 | node.ExpandAll();
|
---|
| 139 | UpdateNodeItemImage(node);
|
---|
| 140 | }
|
---|
[5793] | 141 | }
|
---|
| 142 | }
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | private void item_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IItemTree> e) {
|
---|
| 146 | if (InvokeRequired) {
|
---|
| 147 | Invoke(new CollectionItemsChangedEventHandler<IItemTree>(item_ItemsRemoved), sender, e);
|
---|
| 148 | } else {
|
---|
[6006] | 149 | TreeNode node = GetNodeByItem(sender as T);
|
---|
[5797] | 150 | if (node != null) {
|
---|
[6006] | 151 | foreach (T item in e.Items) {
|
---|
[5797] | 152 | RemoveChildNodes(item, node.Nodes);
|
---|
| 153 | }
|
---|
| 154 | RebuildImageList();
|
---|
[5793] | 155 | }
|
---|
| 156 | }
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | private void item_CollectionReset(object sender, CollectionItemsChangedEventArgs<IItemTree> e) {
|
---|
| 160 | if (InvokeRequired) {
|
---|
| 161 | Invoke(new CollectionItemsChangedEventHandler<IItemTree>(item_CollectionReset), sender, e);
|
---|
| 162 | } else {
|
---|
[6006] | 163 | TreeNode node = GetNodeByItem(sender as T);
|
---|
[5797] | 164 | if (node != null) {
|
---|
[6006] | 165 | foreach (T item in e.OldItems) {
|
---|
[5797] | 166 | RemoveChildNodes(item, node.Nodes);
|
---|
| 167 | }
|
---|
| 168 | RebuildImageList();
|
---|
[6006] | 169 | foreach (T item in e.Items) {
|
---|
[5797] | 170 | AddChildNodes(item, node.Nodes, true);
|
---|
| 171 | }
|
---|
[5793] | 172 | }
|
---|
| 173 | }
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | private void item_ToStringChanged(object sender, EventArgs e) {
|
---|
| 177 | if (InvokeRequired) {
|
---|
| 178 | Invoke(new EventHandler(item_ToStringChanged), sender, e);
|
---|
| 179 | } else {
|
---|
[6006] | 180 | var item = sender as T;
|
---|
[5793] | 181 | TreeNode node = GetNodeByItem(item);
|
---|
[5797] | 182 | if(node != null)
|
---|
| 183 | node.Text = item.ToString();
|
---|
[5793] | 184 | }
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | private void item_ItemImageChanged(object sender, EventArgs e) {
|
---|
| 188 | if (InvokeRequired) {
|
---|
| 189 | Invoke(new EventHandler(item_ItemImageChanged), sender, e);
|
---|
| 190 | } else {
|
---|
[6006] | 191 | var item = sender as T;
|
---|
[5793] | 192 | TreeNode node = GetNodeByItem(item);
|
---|
[5797] | 193 | if(node != null)
|
---|
| 194 | UpdateNodeItemImage(node);
|
---|
[5793] | 195 | }
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | private void treeView_DoubleClick(object sender, EventArgs e) {
|
---|
| 199 | if (InvokeRequired) {
|
---|
| 200 | Invoke(new EventHandler(treeView_DoubleClick), sender, e);
|
---|
| 201 | } else {
|
---|
| 202 | if (treeView.SelectedNode != null) {
|
---|
[6006] | 203 | var item = treeView.SelectedNode.Tag as T;
|
---|
[5793] | 204 | if (item != null) {
|
---|
| 205 | IContentView view = MainFormManager.MainForm.ShowContent(item);
|
---|
| 206 | if (view != null) {
|
---|
| 207 | view.ReadOnly = ReadOnly;
|
---|
| 208 | view.Locked = Locked;
|
---|
| 209 | }
|
---|
| 210 | }
|
---|
| 211 | }
|
---|
| 212 | }
|
---|
| 213 | }
|
---|
[5637] | 214 | #endregion
|
---|
| 215 |
|
---|
| 216 | protected override void OnContentChanged() {
|
---|
| 217 | base.OnContentChanged();
|
---|
| 218 | if (Content == null) {
|
---|
[5675] | 219 | ClearNodes();
|
---|
[5793] | 220 | detailsViewHost.Content = null;
|
---|
[5637] | 221 | } else {
|
---|
[5675] | 222 | ClearNodes();
|
---|
[6006] | 223 | foreach (T itemTree in Content) {
|
---|
| 224 | AddChildNodes(itemTree, treeView.Nodes, false);
|
---|
| 225 | }
|
---|
[5675] | 226 | foreach (TreeNode node in treeView.Nodes) {
|
---|
| 227 | UpdateNodeItemImage(node);
|
---|
| 228 | }
|
---|
[6006] | 229 | detailsViewHost.Content = null;
|
---|
[5637] | 230 | }
|
---|
| 231 | }
|
---|
| 232 |
|
---|
[5675] | 233 | private void ClearNodes() {
|
---|
| 234 | treeView.Nodes.Clear();
|
---|
| 235 | }
|
---|
| 236 |
|
---|
[6006] | 237 | private TreeNode AddChildNodes(T item, TreeNodeCollection nodes, bool registerEvents) {
|
---|
[5675] | 238 | TreeNode node = CreateTreeViewNode(item);
|
---|
[5637] | 239 | nodes.Add(node);
|
---|
[5675] | 240 | RebuildImageList(node);
|
---|
| 241 | RegisterContentEvents(item, node);
|
---|
| 242 | var childItems = item.GetChildItems();
|
---|
[6006] | 243 | foreach (T childItem in childItems) {
|
---|
[5675] | 244 | AddChildNodes(childItem, node.Nodes, registerEvents);
|
---|
[5637] | 245 | }
|
---|
[5675] | 246 | node.ExpandAll();
|
---|
[6006] | 247 | return node;
|
---|
[5637] | 248 | }
|
---|
| 249 |
|
---|
[6006] | 250 | private void RemoveChildNodes(T item, TreeNodeCollection treeNodeCollection) {
|
---|
[5675] | 251 | int idx = -1;
|
---|
| 252 | for (int i = 0; i < treeNodeCollection.Count; i++) {
|
---|
| 253 | if (treeNodeCollection[i].Tag == item) {
|
---|
| 254 | idx = i; break;
|
---|
| 255 | }
|
---|
| 256 | }
|
---|
| 257 | DeregisterContentEvents(item, treeNodeCollection[idx]);
|
---|
| 258 | treeNodeCollection.RemoveAt(idx);
|
---|
| 259 | }
|
---|
[5793] | 260 |
|
---|
[6006] | 261 | private TreeNode GetNodeByItem(T item) {
|
---|
[5675] | 262 | TreeNode found = null;
|
---|
| 263 | foreach (TreeNode node in treeView.Nodes) {
|
---|
| 264 | found = GetNodeByItem(node, item);
|
---|
| 265 | if (found != null)
|
---|
| 266 | return found;
|
---|
| 267 | }
|
---|
| 268 | return null;
|
---|
| 269 | }
|
---|
| 270 |
|
---|
[6006] | 271 | private TreeNode GetNodeByItem(TreeNode node, T item) {
|
---|
[5675] | 272 | if (node.Tag == item)
|
---|
| 273 | return node;
|
---|
| 274 | TreeNode found = null;
|
---|
| 275 | foreach (TreeNode childNode in node.Nodes) {
|
---|
| 276 | found = GetNodeByItem(childNode, item);
|
---|
| 277 | if (found != null)
|
---|
| 278 | return found;
|
---|
| 279 | }
|
---|
| 280 | return null;
|
---|
| 281 | }
|
---|
| 282 |
|
---|
[6006] | 283 | private TreeNode CreateTreeViewNode(T item) {
|
---|
[5637] | 284 | var node = new TreeNode(item.ToString());
|
---|
| 285 | node.Tag = item;
|
---|
| 286 | return node;
|
---|
| 287 | }
|
---|
| 288 |
|
---|
[5675] | 289 | private void UpdateNodeItemImage(TreeNode node) {
|
---|
| 290 | if (node == null) throw new ArgumentNullException();
|
---|
[6006] | 291 | var item = node.Tag as T;
|
---|
[5675] | 292 | int i = node.ImageIndex;
|
---|
| 293 | this.imageList.Images[i] = item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage;
|
---|
| 294 | node.ImageIndex = -1;
|
---|
| 295 | node.ImageIndex = i;
|
---|
| 296 | node.SelectedImageIndex = node.ImageIndex;
|
---|
| 297 | foreach (TreeNode childNode in node.Nodes) {
|
---|
| 298 | UpdateNodeItemImage(childNode);
|
---|
| 299 | }
|
---|
| 300 | }
|
---|
| 301 |
|
---|
[5637] | 302 | protected override void SetEnabledStateOfControls() {
|
---|
| 303 | base.SetEnabledStateOfControls();
|
---|
| 304 | // Enable or disable controls based on whether the content is null or the view is set readonly
|
---|
| 305 | }
|
---|
| 306 |
|
---|
| 307 | #region Event Handlers (child controls)
|
---|
| 308 | // Put event handlers of child controls here.
|
---|
| 309 | private void treeView_AfterSelect(object sender, TreeViewEventArgs e) {
|
---|
[5793] | 310 | detailsViewHost.Content = (IContent)treeView.SelectedNode.Tag;
|
---|
[5637] | 311 | }
|
---|
| 312 | #endregion
|
---|
| 313 |
|
---|
[5675] | 314 | private void RebuildImageList() {
|
---|
| 315 | treeView.ImageList.Images.Clear();
|
---|
| 316 | foreach (TreeNode node in treeView.Nodes) {
|
---|
| 317 | RebuildImageList(node);
|
---|
| 318 | }
|
---|
| 319 | }
|
---|
[5637] | 320 |
|
---|
[5675] | 321 | private void RebuildImageList(TreeNode node) {
|
---|
[6006] | 322 | var item = node.Tag as T;
|
---|
[5675] | 323 | treeView.ImageList.Images.Add(item == null ? HeuristicLab.Common.Resources.VSImageLibrary.Nothing : item.ItemImage);
|
---|
| 324 | node.ImageIndex = treeView.ImageList.Images.Count - 1;
|
---|
| 325 | node.SelectedImageIndex = node.ImageIndex;
|
---|
| 326 | foreach (TreeNode childNode in node.Nodes) {
|
---|
| 327 | RebuildImageList(childNode);
|
---|
| 328 | }
|
---|
| 329 | }
|
---|
[5637] | 330 | }
|
---|
| 331 | }
|
---|