Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4767 was 4477, checked in by gkronber, 14 years ago

Merged r4458, r4459,r4462,r4464 from data analysis exploration branch into trunk. #1142

File size: 12.6 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[2790]3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2]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;
[2818]25using HeuristicLab.Collections;
[2520]26using HeuristicLab.MainForm;
[2]27
[2520]28namespace HeuristicLab.Core.Views {
[776]29  /// <summary>
[2655]30  /// The visual represenation of <see cref="Scope"/>.
[776]31  /// </summary>
[2917]32  [View("Scope View")]
[2520]33  [Content(typeof(Scope), true)]
[2727]34  [Content(typeof(IScope), false)]
[2676]35  public sealed partial class ScopeView : ItemView {
[2687]36    private Dictionary<IScope, TreeNode> scopeNodeTable;
37    private Dictionary<ScopeList, IScope> subScopesScopeTable;
[2]38
[776]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>
[2713]44    public new IScope Content {
45      get { return (IScope)base.Content; }
46      set { base.Content = value; }
[2]47    }
48
[776]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>
[2]53    public ScopeView() {
54      InitializeComponent();
[2687]55      scopeNodeTable = new Dictionary<IScope, TreeNode>();
56      subScopesScopeTable = new Dictionary<ScopeList, IScope>();
[2]57    }
58
[776]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>
[2713]63    protected override void OnContentChanged() {
64      base.OnContentChanged();
[3528]65      if (scopesTreeView.Nodes.Count > 0)
66        ClearTreeNode(scopesTreeView.Nodes[0]);
67      scopesTreeView.Nodes.Clear();
[2713]68      variableCollectionView.Content = null;
[3362]69      if (Content != null) {
[2713]70        scopesTreeView.Nodes.Add(CreateTreeNode(Content));
[2]71      }
72    }
73
[3904]74    protected override void SetEnabledStateOfControls() {
75      base.SetEnabledStateOfControls();
[3362]76      scopesTreeView.Enabled = Content != null;
[3588]77      variableCollectionView.Enabled = scopesTreeView.SelectedNode != null;
[3362]78    }
79
[3528]80    #region TreeNode Management
[2687]81    private TreeNode CreateTreeNode(IScope scope) {
[2]82      TreeNode node = new TreeNode();
83      node.Text = scope.Name;
84      node.Tag = scope;
85
86      scopeNodeTable.Add(scope, node);
[2655]87      scope.NameChanged += new EventHandler(Scope_NameChanged);
[3528]88      subScopesScopeTable.Add(scope.SubScopes, scope);
[2687]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);
[2]94      if (scope.SubScopes.Count > 0)
95        node.Nodes.Add(new TreeNode());
96      return node;
97    }
[2655]98
[3528]99    private void ClearTreeNode(TreeNode node) {
100      if (scopesTreeView.SelectedNode == node) {
101        scopesTreeView.SelectedNode = null;
102        UpdateVariables();
103      }
104
[2]105      foreach (TreeNode child in node.Nodes)
[3528]106        ClearTreeNode(child);
[2]107
[2687]108      IScope scope = node.Tag as IScope;
[2655]109      if (scope != null) {
[3528]110        scope.NameChanged -= new EventHandler(Scope_NameChanged);
[2]111        scopeNodeTable.Remove(scope);
[2687]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);
[2655]117        subScopesScopeTable.Remove(scope.SubScopes);
[2]118      }
119    }
[3528]120    #endregion
[2]121
122    #region TreeView Events
[3528]123    private void scopesTreeView_AfterSelect(object sender, TreeViewEventArgs e) {
124      UpdateVariables();
125    }
[2655]126    private void scopesTreeView_MouseDown(object sender, MouseEventArgs e) {
127      TreeNode node = scopesTreeView.GetNodeAt(e.X, e.Y);
[3528]128      scopesTreeView.SelectedNode = node;
129      UpdateVariables();
[2]130    }
131    private void scopesTreeView_BeforeExpand(object sender, TreeViewCancelEventArgs e) {
132      TreeNode node = e.Node;
[2687]133      IScope scope = (IScope)node.Tag;
[2]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;
[2687]141      IScope scope = (IScope)node.Tag;
[2]142
143      if (node.Nodes.Count > 0) {
144        for (int i = 0; i < node.Nodes.Count; i++)
[3528]145          ClearTreeNode(node.Nodes[i]);
[2]146        node.Nodes.Clear();
147        node.Nodes.Add(new TreeNode());
148      }
149    }
150    private void scopesTreeView_ItemDrag(object sender, ItemDragEventArgs e) {
[3432]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        }
[2]160      }
161    }
162    #endregion
163
[3528]164    #region Content Events
[2655]165    private void Scope_NameChanged(object sender, EventArgs e) {
166      if (InvokeRequired)
167        Invoke(new EventHandler(Scope_NameChanged), sender, e);
168      else {
[2687]169        IScope scope = (IScope)sender;
[3561]170        TreeNode node = null;
171        scopeNodeTable.TryGetValue(scope, out node);
172        if (node != null) node.Text = scope.Name;
[2]173      }
174    }
[2687]175    private void SubScopes_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
[2655]176      if (InvokeRequired)
[2687]177        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsAdded), sender, e);
[2655]178      else {
[3561]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();
[2655]195          }
196        }
197      }
[2]198    }
[2687]199    private void SubScopes_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
[2655]200      if (InvokeRequired)
[2687]201        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsRemoved), sender, e);
[2655]202      else {
[3561]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();
[2655]220          }
221        }
[2]222      }
223    }
[2687]224    private void SubScopes_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
[2655]225      if (InvokeRequired)
[2687]226        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsReplaced), sender, e);
[2655]227      else {
[3561]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            }
[2655]245          }
[2]246        }
247      }
248    }
[2687]249    private void SubScopes_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
[2655]250      if (InvokeRequired)
[2687]251        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsMoved), sender, e);
[2655]252      else {
[3561]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          }
[2655]267        }
[2]268      }
269    }
[2687]270    private void SubScopes_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
[2655]271      if (InvokeRequired)
[2687]272        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_CollectionReset), sender, e);
[2655]273      else {
[3561]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();
[2655]295          }
296        }
[2]297      }
298    }
299    #endregion
[3528]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
[2]312  }
313}
Note: See TracBrowser for help on using the repository browser.