1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 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 HEAL.Attic;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Parameters {
|
---|
29 | /// <summary>
|
---|
30 | /// A parameter whose value is either defined in the parameter itself or is retrieved from the scope.
|
---|
31 | /// </summary>
|
---|
32 | [Item("ValueLookupParameter", "A parameter whose value is either defined in the parameter itself or is retrieved from or written to a scope.")]
|
---|
33 | [StorableType("B34BB41A-CF50-4275-9BCD-1861EBA7C58F")]
|
---|
34 | public class ValueLookupParameter<T> : LookupParameter<T>, IValueLookupParameter<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 T Value {
|
---|
45 | get { return this.value; }
|
---|
46 | set {
|
---|
47 | if (ReadOnly) throw new InvalidOperationException("Cannot set the value of a readonly parameter.");
|
---|
48 | DoSetValue(value);
|
---|
49 | }
|
---|
50 | }
|
---|
51 | public virtual void ForceValue(T value) {
|
---|
52 | DoSetValue(value);
|
---|
53 | }
|
---|
54 | private void DoSetValue(T value) {
|
---|
55 | if (value != this.value) {
|
---|
56 | DeregisterValueEvents();
|
---|
57 | this.value = value;
|
---|
58 | RegisterValueEvents();
|
---|
59 | OnValueChanged();
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | IItem IValueParameter.Value {
|
---|
64 | get { return Value; }
|
---|
65 | set {
|
---|
66 | T val = value as T;
|
---|
67 | if ((value != null) && (val == null))
|
---|
68 | throw new InvalidOperationException(
|
---|
69 | string.Format("Type mismatch. Value is not a \"{0}\".",
|
---|
70 | typeof(T).GetPrettyName())
|
---|
71 | );
|
---|
72 | Value = val;
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | [Storable(DefaultValue = false)]
|
---|
77 | private bool readOnly;
|
---|
78 | public bool ReadOnly {
|
---|
79 | get { return readOnly; }
|
---|
80 | set {
|
---|
81 | if (value == readOnly) return;
|
---|
82 | readOnly = value;
|
---|
83 | OnReadOnlyChanged();
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | [Storable(DefaultValue = true)]
|
---|
88 | private bool getsCollected;
|
---|
89 | public bool GetsCollected {
|
---|
90 | get { return getsCollected; }
|
---|
91 | set {
|
---|
92 | if (value != getsCollected) {
|
---|
93 | getsCollected = value;
|
---|
94 | OnGetsCollectedChanged();
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | #region Constructors
|
---|
100 | [StorableConstructor]
|
---|
101 | protected ValueLookupParameter(StorableConstructorFlag _) : base(_) { }
|
---|
102 | protected ValueLookupParameter(ValueLookupParameter<T> original, Cloner cloner)
|
---|
103 | : base(original, cloner) {
|
---|
104 | value = cloner.Clone(original.value);
|
---|
105 | readOnly = original.readOnly;
|
---|
106 | getsCollected = original.getsCollected;
|
---|
107 | RegisterValueEvents();
|
---|
108 | }
|
---|
109 | public ValueLookupParameter()
|
---|
110 | : base() {
|
---|
111 | this.readOnly = false;
|
---|
112 | this.Hidden = false;
|
---|
113 | this.getsCollected = true;
|
---|
114 | }
|
---|
115 | public ValueLookupParameter(string name)
|
---|
116 | : base(name) {
|
---|
117 | this.readOnly = false;
|
---|
118 | this.Hidden = false;
|
---|
119 | this.getsCollected = true;
|
---|
120 | }
|
---|
121 | public ValueLookupParameter(string name, T value)
|
---|
122 | : base(name) {
|
---|
123 | this.value = value;
|
---|
124 | this.readOnly = false;
|
---|
125 | this.Hidden = false;
|
---|
126 | this.getsCollected = true;
|
---|
127 | RegisterValueEvents();
|
---|
128 | }
|
---|
129 | public ValueLookupParameter(string name, string description)
|
---|
130 | : base(name, description) {
|
---|
131 | this.readOnly = false;
|
---|
132 | this.Hidden = false;
|
---|
133 | this.getsCollected = true;
|
---|
134 | }
|
---|
135 | public ValueLookupParameter(string name, string description, T value)
|
---|
136 | : base(name, description) {
|
---|
137 | this.value = value;
|
---|
138 | this.readOnly = false;
|
---|
139 | this.Hidden = false;
|
---|
140 | this.getsCollected = true;
|
---|
141 | RegisterValueEvents();
|
---|
142 | }
|
---|
143 | public ValueLookupParameter(string name, string description, string actualName)
|
---|
144 | : base(name, description, actualName) {
|
---|
145 | this.readOnly = false;
|
---|
146 | this.Hidden = false;
|
---|
147 | this.getsCollected = true;
|
---|
148 | }
|
---|
149 | #endregion
|
---|
150 |
|
---|
151 | [StorableHook(HookType.AfterDeserialization)]
|
---|
152 | private void AfterDeserialization() {
|
---|
153 | RegisterValueEvents();
|
---|
154 | }
|
---|
155 |
|
---|
156 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
157 | return new ValueLookupParameter<T>(this, cloner);
|
---|
158 | }
|
---|
159 |
|
---|
160 | public override string ToString() {
|
---|
161 | if (Value != null)
|
---|
162 | return Name + ": " + Value.ToString();
|
---|
163 | else if (Name.Equals(ActualName))
|
---|
164 | return Name;
|
---|
165 | else
|
---|
166 | return Name + ": " + ActualName;
|
---|
167 | }
|
---|
168 |
|
---|
169 | public event EventHandler ValueChanged;
|
---|
170 | protected virtual void OnValueChanged() {
|
---|
171 | EventHandler handler = ValueChanged;
|
---|
172 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
173 | OnItemImageChanged();
|
---|
174 | OnToStringChanged();
|
---|
175 | }
|
---|
176 | public event EventHandler ReadOnlyChanged;
|
---|
177 | protected virtual void OnReadOnlyChanged() {
|
---|
178 | EventHandler handler = ReadOnlyChanged;
|
---|
179 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
180 | }
|
---|
181 | public event EventHandler GetsCollectedChanged;
|
---|
182 | protected virtual void OnGetsCollectedChanged() {
|
---|
183 | EventHandler handler = GetsCollectedChanged;
|
---|
184 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
185 | }
|
---|
186 |
|
---|
187 | private void RegisterValueEvents() {
|
---|
188 | if (value != null) {
|
---|
189 | value.ItemImageChanged += new EventHandler(Value_ItemImageChanged);
|
---|
190 | value.ToStringChanged += new EventHandler(Value_ToStringChanged);
|
---|
191 | }
|
---|
192 | }
|
---|
193 | private void DeregisterValueEvents() {
|
---|
194 | if (value != null) {
|
---|
195 | value.ItemImageChanged -= new EventHandler(Value_ItemImageChanged);
|
---|
196 | value.ToStringChanged -= new EventHandler(Value_ToStringChanged);
|
---|
197 | }
|
---|
198 | }
|
---|
199 | private void Value_ItemImageChanged(object sender, EventArgs e) {
|
---|
200 | OnItemImageChanged();
|
---|
201 | }
|
---|
202 | private void Value_ToStringChanged(object sender, EventArgs e) {
|
---|
203 | OnToStringChanged();
|
---|
204 | }
|
---|
205 | }
|
---|
206 | }
|
---|