Statistics
| Revision:

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

History | View | Annotate | Download (16.1 KB)

1
/* Generated by Together */
2
package com.iver.cit.gvsig.fmap;
3

    
4
import java.awt.Graphics2D;
5
import java.awt.RenderingHints;
6
import java.awt.Toolkit;
7
import java.awt.geom.Point2D;
8
import java.awt.geom.Rectangle2D;
9
import java.awt.image.BufferedImage;
10
import java.util.ArrayList;
11

    
12
import org.cresques.cts.ICoordTrans;
13
import org.cresques.cts.IProjection;
14
import org.cresques.geo.Projected;
15

    
16
import com.hardcode.driverManager.DriverLoadException;
17
import com.iver.cit.gvsig.fmap.core.IGeometry;
18
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
19
import com.iver.cit.gvsig.fmap.layers.CancelationException;
20
import com.iver.cit.gvsig.fmap.layers.FLayer;
21
import com.iver.cit.gvsig.fmap.layers.FLayers;
22
import com.iver.cit.gvsig.fmap.layers.LayerChangeSupport;
23
import com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent;
24
import com.iver.cit.gvsig.fmap.layers.LayerCollectionListener;
25
import com.iver.cit.gvsig.fmap.layers.LayerPositionEvent;
26
import com.iver.cit.gvsig.fmap.layers.LegendListener;
27
import com.iver.cit.gvsig.fmap.layers.VectorialAdapter;
28
import com.iver.cit.gvsig.fmap.layers.XMLException;
29
import com.iver.cit.gvsig.fmap.layers.layerOperations.Classifiable;
30
import com.iver.cit.gvsig.fmap.layers.layerOperations.Selectable;
31
import com.iver.cit.gvsig.fmap.operations.Cancellable;
32
import com.iver.cit.gvsig.fmap.operations.selection.Record;
33
import com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor;
34
import com.iver.cit.gvsig.fmap.operations.strategies.SelectByPointVisitor;
35
import com.iver.cit.gvsig.fmap.operations.strategies.SelectByRectVisitor;
36
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException;
37
import com.iver.cit.gvsig.fmap.rendering.LegendChangedEvent;
38
import com.iver.cit.gvsig.fmap.rendering.styling.FStyle2D;
39
import com.iver.utiles.XMLEntity;
40

    
41

    
42
/**
43
 * DOCUMENT ME!
44
 *
45
 * @author Fernando Gonz?lez Cort?s
46
 */
