forked from SharpMap/SharpMap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLayerCollection.cs
More file actions
251 lines (221 loc) · 8.96 KB
/
LayerCollection.cs
File metadata and controls
251 lines (221 loc) · 8.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Copyright 2007 - Christian Gräfe (SharpMap@SharpTools.de)
//
// This file is part of SharpMap.
// SharpMap is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// SharpMap is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public License
// along with SharpMap; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.Collections;
using System.Collections.Specialized;
namespace SharpMap.Layers
{
/// <summary>
/// A collection of <see cref="ILayer"/> instances.
/// </summary>
[Serializable]
public class LayerCollection : System.ComponentModel.BindingList<ILayer>, INotifyCollectionChanged, ILayersContainer
{
/// <summary>
/// Gets or sets the layer with the given <paramref name="layerName"/>.
/// </summary>
/// <param name="layerName">
/// Name of the layer to replace, if it exists.
/// </param>
public virtual ILayer this[string layerName]
{
get { return GetLayerByName(layerName); }
set
{
lock (this)
{
for (int i = 0; i < Count; i++)
{
int comparison = String.Compare(this[i].LayerName,
layerName, StringComparison.CurrentCultureIgnoreCase);
if (comparison == 0)
{
this[i] = value;
return;
}
}
Add(value);
}
}
}
/// <summary>
/// Returns a cloned copy of the LayerCollection
/// </summary>
/// <remarks>
/// The layer instances are the same as in the original collection, however if a layer implements ICloneable this could not be true.
/// </remarks>
/// <returns></returns>
public LayerCollection Clone()
{
lock (this)
{
var newColl = new LayerCollection();
foreach (ILayer lay in this)
{
var cloneable = lay as ICloneable;
if (cloneable != null)
newColl.Add((ILayer) cloneable.Clone());
else
newColl.Add(lay);
}
return newColl;
}
}
/// <summary>
/// Method to add all layers of <paramref name="other"/> to this collection
/// </summary>
/// <param name="other">A collection of layers</param>
public void AddCollection(LayerCollection other)
{
lock (this)
{
foreach (var lay in other)
{
Add(lay);
}
}
}
/// <summary>
/// Inserts the layer at the given <paramref name="index"/>.
/// </summary>
/// <param name="index">The index at which to add the layer.</param>
/// <param name="layer">The layer to insert.</param>
/// <exception cref="ArgumentOutOfRangeException">
/// Thrown if <paramref name="index"/> is less than 0 or is
/// greater or equal to <see cref="CollectionBase.Count"/>.
/// </exception>
public new void Insert(int index, ILayer layer)
{
lock (this)
{
if (index > Count || index < 0)
{
throw new ArgumentOutOfRangeException("index", index, "Index not in range");
}
InsertItem(index, layer);
}
}
/// <inheritdoc/>
protected override void OnAddingNew(System.ComponentModel.AddingNewEventArgs e)
{
ILayer newLayer = (e.NewObject as ILayer);
if (newLayer == null) throw new ArgumentNullException("value", "The passed argument is null or not an ILayer");
lock (this)
{
foreach (ILayer layer in this)
{
int comparison = String.Compare(layer.LayerName,
newLayer.LayerName, StringComparison.CurrentCultureIgnoreCase);
if (comparison == 0) throw new DuplicateLayerException(newLayer.LayerName);
}
}
base.OnAddingNew(new System.ComponentModel.AddingNewEventArgs(newLayer));
}
/// <summary>
/// Function to search for a layer in this collection by its name
/// </summary>
/// <param name="layerName">The name of the layer to search for</param>
/// <returns>The layer if found, otherwise <value>null</value></returns>
public ILayer GetLayerByName(string layerName)
{
lock (this)
{
LayerCollection lays = this;
return GetLayerByNameInternal(layerName, lays);
}
}
private static ILayer GetLayerByNameInternal(string layerName, System.Collections.Generic.IEnumerable<ILayer> lays)
{
foreach (ILayer layer in lays)
{
int comparison = String.Compare(layer.LayerName,
layerName, StringComparison.CurrentCultureIgnoreCase);
if (comparison == 0) return layer;
//If this is a layergroup, check sublayers also
if (layer is LayerGroup)
{
LayerGroup lg = layer as LayerGroup;
ILayer lay = GetLayerByNameInternal(layerName, lg.Layers);
if (lay != null)
return lay;
}
}
return null;
}
/// <summary>
/// Removes all layers from the collection.
/// </summary>
protected override void ClearItems()
{
lock (this)
{
foreach (var layer in Items)
{
var asyncLayer = layer as ITileAsyncLayer;
if (asyncLayer != null) asyncLayer.Cancel();
}
base.ClearItems();
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
}
/// <summary>
/// Event raised when this collection has changed.
/// </summary>
public event NotifyCollectionChangedEventHandler CollectionChanged;
/// <summary>
/// Fires the CollectionChanged event.
/// </summary>
/// <param name="e">Event to fire-</param>
protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
NotifyCollectionChangedEventHandler handler = CollectionChanged;
if (handler != null) handler(this, e);
}
/// <inheritdoc cref="System.Collections.ObjectModel.Collection{T}.InsertItem"/>
protected override void InsertItem(int index, ILayer item)
{
lock (this)
{
base.InsertItem(index, item);
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item, index));
}
}
/// <inheritdoc cref="System.Collections.ObjectModel.Collection{T}.RemoveItem"/>
protected override void RemoveItem(int index)
{
lock (this)
{
var removedItem = this[index];
base.RemoveItem(index);
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, removedItem, index));
}
}
/// <inheritdoc cref="System.Collections.ObjectModel.Collection{T}.SetItem"/>
protected override void SetItem(int index, ILayer item)
{
lock (this)
{
var oldItem = this[index];
base.SetItem(index, item);
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, item, oldItem, index));
}
}
System.Collections.Generic.IList<ILayer> ILayersContainer.Layers
{
get { return this; }
}
}
}