Revision 42287 trunk/org.gvsig.desktop/org.gvsig.desktop.compat.cdc/org.gvsig.fmap.mapcontext/org.gvsig.fmap.mapcontext.api/src/main/java/org/gvsig/fmap/mapcontext/layers/FLayers.java

View differences:

FLayers.java
3 3
 *
4 4
 * Copyright (C) 2007-2013 gvSIG Association.
5 5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
6
 * This program is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 3 of the License, or (at your option) any later
9
 * version.
10 10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
11
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15 15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
16
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 19
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
23 22
 */
24 23
package org.gvsig.fmap.mapcontext.layers;
25 24

  
......
70 69
import org.gvsig.tools.visitor.VisitCanceledException;
71 70
import org.gvsig.tools.visitor.Visitor;
72 71

  
73

  
74 72
/**
75
 * <p>Represents a generic collection of layers, that can be represented as a node in a tree of nodes of layers.</p>
73
 * <p>
74
 * Represents a generic collection of layers, that can be represented as a node
75
 * in a tree of nodes of layers.</p>
76 76
 *
77
 * <p>Adapts the basic functionality implemented for a layer in the abstract class <code>FLyrDefault</code>, to
78
 *  a collection of layers, implementing, as well, specific methods for this kind of object, defined in the
79
 *  interfaces <code>VectorialData</code>, <code>LayerCollection</code>, and <code>InfoByPoint</code>.</p>
77
 * <p>
78
 * Adapts the basic functionality implemented for a layer in the abstract class
79
 * <code>FLyrDefault</code>, to a collection of layers, implementing, as well,
80
 * specific methods for this kind of object, defined in the interfaces
81
 * <code>VectorialData</code>, <code>LayerCollection</code>, and
82
 * <code>InfoByPoint</code>.</p>
80 83
 *
81 84
 * @see FLyrDefault
82 85
 */
