[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[2790] | 3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2] | 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;
|
---|
[2818] | 25 | using HeuristicLab.Collections;
|
---|
[2520] | 26 | using HeuristicLab.MainForm;
|
---|
[2] | 27 |
|
---|
[2520] | 28 | namespace HeuristicLab.Core.Views {
|
---|
[776] | 29 | /// <summary>
|
---|
[2655] | 30 | /// The visual represenation of <see cref="Scope"/>.
|
---|
[776] | 31 | /// </summary>
|
---|
[2917] | 32 | [View("Scope View")]
|
---|
[2520] | 33 | [Content(typeof(Scope), true)]
|
---|
[2727] | 34 | [Content(typeof(IScope), false)]
|
---|
[2676] | 35 | public sealed partial class ScopeView : ItemView {
|
---|
[2687] | 36 | private Dictionary<IScope, TreeNode> scopeNodeTable;
|
---|
| 37 | private Dictionary<ScopeList, IScope> subScopesScopeTable;
|
---|
[2] | 38 |
|
---|
[776] | 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>
|
---|
[2713] | 44 | public new IScope Content {
|
---|
| 45 | get { return (IScope)base.Content; }
|
---|
| 46 | set { base.Content = value; }
|
---|
[2] | 47 | }
|
---|
| 48 |
|
---|
[776] | 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>
|
---|
[2] | 53 | public ScopeView() {
|
---|
| 54 | InitializeComponent();
|
---|
| 55 | Caption = "Scope";
|
---|
[2687] | 56 | scopeNodeTable = new Dictionary<IScope, TreeNode>();
|
---|
| 57 | subScopesScopeTable = new Dictionary<ScopeList, IScope>();
|
---|
[2655] | 58 |
|
---|
[2] | 59 | }
|
---|
[776] | 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>
|
---|
[2727] | 65 | public ScopeView(IScope content)
|
---|
[2] | 66 | : this() {
|
---|
[2727] | 67 | Content = content;
|
---|
[2] | 68 | }
|
---|
| 69 |
|
---|
[776] | 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>
|
---|
[2713] | 74 | protected override void OnContentChanged() {
|
---|
| 75 | base.OnContentChanged();
|
---|
[3528] | 76 | if (scopesTreeView.Nodes.Count > 0)
|
---|
| 77 | ClearTreeNode(scopesTreeView.Nodes[0]);
|
---|
| 78 | scopesTreeView.Nodes.Clear();
|
---|
[2713] | 79 | variableCollectionView.Content = null;
|
---|
[3362] | 80 | Caption = "Scope";
|
---|
| 81 | if (Content != null) {
|
---|
[3528] | 82 | Caption = Content.Name + " (" + Content.ItemName + ")";
|
---|
[2713] | 83 | scopesTreeView.Nodes.Add(CreateTreeNode(Content));
|
---|
[2] | 84 | }
|
---|
[3362] | 85 | SetEnabledStateOfControls();
|
---|
[2] | 86 | }
|
---|
| 87 |
|
---|
[3362] | 88 | protected override void OnReadOnlyChanged() {
|
---|
| 89 | base.OnReadOnlyChanged();
|
---|
| 90 | SetEnabledStateOfControls();
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | private void SetEnabledStateOfControls() {
|
---|
| 94 | scopesTreeView.Enabled = Content != null;
|
---|
| 95 | variableCollectionView.Enabled = Content != null;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
[3528] | 98 | #region TreeNode Management
|
---|
[2687] | 99 | private TreeNode CreateTreeNode(IScope scope) {
|
---|
[2] | 100 | TreeNode node = new TreeNode();
|
---|
| 101 | node.Text = scope.Name;
|
---|
| 102 | node.Tag = scope;
|
---|
| 103 |
|
---|
| 104 | scopeNodeTable.Add(scope, node);
|
---|
[2655] | 105 | scope.NameChanged += new EventHandler(Scope_NameChanged);
|
---|
[3528] | 106 | subScopesScopeTable.Add(scope.SubScopes, scope);
|
---|
[2687] | 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);
|
---|
[2] | 112 | if (scope.SubScopes.Count > 0)
|
---|
| 113 | node.Nodes.Add(new TreeNode());
|
---|
| 114 | return node;
|
---|
| 115 | }
|
---|
[2655] | 116 |
|
---|
[3528] | 117 | private void ClearTreeNode(TreeNode node) {
|
---|
| 118 | if (scopesTreeView.SelectedNode == node) {
|
---|
| 119 | scopesTreeView.SelectedNode = null;
|
---|
| 120 | UpdateVariables();
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[2] | 123 | foreach (TreeNode child in node.Nodes)
|
---|
[3528] | 124 | ClearTreeNode(child);
|
---|
[2] | 125 |
|
---|
[2687] | 126 | IScope scope = node.Tag as IScope;
|
---|
[2655] | 127 | if (scope != null) {
|
---|
[3528] | 128 | scope.NameChanged -= new EventHandler(Scope_NameChanged);
|
---|
[2] | 129 | scopeNodeTable.Remove(scope);
|
---|
[2687] | 130 | scope.SubScopes.ItemsAdded -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsAdded);
|
---|
| 131 | scope.SubScopes.ItemsRemoved -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsRemoved);
|
---|
| 132 | scope.SubScopes.ItemsReplaced -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsReplaced);
|
---|
| 133 | scope.SubScopes.ItemsMoved -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsMoved);
|
---|
| 134 | scope.SubScopes.CollectionReset -= new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_CollectionReset);
|
---|
[2655] | 135 | subScopesScopeTable.Remove(scope.SubScopes);
|
---|
[2] | 136 | }
|
---|
| 137 | }
|
---|
[3528] | 138 | #endregion
|
---|
[2] | 139 |
|
---|
| 140 | #region TreeView Events
|
---|
[3528] | 141 | private void scopesTreeView_AfterSelect(object sender, TreeViewEventArgs e) {
|
---|
| 142 | UpdateVariables();
|
---|
| 143 | }
|
---|
[2655] | 144 | private void scopesTreeView_MouseDown(object sender, MouseEventArgs e) {
|
---|
| 145 | TreeNode node = scopesTreeView.GetNodeAt(e.X, e.Y);
|
---|
[3528] | 146 | scopesTreeView.SelectedNode = node;
|
---|
| 147 | UpdateVariables();
|
---|
[2] | 148 | }
|
---|
| 149 | private void scopesTreeView_BeforeExpand(object sender, TreeViewCancelEventArgs e) {
|
---|
| 150 | TreeNode node = e.Node;
|
---|
[2687] | 151 | IScope scope = (IScope)node.Tag;
|
---|
[2] | 152 |
|
---|
| 153 | node.Nodes.Clear();
|
---|
| 154 | for (int i = 0; i < scope.SubScopes.Count; i++)
|
---|
| 155 | node.Nodes.Add(CreateTreeNode(scope.SubScopes[i]));
|
---|
| 156 | }
|
---|
| 157 | private void scopesTreeView_AfterCollapse(object sender, System.Windows.Forms.TreeViewEventArgs e) {
|
---|
| 158 | TreeNode node = e.Node;
|
---|
[2687] | 159 | IScope scope = (IScope)node.Tag;
|
---|
[2] | 160 |
|
---|
| 161 | if (node.Nodes.Count > 0) {
|
---|
| 162 | for (int i = 0; i < node.Nodes.Count; i++)
|
---|
[3528] | 163 | ClearTreeNode(node.Nodes[i]);
|
---|
[2] | 164 | node.Nodes.Clear();
|
---|
| 165 | node.Nodes.Add(new TreeNode());
|
---|
| 166 | }
|
---|
| 167 | }
|
---|
| 168 | private void scopesTreeView_ItemDrag(object sender, ItemDragEventArgs e) {
|
---|
[3432] | 169 | if (!Locked) {
|
---|
| 170 | TreeNode node = (TreeNode)e.Item;
|
---|
| 171 | IScope scope = node.Tag as IScope;
|
---|
| 172 | if (scope != null) {
|
---|
| 173 | DataObject data = new DataObject();
|
---|
| 174 | data.SetData("Type", scope.GetType());
|
---|
| 175 | data.SetData("Value", scope);
|
---|
| 176 | DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link);
|
---|
| 177 | }
|
---|
[2] | 178 | }
|
---|
| 179 | }
|
---|
| 180 | #endregion
|
---|
| 181 |
|
---|
[3528] | 182 | #region Content Events
|
---|
[2655] | 183 | private void Scope_NameChanged(object sender, EventArgs e) {
|
---|
| 184 | if (InvokeRequired)
|
---|
| 185 | Invoke(new EventHandler(Scope_NameChanged), sender, e);
|
---|
| 186 | else {
|
---|
[2687] | 187 | IScope scope = (IScope)sender;
|
---|
[2655] | 188 | scopeNodeTable[scope].Text = scope.Name;
|
---|
[2] | 189 | }
|
---|
| 190 | }
|
---|
[2687] | 191 | private void SubScopes_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
|
---|
[2655] | 192 | if (InvokeRequired)
|
---|
[2687] | 193 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsAdded), sender, e);
|
---|
[2655] | 194 | else {
|
---|
[2687] | 195 | IScope parentScope = subScopesScopeTable[(ScopeList)sender];
|
---|
[2655] | 196 | TreeNode parentNode = scopeNodeTable[parentScope];
|
---|
[3528] | 197 | scopesTreeView.BeginUpdate();
|
---|
[2655] | 198 | if (parentNode.IsExpanded) {
|
---|
[2687] | 199 | foreach (IndexedItem<IScope> item in e.Items) {
|
---|
[2655] | 200 | TreeNode node = CreateTreeNode(item.Value);
|
---|
| 201 | parentNode.Nodes.Insert(item.Index, node);
|
---|
| 202 | }
|
---|
| 203 | } else if (parentNode.Nodes.Count == 0) {
|
---|
| 204 | parentNode.Nodes.Add(new TreeNode());
|
---|
| 205 | }
|
---|
[3528] | 206 | scopesTreeView.EndUpdate();
|
---|
[2655] | 207 | }
|
---|
[2] | 208 | }
|
---|
[2687] | 209 | private void SubScopes_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
|
---|
[2655] | 210 | if (InvokeRequired)
|
---|
[2687] | 211 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsRemoved), sender, e);
|
---|
[2655] | 212 | else {
|
---|
[2687] | 213 | IScope parentScope = subScopesScopeTable[(ScopeList)sender];
|
---|
[2655] | 214 | TreeNode parentNode = scopeNodeTable[parentScope];
|
---|
[3528] | 215 | scopesTreeView.BeginUpdate();
|
---|
[2655] | 216 | if (parentNode.IsExpanded) {
|
---|
[2687] | 217 | foreach (IndexedItem<IScope> item in e.Items) {
|
---|
[2655] | 218 | TreeNode node = scopeNodeTable[item.Value];
|
---|
[3528] | 219 | ClearTreeNode(node);
|
---|
[2655] | 220 | node.Remove();
|
---|
| 221 | }
|
---|
| 222 | } else if (parentScope.SubScopes.Count == 0) {
|
---|
| 223 | parentNode.Nodes.Clear();
|
---|
| 224 | }
|
---|
[3528] | 225 | scopesTreeView.EndUpdate();
|
---|
[2] | 226 | }
|
---|
| 227 | }
|
---|
[2687] | 228 | private void SubScopes_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
|
---|
[2655] | 229 | if (InvokeRequired)
|
---|
[2687] | 230 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsReplaced), sender, e);
|
---|
[2655] | 231 | else {
|
---|
[2687] | 232 | IScope parentScope = subScopesScopeTable[(ScopeList)sender];
|
---|
[2655] | 233 | TreeNode parentNode = scopeNodeTable[parentScope];
|
---|
[3528] | 234 | scopesTreeView.BeginUpdate();
|
---|
[2655] | 235 | if (parentNode.IsExpanded) {
|
---|
[2687] | 236 | foreach (IndexedItem<IScope> item in e.Items) {
|
---|
[2655] | 237 | TreeNode node = parentNode.Nodes[item.Index];
|
---|
[3528] | 238 | ClearTreeNode(node);
|
---|
[2655] | 239 | node.Remove();
|
---|
| 240 | node = CreateTreeNode(item.Value);
|
---|
| 241 | parentNode.Nodes.Insert(item.Index, node);
|
---|
| 242 | }
|
---|
[3528] | 243 | scopesTreeView.EndUpdate();
|
---|
[2] | 244 | }
|
---|
| 245 | }
|
---|
| 246 | }
|
---|
[2687] | 247 | private void SubScopes_ItemsMoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
|
---|
[2655] | 248 | if (InvokeRequired)
|
---|
[2687] | 249 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsMoved), sender, e);
|
---|
[2655] | 250 | else {
|
---|
[2687] | 251 | IScope parentScope = subScopesScopeTable[(ScopeList)sender];
|
---|
[2655] | 252 | TreeNode parentNode = scopeNodeTable[parentScope];
|
---|
[3528] | 253 | scopesTreeView.BeginUpdate();
|
---|
[2655] | 254 | if (parentNode.IsExpanded) {
|
---|
| 255 | parentNode.Nodes.Clear();
|
---|
[2687] | 256 | foreach (IndexedItem<IScope> item in e.Items)
|
---|
[2655] | 257 | parentNode.Nodes.Insert(item.Index, scopeNodeTable[item.Value]);
|
---|
| 258 | }
|
---|
[3528] | 259 | scopesTreeView.EndUpdate();
|
---|
[2] | 260 | }
|
---|
| 261 | }
|
---|
[2687] | 262 | private void SubScopes_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
|
---|
[2655] | 263 | if (InvokeRequired)
|
---|
[2687] | 264 | Invoke(new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_CollectionReset), sender, e);
|
---|
[2655] | 265 | else {
|
---|
[2687] | 266 | IScope parentScope = subScopesScopeTable[(ScopeList)sender];
|
---|
[2655] | 267 | TreeNode parentNode = scopeNodeTable[parentScope];
|
---|
[3528] | 268 | scopesTreeView.BeginUpdate();
|
---|
[2655] | 269 | if (parentNode.IsExpanded) {
|
---|
| 270 | foreach (TreeNode node in parentNode.Nodes)
|
---|
[3528] | 271 | ClearTreeNode(node);
|
---|
[2655] | 272 | parentNode.Nodes.Clear();
|
---|
[2687] | 273 | foreach (IndexedItem<IScope> item in e.Items) {
|
---|
[2655] | 274 | TreeNode node = CreateTreeNode(item.Value);
|
---|
| 275 | parentNode.Nodes.Insert(item.Index, node);
|
---|
| 276 | }
|
---|
| 277 | } else {
|
---|
| 278 | parentNode.Nodes.Clear();
|
---|
| 279 | if (parentScope.SubScopes.Count > 0)
|
---|
| 280 | parentNode.Nodes.Add(new TreeNode());
|
---|
| 281 | }
|
---|
[3528] | 282 | scopesTreeView.EndUpdate();
|
---|
[2] | 283 | }
|
---|
| 284 | }
|
---|
| 285 | #endregion
|
---|
[3528] | 286 |
|
---|
| 287 | #region Helpers
|
---|
| 288 | private void UpdateVariables() {
|
---|
| 289 | if (scopesTreeView.SelectedNode == null) {
|
---|
| 290 | variableCollectionView.Content = null;
|
---|
| 291 | variableCollectionView.Enabled = false;
|
---|
| 292 | } else {
|
---|
| 293 | variableCollectionView.Enabled = true;
|
---|
| 294 | variableCollectionView.Content = ((IScope)scopesTreeView.SelectedNode.Tag).Variables;
|
---|
| 295 | }
|
---|
| 296 | }
|
---|
| 297 | #endregion
|
---|
[2] | 298 | }
|
---|
| 299 | }
|
---|