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", "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 | if (Value != null)
|
---|
107 | return Name + ": " + Value.ToString();
|
---|
108 | else if (Name.Equals(ActualName))
|
---|
109 | return Name;
|
---|
110 | else
|
---|
111 | return Name + ": " + ActualName;
|
---|
112 | }
|
---|
113 |
|
---|
114 | public event EventHandler ValueChanged;
|
---|
115 | private void OnValueChanged() {
|
---|
116 | if (ValueChanged != null)
|
---|
117 | ValueChanged(this, EventArgs.Empty);
|
---|
118 | OnItemImageChanged();
|
---|
119 | OnToStringChanged();
|
---|
120 | }
|
---|
121 |
|
---|
122 | private void RegisterValueEvents() {
|
---|
123 | if (value != null) {
|
---|
124 | value.ItemImageChanged += new EventHandler(Value_ItemImageChanged);
|
---|
125 | value.ToStringChanged += new EventHandler(Value_ToStringChanged);
|
---|
126 | }
|
---|
127 | }
|
---|
128 | private void DeregisterValueEvents() {
|
---|
129 | if (value != null) {
|
---|
130 | value.ItemImageChanged -= new EventHandler(Value_ItemImageChanged);
|
---|
131 | value.ToStringChanged -= new EventHandler(Value_ToStringChanged);
|
---|
132 | }
|
---|
133 | }
|
---|
134 | private void Value_ItemImageChanged(object sender, EventArgs e) {
|
---|
135 | OnItemImageChanged();
|
---|
136 | }
|
---|
137 | private void Value_ToStringChanged(object sender, EventArgs e) {
|
---|
138 | OnToStringChanged();
|
---|
139 | }
|
---|
140 | }
|
---|
141 | }
|
---|