Free cookie consent management tool by TermsFeed Policy Generator

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

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

Updated year of copyrights (#1406)

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