Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Core.Networks/NetworkItem.cs @ 11529

Last change on this file since 11529 was 11528, checked in by swagner, 9 years ago

#2205: Implemented review comments

  • adapted formatting of multi-line LINQ queries
  • renamed Entity to NetworkItem
File size: 3.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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 HeuristicLab.Common;
23using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
24using System;
25using System.Collections.Generic;
26using System.Drawing;
27using System.Linq;
28
29namespace HeuristicLab.Core.Networks {
30  [Item("NetworkItem", "Abstract base class for items of a network.")]
31  [StorableClass]
32  public abstract class NetworkItem : NamedItem, INetworkItem {
33    public static readonly string PathSeparator = " => ";
34    public static new Image StaticItemImage {
35      get { return HeuristicLab.Common.Resources.VSImageLibrary.Class; }
36    }
37
38    private INetworkItem parent;
39    public INetworkItem Parent {
40      get { return parent; }
41      protected set {
42        if (value != parent) {
43          DeregisterParentEvents();
44          parent = value;
45          RegisterParentEvents();
46          OnPathChanged();
47        }
48      }
49    }
50    public virtual IEnumerable<INetworkItem> Children {
51      get { return Enumerable.Empty<INetworkItem>(); }
52    }
53    public string Path {
54      get {
55        return parent != null ? parent.Path + PathSeparator + Name : Name;
56      }
57    }
58
59    [StorableConstructor]
60    protected NetworkItem(bool deserializing) : base(deserializing) { }
61    protected NetworkItem(NetworkItem original, Cloner cloner) : base(original, cloner) { }
62    protected NetworkItem() : base("NetworkItem") { }
63    protected NetworkItem(string name) : base(name) { }
64    protected NetworkItem(string name, string description) : base(name, description) { }
65
66    protected override void OnNameChanging(CancelEventArgs<string> e) {
67      base.OnNameChanging(e);
68      e.Cancel = e.Cancel || e.Value.Contains(PathSeparator);
69    }
70    protected override void OnNameChanged() {
71      base.OnNameChanged();
72      OnPathChanged();
73    }
74
75    public event EventHandler PathChanged;
76    protected virtual void OnPathChanged() {
77      var handler = PathChanged;
78      if (handler != null) handler(this, EventArgs.Empty);
79    }
80
81    #region Parent Events
82    protected virtual void RegisterParentEvents() {
83      if (parent != null) {
84        parent.PathChanged += Parent_PathChanged;
85      }
86    }
87    protected virtual void DeregisterParentEvents() {
88      if (parent != null) {
89        parent.PathChanged -= Parent_PathChanged;
90      }
91    }
92    void Parent_PathChanged(object sender, EventArgs e) {
93      OnPathChanged();
94    }
95    #endregion
96  }
97}
Note: See TracBrowser for help on using the repository browser.