[2653] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[2790] | 3 | * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2653] | 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.Collections.Generic;
|
---|
[2790] | 24 | using System.Drawing;
|
---|
[2834] | 25 | using System.Linq;
|
---|
[2653] | 26 | using HeuristicLab.Collections;
|
---|
| 27 | using HeuristicLab.Common;
|
---|
[2790] | 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2653] | 29 |
|
---|
| 30 | namespace HeuristicLab.Core {
|
---|
[2790] | 31 | [Item("NamedItemCollection<T>", "Represents a collection of named items.")]
|
---|
[3017] | 32 | [StorableClass]
|
---|
[2790] | 33 | public class NamedItemCollection<T> : ObservableKeyedCollection<string, T>, IItem where T : class, INamedItem {
|
---|
| 34 | public virtual string ItemName {
|
---|
| 35 | get { return ItemAttribute.GetName(this.GetType()); }
|
---|
| 36 | }
|
---|
| 37 | public virtual string ItemDescription {
|
---|
| 38 | get { return ItemAttribute.GetDescription(this.GetType()); }
|
---|
| 39 | }
|
---|
| 40 | public virtual Image ItemImage {
|
---|
| 41 | get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Class; }
|
---|
| 42 | }
|
---|
| 43 |
|
---|
[2653] | 44 | [Storable(Name = "RestoreEvents")]
|
---|
| 45 | private object RestoreEvents {
|
---|
| 46 | get { return null; }
|
---|
[2830] | 47 | set { RegisterItemEvents(this); }
|
---|
[2653] | 48 | }
|
---|
| 49 |
|
---|
| 50 | public NamedItemCollection() : base() { }
|
---|
| 51 | public NamedItemCollection(int capacity) : base(capacity) { }
|
---|
[2830] | 52 | public NamedItemCollection(IEnumerable<T> collection) : base(collection) {
|
---|
| 53 | RegisterItemEvents(this);
|
---|
| 54 | }
|
---|
[2653] | 55 |
|
---|
| 56 | public object Clone() {
|
---|
| 57 | return Clone(new Cloner());
|
---|
| 58 | }
|
---|
[2830] | 59 | public virtual IDeepCloneable Clone(Cloner cloner) {
|
---|
| 60 | NamedItemCollection<T> clone = (NamedItemCollection<T>)Activator.CreateInstance(this.GetType(), this.Select(x => (T)cloner.Clone(x)));
|
---|
[2653] | 61 | cloner.RegisterClonedObject(this, clone);
|
---|
| 62 | return clone;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[2790] | 65 | public override string ToString() {
|
---|
| 66 | return ItemName;
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[2932] | 69 | public event EventHandler ToStringChanged;
|
---|
| 70 | protected virtual void OnToStringChanged() {
|
---|
| 71 | if (ToStringChanged != null)
|
---|
| 72 | ToStringChanged(this, EventArgs.Empty);
|
---|
[2653] | 73 | }
|
---|
| 74 |
|
---|
| 75 | protected override string GetKeyForItem(T item) {
|
---|
| 76 | return item.Name;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | protected override void OnItemsAdded(IEnumerable<T> items) {
|
---|
[2830] | 80 | RegisterItemEvents(items);
|
---|
[2653] | 81 | base.OnItemsAdded(items);
|
---|
| 82 | }
|
---|
| 83 | protected override void OnItemsRemoved(IEnumerable<T> items) {
|
---|
[2830] | 84 | DeregisterItemEvents(items);
|
---|
[2653] | 85 | base.OnItemsRemoved(items);
|
---|
| 86 | }
|
---|
| 87 | #region NOTE
|
---|
| 88 | // NOTE: OnItemsReplaced is not overridden as ItemsReplaced is only fired
|
---|
[2676] | 89 | // by ObservableKeyedCollection when the key of an item has changed. The items stays
|
---|
[2653] | 90 | // in the collection and therefore the NameChanging, NameChanged and Changed event handler
|
---|
| 91 | // do not have to be removed and added again.
|
---|
| 92 | #endregion
|
---|
| 93 | protected override void OnCollectionReset(IEnumerable<T> items, IEnumerable<T> oldItems) {
|
---|
[2830] | 94 | DeregisterItemEvents(oldItems);
|
---|
| 95 | RegisterItemEvents(items);
|
---|
| 96 | base.OnCollectionReset(items, oldItems);
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | private void RegisterItemEvents(IEnumerable<T> items) {
|
---|
[2653] | 100 | foreach (T item in items) {
|
---|
[2833] | 101 | if (item != null) {
|
---|
| 102 | item.NameChanging += new EventHandler<CancelEventArgs<string>>(Item_NameChanging);
|
---|
| 103 | item.NameChanged += new EventHandler(Item_NameChanged);
|
---|
| 104 | }
|
---|
[2653] | 105 | }
|
---|
| 106 | }
|
---|
[2830] | 107 | private void DeregisterItemEvents(IEnumerable<T> items) {
|
---|
| 108 | foreach (T item in items) {
|
---|
[2833] | 109 | if (item != null) {
|
---|
| 110 | item.NameChanging -= new EventHandler<CancelEventArgs<string>>(Item_NameChanging);
|
---|
| 111 | item.NameChanged -= new EventHandler(Item_NameChanged);
|
---|
| 112 | }
|
---|
[2830] | 113 | }
|
---|
[2653] | 114 | }
|
---|
| 115 |
|
---|
| 116 | private void Item_NameChanging(object sender, CancelEventArgs<string> e) {
|
---|
[2669] | 117 | e.Cancel = e.Cancel || this.ContainsKey(e.Value);
|
---|
[2653] | 118 | }
|
---|
| 119 | private void Item_NameChanged(object sender, EventArgs e) {
|
---|
| 120 | T item = (T)sender;
|
---|
| 121 | UpdateItemKey(item);
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 | }
|
---|