1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 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 |
|
---|
22 | using System;
|
---|
23 | using System.Drawing;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HEAL.Attic;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Data {
|
---|
29 | [StorableType("06A7697C-2C25-49AF-95A2-DDB14D2F7B81")]
|
---|
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 | 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(StorableConstructorFlag _) : base(_) {
|
---|
54 | }
|
---|
55 | protected StringConvertibleValueTuple(StringConvertibleValueTuple<T, U> original, Cloner cloner)
|
---|
56 | : base(original, cloner) {
|
---|
57 | T item1 = (T)cloner.Clone(original.values.Item1);
|
---|
58 | U item2 = (U)cloner.Clone(original.values.Item2);
|
---|
59 | values = Tuple.Create<T, U>(item1, item2);
|
---|
60 | RegisterEventHandler();
|
---|
61 | }
|
---|
62 |
|
---|
63 | public StringConvertibleValueTuple(T item1, U item2)
|
---|
64 | : base() {
|
---|
65 | values = Tuple.Create<T, U>(item1, item2);
|
---|
66 | readOnly = false;
|
---|
67 | RegisterEventHandler();
|
---|
68 | }
|
---|
69 |
|
---|
70 | [StorableHook(HookType.AfterDeserialization)]
|
---|
71 | private void AfterDeserialization() {
|
---|
72 | RegisterEventHandler();
|
---|
73 | }
|
---|
74 |
|
---|
75 |
|
---|
76 | public abstract StringConvertibleValueTuple<T, U> AsReadOnly();
|
---|
77 |
|
---|
78 | public override string ToString() {
|
---|
79 | return string.Format("Item1: {0}, Item2: {1}", Item1, Item2);
|
---|
80 | }
|
---|
81 |
|
---|
82 | private void RegisterEventHandler() {
|
---|
83 | Item1.ValueChanged += Item_ValueChanged;
|
---|
84 | Item2.ValueChanged += Item_ValueChanged;
|
---|
85 | }
|
---|
86 |
|
---|
87 | private void Item_ValueChanged(object sender, EventArgs e) {
|
---|
88 | OnValueChanged();
|
---|
89 | }
|
---|
90 |
|
---|
91 | public event EventHandler ValueChanged;
|
---|
92 | protected virtual void OnValueChanged() {
|
---|
93 | if (ValueChanged != null)
|
---|
94 | ValueChanged(this, EventArgs.Empty);
|
---|
95 | OnToStringChanged();
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|