Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6664 was 6631, checked in by abeham, 13 years ago

#1054

  • Changed Result's ItemDescription to display its Description if one is defined, otherwise the description was not visible
File size: 5.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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, IStorableContent {
35    public string Filename { get; set; }
36
37    public override Image ItemImage {
38      get {
39        if (value != null) return value.ItemImage;
40        else return base.ItemImage;
41      }
42    }
43
44    public override string ItemDescription {
45      get {
46        if (Description != String.Empty) return Description;
47        else return base.ItemDescription;
48      }
49    }
50
51    public override bool CanChangeName {
52      get { return false; }
53    }
54    public override bool CanChangeDescription {
55      get { return false; }
56    }
57
58    [Storable]
59    private Type dataType;
60    public Type DataType {
61      get { return dataType; }
62    }
63
64    [Storable]
65    private IItem value;
66    public IItem Value {
67      get { return value; }
68      set {
69        if (this.value != value) {
70          if ((value != null) && (!dataType.IsInstanceOfType(value)))
71            throw new ArgumentException(
72              string.Format("Type mismatch. Value is not a \"{0}\".",
73                            dataType.GetPrettyName())
74            );
75
76          DeregisterValueEvents();
77          this.value = value;
78          RegisterValueEvents();
79          OnValueChanged();
80        }
81      }
82    }
83
84    [StorableConstructor]
85    private Result(bool deserializing) : base(deserializing) { }
86    private Result(Result original, Cloner cloner)
87      : base(original, cloner) {
88      dataType = original.dataType;
89      value = cloner.Clone(original.value);
90      Initialize();
91    }
92    public Result()
93      : base("Anonymous") {
94      this.dataType = typeof(IItem);
95      this.value = null;
96    }
97    public Result(string name, Type dataType)
98      : base(name) {
99      this.dataType = dataType;
100      this.value = null;
101    }
102    public Result(string name, string description, Type dataType)
103      : base(name, description) {
104      this.dataType = dataType;
105      this.value = null;
106    }
107    public Result(string name, IItem value)
108      : base(name) {
109      this.dataType = value == null ? typeof(IItem) : value.GetType();
110      this.value = value;
111      Initialize();
112    }
113    public Result(string name, string description, IItem value)
114      : base(name, description) {
115      this.dataType = value == null ? typeof(IItem) : value.GetType();
116      this.value = value;
117      Initialize();
118    }
119
120    [StorableHook(HookType.AfterDeserialization)]
121    private void AfterDeserialization() {
122      Initialize();
123    }
124
125    public override IDeepCloneable Clone(Cloner cloner) {
126      return new Result(this, cloner);
127    }
128
129    private void Initialize() {
130      RegisterValueEvents();
131    }
132
133    public override string ToString() {
134      return string.Format("{0}: {1}", Name, Value == null ? "null" : Value.ToString());
135    }
136
137    public event EventHandler ValueChanged;
138    private void OnValueChanged() {
139      var handler = ValueChanged;
140      if (handler != null) handler(this, EventArgs.Empty);
141      OnItemImageChanged();
142      OnToStringChanged();
143    }
144
145    private void RegisterValueEvents() {
146      if (value != null) {
147        value.ItemImageChanged += new EventHandler(Value_ItemImageChanged);
148        value.ToStringChanged += new EventHandler(Value_ToStringChanged);
149      }
150    }
151    private void DeregisterValueEvents() {
152      if (value != null) {
153        value.ItemImageChanged -= new EventHandler(Value_ItemImageChanged);
154        value.ToStringChanged -= new EventHandler(Value_ToStringChanged);
155      }
156    }
157    private void Value_ItemImageChanged(object sender, EventArgs e) {
158      OnItemImageChanged();
159    }
160    private void Value_ToStringChanged(object sender, EventArgs e) {
161      OnToStringChanged();
162    }
163  }
164}
Note: See TracBrowser for help on using the repository browser.