Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/3.3/Scope.cs @ 4058

Last change on this file since 4058 was 3528, checked in by swagner, 14 years ago

Implemented reviewers' comments (#893)

File size: 5.5 KB
RevLine 
[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
[3528]22using System.Drawing;
[2830]23using HeuristicLab.Collections;
[3376]24using HeuristicLab.Common;
[1823]25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[2]26
27namespace HeuristicLab.Core {
[776]28  /// <summary>
29  /// Hierarchical container of variables (and of subscopes).
30  /// </summary>
[2653]31  [Item("Scope", "A scope which contains variables and sub-scopes.")]
[3017]32  [StorableClass]
[2687]33  public sealed class Scope : NamedItem, IScope {
[3528]34    public override Image ItemImage {
35      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.OrgChart; }
36    }
37
[1727]38    [Storable]
[2687]39    private IScope parent;
40    public IScope Parent {
41      get { return parent; }
42      set {
[3280]43        if (parent != value) {
44          IScope oldParent = parent;
45          parent = null;
46          if (oldParent != null) oldParent.SubScopes.Remove(this);
47          parent = value;
48          if ((parent != null) && !parent.SubScopes.Contains(this)) parent.SubScopes.Add(this);
49        }
[2687]50      }
51    }
[2]52
[2932]53    [Storable]
[2653]54    private VariableCollection variables;
55    public VariableCollection Variables {
56      get { return variables; }
[1727]57    }
[1667]58
[3317]59    [Storable]
[2653]60    private ScopeList subScopes;
61    public ScopeList SubScopes {
62      get { return subScopes; }
[2]63    }
64
[776]65    /// <summary>
66    /// Initializes a new instance of <see cref="Scope"/> having "Anonymous" as default name.
67    /// </summary>
[2653]68    public Scope()
69      : base("Anonymous") {
70      parent = null;
[2932]71      variables = new VariableCollection();
[3317]72      subScopes = new ScopeList();
73      Initialize();
[2]74    }
[776]75    /// <summary>
76    /// Initializes a new instance of <see cref="Scope"/> with the given <paramref name="name"/>.
77    /// </summary>
78    /// <param name="name">The name of the scope.</param>
[2]79    public Scope(string name)
[2653]80      : base(name) {
81      parent = null;
[2932]82      variables = new VariableCollection();
[3317]83      subScopes = new ScopeList();
84      Initialize();
[2]85    }
[2931]86    public Scope(string name, string description)
87      : base(name, description) {
88      parent = null;
[2932]89      variables = new VariableCollection();
[3317]90      subScopes = new ScopeList();
91      Initialize();
[2931]92    }
[3317]93    [StorableConstructor]
94    private Scope(bool deserializing) : base(deserializing) { }
[2]95
[3317]96    [StorableHook(HookType.AfterDeserialization)]
97    private void Initialize() {
98      RegisterSubScopesEvents();
99    }
100
[776]101    /// <inheritdoc/>
[2]102    public void Clear() {
[2653]103      variables.Clear();
104      subScopes.Clear();
[2]105    }
106
[776]107    /// <inheritdoc/>
[2653]108    public override IDeepCloneable Clone(Cloner cloner) {
[2687]109      Scope clone = new Scope();
110      cloner.RegisterClonedObject(this, clone);
[3317]111      clone.name = name;
112      clone.description = description;
[2932]113      if (variables.Count > 0) clone.variables = (VariableCollection)cloner.Clone(variables);
[3280]114      if (subScopes.Count > 0) {
[3317]115        clone.subScopes = (ScopeList)cloner.Clone(subScopes);
[3280]116        foreach (IScope child in clone.SubScopes)
117          child.Parent = clone;
[3317]118        clone.Initialize();
[3280]119      }
[2]120      return clone;
121    }
122
[2653]123    #region SubScopes Events
124    private void RegisterSubScopesEvents() {
125      if (subScopes != null) {
[2687]126        subScopes.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsAdded);
127        subScopes.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsRemoved);
128        subScopes.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsReplaced);
129        subScopes.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_CollectionReset);
[2653]130      }
[2]131    }
[2687]132    private void SubScopes_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
133      foreach (IndexedItem<IScope> item in e.Items)
134        item.Value.Parent = this;
[61]135    }
[2687]136    private void SubScopes_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
137      foreach (IndexedItem<IScope> item in e.Items)
138        item.Value.Parent = null;
[61]139    }
[2687]140    private void SubScopes_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
141      foreach (IndexedItem<IScope> oldItem in e.OldItems)
142        oldItem.Value.Parent = null;
143      foreach (IndexedItem<IScope> item in e.Items)
144        item.Value.Parent = this;
[2]145    }
[2687]146    private void SubScopes_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
147      foreach (IndexedItem<IScope> oldItem in e.OldItems)
148        oldItem.Value.Parent = null;
149      foreach (IndexedItem<IScope> item in e.Items)
150        item.Value.Parent = this;
[2]151    }
[2653]152    #endregion
[2]153  }
154}
Note: See TracBrowser for help on using the repository browser.