#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.Drawing; using HeuristicLab.Collections; using HeuristicLab.Common; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Core { /// /// Hierarchical container of variables (and of subscopes). /// [Item("Scope", "A scope which contains variables and sub-scopes.")] [StorableClass] public sealed class Scope : NamedItem, IScope { public override Image ItemImage { get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.OrgChart; } } [Storable] private IScope parent; public IScope Parent { get { return parent; } set { if (parent != value) { IScope oldParent = parent; parent = null; if (oldParent != null) oldParent.SubScopes.Remove(this); parent = value; if ((parent != null) && !parent.SubScopes.Contains(this)) parent.SubScopes.Add(this); } } } [Storable] private VariableCollection variables; public VariableCollection Variables { get { return variables; } } [Storable] private ScopeList subScopes; public ScopeList SubScopes { get { return subScopes; } } /// /// Initializes a new instance of having "Anonymous" as default name. /// public Scope() : base("Anonymous") { parent = null; variables = new VariableCollection(); subScopes = new ScopeList(); Initialize(); } /// /// 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(); Initialize(); } public Scope(string name, string description) : base(name, description) { parent = null; variables = new VariableCollection(); subScopes = new ScopeList(); Initialize(); } [StorableConstructor] private Scope(bool deserializing) : base(deserializing) { } [StorableHook(HookType.AfterDeserialization)] private void Initialize() { RegisterSubScopesEvents(); } /// public void Clear() { variables.Clear(); subScopes.Clear(); } /// public override IDeepCloneable Clone(Cloner cloner) { Scope clone = new Scope(); cloner.RegisterClonedObject(this, clone); clone.name = name; clone.description = description; if (variables.Count > 0) clone.variables = (VariableCollection)cloner.Clone(variables); if (subScopes.Count > 0) { clone.subScopes = (ScopeList)cloner.Clone(subScopes); foreach (IScope child in clone.SubScopes) child.Parent = clone; clone.Initialize(); } return clone; } #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 } }