Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceSpeedUp/HeuristicLab.Optimization/3.3/Result.cs @ 10851

Last change on this file since 10851 was 6760, checked in by epitzer, 13 years ago

#1530 integrate changes from trunk

File size: 5.0 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
[6760]44    public override string ItemDescription {
45      get {
46        if (Description != String.Empty) return Description;
47        else return base.ItemDescription;
48      }
49    }
50
[3226]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
[3317]64    [Storable]
[3226]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
[3341]76          DeregisterValueEvents();
[3226]77          this.value = value;
[3341]78          RegisterValueEvents();
[3226]79          OnValueChanged();
80        }
81      }
82    }
83
[4725]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    }
[3226]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;
[3317]111      Initialize();
[3226]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;
[3317]117      Initialize();
[3226]118    }
[4725]119
[4722]120    [StorableHook(HookType.AfterDeserialization)]
121    private void AfterDeserialization() {
122      Initialize();
123    }
[4725]124
[4722]125    public override IDeepCloneable Clone(Cloner cloner) {
126      return new Result(this, cloner);
127    }
[3226]128
[3317]129    private void Initialize() {
[3341]130      RegisterValueEvents();
[3317]131    }
132
[3226]133    public override string ToString() {
[3555]134      return string.Format("{0}: {1}", Name, Value == null ? "null" : Value.ToString());
[3226]135    }
136
137    public event EventHandler ValueChanged;
138    private void OnValueChanged() {
[4763]139      var handler = ValueChanged;
140      if (handler != null) handler(this, EventArgs.Empty);
[3341]141      OnItemImageChanged();
[3226]142      OnToStringChanged();
143    }
144
[3341]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    }
[3226]160    private void Value_ToStringChanged(object sender, EventArgs e) {
161      OnToStringChanged();
162    }
163  }
164}
Note: See TracBrowser for help on using the repository browser.