Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OptimizationNetworks/HeuristicLab.Optimization.Networks/3.3/Ports/PortParameter.cs @ 11501

Last change on this file since 11501 was 11501, checked in by swagner, 8 years ago

#2205: Worked on optimization networks

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