Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB (trunk integration)/HeuristicLab.Clients.OKB/3.3/RunCreation/ItemWrapper.cs @ 5639

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

Worked on OKB (#1174)

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