#region License Information /* HeuristicLab * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Windows.Forms; using HeuristicLab.Collections; using HeuristicLab.MainForm; namespace HeuristicLab.Core.Views { /// /// The visual represenation of . /// [View("Scope View")] [Content(typeof(Scope), true)] [Content(typeof(IScope), false)] public sealed partial class ScopeView : ItemView { private Dictionary scopeNodeTable; private Dictionary subScopesScopeTable; /// /// Gets or sets the scope to represent visually. /// /// Uses property of base class . /// No own data storage present. public new IScope Content { get { return (IScope)base.Content; } set { base.Content = value; } } /// /// Initializes a new instance of with caption "Scope" and /// property set to false. /// public ScopeView() { InitializeComponent(); Caption = "Scope"; scopeNodeTable = new Dictionary(); subScopesScopeTable = new Dictionary(); } /// /// Initializes a new instance of with the given . /// /// Calls . /// The scope to represent visually. public ScopeView(IScope content) : this() { Content = content; } /// /// Updates all controls with the latest data of the model. /// /// Calls of base class . protected override void OnContentChanged() { base.OnContentChanged(); if (scopesTreeView.Nodes.Count > 0) { RemoveTreeNode(scopesTreeView.Nodes[0]); scopesTreeView.Nodes.Clear(); } variableCollectionView.Content = null; Caption = "Scope"; if (Content != null) { Caption = Content.Name + " (" + Content.GetType().Name + ")"; scopesTreeView.Nodes.Add(CreateTreeNode(Content)); } SetEnabledStateOfControls(); } protected override void OnReadOnlyChanged() { base.OnReadOnlyChanged(); SetEnabledStateOfControls(); } private void SetEnabledStateOfControls() { scopesTreeView.Enabled = Content != null; variableCollectionView.Enabled = Content != null; } private TreeNode CreateTreeNode(IScope scope) { TreeNode node = new TreeNode(); node.Text = scope.Name; node.Tag = scope; scopeNodeTable.Add(scope, node); scope.NameChanged += new EventHandler(Scope_NameChanged); scope.SubScopes.ItemsAdded += new CollectionItemsChangedEventHandler>(SubScopes_ItemsAdded); scope.SubScopes.ItemsRemoved += new CollectionItemsChangedEventHandler>(SubScopes_ItemsRemoved); scope.SubScopes.ItemsReplaced += new CollectionItemsChangedEventHandler>(SubScopes_ItemsReplaced); scope.SubScopes.ItemsMoved += new CollectionItemsChangedEventHandler>(SubScopes_ItemsMoved); scope.SubScopes.CollectionReset += new CollectionItemsChangedEventHandler>(SubScopes_CollectionReset); subScopesScopeTable.Add(scope.SubScopes, scope); if (scope.SubScopes.Count > 0) node.Nodes.Add(new TreeNode()); return node; } private void RemoveTreeNode(TreeNode node) { foreach (TreeNode child in node.Nodes) RemoveTreeNode(child); IScope scope = node.Tag as IScope; if (scope != null) { scopeNodeTable.Remove(scope); scope.NameChanged -= new EventHandler(Scope_NameChanged); scope.SubScopes.ItemsAdded -= new CollectionItemsChangedEventHandler>(SubScopes_ItemsAdded); scope.SubScopes.ItemsRemoved -= new CollectionItemsChangedEventHandler>(SubScopes_ItemsRemoved); scope.SubScopes.ItemsReplaced -= new CollectionItemsChangedEventHandler>(SubScopes_ItemsReplaced); scope.SubScopes.ItemsMoved -= new CollectionItemsChangedEventHandler>(SubScopes_ItemsMoved); scope.SubScopes.CollectionReset -= new CollectionItemsChangedEventHandler>(SubScopes_CollectionReset); subScopesScopeTable.Remove(scope.SubScopes); } } #region TreeView Events private void scopesTreeView_MouseDown(object sender, MouseEventArgs e) { TreeNode node = scopesTreeView.GetNodeAt(e.X, e.Y); if ((node != null) && (node.Tag is IScope)) { variableCollectionView.Content = ((IScope)node.Tag).Variables; variableCollectionView.Enabled = true; } else { variableCollectionView.Content = null; variableCollectionView.Enabled = false; if (node == null) scopesTreeView.SelectedNode = null; } } private void scopesTreeView_BeforeExpand(object sender, TreeViewCancelEventArgs e) { TreeNode node = e.Node; IScope scope = (IScope)node.Tag; node.Nodes.Clear(); for (int i = 0; i < scope.SubScopes.Count; i++) node.Nodes.Add(CreateTreeNode(scope.SubScopes[i])); } private void scopesTreeView_AfterCollapse(object sender, System.Windows.Forms.TreeViewEventArgs e) { TreeNode node = e.Node; IScope scope = (IScope)node.Tag; if (node.Nodes.Count > 0) { for (int i = 0; i < node.Nodes.Count; i++) RemoveTreeNode(node.Nodes[i]); node.Nodes.Clear(); node.Nodes.Add(new TreeNode()); } } private void scopesTreeView_ItemDrag(object sender, ItemDragEventArgs e) { if (!Locked) { TreeNode node = (TreeNode)e.Item; IScope scope = node.Tag as IScope; if (scope != null) { DataObject data = new DataObject(); data.SetData("Type", scope.GetType()); data.SetData("Value", scope); DoDragDrop(data, DragDropEffects.Copy | DragDropEffects.Link); } } } #endregion #region Scope Events private void Scope_NameChanged(object sender, EventArgs e) { if (InvokeRequired) Invoke(new EventHandler(Scope_NameChanged), sender, e); else { IScope scope = (IScope)sender; scopeNodeTable[scope].Text = scope.Name; } } #endregion #region SubScopes Events private void SubScopes_ItemsAdded(object sender, CollectionItemsChangedEventArgs> e) { if (InvokeRequired) Invoke(new CollectionItemsChangedEventHandler>(SubScopes_ItemsAdded), sender, e); else { IScope parentScope = subScopesScopeTable[(ScopeList)sender]; TreeNode parentNode = scopeNodeTable[parentScope]; if (parentNode.IsExpanded) { foreach (IndexedItem item in e.Items) { TreeNode node = CreateTreeNode(item.Value); parentNode.Nodes.Insert(item.Index, node); } } else if (parentNode.Nodes.Count == 0) { parentNode.Nodes.Add(new TreeNode()); } } } private void SubScopes_ItemsRemoved(object sender, CollectionItemsChangedEventArgs> e) { if (InvokeRequired) Invoke(new CollectionItemsChangedEventHandler>(SubScopes_ItemsRemoved), sender, e); else { IScope parentScope = subScopesScopeTable[(ScopeList)sender]; TreeNode parentNode = scopeNodeTable[parentScope]; if (parentNode.IsExpanded) { foreach (IndexedItem item in e.Items) { TreeNode node = scopeNodeTable[item.Value]; RemoveTreeNode(node); node.Remove(); } } else if (parentScope.SubScopes.Count == 0) { parentNode.Nodes.Clear(); } } } private void SubScopes_ItemsReplaced(object sender, CollectionItemsChangedEventArgs> e) { if (InvokeRequired) Invoke(new CollectionItemsChangedEventHandler>(SubScopes_ItemsReplaced), sender, e); else { IScope parentScope = subScopesScopeTable[(ScopeList)sender]; TreeNode parentNode = scopeNodeTable[parentScope]; if (parentNode.IsExpanded) { foreach (IndexedItem item in e.Items) { TreeNode node = parentNode.Nodes[item.Index]; RemoveTreeNode(node); node.Remove(); node = CreateTreeNode(item.Value); parentNode.Nodes.Insert(item.Index, node); } } } } private void SubScopes_ItemsMoved(object sender, CollectionItemsChangedEventArgs> e) { if (InvokeRequired) Invoke(new CollectionItemsChangedEventHandler>(SubScopes_ItemsMoved), sender, e); else { IScope parentScope = subScopesScopeTable[(ScopeList)sender]; TreeNode parentNode = scopeNodeTable[parentScope]; if (parentNode.IsExpanded) { parentNode.Nodes.Clear(); foreach (IndexedItem item in e.Items) parentNode.Nodes.Insert(item.Index, scopeNodeTable[item.Value]); } } } private void SubScopes_CollectionReset(object sender, CollectionItemsChangedEventArgs> e) { if (InvokeRequired) Invoke(new CollectionItemsChangedEventHandler>(SubScopes_CollectionReset), sender, e); else { IScope parentScope = subScopesScopeTable[(ScopeList)sender]; TreeNode parentNode = scopeNodeTable[parentScope]; if (parentNode.IsExpanded) { foreach (TreeNode node in parentNode.Nodes) RemoveTreeNode(node); parentNode.Nodes.Clear(); foreach (IndexedItem item in e.Items) { TreeNode node = CreateTreeNode(item.Value); parentNode.Nodes.Insert(item.Index, node); } } else { parentNode.Nodes.Clear(); if (parentScope.SubScopes.Count > 0) parentNode.Nodes.Add(new TreeNode()); } } } #endregion } }