Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization/3.3/Result.cs @ 3555

Last change on this file since 3555 was 3555, checked in by swagner, 14 years ago

Implemented reviewers' comments (#893)

File size: 4.6 KB
Line 
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
22using System;
23using System.Drawing;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Optimization {
29  /// <summary>
30  /// Represents a result which has a name and a data type and holds an IItem.
31  /// </summary>
32  [Item("Result", "A result which has a name and a data type and holds an IItem.")]
33  [StorableClass]
34  public sealed class Result : NamedItem, IResult {
35    public override Image ItemImage {
36      get {
37        if (value != null) return value.ItemImage;
38        else return base.ItemImage;
39      }
40    }
41
42    public override bool CanChangeName {
43      get { return false; }
44    }
45    public override bool CanChangeDescription {
46      get { return false; }
47    }
48
49    [Storable]
50    private Type dataType;
51    public Type DataType {
52      get { return dataType; }
53    }
54
55    [Storable]
56    private IItem value;
57    public IItem Value {
58      get { return value; }
59      set {
60        if (this.value != value) {
61          if ((value != null) && (!dataType.IsInstanceOfType(value)))
62            throw new ArgumentException(
63              string.Format("Type mismatch. Value is not a \"{0}\".",
64                            dataType.GetPrettyName())
65            );
66
67          DeregisterValueEvents();
68          this.value = value;
69          RegisterValueEvents();
70          OnValueChanged();
71        }
72      }
73    }
74
75    public Result()
76      : base("Anonymous") {
77      this.dataType = typeof(IItem);
78      this.value = null;
79    }
80    public Result(string name, Type dataType)
81      : base(name) {
82      this.dataType = dataType;
83      this.value = null;
84    }
85    public Result(string name, string description, Type dataType)
86      : base(name, description) {
87      this.dataType = dataType;
88      this.value = null;
89    }
90    public Result(string name, IItem value)
91      : base(name) {
92      this.dataType = value == null ? typeof(IItem) : value.GetType();
93      this.value = value;
94      Initialize();
95    }
96    public Result(string name, string description, IItem value)
97      : base(name, description) {
98      this.dataType = value == null ? typeof(IItem) : value.GetType();
99      this.value = value;
100      Initialize();
101    }
102    [StorableConstructor]
103    private Result(bool deserializing) : base(deserializing) { }
104
105    [StorableHook(HookType.AfterDeserialization)]
106    private void Initialize() {
107      RegisterValueEvents();
108    }
109
110    public override IDeepCloneable Clone(Cloner cloner) {
111      Result clone = new Result(Name, Description, DataType);
112      cloner.RegisterClonedObject(this, clone);
113      clone.value = (IItem)cloner.Clone(value);
114      clone.Initialize();
115      return clone;
116    }
117
118    public override string ToString() {
119      return string.Format("{0}: {1}", Name, Value == null ? "null" : Value.ToString());
120    }
121
122    public event EventHandler ValueChanged;
123    private void OnValueChanged() {
124      if (ValueChanged != null)
125        ValueChanged(this, EventArgs.Empty);
126      OnItemImageChanged();
127      OnToStringChanged();
128    }
129
130    private void RegisterValueEvents() {
131      if (value != null) {
132        value.ItemImageChanged += new EventHandler(Value_ItemImageChanged);
133        value.ToStringChanged += new EventHandler(Value_ToStringChanged);
134      }
135    }
136    private void DeregisterValueEvents() {
137      if (value != null) {
138        value.ItemImageChanged -= new EventHandler(Value_ItemImageChanged);
139        value.ToStringChanged -= new EventHandler(Value_ToStringChanged);
140      }
141    }
142    private void Value_ItemImageChanged(object sender, EventArgs e) {
143      OnItemImageChanged();
144    }
145    private void Value_ToStringChanged(object sender, EventArgs e) {
146      OnToStringChanged();
147    }
148  }
149}
Note: See TracBrowser for help on using the repository browser.