Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Ports/MessageValue.cs @ 11500

Last change on this file since 11500 was 11500, checked in by swagner, 10 years ago

#2205: Worked on optimization networks

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