Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / extensions / extAnnotations / src / com / iver / cit / gvsig / fmap / layers / Annotation_Layer.java @ 12703

History | View | Annotate | Download (18.5 KB)

1

    
2
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
3
 *
4
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
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 2
9
 * of the License, or (at your option) any later version.
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.
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
 *
20
 * For more information, contact:
21
 *
22
 *  Generalitat Valenciana
23
 *   Conselleria d'Infraestructures i Transport
24
 *   Av. Blasco Ib??ez, 50
25
 *   46010 VALENCIA
26
 *   SPAIN
27
 *
28
 *      +34 963862235
29
 *   gvsig@gva.es
30
 *      www.gvsig.gva.es
31
 *
32
 *    or
33
 *
34
 *   IVER T.I. S.A
35
 *   Salamanca 50
36
 *   46005 Valencia
37
 *   Spain
38
 *
39
 *   +34 963163400
40
 *   dac@iver.es
41
 */
42

    
43
package com.iver.cit.gvsig.fmap.layers;
44

    
45
import java.awt.Color;
46
import java.awt.Font;
47
import java.awt.Graphics2D;
48
import java.awt.Shape;
49
import java.awt.font.FontRenderContext;
50
import java.awt.font.GlyphVector;
51
import java.awt.geom.AffineTransform;
52
import java.awt.geom.Point2D;
53
import java.awt.geom.Rectangle2D;
54
import java.awt.image.BufferedImage;
55
import java.io.File;
56
import java.io.IOException;
57

    
58
import javax.print.attribute.PrintRequestAttributeSet;
59

    
60
import org.cresques.cts.ICoordTrans;
61
import org.cresques.cts.IProjection;
62

    
63
import com.hardcode.driverManager.Driver;
64
import com.hardcode.driverManager.DriverLoadException;
65
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
66
import com.hardcode.gdbms.engine.values.Value;
67
import com.hardcode.gdbms.engine.values.ValueFactory;
68
import com.iver.cit.gvsig.fmap.DriverException;
69
import com.iver.cit.gvsig.fmap.ViewPort;
70
import com.iver.cit.gvsig.fmap.core.FPoint2D;
71
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
72
import com.iver.cit.gvsig.fmap.core.IGeometry;
73
import com.iver.cit.gvsig.fmap.core.ISymbol;
74
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
75
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
76
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
77
import com.iver.cit.gvsig.fmap.drivers.BoundedShapes;
78
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
79
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
80
import com.iver.cit.gvsig.fmap.edition.Annotation_EditableAdapter;
81
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
82
import com.iver.cit.gvsig.fmap.edition.EditionException;
83
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
84
import com.iver.cit.gvsig.fmap.operation.strategies.Annotation_Strategy;
85
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy;
86
import com.iver.cit.gvsig.fmap.operations.strategies.StrategyManager;
87
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException;
88
import com.iver.cit.gvsig.fmap.rendering.Annotation_Legend;
89
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
90
import com.iver.cit.gvsig.fmap.spatialindex.QuadtreeJts;
91
import com.iver.utiles.XMLEntity;
92
import com.iver.utiles.swing.threads.Cancellable;
93

    
94

    
95
/**
96
 * Annotation's layer.
97
 *
98
 * @author Vicente Caballero Navarro
99
 */
