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 either defined in the parameter itself or is retrieved from the scope.
|
---|
31 | /// </summary>
|
---|
32 | [Item("ValueLookupParameter<T>", "A parameter whose value is either defined in the parameter itself or is retrieved from or written to a scope.")]
|
---|
33 | [StorableClass]
|
---|
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 (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 | public ValueLookupParameter()
|
---|
69 | : base() {
|
---|
70 | }
|
---|
71 | public ValueLookupParameter(string name)
|
---|
72 | : base(name) {
|
---|
73 | }
|
---|
74 | public ValueLookupParameter(string name, T value)
|
---|
75 | : base(name) {
|
---|
76 | this.value = value;
|
---|
77 | Initialize();
|
---|
78 | }
|
---|
79 | public ValueLookupParameter(string name, string description)
|
---|
80 | : base(name, description) {
|
---|
81 | }
|
---|
82 | public ValueLookupParameter(string name, string description, T value)
|
---|
83 | : base(name, description) {
|
---|
84 | this.value = value;
|
---|
85 | Initialize();
|
---|
86 | }
|
---|
87 | public ValueLookupParameter(string name, string description, string actualName)
|
---|
88 | : base(name, description, actualName) {
|
---|
89 | }
|
---|
90 | [StorableConstructor]
|
---|
91 | protected ValueLookupParameter(bool deserializing) : base(deserializing) { }
|
---|
92 |
|
---|
93 | [StorableHook(HookType.AfterDeserialization)]
|
---|
94 | private void Initialize() {
|
---|
95 | RegisterValueEvents();
|
---|
96 | }
|
---|
97 |
|
---|
98 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
99 | ValueLookupParameter<T> clone = (ValueLookupParameter<T>)base.Clone(cloner);
|
---|
100 | clone.value = (T)cloner.Clone(value);
|
---|
101 | clone.Initialize();
|
---|
102 | return clone;
|
---|
103 | }
|
---|
104 |
|
---|
105 | public override string ToString() {
|
---|
106 | return string.Format("{0}: {1} ({2})", Name, Value != null ? Value.ToString() : ActualName, DataType.GetPrettyName());
|
---|
107 | }
|
---|
108 |
|
---|
109 | public event EventHandler ValueChanged;
|
---|
110 | private void OnValueChanged() {
|
---|
111 | if (ValueChanged != null)
|
---|
112 | ValueChanged(this, EventArgs.Empty);
|
---|
113 | OnItemImageChanged();
|
---|
114 | OnToStringChanged();
|
---|
115 | }
|
---|
116 |
|
---|
117 | private void RegisterValueEvents() {
|
---|
118 | if (value != null) {
|
---|
119 | value.ItemImageChanged += new EventHandler(Value_ItemImageChanged);
|
---|
120 | value.ToStringChanged += new EventHandler(Value_ToStringChanged);
|
---|
121 | }
|
---|
122 | }
|
---|
123 | private void DeregisterValueEvents() {
|
---|
124 | if (value != null) {
|
---|
125 | value.ItemImageChanged -= new EventHandler(Value_ItemImageChanged);
|
---|
126 | value.ToStringChanged -= new EventHandler(Value_ToStringChanged);
|
---|
127 | }
|
---|
128 | }
|
---|
129 | private void Value_ItemImageChanged(object sender, EventArgs e) {
|
---|
130 | OnItemImageChanged();
|
---|
131 | }
|
---|
132 | private void Value_ToStringChanged(object sender, EventArgs e) {
|
---|
133 | OnToStringChanged();
|
---|
134 | }
|
---|
135 | }
|
---|
136 | }
|
---|