#region License Information
/* HeuristicLab
* Copyright (C) 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.Drawing;
using HeuristicLab.Collections;
using HeuristicLab.Common;
using HEAL.Attic;
namespace HeuristicLab.Core {
///
/// Hierarchical container of variables (and of subscopes).
///
[Item("Scope", "A scope which contains variables and sub-scopes.")]
[StorableType("57AAC5CA-2EEF-4ECB-80DA-1B8F3B750A2B")]
public sealed class Scope : NamedItem, IScope {
public static new Image StaticItemImage {
get { return HeuristicLab.Common.Resources.VSImageLibrary.OrgChart; }
}
[Storable]
private IScope parent;
public IScope Parent {
get { return parent; }
set {
if (parent != value) {
parent = value;
}
}
}
[Storable]
private VariableCollection variables;
public VariableCollection Variables {
get { return variables; }
}
[Storable]
private ScopeList subScopes;
public ScopeList SubScopes {
get { return subScopes; }
}
[StorableConstructor]
private Scope(StorableConstructorFlag _) : base(_) { }
private Scope(Scope original, Cloner cloner)
: base(original, cloner) {
if (original.variables.Count > 0) variables = cloner.Clone(original.variables);
else variables = new VariableCollection();
if (original.subScopes.Count > 0) {
subScopes = cloner.Clone(original.subScopes);
foreach (IScope child in SubScopes)
child.Parent = this;
} else subScopes = new ScopeList();
RegisterSubScopesEvents();
}
///
/// Initializes a new instance of having "Anonymous" as default name.
///
public Scope()
: base("Anonymous") {
parent = null;
variables = new VariableCollection();
subScopes = new ScopeList();
RegisterSubScopesEvents();
}
///
/// Initializes a new instance of with the given .
///
/// The name of the scope.
public Scope(string name)
: base(name) {
parent = null;
variables = new VariableCollection();
subScopes = new ScopeList();
RegisterSubScopesEvents();
}
public Scope(string name, string description)
: base(name, description) {
parent = null;
variables = new VariableCollection();
subScopes = new ScopeList();
RegisterSubScopesEvents();
}
[StorableHook(HookType.AfterDeserialization)]
private void AfterDeserialization() {
RegisterSubScopesEvents();
}
///
public void Clear() {
variables.Clear();
subScopes.Clear();
}
///
public override IDeepCloneable Clone(Cloner cloner) {
return new Scope(this, cloner);
}
#region SubScopes Events
private void RegisterSubScopesEvents() {
if (subScopes != null) {
subScopes.ItemsAdded += new CollectionItemsChangedEventHandler>(SubScopes_ItemsAdded);
subScopes.ItemsRemoved += new CollectionItemsChangedEventHandler>(SubScopes_ItemsRemoved);
subScopes.ItemsReplaced += new CollectionItemsChangedEventHandler>(SubScopes_ItemsReplaced);
subScopes.CollectionReset += new CollectionItemsChangedEventHandler>(SubScopes_CollectionReset);
}
}
private void SubScopes_ItemsAdded(object sender, CollectionItemsChangedEventArgs> e) {
foreach (IndexedItem item in e.Items)
item.Value.Parent = this;
}
private void SubScopes_ItemsRemoved(object sender, CollectionItemsChangedEventArgs> e) {
foreach (IndexedItem item in e.Items)
item.Value.Parent = null;
}
private void SubScopes_ItemsReplaced(object sender, CollectionItemsChangedEventArgs> e) {
foreach (IndexedItem oldItem in e.OldItems)
oldItem.Value.Parent = null;
foreach (IndexedItem item in e.Items)
item.Value.Parent = this;
}
private void SubScopes_CollectionReset(object sender, CollectionItemsChangedEventArgs> e) {
foreach (IndexedItem oldItem in e.OldItems)
oldItem.Value.Parent = null;
foreach (IndexedItem item in e.Items)
item.Value.Parent = this;
}
#endregion
}
}