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 |
|
---|
22 | using HeuristicLab.Common;
|
---|
23 | using HeuristicLab.Core;
|
---|
24 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
25 | using System;
|
---|
26 | using System.Drawing;
|
---|
27 |
|
---|
28 | namespace 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 == PortParameterType.Input) return HeuristicLab.Common.Resources.VSImageLibrary.ArrowDown;
|
---|
38 | else if (Type == PortParameterType.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 PortParameterType type;
|
---|
55 | public PortParameterType Type {
|
---|
56 | get { return type; }
|
---|
57 | set {
|
---|
58 | if (value != type) {
|
---|
59 | type = value;
|
---|
60 | OnTypeChanged();
|
---|
61 | }
|
---|
62 | }
|
---|
63 | }
|
---|
64 | [Storable]
|
---|
65 | protected T defaultValue;
|
---|
66 | public T DefaultValue {
|
---|
67 | get { return defaultValue; }
|
---|
68 | set {
|
---|
69 | if (value != this.defaultValue) {
|
---|
70 | DeregisterDefaultValueEvents();
|
---|
71 | this.defaultValue = value;
|
---|
72 | RegisterDefaultValueEvents();
|
---|
73 | OnDefaultValueChanged();
|
---|
74 | }
|
---|
75 | }
|
---|
76 | }
|
---|
77 | IItem IPortParameter.DefaultValue {
|
---|
78 | get { return DefaultValue; }
|
---|
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 | DefaultValue = val;
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | [StorableConstructor]
|
---|
91 | protected PortParameter(bool deserializing) : base(deserializing) { }
|
---|
92 | protected PortParameter(PortParameter<T> original, Cloner cloner)
|
---|
93 | : base(original, cloner) {
|
---|
94 | this.type = original.type;
|
---|
95 | this.defaultValue = cloner.Clone(original.defaultValue);
|
---|
96 | RegisterDefaultValueEvents();
|
---|
97 | }
|
---|
98 | public PortParameter() : base("PortParameter") { }
|
---|
99 | public PortParameter(string name) : base(name) { }
|
---|
100 | public PortParameter(string name, string description) : base(name, description) { }
|
---|
101 |
|
---|
102 | [StorableHook(HookType.AfterDeserialization)]
|
---|
103 | private void AfterDeserialization() {
|
---|
104 | RegisterDefaultValueEvents();
|
---|
105 | }
|
---|
106 |
|
---|
107 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
108 | return new PortParameter<T>(this, cloner);
|
---|
109 | }
|
---|
110 |
|
---|
111 | public override string ToString() {
|
---|
112 | var s = base.ToString();
|
---|
113 | if (DefaultValue != null)
|
---|
114 | s += ": " + DefaultValue.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 DefaultValueChanged;
|
---|
126 | protected virtual void OnDefaultValueChanged() {
|
---|
127 | var handler = DefaultValueChanged;
|
---|
128 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
129 | OnToStringChanged();
|
---|
130 | }
|
---|
131 |
|
---|
132 | public IMessageValue<T> CreateMessageValue() {
|
---|
133 | if (DefaultValue == null)
|
---|
134 | return new MessageValue<T>(Name);
|
---|
135 | else
|
---|
136 | return new MessageValue<T>(Name, (T)DefaultValue.Clone());
|
---|
137 | }
|
---|
138 | IMessageValue IPortParameter.CreateMessageValue() {
|
---|
139 | return CreateMessageValue();
|
---|
140 | }
|
---|
141 |
|
---|
142 | #region DefaultValue Events
|
---|
143 | protected virtual void RegisterDefaultValueEvents() {
|
---|
144 | if (defaultValue != null) {
|
---|
145 | defaultValue.ToStringChanged += new EventHandler(DefaultValue_ToStringChanged);
|
---|
146 | }
|
---|
147 | }
|
---|
148 | protected virtual void DeregisterDefaultValueEvents() {
|
---|
149 | if (defaultValue != null) {
|
---|
150 | defaultValue.ToStringChanged -= new EventHandler(DefaultValue_ToStringChanged);
|
---|
151 | }
|
---|
152 | }
|
---|
153 | private void DefaultValue_ToStringChanged(object sender, EventArgs e) {
|
---|
154 | OnToStringChanged();
|
---|
155 | }
|
---|
156 | #endregion
|
---|
157 | }
|
---|
158 | }
|
---|