Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4722 was 4722, checked in by swagner, 12 years ago

Merged cloning refactoring branch back into trunk (#922)

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, 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    public Result()
78      : base("Anonymous") {
79      this.dataType = typeof(IItem);
80      this.value = null;
81    }
82    public Result(string name, Type dataType)
83      : base(name) {
84      this.dataType = dataType;
85      this.value = null;
86    }
87    public Result(string name, string description, Type dataType)
88      : base(name, description) {
89      this.dataType = dataType;
90      this.value = null;
91    }
92    public Result(string name, IItem value)
93      : base(name) {
94      this.dataType = value == null ? typeof(IItem) : value.GetType();
95      this.value = value;
96      Initialize();
97    }
98    public Result(string name, string description, IItem value)
99      : base(name, description) {
100      this.dataType = value == null ? typeof(IItem) : value.GetType();
101      this.value = value;
102      Initialize();
103    }
104    [StorableConstructor]
105    private Result(bool deserializing) : base(deserializing) { }
106    [StorableHook(HookType.AfterDeserialization)]
107    private void AfterDeserialization() {
108      Initialize();
109    }
110    private Result(Result original, Cloner cloner)
111      : base(original, cloner) {
112      value = cloner.Clone(original.value);
113      Initialize();
114    }
115    public override IDeepCloneable Clone(Cloner cloner) {
116      return new Result(this, cloner);
117    }
118
119    private void Initialize() {
120      RegisterValueEvents();
121    }
122
123    public override string ToString() {
124      return string.Format("{0}: {1}", Name, Value == null ? "null" : Value.ToString());
125    }
126
127    public event EventHandler ValueChanged;
128    private void OnValueChanged() {
129      if (ValueChanged != null)
130        ValueChanged(this, EventArgs.Empty);
131      OnItemImageChanged();
132      OnToStringChanged();
133    }
134
135    private void RegisterValueEvents() {
136      if (value != null) {
137        value.ItemImageChanged += new EventHandler(Value_ItemImageChanged);
138        value.ToStringChanged += new EventHandler(Value_ToStringChanged);
139      }
140    }
141    private void DeregisterValueEvents() {
142      if (value != null) {
143        value.ItemImageChanged -= new EventHandler(Value_ItemImageChanged);
144        value.ToStringChanged -= new EventHandler(Value_ToStringChanged);
145      }
146    }
147    private void Value_ItemImageChanged(object sender, EventArgs e) {
148      OnItemImageChanged();
149    }
150    private void Value_ToStringChanged(object sender, EventArgs e) {
151      OnToStringChanged();
152    }
153  }
154}
Note: See TracBrowser for help on using the repository browser.