Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3432 was 3432, checked in by swagner, 15 years ago

Prevented drag operations in locked content views (#982)

File size: 11.3 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      Caption = "Scope";
56      scopeNodeTable = new Dictionary<IScope, TreeNode>();
57      subScopesScopeTable = new Dictionary<ScopeList, IScope>();
58
59    }
60    /// <summary>
61    /// Initializes a new instance of <see cref="ScopeView"/> with the given <paramref name="scope"/>.
62    /// </summary>
63    /// <remarks>Calls <see cref="ScopeView()"/>.</remarks>
64    /// <param name="scope">The scope to represent visually.</param>
65    public ScopeView(IScope content)
66      : this() {
67      Content = content;
68    }
69
70    /// <summary>
71    /// Updates all controls with the latest data of the model.
72    /// </summary>
73    /// <remarks>Calls <see cref="ViewBase.UpdateControls"/> of base class <see cref="ViewBase"/>.</remarks>
74    protected override void OnContentChanged() {
75      base.OnContentChanged();
76      if (scopesTreeView.Nodes.Count > 0) {
77        RemoveTreeNode(scopesTreeView.Nodes[0]);
78        scopesTreeView.Nodes.Clear();
79      }
80      variableCollectionView.Content = null;
81      Caption = "Scope";
82      if (Content != null) {
83        Caption = Content.Name + " (" + Content.GetType().Name + ")";
84        scopesTreeView.Nodes.Add(CreateTreeNode(Content));
85      }
86      SetEnabledStateOfControls();
87    }
88
89    protected override void OnReadOnlyChanged() {
90      base.OnReadOnlyChanged();
91      SetEnabledStateOfControls();
92    }
93
94    private void SetEnabledStateOfControls() {
95      scopesTreeView.Enabled = Content != null;
96      variableCollectionView.Enabled = Content != null;
97      variableCollectionView.ReadOnly = ReadOnly;
98    }
99
100    private TreeNode CreateTreeNode(IScope scope) {
101      TreeNode node = new TreeNode();
102      node.Text = scope.Name;
103      node.Tag = scope;
104
105      scopeNodeTable.Add(scope, node);
106      scope.NameChanged += new EventHandler(Scope_NameChanged);
107      scope.SubScopes.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsAdded);
108      scope.SubScopes.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsRemoved);
109      scope.SubScopes.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsReplaced);
110      scope.SubScopes.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsMoved);
111      scope.SubScopes.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_CollectionReset);
112      subScopesScopeTable.Add(scope.SubScopes, scope);
113      if (scope.SubScopes.Count > 0)
114        node.Nodes.Add(new TreeNode());
115      return node;
116    }
117
118    private void RemoveTreeNode(TreeNode node) {
119      foreach (TreeNode child in node.Nodes)
120        RemoveTreeNode(child);
121
122      IScope scope = node.Tag as IScope;
123      if (scope != null) {
124        scopeNodeTable.Remove(scope);
125        scope.NameChanged -= new EventHandler(Scope_NameChanged);
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);
131        subScopesScopeTable.Remove(scope.SubScopes);
132      }
133    }
134
135    #region TreeView Events
136    private void scopesTreeView_MouseDown(object sender, MouseEventArgs e) {
137      TreeNode node = scopesTreeView.GetNodeAt(e.X, e.Y);
138      if ((node != null) && (node.Tag is IScope)) {
139        variableCollectionView.Content = ((IScope)node.Tag).Variables;
140        variableCollectionView.Enabled = true;
141      } else {
142        variableCollectionView.Content = null;
143        variableCollectionView.Enabled = false;
144        if (node == null) scopesTreeView.SelectedNode = null;
145      }
146    }
147    private void scopesTreeView_BeforeExpand(object sender, TreeViewCancelEventArgs e) {
148      TreeNode node = e.Node;
149      IScope scope = (IScope)node.Tag;
150
151      node.Nodes.Clear();
152      for (int i = 0; i < scope.SubScopes.Count; i++)
153        node.Nodes.Add(CreateTreeNode(scope.SubScopes[i]));
154    }
155    private void scopesTreeView_AfterCollapse(object sender, System.Windows.Forms.TreeViewEventArgs e) {
156      TreeNode node = e.Node;
157      IScope scope = (IScope)node.Tag;
158
159      if (node.Nodes.Count > 0) {
160        for (int i = 0; i < node.Nodes.Count; i++)
161          RemoveTreeNode(node.Nodes[i]);
162        node.Nodes.Clear();
163        node.Nodes.Add(new TreeNode());
164      }
165    }
166    private void scopesTreeView_ItemDrag(object sender, ItemDragEventArgs e) {
167      if (!Locked) {
168        TreeNode node = (TreeNode)e.Item;
169        IScope scope = node.Tag as IScope;
170        if (scope != null) {
171          DataObject data = new DataObject();
172          data.SetData("Type", scope.GetType());
173          data.SetData("Value", scope);
174          DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
175        }
176      }
177    }
178    #endregion
179
180    #region Scope Events
181    private void Scope_NameChanged(object sender, EventArgs e) {
182      if (InvokeRequired)
183        Invoke(new EventHandler(Scope_NameChanged), sender, e);
184      else {
185        IScope scope = (IScope)sender;
186        scopeNodeTable[scope].Text = scope.Name;
187      }
188    }
189    #endregion
190
191    #region SubScopes Events
192    private void SubScopes_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
193      if (InvokeRequired)
194        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsAdded), sender, e);
195      else {
196        IScope parentScope = subScopesScopeTable[(ScopeList)sender];
197        TreeNode parentNode = scopeNodeTable[parentScope];
198        if (parentNode.IsExpanded) {
199          foreach (IndexedItem<IScope> item in e.Items) {
200            TreeNode node = CreateTreeNode(item.Value);
201            parentNode.Nodes.Insert(item.Index, node);
202          }
203        } else if (parentNode.Nodes.Count == 0) {
204          parentNode.Nodes.Add(new TreeNode());
205        }
206      }
207    }
208    private void SubScopes_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
209      if (InvokeRequired)
210        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsRemoved), sender, e);
211      else {
212        IScope parentScope = subScopesScopeTable[(ScopeList)sender];
213        TreeNode parentNode = scopeNodeTable[parentScope];
214        if (parentNode.IsExpanded) {
215          foreach (IndexedItem<IScope> item in e.Items) {
216            TreeNode node = scopeNodeTable[item.Value];
217            RemoveTreeNode(node);
218            node.Remove();
219          }
220        } else if (parentScope.SubScopes.Count == 0) {
221          parentNode.Nodes.Clear();
222        }
223      }
224    }
225    private void SubScopes_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
226      if (InvokeRequired)
227        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsReplaced), sender, e);
228      else {
229        IScope parentScope = subScopesScopeTable[(ScopeList)sender];
230        TreeNode parentNode = scopeNodeTable[parentScope];
231        if (parentNode.IsExpanded) {
232          foreach (IndexedItem<IScope> item in e.Items) {
233            TreeNode node = parentNode.Nodes[item.Index];
234            RemoveTreeNode(node);
235            node.Remove();
236            node = CreateTreeNode(item.Value);
237            parentNode.Nodes.Insert(item.Index, node);
238          }
239        }
240      }
241    }
242    private void SubScopes_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
243      if (InvokeRequired)
244        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsMoved), sender, e);
245      else {
246        IScope parentScope = subScopesScopeTable[(ScopeList)sender];
247        TreeNode parentNode = scopeNodeTable[parentScope];
248        if (parentNode.IsExpanded) {
249          parentNode.Nodes.Clear();
250          foreach (IndexedItem<IScope> item in e.Items)
251            parentNode.Nodes.Insert(item.Index, scopeNodeTable[item.Value]);
252        }
253      }
254    }
255    private void SubScopes_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
256      if (InvokeRequired)
257        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_CollectionReset), sender, e);
258      else {
259        IScope parentScope = subScopesScopeTable[(ScopeList)sender];
260        TreeNode parentNode = scopeNodeTable[parentScope];
261        if (parentNode.IsExpanded) {
262          foreach (TreeNode node in parentNode.Nodes)
263            RemoveTreeNode(node);
264          parentNode.Nodes.Clear();
265          foreach (IndexedItem<IScope> item in e.Items) {
266            TreeNode node = CreateTreeNode(item.Value);
267            parentNode.Nodes.Insert(item.Index, node);
268          }
269        } else {
270          parentNode.Nodes.Clear();
271          if (parentScope.SubScopes.Count > 0)
272            parentNode.Nodes.Add(new TreeNode());
273        }
274      }
275    }
276    #endregion
277  }
278}
Note: See TracBrowser for help on using the repository browser.