Free cookie consent management tool by TermsFeed Policy Generator

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

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

Operator architecture refactoring (#95)

  • implemented reviewers' comments
File size: 11.0 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      variableCollectionView.Enabled = false;
82      if (Content == null) {
83        Caption = "Scope";
84        scopesTreeView.Enabled = false;
85      } else {
86        Caption = Content.Name + " (" + Content.GetType().Name + ")";
87        scopesTreeView.Enabled = true;
88        scopesTreeView.Nodes.Add(CreateTreeNode(Content));
89      }
90    }
91
92    private TreeNode CreateTreeNode(IScope scope) {
93      TreeNode node = new TreeNode();
94      node.Text = scope.Name;
95      node.Tag = scope;
96
97      scopeNodeTable.Add(scope, node);
98      scope.NameChanged += new EventHandler(Scope_NameChanged);
99      scope.SubScopes.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsAdded);
100      scope.SubScopes.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsRemoved);
101      scope.SubScopes.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsReplaced);
102      scope.SubScopes.ItemsMoved += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsMoved);
103      scope.SubScopes.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_CollectionReset);
104      subScopesScopeTable.Add(scope.SubScopes, scope);
105      if (scope.SubScopes.Count > 0)
106        node.Nodes.Add(new TreeNode());
107      return node;
108    }
109
110    private void RemoveTreeNode(TreeNode node) {
111      foreach (TreeNode child in node.Nodes)
112        RemoveTreeNode(child);
113
114      IScope scope = node.Tag as IScope;
115      if (scope != null) {
116        scopeNodeTable.Remove(scope);
117        scope.NameChanged -= new EventHandler(Scope_NameChanged);
118        scope.SubScopes.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsAdded);
119        scope.SubScopes.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsRemoved);
120        scope.SubScopes.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsReplaced);
121        scope.SubScopes.ItemsMoved -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsMoved);
122        scope.SubScopes.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_CollectionReset);
123        subScopesScopeTable.Remove(scope.SubScopes);
124      }
125    }
126
127    #region TreeView Events
128    private void scopesTreeView_MouseDown(object sender, MouseEventArgs e) {
129      TreeNode node = scopesTreeView.GetNodeAt(e.X, e.Y);
130      if ((node != null) && (node.Tag is IScope)) {
131        variableCollectionView.Content = ((IScope)node.Tag).Variables;
132        variableCollectionView.Enabled = true;
133      } else {
134        variableCollectionView.Content = null;
135        variableCollectionView.Enabled = false;
136        if (node == null) scopesTreeView.SelectedNode = null;
137      }
138    }
139    private void scopesTreeView_BeforeExpand(object sender, TreeViewCancelEventArgs e) {
140      TreeNode node = e.Node;
141      IScope scope = (IScope)node.Tag;
142
143      node.Nodes.Clear();
144      for (int i = 0; i < scope.SubScopes.Count; i++)
145        node.Nodes.Add(CreateTreeNode(scope.SubScopes[i]));
146    }
147    private void scopesTreeView_AfterCollapse(object sender, System.Windows.Forms.TreeViewEventArgs e) {
148      TreeNode node = e.Node;
149      IScope scope = (IScope)node.Tag;
150
151      if (node.Nodes.Count > 0) {
152        for (int i = 0; i < node.Nodes.Count; i++)
153          RemoveTreeNode(node.Nodes[i]);
154        node.Nodes.Clear();
155        node.Nodes.Add(new TreeNode());
156      }
157    }
158    private void scopesTreeView_ItemDrag(object sender, ItemDragEventArgs e) {
159      TreeNode node = (TreeNode)e.Item;
160      IScope scope = node.Tag as IScope;
161      if (scope != null) {
162        DataObject data = new DataObject();
163        data.SetData("Scope", scope);
164        data.SetData("DragSource", scopesTreeView);
165        DoDragDrop(data, DragDropEffects.Copy);
166      }
167    }
168    #endregion
169
170    #region Scope Events
171    private void Scope_NameChanged(object sender, EventArgs e) {
172      if (InvokeRequired)
173        Invoke(new EventHandler(Scope_NameChanged), sender, e);
174      else {
175        IScope scope = (IScope)sender;
176        scopeNodeTable[scope].Text = scope.Name;
177      }
178    }
179    #endregion
180
181    #region SubScopes Events
182    private void SubScopes_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
183      if (InvokeRequired)
184        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsAdded), sender, e);
185      else {
186        IScope parentScope = subScopesScopeTable[(ScopeList)sender];
187        TreeNode parentNode = scopeNodeTable[parentScope];
188        if (parentNode.IsExpanded) {
189          foreach (IndexedItem<IScope> item in e.Items) {
190            TreeNode node = CreateTreeNode(item.Value);
191            parentNode.Nodes.Insert(item.Index, node);
192          }
193        } else if (parentNode.Nodes.Count == 0) {
194          parentNode.Nodes.Add(new TreeNode());
195        }
196      }
197    }
198    private void SubScopes_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
199      if (InvokeRequired)
200        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsRemoved), sender, e);
201      else {
202        IScope parentScope = subScopesScopeTable[(ScopeList)sender];
203        TreeNode parentNode = scopeNodeTable[parentScope];
204        if (parentNode.IsExpanded) {
205          foreach (IndexedItem<IScope> item in e.Items) {
206            TreeNode node = scopeNodeTable[item.Value];
207            RemoveTreeNode(node);
208            node.Remove();
209          }
210        } else if (parentScope.SubScopes.Count == 0) {
211          parentNode.Nodes.Clear();
212        }
213      }
214    }
215    private void SubScopes_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
216      if (InvokeRequired)
217        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsReplaced), sender, e);
218      else {
219        IScope parentScope = subScopesScopeTable[(ScopeList)sender];
220        TreeNode parentNode = scopeNodeTable[parentScope];
221        if (parentNode.IsExpanded) {
222          foreach (IndexedItem<IScope> item in e.Items) {
223            TreeNode node = parentNode.Nodes[item.Index];
224            RemoveTreeNode(node);
225            node.Remove();
226            node = CreateTreeNode(item.Value);
227            parentNode.Nodes.Insert(item.Index, node);
228          }
229        }
230      }
231    }
232    private void SubScopes_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
233      if (InvokeRequired)
234        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsMoved), sender, e);
235      else {
236        IScope parentScope = subScopesScopeTable[(ScopeList)sender];
237        TreeNode parentNode = scopeNodeTable[parentScope];
238        if (parentNode.IsExpanded) {
239          parentNode.Nodes.Clear();
240          foreach (IndexedItem<IScope> item in e.Items)
241            parentNode.Nodes.Insert(item.Index, scopeNodeTable[item.Value]);
242        }
243      }
244    }
245    private void SubScopes_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
246      if (InvokeRequired)
247        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_CollectionReset), sender, e);
248      else {
249        IScope parentScope = subScopesScopeTable[(ScopeList)sender];
250        TreeNode parentNode = scopeNodeTable[parentScope];
251        if (parentNode.IsExpanded) {
252          foreach (TreeNode node in parentNode.Nodes)
253            RemoveTreeNode(node);
254          parentNode.Nodes.Clear();
255          foreach (IndexedItem<IScope> item in e.Items) {
256            TreeNode node = CreateTreeNode(item.Value);
257            parentNode.Nodes.Insert(item.Index, node);
258          }
259        } else {
260          parentNode.Nodes.Clear();
261          if (parentScope.SubScopes.Count > 0)
262            parentNode.Nodes.Add(new TreeNode());
263        }
264      }
265    }
266    #endregion
267  }
268}
Note: See TracBrowser for help on using the repository browser.