Free cookie consent management tool by TermsFeed Policy Generator

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

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

Prevented drag operations in locked content views (#982)

File size: 11.3 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();
55      Caption = "Scope";
[2687]56      scopeNodeTable = new Dictionary<IScope, TreeNode>();
57      subScopesScopeTable = new Dictionary<ScopeList, IScope>();
[2655]58
[2]59    }
[776]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>
[2727]65    public ScopeView(IScope content)
[2]66      : this() {
[2727]67      Content = content;
[2]68    }
69
[776]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>
[2713]74    protected override void OnContentChanged() {
75      base.OnContentChanged();
[2655]76      if (scopesTreeView.Nodes.Count > 0) {
[2]77        RemoveTreeNode(scopesTreeView.Nodes[0]);
[2655]78        scopesTreeView.Nodes.Clear();
79      }
[2713]80      variableCollectionView.Content = null;
[3362]81      Caption = "Scope";
82      if (Content != null) {
[2713]83        Caption = Content.Name + " (" + Content.GetType().Name + ")";
84        scopesTreeView.Nodes.Add(CreateTreeNode(Content));
[2]85      }
[3362]86      SetEnabledStateOfControls();
[2]87    }
88
[3362]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
[2687]100    private TreeNode CreateTreeNode(IScope scope) {
[2]101      TreeNode node = new TreeNode();
102      node.Text = scope.Name;
103      node.Tag = scope;
104
105      scopeNodeTable.Add(scope, node);
[2655]106      scope.NameChanged += new EventHandler(Scope_NameChanged);
[2687]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);
[2655]112      subScopesScopeTable.Add(scope.SubScopes, scope);
[2]113      if (scope.SubScopes.Count > 0)
114        node.Nodes.Add(new TreeNode());
115      return node;
116    }
[2655]117
[2]118    private void RemoveTreeNode(TreeNode node) {
119      foreach (TreeNode child in node.Nodes)
120        RemoveTreeNode(child);
121
[2687]122      IScope scope = node.Tag as IScope;
[2655]123      if (scope != null) {
[2]124        scopeNodeTable.Remove(scope);
[2655]125        scope.NameChanged -= new EventHandler(Scope_NameChanged);
[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    }
134
135    #region TreeView Events
[2655]136    private void scopesTreeView_MouseDown(object sender, MouseEventArgs e) {
137      TreeNode node = scopesTreeView.GetNodeAt(e.X, e.Y);
[2687]138      if ((node != null) && (node.Tag is IScope)) {
[2713]139        variableCollectionView.Content = ((IScope)node.Tag).Variables;
[2655]140        variableCollectionView.Enabled = true;
141      } else {
[2713]142        variableCollectionView.Content = null;
[2655]143        variableCollectionView.Enabled = false;
144        if (node == null) scopesTreeView.SelectedNode = null;
[2]145      }
146    }
147    private void scopesTreeView_BeforeExpand(object sender, TreeViewCancelEventArgs e) {
148      TreeNode node = e.Node;
[2687]149      IScope scope = (IScope)node.Tag;
[2]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;
[2687]157      IScope scope = (IScope)node.Tag;
[2]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) {
[3432]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        }
[2]176      }
177    }
178    #endregion
179
[2655]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 {
[2687]185        IScope scope = (IScope)sender;
[2655]186        scopeNodeTable[scope].Text = scope.Name;
[2]187      }
188    }
189    #endregion
190
[2655]191    #region SubScopes Events
[2687]192    private void SubScopes_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
[2655]193      if (InvokeRequired)
[2687]194        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsAdded), sender, e);
[2655]195      else {
[2687]196        IScope parentScope = subScopesScopeTable[(ScopeList)sender];
[2655]197        TreeNode parentNode = scopeNodeTable[parentScope];
198        if (parentNode.IsExpanded) {
[2687]199          foreach (IndexedItem<IScope> item in e.Items) {
[2655]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      }
[2]207    }
[2687]208    private void SubScopes_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
[2655]209      if (InvokeRequired)
[2687]210        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsRemoved), sender, e);
[2655]211      else {
[2687]212        IScope parentScope = subScopesScopeTable[(ScopeList)sender];
[2655]213        TreeNode parentNode = scopeNodeTable[parentScope];
214        if (parentNode.IsExpanded) {
[2687]215          foreach (IndexedItem<IScope> item in e.Items) {
[2655]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        }
[2]223      }
224    }
[2687]225    private void SubScopes_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
[2655]226      if (InvokeRequired)
[2687]227        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsReplaced), sender, e);
[2655]228      else {
[2687]229        IScope parentScope = subScopesScopeTable[(ScopeList)sender];
[2655]230        TreeNode parentNode = scopeNodeTable[parentScope];
231        if (parentNode.IsExpanded) {
[2687]232          foreach (IndexedItem<IScope> item in e.Items) {
[2655]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          }
[2]239        }
240      }
241    }
[2687]242    private void SubScopes_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
[2655]243      if (InvokeRequired)
[2687]244        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsMoved), sender, e);
[2655]245      else {
[2687]246        IScope parentScope = subScopesScopeTable[(ScopeList)sender];
[2655]247        TreeNode parentNode = scopeNodeTable[parentScope];
248        if (parentNode.IsExpanded) {
249          parentNode.Nodes.Clear();
[2687]250          foreach (IndexedItem<IScope> item in e.Items)
[2655]251            parentNode.Nodes.Insert(item.Index, scopeNodeTable[item.Value]);
252        }
[2]253      }
254    }
[2687]255    private void SubScopes_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
[2655]256      if (InvokeRequired)
[2687]257        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_CollectionReset), sender, e);
[2655]258      else {
[2687]259        IScope parentScope = subScopesScopeTable[(ScopeList)sender];
[2655]260        TreeNode parentNode = scopeNodeTable[parentScope];
261        if (parentNode.IsExpanded) {
262          foreach (TreeNode node in parentNode.Nodes)
263            RemoveTreeNode(node);
264          parentNode.Nodes.Clear();
[2687]265          foreach (IndexedItem<IScope> item in e.Items) {
[2655]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        }
[2]274      }
275    }
276    #endregion
277  }
278}
Note: See TracBrowser for help on using the repository browser.