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