Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / FMap.java @ 1034

History | View | Annotate | Download (16.3 KB)

1
package com.iver.cit.gvsig.fmap;
2

    
3
import com.iver.cit.gvsig.fmap.core.IGeometry;
4
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
5
import com.iver.cit.gvsig.fmap.layers.CancelationException;
6
import com.iver.cit.gvsig.fmap.layers.FLayer;
7
import com.iver.cit.gvsig.fmap.layers.FLayers;
8
import com.iver.cit.gvsig.fmap.layers.LayerChangeSupport;
9
import com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent;
10
import com.iver.cit.gvsig.fmap.layers.LayerCollectionListener;
11
import com.iver.cit.gvsig.fmap.layers.LayerPositionEvent;
12
import com.iver.cit.gvsig.fmap.layers.LegendListener;
13
import com.iver.cit.gvsig.fmap.layers.VectorialAdapter;
14
import com.iver.cit.gvsig.fmap.layers.XMLException;
15
import com.iver.cit.gvsig.fmap.layers.layerOperations.Classifiable;
16
import com.iver.cit.gvsig.fmap.layers.layerOperations.Selectable;
17
import com.iver.cit.gvsig.fmap.operations.Cancellable;
18
import com.iver.cit.gvsig.fmap.operations.selection.Record;
19
import com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor;
20
import com.iver.cit.gvsig.fmap.operations.strategies.SelectByPointVisitor;
21
import com.iver.cit.gvsig.fmap.operations.strategies.SelectByRectVisitor;
22
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException;
23
import com.iver.cit.gvsig.fmap.rendering.LegendChangedEvent;
24
import com.iver.cit.gvsig.fmap.rendering.styling.FStyle2D;
25

    
26
import com.iver.utiles.XMLEntity;
27

    
28
import org.cresques.cts.ICoordTrans;
29
import org.cresques.cts.IProjection;
30

    
31
import org.cresques.geo.Projected;
32

    
33
import java.awt.Graphics2D;
34
import java.awt.RenderingHints;
35
import java.awt.Toolkit;
36
import java.awt.geom.Point2D;
37
import java.awt.geom.Rectangle2D;
38
import java.awt.image.BufferedImage;
39

    
40
import java.util.ArrayList;
41

    
42

    
43
/**
44
 * Modelo del mapa.
45
 *
46
 * @author Fernando Gonz?lez Cort?s
47
 */
