Free cookie consent management tool by TermsFeed Policy Generator

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

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

Implemented reviewers' comments (#893).

File size: 4.7 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.ReadOnlyView = ReadOnlyView;
114      clone.value = (IItem)cloner.Clone(value);
115      clone.Initialize();
116      return clone;
117    }
118
119    public override string ToString() {
120      return string.Format("{0}: {1} ({2})", Name, Value == null ? "null" : Value.ToString(), DataType.GetPrettyName());
121    }
122
123    public event EventHandler ValueChanged;
124    private void OnValueChanged() {
125      if (ValueChanged != null)
126        ValueChanged(this, EventArgs.Empty);
127      OnItemImageChanged();
128      OnToStringChanged();
129    }
130
131    private void RegisterValueEvents() {
132      if (value != null) {
133        value.ItemImageChanged += new EventHandler(Value_ItemImageChanged);
134        value.ToStringChanged += new EventHandler(Value_ToStringChanged);
135      }
136    }
137    private void DeregisterValueEvents() {
138      if (value != null) {
139        value.ItemImageChanged -= new EventHandler(Value_ItemImageChanged);
140        value.ToStringChanged -= new EventHandler(Value_ToStringChanged);
141      }
142    }
143    private void Value_ItemImageChanged(object sender, EventArgs e) {
144      OnItemImageChanged();
145    }
146    private void Value_ToStringChanged(object sender, EventArgs e) {
147      OnToStringChanged();
148    }
149  }
150}
Note: See TracBrowser for help on using the repository browser.