Free cookie consent management tool by TermsFeed Policy Generator

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

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

Operator architecture refactoring (#95)

  • implemented ideas which came up during today's presentation of HeuristicLab.Core and related plugins
File size: 11.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.ComponentModel;
25using System.Drawing;
26using System.Data;
27using System.Text;
28using System.Windows.Forms;
29using HeuristicLab.PluginInfrastructure;
30using HeuristicLab.Common;
31using HeuristicLab.MainForm;
32using HeuristicLab.Collections;
33
34namespace HeuristicLab.Core.Views {
35  /// <summary>
36  /// The visual represenation of <see cref="Scope"/>.
37  /// </summary>
38  [Content(typeof(Scope), true)]
39  public sealed partial class ScopeView : ItemView {
40    private Dictionary<IScope, TreeNode> scopeNodeTable;
41    private Dictionary<ScopeList, IScope> subScopesScopeTable;
42
43    /// <summary>
44    /// Gets or sets the scope to represent visually.
45    /// </summary>
46    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
47    /// No own data storage present.</remarks>
48    public IScope Scope {
49      get { return (IScope)Item; }
50      set { base.Item = value; }
51    }
52
53    /// <summary>
54    /// Initializes a new instance of <see cref="ScopeView"/> with caption "Scope" and
55    /// property <see cref="AutomaticUpdating"/> set to <c>false</c>.
56    /// </summary>
57    public ScopeView() {
58      InitializeComponent();
59      Caption = "Scope";
60      scopeNodeTable = new Dictionary<IScope, TreeNode>();
61      subScopesScopeTable = new Dictionary<ScopeList, IScope>();
62
63    }
64    /// <summary>
65    /// Initializes a new instance of <see cref="ScopeView"/> with the given <paramref name="scope"/>.
66    /// </summary>
67    /// <remarks>Calls <see cref="ScopeView()"/>.</remarks>
68    /// <param name="scope">The scope to represent visually.</param>
69    public ScopeView(IScope scope)
70      : this() {
71      Scope = scope;
72    }
73
74    /// <summary>
75    /// Updates all controls with the latest data of the model.
76    /// </summary>
77    /// <remarks>Calls <see cref="ViewBase.UpdateControls"/> of base class <see cref="ViewBase"/>.</remarks>
78    protected override void OnObjectChanged() {
79      base.OnObjectChanged();
80      if (scopesTreeView.Nodes.Count > 0) {
81        RemoveTreeNode(scopesTreeView.Nodes[0]);
82        scopesTreeView.Nodes.Clear();
83      }
84      variableCollectionView.NamedItemCollection = null;
85      variableCollectionView.Enabled = false;
86      if (Scope == null) {
87        Caption = "Scope";
88        scopesTreeView.Enabled = false;
89      } else {
90        Caption = Scope.Name + " (" + Scope.GetType().Name + ")";
91        scopesTreeView.Enabled = true;
92        scopesTreeView.Nodes.Add(CreateTreeNode(Scope));
93      }
94    }
95
96    private TreeNode CreateTreeNode(IScope scope) {
97      TreeNode node = new TreeNode();
98      node.Text = scope.Name;
99      node.Tag = scope;
100
101      scopeNodeTable.Add(scope, node);
102      scope.NameChanged += new EventHandler(Scope_NameChanged);
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);
108      subScopesScopeTable.Add(scope.SubScopes, scope);
109      if (scope.SubScopes.Count > 0)
110        node.Nodes.Add(new TreeNode());
111      return node;
112    }
113
114    private void RemoveTreeNode(TreeNode node) {
115      foreach (TreeNode child in node.Nodes)
116        RemoveTreeNode(child);
117
118      IScope scope = node.Tag as IScope;
119      if (scope != null) {
120        scopeNodeTable.Remove(scope);
121        scope.NameChanged -= new EventHandler(Scope_NameChanged);
122        scope.SubScopes.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsAdded);
123        scope.SubScopes.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsRemoved);
124        scope.SubScopes.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsReplaced);
125        scope.SubScopes.ItemsMoved -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsMoved);
126        scope.SubScopes.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_CollectionReset);
127        subScopesScopeTable.Remove(scope.SubScopes);
128      }
129    }
130
131    #region TreeView Events
132    private void scopesTreeView_MouseDown(object sender, MouseEventArgs e) {
133      TreeNode node = scopesTreeView.GetNodeAt(e.X, e.Y);
134      if ((node != null) && (node.Tag is IScope)) {
135        variableCollectionView.NamedItemCollection = ((IScope)node.Tag).Variables;
136        variableCollectionView.Enabled = true;
137      } else {
138        variableCollectionView.NamedItemCollection = null;
139        variableCollectionView.Enabled = false;
140        if (node == null) scopesTreeView.SelectedNode = null;
141      }
142    }
143    private void scopesTreeView_BeforeExpand(object sender, TreeViewCancelEventArgs e) {
144      TreeNode node = e.Node;
145      IScope scope = (IScope)node.Tag;
146
147      node.Nodes.Clear();
148      for (int i = 0; i < scope.SubScopes.Count; i++)
149        node.Nodes.Add(CreateTreeNode(scope.SubScopes[i]));
150    }
151    private void scopesTreeView_AfterCollapse(object sender, System.Windows.Forms.TreeViewEventArgs e) {
152      TreeNode node = e.Node;
153      IScope scope = (IScope)node.Tag;
154
155      if (node.Nodes.Count > 0) {
156        for (int i = 0; i < node.Nodes.Count; i++)
157          RemoveTreeNode(node.Nodes[i]);
158        node.Nodes.Clear();
159        node.Nodes.Add(new TreeNode());
160      }
161    }
162    private void scopesTreeView_ItemDrag(object sender, ItemDragEventArgs e) {
163      TreeNode node = (TreeNode)e.Item;
164      IScope scope = node.Tag as IScope;
165      if (scope != null) {
166        DataObject data = new DataObject();
167        data.SetData("Scope", scope);
168        data.SetData("DragSource", scopesTreeView);
169        DoDragDrop(data, DragDropEffects.Copy);
170      }
171    }
172    #endregion
173
174    #region Scope Events
175    private void Scope_NameChanged(object sender, EventArgs e) {
176      if (InvokeRequired)
177        Invoke(new EventHandler(Scope_NameChanged), sender, e);
178      else {
179        IScope scope = (IScope)sender;
180        scopeNodeTable[scope].Text = scope.Name;
181      }
182    }
183    #endregion
184
185    #region SubScopes Events
186    private void SubScopes_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
187      if (InvokeRequired)
188        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsAdded), sender, e);
189      else {
190        IScope parentScope = subScopesScopeTable[(ScopeList)sender];
191        TreeNode parentNode = scopeNodeTable[parentScope];
192        if (parentNode.IsExpanded) {
193          foreach (IndexedItem<IScope> item in e.Items) {
194            TreeNode node = CreateTreeNode(item.Value);
195            parentNode.Nodes.Insert(item.Index, node);
196          }
197        } else if (parentNode.Nodes.Count == 0) {
198          parentNode.Nodes.Add(new TreeNode());
199        }
200      }
201    }
202    private void SubScopes_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
203      if (InvokeRequired)
204        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsRemoved), sender, e);
205      else {
206        IScope parentScope = subScopesScopeTable[(ScopeList)sender];
207        TreeNode parentNode = scopeNodeTable[parentScope];
208        if (parentNode.IsExpanded) {
209          foreach (IndexedItem<IScope> item in e.Items) {
210            TreeNode node = scopeNodeTable[item.Value];
211            RemoveTreeNode(node);
212            node.Remove();
213          }
214        } else if (parentScope.SubScopes.Count == 0) {
215          parentNode.Nodes.Clear();
216        }
217      }
218    }
219    private void SubScopes_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
220      if (InvokeRequired)
221        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsReplaced), sender, e);
222      else {
223        IScope parentScope = subScopesScopeTable[(ScopeList)sender];
224        TreeNode parentNode = scopeNodeTable[parentScope];
225        if (parentNode.IsExpanded) {
226          foreach (IndexedItem<IScope> item in e.Items) {
227            TreeNode node = parentNode.Nodes[item.Index];
228            RemoveTreeNode(node);
229            node.Remove();
230            node = CreateTreeNode(item.Value);
231            parentNode.Nodes.Insert(item.Index, node);
232          }
233        }
234      }
235    }
236    private void SubScopes_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
237      if (InvokeRequired)
238        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsMoved), sender, e);
239      else {
240        IScope parentScope = subScopesScopeTable[(ScopeList)sender];
241        TreeNode parentNode = scopeNodeTable[parentScope];
242        if (parentNode.IsExpanded) {
243          parentNode.Nodes.Clear();
244          foreach (IndexedItem<IScope> item in e.Items)
245            parentNode.Nodes.Insert(item.Index, scopeNodeTable[item.Value]);
246        }
247      }
248    }
249    private void SubScopes_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
250      if (InvokeRequired)
251        Invoke(new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_CollectionReset), sender, e);
252      else {
253        IScope parentScope = subScopesScopeTable[(ScopeList)sender];
254        TreeNode parentNode = scopeNodeTable[parentScope];
255        if (parentNode.IsExpanded) {
256          foreach (TreeNode node in parentNode.Nodes)
257            RemoveTreeNode(node);
258          parentNode.Nodes.Clear();
259          foreach (IndexedItem<IScope> item in e.Items) {
260            TreeNode node = CreateTreeNode(item.Value);
261            parentNode.Nodes.Insert(item.Index, node);
262          }
263        } else {
264          parentNode.Nodes.Clear();
265          if (parentScope.SubScopes.Count > 0)
266            parentNode.Nodes.Add(new TreeNode());
267        }
268      }
269    }
270    #endregion
271  }
272}
Note: See TracBrowser for help on using the repository browser.