Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.0/sources/HeuristicLab.Core/ScopeView.cs @ 7624

Last change on this file since 7624 was 2, checked in by swagner, 17 years ago

Added HeuristicLab 3.0 sources from former SVN repository at revision 52

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