Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4489 was 4489, checked in by gkronber, 14 years ago

Removed code fragments from class Scope which updated the SubScopes list when setting the parent property of a scope. ##1207

File size: 5.2 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          parent = value;
45        }
46      }
47    }
48
49    [Storable]
50    private VariableCollection variables;
51    public VariableCollection Variables {
52      get { return variables; }
53    }
54
55    [Storable]
56    private ScopeList subScopes;
57    public ScopeList SubScopes {
58      get { return subScopes; }
59    }
60
61    /// <summary>
62    /// Initializes a new instance of <see cref="Scope"/> having "Anonymous" as default name.
63    /// </summary>
64    public Scope()
65      : base("Anonymous") {
66      parent = null;
67      variables = new VariableCollection();
68      subScopes = new ScopeList();
69      Initialize();
70    }
71    /// <summary>
72    /// Initializes a new instance of <see cref="Scope"/> with the given <paramref name="name"/>.
73    /// </summary>
74    /// <param name="name">The name of the scope.</param>
75    public Scope(string name)
76      : base(name) {
77      parent = null;
78      variables = new VariableCollection();
79      subScopes = new ScopeList();
80      Initialize();
81    }
82    public Scope(string name, string description)
83      : base(name, description) {
84      parent = null;
85      variables = new VariableCollection();
86      subScopes = new ScopeList();
87      Initialize();
88    }
89    [StorableConstructor]
90    private Scope(bool deserializing) : base(deserializing) { }
91
92    [StorableHook(HookType.AfterDeserialization)]
93    private void Initialize() {
94      RegisterSubScopesEvents();
95    }
96
97    /// <inheritdoc/>
98    public void Clear() {
99      variables.Clear();
100      subScopes.Clear();
101    }
102
103    /// <inheritdoc/>
104    public override IDeepCloneable Clone(Cloner cloner) {
105      Scope clone = new Scope();
106      cloner.RegisterClonedObject(this, clone);
107      clone.name = name;
108      clone.description = description;
109      if (variables.Count > 0) clone.variables = (VariableCollection)cloner.Clone(variables);
110      if (subScopes.Count > 0) {
111        clone.subScopes = (ScopeList)cloner.Clone(subScopes);
112        foreach (IScope child in clone.SubScopes)
113          child.Parent = clone;
114        clone.Initialize();
115      }
116      return clone;
117    }
118
119    #region SubScopes Events
120    private void RegisterSubScopesEvents() {
121      if (subScopes != null) {
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);
126      }
127    }
128    private void SubScopes_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
129      foreach (IndexedItem<IScope> item in e.Items)
130        item.Value.Parent = this;
131    }
132    private void SubScopes_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<IScope>> e) {
133      foreach (IndexedItem<IScope> item in e.Items)
134        item.Value.Parent = null;
135    }
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;
141    }
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;
147    }
148    #endregion
149  }
150}
Note: See TracBrowser for help on using the repository browser.