1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
5
using System.Diagnostics;
6
using System.Collections.Generic;
7
using System.Collections.ObjectModel;
8
using ICSharpCode.WpfDesign.XamlDom;
9
using ICSharpCode.WpfDesign.Designer.Services;
11
namespace ICSharpCode.WpfDesign.Designer.Xaml
13
sealed class XamlModelCollectionElementsCollection : IList<DesignItem>
15
readonly XamlModelProperty modelProperty;
16
readonly XamlProperty property;
17
readonly XamlDesignContext context;
19
public XamlModelCollectionElementsCollection(XamlModelProperty modelProperty, XamlProperty property)
21
this.modelProperty = modelProperty;
22
this.property = property;
23
this.context = (XamlDesignContext)modelProperty.DesignItem.Context;
28
return property.CollectionElements.Count;
32
public bool IsReadOnly {
38
public void Add(DesignItem item)
40
Insert(this.Count, item);
45
while (this.Count > 0) {
46
RemoveAt(this.Count - 1);
50
public bool Contains(DesignItem item)
52
XamlDesignItem xitem = CheckItemNoException(item);
54
return property.CollectionElements.Contains(xitem.XamlObject);
59
public int IndexOf(DesignItem item)
61
XamlDesignItem xitem = CheckItemNoException(item);
63
return property.CollectionElements.IndexOf(xitem.XamlObject);
68
public void CopyTo(DesignItem[] array, int arrayIndex)
70
for (int i = 0; i < this.Count; i++) {
71
array[arrayIndex + i] = this[i];
75
public bool Remove(DesignItem item)
77
int index = IndexOf(item);
84
public IEnumerator<DesignItem> GetEnumerator()
86
foreach (XamlPropertyValue val in property.CollectionElements) {
87
yield return GetItem(val);
91
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
93
return this.GetEnumerator();
96
DesignItem GetItem(XamlPropertyValue val)
98
if (val is XamlObject) {
99
return context._componentService.GetDesignItem( ((XamlObject)val).Instance );
101
throw new NotImplementedException();
105
XamlDesignItem CheckItem(DesignItem item)
108
throw new ArgumentNullException("item");
109
if (item.Context != modelProperty.DesignItem.Context)
110
throw new ArgumentException("The item must belong to the same context as this collection", "item");
111
XamlDesignItem xitem = item as XamlDesignItem;
112
Debug.Assert(xitem != null);
116
XamlDesignItem CheckItemNoException(DesignItem item)
118
return item as XamlDesignItem;
121
public DesignItem this[int index] {
123
return GetItem(property.CollectionElements[index]);
127
Insert(index, value);
131
public void Insert(int index, DesignItem item)
133
Execute(new InsertAction(this, index, CheckItem(item)));
136
public void RemoveAt(int index)
138
Execute(new RemoveAtAction(this, index, (XamlDesignItem)this[index]));
141
void Execute(ITransactionItem item)
143
UndoService undoService = context.Services.GetService<UndoService>();
144
if (undoService != null)
145
undoService.Execute(item);
150
void RemoveInternal(int index, XamlDesignItem item)
152
Debug.Assert(property.CollectionElements[index] == item.XamlObject);
153
property.CollectionElements.RemoveAt(index);
156
void InsertInternal(int index, XamlDesignItem item)
158
property.CollectionElements.Insert(index, item.XamlObject);
161
sealed class InsertAction : ITransactionItem
163
readonly XamlModelCollectionElementsCollection collection;
165
readonly XamlDesignItem item;
167
public InsertAction(XamlModelCollectionElementsCollection collection, int index, XamlDesignItem item)
169
this.collection = collection;
174
public ICollection<DesignItem> AffectedElements {
176
return new DesignItem[] { item };
180
public string Title {
182
return "Insert into collection";
188
collection.InsertInternal(index, item);
189
collection.modelProperty.XamlDesignItem.NotifyPropertyChanged(collection.modelProperty);
194
collection.RemoveInternal(index, item);
195
collection.modelProperty.XamlDesignItem.NotifyPropertyChanged(collection.modelProperty);
198
public bool MergeWith(ITransactionItem other)
204
sealed class RemoveAtAction : ITransactionItem
206
readonly XamlModelCollectionElementsCollection collection;
208
readonly XamlDesignItem item;
210
public RemoveAtAction(XamlModelCollectionElementsCollection collection, int index, XamlDesignItem item)
212
this.collection = collection;
217
public ICollection<DesignItem> AffectedElements {
219
return new DesignItem[] { collection.modelProperty.DesignItem };
223
public string Title {
225
return "Remove from collection";
231
collection.RemoveInternal(index, item);
232
collection.modelProperty.XamlDesignItem.NotifyPropertyChanged(collection.modelProperty);
237
collection.InsertInternal(index, item);
238
collection.modelProperty.XamlDesignItem.NotifyPropertyChanged(collection.modelProperty);
241
public bool MergeWith(ITransactionItem other)