Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Data/ConstrainedItemList.cs @ 312

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

Added HeuristicLab 3.0 sources from former SVN repository at revision 52

File size: 7.1 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
69    public override void Populate(XmlNode node, IDictionary<Guid,IStorable> restoredObjects) {
70      base.Populate(node, restoredObjects);
71      XmlNode listNode = node.SelectSingleNode("ListItems");
72      list = new List<IItem>();
73      for (int i = 0; i < listNode.ChildNodes.Count; i++)
74        list.Add((IItem)PersistenceManager.Restore(listNode.ChildNodes[i], restoredObjects));
75      suspendConstraintCheck = bool.Parse(listNode.Attributes["SuspendConstraintCheck"].Value);
76    }
77    #endregion
78
79    public override string ToString() {
80      if (list.Count > 0) {
81        StringBuilder builder = new StringBuilder();
82        builder.Append(list[0].ToString());
83        for (int i = 1; i < list.Count; i++) {
84          builder.Append(";");
85          builder.Append(list[i].ToString());
86        }
87        return builder.ToString();
88      } else {
89        return "Empty List";
90      }
91    }
92
93    public int IndexOf(IItem item) {
94      return list.IndexOf(item);
95    }
96
97    public void BeginCombinedOperation() {
98      suspendConstraintCheck = true;
99    }
100
101    public bool EndCombinedOperation(out ICollection<IConstraint> violatedConstraints) {
102      suspendConstraintCheck = false;
103      return IsValid(out violatedConstraints);
104    }
105
106    public bool TryInsert(int index, IItem item, out ICollection<IConstraint> violatedConstraints) {
107      list.Insert(index, item);
108      if (!suspendConstraintCheck && IsValid(out violatedConstraints)) {
109        OnItemAdded(item, index);
110        return true;
111      } else {
112        violatedConstraints = new List<IConstraint>();
113        list.RemoveAt(index);
114        return false;
115      }
116    }
117
118    public bool TryRemoveAt(int index, out ICollection<IConstraint> violatedConstraints) {
119      IItem item = list[index];
120      list.RemoveAt(index);
121      if (!suspendConstraintCheck && IsValid(out violatedConstraints)) {
122        OnItemRemoved(item, index);
123        return true;
124      } else {
125        violatedConstraints = new List<IConstraint>();
126        list.Insert(index, item);
127        return false;
128      }
129    }
130
131    public IItem this[int index] {
132      get { return list[index]; }
133    }
134
135    public bool TrySetAt(int index, IItem item, out ICollection<IConstraint> violatedConstraints) {
136      IItem backup = this[index];
137      list[index] = item;
138      if (!suspendConstraintCheck && IsValid(out violatedConstraints)) {
139        return true;
140      } else {
141        violatedConstraints = new List<IConstraint>();
142        list[index] = backup;
143        return false;
144      }
145    }
146
147    public bool TryAdd(IItem item, out ICollection<IConstraint> violatedConstraints) {
148      list.Add(item);
149      if (!suspendConstraintCheck && IsValid(out violatedConstraints)) {
150        OnItemAdded(item, list.Count - 1);
151        return true;
152      } else {
153        violatedConstraints = new List<IConstraint>();
154        list.RemoveAt(list.Count - 1);
155        return false;
156      }
157    }
158    public void Clear() {
159      list.Clear();
160      OnCleared();
161    }
162    public bool Contains(IItem item) {
163      return list.Contains(item);
164    }
165    public void CopyTo(IItem[] array, int arrayIndex) {
166      list.CopyTo(array, arrayIndex);
167    }
168    public int Count {
169      get { return list.Count; }
170    }
171    public bool IsReadOnly {
172      get { return false; }
173    }
174    public bool TryRemove(IItem item, out ICollection<IConstraint> violatedConstraints) {
175      int index = list.IndexOf(item);
176      if (index >= 0) {
177        return TryRemoveAt(index, out violatedConstraints);
178      } else {
179        violatedConstraints = new List<IConstraint>();
180        return false;
181      }
182    }
183
184    public IEnumerator<IItem> GetEnumerator() {
185      return list.GetEnumerator();
186    }
187
188    IEnumerator IEnumerable.GetEnumerator() {
189      return list.GetEnumerator();
190    }
191
192    public event EventHandler<ItemIndexEventArgs> ItemAdded;
193    protected virtual void OnItemAdded(IItem item, int index) {
194      if (ItemAdded != null)
195        ItemAdded(this, new ItemIndexEventArgs(item, index));
196      OnChanged();
197    }
198    public event EventHandler<ItemIndexEventArgs> ItemRemoved;
199    protected virtual void OnItemRemoved(IItem item, int index) {
200      if (ItemRemoved != null)
201        ItemRemoved(this, new ItemIndexEventArgs(item, index));
202      OnChanged();
203    }
204    public event EventHandler Cleared;
205    protected virtual void OnCleared() {
206      if (Cleared != null)
207        Cleared(this, new EventArgs());
208      OnChanged();
209    }
210  }
211}
Note: See TracBrowser for help on using the repository browser.