Free cookie consent management tool by TermsFeed Policy Generator

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

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

Operator architecture refactoring (#95)

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