Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Data/ItemList.cs @ 22

Last change on this file since 22 was 2, checked in by swagner, 17 years ago

Added HeuristicLab 3.0 sources from former SVN repository at revision 52

File size: 6.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Collections;
24using System.Collections.Generic;
25using System.Text;
26using System.Xml;
27using HeuristicLab.Core;
28
29namespace HeuristicLab.Data {
30  public class ItemList : ItemBase, IList<IItem> {
31    private List<IItem> list;
32
33    private Type myItemType;
34    public Type ItemType {
35      get { return myItemType; }
36      set {
37        if (value == null) value = typeof(IItem);
38        if (value != myItemType) {
39          if (!typeof(IItem).IsAssignableFrom(value))
40            throw new InvalidOperationException("Cannot set item type. Only types compatible to IItem are allowed.");
41          foreach (IItem item in list) {
42            if (!value.IsAssignableFrom(item.GetType()))
43              throw new InvalidOperationException("Cannot set item type. Items are incompatible.");
44          }
45          myItemType = value;
46          OnItemTypeChanged();
47        }
48      }
49    }
50
51    public ItemList() {
52      list = new List<IItem>();
53      myItemType = typeof(IItem);
54    }
55
56    public override IView CreateView() {
57      return new ItemListView(this);
58    }
59
60    public override object Clone(IDictionary<Guid, object> clonedObjects) {
61      ItemList clone = new ItemList();
62      clonedObjects.Add(Guid, clone);
63      clone.myItemType = ItemType;
64      for (int i = 0; i < list.Count; i++)
65        clone.list.Add((IItem)Auxiliary.Clone(list[i], clonedObjects));
66      return clone;
67    }
68
69    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
70      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
71      if (ItemType != null) {
72        XmlAttribute itemTypeAttribute = document.CreateAttribute("ItemType");
73        string typeString = ItemType.AssemblyQualifiedName;
74        string[] tokens = typeString.Split(new string[] { ", " }, StringSplitOptions.None);
75        typeString = tokens[0] + ", " + tokens[1];
76        itemTypeAttribute.Value = typeString;
77        node.Attributes.Append(itemTypeAttribute);
78      }
79      for (int i = 0; i < list.Count; i++)
80        node.AppendChild(PersistenceManager.Persist(list[i], document, persistedObjects));
81      return node;
82    }
83    public override void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
84      base.Populate(node, restoredObjects);
85      XmlAttribute itemTypeAttribute = node.Attributes["ItemType"];
86      if (itemTypeAttribute != null)
87        myItemType = Type.GetType(itemTypeAttribute.Value);
88      for (int i = 0; i < node.ChildNodes.Count; i++)
89        list.Add((IItem)PersistenceManager.Restore(node.ChildNodes[i], restoredObjects));
90    }
91
92    public override string ToString() {
93      if (list.Count > 0) {
94        StringBuilder builder = new StringBuilder();
95        builder.Append(list[0].ToString());
96        for (int i = 1; i < list.Count; i++) {
97          builder.Append(";");
98          builder.Append(list[i].ToString());
99        }
100        return builder.ToString();
101      } else {
102        return "Empty List";
103      }
104    }
105
106    #region IList<IItem> Members
107    public int IndexOf(IItem item) {
108      return list.IndexOf(item);
109    }
110    public void Insert(int index, IItem item) {
111      if ((ItemType != null) && (!ItemType.IsAssignableFrom(item.GetType())))
112        throw new InvalidOperationException("item type is incompatible to required item type");
113      list.Insert(index, item);
114      OnItemAdded(item, index);
115    }
116    public void RemoveAt(int index) {
117      IItem item = list[index];
118      list.RemoveAt(index);
119      OnItemRemoved(item, index);
120    }
121    public IItem this[int index] {
122      get { return list[index]; }
123      set {
124        if ((ItemType != null) && (!ItemType.IsAssignableFrom(value.GetType())))
125          throw new InvalidOperationException("item type is incompatible to required item type");
126        list[index] = value;
127      }
128    }
129    #endregion
130
131    #region ICollection<IItem> Members
132    public void Add(IItem item) {
133      if ((ItemType != null) && (!ItemType.IsAssignableFrom(item.GetType())))
134        throw new InvalidOperationException("item type is incompatible to required item type");
135      list.Add(item);
136      OnItemAdded(item, list.Count - 1);
137    }
138    public void Clear() {
139      list.Clear();
140      OnCleared();
141    }
142    public bool Contains(IItem item) {
143      return list.Contains(item);
144    }
145    public void CopyTo(IItem[] array, int arrayIndex) {
146      list.CopyTo(array, arrayIndex);
147    }
148    public int Count {
149      get { return list.Count; }
150    }
151    public bool IsReadOnly {
152      get { return false; }
153    }
154    public bool Remove(IItem item) {
155      int index = list.IndexOf(item);
156      if (list.Remove(item)) {
157        OnItemRemoved(item, index);
158        return true;
159      } else {
160        return false;
161      }
162    }
163    #endregion
164
165    #region IEnumerable<IItem> Members
166    public IEnumerator<IItem> GetEnumerator() {
167      return list.GetEnumerator();
168    }
169    #endregion
170
171    #region IEnumerable Members
172    IEnumerator IEnumerable.GetEnumerator() {
173      return list.GetEnumerator();
174    }
175    #endregion
176
177    public event EventHandler ItemTypeChanged;
178    protected virtual void OnItemTypeChanged() {
179      if (ItemTypeChanged != null)
180        ItemTypeChanged(this, new EventArgs());
181      OnChanged();
182    }
183    public event EventHandler<ItemIndexEventArgs> ItemAdded;
184    protected virtual void OnItemAdded(IItem item, int index) {
185      if (ItemAdded != null)
186        ItemAdded(this, new ItemIndexEventArgs(item, index));
187      OnChanged();
188    }
189    public event EventHandler<ItemIndexEventArgs> ItemRemoved;
190    protected virtual void OnItemRemoved(IItem item, int index) {
191      if (ItemRemoved != null)
192        ItemRemoved(this, new ItemIndexEventArgs(item, index));
193      OnChanged();
194    }
195    public event EventHandler Cleared;
196    protected virtual void OnCleared() {
197      if (Cleared != null)
198        Cleared(this, new EventArgs());
199      OnChanged();
200    }
201  }
202}
Note: See TracBrowser for help on using the repository browser.