100
public class Annotation_Layer extends FLyrVect {
101
    private Annotation_Mapping mapping = null;
102
    private int indexEditing = -1;
103
    private Annotation_Legend aLegend;
104
    private Strategy strategy = null;
105
    private final static AffineTransform ati=new AffineTransform();
106
    /**
107
     * Crea un nuevo FLyrAnnotation.
108
     */
109
    public Annotation_Layer() {
110
        super();
111
    }
112

    
113
    /**
114
     * DOCUMENT ME!
115
     *
116
     * @param mapping DOCUMENT ME!
117
     * @throws DriverException
118
     * @throws FieldNotFoundException
119
     */
120
    public void setMapping(Annotation_Mapping mapping) throws FieldNotFoundException, DriverException {
121
        this.mapping = mapping;
122
        aLegend = new Annotation_Legend();
123
        setLegend();
124
    }
125

    
126
    /**
127
     * DOCUMENT ME!
128
     *
129
     * @return DOCUMENT ME!
130
     */
131
    public Annotation_Mapping getAnnotatonMapping() {
132
        return mapping;
133
    }
134

    
135
    /**
136
     * @see com.iver.cit.gvsig.fmap.layers.LayerOperations#draw(java.awt.image.BufferedImage,
137
     *      java.awt.Graphics2D, ISymbol)
138
     */
139
    public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
140
        Cancellable cancel, double scale) throws DriverException {
141
        if (isWithinScale(scale)) {
142
            // Las que solo tienen etiquetado sin pintar el shape,
143
            // no pasamos por ellas
144
            boolean bDrawShapes = true;
145

    
146
            if (bDrawShapes) {
147
                if (strategy == null) {
148
                    strategy = new Annotation_Strategy(this);
149
                }
150

    
151
                try {
152
                    g.setColor(Color.black);
153

    
154
                    ReadableVectorial adapter = getSource();
155
                    adapter.start();
156

    
157
                    strategy.draw(image, g, viewPort, cancel);
158
                    adapter.stop();
159
                } catch (DriverException e) {
160
                    this.setVisible(false);
161
                    this.setActive(false);
162
                    throw e;
163
                } catch (DriverIOException e) {
164
                    this.setVisible(false);
165
                    this.setActive(false);
166
                    throw new DriverException(e);
167
                }
168
            }
169

    
170
            if (getVirtualLayers() != null) {
171
                getVirtualLayers().draw(image, g, viewPort, cancel, scale);
172
            }
173
        }
174
    }
175

    
176
    /**
177
     * @see com.iver.cit.gvsig.fmap.layers.LayerOperations#getFullExtent()
178
     */
179
    public Rectangle2D getFullExtent() throws DriverException {
180
        try {
181
            Rectangle2D rAux;
182
            getSource().start();
183
            rAux = getSource().getFullExtent();
184
            getSource().stop();
185

    
186
            // Si existe reproyecci?n, reproyectar el extent
187
            ICoordTrans ct = getCoordTrans();
188

    
189
            if (ct != null) {
190
                Point2D pt1 = new Point2D.Double(rAux.getMinX(), rAux.getMinY());
191
                Point2D pt2 = new Point2D.Double(rAux.getMaxX(), rAux.getMaxY());
192
                pt1 = ct.convert(pt1, null);
193
                pt2 = ct.convert(pt2, null);
194
                rAux = new Rectangle2D.Double();
195
                rAux.setFrameFromDiagonal(pt1, pt2);
196
            }
197

    
198
            return rAux;
199
        } catch (DriverIOException e) {
200
            throw new DriverException(e);
201
        }
202
    }
203

    
204
    /**
205
     * @see com.iver.cit.gvsig.fmap.layers.FLayer#print(java.awt.Graphics2D,
206
     *      com.iver.cit.gvsig.fmap.ViewPort,
207
     *      com.iver.utiles.swing.threads.Cancellable)
208
     */
209
    public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel,
210
        double scale, PrintRequestAttributeSet properties)
211
        throws DriverException {
212
        if (isVisible() && isWithinScale(scale)) {
213
            Strategy strategy = StrategyManager.getStrategy(this);
214
            strategy.print(g, viewPort, cancel, properties);
215
        }
216
    }
217

    
218
    /*
219
     * (non-Javadoc)
220
     *
221
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.RandomVectorialData#queryByRect(java.awt.geom.Rectangle2D)
222
     */
223
    public FBitSet queryByRect(Rectangle2D rect) throws DriverException {
224
        Strategy s = StrategyManager.getStrategy(this);
225

    
226
        return s.queryByRect(rect);
227
    }
228

    
229
    /**
230
     * DOCUMENT ME!
231
     *
232
     * @param p DOCUMENT ME!
233
     * @param tolerance DOCUMENT ME!
234
     *
235
     * @return DOCUMENT ME!
236
     *
237
     * @throws DriverException DOCUMENT ME!
238
     */
239
    public FBitSet queryByPoint(Point2D p, double tolerance)
240
        throws DriverException {
241
        Strategy s = StrategyManager.getStrategy(this);
242

    
243
        return s.queryByPoint(p, tolerance);
244
    }
