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