Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6549 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 5.3 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 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 {
[5287]35      get { return HeuristicLab.Common.Resources.VSImageLibrary.OrgChart; }
[3528]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          parent = value;
45        }
[2687]46      }
47    }
[2]48
[2932]49    [Storable]
[2653]50    private VariableCollection variables;
51    public VariableCollection Variables {
52      get { return variables; }
[1727]53    }
[1667]54
[3317]55    [Storable]
[2653]56    private ScopeList subScopes;
57    public ScopeList SubScopes {
58      get { return subScopes; }
[2]59    }
60
[4722]61    [StorableConstructor]
62    private Scope(bool deserializing) : base(deserializing) { }
63    private Scope(Scope original, Cloner cloner)
64      : base(original, cloner) {
65      if (original.variables.Count > 0) variables = cloner.Clone(original.variables);
66      else variables = new VariableCollection();
67      if (original.subScopes.Count > 0) {
68        subScopes = cloner.Clone(original.subScopes);
69        foreach (IScope child in SubScopes)
70          child.Parent = this;
71      } else subScopes = new ScopeList();
72      RegisterSubScopesEvents();
73    }
[776]74    /// <summary>
75    /// Initializes a new instance of <see cref="Scope"/> having "Anonymous" as default name.
76    /// </summary>
[2653]77    public Scope()
78      : base("Anonymous") {
79      parent = null;
[2932]80      variables = new VariableCollection();
[3317]81      subScopes = new ScopeList();
[4722]82      RegisterSubScopesEvents();
[2]83    }
[776]84    /// <summary>
85    /// Initializes a new instance of <see cref="Scope"/> with the given <paramref name="name"/>.
86    /// </summary>
87    /// <param name="name">The name of the scope.</param>
[2]88    public Scope(string name)
[2653]89      : base(name) {
90      parent = null;
[2932]91      variables = new VariableCollection();
[3317]92      subScopes = new ScopeList();
[4722]93      RegisterSubScopesEvents();
[2]94    }
[2931]95    public Scope(string name, string description)
96      : base(name, description) {
97      parent = null;
[2932]98      variables = new VariableCollection();
[3317]99      subScopes = new ScopeList();
[4722]100      RegisterSubScopesEvents();
[2931]101    }
[2]102
[3317]103    [StorableHook(HookType.AfterDeserialization)]
[4722]104    private void AfterDeserialization() {
[3317]105      RegisterSubScopesEvents();
106    }
107
[776]108    /// <inheritdoc/>
[2]109    public void Clear() {
[2653]110      variables.Clear();
111      subScopes.Clear();
[2]112    }
113
[776]114    /// <inheritdoc/>
[2653]115    public override IDeepCloneable Clone(Cloner cloner) {
[4722]116      return new Scope(this, cloner);
[2]117    }
118
[2653]119    #region SubScopes Events
120    private void RegisterSubScopesEvents() {
121      if (subScopes != null) {
[2687]122        subScopes.ItemsAdded += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsAdded);
123        subScopes.ItemsRemoved += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsRemoved);
124        subScopes.ItemsReplaced += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_ItemsReplaced);
125        subScopes.CollectionReset += new CollectionItemsChangedEventHandler<IndexedItem<IScope>>(SubScopes_CollectionReset);
[2653]126      }
[2]127    }
[2687]128    private void SubScopes_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
129      foreach (IndexedItem<IScope> item in e.Items)
130        item.Value.Parent = this;
[61]131    }
[2687]132    private void SubScopes_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
133      foreach (IndexedItem<IScope> item in e.Items)
134        item.Value.Parent = null;
[61]135    }
[2687]136    private void SubScopes_ItemsReplaced(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
137      foreach (IndexedItem<IScope> oldItem in e.OldItems)
138        oldItem.Value.Parent = null;
139      foreach (IndexedItem<IScope> item in e.Items)
140        item.Value.Parent = this;
[2]141    }
[2687]142    private void SubScopes_CollectionReset(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
143      foreach (IndexedItem<IScope> oldItem in e.OldItems)
144        oldItem.Value.Parent = null;
145      foreach (IndexedItem<IScope> item in e.Items)
146        item.Value.Parent = this;
[2]147    }
[2653]148    #endregion
[2]149  }
150}
Note: See TracBrowser for help on using the repository browser.