245

    
246
    /**
247
     * DOCUMENT ME!
248
     *
249
     * @param g DOCUMENT ME!
250
     * @param relationship DOCUMENT ME!
251
     *
252
     * @return DOCUMENT ME!
253
     *
254
     * @throws DriverException DOCUMENT ME!
255
     * @throws VisitException DOCUMENT ME!
256
     */
257
    public FBitSet queryByShape(IGeometry g, int relationship)
258
        throws DriverException, VisitException {
259
        Strategy s = StrategyManager.getStrategy(this);
260

    
261
        return s.queryByShape(g, relationship);
262
    }
263

    
264
    /**
265
     * DOCUMENT ME!
266
     *
267
     * @return DOCUMENT ME!
268
     *
269
     * @throws XMLException
270
     *
271
     * @see com.iver.cit.gvsig.fmap.layers.FLayer#getProperties()
272
     */
273
    public XMLEntity getXMLEntity() throws XMLException {
274
        XMLEntity xml = super.getXMLEntity();
275
        return xml;
276
    }
277

    
278
    /**
279
     * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setXMLEntity(com.iver.utiles.XMLEntity)
280
     */
281
    public void setXMLEntity(XMLEntity xml) throws XMLException {
282
        IProjection proj = null;
283

    
284
        if (xml.contains("proj")) {
285
            proj = CRSFactory.getCRS(xml.getStringProperty("proj"));
286
        }
287

    
288
        if (xml.contains("file")) {
289
            Driver d;
290

    
291
            try {
292
                d = LayerFactory.getDM().getDriver(xml.getStringProperty(
293
                            "driverName"));
294
            } catch (DriverLoadException e1) {
295
                throw new XMLException(e1);
296
            }
297

    
298
            FLyrVect lv = (FLyrVect) LayerFactory.createLayer(xml.getName(),
299
                    (VectorialFileDriver) d,
300
                    new File(xml.getStringProperty("file")), proj);
301

    
302
            try {
303
                this.setSource(lv.getSource());
304
                this.setRecordset(lv.getRecordset());
305
                this.setProjection(lv.getProjection());
306
                this.setLegend((VectorialLegend) lv.getLegend());
307
                Annotation_Mapping.addAnnotationMapping(this);
308
            } catch (DriverException e) {
309
                throw new XMLException(e);
310
            } catch (FieldNotFoundException e) {
311
                    throw new XMLException(e);
312
            } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
313
                    throw new XMLException(e);
314
            }
315
        }
316
        super.setXMLEntity(xml);
317
    }
318

    
319
    public Value getSymbolKey(int i)
320
        throws com.hardcode.gdbms.engine.data.driver.DriverException,
321
            DriverException {
322
        SelectableDataSource ds = getRecordset();
323
        String t = new String();
324
        Value val = ds.getFieldValue(i, mapping.getColumnText());
325
        t = val.toString();
326

    
327
        if (mapping.getColumnColor() != -1) {
328
            Value valColor = ds.getFieldValue(i, mapping.getColumnColor());
329
            t = t.concat(valColor.toString());
330
        }
331

    
332
        if (mapping.getColumnTypeFont() != -1) {
333
            Value valTypeFont = ds.getFieldValue(i, mapping.getColumnTypeFont());
334
            t = t.concat(valTypeFont.toString());
335
        }
336

    
337
        if (mapping.getColumnStyleFont() != -1) {
338
            Value valStyleFont = ds.getFieldValue(i,
339
                    mapping.getColumnStyleFont());
340
            t = t.concat(valStyleFont.toString());
341
        }
342

    
343
        Value total = ValueFactory.createValue(t);
344

    
345
        return total;
346
    }
347

    
348
    /*
349
     * (non-Javadoc)
350
     *
351
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.RandomVectorialData#createIndex()
352
     */
