Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4128 was 3528, checked in by swagner, 15 years ago

Implemented reviewers' comments (#893)

File size: 5.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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
22using System.Drawing;
23using HeuristicLab.Collections;
24using HeuristicLab.Common;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26
27namespace HeuristicLab.Core {
28  /// <summary>
29  /// Hierarchical container of variables (and of subscopes).
30  /// </summary>
31  [Item("Scope", "A scope which contains variables and sub-scopes.")]
32  [StorableClass]
33  public sealed class Scope : NamedItem, IScope {
34    public override Image ItemImage {
35      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.OrgChart; }
36    }
37
38    [Storable]
39    private IScope parent;
40    public IScope Parent {
41      get { return parent; }
42      set {
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        }
50      }
51    }
52
53    [Storable]
54    private VariableCollection variables;
55    public VariableCollection Variables {
56      get { return variables; }
57    }
58
59    [Storable]
60    private ScopeList subScopes;
61    public ScopeList SubScopes {
62      get { return subScopes; }
63    }
64
65    /// <summary>
66    /// Initializes a new instance of <see cref="Scope"/> having "Anonymous" as default name.
67    /// </summary>
68    public Scope()
69      : base("Anonymous") {
70      parent = null;
71      variables = new VariableCollection();
72      subScopes = new ScopeList();
73      Initialize();
74    }
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>
79    public Scope(string name)
80      : base(name) {
81      parent = null;
82      variables = new VariableCollection();
83      subScopes = new ScopeList();
84      Initialize();
85    }
86    public Scope(string name, string description)
87      : base(name, description) {
88      parent = null;
89      variables = new VariableCollection();
90      subScopes = new ScopeList();
91      Initialize();
92    }
93    [StorableConstructor]
94    private Scope(bool deserializing) : base(deserializing) { }
95
96    [StorableHook(HookType.AfterDeserialization)]
97    private void Initialize() {
98      RegisterSubScopesEvents();
99    }
100
101    /// <inheritdoc/>
102    public void Clear() {
103      variables.Clear();
104      subScopes.Clear();
105    }
106
107    /// <inheritdoc/>
108    public override IDeepCloneable Clone(Cloner cloner) {
109      Scope clone = new Scope();
110      cloner.RegisterClonedObject(this, clone);
111      clone.name = name;
112      clone.description = description;
113      if (variables.Count > 0) clone.variables = (VariableCollection)cloner.Clone(variables);
114      if (subScopes.Count > 0) {
115        clone.subScopes = (ScopeList)cloner.Clone(subScopes);
116        foreach (IScope child in clone.SubScopes)
117          child.Parent = clone;
118        clone.Initialize();
119      }
120      return clone;
121    }
122
123    #region SubScopes Events
124    private void RegisterSubScopesEvents() {
125      if (subScopes != null) {
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);
130      }
131    }
132    private void SubScopes_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
133      foreach (IndexedItem<IScope> item in e.Items)
134        item.Value.Parent = this;
135    }
136    private void SubScopes_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
137      foreach (IndexedItem<IScope> item in e.Items)
138        item.Value.Parent = null;
139    }
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;
145    }
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;
151    }
152    #endregion
153  }
154}
Note: See TracBrowser for help on using the repository browser.