Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 2546 was 2546, checked in by swagner, 14 years ago

Continued work on Optimizer and on adapting all views to the new MainForm concept (#770)

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