353
    public void createSpatialIndex() {
354
        // FJP: ESTO HABR? QUE CAMBIARLO. PARA LAS CAPAS SECUENCIALES, TENDREMOS
355
        // QUE ACCEDER CON UN WHILE NEXT. (O mejorar lo de los FeatureVisitor
356
        // para que acepten recorrer sin geometria, solo con rectangulos.
357
        // AZABALA: Como no tengo claro de donde se crean las capas de textos
358
        // el ?ndice espacial creado seguir? siendo el Quadtree en memoria
359
        // de JTS (QuadtreeJts es un adapter a nuestra api de indices)
360
        spatialIndex = new QuadtreeJts();
361

    
362
        ReadableVectorial va = getSource();
363
        ICoordTrans ct = getCoordTrans();
364
        BoundedShapes shapeBounds = (BoundedShapes) va.getDriver();
365

    
366
        try {
367
            va.start();
368

    
369
            for (int i = 0; i < va.getShapeCount(); i++) {
370
                Rectangle2D r = null;
371
                IGeometry geom = va.getShape(i);
372
                 if (geom != null) {
373
                    r = geom.getBounds2D();
374
                } else {
375
                    r = shapeBounds.getShapeBounds(i);
376
                }
377

    
378
                // TODO: MIRAR COMO SE TRAGAR?A ESTO LO DE LAS REPROYECCIONES
379
                if (ct != null) {
380
                    r = ct.convert(r);
381
                }
382

    
383
                if (r != null) {
384
                    spatialIndex.insert(r, i);
385
                }
386
            } // for
387

    
388
            va.stop();
389
        } catch (DriverIOException e) {
390
            e.printStackTrace();
391
        } catch (IOException e) {
392
            e.printStackTrace();
393
        }
394
    }
395

    
396
    /**
397
     * DOCUMENT ME!
398
     *
399
     * @throws DriverException DOCUMENT ME!
400
     */
401
    public void setSelectedEditing() throws DriverException {
402
        FBitSet bitSet = getRecordset().getSelection();
403

    
404
        if (bitSet.cardinality() == 0) {
405
            return;
406
        }
407

    
408
        indexEditing = bitSet.nextSetBit(0);
409
    }
410

    
411
    public void setInEdition(int i) {
412
        indexEditing = i;
413
    }
414

    
415
    /**
416
     * DOCUMENT ME!
417
     *
418
     * @return DOCUMENT ME!
419
     */
420
    public int getInEdition() {
421
        return indexEditing;
422
    }
423

    
424
    public void setLegend() throws DriverException,
425
        FieldNotFoundException {
426
        try {
427
            getSource().getRecordset().start();
428
            aLegend.setFieldName(getSource().getRecordset().getFieldName(mapping.getColumnText()));
429
            setLegend(aLegend);
430
            getSource().getRecordset().stop();
431
        } catch (DriverLoadException e) {
432
                throw new DriverException(e);
433
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
434
                throw new DriverException(e);
435
        } catch (ArrayIndexOutOfBoundsException e) {
436
                throw new DriverException(e);
437
        }
438
    }
439

    
440
    /**
441
     * DOCUMENT ME!
442
     *
443
     * @return DOCUMENT ME!
444
     */
445
    public Strategy getStrategy() {
446
        return strategy;
447
    }
448

    
449
    /**
450
     * DOCUMENT ME!
451
     *
452
     * @param b DOCUMENT ME!
453
     *
454
     * @throws EditionException DOCUMENT ME!
455
     */
456
    public void setEditing(boolean b) throws EditionException {
457
       super.setEditing(b);
458

    
459
        try {
460
            if (b) {
461
                Annotation_EditableAdapter aea = new Annotation_EditableAdapter(this);
462
                aea.setOriginalVectorialAdapter(((VectorialEditableAdapter) super.getSource()).getOriginalAdapter());
463
                ((VectorialEditableAdapter) super.getSource()).cancelEdition(EditionEvent.GRAPHIC);
464
                aea.start();
465

    
466
                // CHEMA
467
                aea.startEdition(EditionEvent.GRAPHIC);
468
                setSource(aea);
469
                getRecordset().setSelectionSupport(aea.getOriginalAdapter()
470
                                                      .getRecordset()
471
                                                      .getSelectionSupport());
472
            } else {
473
            }
474
        } catch (DriverIOException e) {
475
            throw new EditionException();
476
        } catch (DriverLoadException e) {
477
            throw new EditionException();
478
        } catch (DriverException e) {
479
            throw new EditionException();
480
        } catch (IOException e) {
481
            throw new EditionException();
482
        }
483

    
484
        deleteSpatialIndex();
485
    }
