Free cookie consent management tool by TermsFeed Policy Generator

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

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

Updated year of copyrights (#1406)

File size: 4.8 KB
RevLine 
[3226]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3226]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;
[3341]23using System.Drawing;
[3226]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]
[4419]34  public sealed class Result : NamedItem, IResult, IStorableContent {
35    public string Filename { get; set; }
36
[3341]37    public override Image ItemImage {
38      get {
39        if (value != null) return value.ItemImage;
40        else return base.ItemImage;
41      }
42    }
43
[3226]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
[3317]57    [Storable]
[3226]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
[3341]69          DeregisterValueEvents();
[3226]70          this.value = value;
[3341]71          RegisterValueEvents();
[3226]72          OnValueChanged();
73        }
74      }
75    }
76
[4725]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    }
[3226]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;
[3317]104      Initialize();
[3226]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;
[3317]110      Initialize();
[3226]111    }
[4725]112
[4722]113    [StorableHook(HookType.AfterDeserialization)]
114    private void AfterDeserialization() {
115      Initialize();
116    }
[4725]117
[4722]118    public override IDeepCloneable Clone(Cloner cloner) {
119      return new Result(this, cloner);
120    }
[3226]121
[3317]122    private void Initialize() {
[3341]123      RegisterValueEvents();
[3317]124    }
125
[3226]126    public override string ToString() {
[3555]127      return string.Format("{0}: {1}", Name, Value == null ? "null" : Value.ToString());
[3226]128    }
129
130    public event EventHandler ValueChanged;
131    private void OnValueChanged() {
[4763]132      var handler = ValueChanged;
133      if (handler != null) handler(this, EventArgs.Empty);
[3341]134      OnItemImageChanged();
[3226]135      OnToStringChanged();
136    }
137
[3341]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    }
[3226]153    private void Value_ToStringChanged(object sender, EventArgs e) {
154      OnToStringChanged();
155    }
156  }
157}
Note: See TracBrowser for help on using the repository browser.