[5355] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 26 |
|
---|
| 27 | namespace HeuristicLab.Clients.OKB {
|
---|
| 28 | [Item("NamedItemWrapper", "A wrapper which wraps an INamedItem.")]
|
---|
| 29 | [StorableClass]
|
---|
| 30 | public class NamedItemWrapper<T> : ItemWrapper<T>, INamedItem where T : class, INamedItem {
|
---|
| 31 | protected override T WrappedItem {
|
---|
| 32 | get { return base.WrappedItem; }
|
---|
| 33 | set {
|
---|
| 34 | if (value == null) throw new ArgumentNullException("WrappedItem", "WrappedItem cannot be null.");
|
---|
| 35 | if (value != base.WrappedItem) {
|
---|
| 36 | CancelEventArgs<string> e = new CancelEventArgs<string>(value.Name);
|
---|
| 37 | OnNameChanging(e);
|
---|
| 38 | if (!e.Cancel) base.WrappedItem = value;
|
---|
| 39 | }
|
---|
| 40 | }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | public virtual string Name {
|
---|
| 44 | get { return WrappedItem.Name; }
|
---|
| 45 | set { WrappedItem.Name = value; }
|
---|
| 46 | }
|
---|
| 47 | public virtual bool CanChangeName {
|
---|
| 48 | get { return WrappedItem.CanChangeName; }
|
---|
| 49 | }
|
---|
| 50 | public virtual string Description {
|
---|
| 51 | get { return WrappedItem.Description; }
|
---|
| 52 | set { WrappedItem.Description = value; }
|
---|
| 53 | }
|
---|
| 54 | public virtual bool CanChangeDescription {
|
---|
| 55 | get { return WrappedItem.CanChangeDescription; }
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | [StorableConstructor]
|
---|
| 59 | protected NamedItemWrapper(bool deserializing) : base(deserializing) { }
|
---|
| 60 | protected NamedItemWrapper(NamedItemWrapper<T> original, Cloner cloner) : base(original, cloner) { }
|
---|
| 61 | public NamedItemWrapper(T item) : base(item) { }
|
---|
| 62 |
|
---|
| 63 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 64 | return new NamedItemWrapper<T>(this, cloner);
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | protected override void OnWrappedItemChanged() {
|
---|
| 68 | base.OnWrappedItemChanged();
|
---|
| 69 | OnNameChanged();
|
---|
| 70 | OnDescriptionChanged();
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | #region Events
|
---|
| 74 | public event EventHandler<CancelEventArgs<string>> NameChanging;
|
---|
| 75 | protected virtual void OnNameChanging(CancelEventArgs<string> e) {
|
---|
| 76 | var handler = NameChanging;
|
---|
| 77 | if (handler != null) handler(this, e);
|
---|
| 78 | }
|
---|
| 79 | public event EventHandler NameChanged;
|
---|
| 80 | protected virtual void OnNameChanged() {
|
---|
| 81 | var handler = NameChanged;
|
---|
| 82 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 83 | }
|
---|
| 84 | public event EventHandler DescriptionChanged;
|
---|
| 85 | protected virtual void OnDescriptionChanged() {
|
---|
| 86 | var handler = DescriptionChanged;
|
---|
| 87 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | protected override void RegisterWrappedItemEvents() {
|
---|
| 91 | base.RegisterWrappedItemEvents();
|
---|
| 92 | WrappedItem.NameChanging += new EventHandler<CancelEventArgs<string>>(WrappedItem_NameChanging);
|
---|
| 93 | WrappedItem.NameChanged += new EventHandler(WrappedItem_NameChanged);
|
---|
| 94 | WrappedItem.DescriptionChanged += new EventHandler(WrappedItem_DescriptionChanged);
|
---|
| 95 | }
|
---|
| 96 | protected override void DeregisterWrappedItemEvents() {
|
---|
| 97 | WrappedItem.NameChanging -= new EventHandler<CancelEventArgs<string>>(WrappedItem_NameChanging);
|
---|
| 98 | WrappedItem.NameChanged -= new EventHandler(WrappedItem_NameChanged);
|
---|
| 99 | WrappedItem.DescriptionChanged -= new EventHandler(WrappedItem_DescriptionChanged);
|
---|
| 100 | base.DeregisterWrappedItemEvents();
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | protected virtual void WrappedItem_NameChanging(object sender, CancelEventArgs<string> e) {
|
---|
| 104 | OnNameChanging(e);
|
---|
| 105 | }
|
---|
| 106 | protected virtual void WrappedItem_NameChanged(object sender, EventArgs e) {
|
---|
| 107 | OnNameChanged();
|
---|
| 108 | }
|
---|
| 109 | protected virtual void WrappedItem_DescriptionChanged(object sender, EventArgs e) {
|
---|
| 110 | OnDescriptionChanged();
|
---|
| 111 | }
|
---|
| 112 | #endregion
|
---|
| 113 | }
|
---|
| 114 | }
|
---|