83 86
public class FLayers extends FLyrDefault implements LayerCollection,
84
InfoByPoint {
87
        InfoByPoint {
85 88

  
86
	
87
	/**
88
	 * List with all listeners registered for this kind of node.
89
	 *
90
	 * @see #addLayerCollectionListener(LayerCollectionListener)
91
	 * @see #removeLayerCollectionListener(LayerCollectionListener)
92
	 * @see #callLayerAdded(LayerCollectionEvent)
93
	 * @see #callLayerAdding(LayerCollectionEvent)
94
	 * @see #callLayerMoved(LayerPositionEvent)
95
	 * @see #callLayerMoving(LayerPositionEvent)
96
	 * @see #callLayerRemoved(LayerCollectionEvent)
97
	 * @see #callLayerRemoving(LayerCollectionEvent)
98
	 */
99
	protected ArrayList layerCollectionListeners = null;
89
    /**
90
     * List with all listeners registered for this kind of node.
91
     *
92
     * @see #addLayerCollectionListener(LayerCollectionListener)
93
     * @see #removeLayerCollectionListener(LayerCollectionListener)
94
     * @see #callLayerAdded(LayerCollectionEvent)
95
     * @see #callLayerAdding(LayerCollectionEvent)
96
     * @see #callLayerMoved(LayerPositionEvent)
97
     * @see #callLayerMoving(LayerPositionEvent)
98
     * @see #callLayerRemoved(LayerCollectionEvent)
99
     * @see #callLayerRemoving(LayerCollectionEvent)
100
     */
101
    protected ArrayList layerCollectionListeners = null;
100 102

  
101
	/**
102
	 * A synchronized list with the layers.
103
	 *
104
	 * @see #setAllVisibles(boolean)
105
	 * @see #addLayer(FLayer)
106
	 * @see #addLayer(int, FLayer)
107
	 * @see #moveTo(int, int)
108
	 * @see #removeLayer(FLayer)
109
	 * @see #removeLayer(int)
110
	 * @see #removeLayer(String)
111
	 * @see #replaceLayer(String, FLayer)
112
	 * @see #getVisibles()
113
	 * @see #getLayer(int)
114
	 * @see #getLayer(String)
115
	 * @see #getLayersCount()
116
	 * @see #getFullEnvelope()
117
	 */
118
	protected List layers = null;
103
    /**
104
     * A synchronized list with the layers.
105
     *
106
     * @see #setAllVisibles(boolean)
107
     * @see #addLayer(FLayer)
108
     * @see #addLayer(int, FLayer)
109
     * @see #moveTo(int, int)
110
     * @see #removeLayer(FLayer)
111
     * @see #removeLayer(int)
112
     * @see #removeLayer(String)
113
     * @see #replaceLayer(String, FLayer)
114
     * @see #getVisibles()
115
     * @see #getLayer(int)
116
     * @see #getLayer(String)
117
     * @see #getLayersCount()
118
     * @see #getFullEnvelope()
119
     */
120
    protected List layers = null;
119 121

  
120
	/**
121
	 * The model of the layer.
122
	 *
123
	 * @see #getMapContext()
124
	 */
125
	protected MapContext fmap;
122
    /**
123
     * The model of the layer.
124
     *
125
     * @see #getMapContext()
126
     */
127
    protected MapContext fmap;
126 128

  
127
	/**
128
	 * Useful for debug the problems during the implementation.
129
	 */
130
	private static Logger logger = LoggerFactory.getLogger(FLayers.class);
129
    /**
130
     * Useful for debug the problems during the implementation.
131
     */
132
    private static Logger logger = LoggerFactory.getLogger(FLayers.class);
131 133

  
132
	public FLayers() {
133
		super();
134
		layerCollectionListeners = new ArrayList();
135
		layers = Collections.synchronizedList(new ArrayList());
136
		
137
		logger = LoggerFactory.getLogger(FLayers.class);
138
	}
139
	/*
140
	 * (non-Javadoc)
141
	 * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#addLayerCollectionListener(com.iver.cit.gvsig.fmap.layers.LayerCollectionListener)
142
	 */
143
	public void addLayerCollectionListener(LayerCollectionListener listener) {
144
		if (!layerCollectionListeners.contains(listener)) {
145
			layerCollectionListeners.add(listener);
146
		}
147
	}
134
    public FLayers() {
135
        super();
136
        layerCollectionListeners = new ArrayList();
137
        layers = Collections.synchronizedList(new ArrayList());
148 138

  
149
	/*
150
	 * (non-Javadoc)
151
	 * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#setAllVisibles(boolean)
152
	 */
153
	public void setAllVisibles(boolean visible) {
154
		FLayer lyr;
139
        logger = LoggerFactory.getLogger(FLayers.class);
140
    }
141
    /*
142
     * (non-Javadoc)
143
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#addLayerCollectionListener(com.iver.cit.gvsig.fmap.layers.LayerCollectionListener)
144
     */
155 145

  
156
		for (int i = 0; i < layers.size(); i++) {
157
			lyr = ((FLayer) layers.get(i));
158
			lyr.setVisible(visible);
146
    public void addLayerCollectionListener(LayerCollectionListener listener) {
147
        if (!layerCollectionListeners.contains(listener)) {
148
            layerCollectionListeners.add(listener);
149
        }
150
    }
159 151

  
160
			if (lyr instanceof LayerCollection) {
161
				((LayerCollection) lyr).setAllVisibles(visible);
162
			}
163
		}
164
	}
152
    /*
153
     * (non-Javadoc)
154
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#setAllVisibles(boolean)
155
     */
156
    public void setAllVisibles(boolean visible) {
157
        FLayer lyr;
165 158

  
166
	/*
167
	 * (non-Javadoc)
168
	 * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#removeLayerCollectionListener(com.iver.cit.gvsig.fmap.layers.LayerCollectionListener)
169
	 */
170
	public void removeLayerCollectionListener(LayerCollectionListener listener) {
171
		layerCollectionListeners.remove(listener);
172
	}
159
        for (int i = 0; i < layers.size(); i++) {
160
            lyr = ((FLayer) layers.get(i));
161
            lyr.setVisible(visible);
173 162

  
174
	/**
175
	 * Adds a layer on an specified position in this node.
176
	 *
177
	 * @param pos position in the inner list where the layer will be added
178
	 * @param layer a layer
179
	 */
180
	private void doAddLayer(int pos,FLayer layer) {
181
		layers.add(pos,layer);
182
		ToolsLocator.getDisposableManager().bind(layer);
183
		layer.setParentLayer(this);
184
		IProjection layerProj = layer.getProjection();
185
		if(layerProj != null && fmap != null) {
186
			IProjection mapContextProj = fmap.getProjection();
163
            if (lyr instanceof LayerCollection) {
164
                ((LayerCollection) lyr).setAllVisibles(visible);
165
            }
166
        }
167
    }
168

  
169
    /*
170
     * (non-Javadoc)
171
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#removeLayerCollectionListener(com.iver.cit.gvsig.fmap.layers.LayerCollectionListener)
172
     */
173
    public void removeLayerCollectionListener(LayerCollectionListener listener) {
174
        layerCollectionListeners.remove(listener);
175
    }
176

  
177
    /**
178
     * Adds a layer on an specified position in this node.
179
     *
180
     * @param pos position in the inner list where the layer will be added
181
     * @param layer a layer
182
     */
183
    private void doAddLayer(int pos, FLayer layer) {
184
        layers.add(pos, layer);
185
        ToolsLocator.getDisposableManager().bind(layer);
186
        layer.setParentLayer(this);
187
        IProjection layerProj = layer.getProjection();
188
        if (layerProj != null && fmap != null) {
189
            IProjection mapContextProj = fmap.getProjection();
187 190
			// TODO REVISAR ESTO !!!!
188
			// Esta condici?n puede que no fuese exacta para todos los casos
189
			if (!layerProj.getAbrev().equals(mapContextProj.getAbrev())) {
190
				ICoordTrans ct = layerProj.getCT(mapContextProj);
191
				layer.setCoordTrans(ct);
192
			} else {
193
				layer.setCoordTrans(null);
194
			}
195
		}
196
		this.updateDrawVersion();
197
	}
191
            // Esta condici?n puede que no fuese exacta para todos los casos
192
            if (!layerProj.getAbrev().equals(mapContextProj.getAbrev())) {
193
                ICoordTrans ct = layerProj.getCT(mapContextProj);
194
                layer.setCoordTrans(ct);
195
            } else {
196
                layer.setCoordTrans(null);
197
            }
198
        }
199
        this.updateDrawVersion();
200
    }
198 201

  
199
	/*
200
	 * (non-Javadoc)
201
	 * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#addLayer(com.iver.cit.gvsig.fmap.layers.FLayer)
202
	 */
203
	public void addLayer(FLayer layer) {
204
		
205
		MapContext mco = this.getMapContext();
206
		
207
		if (mco != null) {
208
			/*
209
			 * Get order manager from map context
210
			 */
211
			int position = mco.getOrderManager().getPosition(this, layer);
212
			addLayer(position, layer);
213
		} else {
214
			/*
215
			 * This happens when FLayers object is not in a
216
			 * map context, so no order manager is available.
217
			 */
218
			addLayer(layers.size(),layer);
219
		}
220
	}
202
    /*
203
     * (non-Javadoc)
204
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#addLayer(com.iver.cit.gvsig.fmap.layers.FLayer)
205
     */
206
    public void addLayer(FLayer layer) {
221 207

  
222
	/**
223
	 * Adds a layer in an specified position in this node.
224
	 *
225
	 * @param layer a layer
226
	 */
227
	public void addLayer(int pos,FLayer layer) {
228
		try {
229
			//Notificamos a la capa que va a ser a?adida
230
			if (layer instanceof FLyrDefault) {
231
				((FLyrDefault)layer).wakeUp();
232
			}
208
        MapContext mco = this.getMapContext();
233 209

  
234
			if (layer instanceof FLayers){
235
				FLayers layers=(FLayers)layer;
236
				fmap.addAsCollectionListener(layers);
237
			}
238
			callLayerAdding(LayerCollectionEvent.createLayerAddingEvent(layer));
210
        if (mco != null) {
211
            /*
212
             * Get order manager from map context
213
             */
214
            int position = mco.getOrderManager().getPosition(this, layer);
215
            addLayer(position, layer);
216
        } else {
217
            /*
218
             * This happens when FLayers object is not in a
219
             * map context, so no order manager is available.
220
             */
221
            addLayer(layers.size(), layer);
222
        }
223
    }
239 224

  
240
			doAddLayer(pos,layer);
225
    /**
226
     * Adds a layer in an specified position in this node.
227
     *
228
     * @param layer a layer
229
     */
230
    public void addLayer(int pos, FLayer layer) {
231
        try {
232
            //Notificamos a la capa que va a ser a?adida
233
            if (layer instanceof FLyrDefault) {
234
                ((FLyrDefault) layer).wakeUp();
235
            }
241 236

  
242
			callLayerAdded(LayerCollectionEvent.createLayerAddedEvent(layer));
243
		} catch (CancelationException e) {
244
			logger.warn(e.getMessage());
245
		} catch (LoadLayerException e) {
246
			layer.setAvailable(false);
247
			layer.addError(e);
248
		}
249
	}
237
            if (layer instanceof FLayers) {
238
                FLayers layers = (FLayers) layer;
239
                fmap.addAsCollectionListener(layers);
240
            }
241
            callLayerAdding(LayerCollectionEvent.createLayerAddingEvent(layer));
250 242

  
251
	/*
252
	 * (non-Javadoc)
253
	 * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#moveTo(int, int)
254
	 */
255
	public void moveTo(int from, int to) throws CancelationException {
256
		int newfrom=layers.size()-from-1;
257
		int newto=layers.size()-to-1;
258
		if ( newfrom < 0 || newfrom >=layers.size() || newto < 0 || newto >= layers.size()) {
259
			return;
260
		}
261
		FLayer aux = (FLayer) layers.get(newfrom);
262
		callLayerMoving(LayerPositionEvent.createLayerMovingEvent(aux, newfrom, newto));
263
		layers.remove(newfrom);
264
		layers.add(newto, aux);
265
		this.updateDrawVersion();
266
		callLayerMoved(LayerPositionEvent.createLayerMovedEvent(aux, newfrom, newto));
267
	}
243
            doAddLayer(pos, layer);
268 244

  
269
	/**
270
	 * Removes an inner layer.
271
	 *
272
	 * @param lyr a layer
273
	 */
274
	private void doRemoveLayer(FLayer lyr) {
275
		layers.remove(lyr);
276
		lyr.dispose();
277
		this.updateDrawVersion();
278
	}
245
            callLayerAdded(LayerCollectionEvent.createLayerAddedEvent(layer));
246
        } catch (CancelationException e) {
247
            logger.warn(e.getMessage());
248
        } catch (LoadLayerException e) {
249
            layer.setAvailable(false);
250
            layer.addError(e);
251
        }
252
    }
279 253

  
280
	/*
281
	 * (non-Javadoc)
282
	 * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#removeLayer(com.iver.cit.gvsig.fmap.layers.FLayer)
283
	 */
284
	public void removeLayer(FLayer lyr) throws CancelationException {
285
		callLayerRemoving(LayerCollectionEvent.createLayerRemovingEvent(lyr));
286
		doRemoveLayer(lyr);
287
		callLayerRemoved(LayerCollectionEvent.createLayerRemovedEvent(lyr));
288
	}
254
    /*
255
     * (non-Javadoc)
256
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#moveTo(int, int)
257
     */
258
    public void moveTo(int from, int to) throws CancelationException {
259
        int newfrom = layers.size() - from - 1;
260
        int newto = layers.size() - to - 1;
261
        if (newfrom < 0 || newfrom >= layers.size() || newto < 0 || newto >= layers.size()) {
262
            return;
263
        }
264
        FLayer aux = (FLayer) layers.get(newfrom);
265
        callLayerMoving(LayerPositionEvent.createLayerMovingEvent(aux, newfrom, newto));
266
        layers.remove(newfrom);
267
        layers.add(newto, aux);
268
        this.updateDrawVersion();
269
        callLayerMoved(LayerPositionEvent.createLayerMovedEvent(aux, newfrom, newto));
270
    }
289 271

  
290
	/*
291
	 * (non-Javadoc)
292
	 * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#removeLayer(int)
293
	 */
294
	public void removeLayer(int idLayer) {
295
		FLayer lyr = (FLayer) layers.get(idLayer);
296
		callLayerRemoving(LayerCollectionEvent.createLayerRemovingEvent(lyr));
297
		this.doRemoveLayer(lyr);
272
    /**
273
     * Removes an inner layer.
274
     *
275
     * @param lyr a layer
276
     */
277
    private void doRemoveLayer(FLayer lyr) {
278
        layers.remove(lyr);
279
        lyr.dispose();
280
        this.updateDrawVersion();
281
    }
282

  
283
    /*
284
     * (non-Javadoc)
285
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#removeLayer(com.iver.cit.gvsig.fmap.layers.FLayer)
286
     */
287
    public void removeLayer(FLayer lyr) throws CancelationException {
288
        callLayerRemoving(LayerCollectionEvent.createLayerRemovingEvent(lyr));
289
        doRemoveLayer(lyr);
290
        callLayerRemoved(LayerCollectionEvent.createLayerRemovedEvent(lyr));
291
    }
292

  
293
    /*
294
     * (non-Javadoc)
295
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#removeLayer(int)
296
     */
297
    public void removeLayer(int idLayer) {
298
        FLayer lyr = (FLayer) layers.get(idLayer);
299
        callLayerRemoving(LayerCollectionEvent.createLayerRemovingEvent(lyr));
300
        this.doRemoveLayer(lyr);
298 301
//		layers.remove(idLayer);
299 302
//		this.updateDrawVersion();
300
		callLayerRemoved(LayerCollectionEvent.createLayerRemovedEvent(lyr));
301
	}
303
        callLayerRemoved(LayerCollectionEvent.createLayerRemovedEvent(lyr));
304
    }
302 305

  
303
	/*
304
	 * (non-Javadoc)
305
	 * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#removeLayer(java.lang.String)
306
	 */
307
	public void removeLayer(String layerName) {
308
		FLayer lyr;
306
    /*
307
     * (non-Javadoc)
308
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#removeLayer(java.lang.String)
309
     */
310
    public void removeLayer(String layerName) {
311
        FLayer lyr;
309 312

  
310
		for (int i = 0; i < layers.size(); i++) {
311
			lyr = ((FLayer) layers.get(i));
313
        for (int i = 0; i < layers.size(); i++) {
314
            lyr = ((FLayer) layers.get(i));
312 315

  
313
			if (lyr.getName().compareToIgnoreCase(layerName) == 0) {
314
				removeLayer(i);
316
            if (lyr.getName().compareToIgnoreCase(layerName) == 0) {
317
                removeLayer(i);
315 318

  
316
				break;
317
			}
318
		}
319
	}
319
                break;
320
            }
321
        }
322
    }
320 323

  
321
	/**
322
	 * Replace a layer identified by its name, by another.
323
	 *
324
	 * @param layerName the name of the layer to be replaced
325
	 * @param layer the new layer
326
	 * @deprecated use {@link FLayers#replaceLayer(FLayer, FLayer)}
327
	 */
328
	public void replaceLayer(String layerName, FLayer layer) throws LoadLayerException
329
	{
330
		replaceLayer(getLayer(layerName), layer);
331
	}
332
	
333
	/**
334
     * Replace a layer by another layer.  It search recursively by all the 
324
    /**
325
     * Replace a layer identified by its name, by another.
326
     *
327
     * @param layerName the name of the layer to be replaced
328
     * @param layer the new layer
329
     * @deprecated use {@link FLayers#replaceLayer(FLayer, FLayer)}
330
     */
331
    public void replaceLayer(String layerName, FLayer layer) throws LoadLayerException {
332
        replaceLayer(getLayer(layerName), layer);
333
    }
334

  
335
    /**
336
     * Replace a layer by another layer. It search recursively by all the
335 337
     * ILayerCollection nodes
336 338
     *
337
     * @param layer
338
     *          the layer to be replaced
339
     * @param newLayer
340
     *          the new layer
339
     * @param layer the layer to be replaced
340
     * @param newLayer the new layer
341 341
     */
342
    public void replaceLayer(FLayer layer, FLayer newLayer) throws LoadLayerException
343
    {        
342
    public void replaceLayer(FLayer layer, FLayer newLayer) throws LoadLayerException {
344 343
        replaceLayer(this, layer, newLayer);
345 344
    }
346
    
345

  
347 346
    /**
348
     * Replace a layer by other layer. It search recursively by all the 
347
     * Replace a layer by other layer. It search recursively by all the
349 348
     * ILayerCollection nodes
350
     * @param parentLayer
351
     *          the parent layer
352
     * @param layer
353
     *          the layer to be replaced
354
     * @param newLayer
355
     *          the new layer
349
     *
350
     * @param parentLayer the parent layer
351
     * @param layer the layer to be replaced
352
     * @param newLayer the new layer
356 353
     * @throws LoadLayerException
357 354
     */
358
    private void replaceLayer(FLayers parentLayer, FLayer layer, FLayer newLayer) throws LoadLayerException
359
    {        
360
        FLayer lyr;       
355
    private void replaceLayer(FLayers parentLayer, FLayer layer, FLayer newLayer) throws LoadLayerException {
356
        FLayer lyr;
361 357
        for (int i = 0; i < parentLayer.getLayersCount(); i++) {
362 358
            lyr = ((FLayer) parentLayer.getLayer(i));
363
            if (lyr.equals(layer)) {              
364
                parentLayer.removeLayer(i);             
365
                parentLayer.addLayer(i, newLayer);                
359
            if (lyr.equals(layer)) {
360
                parentLayer.removeLayer(i);
361
                parentLayer.addLayer(i, newLayer);
366 362
                break;
367 363
            }
368
            if (lyr instanceof LayerCollection){
369
                replaceLayer((FLayers)lyr, layer, newLayer);
364
            if (lyr instanceof LayerCollection) {
365
                replaceLayer((FLayers) lyr, layer, newLayer);
370 366
            }
371 367
        }
372 368
    }
373 369

  
374
	/*
375
	 * (non-Javadoc)
376
	 * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#getVisibles()
377
	 */
378
	public FLayer[] getVisibles() {
379
		ArrayList array = new ArrayList();
380
		LayersIterator iter = new LayersIterator(this) {
381
			public boolean evaluate(FLayer layer) {
382
				return layer.isVisible();
383
			}
370
    /*
371
     * (non-Javadoc)
372
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#getVisibles()
373
     */
374
    public FLayer[] getVisibles() {
375
        ArrayList array = new ArrayList();
376
        LayersIterator iter = new LayersIterator(this) {
377
            public boolean evaluate(FLayer layer) {
378
                return layer.isVisible();
379
            }
384 380

  
385
		};
381
        };
386 382

  
387
		while (iter.hasNext()) {
388
			array.add(iter.nextLayer());
389
		}
383
        while (iter.hasNext()) {
384
            array.add(iter.nextLayer());
385
        }
390 386

  
391
		return (FLayer[]) array.toArray(new FLayer[0]);
392
	}
387
        return (FLayer[]) array.toArray(new FLayer[0]);
388
    }
393 389

  
394
	/*
395
	 * (non-Javadoc)
396
	 * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#getLayer(int)
397
	 */
398
	public FLayer getLayer(int index) {
399
		return (FLayer) layers.get(index);
400
	}
390
    /*
391
     * (non-Javadoc)
392
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#getLayer(int)
393
     */
394
    public FLayer getLayer(int index) {
395
        return (FLayer) layers.get(index);
396
    }
401 397

  
402
	/*
403
	 * (non-Javadoc)
404
	 * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#getLayer(java.lang.String)
405
	 */
406
	public FLayer getLayer(String layerName) {
407
		FLayer lyr;
408
		FLayer lyr2;
409
		ArrayList layerList;
398
    /*
399
     * (non-Javadoc)
400
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#getLayer(java.lang.String)
401
     */
402
    public FLayer getLayer(String layerName) {
403
        FLayer lyr;
404
        FLayer lyr2;
405
        ArrayList layerList;
410 406

  
411
		for (int i = 0; i < layers.size(); i++) {
412
			lyr = ((FLayer) layers.get(i));
407
        for (int i = 0; i < layers.size(); i++) {
408
            lyr = ((FLayer) layers.get(i));
413 409

  
414
			if (lyr.getName().compareToIgnoreCase(layerName) == 0) {
415
				return lyr;
416
			}
410
            if (lyr.getName().compareToIgnoreCase(layerName) == 0) {
411
                return lyr;
412
            }
417 413

  
418
			layerList = new ArrayList();
419
			splitLayerGroup(lyr,layerList);
420
			for(int j = 0; j<layerList.size(); j++ )
421
			{
422
				lyr2 = ((FLayer)layerList.get(j));
423
				if (lyr2.getName().compareToIgnoreCase(layerName) == 0) {
424
					return lyr2;
425
				}
426
			}
427
		}
414
            layerList = new ArrayList();
415
            splitLayerGroup(lyr, layerList);
416
            for (int j = 0; j < layerList.size(); j++) {
417
                lyr2 = ((FLayer) layerList.get(j));
418
                if (lyr2.getName().compareToIgnoreCase(layerName) == 0) {
419
                    return lyr2;
420
                }
421
            }
422
        }
428 423

  
429
		return null;
430
	}
424
        return null;
425
    }
431 426

  
432
	/**
433
	 * <p> Splits up a layer group in order to get a layer by name when there are layer groups</p>
434
	 *
435
	 * <p>In <code>result</code> always will be at least one layer.</p>
436
	 *
437
	 * @param layer the layer we are looking for
438
	 * @param result an array list that will have the results of the search
439
	 */
440
	private void splitLayerGroup(FLayer layer, ArrayList result)
441
	{
442
		int i;
443
		FLayers layerGroup;
444
		if (layer instanceof FLayers)
445
		{
446
			layerGroup = (FLayers)layer;
447
			for (i=0; i < layerGroup.getLayersCount(); i++ )
448
			{
449
				splitLayerGroup(layerGroup.getLayer(i),result);
450
			}
451
		}
452
		else
453
		{
454
			result.add(layer);
455
		}
456
	}
427
    /**
428
     * <p>
429
     * Splits up a layer group in order to get a layer by name when there are
430
     * layer groups</p>
431
     *
432
     * <p>
433
     * In <code>result</code> always will be at least one layer.</p>
434
     *
435
     * @param layer the layer we are looking for
436
     * @param result an array list that will have the results of the search
437
     */
438
    private void splitLayerGroup(FLayer layer, ArrayList result) {
439
        int i;
440
        FLayers layerGroup;
441
        if (layer instanceof FLayers) {
442
            layerGroup = (FLayers) layer;
443
            for (i = 0; i < layerGroup.getLayersCount(); i++) {
444
                splitLayerGroup(layerGroup.getLayer(i), result);
445
            }
446
        } else {
447
            result.add(layer);
448
        }
449
    }
457 450

  
458
	/*
459
	 * (non-Javadoc)
460
	 * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#getLayersCount()
461
	 */
462
	public int getLayersCount() {
463
		return layers.size();
464
	}
451
    /*
452
     * (non-Javadoc)
453
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#getLayersCount()
454
     */
455
    public int getLayersCount() {
456
        return layers.size();
457
    }
465 458

  
466
	/*
467
	 * (non-Javadoc)
468
	 * @see com.iver.cit.gvsig.fmap.layers.FLayer#draw(java.awt.image.BufferedImage, java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort, com.iver.utiles.swing.threads.Cancellable, double)
469
	 */
470
	public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
471
			Cancellable cancel, double scale) throws ReadException {
472
		// FIXME Arreglar este error
473
		throw new RuntimeException("Esto no deberia de llamarse");
474
	}
459
    /*
460
     * (non-Javadoc)
461
     * @see com.iver.cit.gvsig.fmap.layers.FLayer#draw(java.awt.image.BufferedImage, java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort, com.iver.utiles.swing.threads.Cancellable, double)
462
     */
463
    public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
464
            Cancellable cancel, double scale) throws ReadException {
465
        // FIXME Arreglar este error
466
        throw new RuntimeException("Esto no deberia de llamarse");
467
    }
475 468

  
476
	/*
477
	 * (non-Javadoc)
478
	 *
479
	 * @see com.iver.cit.gvsig.fmap.layers.FLayer#print(java.awt.Graphics2D,
480
	 * com.iver.cit.gvsig.fmap.ViewPort,
481
	 * com.iver.utiles.swing.threads.Cancellable, double,
482
	 * javax.print.attribute.PrintAttributes)
483
	 */
484
	public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel,
485
			double scale, PrintAttributes properties)
486
	throws ReadException {
487
		throw new RuntimeException("No deberia pasar por aqui");
488
	}
469
    /*
470
     * (non-Javadoc)
471
     *
472
     * @see com.iver.cit.gvsig.fmap.layers.FLayer#print(java.awt.Graphics2D,
473
     * com.iver.cit.gvsig.fmap.ViewPort,
474
     * com.iver.utiles.swing.threads.Cancellable, double,
475
     * javax.print.attribute.PrintAttributes)
476
     */
477
    public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel,
478
            double scale, PrintAttributes properties)
479
            throws ReadException {
480
        throw new RuntimeException("No deberia pasar por aqui");
481
    }
489 482

  
490
	public void print_old(Graphics2D g, ViewPort viewPort, Cancellable cancel,
491
			double scale, PrintAttributes properties)
492
	throws ReadException {
493
		this.print_old(g, viewPort, cancel, scale, properties, null);
494
	}
483
    public void print_old(Graphics2D g, ViewPort viewPort, Cancellable cancel,
484
            double scale, PrintAttributes properties)
485
            throws ReadException {
486
        this.print_old(g, viewPort, cancel, scale, properties, null);
487
    }
495 488

  
496
	/**
497
	 * <p>Checks all layers (each one as a sub-node of this node <i>collection of layers</i>) of this collection and draws their requested properties. If a node is
498
	 *  a group of layers (<code>ComposedLayer</code>), executes it's drawn.</p>
499
	 *
500
	 * <p>All nodes which could group with the composed layer <code>group</code>, will be drawn together. And once the <code>
501
	 * group</code> is drawn, will be set to <code>null</code> if hasn't a parent layer.</p>
502
	 *
503
	 * <p>The particular implementation depends on the kind of each layer and composed layer. And this process can be cancelled at any
504
	 *  time by the shared object <code>cancel</code>.</p>
505
	 *
506
	 * <p>According the print quality, labels will be printed in different resolution:
507
	 *  <ul>
508
	 *   <li><b>PrintQuality.DRAFT</b>: 72 dpi (dots per inch).</li>
509
	 *   <li><b>PrintQuality.NORMAL</b>: 300 dpi (dots per inch).</li>
510
	 *   <li><b>PrintQuality.HIGH</b>: 600 dpi (dots per inch).</li>
511
	 *  </ul>
512
	 * </p>
513
	 *
514
	 * @param g for rendering 2-dimensional shapes, text and images on the Java(tm) platform
515
	 * @param viewPort the information for drawing the layers
516
	 * @param cancel shared object that determines if this layer can continue being drawn
517
	 * @param scale the scale of the view. Must be between {@linkplain FLayer#getMinScale()} and {@linkplain FLayer#getMaxScale()}.
518
	 * @param properties properties that will be print
519
	 * @param group a composed layer pending to paint; if this parameter is <code>null</code>, the composed layer
520
	 *
521
	 * @return <code>null</code> if the layers in <code>group</code> had been drawn or were <code>null</code>; otherwise, the <code>group</code>
522
	 *
523
	 * @see FLayer#print(Graphics2D, ViewPort, Cancellable, double, PrintAttributes)
524
	 *
525
	 * @throws ReadDriverException if fails the driver reading the data.
526
	 */
527
	public ComposedLayer print_old(Graphics2D g, ViewPort viewPort, Cancellable cancel, double scale, PrintAttributes properties, ComposedLayer group)
528
	throws ReadException {
529
		double dpi = 72;
489
    /**
490
     * <p>
491
     * Checks all layers (each one as a sub-node of this node <i>collection of
492
     * layers</i>) of this collection and draws their requested properties. If a
493
     * node is a group of layers (<code>ComposedLayer</code>), executes it's
494
     * drawn.</p>
495
     *
496
     * <p>
497
     * All nodes which could group with the composed layer <code>group</code>,
498
     * will be drawn together. And once the <code>
499
     * group</code> is drawn, will be set to <code>null</code> if hasn't a
500
     * parent layer.</p>
501
     *
502
     * <p>
503
     * The particular implementation depends on the kind of each layer and
504
     * composed layer. And this process can be cancelled at any time by the
505
     * shared object <code>cancel</code>.</p>
506
     *
507
     * <p>
508
     * According the print quality, labels will be printed in different
509
     * resolution:
510
     * <ul>
511
     * <li><b>PrintQuality.DRAFT</b>: 72 dpi (dots per inch).</li>
512
     * <li><b>PrintQuality.NORMAL</b>: 300 dpi (dots per inch).</li>
513
     * <li><b>PrintQuality.HIGH</b>: 600 dpi (dots per inch).</li>
514
     * </ul>
515
     * </p>
516
     *
517
     * @param g for rendering 2-dimensional shapes, text and images on the
518
     * Java(tm) platform
519
     * @param viewPort the information for drawing the layers
520
     * @param cancel shared object that determines if this layer can continue
521
     * being drawn
522
     * @param scale the scale of the view. Must be between
523
     * {@linkplain FLayer#getMinScale()} and {@linkplain FLayer#getMaxScale()}.
524
     * @param properties properties that will be print
525
     * @param group a composed layer pending to paint; if this parameter is
526
     * <code>null</code>, the composed layer
527
     *
528
     * @return <code>null</code> if the layers in <code>group</code> had been
529
     * drawn or were <code>null</code>; otherwise, the <code>group</code>
530
     *
531
     * @see FLayer#print(Graphics2D, ViewPort, Cancellable, double,
532
     * PrintAttributes)
533
     *
534
     * @throws ReadDriverException if fails the driver reading the data.
535
     */
536
    public ComposedLayer print_old(Graphics2D g, ViewPort viewPort, Cancellable cancel, double scale, PrintAttributes properties, ComposedLayer group)
537
            throws ReadException {
538
        double dpi = 72;
530 539

  
531
		int resolution = properties.getPrintQuality();
532
		if (resolution == PrintAttributes.PRINT_QUALITY_NORMAL) {
533
			dpi = 300;
534
		} else if (resolution == PrintAttributes.PRINT_QUALITY_HIGH){
535
			dpi = 600;
536
		} else if (resolution == PrintAttributes.PRINT_QUALITY_DRAFT){
537
			dpi = 72;
538
		}
540
        int resolution = properties.getPrintQuality();
541
        if (resolution == PrintAttributes.PRINT_QUALITY_NORMAL) {
542
            dpi = 300;
543
        } else if (resolution == PrintAttributes.PRINT_QUALITY_HIGH) {
544
            dpi = 600;
545
        } else if (resolution == PrintAttributes.PRINT_QUALITY_DRAFT) {
546
            dpi = 72;
547
        }
539 548

  
540 549
		// TODO: A la hora de imprimir, isWithinScale falla, porque est?
541
		// calculando la escala en pantalla, no para el layout.
542
		// Revisar esto.
550
        // calculando la escala en pantalla, no para el layout.
551
        // Revisar esto.
552
        // TODO: We have to check when we have to call the drawLabels method when exists a ComposedLayer group.
553
        for (int i = 0; i < layers.size(); i++) {
554
            FLayer lyr = (FLayer) layers.get(i);
555
            if (!lyr.isVisible() || !lyr.isWithinScale(scale)) {
556
                continue;
557
            }
543 558

  
544
		// TODO: We have to check when we have to call the drawLabels method when exists a ComposedLayer group.
545
		for (int i=0; i < layers.size(); i++) {
546
			FLayer lyr = (FLayer) layers.get(i);
547
			if (!lyr.isVisible() || !lyr.isWithinScale(scale)) {
548
				continue;
549
			}
559
            try {
550 560

  
551
			try{
552

  
553 561
				///// CHEMA ComposedLayer
554
				// Checks for draw group (ComposedLayer)
555
				if (group != null) {
556
					if (lyr instanceof FLayers){
557
						group = ((FLayers)lyr).print_old(g, viewPort, cancel,scale,properties,group);
558
					} else {
559
						// If layer can be added to the group, does it
560
						if (lyr instanceof ILabelable
561
								&& ((ILabelable) lyr).isLabeled()
562
								&& ((ILabelable) lyr).getLabelingStrategy() != null
563
								&& ((ILabelable) lyr).getLabelingStrategy().shouldDrawLabels(scale)) {
564
							group.add(lyr);
565
						} else {
566
							// draw the 'pending to draw' layer group
567
							group.print(g,viewPort,cancel,scale,properties);
562
                // Checks for draw group (ComposedLayer)
563
                if (group != null) {
564
                    if (lyr instanceof FLayers) {
565
                        group = ((FLayers) lyr).print_old(g, viewPort, cancel, scale, properties, group);
566
                    } else {
567
                        // If layer can be added to the group, does it
568
                        if (lyr instanceof ILabelable
569
                                && ((ILabelable) lyr).isLabeled()
570
                                && ((ILabelable) lyr).getLabelingStrategy() != null
571
                                && ((ILabelable) lyr).getLabelingStrategy().shouldDrawLabels(scale)) {
572
                            group.add(lyr);
573
                        } else {
574
                            // draw the 'pending to draw' layer group
575
                            group.print(g, viewPort, cancel, scale, properties);
568 576

  
569
							// gets a new group instance
570
							if (lyr instanceof ILabelable
571
									&& ((ILabelable) lyr).isLabeled()
572
									&& ((ILabelable) lyr).getLabelingStrategy() != null
573
									&& ((ILabelable) lyr).getLabelingStrategy().shouldDrawLabels(scale)) {
574
								group = lyr.newComposedLayer();
575
							} else {
576
								group = null;
577
							}
578
							// if layer hasn't group, draws it inmediately
579
							if (group == null) {
580
								if (lyr instanceof FLayers){
581
									group = ((FLayers)lyr).print_old(g, viewPort, cancel,scale,properties,group);
582
								} else {
583
									lyr.print(g, viewPort, cancel,scale,properties);
584
									if (lyr instanceof ILabelable
585
											&& ((ILabelable) lyr).isLabeled()
586
											&& ((ILabelable) lyr).getLabelingStrategy() != null
587
											&& ((ILabelable) lyr).getLabelingStrategy().shouldDrawLabels(scale)) {
588
										ILabelable lLayer = (ILabelable) lyr;
589
										lLayer.drawLabels(null, g, viewPort, cancel, scale, dpi);
590
									}
591
								}
592
							} else {
593
								// add the layer to the group
594
								group.setMapContext(fmap);
595
								group.add(lyr);
577
                            // gets a new group instance
578
                            if (lyr instanceof ILabelable
579
                                    && ((ILabelable) lyr).isLabeled()
580
                                    && ((ILabelable) lyr).getLabelingStrategy() != null
581
                                    && ((ILabelable) lyr).getLabelingStrategy().shouldDrawLabels(scale)) {
582
                                group = lyr.newComposedLayer();
583
                            } else {
584
                                group = null;
585
                            }
586
                            // if layer hasn't group, draws it inmediately
587
                            if (group == null) {
588
                                if (lyr instanceof FLayers) {
589
                                    group = ((FLayers) lyr).print_old(g, viewPort, cancel, scale, properties, group);
590
                                } else {
591
                                    lyr.print(g, viewPort, cancel, scale, properties);
592
                                    if (lyr instanceof ILabelable
593
                                            && ((ILabelable) lyr).isLabeled()
594
                                            && ((ILabelable) lyr).getLabelingStrategy() != null
595
                                            && ((ILabelable) lyr).getLabelingStrategy().shouldDrawLabels(scale)) {
596
                                        ILabelable lLayer = (ILabelable) lyr;
597
                                        lLayer.drawLabels(null, g, viewPort, cancel, scale, dpi);
598
                                    }
599
                                }
600
                            } else {
601
                                // add the layer to the group
602
                                group.setMapContext(fmap);
603
                                group.add(lyr);
596 604

  
597
							}
605
                            }
598 606

  
599
						}
600
					}
601
				} else {
602
					// gets a new group instance
603
					group = lyr.newComposedLayer();
604
					// if layer hasn't group, draws it inmediately
605
					if (group == null) {
606
						if (lyr instanceof FLayers){
607
							group = ((FLayers)lyr).print_old(g, viewPort, cancel,scale,properties,group);
608
						} else {
609
							lyr.print(g, viewPort, cancel,scale,properties);
610
							if (lyr instanceof ILabelable && ((ILabelable) lyr).isLabeled()) {
611
								ILabelable lLayer = (ILabelable) lyr;
607
                        }
608
                    }
609
                } else {
610
                    // gets a new group instance
611
                    group = lyr.newComposedLayer();
612
                    // if layer hasn't group, draws it inmediately
613
                    if (group == null) {
614
                        if (lyr instanceof FLayers) {
615
                            group = ((FLayers) lyr).print_old(g, viewPort, cancel, scale, properties, group);
616
                        } else {
617
                            lyr.print(g, viewPort, cancel, scale, properties);
618
                            if (lyr instanceof ILabelable && ((ILabelable) lyr).isLabeled()) {
619
                                ILabelable lLayer = (ILabelable) lyr;
612 620

  
613
								lLayer.drawLabels(null, g, viewPort, cancel, scale, dpi);
614
							}
615
						}
616
					} else {
617
						// add the layer to the group
618
						group.setMapContext(fmap);
619
						group.add(lyr);
621
                                lLayer.drawLabels(null, g, viewPort, cancel, scale, dpi);
622
                            }
623
                        }
624
                    } else {
625
                        // add the layer to the group
626
                        group.setMapContext(fmap);
627
                        group.add(lyr);
620 628

  
621
					}
622
				}
623
				///// CHEMA ComposedLayer
629
                    }
630
                }
631
                ///// CHEMA ComposedLayer
624 632

  
625
			} catch (Exception e){
626
				String mesg = Messages.getString("error_printing_layer")+" "+ lyr.getName() + ": " + e.getMessage();
627
				fmap.addLayerError(mesg);
628
				logger.error(mesg, e);
629
			}
633
            } catch (Exception e) {
634
                String mesg = Messages.getString("error_printing_layer") + " " + lyr.getName() + ": " + e.getMessage();
635
                fmap.addLayerError(mesg);
636
                logger.error(mesg, e);
637
            }
630 638

  
631
		}
639
        }
632 640

  
633
		///// CHEMA ComposedLayer
634
		if (group != null && this.getParentLayer() == null) {
635
			//si tenemos un grupo pendiente de pintar, pintamos
636
			group.print(g, viewPort, cancel,scale,properties);
637
			group = null;
641
        ///// CHEMA ComposedLayer
642
        if (group != null && this.getParentLayer() == null) {
643
            //si tenemos un grupo pendiente de pintar, pintamos
644
            group.print(g, viewPort, cancel, scale, properties);
645
            group = null;
638 646

  
639
		}
647
        }
640 648
		///// CHEMA ComposedLayer
641 649

  
642 650
		//		if (getVirtualLayers() != null) {
643
		//			getVirtualLayers().print( g, viewPort, cancel, scale, properties);
644
		//		}
651
        //			getVirtualLayers().print( g, viewPort, cancel, scale, properties);
652
        //		}
653
        ///// CHEMA ComposedLayer
654
        return group;
655
        ///// CHEMA ComposedLayer
656
    }
645 657

  
646
		///// CHEMA ComposedLayer
647
		return group;
648
		///// CHEMA ComposedLayer
649
	}
658
    /*
659
     * (non-Javadoc)
660
     * @see com.iver.cit.gvsig.fmap.layers.FLayer#getFullExtent()
661
     */
662
    public Envelope getFullEnvelope() {
663
        Envelope rAux = null;
664
        boolean first = true;
650 665

  
651
	/*
652
	 * (non-Javadoc)
653
	 * @see com.iver.cit.gvsig.fmap.layers.FLayer#getFullExtent()
654
	 */
655
	public Envelope getFullEnvelope() {
656
		Envelope rAux = null;
657
		boolean first = true;
666
        for (Iterator iter = layers.iterator(); iter.hasNext();) {
667
            FLayer capa = (FLayer) iter.next();
668
            try {
669
                if (first) {
670
                    rAux = (Envelope) capa.getFullEnvelope().clone();
671
                    first = false;
672
                } else {
673
                    rAux.add(capa.getFullEnvelope());
674
                }
675
            } catch (Exception e) {
676
                e.printStackTrace();//TODO hay que revisar para determinar el comportamiento que espera el usuario.
677
            }
678
        }
658 679

  
680
        return rAux;
681
    }
659 682

  
660
		for (Iterator iter = layers.iterator(); iter.hasNext();) {
661
			FLayer capa = (FLayer) iter.next();
662
			try{
663
				if (first) {
664
					rAux = (Envelope)capa.getFullEnvelope().clone();
665
					first=false;
666
				} else {
667
					rAux.add(capa.getFullEnvelope());
668
				}
669
			}catch (Exception e) {
670
				e.printStackTrace();//TODO hay que revisar para determinar el comportamiento que espera el usuario.
671
			}
672
		}
683
    /**
684
     * Notifies all listeners associated to this collection of layers, that
685
     * another layer is going to be added or replaced in the internal list of
686
     * layers.
687
     *
688
     * @param e a layer collection event with the new layer
689
     */
690
    protected void callLayerAdding(LayerCollectionEvent event)
691
            throws CancelationException {
692
        ArrayList aux = (ArrayList) layerCollectionListeners.clone();
693
        for (Iterator iter = aux.iterator(); iter.hasNext();) {
694
            ((LayerCollectionListener) iter.next()).layerAdding(event);
695
        }
696
    }
673 697

  
674
		return rAux;
675
	}
698
    /**
699
     * Notifies all listeners associated to this collection of layers, that a
700
     * layer is going to be removed from the internal list of layers.
701
     *
702
     * @param event a layer collection event with the layer being removed
703
     *
704
     * @throws CancelationException any exception produced during the
705
     * cancellation of the driver.
706
     */
707
    protected void callLayerRemoving(LayerCollectionEvent event)
708
            throws CancelationException {
709
        ArrayList aux = (ArrayList) layerCollectionListeners.clone();
710
        for (Iterator iter = aux.iterator(); iter.hasNext();) {
711
            ((LayerCollectionListener) iter.next()).layerRemoving(event);
712
        }
713
    }
676 714

  
677
	/**
678
	 * Notifies all listeners associated to this collection of layers,
679
	 *  that another layer is going to be added or replaced in the internal
680
	 *  list of layers.
681
	 *
682
	 * @param e a layer collection event with the new layer
683
	 */
684
	protected void callLayerAdding(LayerCollectionEvent event)
685
	throws CancelationException {
686
		ArrayList aux = (ArrayList) layerCollectionListeners.clone();
687
		for (Iterator iter = aux.iterator(); iter.hasNext();) {
688
			((LayerCollectionListener) iter.next()).layerAdding(event);
689
		}
690
	}
715
    /**
716
     * Notifies all listeners associated to this collection of layers, that a
717
     * layer is going to be moved in the internal list of layers.
718
     *
719
     * @param event a layer collection event with the layer being moved, and the
720
     * initial and final positions
721
     *
722
     * @throws CancelationException any exception produced during the
723
     * cancellation of the driver.
724
     */
725
    protected void callLayerMoving(LayerPositionEvent event)
726
            throws CancelationException {
727
        ArrayList aux = (ArrayList) layerCollectionListeners.clone();
728
        for (Iterator iter = aux.iterator(); iter.hasNext();) {
729
            ((LayerCollectionListener) iter.next()).layerMoving(event);
730
        }
731
    }
691 732

  
692
	/**
693
	 * Notifies all listeners associated to this collection of layers,
694
	 *  that a layer is going to be removed from the internal list of layers.
695
	 *
696
	 * @param event a layer collection event with the layer being removed
697
	 *
698
	 * @throws CancelationException any exception produced during the cancellation of the driver.
699
	 */
700
	protected void callLayerRemoving(LayerCollectionEvent event)
701
	throws CancelationException {
702
		ArrayList aux = (ArrayList) layerCollectionListeners.clone();
703
		for (Iterator iter = aux.iterator(); iter.hasNext();) {
704
			((LayerCollectionListener) iter.next()).layerRemoving(event);
705
		}
706
	}
733
    /**
734
     * Notifies all listeners associated to this collection of layers, that
735
     * another layer has been added or replaced in the internal list of layers.
736
     *
737
     * @param e a layer collection event with the new layer
738
     */
739
    protected void callLayerAdded(LayerCollectionEvent event) {
740
        ArrayList aux = (ArrayList) layerCollectionListeners.clone();
741
        for (Iterator iter = aux.iterator(); iter.hasNext();) {
742
            ((LayerCollectionListener) iter.next()).layerAdded(event);
743
        }
744
    }
707 745

  
708
	/**
709
	 * Notifies all listeners associated to this collection of layers,
710
	 *  that a layer is going to be moved in the internal list of layers.
711
	 *
712
	 * @param event a layer collection event with the layer being moved, and the initial and final positions
713
	 *
714
	 * @throws CancelationException any exception produced during the cancellation of the driver.
715
	 */
716
	protected void callLayerMoving(LayerPositionEvent event)
717
	throws CancelationException {
718
		ArrayList aux = (ArrayList) layerCollectionListeners.clone();
719
		for (Iterator iter = aux.iterator(); iter.hasNext();) {
720
			((LayerCollectionListener) iter.next()).layerMoving(event);
721
		}
722
	}
746
    /**
747
     * Notifies all listeners associated to this collection of layers, that
748
     * another layer has been removed from the internal list of layers.
749
     *
750
     * @param e a layer collection event with the layer removed
751
     */
752
    protected void callLayerRemoved(LayerCollectionEvent event) {
753
        ArrayList aux = (ArrayList) layerCollectionListeners.clone();
754
        for (Iterator iter = aux.iterator(); iter.hasNext();) {
755
            ((LayerCollectionListener) iter.next()).layerRemoved(event);
756
        }
757
    }
723 758

  
724
	/**
725
	 * Notifies all listeners associated to this collection of layers,
726
	 *  that another layer has been added or replaced in the internal
727
	 *  list of layers.
728
	 *
729
	 * @param e a layer collection event with the new layer
730
	 */
731
	protected void callLayerAdded(LayerCollectionEvent event) {
732
		ArrayList aux = (ArrayList) layerCollectionListeners.clone();
733
		for (Iterator iter = aux.iterator(); iter.hasNext();) {
734
			((LayerCollectionListener) iter.next()).layerAdded(event);
735
		}
736
	}
759
    /**
760
     * Notifies all listeners associated to this collection of layers, that
761
     * another layer has been moved in the internal list of layers.
762
     *
763
     * @param e a layer collection event with the layer moved, and the initial
764
     * and final positions
765
     */
766
    protected void callLayerMoved(LayerPositionEvent event) {
767
        ArrayList aux = (ArrayList) layerCollectionListeners.clone();
768
        for (Iterator iter = aux.iterator(); iter.hasNext();) {
769
            ((LayerCollectionListener) iter.next()).layerMoved(event);
770
        }
771
    }
737 772

  
738
	/**
739
	 * Notifies all listeners associated to this collection of layers,
740
	 *  that another layer has been removed from the internal list of layers.
741
	 *
742
	 * @param e a layer collection event with the layer removed
743
	 */
744
	protected void callLayerRemoved(LayerCollectionEvent event) {
745
		ArrayList aux = (ArrayList) layerCollectionListeners.clone();
746
		for (Iterator iter = aux.iterator(); iter.hasNext();) {
747
			((LayerCollectionListener) iter.next()).layerRemoved(event);
748
		}
749
	}
773
    public void saveToState(PersistentState state) throws PersistenceException {
750 774

  
751
	/**
752
	 * Notifies all listeners associated to this collection of layers,
753
	 *  that another layer has been moved in the internal list of layers.
754
	 *
755
	 * @param e a layer collection event with the layer moved, and the initial and final positions
756
	 */
757
	protected void callLayerMoved(LayerPositionEvent event) {
758
		ArrayList aux = (ArrayList) layerCollectionListeners.clone();
759
		for (Iterator iter = aux.iterator(); iter.hasNext();) {
760
			((LayerCollectionListener) iter.next()).layerMoved(event);
761
		}
762
	}
775
        super.saveToState(state);
763 776

  
764
	public void saveToState(PersistentState state) throws PersistenceException {
765
		
766
		super.saveToState(state);
767
		
768
		state.set("mapContext", fmap);
769
                
770
                List layersToSave = new ArrayList();
771
		Iterator iter = this.layers.iterator();
772
		while (iter.hasNext()) {
773
			FLayer layer = (FLayer) iter.next();
774
                        if( !layer.isTemporary() ) {
775
                            layersToSave.add(layer); 
776
                        }
777
		}                
778
		state.set("layers", layersToSave);
779
	}
777
        state.set("mapContext", fmap);
780 778

  
781
	public void loadFromState(PersistentState state) throws PersistenceException {
779
        List layersToSave = new ArrayList();
780
        Iterator iter = this.layers.iterator();
781
        while (iter.hasNext()) {
782
            FLayer layer = (FLayer) iter.next();
783
            if (!layer.isTemporary()) {
784
                layersToSave.add(layer);
785
            }
786
        }
787
        state.set("layers", layersToSave);
788
    }
782 789

  
783
		super.loadFromState(state);
790
    public void loadFromState(PersistentState state) throws PersistenceException {
784 791

  
785
		setMapContext((MapContext) state.get("mapContext"));
786
		Iterator iter = state.getIterator("layers");
787
		while (iter.hasNext()) {
788
			FLayer item = (FLayer) iter.next();
789
			layers.add(item); 
790
		}
791
	}
792
	
793
	/*
794
	 * (non-Javadoc)
795
	 * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#getMapContext()
796
	 */
797
	public MapContext getMapContext() {
798
		return fmap;
799
	}
792
        super.loadFromState(state);
800 793

  
801
	/*
802
	 * I don't think this implementation makes any sense.
803
	 * We can group layers with different transformations,
804
	 * we cannot set the ICoordTrans for all (?)
805
	 * 
806
	public void setCoordTrans(ICoordTrans ct) {
807
		super.setCoordTrans(ct);
794
        setMapContext((MapContext) state.get("mapContext"));
795
        Iterator iter = state.getIterator("layers");
796
        while (iter.hasNext()) {
797
            FLayer item = (FLayer) iter.next();
798
            layers.add(item);
799
        }
800
    }
808 801

  
809
		for (Iterator iter = layers.iterator(); iter.hasNext();) {
810
			FLayer layer = (FLayer) iter.next();
811
			layer.setCoordTrans(ct);
812
		}
813
	}
814
	*/
815
	
816
	/*
817
	 * (non-Javadoc)
818
	 * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#setAllActives(boolean)
819
	 */
820
	public void setAllActives(boolean active) {
821
		FLayer lyr;
802
    /*
803
     * (non-Javadoc)
804
     * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#getMapContext()
805
     */
806
    public MapContext getMapContext() {
807
        return fmap;
808
    }
822 809

  
823
		for (int i = 0; i < layers.size(); i++) {
824
			lyr = ((FLayer) layers.get(i));
825
			lyr.setActive(active);
810
    /*
811
     * I don't think this implementation makes any sense.
812
     * We can group layers with different transformations,
813
     * we cannot set the ICoordTrans for all (?)
814
     * 
815
     public void setCoordTrans(ICoordTrans ct) {
816
     super.setCoordTrans(ct);
826 817

  
827
			if (lyr instanceof LayerCollection) {
828
				((LayerCollection) lyr).setAllActives(active);
829
			}
830
		}
831
	}
818
     for (Iterator iter = layers.iterator(); iter.hasNext();) {
819
     FLayer layer = (FLayer) iter.next();
820
     layer.setCoordTrans(ct);
821
     }
822
     }
823
     */
824
    /*
825
     * (non-Javadoc)
826
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#setAllActives(boolean)
827
     */
828
    public void setAllActives(boolean active) {
829
        FLayer lyr;
832 830

  
833
	/*
834
	 * (non-Javadoc)
835
	 * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#getActives()
836
	 */
837
	public FLayer[] getActives() {
838
		ArrayList ret = new ArrayList();
839
		LayersIterator it = new LayersIterator(this) {
831
        for (int i = 0; i < layers.size(); i++) {
832
            lyr = ((FLayer) layers.get(i));
833
            lyr.setActive(active);
840 834

  
841
			public boolean evaluate(FLayer layer) {
842
				return layer.isActive();
843
			}
835
            if (lyr instanceof LayerCollection) {
836
                ((LayerCollection) lyr).setAllActives(active);
837
            }
838
        }
839
    }
844 840

  
845
		};
841
    /*
842
     * (non-Javadoc)
843
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.LayerCollection#getActives()
844
     */
845
    public FLayer[] getActives() {
846
        ArrayList ret = new ArrayList();
847
        LayersIterator it = new LayersIterator(this) {
846 848

  
847
		while (it.hasNext())
848
		{
849
			ret.add(it.next());
850
		}
851
		return (FLayer[]) ret.toArray(new FLayer[0]);
852
	}
849
            public boolean evaluate(FLayer layer) {
850
                return layer.isActive();
851
            }
853 852

  
854
	/*
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff