Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Port.cs @ 11359

Last change on this file since 11359 was 11177, checked in by swagner, 11 years ago

#2205: Worked on optimization networks

File size: 4.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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;
23using System.Drawing;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Optimization.Networks {
29  [Item("Port", "A port of an optimization network entity.")]
30  [StorableClass]
31  public class Port<T> : NamedItem, IPort<T> where T : class, IItem {
32    public override Image ItemImage {
33      get {
34        if (value != null) return value.ItemImage;
35        else return base.ItemImage;
36      }
37    }
38
39    [Storable]
40    protected T value;
41    public T Value {
42      get { return value; }
43      set {
44        if (value != this.value) {
45          DeregisterValueEvents();
46          this.value = value;
47          RegisterValueEvents();
48          OnValueChanged();
49        }
50      }
51    }
52    object IPort.Value {
53      get { return Value; }
54      set {
55        T val = value as T;
56        if ((value != null) && (val == null))
57          throw new InvalidOperationException(
58            string.Format("Type mismatch. Value is not a \"{0}\".",
59                          typeof(T).GetPrettyName())
60          );
61        Value = val;
62      }
63    }
64
65    [StorableConstructor]
66    protected Port(bool deserializing) : base(deserializing) { }
67    protected Port(Port<T> original, Cloner cloner)
68      : base(original, cloner) {
69      value = cloner.Clone(original.value);
70      RegisterValueEvents();
71    }
72    public Port() : base("Anonymous") { }
73    public Port(string name) : base(name) { }
74    public Port(string name, string description) : base(name, description) { }
75    public Port(string name, T value)
76      : base(name) {
77      this.value = value;
78      RegisterValueEvents();
79    }
80    public Port(string name, string description, T value)
81      : base(name, description) {
82      this.value = value;
83      RegisterValueEvents();
84    }
85
86    [StorableHook(HookType.AfterDeserialization)]
87    private void AfterDeserialization() {
88      RegisterValueEvents();
89    }
90
91    public override IDeepCloneable Clone(Cloner cloner) {
92      return new Port<T>(this, cloner);
93    }
94
95    public override string ToString() {
96      return Name + ": " + (Value != null ? Value.ToString() : "null");
97    }
98
99    public event EventHandler ValueChanged;
100    protected virtual void OnValueChanged() {
101      EventHandler handler = ValueChanged;
102      if (handler != null) handler(this, EventArgs.Empty);
103      OnItemImageChanged();
104      OnToStringChanged();
105    }
106
107    private void RegisterValueEvents() {
108      if (value != null) {
109        value.ItemImageChanged += new EventHandler(Value_ItemImageChanged);
110        value.ToStringChanged += new EventHandler(Value_ToStringChanged);
111      }
112    }
113    private void DeregisterValueEvents() {
114      if (value != null) {
115        value.ItemImageChanged -= new EventHandler(Value_ItemImageChanged);
116        value.ToStringChanged -= new EventHandler(Value_ToStringChanged);
117      }
118    }
119    private void Value_ItemImageChanged(object sender, EventArgs e) {
120      OnItemImageChanged();
121    }
122    private void Value_ToStringChanged(object sender, EventArgs e) {
123      OnToStringChanged();
124    }
125  }
126}
Note: See TracBrowser for help on using the repository browser.