48
public class FMap implements Projected {
49
        public static final double[] CHANGEM = {
50
                        1000, 1, 0.01, 0.001, 1609.344, 0.9144, 0.3048, 0.0254
51
                };
52
        public static final double[] CHANGE = {
53
                        100000, 100, 1, 0.1, 160934.4, 91.44, 30.48, 2.54
54
                };
55
        public static final int EQUALS = 0;
56
        public static final int DISJOINT = 1;
57
        public static final int INTERSECTS = 2;
58
        public static final int TOUCHES = 3;
59
        public static final int CROSSES = 4;
60
        public static final int WITHIN = 5;
61
        public static final int CONTAINS = 6;
62
        public static final int OVERLAPS = 7;
63
        private FLayers layers = new FLayers(this, null);
64
        private ViewPort viewPort;
65
        private ArrayList invalidationListeners = new ArrayList();
66
        private LayerChangeSupport layerChangeSupport = new LayerChangeSupport();
67
        private EventBuffer eventBuffer = new EventBuffer();
68

    
69
        /**
70
         * Crea un nuevo FMap.
71
         *
72
         * @param vp ViewPort.
73
         */
74
        public FMap(ViewPort vp) {
75
                this.viewPort = vp;
76
                layers.addLayerCollectionListener(new LayerEventListener());
77
                layers.addLayerCollectionListener(eventBuffer);
78

    
79
                if (viewPort != null) {
80
                        viewPort.addExtentListener(eventBuffer);
81
                }
82
        }
83

    
84
        /**
85
         * Devuelve el EventBuffer.
86
         *
87
         * @return EventBuffer.
88
         */
89
        public EventBuffer getEventBuffer() {
90
                return eventBuffer;
91
        }
92

    
93
        /**
94
         * A?ade un LegendListener.
95
         *
96
         * @param listener LegendListener a a?adir.
97
         */
98
        public void addLayerListener(LegendListener listener) {
99
                layerChangeSupport.addLayerListener(listener);
100
        }
101

    
102
        /**
103
         * M?todo ejecutado cuando hay un cambio de leyenda que se quiera reflejar.
104
         *
105
         * @param e LegendChangedEvent.
106
         */
107
        public void callLegendChanged(LegendChangedEvent e) {
108
                layerChangeSupport.callLegendChanged(e);
109
        }
110

    
111
        /**
112
         * Borra un LegendListener.
113
         *
114
         * @param listener LegendListener a borrar.
115
         */
116
        public void removeLayerListener(LegendListener listener) {
117
                layerChangeSupport.removeLayerListener(listener);
118
        }
119

    
120
        /**
121
         * Devuelve las capas que contiene el mapa.
122
         *
123
         * @return Capas.
124
         */
125
        public FLayers getLayers() {
126
                return layers;
127
        }
128

    
129
        /**
130
         * Dibuja en la imagen que se pasa como par?metro el contenido de las capas
131
         * visibles del mapa y teniendo en cuenta los datos del ViewPort contenido
132
         * en este FMap
133
         *
134
         * @param b Imagen.
135
         */
136
        public void drawLabels(BufferedImage b) {
137
        }
138

    
139
        /**
140
         * Imprime el las capas que contiene el FMap sobre el Graphics2D que se
141
         * pasa como par?metro, normalmente es el Graphics de la impresora.
142
         *
143
         * @param g Graphics2D
144
         *
145
         * @throws DriverException
146
         */
147
        public void print(Graphics2D g) throws DriverException {
148
                layers.print(g, viewPort,
149
                        new Cancellable() {
150
                                /**
151
                                 * @see com.iver.cit.gvsig.fmap.operations.Cancellable#isCanceled()
152
                                 */
153
                                public boolean isCanceled() {
154
                                        return false;
155
                                }
156
                        });
157
        }
158

    
159
        /**
160
         * Crea un nuevo FMap con la informaci?n del ViewPort que se pasa como
161
         * par?metro.
162
         *
163
         * @param vp ViewPort.
164
         *
165
         * @return FMap nuevo.
166
         */
167
        public FMap createNewFMap(ViewPort vp) {
168
                FMap ret = new FMap(vp);
169
                ret.layers = this.layers;
170

    
171
                return ret;
172
        }
173

    
174
        /**
175
         * Crea un nuevo FMap totalmente desligado, se replican las capas y el
176
         * ViewPort
177
         *
178
         * @return FMap clonado.
179
         *
180
         * @throws XMLException
181
         * @throws DriverException
182
         * @throws DriverIOException
183
         */
184
        public FMap cloneFMap()
185
                throws XMLException, DriverException, DriverIOException {
186
                return createFromXML(getXMLEntity());
187
        }
188

    
189
        /**
190
         * A?ade la capa que se pasa como par?metro al nodo que se pasa como
191
         * parametro y lanza ProjectionMismatchException si no est?n todas las
192
         * capas de este FMap en la misma proyecci?n. Lanza un
193
         * ChildNotAllowedException si la capa no es un FLayers y no permite hijos
194
         *
195
         * @param vectorial DOCUMENT ME!
196
         */
197

    
198
        /*        public void addLayer(LayerPath parent, FLayer layer)
199
           throws ProjectionMismatchException, ChildrenNotAllowedException {
200
           layers.addLayer(parent, layer);
201
           }
202
           public void removeLayer(LayerPath parent)throws ChildrenNotAllowedException{
203
                   layers.removeLayer(parent);
204
           }
205
         */
206

    
207
        /**
208
         * A?ade una capa al grupo de capas que se sit?a por encima de todas las
209
         * otras capas
210
         *
211
         * @param vectorial FLayer.
212
         */
213
        public void addToTrackLayer(FLayer vectorial) {
214
        }
215

    
216
        /**
217
         * Devuelve la escala de la vista en pantalla.
218
         *
219
         * @return escala de la vista.
220
         */
221
        public long getScaleView() {
222
                Toolkit kit = Toolkit.getDefaultToolkit();
223
                double dpi = kit.getScreenResolution();
224
                IProjection proj = viewPort.getProjection();
225

    
226
                double w = ((viewPort.getImageSize().getWidth() / dpi) * 2.54);
227

    
228
                if (viewPort.getAdjustedExtent() == null) {
229
                        return 0;
230
                }
231

    
232
                if (proj == null) {
233
                        return (long) (viewPort.getAdjustedExtent().getWidth() / w * CHANGE[getViewPort()
234
                                                                                                                                                                        .getMapUnits()]);
235
                }
236

    
237
                return (long) proj.getScale(viewPort.getAdjustedExtent().getMinX(),
238
                        viewPort.getAdjustedExtent().getMaxX(),
239
                        viewPort.getImageSize().getWidth(), dpi);
240
        }
241

    
242
        /**
243
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#setVectorial(com.iver.cit.gvsig.fmap.VectorialAdapter)
244
         */
245
        public void setVectorial(VectorialAdapter v) {
246
        }
247

    
248
        /**
249
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#process(com.iver.cit.gvsig.fmap.FeatureSelectorVisitor,
250
         *                 VectorialSubSet)
251
         */
252
        public void process(FeatureVisitor visitor) {
253
        }
254

    
255
        /**
256
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#processSelected(com.iver.cit.gvsig.fmap.FeatureVisitor)
257
         */
258
        public void processSelected(FeatureVisitor visitor) {
259
        }
260

    
261
        /**
262
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#select(com.iver.cit.gvsig.fmap.FeatureSelectorVisitor,
263
         *                 VectorialSubSet)
264
         */
265
        public void select(FeatureVisitor visitor) {
266
        }
267

    
268
        /**
269
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#selectFromSelection()
270
         */
271
        public void selectFromSelection() {
272
        }
273

    
274
        /**
275
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#createIndex()
276
         */
277
        public void createIndex() {
278
        }
279

    
280
        /**
281
         * @see org.cresques.geo.Projected#getProjection()
282
         */
283
        public IProjection getProjection() {
284
                return getViewPort().getProjection();
285
        }
286

    
287
        /**
288
         * Inserta la proyecci?n.
289
         *
290
         * @param proj Proyecci?n.
291
         */
292
        public void setProjection(IProjection proj) {
293
                getViewPort().setProjection(proj);
294
        }
295

    
296
        /**
297
         * @see org.cresques.geo.Projected#reProject(org.cresques.cts.ICoordTrans)
298
         */
299
        public void reProject(ICoordTrans arg0) {
300
                // TODO implementar reprojecci?n (lo que sea eso)
301
        }
302

    
303
        /**
304
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#selectByPoint(java.awt.geom.Point2D,
305
         *                 double)
306
         */
307
        public void selectByPoint(Point2D p, double tolerance)
308
                throws DriverException {
309
                Point2D mapPoint = viewPort.toMapPoint((int) p.getX(), (int) p.getY());
310
                SelectByPointVisitor visitor = new SelectByPointVisitor();
311
                visitor.setQueriedPoint(mapPoint);
312
                visitor.setTolerance(getViewPort().toMapDistance(3));
313

    
314
                try {
315
                        layers.process(visitor);
316
                } catch (VisitException e) {
317
                        throw new RuntimeException("No se espera que SelectByPointVisitor lance esta excepci?n",
318
                                e);
319
                }
320
        }
321

    
322
        /**
323
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#selectByRect(java.awt.geom.Rectangle2D)
324
         */
325
        public void selectByRect(Rectangle2D rect) throws DriverException {
326
                SelectByRectVisitor visitor = new SelectByRectVisitor();
327
                visitor.setRect(rect);
328

    
329
                try {
330
                        layers.process(visitor);
331
                } catch (VisitException e) {
332
                        throw new RuntimeException("No se espera que SelectByRectVisitor lance esta excepci?n",
333
                                e);
334
                }
335
        }
336

    
337
        /**
338
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#selectByShape(com.iver.cit.gvsig.fmap.fshape.IGeometry,
339
         *                 int)
340
         */
341
        public void selectByShape(IGeometry g, int relationship) {
342
        }
343

    
344
        /**
345
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#queryByPoint(Point2D,
346
         *                 double)
347
         */
348
        public Record[] queryByPoint(Point2D p, double tolerance) {
349
                return null;
350
        }
351

    
352
        /**
353
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#queryByRect(java.awt.geom.Rectangle2D)
354
         */
355
        public Record[] queryByRect(Rectangle2D rect) {
356
                return null;
357
        }
358

    
359
        /**
360
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#queryByShape(com.iver.cit.gvsig.fmap.fshape.IGeometry,
361
         *                 int)
362
         */
363
        public Record[] queryByShape(IGeometry g, int relationship) {
364
                return null;
365
        }
366

    
367
        /**
368
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#getSelectionBounds()
369
         */
370
        public Rectangle2D getSelectionBounds() {
371
                return null;
372
        }
373

    
374
        /**
375
         * @see com.iver.cit.gvsig.fmap.operations.LayerOperations#draw(java.awt.image.BufferedImage,
376
         *                 java.awt.Graphics2D, FStyle2D)
377
         */
378
        public void draw(BufferedImage image, Graphics2D g, Cancellable cancel)
379
                throws DriverException {
380
                if (viewPort.getExtent() == null) {
381
                        // System.err.println("viewPort.getExtent() = null");
382
                        return;
383
                }
384

    
385
                g.setColor(viewPort.getBackColor());
386
                g.fillRect(0, 0, viewPort.getImageWidth(), viewPort.getImageHeight());
387

    
388
                // System.out.println("Viewport despues: " + viewPort); 
389
                /* if ((viewPort.getImageWidth() <=0) || (viewPort.getImageHeight() <= 0))
390
                   {
391
                           return;
392
                   } */
393
                // M?s c?lidad al texto
394
                g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
395
                        RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
396

    
397
                layers.draw(image, g, viewPort, cancel);
398

    
399
                /* g.setColor(Color.BLUE);
400
                   GeneralPath shpR = new GeneralPath(viewPort.getExtent());
401
                   shpR.transform(viewPort.getAffineTransform());
402
                   g.draw(shpR); */
403
        }
404

    
405
        /**
406
         * @see com.iver.cit.gvsig.fmap.operations.LayerOperations#draw(java.awt.image.BufferedImage,
407
         *                 java.awt.Graphics2D, FStyle2D)
408
         */
409
        public void draw(BufferedImage image, Graphics2D g)
410
                throws DriverException {
411
                layers.draw(image, g, viewPort,
412
                        new Cancellable() {
413
                                /**
414
                                 * @see com.iver.cit.gvsig.fmap.operations.Cancellable#isCanceled()
415
                                 */
416
                                public boolean isCanceled() {
417
                                        return false;
418
                                }
419
                        });
420
        }
421

    
422
        /**
423
         * Devuelve el ViewPort.
424
         *
425
         * @return Returns the viewPort.
426
         */
427
        public ViewPort getViewPort() {
428
                return viewPort;
429
        }
430

    
431
        /**
432
         * Inserta un ViewPort.
433
         *
434
         * @param viewPort The viewPort to set.
435
         */
436
        public void setViewPort(ViewPort viewPort) {
437
                if (this.viewPort != null) {
438
                        this.viewPort.removeExtentListener(eventBuffer);
439
                }
440

    
441
                this.viewPort = viewPort;
442
                viewPort.addExtentListener(eventBuffer);
443
        }
444

    
445
        /**
446
         * M?todo de conveniencia. Recorre las capas y te da el fullExtent
447
         *
448
         * @return fullExtent de todas las capas.
449
         *
450
         * @throws DriverException
451
         */
452
        public Rectangle2D getFullExtent() throws DriverException {
453
                return layers.getFullExtent();
454
        }
455

    
456
        /**
457
         * Devuelve el XMLEntity.
458
         *
459
         * @return XMLEntity.
460
         */
461
        public XMLEntity getXMLEntity() {
462
                XMLEntity xml = new XMLEntity();
463
                xml.addChild(viewPort.getXMLEntity());
464
                xml.addChild(layers.getXMLEntity());
465

    
466
                return xml;
467
        }
468

    
469
        /**
470
         * Crea un nuevo FMAp a partir del XMLEntity.
471
         *
472
         * @param xml XMLEntity
473
         *
474
         * @return Nuevo FMap.
475
         *
476
         * @throws XMLException
477
         * @throws DriverException
478
         * @throws DriverIOException
479
         */
480
        public static FMap createFromXML(XMLEntity xml)
481
                throws XMLException, DriverException, DriverIOException {
482
                ViewPort vp = ViewPort.createFromXML(xml.getChild(0));
483
                FMap fmap = new FMap(vp);
484
                fmap.layers.setXMLEntity(xml.getChild(1));
485

    
486
                return fmap;
487
        }
488

    
489
        /**
490
         * A?ade un AtomicEventListener.
491
         *
492
         * @param listener AtomicEventListener.
493
         *
494
         * @return True si se ha a?adido correctamente.
495
         */
496
        public boolean addAtomicEventListener(AtomicEventListener listener) {
497
                return eventBuffer.addAtomicEventListener(listener);
498
        }
499

    
500
        /**
501
         * Borra un AtomicEventListener de la lista de listeners.
502
         *
503
         * @param listener AtomicEventListener a borrar.
504
         *
505
         * @return True si se ha borrado correctamente.
506
         */
507
        public boolean removeAtomicEventListener(AtomicEventListener listener) {
508
                return eventBuffer.removeAtomicEventListener(listener);
509
        }
510

    
511
        /**
512
         * Inicializa los AtomicEvent.
513
         */
514
        public void beginAtomicEvent() {
515
                eventBuffer.beginAtomicEvent();
516
        }
517

    
518
        /**
519
         * Finaliza los AtomicEvent.
520
         */
521
        public void endAtomicEvent() {
522
                eventBuffer.endAtomicEvent();
523
        }
524

    
525
        /**
526
         * Evento Layer.
527
         *
528
         * @author Fernando Gonz?lez Cort?s
529
         */
530
        public class LayerEventListener implements LayerCollectionListener {
531
                /**
532
                 * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerAdded(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
533
                 */
534
                public void layerAdded(LayerCollectionEvent e) {
535
                        // Si es la primera capa, fijamos su extent al ViewPort
536
                        //if (getLayers().getLayersCount() == 1) {
537
                        if (getViewPort().getExtent() == null) {
538
                                FLayer lyr = e.getAffectedLayer();
539

    
540
                                try {
541
                                        getViewPort().setExtent(lyr.getFullExtent());
542
                                } catch (DriverException e1) {
543
                                }
544
                        }
545

    
546
                        //Registramos al FMap como listener del legend de las capas
547
                        FLayer lyr = e.getAffectedLayer();
548

    
549
                        lyr.addLayerListener(eventBuffer);
550

    
551
                        if (lyr instanceof Classifiable) {
552
                                Classifiable c = (Classifiable) lyr;
553
                                c.addLegendListener(eventBuffer);
554
                        }
555

    
556
                        if (lyr instanceof Selectable) {
557
                                Selectable s = (Selectable) lyr;
558
                                s.addSelectionListener(eventBuffer);
559
                        }
560
                }
561

    
562
                /**
563
                 * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerMoved(com.iver.cit.gvsig.fmap.layers.LayerPositionEvent)
564
                 */
565
                public void layerMoved(LayerPositionEvent e) {
566
                }
567

    
568
                /**
569
                 * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerRemoved(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
570
                 */
571
                public void layerRemoved(LayerCollectionEvent e) {
572
                        FLayer lyr = e.getAffectedLayer();
573

    
574
                        lyr.removeLayerListener(eventBuffer);
575

    
576
                        if (lyr instanceof Classifiable) {
577
                                Classifiable c = (Classifiable) lyr;
578
                                c.removeLegendListener(eventBuffer);
579
                        }
580

    
581
                        if (lyr instanceof Selectable) {
582
                                Selectable s = (Selectable) lyr;
583
                                s.addSelectionListener(eventBuffer);
584
                        }
585
                }
586

    
587
                /**
588
                 * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerAdding(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
589
                 */
590
                public void layerAdding(LayerCollectionEvent e)
591
                        throws CancelationException {
592
                }
593

    
594
                /**
595
                 * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerMoving(com.iver.cit.gvsig.fmap.layers.LayerPositionEvent)
596
                 */
597
                public void layerMoving(LayerPositionEvent e)
598
                        throws CancelationException {
599
                }
600

    
601
                /**
602
                 * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerRemoving(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
603
                 */
604
                public void layerRemoving(LayerCollectionEvent e)
605
                        throws CancelationException {
606
                }
607

    
608
                /**
609
                 * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#activationChanged(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
610
                 */
611
                public void activationChanged(LayerCollectionEvent e)
612
                        throws CancelationException {
613
                }
614

    
615
                /**
616
                 * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#visibilityChanged(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
617
                 */
618
                public void visibilityChanged(LayerCollectionEvent e)
619
                        throws CancelationException {
620
                }
621
        }
622
}