source: branches/2994-AutoDiffForIntervals/HeuristicLab.Clients.OKB/3.3/RunCreation/ItemWrapper.cs @ 17209

Last change on this file since 17209 was 17209, checked in by gkronber, 4 months ago

#2994: merged r17132:17198 from trunk to branch

File size: 4.3 KB
Line 
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
22using System;
23using System.Drawing;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HEAL.Attic;
27using HeuristicLab.PluginInfrastructure;
28
29namespace HeuristicLab.Clients.OKB.RunCreation {
30  [Item("ItemWrapper", "A wrapper which wraps an IItem.")]
31  [StorableType("27F3E8E3-AC7D-415E-BF16-7E05EF6E1549")]
32  [NonDiscoverableType]
33  public class ItemWrapper<T> : IItem where T : class, IItem {
34    private T wrappedItem;
35    protected virtual T WrappedItem {
36      get { return wrappedItem; }
37      set {
38        if (value == null) throw new ArgumentNullException("WrappedItem", "WrappedItem cannot be null.");
39        if (value != wrappedItem) {
40          DeregisterWrappedItemEvents();
41          wrappedItem = value;
42          RegisterWrappedItemEvents();
43
44          OnToStringChanged();
45          OnItemImageChanged();
46          OnWrappedItemChanged();
47        }
48      }
49    }
50
51    public virtual string ItemName {
52      get { return wrappedItem.ItemName; }
53    }
54    public virtual string ItemDescription {
55      get { return wrappedItem.ItemDescription; }
56    }
57    public Version ItemVersion {
58      get { return wrappedItem.ItemVersion; }
59    }
60    public virtual Image ItemImage {
61      get { return WrappedItem.ItemImage; }
62    }
63
64    #region Persistence Properties
65    [Storable(Name = "WrappedItem")]
66    private T StorableWrappedItem {
67      get { return wrappedItem; }
68      set {
69        wrappedItem = value;
70        RegisterWrappedItemEvents();
71      }
72    }
73    #endregion
74
75    [StorableConstructor]
76    protected ItemWrapper(StorableConstructorFlag _) { }
77    protected ItemWrapper(ItemWrapper<T> original, Cloner cloner) {
78      cloner.RegisterClonedObject(original, this);
79      wrappedItem = cloner.Clone(original.wrappedItem);
80      RegisterWrappedItemEvents();
81    }
82    public ItemWrapper(T item) {
83      if (item == null) throw new ArgumentNullException("item", "ItemWrapper cannot wrap a null reference.");
84      wrappedItem = item;
85      RegisterWrappedItemEvents();
86    }
87
88    public object Clone() {
89      return Clone(new Cloner());
90    }
91    public virtual IDeepCloneable Clone(Cloner cloner) {
92      return new ItemWrapper<T>(this, cloner);
93    }
94
95    public override string ToString() {
96      return wrappedItem.ToString();
97    }
98
99    protected virtual void OnWrappedItemChanged() { }
100
101    #region Events
102    public event EventHandler ItemImageChanged;
103    protected virtual void OnItemImageChanged() {
104      EventHandler handler = ItemImageChanged;
105      if (handler != null) handler(this, EventArgs.Empty);
106    }
107    public event EventHandler ToStringChanged;
108    protected virtual void OnToStringChanged() {
109      EventHandler handler = ToStringChanged;
110      if (handler != null) handler(this, EventArgs.Empty);
111    }
112
113    protected virtual void RegisterWrappedItemEvents() {
114      WrappedItem.ToStringChanged += new EventHandler(WrappedItem_ToStringChanged);
115      WrappedItem.ItemImageChanged += new EventHandler(WrappedItem_ItemImageChanged);
116    }
117    protected virtual void DeregisterWrappedItemEvents() {
118      WrappedItem.ToStringChanged -= new EventHandler(WrappedItem_ToStringChanged);
119      WrappedItem.ItemImageChanged -= new EventHandler(WrappedItem_ItemImageChanged);
120    }
121
122    protected virtual void WrappedItem_ToStringChanged(object sender, EventArgs e) {
123      OnToStringChanged();
124    }
125    protected virtual void WrappedItem_ItemImageChanged(object sender, EventArgs e) {
126      OnItemImageChanged();
127    }
128    #endregion
129  }
130}
Note: See TracBrowser for help on using the repository browser.