Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Data/3.3/StringConvertibleValueTuple.cs @ 7028

Last change on this file since 7028 was 5809, checked in by mkommend, 14 years ago

#1418: Reintegrated branch into trunk.

File size: 3.4 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.Data {
29  [StorableClass]
30  [Item("StringConvertibleValueTuple<,>", "A generic abstract base class for representing multiple values of a string convertible value.")]
31  public abstract class StringConvertibleValueTuple<T, U> : Item, IStringConvertibleValueTuple
32    where T : class, IDeepCloneable, IStringConvertibleValue
33    where U : class, IDeepCloneable, IStringConvertibleValue {
34    public override Image ItemImage {
35      get { return HeuristicLab.Common.Resources.VSImageLibrary.ValueType; }
36    }
37
38    [Storable]
39    protected bool readOnly;
40    public virtual bool ReadOnly {
41      get { return readOnly; }
42    }
43
44    [Storable(Name = "Values")]
45    protected Tuple<T, U> values;
46
47    protected T Item1 { get { return values.Item1; } }
48    IStringConvertibleValue IStringConvertibleValueTuple.Item1 { get { return Item1; } }
49    protected U Item2 { get { return values.Item2; } }
50    IStringConvertibleValue IStringConvertibleValueTuple.Item2 { get { return Item2; } }
51
52    [StorableConstructor]
53    protected StringConvertibleValueTuple(bool deserializing)
54      : base(deserializing) {
55    }
56    protected StringConvertibleValueTuple(StringConvertibleValueTuple<T, U> original, Cloner cloner)
57      : base(original, cloner) {
58      T item1 = (T)cloner.Clone(original.values.Item1);
59      U item2 = (U)cloner.Clone(original.values.Item2);
60      values = Tuple.Create<T, U>(item1, item2);
61      RegisterEventHandler();
62    }
63
64    public StringConvertibleValueTuple(T item1, U item2)
65      : base() {
66      values = Tuple.Create<T, U>(item1, item2);
67      readOnly = false;
68      RegisterEventHandler();
69    }
70
71    [StorableHook(HookType.AfterDeserialization)]
72    private void AfterDeserialization() {
73      RegisterEventHandler();
74    }
75
76
77    public abstract StringConvertibleValueTuple<T, U> AsReadOnly();
78
79    public override string ToString() {
80      return string.Format("Item1: {0}, Item2: {1}", Item1, Item2);
81    }
82
83    private void RegisterEventHandler() {
84      Item1.ValueChanged += Item_ValueChanged;
85      Item2.ValueChanged += Item_ValueChanged;
86    }
87
88    private void Item_ValueChanged(object sender, EventArgs e) {
89      OnValueChanged();
90    }
91
92    public event EventHandler ValueChanged;
93    protected virtual void OnValueChanged() {
94      if (ValueChanged != null)
95        ValueChanged(this, EventArgs.Empty);
96      OnToStringChanged();
97    }
98  }
99}
Note: See TracBrowser for help on using the repository browser.