Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Data/3.3/StringConvertibleValueTuple.cs @ 13656

Last change on this file since 13656 was 13656, checked in by ascheibe, 8 years ago

#2582 created branch for Hive Web Job Manager

File size: 3.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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 static new Image StaticItemImage
35    {
36      get { return new Bitmap(25, 25); }
37    }
38
39    [Storable]
40    protected bool readOnly;
41    public virtual bool ReadOnly
42    {
43      get { return readOnly; }
44    }
45
46    [Storable(Name = "Values")]
47    protected Tuple<T, U> values;
48
49    protected T Item1 { get { return values.Item1; } }
50    IStringConvertibleValue IStringConvertibleValueTuple.Item1 { get { return Item1; } }
51    protected U Item2 { get { return values.Item2; } }
52    IStringConvertibleValue IStringConvertibleValueTuple.Item2 { get { return Item2; } }
53
54    [StorableConstructor]
55    protected StringConvertibleValueTuple(bool deserializing)
56      : base(deserializing) {
57    }
58    protected StringConvertibleValueTuple(StringConvertibleValueTuple<T, U> original, Cloner cloner)
59      : base(original, cloner) {
60      T item1 = (T)cloner.Clone(original.values.Item1);
61      U item2 = (U)cloner.Clone(original.values.Item2);
62      values = Tuple.Create<T, U>(item1, item2);
63      RegisterEventHandler();
64    }
65
66    public StringConvertibleValueTuple(T item1, U item2)
67      : base() {
68      values = Tuple.Create<T, U>(item1, item2);
69      readOnly = false;
70      RegisterEventHandler();
71    }
72
73    [StorableHook(HookType.AfterDeserialization)]
74    private void AfterDeserialization() {
75      RegisterEventHandler();
76    }
77
78
79    public abstract StringConvertibleValueTuple<T, U> AsReadOnly();
80
81    public override string ToString() {
82      return string.Format("Item1: {0}, Item2: {1}", Item1, Item2);
83    }
84
85    private void RegisterEventHandler() {
86      Item1.ValueChanged += Item_ValueChanged;
87      Item2.ValueChanged += Item_ValueChanged;
88    }
89
90    private void Item_ValueChanged(object sender, EventArgs e) {
91      OnValueChanged();
92    }
93
94    public event EventHandler ValueChanged;
95    protected virtual void OnValueChanged() {
96      if (ValueChanged != null)
97        ValueChanged(this, EventArgs.Empty);
98      OnToStringChanged();
99    }
100  }
101}
Note: See TracBrowser for help on using the repository browser.