~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/DisplayBindings/WpfDesign/WpfDesign.Designer/Project/Xaml/XamlModelCollectionElementsCollection.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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)
 
3
 
 
4
using System;
 
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;
 
10
 
 
11
namespace ICSharpCode.WpfDesign.Designer.Xaml
 
12
{
 
13
        sealed class XamlModelCollectionElementsCollection : IList<DesignItem>
 
14
        {
 
15
                readonly XamlModelProperty modelProperty;
 
16
                readonly XamlProperty property;
 
17
                readonly XamlDesignContext context;
 
18
                
 
19
                public XamlModelCollectionElementsCollection(XamlModelProperty modelProperty, XamlProperty property)
 
20
                {
 
21
                        this.modelProperty = modelProperty;
 
22
                        this.property = property;
 
23
                        this.context = (XamlDesignContext)modelProperty.DesignItem.Context;
 
24
                }
 
25
                
 
26
                public int Count {
 
27
                        get {
 
28
                                return property.CollectionElements.Count;
 
29
                        }
 
30
                }
 
31
                
 
32
                public bool IsReadOnly {
 
33
                        get {
 
34
                                return false;
 
35
                        }
 
36
                }
 
37
                
 
38
                public void Add(DesignItem item)
 
39
                {
 
40
                        Insert(this.Count, item);
 
41
                }
 
42
                
 
43
                public void Clear()
 
44
                {
 
45
                        while (this.Count > 0) {
 
46
                                RemoveAt(this.Count - 1);
 
47
                        }
 
48
                }
 
49
                
 
50
                public bool Contains(DesignItem item)
 
51
                {
 
52
                        XamlDesignItem xitem = CheckItemNoException(item);
 
53
                        if (xitem != null)
 
54
                                return property.CollectionElements.Contains(xitem.XamlObject);
 
55
                        else
 
56
                                return false;
 
57
                }
 
58
                
 
59
                public int IndexOf(DesignItem item)
 
60
                {
 
61
                        XamlDesignItem xitem = CheckItemNoException(item);
 
62
                        if (xitem != null)
 
63
                                return property.CollectionElements.IndexOf(xitem.XamlObject);
 
64
                        else
 
65
                                return -1;
 
66
                }
 
67
                
 
68
                public void CopyTo(DesignItem[] array, int arrayIndex)
 
69
                {
 
70
                        for (int i = 0; i < this.Count; i++) {
 
71
                                array[arrayIndex + i] = this[i];
 
72
                        }
 
73
                }
 
74
                
 
75
                public bool Remove(DesignItem item)
 
76
                {
 
77
                        int index = IndexOf(item);
 
78
                        if (index < 0)
 
79
                                return false;
 
80
                        RemoveAt(index);
 
81
                        return true;
 
82
                }
 
83
                
 
84
                public IEnumerator<DesignItem> GetEnumerator()
 
85
                {
 
86
                        foreach (XamlPropertyValue val in property.CollectionElements) {
 
87
                                yield return GetItem(val);
 
88
                        }
 
89
                }
 
90
                
 
91
                System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
 
92
                {
 
93
                        return this.GetEnumerator();
 
94
                }
 
95
                
 
96
                DesignItem GetItem(XamlPropertyValue val)
 
97
                {
 
98
                        if (val is XamlObject) {
 
99
                                return context._componentService.GetDesignItem( ((XamlObject)val).Instance );
 
100
                        } else {
 
101
                                throw new NotImplementedException();
 
102
                        }
 
103
                }
 
104
                
 
105
                XamlDesignItem CheckItem(DesignItem item)
 
106
                {
 
107
                        if (item == null)
 
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);
 
113
                        return xitem;
 
114
                }
 
115
                
 
116
                XamlDesignItem CheckItemNoException(DesignItem item)
 
117
                {
 
118
                        return item as XamlDesignItem;
 
119
                }
 
120
                
 
121
                public DesignItem this[int index] {
 
122
                        get {
 
123
                                return GetItem(property.CollectionElements[index]);
 
124
                        }
 
125
                        set {
 
126
                                RemoveAt(index);
 
127
                                Insert(index, value);
 
128
                        }
 
129
                }
 
130
                
 
131
                public void Insert(int index, DesignItem item)
 
132
                {
 
133
                        Execute(new InsertAction(this, index, CheckItem(item)));
 
134
                }
 
135
                
 
136
                public void RemoveAt(int index)
 
137
                {
 
138
                        Execute(new RemoveAtAction(this, index, (XamlDesignItem)this[index]));
 
139
                }
 
140
                
 
141
                void Execute(ITransactionItem item)
 
142
                {
 
143
                        UndoService undoService = context.Services.GetService<UndoService>();
 
144
                        if (undoService != null)
 
145
                                undoService.Execute(item);
 
146
                        else
 
147
                                item.Do();
 
148
                }
 
149
                
 
150
                void RemoveInternal(int index, XamlDesignItem item)
 
151
                {
 
152
                        Debug.Assert(property.CollectionElements[index] == item.XamlObject);
 
153
                        property.CollectionElements.RemoveAt(index);
 
154
                }
 
155
                
 
156
                void InsertInternal(int index, XamlDesignItem item)
 
157
                {
 
158
                        property.CollectionElements.Insert(index, item.XamlObject);
 
159
                }
 
160
                
 
161
                sealed class InsertAction : ITransactionItem
 
162
                {
 
163
                        readonly XamlModelCollectionElementsCollection collection;
 
164
                        readonly int index;
 
165
                        readonly XamlDesignItem item;
 
166
                        
 
167
                        public InsertAction(XamlModelCollectionElementsCollection collection, int index, XamlDesignItem item)
 
168
                        {
 
169
                                this.collection = collection;
 
170
                                this.index = index;
 
171
                                this.item = item;
 
172
                        }
 
173
                        
 
174
                        public ICollection<DesignItem> AffectedElements {
 
175
                                get {
 
176
                                        return new DesignItem[] { item };
 
177
                                }
 
178
                        }
 
179
                        
 
180
                        public string Title {
 
181
                                get {
 
182
                                        return "Insert into collection";
 
183
                                }
 
184
                        }
 
185
                        
 
186
                        public void Do()
 
187
                        {
 
188
                                collection.InsertInternal(index, item);
 
189
                                collection.modelProperty.XamlDesignItem.NotifyPropertyChanged(collection.modelProperty);
 
190
                        }
 
191
                        
 
192
                        public void Undo()
 
193
                        {
 
194
                                collection.RemoveInternal(index, item);
 
195
                                collection.modelProperty.XamlDesignItem.NotifyPropertyChanged(collection.modelProperty);
 
196
                        }
 
197
                        
 
198
                        public bool MergeWith(ITransactionItem other)
 
199
                        {
 
200
                                return false;
 
201
                        }
 
202
                }
 
203
                
 
204
                sealed class RemoveAtAction : ITransactionItem
 
205
                {
 
206
                        readonly XamlModelCollectionElementsCollection collection;
 
207
                        readonly int index;
 
208
                        readonly XamlDesignItem item;
 
209
                        
 
210
                        public RemoveAtAction(XamlModelCollectionElementsCollection collection, int index, XamlDesignItem item)
 
211
                        {
 
212
                                this.collection = collection;
 
213
                                this.index = index;
 
214
                                this.item = item;
 
215
                        }
 
216
                        
 
217
                        public ICollection<DesignItem> AffectedElements {
 
218
                                get {
 
219
                                        return new DesignItem[] { collection.modelProperty.DesignItem };
 
220
                                }
 
221
                        }
 
222
                        
 
223
                        public string Title {
 
224
                                get {
 
225
                                        return "Remove from collection";
 
226
                                }
 
227
                        }
 
228
                        
 
229
                        public void Do()
 
230
                        {
 
231
                                collection.RemoveInternal(index, item);
 
232
                                collection.modelProperty.XamlDesignItem.NotifyPropertyChanged(collection.modelProperty);
 
233
                        }
 
234
                        
 
235
                        public void Undo()
 
236
                        {
 
237
                                collection.InsertInternal(index, item);
 
238
                                collection.modelProperty.XamlDesignItem.NotifyPropertyChanged(collection.modelProperty);
 
239
                        }
 
240
                        
 
241
                        public bool MergeWith(ITransactionItem other)
 
242
                        {
 
243
                                return false;
 
244
                        }
 
245
                }
 
246
        }
 
247
}