Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2205: Worked on optimization networks

File size: 4.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.Drawing;
26
27namespace HeuristicLab.Core.Networks {
28  [Item("MessageValue", "A value of a message sent between ports.")]
29  [StorableClass]
30  public class MessageValue<T> : Item, IMessageValue<T> where T : class, IItem {
31    public static new Image StaticItemImage {
32      get { return HeuristicLab.Common.Resources.VSImageLibrary.Field; }
33    }
34    public override Image ItemImage {
35      get {
36        if (value != null) return value.ItemImage;
37        else return base.ItemImage;
38      }
39    }
40
41    [Storable]
42    protected string name;
43    public string Name {
44      get { return name; }
45    }
46    public Type DataType {
47      get { return typeof(T); }
48    }
49    [Storable]
50    protected T value;
51    public T Value {
52      get { return value; }
53      set {
54        if (value != this.value) {
55          DeregisterValueEvents();
56          this.value = value;
57          RegisterValueEvents();
58          OnValueChanged();
59        }
60      }
61    }
62    IItem IMessageValue.Value {
63      get { return Value; }
64      set {
65        T val = value as T;
66        if ((value != null) && (val == null))
67          throw new InvalidOperationException(
68            string.Format("Type mismatch. Value is not a \"{0}\".",
69                          typeof(T).GetPrettyName())
70          );
71        Value = val;
72      }
73    }
74
75    [StorableConstructor]
76    protected MessageValue(bool deserializing) : base(deserializing) { }
77    protected MessageValue(MessageValue<T> original, Cloner cloner)
78      : base(original, cloner) {
79      this.name = original.name;
80      this.value = cloner.Clone(original.value);
81      RegisterValueEvents();
82    }
83    public MessageValue() {
84      name = "MessageValue";
85    }
86    public MessageValue(string name) {
87      if (name == null) this.name = string.Empty;
88      else this.name = name;
89    }
90    public MessageValue(string name, T value)
91      : this(name) {
92      this.value = value;
93      RegisterValueEvents();
94    }
95
96    [StorableHook(HookType.AfterDeserialization)]
97    private void AfterDeserialization() {
98      RegisterValueEvents();
99    }
100
101    public override IDeepCloneable Clone(Cloner cloner) {
102      return new MessageValue<T>(this, cloner);
103    }
104
105    public override string ToString() {
106      if (Value == null)
107        return string.Format("{0}: null", Name);
108      else
109        return string.Format("{0}: {1}", Name, Value.ToString());
110    }
111
112    public event EventHandler ValueChanged;
113    protected virtual void OnValueChanged() {
114      var handler = ValueChanged;
115      if (handler != null) handler(this, EventArgs.Empty);
116      OnItemImageChanged();
117      OnToStringChanged();
118    }
119    protected virtual void RegisterValueEvents() {
120      if (value != null) {
121        value.ItemImageChanged += Value_ItemImageChanged;
122        value.ToStringChanged += Value_ToStringChanged;
123      }
124    }
125    protected virtual void DeregisterValueEvents() {
126      if (value != null) {
127        value.ItemImageChanged -= Value_ItemImageChanged;
128        value.ToStringChanged -= Value_ToStringChanged;
129      }
130    }
131    private void Value_ItemImageChanged(object sender, EventArgs e) {
132      OnItemImageChanged();
133    }
134    private void Value_ToStringChanged(object sender, EventArgs e) {
135      OnToStringChanged();
136    }
137  }
138}
Note: See TracBrowser for help on using the repository browser.