Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/3.2/ScopeView.cs @ 2895

Last change on this file since 2895 was 2591, checked in by gkronber, 15 years ago

Copied refactored plugin infrastructure from branch and merged changeset r2586:2589 from branch into the trunk. #799

File size: 10.7 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;
31
32namespace HeuristicLab.Core {
33  /// <summary>
34  /// The visual represenation of <see cref="IScope"/>.
35  /// </summary>
36  public partial class ScopeView : ViewBase {
37    private Dictionary<IScope, TreeNode> scopeNodeTable;
38    private Dictionary<IScope, bool> scopeExpandedTable;
39
40    /// <summary>
41    /// Gets or sets the scope to represent visually.
42    /// </summary>
43    /// <remarks>Uses property <see cref="ViewBase.Item"/> of base class <see cref="ViewBase"/>.
44    /// No own data storage present.</remarks>
45    public IScope Scope {
46      get { return (IScope)Item; }
47      set { base.Item = value; }
48    }
49    private bool myAutomaticUpdating;
50    /// <summary>
51    /// Gets information whether the scope is automatically updating.
52    /// </summary>
53    public bool AutomaticUpdating {
54      get { return myAutomaticUpdating; }
55    }
56
57    /// <summary>
58    /// Initializes a new instance of <see cref="ScopeView"/> with caption "Scope" and
59    /// property <see cref="AutomaticUpdating"/> set to <c>false</c>.
60    /// </summary>
61    public ScopeView() {
62      InitializeComponent();
63      Caption = "Scope";
64      scopeNodeTable = new Dictionary<IScope, TreeNode>();
65      scopeExpandedTable = new Dictionary<IScope, bool>();
66      myAutomaticUpdating = false;
67    }
68    /// <summary>
69    /// Initializes a new instance of <see cref="ScopeView"/> with the given <paramref name="scope"/>.
70    /// </summary>
71    /// <remarks>Calls <see cref="ScopeView()"/>.</remarks>
72    /// <param name="scope">The scope to represent visually.</param>
73    public ScopeView(IScope scope)
74      : this() {
75      Scope = scope;
76    }
77
78    /// <summary>
79    /// Updates all controls with the latest data of the model.
80    /// </summary>
81    /// <remarks>Calls <see cref="ViewBase.UpdateControls"/> of base class <see cref="ViewBase"/>.</remarks>
82    protected override void UpdateControls() {
83      base.UpdateControls();
84      if (scopesTreeView.Nodes.Count > 0)
85        RemoveTreeNode(scopesTreeView.Nodes[0]);
86      scopesTreeView.SelectedNode = null;
87      scopesTreeView.Nodes.Clear();
88      if (Scope == null) {
89        Caption = "Scope";
90        scopesTreeView.Enabled = false;
91      } else {
92        Caption = Scope.Name + " (" + Scope.GetType().Name + ")";
93        scopesTreeView.Nodes.Add(CreateTreeNode(Scope));
94        scopesTreeView.Enabled = true;
95      }
96    }
97
98    private TreeNode CreateTreeNode(IScope scope) {
99      TreeNode node = new TreeNode();
100      node.Text = scope.Name;
101      node.Tag = scope;
102
103      scopeNodeTable.Add(scope, node);
104      scopeExpandedTable.Add(scope, false);
105      if (myAutomaticUpdating) {
106        scope.SubScopeAdded += new EventHandler<EventArgs<IScope, int>>(Scope_SubScopeAdded);
107        scope.SubScopeRemoved += new EventHandler<EventArgs<IScope, int>>(Scope_SubScopeRemoved);
108        scope.SubScopesReordered += new EventHandler(Scope_SubScopesReordered);
109      }
110      if (scope.SubScopes.Count > 0)
111        node.Nodes.Add(new TreeNode());
112      return node;
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) && (scopeNodeTable.ContainsKey(scope))) {
120        scopeNodeTable.Remove(scope);
121        scopeExpandedTable.Remove(scope);
122        scope.SubScopeAdded -= new EventHandler<EventArgs<IScope, int>>(Scope_SubScopeAdded);
123        scope.SubScopeRemoved -= new EventHandler<EventArgs<IScope, int>>(Scope_SubScopeRemoved);
124        scope.SubScopesReordered -= new EventHandler(Scope_SubScopesReordered);
125      }
126    }
127
128    #region TreeView Events
129    private void scopesTreeView_DoubleClick(object sender, EventArgs e) {
130      // make sure that we can't get NullPointerExceptions
131      if(scopesTreeView.SelectedNode != null && scopesTreeView.SelectedNode.Tag != null) {
132        IScope scope = (IScope)scopesTreeView.SelectedNode.Tag;
133        ControlManager.Manager.ShowControl(new VariablesScopeView(scope));
134      }
135    }
136    private void scopesTreeView_BeforeExpand(object sender, TreeViewCancelEventArgs e) {
137      TreeNode node = e.Node;
138      IScope scope = (IScope)node.Tag;
139
140      node.Nodes.Clear();
141      for (int i = 0; i < scope.SubScopes.Count; i++)
142        node.Nodes.Add(CreateTreeNode(scope.SubScopes[i]));
143      scopeExpandedTable[scope] = true;
144    }
145    private void scopesTreeView_AfterCollapse(object sender, System.Windows.Forms.TreeViewEventArgs e) {
146      TreeNode node = e.Node;
147      IScope scope = (IScope)node.Tag;
148
149      if (node.Nodes.Count > 0) {
150        for (int i = 0; i < node.Nodes.Count; i++)
151          RemoveTreeNode(node.Nodes[i]);
152        node.Nodes.Clear();
153        node.Nodes.Add(new TreeNode());
154      }
155      scopeExpandedTable[scope] = false;
156    }
157    private void scopesTreeView_ItemDrag(object sender, ItemDragEventArgs e) {
158      TreeNode node = (TreeNode)e.Item;
159      IScope scope = node.Tag as IScope;
160      if (scope != null) {
161        DataObject data = new DataObject();
162        data.SetData("IScope", scope);
163        data.SetData("DragSource", scopesTreeView);
164        DoDragDrop(data, DragDropEffects.Copy);
165      }
166    }
167    #endregion
168
169    #region Context Menu Events
170    private void contextMenuStrip_Opening(object sender, CancelEventArgs e) {
171      variablesToolStripMenuItem.Enabled = false;
172      viewToolStripMenuItem.DropDownItems.Clear();
173      viewToolStripMenuItem.Enabled = false;
174      if (scopesTreeView.SelectedNode != null) {
175        variablesToolStripMenuItem.Enabled = true;
176        IScope scope = (IScope)scopesTreeView.SelectedNode.Tag;
177        foreach (IVariable variable in scope.Variables) {
178          if (variable.Value is IVisualizationItem) {
179            ToolStripMenuItem item = new ToolStripMenuItem();
180            item.Text = variable.Name + "...";
181            item.Tag = variable.Value;
182            item.Click += new EventHandler(showViewToolStripMenuItem_Click);
183            viewToolStripMenuItem.DropDownItems.Add(item);
184          }
185        }
186        if (viewToolStripMenuItem.DropDownItems.Count > 0)
187          viewToolStripMenuItem.Enabled = true;
188      }
189    }
190    private void automaticUpdatingToolStripMenuItem_Click(object sender, EventArgs e) {
191      ToolStripMenuItem item = (ToolStripMenuItem)sender;
192      myAutomaticUpdating = item.Checked;
193      if (myAutomaticUpdating)
194        Refresh();
195    }
196    private void refreshToolStripMenuItem_Click(object sender, EventArgs e) {
197      Refresh();
198    }
199    private void variablesToolStripMenuItem_Click(object sender, EventArgs e) {
200      IScope scope = (IScope)scopesTreeView.SelectedNode.Tag;
201      ControlManager.Manager.ShowControl(new VariablesScopeView(scope));
202    }
203    private void showViewToolStripMenuItem_Click(object sender, EventArgs e) {
204      IItem item = (IItem)((ToolStripMenuItem)sender).Tag;
205      ControlManager.Manager.ShowControl(item.CreateView());
206    }
207    #endregion
208
209    #region Scope Events
210    private delegate void ScopeDelegate(IScope scope);
211    private delegate void ScopeScopeIndexDelegate(IScope scope, IScope subScope, int index);
212    private void Scope_SubScopeAdded(object sender, EventArgs<IScope, int> e) {
213      IScope scope = (IScope)sender;
214      TreeNode node = scopeNodeTable[scope];
215      if (scopeExpandedTable[scope] || (scope.SubScopes.Count == 1))
216        AddSubScope(scope, e.Value, e.Value2);
217    }
218    private void AddSubScope(IScope scope, IScope subScope, int index) {
219      if (InvokeRequired) {
220        Invoke(new ScopeScopeIndexDelegate(AddSubScope), scope, subScope, index);
221      } else {
222        TreeNode parent = scopeNodeTable[scope];
223        TreeNode child;
224        if (parent.IsExpanded)
225          child = CreateTreeNode(subScope);
226        else
227          child = new TreeNode();
228        parent.Nodes.Insert(index, child);
229      }
230    }
231    private void Scope_SubScopeRemoved(object sender, EventArgs<IScope, int> e) {
232      IScope scope = (IScope)sender;
233      TreeNode node = scopeNodeTable[scope];
234      if (scopeExpandedTable[scope] || (scope.SubScopes.Count == 0))
235        RemoveSubScope(scope, e.Value, e.Value2);
236    }
237    private void RemoveSubScope(IScope scope, IScope subScope, int index) {
238      if (InvokeRequired) {
239        Invoke(new ScopeScopeIndexDelegate(RemoveSubScope), scope, subScope, index);
240      } else {
241        if (scopeNodeTable.ContainsKey(subScope)) {
242          TreeNode node = scopeNodeTable[subScope];
243          RemoveTreeNode(scopeNodeTable[subScope]);
244          node.Remove();
245        } else {
246          TreeNode node = scopeNodeTable[scope];
247          node.Nodes[0].Remove();
248        }
249      }
250    }
251    private void Scope_SubScopesReordered(object sender, EventArgs e) {
252      IScope scope = (IScope)sender;
253      TreeNode node = scopeNodeTable[scope];
254      if (scopeExpandedTable[scope])
255        ReorderSubScopes(scope);
256    }
257    private void ReorderSubScopes(IScope scope) {
258      if (InvokeRequired) {
259        Invoke(new ScopeDelegate(ReorderSubScopes), scope);
260      } else {
261        TreeNode node = scopeNodeTable[scope];
262        node.Nodes.Clear();
263        for (int i = 0; i < scope.SubScopes.Count; i++)
264          node.Nodes.Add(scopeNodeTable[scope.SubScopes[i]]);
265      }
266    }
267    #endregion
268
269    #region Mouse Events
270    private void scopesTreeView_MouseDown(object sender, MouseEventArgs e) {
271      if (e.Button != MouseButtons.Right)
272        return;
273      TreeNode clickedNode = scopesTreeView.GetNodeAt(e.X, e.Y);
274      if (clickedNode != null) {
275        scopesTreeView.SelectedNode = clickedNode;
276        scopesTreeView.Refresh();
277      }
278    }
279    #endregion
280  }
281}
Note: See TracBrowser for help on using the repository browser.