Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core.Views/3.3/ScopeView.cs @ 4040

Last change on this file since 4040 was 3904, checked in by mkommend, 14 years ago

Added SetEnabledStateOfControls as protected virtual method in !View. Therefore the overloading of OnReadOnlyChanged and OnLockedChanged got obsolete in most views, because the method got called in the !View respectively ContentView. (ticket #1021)

File size: 12.6 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.Collections.Generic;
24using System.Windows.Forms;
25using HeuristicLab.Collections;
26using HeuristicLab.MainForm;
27
28namespace HeuristicLab.Core.Views {
29  /// <summary>
30  /// The visual represenation of <see cref="Scope"/>.
31  /// </summary>
32  [View("Scope View")]
33  [Content(typeof(Scope), true)]
34  [Content(typeof(IScope), false)]
35  public sealed partial class ScopeView : ItemView {
36    private Dictionary<IScope, TreeNode> scopeNodeTable;
37    private Dictionary<ScopeList, IScope> subScopesScopeTable;
38
39    /// <summary>
40    /// Gets or sets the scope to represent visually.
41    /// </summary>
42    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
43    /// No own data storage present.</remarks>
44    public new IScope Content {
45      get { return (IScope)base.Content; }
46      set { base.Content = value; }
47    }
48
49    /// <summary>
50    /// Initializes a new instance of <see cref="ScopeView"/> with caption "Scope" and
51    /// property <see cref="AutomaticUpdating"/> set to <c>false</c>.
52    /// </summary>
53    public ScopeView() {
54      InitializeComponent();
55      scopeNodeTable = new Dictionary<IScope, TreeNode>();
56      subScopesScopeTable = new Dictionary<ScopeList, IScope>();
57    }
58
59    /// <summary>
60    /// Updates all controls with the latest data of the model.
61    /// </summary>
62    /// <remarks>Calls <see cref="ViewBase.UpdateControls"/> of base class <see cref="ViewBase"/>.</remarks>
63    protected override void OnContentChanged() {
64      base.OnContentChanged();
65      if (scopesTreeView.Nodes.Count > 0)
66        ClearTreeNode(scopesTreeView.Nodes[0]);
67      scopesTreeView.Nodes.Clear();
68      variableCollectionView.Content = null;
69      if (Content != null) {
70        scopesTreeView.Nodes.Add(CreateTreeNode(Content));
71      }
72    }
73
74    protected override void SetEnabledStateOfControls() {
75      base.SetEnabledStateOfControls();
76      scopesTreeView.Enabled = Content != null;
77      variableCollectionView.Enabled = scopesTreeView.SelectedNode != null;
78    }
79
80    #region TreeNode Management
81    private TreeNode CreateTreeNode(IScope scope) {
82      TreeNode node = new TreeNode();
83      node.Text = scope.Name;
84      node.Tag = scope;
85
86      scopeNodeTable.Add(scope, node);
87      scope.NameChanged += new EventHandler(Scope_NameChanged);
88      subScopesScopeTable.Add(scope.SubScopes, scope);
89      scope.SubScopes.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsAdded);
90      scope.SubScopes.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsRemoved);
91      scope.SubScopes.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsReplaced);
92      scope.SubScopes.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsMoved);
93      scope.SubScopes.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_CollectionReset);
94      if (scope.SubScopes.Count > 0)
95        node.Nodes.Add(new TreeNode());
96      return node;
97    }
98
99    private void ClearTreeNode(TreeNode node) {
100      if (scopesTreeView.SelectedNode == node) {
101        scopesTreeView.SelectedNode = null;
102        UpdateVariables();
103      }
104
105      foreach (TreeNode child in node.Nodes)
106        ClearTreeNode(child);
107
108      IScope scope = node.Tag as IScope;
109      if (scope != null) {
110        scope.NameChanged -= new EventHandler(Scope_NameChanged);
111        scopeNodeTable.Remove(scope);
112        scope.SubScopes.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsAdded);
113        scope.SubScopes.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsRemoved);
114        scope.SubScopes.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsReplaced);
115        scope.SubScopes.ItemsMoved -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsMoved);
116        scope.SubScopes.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_CollectionReset);
117        subScopesScopeTable.Remove(scope.SubScopes);
118      }
119    }
120    #endregion
121
122    #region TreeView Events
123    private void scopesTreeView_AfterSelect(object sender, TreeViewEventArgs e) {
124      UpdateVariables();
125    }
126    private void scopesTreeView_MouseDown(object sender, MouseEventArgs e) {
127      TreeNode node = scopesTreeView.GetNodeAt(e.X, e.Y);
128      scopesTreeView.SelectedNode = node;
129      UpdateVariables();
130    }
131    private void scopesTreeView_BeforeExpand(object sender, TreeViewCancelEventArgs e) {
132      TreeNode node = e.Node;
133      IScope scope = (IScope)node.Tag;
134
135      node.Nodes.Clear();
136      for (int i = 0; i < scope.SubScopes.Count; i++)
137        node.Nodes.Add(CreateTreeNode(scope.SubScopes[i]));
138    }
139    private void scopesTreeView_AfterCollapse(object sender, System.Windows.Forms.TreeViewEventArgs e) {
140      TreeNode node = e.Node;
141      IScope scope = (IScope)node.Tag;
142
143      if (node.Nodes.Count > 0) {
144        for (int i = 0; i < node.Nodes.Count; i++)
145          ClearTreeNode(node.Nodes[i]);
146        node.Nodes.Clear();
147        node.Nodes.Add(new TreeNode());
148      }
149    }
150    private void scopesTreeView_ItemDrag(object sender, ItemDragEventArgs e) {
151      if (!Locked) {
152        TreeNode node = (TreeNode)e.Item;
153        IScope scope = node.Tag as IScope;
154        if (scope != null) {
155          DataObject data = new DataObject();
156          data.SetData("Type", scope.GetType());
157          data.SetData("Value", scope);
158          DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
159        }
160      }
161    }
162    #endregion
163
164    #region Content Events
165    private void Scope_NameChanged(object sender, EventArgs e) {
166      if (InvokeRequired)
167        Invoke(new EventHandler(Scope_NameChanged), sender, e);
168      else {
169        IScope scope = (IScope)sender;
170        TreeNode node = null;
171        scopeNodeTable.TryGetValue(scope, out node);
172        if (node != null) node.Text = scope.Name;
173      }
174    }
175    private void SubScopes_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
176      if (InvokeRequired)
177        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsAdded), sender, e);
178      else {
179        IScope parentScope = null;
180        subScopesScopeTable.TryGetValue((ScopeList)sender, out parentScope);
181        if (parentScope != null) {
182          TreeNode parentNode = null;
183          scopeNodeTable.TryGetValue(parentScope, out parentNode);
184          if (parentNode != null) {
185            scopesTreeView.BeginUpdate();
186            if (parentNode.IsExpanded) {
187              foreach (IndexedItem<IScope> item in e.Items) {
188                TreeNode node = CreateTreeNode(item.Value);
189                parentNode.Nodes.Insert(item.Index, node);
190              }
191            } else if (parentNode.Nodes.Count == 0) {
192              parentNode.Nodes.Add(new TreeNode());
193            }
194            scopesTreeView.EndUpdate();
195          }
196        }
197      }
198    }
199    private void SubScopes_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
200      if (InvokeRequired)
201        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsRemoved), sender, e);
202      else {
203        IScope parentScope = null;
204        subScopesScopeTable.TryGetValue((ScopeList)sender, out parentScope);
205        if (parentScope != null) {
206          TreeNode parentNode = null;
207          scopeNodeTable.TryGetValue(parentScope, out parentNode);
208          if (parentNode != null) {
209            scopesTreeView.BeginUpdate();
210            if (parentNode.IsExpanded) {
211              foreach (IndexedItem<IScope> item in e.Items) {
212                TreeNode node = scopeNodeTable[item.Value];
213                ClearTreeNode(node);
214                node.Remove();
215              }
216            } else if (parentScope.SubScopes.Count == 0) {
217              parentNode.Nodes.Clear();
218            }
219            scopesTreeView.EndUpdate();
220          }
221        }
222      }
223    }
224    private void SubScopes_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
225      if (InvokeRequired)
226        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsReplaced), sender, e);
227      else {
228        IScope parentScope = null;
229        subScopesScopeTable.TryGetValue((ScopeList)sender, out parentScope);
230        if (parentScope != null) {
231          TreeNode parentNode = null;
232          scopeNodeTable.TryGetValue(parentScope, out parentNode);
233          if (parentNode != null) {
234            scopesTreeView.BeginUpdate();
235            if (parentNode.IsExpanded) {
236              foreach (IndexedItem<IScope> item in e.Items) {
237                TreeNode node = parentNode.Nodes[item.Index];
238                ClearTreeNode(node);
239                node.Remove();
240                node = CreateTreeNode(item.Value);
241                parentNode.Nodes.Insert(item.Index, node);
242              }
243              scopesTreeView.EndUpdate();
244            }
245          }
246        }
247      }
248    }
249    private void SubScopes_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
250      if (InvokeRequired)
251        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsMoved), sender, e);
252      else {
253        IScope parentScope = null;
254        subScopesScopeTable.TryGetValue((ScopeList)sender, out parentScope);
255        if (parentScope != null) {
256          TreeNode parentNode = null;
257          scopeNodeTable.TryGetValue(parentScope, out parentNode);
258          if (parentNode != null) {
259            scopesTreeView.BeginUpdate();
260            if (parentNode.IsExpanded) {
261              parentNode.Nodes.Clear();
262              foreach (IndexedItem<IScope> item in e.Items)
263                parentNode.Nodes.Insert(item.Index, scopeNodeTable[item.Value]);
264            }
265            scopesTreeView.EndUpdate();
266          }
267        }
268      }
269    }
270    private void SubScopes_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
271      if (InvokeRequired)
272        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_CollectionReset), sender, e);
273      else {
274        IScope parentScope = null;
275        subScopesScopeTable.TryGetValue((ScopeList)sender, out parentScope);
276        if (parentScope != null) {
277          TreeNode parentNode = null;
278          scopeNodeTable.TryGetValue(parentScope, out parentNode);
279          if (parentNode != null) {
280            scopesTreeView.BeginUpdate();
281            if (parentNode.IsExpanded) {
282              foreach (TreeNode node in parentNode.Nodes)
283                ClearTreeNode(node);
284              parentNode.Nodes.Clear();
285              foreach (IndexedItem<IScope> item in e.Items) {
286                TreeNode node = CreateTreeNode(item.Value);
287                parentNode.Nodes.Insert(item.Index, node);
288              }
289            } else {
290              parentNode.Nodes.Clear();
291              if (parentScope.SubScopes.Count > 0)
292                parentNode.Nodes.Add(new TreeNode());
293            }
294            scopesTreeView.EndUpdate();
295          }
296        }
297      }
298    }
299    #endregion
300
301    #region Helpers
302    private void UpdateVariables() {
303      if (scopesTreeView.SelectedNode == null) {
304        variableCollectionView.Content = null;
305        variableCollectionView.Enabled = false;
306      } else {
307        variableCollectionView.Enabled = true;
308        variableCollectionView.Content = ((IScope)scopesTreeView.SelectedNode.Tag).Variables;
309      }
310    }
311    #endregion
312  }
313}
Note: See TracBrowser for help on using the repository browser.