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 | DeregisterValueEvents();
|
---|
49 | this.value = value;
|
---|
50 | RegisterValueEvents();
|
---|
51 | OnValueChanged();
|
---|
52 | }
|
---|
53 | }
|
---|
54 | }
|
---|
55 | IItem IValueParameter.Value {
|
---|
56 | get { return Value; }
|
---|
57 | set {
|
---|
58 | T val = value as T;
|
---|
59 | if ((value != null) && (val == null))
|
---|
60 | throw new InvalidOperationException(
|
---|
61 | string.Format("Type mismatch. Value is not a \"{0}\".",
|
---|
62 | typeof(T).GetPrettyName())
|
---|
63 | );
|
---|
64 | Value = val;
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | [Storable(DefaultValue = true)]
|
---|
69 | private bool getsCollected;
|
---|
70 | public bool GetsCollected {
|
---|
71 | get { return getsCollected; }
|
---|
72 | set {
|
---|
73 | if (value != getsCollected) {
|
---|
74 | getsCollected = value;
|
---|
75 | OnGetsCollectedChanged();
|
---|
76 | }
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | #region Constructors
|
---|
81 | public OptionalValueParameter()
|
---|
82 | : base("Anonymous", typeof(T)) {
|
---|
83 | this.getsCollected = true;
|
---|
84 | }
|
---|
85 | public OptionalValueParameter(string name)
|
---|
86 | : base(name, typeof(T)) {
|
---|
87 | this.getsCollected = true;
|
---|
88 | }
|
---|
89 | public OptionalValueParameter(string name, bool getsCollected)
|
---|
90 | : base(name, typeof(T)) {
|
---|
91 | this.getsCollected = getsCollected;
|
---|
92 | }
|
---|
93 | public OptionalValueParameter(string name, T value)
|
---|
94 | : base(name, typeof(T)) {
|
---|
95 | this.value = value;
|
---|
96 | this.getsCollected = true;
|
---|
97 | Initialize();
|
---|
98 | }
|
---|
99 | public OptionalValueParameter(string name, T value, bool getsCollected)
|
---|
100 | : base(name, typeof(T)) {
|
---|
101 | this.value = value;
|
---|
102 | this.getsCollected = getsCollected;
|
---|
103 | Initialize();
|
---|
104 | }
|
---|
105 | public OptionalValueParameter(string name, string description)
|
---|
106 | : base(name, description, typeof(T)) {
|
---|
107 | this.getsCollected = true;
|
---|
108 | }
|
---|
109 | public OptionalValueParameter(string name, string description, bool getsCollected)
|
---|
110 | : base(name, description, typeof(T)) {
|
---|
111 | this.getsCollected = getsCollected;
|
---|
112 | }
|
---|
113 | public OptionalValueParameter(string name, string description, T value)
|
---|
114 | : base(name, description, typeof(T)) {
|
---|
115 | this.value = value;
|
---|
116 | this.getsCollected = true;
|
---|
117 | Initialize();
|
---|
118 | }
|
---|
119 | public OptionalValueParameter(string name, string description, T value, bool getsCollected)
|
---|
120 | : base(name, description, typeof(T)) {
|
---|
121 | this.value = value;
|
---|
122 | this.getsCollected = getsCollected;
|
---|
123 | Initialize();
|
---|
124 | }
|
---|
125 | [StorableConstructor]
|
---|
126 | protected OptionalValueParameter(bool deserializing) : base(deserializing) { }
|
---|
127 | #endregion
|
---|
128 |
|
---|
129 | [StorableHook(HookType.AfterDeserialization)]
|
---|
130 | private void Initialize() {
|
---|
131 | RegisterValueEvents();
|
---|
132 | }
|
---|
133 |
|
---|
134 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
135 | OptionalValueParameter<T> clone = (OptionalValueParameter<T>)base.Clone(cloner);
|
---|
136 | clone.value = (T)cloner.Clone(value);
|
---|
137 | clone.getsCollected = getsCollected;
|
---|
138 | clone.Initialize();
|
---|
139 | return clone;
|
---|
140 | }
|
---|
141 |
|
---|
142 | public override string ToString() {
|
---|
143 | return Name + ": " + (Value != null ? Value.ToString() : "null");
|
---|
144 | }
|
---|
145 |
|
---|
146 | protected override IItem GetActualValue() {
|
---|
147 | return Value;
|
---|
148 | }
|
---|
149 | protected override void SetActualValue(IItem value) {
|
---|
150 | ((IValueParameter)this).Value = value;
|
---|
151 | }
|
---|
152 |
|
---|
153 | public event EventHandler ValueChanged;
|
---|
154 | protected virtual void OnValueChanged() {
|
---|
155 | EventHandler handler = ValueChanged;
|
---|
156 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
157 | OnItemImageChanged();
|
---|
158 | OnToStringChanged();
|
---|
159 | }
|
---|
160 | public event EventHandler GetsCollectedChanged;
|
---|
161 | protected virtual void OnGetsCollectedChanged() {
|
---|
162 | EventHandler handler = GetsCollectedChanged;
|
---|
163 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
164 | }
|
---|
165 |
|
---|
166 | private void RegisterValueEvents() {
|
---|
167 | if (value != null) {
|
---|
168 | value.ItemImageChanged += new EventHandler(Value_ItemImageChanged);
|
---|
169 | value.ToStringChanged += new EventHandler(Value_ToStringChanged);
|
---|
170 | }
|
---|
171 | }
|
---|
172 | private void DeregisterValueEvents() {
|
---|
173 | if (value != null) {
|
---|
174 | value.ItemImageChanged -= new EventHandler(Value_ItemImageChanged);
|
---|
175 | value.ToStringChanged -= new EventHandler(Value_ToStringChanged);
|
---|
176 | }
|
---|
177 | }
|
---|
178 | private void Value_ItemImageChanged(object sender, EventArgs e) {
|
---|
179 | OnItemImageChanged();
|
---|
180 | }
|
---|
181 | private void Value_ToStringChanged(object sender, EventArgs e) {
|
---|
182 | OnToStringChanged();
|
---|
183 | }
|
---|
184 | }
|
---|
185 | }
|
---|