Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Core.Views/3.3/ScopeView.cs @ 12808

Last change on this file since 12808 was 12009, checked in by ascheibe, 10 years ago

#2212 updated copyright year

File size: 13.1 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[12009]3 * Copyright (C) 2002-2015 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
[5237]59    /// <summary>
60    /// Clean up any resources being used.
61    /// </summary>
62    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
63    protected override void Dispose(bool disposing) {
64      if (disposing) {
65        if (scopesTreeView.Nodes.Count > 0) {
66          ClearTreeNode(scopesTreeView.Nodes[0]);
67        }
68        if (components != null) components.Dispose();
69      }
70      base.Dispose(disposing);
71    }
72
[776]73    /// <summary>
74    /// Updates all controls with the latest data of the model.
75    /// </summary>
76    /// <remarks>Calls <see cref="ViewBase.UpdateControls"/> of base class <see cref="ViewBase"/>.</remarks>
[2713]77    protected override void OnContentChanged() {
78      base.OnContentChanged();
[3528]79      if (scopesTreeView.Nodes.Count > 0)
80        ClearTreeNode(scopesTreeView.Nodes[0]);
81      scopesTreeView.Nodes.Clear();
[2713]82      variableCollectionView.Content = null;
[3362]83      if (Content != null) {
[2713]84        scopesTreeView.Nodes.Add(CreateTreeNode(Content));
[2]85      }
86    }
87
[3904]88    protected override void SetEnabledStateOfControls() {
89      base.SetEnabledStateOfControls();
[3362]90      scopesTreeView.Enabled = Content != null;
[3588]91      variableCollectionView.Enabled = scopesTreeView.SelectedNode != null;
[3362]92    }
93
[3528]94    #region TreeNode Management
[2687]95    private TreeNode CreateTreeNode(IScope scope) {
[2]96      TreeNode node = new TreeNode();
97      node.Text = scope.Name;
98      node.Tag = scope;
99
100      scopeNodeTable.Add(scope, node);
[2655]101      scope.NameChanged += new EventHandler(Scope_NameChanged);
[3528]102      subScopesScopeTable.Add(scope.SubScopes, scope);
[2687]103      scope.SubScopes.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsAdded);
104      scope.SubScopes.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsRemoved);
105      scope.SubScopes.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsReplaced);
106      scope.SubScopes.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsMoved);
107      scope.SubScopes.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_CollectionReset);
[2]108      if (scope.SubScopes.Count > 0)
109        node.Nodes.Add(new TreeNode());
110      return node;
111    }
[2655]112
[3528]113    private void ClearTreeNode(TreeNode node) {
114      if (scopesTreeView.SelectedNode == node) {
115        scopesTreeView.SelectedNode = null;
116        UpdateVariables();
117      }
118
[2]119      foreach (TreeNode child in node.Nodes)
[3528]120        ClearTreeNode(child);
[2]121
[2687]122      IScope scope = node.Tag as IScope;
[2655]123      if (scope != null) {
[3528]124        scope.NameChanged -= new EventHandler(Scope_NameChanged);
[2]125        scopeNodeTable.Remove(scope);
[2687]126        scope.SubScopes.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsAdded);
127        scope.SubScopes.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsRemoved);
128        scope.SubScopes.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsReplaced);
129        scope.SubScopes.ItemsMoved -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsMoved);
130        scope.SubScopes.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_CollectionReset);
[2655]131        subScopesScopeTable.Remove(scope.SubScopes);
[2]132      }
133    }
[3528]134    #endregion
[2]135
136    #region TreeView Events
[3528]137    private void scopesTreeView_AfterSelect(object sender, TreeViewEventArgs e) {
138      UpdateVariables();
139    }
[2655]140    private void scopesTreeView_MouseDown(object sender, MouseEventArgs e) {
141      TreeNode node = scopesTreeView.GetNodeAt(e.X, e.Y);
[3528]142      scopesTreeView.SelectedNode = node;
143      UpdateVariables();
[2]144    }
145    private void scopesTreeView_BeforeExpand(object sender, TreeViewCancelEventArgs e) {
146      TreeNode node = e.Node;
[2687]147      IScope scope = (IScope)node.Tag;
[2]148
149      node.Nodes.Clear();
150      for (int i = 0; i < scope.SubScopes.Count; i++)
151        node.Nodes.Add(CreateTreeNode(scope.SubScopes[i]));
152    }
153    private void scopesTreeView_AfterCollapse(object sender, System.Windows.Forms.TreeViewEventArgs e) {
154      TreeNode node = e.Node;
[2687]155      IScope scope = (IScope)node.Tag;
[2]156
157      if (node.Nodes.Count > 0) {
158        for (int i = 0; i < node.Nodes.Count; i++)
[3528]159          ClearTreeNode(node.Nodes[i]);
[2]160        node.Nodes.Clear();
161        node.Nodes.Add(new TreeNode());
162      }
163    }
164    private void scopesTreeView_ItemDrag(object sender, ItemDragEventArgs e) {
[3432]165      if (!Locked) {
166        TreeNode node = (TreeNode)e.Item;
167        IScope scope = node.Tag as IScope;
168        if (scope != null) {
169          DataObject data = new DataObject();
[5837]170          data.SetData(HeuristicLab.Common.Constants.DragDropDataFormat, scope);
[3432]171          DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
172        }
[2]173      }
174    }
175    #endregion
176
[3528]177    #region Content Events
[2655]178    private void Scope_NameChanged(object sender, EventArgs e) {
179      if (InvokeRequired)
180        Invoke(new EventHandler(Scope_NameChanged), sender, e);
181      else {
[2687]182        IScope scope = (IScope)sender;
[3561]183        TreeNode node = null;
184        scopeNodeTable.TryGetValue(scope, out node);
185        if (node != null) node.Text = scope.Name;
[2]186      }
187    }
[2687]188    private void SubScopes_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
[2655]189      if (InvokeRequired)
[2687]190        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsAdded), sender, e);
[2655]191      else {
[3561]192        IScope parentScope = null;
193        subScopesScopeTable.TryGetValue((ScopeList)sender, out parentScope);
194        if (parentScope != null) {
195          TreeNode parentNode = null;
196          scopeNodeTable.TryGetValue(parentScope, out parentNode);
197          if (parentNode != null) {
198            scopesTreeView.BeginUpdate();
199            if (parentNode.IsExpanded) {
200              foreach (IndexedItem<IScope> item in e.Items) {
201                TreeNode node = CreateTreeNode(item.Value);
202                parentNode.Nodes.Insert(item.Index, node);
203              }
204            } else if (parentNode.Nodes.Count == 0) {
205              parentNode.Nodes.Add(new TreeNode());
206            }
207            scopesTreeView.EndUpdate();
[2655]208          }
209        }
210      }
[2]211    }
[2687]212    private void SubScopes_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
[2655]213      if (InvokeRequired)
[2687]214        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsRemoved), sender, e);
[2655]215      else {
[3561]216        IScope parentScope = null;
217        subScopesScopeTable.TryGetValue((ScopeList)sender, out parentScope);
218        if (parentScope != null) {
219          TreeNode parentNode = null;
220          scopeNodeTable.TryGetValue(parentScope, out parentNode);
221          if (parentNode != null) {
222            scopesTreeView.BeginUpdate();
223            if (parentNode.IsExpanded) {
224              foreach (IndexedItem<IScope> item in e.Items) {
225                TreeNode node = scopeNodeTable[item.Value];
226                ClearTreeNode(node);
227                node.Remove();
228              }
229            } else if (parentScope.SubScopes.Count == 0) {
230              parentNode.Nodes.Clear();
231            }
232            scopesTreeView.EndUpdate();
[2655]233          }
234        }
[2]235      }
236    }
[2687]237    private void SubScopes_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
[2655]238      if (InvokeRequired)
[2687]239        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsReplaced), sender, e);
[2655]240      else {
[3561]241        IScope parentScope = null;
242        subScopesScopeTable.TryGetValue((ScopeList)sender, out parentScope);
243        if (parentScope != null) {
244          TreeNode parentNode = null;
245          scopeNodeTable.TryGetValue(parentScope, out parentNode);
246          if (parentNode != null) {
247            scopesTreeView.BeginUpdate();
248            if (parentNode.IsExpanded) {
249              foreach (IndexedItem<IScope> item in e.Items) {
250                TreeNode node = parentNode.Nodes[item.Index];
251                ClearTreeNode(node);
252                node.Remove();
253                node = CreateTreeNode(item.Value);
254                parentNode.Nodes.Insert(item.Index, node);
255              }
256              scopesTreeView.EndUpdate();
257            }
[2655]258          }
[2]259        }
260      }
261    }
[2687]262    private void SubScopes_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
[2655]263      if (InvokeRequired)
[2687]264        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsMoved), sender, e);
[2655]265      else {
[3561]266        IScope parentScope = null;
267        subScopesScopeTable.TryGetValue((ScopeList)sender, out parentScope);
268        if (parentScope != null) {
269          TreeNode parentNode = null;
270          scopeNodeTable.TryGetValue(parentScope, out parentNode);
271          if (parentNode != null) {
272            scopesTreeView.BeginUpdate();
273            if (parentNode.IsExpanded) {
274              parentNode.Nodes.Clear();
275              foreach (IndexedItem<IScope> item in e.Items)
276                parentNode.Nodes.Insert(item.Index, scopeNodeTable[item.Value]);
277            }
278            scopesTreeView.EndUpdate();
279          }
[2655]280        }
[2]281      }
282    }
[2687]283    private void SubScopes_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
[2655]284      if (InvokeRequired)
[2687]285        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_CollectionReset), sender, e);
[2655]286      else {
[3561]287        IScope parentScope = null;
288        subScopesScopeTable.TryGetValue((ScopeList)sender, out parentScope);
289        if (parentScope != null) {
290          TreeNode parentNode = null;
291          scopeNodeTable.TryGetValue(parentScope, out parentNode);
292          if (parentNode != null) {
293            scopesTreeView.BeginUpdate();
294            if (parentNode.IsExpanded) {
295              foreach (TreeNode node in parentNode.Nodes)
296                ClearTreeNode(node);
297              parentNode.Nodes.Clear();
298              foreach (IndexedItem<IScope> item in e.Items) {
299                TreeNode node = CreateTreeNode(item.Value);
300                parentNode.Nodes.Insert(item.Index, node);
301              }
302            } else {
303              parentNode.Nodes.Clear();
304              if (parentScope.SubScopes.Count > 0)
305                parentNode.Nodes.Add(new TreeNode());
306            }
307            scopesTreeView.EndUpdate();
[2655]308          }
309        }
[2]310      }
311    }
312    #endregion
[3528]313
314    #region Helpers
315    private void UpdateVariables() {
316      if (scopesTreeView.SelectedNode == null) {
317        variableCollectionView.Content = null;
318        variableCollectionView.Enabled = false;
319      } else {
320        variableCollectionView.Enabled = true;
321        variableCollectionView.Content = ((IScope)scopesTreeView.SelectedNode.Tag).Variables;
322      }
323    }
324    #endregion
[2]325  }
326}
Note: See TracBrowser for help on using the repository browser.