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 |
|
---|
22 | using System;
|
---|
23 | using System.Windows.Forms;
|
---|
24 | using HeuristicLab.Collections;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
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")]
|
---|
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; }
|
---|
38 | set { base.Content = value; }
|
---|
39 | }
|
---|
40 |
|
---|
41 | public T SelectedItem {
|
---|
42 | get {
|
---|
43 | if (treeView.SelectedNode != null)
|
---|
44 | return (T)treeView.SelectedNode.Tag;
|
---|
45 | return null;
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | public ItemTreeView() {
|
---|
50 | InitializeComponent();
|
---|
51 | }
|
---|
52 |
|
---|
53 | protected override void DeregisterContentEvents() {
|
---|
54 | Content.ItemsAdded -= new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded);
|
---|
55 | Content.ItemsRemoved -= new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved);
|
---|
56 | Content.CollectionReset -= new CollectionItemsChangedEventHandler<T>(Content_CollectionReset);
|
---|
57 | foreach (TreeNode node in treeView.Nodes) {
|
---|
58 | DeregisterContentEvents((T)node.Tag, node);
|
---|
59 | }
|
---|
60 | base.DeregisterContentEvents();
|
---|
61 | }
|
---|
62 |
|
---|
63 | private void DeregisterContentEvents(T item, TreeNode node) {
|
---|
64 | item.ItemsAdded -= new CollectionItemsChangedEventHandler<IItemTree>(item_ItemsAdded);
|
---|
65 | item.ItemsRemoved -= new CollectionItemsChangedEventHandler<IItemTree>(item_ItemsRemoved);
|
---|
66 | item.CollectionReset -= new CollectionItemsChangedEventHandler<IItemTree>(item_CollectionReset);
|
---|
67 | item.ToStringChanged -= new EventHandler(item_ToStringChanged);
|
---|
68 | item.ItemImageChanged -= new EventHandler(item_ItemImageChanged);
|
---|
69 | foreach (TreeNode childNode in node.Nodes) {
|
---|
70 | DeregisterContentEvents((T)childNode.Tag, childNode);
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | protected override void RegisterContentEvents() {
|
---|
75 | base.RegisterContentEvents();
|
---|
76 | Content.ItemsAdded += new CollectionItemsChangedEventHandler<T>(Content_ItemsAdded);
|
---|
77 | Content.ItemsRemoved += new CollectionItemsChangedEventHandler<T>(Content_ItemsRemoved);
|
---|
78 | Content.CollectionReset += new CollectionItemsChangedEventHandler<T>(Content_CollectionReset);
|
---|
79 | foreach (TreeNode node in treeView.Nodes) {
|
---|
80 | RegisterContentEvents((T)node.Tag, node);
|
---|
81 | }
|
---|
82 | }
|
---|
83 |
|
---|
84 | private void RegisterContentEvents(T item, TreeNode node) {
|
---|
85 | item.ItemsAdded += new CollectionItemsChangedEventHandler<IItemTree>(item_ItemsAdded);
|
---|
86 | item.ItemsRemoved += new CollectionItemsChangedEventHandler<IItemTree>(item_ItemsRemoved);
|
---|
87 | item.CollectionReset += new CollectionItemsChangedEventHandler<IItemTree>(item_CollectionReset);
|
---|
88 | item.ToStringChanged += new EventHandler(item_ToStringChanged);
|
---|
89 | item.ItemImageChanged += new EventHandler(item_ItemImageChanged);
|
---|
90 | foreach (TreeNode childNode in node.Nodes) {
|
---|
91 | RegisterContentEvents((T)childNode.Tag, childNode);
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | #region Event Handlers (Content)
|
---|
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 |
|
---|
130 | private void item_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IItemTree> e) {
|
---|
131 | if (InvokeRequired) {
|
---|
132 | Invoke(new CollectionItemsChangedEventHandler<IItemTree>(item_ItemsAdded), sender, e);
|
---|
133 | } else {
|
---|
134 | TreeNode node = GetNodeByItem(sender as T);
|
---|
135 | if (node != null) {
|
---|
136 | foreach (T item in e.Items) {
|
---|
137 | AddChildNodes(item, node.Nodes, true);
|
---|
138 | node.ExpandAll();
|
---|
139 | UpdateNodeItemImage(node);
|
---|
140 | }
|
---|
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 {
|
---|
149 | TreeNode node = GetNodeByItem(sender as T);
|
---|
150 | if (node != null) {
|
---|
151 | foreach (T item in e.Items) {
|
---|
152 | RemoveChildNodes(item, node.Nodes);
|
---|
153 | }
|
---|
154 | RebuildImageList();
|
---|
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 {
|
---|
163 | TreeNode node = GetNodeByItem(sender as T);
|
---|
164 | if (node != null) {
|
---|
165 | foreach (T item in e.OldItems) {
|
---|
166 | RemoveChildNodes(item, node.Nodes);
|
---|
167 | }
|
---|
168 | RebuildImageList();
|
---|
169 | foreach (T item in e.Items) {
|
---|
170 | AddChildNodes(item, node.Nodes, true);
|
---|
171 | }
|
---|
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 {
|
---|
180 | var item = sender as T;
|
---|
181 | TreeNode node = GetNodeByItem(item);
|
---|
182 | if(node != null)
|
---|
183 | node.Text = item.ToString();
|
---|
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 {
|
---|
191 | var item = sender as T;
|
---|
192 | TreeNode node = GetNodeByItem(item);
|
---|
193 | if(node != null)
|
---|
194 | UpdateNodeItemImage(node);
|
---|
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) {
|
---|
203 | var item = treeView.SelectedNode.Tag as T;
|
---|
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 | }
|
---|
214 | #endregion
|
---|
215 |
|
---|
216 | protected override void OnContentChanged() {
|
---|
217 | base.OnContentChanged();
|
---|
218 | if (Content == null) {
|
---|
219 | ClearNodes();
|
---|
220 | detailsViewHost.Content = null;
|
---|
221 | } else {
|
---|
222 | ClearNodes();
|
---|
223 | foreach (T itemTree in Content) {
|
---|
224 | AddChildNodes(itemTree, treeView.Nodes, false);
|
---|
225 | }
|
---|
226 | foreach (TreeNode node in treeView.Nodes) {
|
---|
227 | UpdateNodeItemImage(node);
|
---|
228 | }
|
---|
229 | detailsViewHost.Content = null;
|
---|
230 | }
|
---|
231 | }
|
---|
232 |
|
---|
233 | private void ClearNodes() {
|
---|
234 | treeView.Nodes.Clear();
|
---|
235 | }
|
---|
236 |
|
---|
237 | private TreeNode AddChildNodes(T item, TreeNodeCollection nodes, bool registerEvents) {
|
---|
238 | TreeNode node = CreateTreeViewNode(item);
|
---|
239 | nodes.Add(node);
|
---|
240 | RebuildImageList(node);
|
---|
241 | RegisterContentEvents(item, node);
|
---|
242 | var childItems = item.GetChildItems();
|
---|
243 | foreach (T childItem in childItems) {
|
---|
244 | AddChildNodes(childItem, node.Nodes, registerEvents);
|
---|
245 | }
|
---|
246 | node.ExpandAll();
|
---|
247 | return node;
|
---|
248 | }
|
---|
249 |
|
---|
250 | private void RemoveChildNodes(T item, TreeNodeCollection treeNodeCollection) {
|
---|
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 | }
|
---|
260 |
|
---|
261 | private TreeNode GetNodeByItem(T item) {
|
---|
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 |
|
---|
271 | private TreeNode GetNodeByItem(TreeNode node, T item) {
|
---|
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 |
|
---|
283 | private TreeNode CreateTreeViewNode(T item) {
|
---|
284 | var node = new TreeNode(item.ToString());
|
---|
285 | node.Tag = item;
|
---|
286 | return node;
|
---|
287 | }
|
---|
288 |
|
---|
289 | private void UpdateNodeItemImage(TreeNode node) {
|
---|
290 | if (node == null) throw new ArgumentNullException();
|
---|
291 | var item = node.Tag as T;
|
---|
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 |
|
---|
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) {
|
---|
310 | detailsViewHost.Content = (IContent)treeView.SelectedNode.Tag;
|
---|
311 | }
|
---|
312 | #endregion
|
---|
313 |
|
---|
314 | private void RebuildImageList() {
|
---|
315 | treeView.ImageList.Images.Clear();
|
---|
316 | foreach (TreeNode node in treeView.Nodes) {
|
---|
317 | RebuildImageList(node);
|
---|
318 | }
|
---|
319 | }
|
---|
320 |
|
---|
321 | private void RebuildImageList(TreeNode node) {
|
---|
322 | var item = node.Tag as T;
|
---|
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 | }
|
---|
330 | }
|
---|
331 | }
|
---|