47
public class FMap implements Projected {
48
        public static final double[] CHANGEM = {
49
                        1000, 1, 0.01, 0.001, 1609.344, 0.9144, 0.3048, 0.0254
50
                };
51

    
52
        /** DOCUMENT ME! */
53
        public static final double[] CHANGE = {
54
                        100000, 100, 1, 0.1, 160934.4, 91.44, 30.48, 2.54
55
                };
56
        public static final int EQUALS = 0;
57
        public static final int DISJOINT = 1;
58
        public static final int INTERSECTS = 2;
59
        public static final int TOUCHES = 3;
60
        public static final int CROSSES = 4;
61
        public static final int WITHIN = 5;
62
        public static final int CONTAINS = 6;
63
        public static final int OVERLAPS = 7;
64
        private FLayers layers = new FLayers(this, null);
65
        private ViewPort viewPort;
66
        private ArrayList invalidationListeners = new ArrayList();
67
        private LayerChangeSupport layerChangeSupport = new LayerChangeSupport();
68
        private EventBuffer eventBuffer = new EventBuffer();
69

    
70
        /**
71
         * Crea un nuevo FMap.
72
         *
73
         * @param vp DOCUMENT ME!
74
         */
75
        public FMap(ViewPort vp) {
76
                this.viewPort = vp;
77
                layers.addLayerCollectionListener(new LayerEventListener());
78
                layers.addLayerCollectionListener(eventBuffer);
79
                if (viewPort != null)
80
                viewPort.addExtentListener(eventBuffer);
81
        }
82
        public EventBuffer getEventBuffer(){
83
                return eventBuffer;
84
        }
85
        /**
86
         * DOCUMENT ME!
87
         *
88
         * @param listener DOCUMENT ME!
89
         */
90
        public void addLayerListener(LegendListener listener) {
91
                layerChangeSupport.addLayerListener(listener);
92
        }
93

    
94
        /**
95
         * DOCUMENT ME!
96
         *
97
         * @param e DOCUMENT ME!
98
         */
99
        public void callLegendChanged(LegendChangedEvent e) {
100
                layerChangeSupport.callLegendChanged(e);
101
        }
102

    
103
        /**
104
         * DOCUMENT ME!
105
         *
106
         * @param listener DOCUMENT ME!
107
         */
108
        public void removeLayerListener(LegendListener listener) {
109
                layerChangeSupport.removeLayerListener(listener);
110
        }
111

    
112
        /**
113
         * DOCUMENT ME!
114
         *
115
         * @return DOCUMENT ME!
116
         */
117
        public FLayers getLayers() {
118
                return layers;
119
        }
120

    
121
        /**
122
         * Dibuja en la imagen que se pasa como par?metro el contenido de las capas
123
         * visibles del mapa y teniendo en cuenta los datos del ViewPort contenido
124
         * en este FMap
125
         *
126
         * @param b DOCUMENT ME!
127
         */
128
        public void drawLabels(BufferedImage b) {
129
        }
130

    
131
        /**
132
         * DOCUMENT ME!
133
         *
134
         * @param g DOCUMENT ME!
135
         *
136
         * @throws DriverIOException
137
         * @throws DriverLoadException
138
         * @throws DriverException
139
         */
140
        public void print(Graphics2D g)
141
                throws DriverException {
142
                ///draw(null, g);
143
                layers.print(g, viewPort,
144
                                new Cancellable() {
145
                                        /**
146
                                         * @see com.iver.cit.gvsig.fmap.operations.Cancellable#isCanceled()
147
                                         */
148
                                        public boolean isCanceled() {
149
                                                return false;
150
                                        }
151
                                });
152
        }
153

    
154
        /**
155
         * Crea un nuevo FMap con la informaci?n del ViewPort que se pasa como
156
         * par?metro.
157
         *
158
         * @param vp DOCUMENT ME!
159
         *
160
         * @return DOCUMENT ME!
161
         */
162
        public FMap createNewFMap(ViewPort vp) {
163
                FMap ret = new FMap(vp);
164
                ret.layers = this.layers;
165

    
166
                return ret;
167
        }
168

    
169
        /**
170
         * Crea un nuevo FMap totalmente desligado, se replican las capas y el
171
         * ViewPort
172
         *
173
         * @return DOCUMENT ME!
174
         * @throws DriverException
175
         * @throws XMLException
176
         */
177
        public FMap cloneFMap() throws XMLException, DriverException {
178
                return createFromXML(getXMLEntity());
179
        }
180

    
181
        
182
        /**
183
         * A?ade la capa que se pasa como par?metro al nodo que se pasa como
184
         * parametro y lanza ProjectionMismatchException si no est?n todas las
185
         * capas de este FMap en la misma proyecci?n. Lanza un
186
         * ChildNotAllowedException si la capa no es un FLayers y no permite hijos
187
         *
188
         * @param vectorial DOCUMENT ME!
189
         */
190

    
191
        /*        public void addLayer(LayerPath parent, FLayer layer)
192
           throws ProjectionMismatchException, ChildrenNotAllowedException {
193
           layers.addLayer(parent, layer);
194
           }
195
           public void removeLayer(LayerPath parent)throws ChildrenNotAllowedException{
196
                   layers.removeLayer(parent);
197
           }
198
         */
199

    
200
        /**
201
         * A?ade una capa al grupo de capas que se sit?a por encima de todas las
202
         * otras capas
203
         *
204
         * @param vectorial DOCUMENT ME!
205
         */
206
        public void addToTrackLayer(FLayer vectorial) {
207
        }
208

    
209
        /**
210
         * DOCUMENT ME!
211
         *
212
         * @return DOCUMENT ME!
213
         */
214
        public long getScaleView() {
215
                Toolkit kit = Toolkit.getDefaultToolkit();
216
                double dpi = kit.getScreenResolution();
217
                IProjection proj = viewPort.getProjection();
218

    
219
                double w = ((viewPort.getImageSize().getWidth() / dpi) * 2.54);
220

    
221
                if (viewPort.getAdjustedExtent() == null) {
222
                        return 0;
223
                }
224

    
225
                if (proj == null) {
226
                        return (long) (viewPort.getAdjustedExtent().getWidth() / w * CHANGE[getViewPort()
227
                                                                                                                                                                        .getMapUnits()]);
228
                }
229

    
230
                return (long) proj.getScale(viewPort.getAdjustedExtent().getMinX(),
231
                        viewPort.getAdjustedExtent().getMaxX(),
232
                        viewPort.getImageSize().getWidth(), dpi);
233
        }
234

    
235
        /**
236
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#setVectorial(com.iver.cit.gvsig.fmap.VectorialAdapter)
237
         */
238
        public void setVectorial(VectorialAdapter v) {
239
        }
240

    
241
        /**
242
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#process(com.iver.cit.gvsig.fmap.FeatureSelectorVisitor,
243
         *                 VectorialSubSet)
244
         */
245
        public void process(FeatureVisitor visitor) {
246
        }
247

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

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

    
261
        /**
262
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#selectFromSelection()
263
         */
264
        public void selectFromSelection() {
265
        }
266

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

    
273
        /**
274
         * @see org.cresques.geo.Projected#getProjection()
275
         */
276
        public IProjection getProjection() {
277
                return getViewPort().getProjection();
278
        }
279
        public void setProjection(IProjection proj)
280
        {
281
                getViewPort().setProjection(proj);
282
        }
283

    
284
        
285
        /**
286
         * @see org.cresques.geo.Projected#reProject(org.cresques.cts.ICoordTrans)
287
         */
288
        public void reProject(ICoordTrans arg0) {
289
                // TODO implementar reprojecci?n (lo que sea eso)
290
        }
291

    
292
        /**
293
         * @throws DriverException
294
         * @throws VisitException
295
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#selectByPoint(java.awt.geom.Point2D,
296
         *                 double)
297
         */
298
        public void selectByPoint(Point2D p, double tolerance)
299
                throws DriverException {
300
                Point2D mapPoint = viewPort.toMapPoint((int) p.getX(), (int) p.getY());
301
                SelectByPointVisitor visitor = new SelectByPointVisitor();
302
                visitor.setQueriedPoint(mapPoint);
303
                visitor.setTolerance(getViewPort().toMapDistance(3));
304
                try {
305
                        layers.process(visitor);
306
                } catch (VisitException e) {
307
                        throw new RuntimeException("No se espera que SelectByPointVisitor lance esta excepci?n", e);
308
                }
309
        }
310

    
311
        /**
312
         * DOCUMENT ME!
313
         *
314
         * @param rect DOCUMENT ME!
315
         * @throws DriverException
316
         * @throws VisitException
317
         *
318
         * @throws DriverIOException
319
         *
320
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#selectByRect(java.awt.geom.Rectangle2D)
321
         */
322
        public void selectByRect(Rectangle2D rect) throws DriverException {
323
                SelectByRectVisitor visitor = new SelectByRectVisitor();
324
                visitor.setRect(rect);
325
                try {
326
                        layers.process(visitor);
327
                } catch (VisitException e) {
328
                        throw new RuntimeException("No se espera que SelectByRectVisitor lance esta excepci?n", e);
329
                }
330
        }
331

    
332
        /**
333
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#selectByShape(com.iver.cit.gvsig.fmap.fshape.IGeometry,
334
         *                 int)
335
         */
336
        public void selectByShape(IGeometry g, int relationship) {
337
        }
338

    
339
        /**
340
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#queryByPoint(Point2D,
341
         *                 double)
342
         */
343
        public Record[] queryByPoint(Point2D p, double tolerance) {
344
                return null;
345
        }
346

    
347
        /**
348
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#queryByRect(java.awt.geom.Rectangle2D)
349
         */
350
        public Record[] queryByRect(Rectangle2D rect) {
351
                return null;
352
        }
353

    
354
        /**
355
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#queryByShape(com.iver.cit.gvsig.fmap.fshape.IGeometry,
356
         *                 int)
357
         */
358
        public Record[] queryByShape(IGeometry g, int relationship) {
359
                return null;
360
        }
361

    
362
        /**
363
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#getSelectionBounds()
364
         */
365
        public Rectangle2D getSelectionBounds() {
366
                return null;
367
        }
368

    
369
        /**
370
         * DOCUMENT ME!
371
         *
372
         * @param image DOCUMENT ME!
373
         * @param g DOCUMENT ME!
374
         * @param cancel DOCUMENT ME!
375
         *
376
         * @throws DriverIOException
377
         * @throws DriverLoadException
378
         * @throws DriverException
379
         *
380
         * @see com.iver.cit.gvsig.fmap.operations.LayerOperations#draw(java.awt.image.BufferedImage,
381
         *                 java.awt.Graphics2D, FStyle2D)
382
         */
383
        public void draw(BufferedImage image, Graphics2D g, Cancellable cancel)
384
                throws DriverException {
385
                if (viewPort.getExtent() == null) {
386
                        // System.err.println("viewPort.getExtent() = null");
387
                        return;
388
                }
389
            g.setColor(viewPort.getBackColor());
390
            g.fillRect(0, 0, viewPort.getImageWidth(), viewPort.getImageHeight());
391
                
392
                // System.out.println("Viewport despues: " + viewPort); 
393
                /* if ((viewPort.getImageWidth() <=0) || (viewPort.getImageHeight() <= 0))
394
                {
395
                        return;
396
                } */
397
                // M?s c?lidad al texto
398
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
399
                RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
400

    
401
                layers.draw(image, g, viewPort, cancel);
402
                
403
                /* g.setColor(Color.BLUE);
404
                GeneralPath shpR = new GeneralPath(viewPort.getExtent());
405
                shpR.transform(viewPort.getAffineTransform());
406
                g.draw(shpR); */
407
        }
408

    
409
        /**
410
         * DOCUMENT ME!
411
         *
412
         * @param image DOCUMENT ME!
413
         * @param g DOCUMENT ME!
414
         *
415
         * @throws DriverIOException
416
         * @throws DriverLoadException
417
         * @throws DriverException
418
         *
419
         * @see com.iver.cit.gvsig.fmap.operations.LayerOperations#draw(java.awt.image.BufferedImage,
420
         *                 java.awt.Graphics2D, FStyle2D)
421
         */
422
        public void draw(BufferedImage image, Graphics2D g)
423
                throws DriverException {
424
                layers.draw(image, g, viewPort,
425
                        new Cancellable() {
426
                                /**
427
                                 * @see com.iver.cit.gvsig.fmap.operations.Cancellable#isCanceled()
428
                                 */
429
                                public boolean isCanceled() {
430
                                        return false;
431
                                }
432
                        });
433
        }
434

    
435
        /**
436
         * DOCUMENT ME!
437
         *
438
         * @return Returns the viewPort.
439
         */
440
        public ViewPort getViewPort() {
441
                return viewPort;
442
        }
443

    
444
        /**
445
         * DOCUMENT ME!
446
         *
447
         * @param viewPort The viewPort to set.
448
         */
449
        public void setViewPort(ViewPort viewPort) {
450
                if (this.viewPort != null)
451
                this.viewPort.removeExtentListener(eventBuffer);
452
                this.viewPort = viewPort;
453
                viewPort.addExtentListener(eventBuffer);
454
        }
455

    
456
        /**
457
         * M?todo de conveniencia. Recorre las capas y te da el fullExtent
458
         *
459
         * @return fullExtent de todas las capas.
460
         *
461
         * @throws DriverIOException
462
         */
463
        public Rectangle2D getFullExtent() throws DriverException {
464
                return layers.getFullExtent();
465
        }
466

    
467
        /**
468
         * DOCUMENT ME!
469
         *
470
         * @return DOCUMENT ME!
471
         * @throws DriverException
472
         */
473
        public XMLEntity getXMLEntity() {
474
                XMLEntity xml = new XMLEntity();
475
                xml.addChild(viewPort.getXMLEntity());
476
                xml.addChild(layers.getXMLEntity());
477

    
478
                return xml;
479
        }
480

    
481
        /**
482
         * DOCUMENT ME!
483
         *
484
         * @param xml DOCUMENT ME!
485
         *
486
         * @return DOCUMENT ME!
487
         *
488
         * @throws XMLException DOCUMENT ME!
489
         */
490
        public static FMap createFromXML(XMLEntity xml)
491
                throws XMLException, DriverException {
492
                ViewPort vp = ViewPort.createFromXML(xml.getChild(0));
493
                FMap fmap = new FMap(vp);
494
                fmap.layers.setXMLEntity(xml.getChild(1));
495
                return fmap;
496
        }
497

    
498
        /**
499
         * DOCUMENT ME!
500
         *
501
         * @author Fernando Gonz?lez Cort?s
502
         */
503
        public class LayerEventListener implements LayerCollectionListener {
504
                /**
505
                 * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerAdded(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
506
                 */
507
                public void layerAdded(LayerCollectionEvent e) {
508
                        // Si es la primera capa, fijamos su extent al ViewPort
509
                        //if (getLayers().getLayersCount() == 1) {
510
                        if (getViewPort().getExtent()==null){
511
                        FLayer lyr = e.getAffectedLayer();
512

    
513
                                try {
514
                                        getViewPort().setExtent(lyr.getFullExtent());
515
                                } catch (DriverException e1) {
516
                                }
517
                        }
518

    
519
                        //Registramos al FMap como listener del legend de las capas
520
                        FLayer lyr = e.getAffectedLayer();
521
                        
522
                        lyr.addLayerListener(eventBuffer);
523
                        
524
                        if (lyr instanceof Classifiable) {
525
                                Classifiable c = (Classifiable) lyr;
526
                                c.addLegendListener(eventBuffer);
527
                        }
528
                        
529
                        if (lyr instanceof Selectable) {
530
                                Selectable s = (Selectable) lyr;
531
                                s.addSelectionListener(eventBuffer);
532
                        }
533
                }
534

    
535
                /**
536
                 * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerMoved(com.iver.cit.gvsig.fmap.layers.LayerPositionEvent)
537
                 */
538
                public void layerMoved(LayerPositionEvent e) {
539
                }
540

    
541
                /**
542
                 * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerRemoved(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
543
                 */
544
                public void layerRemoved(LayerCollectionEvent e) {
545
                        FLayer lyr = e.getAffectedLayer();
546

    
547
                        lyr.removeLayerListener(eventBuffer);
548
                        
549
                        if (lyr instanceof Classifiable) {
550
                                Classifiable c = (Classifiable) lyr;
551
                                c.removeLegendListener(eventBuffer);
552
                        }
553

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

    
560
                /**
561
                 * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerAdding(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
562
                 */
563
                public void layerAdding(LayerCollectionEvent e)
564
                        throws CancelationException {
565
                }
566

    
567
                /**
568
                 * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerMoving(com.iver.cit.gvsig.fmap.layers.LayerPositionEvent)
569
                 */
570
                public void layerMoving(LayerPositionEvent e)
571
                        throws CancelationException {
572
                }
573

    
574
                /**
575
                 * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerRemoving(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
576
                 */
577
                public void layerRemoving(LayerCollectionEvent e)
578
                        throws CancelationException {
579
                }
580

    
581
                /**
582
                 * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#activationChanged(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
583
                 */
584
                public void activationChanged(LayerCollectionEvent e)
585
                        throws CancelationException {
586
                }
587

    
588
                /**
589
                 * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#visibilityChanged(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
590
                 */
591
                public void visibilityChanged(LayerCollectionEvent e)
592
                        throws CancelationException {
593
                }
594
        }
595
        public boolean addAtomicEventListener(AtomicEventListener listener) {
596
                return eventBuffer.addAtomicEventListener(listener);
597
        }
598
        public boolean removeAtomicEventListener(AtomicEventListener listener) {
599
                return eventBuffer.removeAtomicEventListener(listener);
600
        }
601
        public void beginAtomicEvent() {
602
                eventBuffer.beginAtomicEvent();
603
        }
604
        public void endAtomicEvent() {
605
                eventBuffer.endAtomicEvent();
606
        }
607
}