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