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 | if (value != this.value) {
|
---|
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 = false)]
|
---|
70 | private bool readOnly;
|
---|
71 | public bool ReadOnly {
|
---|
72 | get { return readOnly; }
|
---|
73 | set {
|
---|
74 | if (value == readOnly) return;
|
---|
75 | readOnly = value;
|
---|
76 | OnReadOnlyChanged();
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | [Storable(DefaultValue = true)]
|
---|
81 | private bool getsCollected;
|
---|
82 | public bool GetsCollected {
|
---|
83 | get { return getsCollected; }
|
---|
84 | set {
|
---|
85 | if (value != getsCollected) {
|
---|
86 | getsCollected = value;
|
---|
87 | OnGetsCollectedChanged();
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | #region Constructors
|
---|
93 | [StorableConstructor]
|
---|
94 | protected ValueLookupParameter(StorableConstructorFlag _) : base(_) { }
|
---|
95 | protected ValueLookupParameter(ValueLookupParameter<T> original, Cloner cloner)
|
---|
96 | : base(original, cloner) {
|
---|
97 | value = cloner.Clone(original.value);
|
---|
98 | readOnly = original.readOnly;
|
---|
99 | getsCollected = original.getsCollected;
|
---|
100 | RegisterValueEvents();
|
---|
101 | }
|
---|
102 | public ValueLookupParameter()
|
---|
103 | : base() {
|
---|
104 | this.readOnly = false;
|
---|
105 | this.Hidden = false;
|
---|
106 | this.getsCollected = true;
|
---|
107 | }
|
---|
108 | public ValueLookupParameter(string name)
|
---|
109 | : base(name) {
|
---|
110 | this.readOnly = false;
|
---|
111 | this.Hidden = false;
|
---|
112 | this.getsCollected = true;
|
---|
113 | }
|
---|
114 | public ValueLookupParameter(string name, T value)
|
---|
115 | : base(name) {
|
---|
116 | this.value = value;
|
---|
117 | this.readOnly = false;
|
---|
118 | this.Hidden = false;
|
---|
119 | this.getsCollected = true;
|
---|
120 | RegisterValueEvents();
|
---|
121 | }
|
---|
122 | public ValueLookupParameter(string name, string description)
|
---|
123 | : base(name, description) {
|
---|
124 | this.readOnly = false;
|
---|
125 | this.Hidden = false;
|
---|
126 | this.getsCollected = true;
|
---|
127 | }
|
---|
128 | public ValueLookupParameter(string name, string description, T value)
|
---|
129 | : base(name, description) {
|
---|
130 | this.value = value;
|
---|
131 | this.readOnly = false;
|
---|
132 | this.Hidden = false;
|
---|
133 | this.getsCollected = true;
|
---|
134 | RegisterValueEvents();
|
---|
135 | }
|
---|
136 | public ValueLookupParameter(string name, string description, string actualName)
|
---|
137 | : base(name, description, actualName) {
|
---|
138 | this.readOnly = false;
|
---|
139 | this.Hidden = false;
|
---|
140 | this.getsCollected = true;
|
---|
141 | }
|
---|
142 | #endregion
|
---|
143 |
|
---|
144 | [StorableHook(HookType.AfterDeserialization)]
|
---|
145 | private void AfterDeserialization() {
|
---|
146 | RegisterValueEvents();
|
---|
147 | }
|
---|
148 |
|
---|
149 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
150 | return new ValueLookupParameter<T>(this, cloner);
|
---|
151 | }
|
---|
152 |
|
---|
153 | protected override IItem GetActualValue() {
|
---|
154 | if (ExecutionContext == null)
|
---|
155 | return Value;
|
---|
156 | return base.GetActualValue();
|
---|
157 | }
|
---|
158 |
|
---|
159 | public override string ToString() {
|
---|
160 | if (Value != null)
|
---|
161 | return Name + ": " + Value.ToString();
|
---|
162 | else if (Name.Equals(ActualName))
|
---|
163 | return Name;
|
---|
164 | else
|
---|
165 | return Name + ": " + ActualName;
|
---|
166 | }
|
---|
167 |
|
---|
168 | public event EventHandler ValueChanged;
|
---|
169 | protected virtual void OnValueChanged() {
|
---|
170 | EventHandler handler = ValueChanged;
|
---|
171 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
172 | OnItemImageChanged();
|
---|
173 | OnToStringChanged();
|
---|
174 | }
|
---|
175 | public event EventHandler ReadOnlyChanged;
|
---|
176 | protected virtual void OnReadOnlyChanged() {
|
---|
177 | EventHandler handler = ReadOnlyChanged;
|
---|
178 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
179 | }
|
---|
180 | public event EventHandler GetsCollectedChanged;
|
---|
181 | protected virtual void OnGetsCollectedChanged() {
|
---|
182 | EventHandler handler = GetsCollectedChanged;
|
---|
183 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
184 | }
|
---|
185 |
|
---|
186 | private void RegisterValueEvents() {
|
---|
187 | if (value != null) {
|
---|
188 | value.ItemImageChanged += new EventHandler(Value_ItemImageChanged);
|
---|
189 | value.ToStringChanged += new EventHandler(Value_ToStringChanged);
|
---|
190 | }
|
---|
191 | }
|
---|
192 | private void DeregisterValueEvents() {
|
---|
193 | if (value != null) {
|
---|
194 | value.ItemImageChanged -= new EventHandler(Value_ItemImageChanged);
|
---|
195 | value.ToStringChanged -= new EventHandler(Value_ToStringChanged);
|
---|
196 | }
|
---|
197 | }
|
---|
198 | private void Value_ItemImageChanged(object sender, EventArgs e) {
|
---|
199 | OnItemImageChanged();
|
---|
200 | }
|
---|
201 | private void Value_ToStringChanged(object sender, EventArgs e) {
|
---|
202 | OnToStringChanged();
|
---|
203 | }
|
---|
204 | }
|
---|
205 | }
|
---|