Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 1717 was 1529, checked in by gkronber, 16 years ago

Moved source files of plugins AdvancedOptimizationFrontEnd ... Grid into version-specific sub-folders. #576

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