Free cookie consent management tool by TermsFeed Policy Generator

source: branches/XmlTextWriterBranch/HeuristicLab.Data/ConstrainedItemList.cs @ 241

Last change on this file since 241 was 119, checked in by gkronber, 16 years ago

created a branch that uses XmlTextWriter instead of XMLDocument to save documents. Investigating ticket #103.

File size: 7.6 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 ConstrainedItemList : ConstrainedItemBase, IEnumerable, IEnumerable<IItem> {
31    private List<IItem> list;
32    private bool suspendConstraintCheck;
33
34    public ConstrainedItemList()
35      : base() {
36      list = new List<IItem>();
37      suspendConstraintCheck = false;
38    }
39
40    public override IView CreateView() {
41      return new ConstrainedItemListView(this);
42    }
43
44    #region Clone & Persistence
45    public override object Clone(IDictionary<Guid, object> clonedObjects) {
46      ConstrainedItemList clone = new ConstrainedItemList();
47      clonedObjects.Add(Guid, clone);
48      foreach (IConstraint constraint in Constraints)
49        clone.AddConstraint((IConstraint)Auxiliary.Clone(constraint, clonedObjects));
50      clone.suspendConstraintCheck = suspendConstraintCheck;
51      foreach (IItem item in list) {
52        clone.list.Add((IItem)Auxiliary.Clone(item, clonedObjects));
53      }
54      return clone;
55    }
56
57    //public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid,IStorable> persistedObjects) {
58    //  XmlNode node = base.GetXmlNode(name, document, persistedObjects);
59    //  XmlNode listNode = document.CreateNode(XmlNodeType.Element, "ListItems", null);
60    //  for (int i = 0; i < list.Count; i++)
61    //    listNode.AppendChild(PersistenceManager.Persist(list[i], document, persistedObjects));
62    //  XmlAttribute sccAttrib = document.CreateAttribute("SuspendConstraintCheck");
63    //  sccAttrib.Value = suspendConstraintCheck.ToString();
64    //  listNode.Attributes.Append(sccAttrib);
65    //  node.AppendChild(listNode);
66    //  return node;
67    //}
68    public override void Persist(string name, XmlWriter writer, IDictionary<Guid, IStorable> persistedObjects) {
69      base.Persist(name, writer, persistedObjects);
70      writer.WriteStartElement("ListItems");
71      writer.WriteAttributeString("SuspendConstraintCheck", suspendConstraintCheck.ToString());
72      for(int i = 0; i < list.Count; i++)
73        PersistenceManager.Persist(list[i], writer, persistedObjects);
74      writer.WriteEndElement(); // </ListItems>
75    }
76
77    public override void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
78      base.Populate(node, restoredObjects);
79      XmlNode listNode = node.SelectSingleNode("ListItems");
80      list = new List<IItem>();
81      for (int i = 0; i < listNode.ChildNodes.Count; i++)
82        list.Add((IItem)PersistenceManager.Restore(listNode.ChildNodes[i], restoredObjects));
83      suspendConstraintCheck = bool.Parse(listNode.Attributes["SuspendConstraintCheck"].Value);
84    }
85    #endregion
86
87    public override string ToString() {
88      if (list.Count > 0) {
89        StringBuilder builder = new StringBuilder();
90        builder.Append(list[0].ToString());
91        for (int i = 1; i < list.Count; i++) {
92          builder.Append(";");
93          builder.Append(list[i].ToString());
94        }
95        return builder.ToString();
96      } else {
97        return "Empty List";
98      }
99    }
100
101    public int IndexOf(IItem item) {
102      return list.IndexOf(item);
103    }
104
105    public void BeginCombinedOperation() {
106      suspendConstraintCheck = true;
107    }
108
109    public bool EndCombinedOperation(out ICollection<IConstraint> violatedConstraints) {
110      suspendConstraintCheck = false;
111      return IsValid(out violatedConstraints);
112    }
113
114    public bool TryInsert(int index, IItem item, out ICollection<IConstraint> violatedConstraints) {
115      list.Insert(index, item);
116      if (!suspendConstraintCheck && IsValid(out violatedConstraints)) {
117        OnItemAdded(item, index);
118        return true;
119      } else {
120        violatedConstraints = new List<IConstraint>();
121        list.RemoveAt(index);
122        return false;
123      }
124    }
125
126    public bool TryRemoveAt(int index, out ICollection<IConstraint> violatedConstraints) {
127      IItem item = list[index];
128      list.RemoveAt(index);
129      if (!suspendConstraintCheck && IsValid(out violatedConstraints)) {
130        OnItemRemoved(item, index);
131        return true;
132      } else {
133        violatedConstraints = new List<IConstraint>();
134        list.Insert(index, item);
135        return false;
136      }
137    }
138
139    public IItem this[int index] {
140      get { return list[index]; }
141    }
142
143    public bool TrySetAt(int index, IItem item, out ICollection<IConstraint> violatedConstraints) {
144      IItem backup = this[index];
145      list[index] = item;
146      if (!suspendConstraintCheck && IsValid(out violatedConstraints)) {
147        return true;
148      } else {
149        violatedConstraints = new List<IConstraint>();
150        list[index] = backup;
151        return false;
152      }
153    }
154
155    public bool TryAdd(IItem item, out ICollection<IConstraint> violatedConstraints) {
156      list.Add(item);
157      if (!suspendConstraintCheck && IsValid(out violatedConstraints)) {
158        OnItemAdded(item, list.Count - 1);
159        return true;
160      } else {
161        violatedConstraints = new List<IConstraint>();
162        list.RemoveAt(list.Count - 1);
163        return false;
164      }
165    }
166    public void Clear() {
167      list.Clear();
168      OnCleared();
169    }
170    public bool Contains(IItem item) {
171      return list.Contains(item);
172    }
173    public void CopyTo(IItem[] array, int arrayIndex) {
174      list.CopyTo(array, arrayIndex);
175    }
176    public int Count {
177      get { return list.Count; }
178    }
179    public bool IsReadOnly {
180      get { return false; }
181    }
182    public bool TryRemove(IItem item, out ICollection<IConstraint> violatedConstraints) {
183      int index = list.IndexOf(item);
184      if (index >= 0) {
185        return TryRemoveAt(index, out violatedConstraints);
186      } else {
187        violatedConstraints = new List<IConstraint>();
188        return false;
189      }
190    }
191
192    public IEnumerator<IItem> GetEnumerator() {
193      return list.GetEnumerator();
194    }
195
196    IEnumerator IEnumerable.GetEnumerator() {
197      return list.GetEnumerator();
198    }
199
200    public event EventHandler<ItemIndexEventArgs> ItemAdded;
201    protected virtual void OnItemAdded(IItem item, int index) {
202      if (ItemAdded != null)
203        ItemAdded(this, new ItemIndexEventArgs(item, index));
204      OnChanged();
205    }
206    public event EventHandler<ItemIndexEventArgs> ItemRemoved;
207    protected virtual void OnItemRemoved(IItem item, int index) {
208      if (ItemRemoved != null)
209        ItemRemoved(this, new ItemIndexEventArgs(item, index));
210      OnChanged();
211    }
212    public event EventHandler Cleared;
213    protected virtual void OnCleared() {
214      if (Cleared != null)
215        Cleared(this, new EventArgs());
216      OnChanged();
217    }
218  }
219}
Note: See TracBrowser for help on using the repository browser.