486

    
487
    /**
488
     * DOCUMENT ME!
489
     *
490
     * @param layer DOCUMENT ME!
491
     *
492
     * @return DOCUMENT ME!
493
     *
494
     * @throws DriverException DOCUMENT ME!
495
     * @throws FieldNotFoundException DOCUMENT ME!
496
     */
497
    public static Annotation_Layer createLayerFromVect(FLyrVect layer)
498
        throws DriverException, FieldNotFoundException {
499
        Annotation_Layer la = new Annotation_Layer();
500
        FLyrVect lv=(FLyrVect)LayerFactory.createLayer(layer.getName(),layer.getSource().getDriver(),layer.getProjection());
501
        la.setSource(lv.getSource());
502
        la.setRecordset(lv.getRecordset());
503
        la.setProjection(layer.getProjection());
504
        la.getRecordset().setSelection((FBitSet)layer.getRecordset().getSelection());
505

    
506
        return la;
507
    }
508

    
509
    /**
510
     * DOCUMENT ME!
511
     */
512
    public void removingThisLayer() {
513
        super.removingThisLayer();
514
        spatialIndex = null;
515
        aLegend = null;
516
        strategy = null;
517
        System.gc();
518
    }
519
    public IGeometry getTextWrappingGeometryInPixels(double height, String description,
520
            double rotation, String type, int style, int numReg,ViewPort vp, IGeometry geom) throws DriverIOException {
521

    
522
            Shape shapeP=geom.getInternalShape();
523
            FPoint2D p = null;
524
            if (!(shapeP instanceof FPoint2D)) {
525
                    Rectangle2D rP=shapeP.getBounds2D();
526
                    p=new FPoint2D(rP.getX(),rP.getMaxY());
527
            }else {
528
                    p=(FPoint2D)shapeP;
529
            }
530
//            if (geom.getInternalShape() instanceof FPolygon2D){
531
//                    r=new Rectangle2D.Double(r.getX()-r.getWidth()*0.5,r.getY()-r.getHeight()*0.5,r.getWidth(),r.getHeight());
532
//            }
533
//            p=new Point2D.Double(r.getCenterX(),r.getCenterY());
534

    
535
            if (!((FSymbol) getLegend().getDefaultSymbol()).isFontSizeInPixels()) {
536
                height = vp.fromMapDistance((int) (height));
537
            }
538

    
539
            Font font = new Font(type, style,
540
                    (int) (height));
541
            FontRenderContext frc = new FontRenderContext(ati,
542
                    false, true);
543
            GlyphVector gv = font.createGlyphVector(frc, description);
544
            Shape shape = gv.getOutline((float) p.getX(), (float) (p.getY()));// - height));
545
            Rectangle2D rGeom=shape.getBounds2D();
546
            IGeometry geomResult = ShapeFactory.createPolygon2D(new GeneralPathX(
547
                            rGeom));
548

    
549
            if (rotation != 0) {
550
                    AffineTransform Tx = AffineTransform.getRotateInstance(Math.toRadians(-rotation),p.getX(),p.getY());
551
                        geomResult.transform(Tx);
552
//                geomResult.transform(AffineTransform.getRotateInstance(Math.toRadians(-rotation),p.getX(),p.getY()));
553
//                geomResult.rotate(Math.toRadians(rotation), p.getX(),
554
//                        p.getY());
555
            }
556

    
557
            return geomResult;
558
        }
559
    public FLayer cloneLayer() throws Exception {
560
                Annotation_Layer clonedLayer = new Annotation_Layer();
561
                clonedLayer.setSource(getSource());
562
                clonedLayer.setVisible(isVisible());
563
                clonedLayer.setISpatialIndex(getISpatialIndex());
564
                clonedLayer.setName(getName());
565
                clonedLayer.setCoordTrans(getCoordTrans());
566
                clonedLayer.setLegend((VectorialLegend)getLegend());
567
                clonedLayer.mapping=mapping;
568
                clonedLayer.aLegend=aLegend;
569
                return clonedLayer;
570
        }
571

    
572
}