1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2010 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 System;
|
---|
23 | using System.Drawing;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Parameters {
|
---|
29 | /// <summary>
|
---|
30 | /// A parameter whose value is defined in the parameter itself or is null.
|
---|
31 | /// </summary>
|
---|
32 | [Item("OptionalValueParameter", "A parameter whose value is defined in the parameter itself or is null.")]
|
---|
33 | [StorableClass]
|
---|
34 | public class OptionalValueParameter<T> : Parameter, IValueParameter<T> where T : class, IItem {
|
---|
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 | private T value;
|
---|
44 | public virtual T Value {
|
---|
45 | get { return this.value; }
|
---|
46 | set {
|
---|
47 | if (value != this.value) {
|
---|
48 | OnValueChanging();
|
---|
49 | DeregisterValueEvents();
|
---|
50 | this.value = value;
|
---|
51 | RegisterValueEvents();
|
---|
52 | OnValueChanged();
|
---|
53 | }
|
---|
54 | }
|
---|
55 | }
|
---|
56 | IItem IValueParameter.Value {
|
---|
57 | get { return Value; }
|
---|
58 | set {
|
---|
59 | T val = value as T;
|
---|
60 | if ((value != null) && (val == null))
|
---|
61 | throw new InvalidOperationException(
|
---|
62 | string.Format("Type mismatch. Value is not a \"{0}\".",
|
---|
63 | typeof(T).GetPrettyName())
|
---|
64 | );
|
---|
65 | Value = val;
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | [Storable(DefaultValue = true)]
|
---|
70 | private bool getsCollected;
|
---|
71 | public bool GetsCollected {
|
---|
72 | get { return getsCollected; }
|
---|
73 | set {
|
---|
74 | if (value != getsCollected) {
|
---|
75 | getsCollected = value;
|
---|
76 | OnGetsCollectedChanged();
|
---|
77 | }
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | [Storable(DefaultValue = true)]
|
---|
82 | private bool reactOnValueToStringChangedAndValueItemImageChanged;
|
---|
83 | /// <summary>
|
---|
84 | /// True if this parameter should react on the ToStringChanged and ItemImageChanged events of its value, otherwise false.
|
---|
85 | /// </summary>
|
---|
86 | /// <remarks>
|
---|
87 | /// In some rare cases when the value of the parameter is not deeply cloned, this property has to be set to false
|
---|
88 | /// to avoid a memory leak (cf. ticket #1268). In all other cases this property should always be true.
|
---|
89 | /// </remarks>
|
---|
90 | public bool ReactOnValueToStringChangedAndValueItemImageChanged {
|
---|
91 | get { return reactOnValueToStringChangedAndValueItemImageChanged; }
|
---|
92 | set {
|
---|
93 | if (value != reactOnValueToStringChangedAndValueItemImageChanged) {
|
---|
94 | reactOnValueToStringChangedAndValueItemImageChanged = value;
|
---|
95 | if (reactOnValueToStringChangedAndValueItemImageChanged)
|
---|
96 | RegisterValueEvents();
|
---|
97 | else
|
---|
98 | DeregisterValueEvents();
|
---|
99 | }
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | #region Constructors
|
---|
104 | [StorableConstructor]
|
---|
105 | protected OptionalValueParameter(bool deserializing) : base(deserializing) { }
|
---|
106 | protected OptionalValueParameter(OptionalValueParameter<T> original, Cloner cloner)
|
---|
107 | : base(original, cloner) {
|
---|
108 | value = cloner.Clone(original.value);
|
---|
109 | getsCollected = original.getsCollected;
|
---|
110 | reactOnValueToStringChangedAndValueItemImageChanged = original.reactOnValueToStringChangedAndValueItemImageChanged;
|
---|
111 | RegisterValueEvents();
|
---|
112 | }
|
---|
113 | public OptionalValueParameter()
|
---|
114 | : base("Anonymous", typeof(T)) {
|
---|
115 | this.getsCollected = true;
|
---|
116 | this.reactOnValueToStringChangedAndValueItemImageChanged = true;
|
---|
117 | }
|
---|
118 | public OptionalValueParameter(string name)
|
---|
119 | : base(name, typeof(T)) {
|
---|
120 | this.getsCollected = true;
|
---|
121 | this.reactOnValueToStringChangedAndValueItemImageChanged = true;
|
---|
122 | }
|
---|
123 | public OptionalValueParameter(string name, bool getsCollected)
|
---|
124 | : base(name, typeof(T)) {
|
---|
125 | this.getsCollected = getsCollected;
|
---|
126 | this.reactOnValueToStringChangedAndValueItemImageChanged = true;
|
---|
127 | }
|
---|
128 | public OptionalValueParameter(string name, T value)
|
---|
129 | : base(name, typeof(T)) {
|
---|
130 | this.value = value;
|
---|
131 | this.getsCollected = true;
|
---|
132 | this.reactOnValueToStringChangedAndValueItemImageChanged = true;
|
---|
133 | RegisterValueEvents();
|
---|
134 | }
|
---|
135 | public OptionalValueParameter(string name, T value, bool getsCollected)
|
---|
136 | : base(name, typeof(T)) {
|
---|
137 | this.value = value;
|
---|
138 | this.getsCollected = getsCollected;
|
---|
139 | this.reactOnValueToStringChangedAndValueItemImageChanged = true;
|
---|
140 | RegisterValueEvents();
|
---|
141 | }
|
---|
142 | public OptionalValueParameter(string name, string description)
|
---|
143 | : base(name, description, typeof(T)) {
|
---|
144 | this.getsCollected = true;
|
---|
145 | this.reactOnValueToStringChangedAndValueItemImageChanged = true;
|
---|
146 | }
|
---|
147 | public OptionalValueParameter(string name, string description, bool getsCollected)
|
---|
148 | : base(name, description, typeof(T)) {
|
---|
149 | this.getsCollected = getsCollected;
|
---|
150 | this.reactOnValueToStringChangedAndValueItemImageChanged = true;
|
---|
151 | }
|
---|
152 | public OptionalValueParameter(string name, string description, T value)
|
---|
153 | : base(name, description, typeof(T)) {
|
---|
154 | this.value = value;
|
---|
155 | this.getsCollected = true;
|
---|
156 | this.reactOnValueToStringChangedAndValueItemImageChanged = true;
|
---|
157 | RegisterValueEvents();
|
---|
158 | }
|
---|
159 | public OptionalValueParameter(string name, string description, T value, bool getsCollected)
|
---|
160 | : base(name, description, typeof(T)) {
|
---|
161 | this.value = value;
|
---|
162 | this.getsCollected = getsCollected;
|
---|
163 | this.reactOnValueToStringChangedAndValueItemImageChanged = true;
|
---|
164 | RegisterValueEvents();
|
---|
165 | }
|
---|
166 | #endregion
|
---|
167 |
|
---|
168 | [StorableHook(HookType.AfterDeserialization)]
|
---|
169 | private void AfterDeserialization() {
|
---|
170 | RegisterValueEvents();
|
---|
171 | }
|
---|
172 |
|
---|
173 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
174 | return new OptionalValueParameter<T>(this, cloner);
|
---|
175 | }
|
---|
176 |
|
---|
177 | public override string ToString() {
|
---|
178 | if (reactOnValueToStringChangedAndValueItemImageChanged)
|
---|
179 | return Name + ": " + (Value != null ? Value.ToString() : "null");
|
---|
180 | else
|
---|
181 | return Name;
|
---|
182 | }
|
---|
183 |
|
---|
184 | protected override IItem GetActualValue() {
|
---|
185 | return Value;
|
---|
186 | }
|
---|
187 | protected override void SetActualValue(IItem value) {
|
---|
188 | ((IValueParameter)this).Value = value;
|
---|
189 | }
|
---|
190 |
|
---|
191 | public event EventHandler ValueChanging;
|
---|
192 | protected virtual void OnValueChanging() {
|
---|
193 | EventHandler handler = ValueChanging;
|
---|
194 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
195 | }
|
---|
196 | public event EventHandler ValueChanged;
|
---|
197 | protected virtual void OnValueChanged() {
|
---|
198 | EventHandler handler = ValueChanged;
|
---|
199 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
200 | OnItemImageChanged();
|
---|
201 | OnToStringChanged();
|
---|
202 | }
|
---|
203 | public event EventHandler GetsCollectedChanged;
|
---|
204 | protected virtual void OnGetsCollectedChanged() {
|
---|
205 | EventHandler handler = GetsCollectedChanged;
|
---|
206 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
207 | }
|
---|
208 |
|
---|
209 | private void RegisterValueEvents() {
|
---|
210 | if ((value != null) && reactOnValueToStringChangedAndValueItemImageChanged) {
|
---|
211 | value.ItemImageChanged += new EventHandler(Value_ItemImageChanged);
|
---|
212 | value.ToStringChanged += new EventHandler(Value_ToStringChanged);
|
---|
213 | }
|
---|
214 | }
|
---|
215 | private void DeregisterValueEvents() {
|
---|
216 | if (value != null) {
|
---|
217 | value.ItemImageChanged -= new EventHandler(Value_ItemImageChanged);
|
---|
218 | value.ToStringChanged -= new EventHandler(Value_ToStringChanged);
|
---|
219 | }
|
---|
220 | }
|
---|
221 | private void Value_ItemImageChanged(object sender, EventArgs e) {
|
---|
222 | OnItemImageChanged();
|
---|
223 | }
|
---|
224 | private void Value_ToStringChanged(object sender, EventArgs e) {
|
---|
225 | OnToStringChanged();
|
---|
226 | }
|
---|
227 | }
|
---|
228 | }
|
---|