Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Networks/3.3/### obsolete/ServiceParameter.cs @ 12858

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

#2205: Worked on optimization networks

File size: 4.7 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("ServiceParameter", "A parameter of a service port.")]
30  [StorableClass]
31  public class ServiceParameter<T> : Entity, IServiceParameter<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 (Type == ServiceParameterType.Input) return HeuristicLab.Common.Resources.VSImageLibrary.ArrowDown;
38        else if (Type == ServiceParameterType.Output) return HeuristicLab.Common.Resources.VSImageLibrary.ArrowUp;
39        else return base.ItemImage;
40      }
41    }
42
43    new public IPort Parent {
44      get { return (IPort)base.Parent; }
45      set {
46        // NOTE: never call setter directly as the Parent property is set by the node which contains the port
47        base.Parent = value;
48      }
49    }
50    public Type DataType {
51      get { return typeof(T); }
52    }
53    [Storable]
54    private ServiceParameterType type;
55    public ServiceParameterType Type {
56      get { return type; }
57      set {
58        if (value != type) {
59          type = value;
60          OnTypeChanged();
61        }
62      }
63    }
64    [Storable]
65    protected T value;
66    public T Value {
67      get { return value; }
68      set {
69        if (value != this.value) {
70          DeregisterValueEvents();
71          this.value = value;
72          RegisterValueEvents();
73          OnValueChanged();
74        }
75      }
76    }
77    IItem IServiceParameter.Value {
78      get { return Value; }
79      set {
80        T val = value as T;
81        if ((value != null) && (val == null))
82          throw new InvalidOperationException(
83            string.Format("Type mismatch. Value is not a \"{0}\".",
84                          typeof(T).GetPrettyName())
85          );
86        Value = val;
87      }
88    }
89
90    [StorableConstructor]
91    protected ServiceParameter(bool deserializing) : base(deserializing) { }
92    protected ServiceParameter(ServiceParameter<T> original, Cloner cloner)
93      : base(original, cloner) {
94      this.type = original.type;
95      this.value = cloner.Clone(original.value);
96      RegisterValueEvents();
97    }
98    public ServiceParameter() : base("ServiceParameter") { }
99    public ServiceParameter(string name) : base(name) { }
100    public ServiceParameter(string name, string description) : base(name, description) { }
101
102    [StorableHook(HookType.AfterDeserialization)]
103    private void AfterDeserialization() {
104      RegisterValueEvents();
105    }
106
107    public override IDeepCloneable Clone(Cloner cloner) {
108      return new ServiceParameter<T>(this, cloner);
109    }
110
111    public override string ToString() {
112      var s = base.ToString();
113      if (Value != null)
114        s += ": " + Value.ToString();
115      return s;
116    }
117
118    public event EventHandler TypeChanged;
119    protected virtual void OnTypeChanged() {
120      var handler = TypeChanged;
121      if (handler != null) handler(this, EventArgs.Empty);
122      OnItemImageChanged();
123    }
124
125    public event EventHandler ValueChanged;
126    protected virtual void OnValueChanged() {
127      var handler = ValueChanged;
128      if (handler != null) handler(this, EventArgs.Empty);
129      OnToStringChanged();
130    }
131    protected virtual void RegisterValueEvents() {
132      if (value != null) {
133        value.ToStringChanged += new EventHandler(Value_ToStringChanged);
134      }
135    }
136    protected virtual void DeregisterValueEvents() {
137      if (value != null) {
138        value.ToStringChanged -= new EventHandler(Value_ToStringChanged);
139      }
140    }
141    private void Value_ToStringChanged(object sender, EventArgs e) {
142      OnToStringChanged();
143    }
144  }
145}
Note: See TracBrowser for help on using the repository browser.