Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Core.Networks/Entity.cs @ 11526

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

#2205: Worked on optimization networks

File size: 3.2 KB
RevLine 
[11449]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
[11526]29namespace HeuristicLab.Core.Networks {
[11449]30  [Item("Entity", "Abstract base class for entities of an optimization network.")]
31  [StorableClass]
32  public abstract class Entity : NamedItem, IEntity {
33    public static readonly string PathSeparator = " => ";
34    public static new Image StaticItemImage {
35      get { return HeuristicLab.Common.Resources.VSImageLibrary.Class; }
36    }
37
38    private IEntity parent;
39    public IEntity 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<IEntity> Children {
51      get { return Enumerable.Empty<IEntity>(); }
52    }
53    public string Path {
54      get {
55        return parent != null ? parent.Path + PathSeparator + Name : Name;
56      }
57    }
58
59    [StorableConstructor]
60    protected Entity(bool deserializing) : base(deserializing) { }
61    protected Entity(Entity original, Cloner cloner) : base(original, cloner) { }
62    protected Entity() : base("Entity") { }
63    protected Entity(string name) : base(name) { }
64    protected Entity(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.