Statistics
| Revision:

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

History | View | Annotate | Download (25.7 KB)

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

    
3
import java.awt.Color;
4
import java.awt.Font;
5
import java.awt.Graphics2D;
6
import java.awt.Shape;
7
import java.awt.font.FontRenderContext;
8
import java.awt.font.GlyphVector;
9
import java.awt.geom.AffineTransform;
10
import java.awt.geom.Point2D;
11
import java.awt.geom.Rectangle2D;
12
import java.awt.image.BufferedImage;
13
import java.io.IOException;
14

    
15
import javax.print.attribute.PrintRequestAttributeSet;
16

    
17
import org.cresques.cts.ICoordTrans;
18
import org.cresques.cts.IProjection;
19

    
20
import com.hardcode.driverManager.DriverLoadException;
21
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
22
import com.hardcode.gdbms.engine.values.Value;
23
import com.hardcode.gdbms.engine.values.ValueFactory;
24
import com.iver.cit.gvsig.fmap.DriverException;
25
import com.iver.cit.gvsig.fmap.ViewPort;
26
import com.iver.cit.gvsig.fmap.core.FPoint2D;
27
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
28
import com.iver.cit.gvsig.fmap.core.IGeometry;
29
import com.iver.cit.gvsig.fmap.core.ISymbol;
30
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
31
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
32
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
33
import com.iver.cit.gvsig.fmap.drivers.BoundedShapes;
34
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
35
import com.iver.cit.gvsig.fmap.edition.Annotation_EditableAdapter;
36
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
37
import com.iver.cit.gvsig.fmap.edition.EditionException;
38
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
39
import com.iver.cit.gvsig.fmap.operation.strategies.Annotation_Strategy;
40
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy;
41
import com.iver.cit.gvsig.fmap.operations.strategies.StrategyManager;
42
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException;
43
import com.iver.cit.gvsig.fmap.rendering.Annotation_Legend;
44
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
45
import com.iver.cit.gvsig.fmap.spatialindex.QuadtreeJts;
46
import com.iver.utiles.XMLEntity;
47
import com.iver.utiles.swing.threads.Cancellable;
48

    
49
/**
50
 * Annotation's layer.
51
 *
52
 * @author Vicente Caballero Navarro
53
 */
54
public class Annotation_Layer extends FLyrVect {
55
        private Annotation_Mapping mapping = null;
56

    
57
//        private ArrayList m_labels;
58

    
59
        private int indexEditing = -1;
60

    
61
        private boolean inPixels=false;
62
        private Annotation_Legend aLegend;
63
        private Strategy strategy=null;
64
        /**
65
         * Crea un nuevo FLyrAnnotation.
66
         */
67
        public Annotation_Layer() {
68
                super();
69

    
70
        }
71

    
72
        /**
73
         * DOCUMENT ME!
74
         *
75
         * @param mapping
76
         *            DOCUMENT ME!
77
         */
78
        public void setMapping(Annotation_Mapping mapping) {
79
                this.mapping = mapping;
80
                aLegend=new Annotation_Legend();
81
//                try {
82
                        setLegend();
83
//                        createLabels();
84
//                } catch (DriverException e) {
85
//                        e.printStackTrace();
86
//                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),
87
//                                        PluginServices.getText(this,"Este fichero no es posible abirlo como capa de anotaciones"));
88
//                }
89
        }
90

    
91
        /**
92
         * DOCUMENT ME!
93
         *
94
         * @return DOCUMENT ME!
95
         */
96
        public Annotation_Mapping getMapping() {
97
                return mapping;
98
        }
99

    
100
        /**
101
         * @see com.iver.cit.gvsig.fmap.layers.LayerOperations#draw(java.awt.image.BufferedImage,
102
         *      java.awt.Graphics2D, ISymbol)
103
         */
104
        public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
105
                        Cancellable cancel, double scale) throws DriverException {
106

    
107

    
108
                if (isWithinScale(scale)) {
109
                        // Las que solo tienen etiquetado sin pintar el shape,
110
                        // no pasamos por ellas
111
                        boolean bDrawShapes = true;
112

    
113
                        if (bDrawShapes) {
114
                                if (strategy == null){
115
                                        strategy = new Annotation_Strategy(this);
116
                                }
117
                                try {
118
                                        g.setColor(Color.black);
119
                                        ReadableVectorial adapter = getSource();
120
                                        adapter.start();
121

    
122
                                        strategy.draw(image, g, viewPort, cancel);
123
                                        adapter.stop();
124
                                        if (getISpatialIndex()==null && !isEditing()) {
125
                                                createSpatialIndex();
126
                                        }
127
                                } catch (DriverException e) {
128
                                        this.setVisible(false);
129
                                        this.setActive(false);
130
                                        throw e;
131
                                } catch (DriverIOException e) {
132
                                        this.setVisible(false);
133
                                        this.setActive(false);
134
                                        throw new DriverException(e);
135
                                }
136
                        }
137

    
138
                        if (getVirtualLayers() != null) {
139
                                getVirtualLayers().draw(image, g, viewPort, cancel, scale);
140
                        }
141
                }
142
        }
143

    
144
        /**
145
         * @see com.iver.cit.gvsig.fmap.layers.LayerOperations#getFullExtent()
146
         */
147
        public Rectangle2D getFullExtent() throws DriverException {
148
                try {
149
                        Rectangle2D rAux;
150

    
151
                        // logger.debug("source.start()");
152
                        getSource().start();
153
                        rAux = getSource().getFullExtent();
154

    
155
                        // logger.debug("source.stop()");
156
                        getSource().stop();
157

    
158
                        // Si existe reproyecci?n, reproyectar el extent
159
                        ICoordTrans ct = getCoordTrans();
160

    
161
                        if (ct != null) {
162
                                Point2D pt1 = new Point2D.Double(rAux.getMinX(), rAux.getMinY());
163
                                Point2D pt2 = new Point2D.Double(rAux.getMaxX(), rAux.getMaxY());
164
                                pt1 = ct.convert(pt1, null);
165
                                pt2 = ct.convert(pt2, null);
166
                                rAux = new Rectangle2D.Double();
167
                                rAux.setFrameFromDiagonal(pt1, pt2);
168
                        }
169

    
170
                        return rAux;
171
                } catch (DriverIOException e) {
172
                        throw new DriverException(e);
173
                }
174
        }
175

    
176
        /**
177
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#print(java.awt.Graphics2D,
178
         *      com.iver.cit.gvsig.fmap.ViewPort,
179
         *      com.iver.utiles.swing.threads.Cancellable)
180
         */
181
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel,
182
                        double scale, PrintRequestAttributeSet properties) throws DriverException {
183
                if (isVisible() && isWithinScale(scale)) {
184
                        Strategy strategy = StrategyManager.getStrategy(this);
185
                        strategy.print(g, viewPort, cancel, properties);
186
                }
187
        }
188

    
189
        /*
190
         * (non-Javadoc)
191
         *
192
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.RandomVectorialData#queryByRect(java.awt.geom.Rectangle2D)
193
         */
194
        public FBitSet queryByRect(Rectangle2D rect) throws DriverException {
195
                Strategy s = StrategyManager.getStrategy(this);
196

    
197
                return s.queryByRect(rect);
198
        }
199

    
200
        /**
201
         * DOCUMENT ME!
202
         *
203
         * @param p
204
         *            DOCUMENT ME!
205
         * @param tolerance
206
         *            DOCUMENT ME!
207
         *
208
         * @return DOCUMENT ME!
209
         *
210
         * @throws DriverException
211
         *             DOCUMENT ME!
212
         */
213
        public FBitSet queryByPoint(Point2D p, double tolerance)
214
                        throws DriverException {
215
                Strategy s = StrategyManager.getStrategy(this);
216

    
217
                return s.queryByPoint(p, tolerance);
218
        }
219

    
220
        /**
221
         * DOCUMENT ME!
222
         *
223
         * @param g
224
         *            DOCUMENT ME!
225
         * @param relationship
226
         *            DOCUMENT ME!
227
         *
228
         * @return DOCUMENT ME!
229
         *
230
         * @throws DriverException
231
         *             DOCUMENT ME!
232
         * @throws VisitException
233
         *             DOCUMENT ME!
234
         */
235
        public FBitSet queryByShape(IGeometry g, int relationship)
236
                        throws DriverException, VisitException {
237
                Strategy s = StrategyManager.getStrategy(this);
238

    
239
                return s.queryByShape(g, relationship);
240
        }
241

    
242
        /**
243
         * DOCUMENT ME!
244
         *
245
         * @return DOCUMENT ME!
246
         *
247
         * @throws XMLException
248
         *
249
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getProperties()
250
         */
251
        public XMLEntity getXMLEntity() throws XMLException {
252
                XMLEntity xml = super.getXMLEntity();
253
//                xml.addChild(mapping.getXMLEntity());
254
                xml.putProperty("isInPixels", isInPixels());
255

    
256
                return xml;
257
        }
258

    
259
        /**
260
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setXMLEntity(com.iver.utiles.XMLEntity)
261
         */
262
        public void setXMLEntity(XMLEntity xml) throws XMLException {
263
//                Iterator iter=xml.findChildren("className",Annotation_Mapping.class.getName());
264
//                if (iter.hasNext())
265
//                        mapping = Annotation_Mapping.createFromXML((XMLEntity)iter.next());
266
//                else{
267
//                        //Este else para versiones anteriores a la 1.0.2.(908)
268
//                        if (xml.getChildrenCount()==3)
269
//                                mapping = Annotation_Mapping.createFromXML(xml.getChild(2));
270
//                        else
271
//                                mapping = Annotation_Mapping.createFromXML(xml.getChild(3));
272
//                }
273
                setInPixels(xml.getBooleanProperty("isInPixels"));
274

    
275
                IProjection proj = null;
276

    
277
                if (xml.contains("proj")) {
278
                        proj = CRSFactory.getCRS(xml.getStringProperty("proj"));
279
                }
280

    
281
//                VectorialAdapter adapter=null;
282
//                if (xml.contains("file")){
283
//                        adapter = new VectorialFileAdapter(new File(xml
284
//                                        .getStringProperty("file")));
285
//                }else if (xml.contains("db")){
286
//                        adapter = new VectorialDBAdapter();
287
//                }
288
//                Driver d;
289
//
290
//                try {
291
//                        d = LayerFactory.getDM().getDriver(
292
//                                        xml.getStringProperty("driverName"));
293
//                } catch (DriverLoadException e1) {
294
//                        throw new XMLException(e1);
295
//                }
296
//
297
//                adapter.setDriver((VectorialDriver) d);
298
//                // TODO Meter esto dentro de la comprobaci?n de si hay memoria
299
//                if (false) {
300
//                } else {
301
//                        setSource(adapter);
302
//                        setProjection(proj);
303
//                }
304

    
305
                // Le asignamos tambi?n una legenda por defecto acorde con
306
                // el tipo de shape que tenga. Tampoco s? si es aqu? el
307
                // sitio adecuado, pero en fin....
308
                /*
309
                 * if (d instanceof WithDefaultLegend) { WithDefaultLegend aux =
310
                 * (WithDefaultLegend) d; adapter.start(); setLegend((VectorialLegend)
311
                 * aux.getDefaultLegend()); adapter.stop(); } else {
312
                 * setLegend(LegendFactory.createSingleSymbolLegend(getShapeType())); }
313
                 */
314

    
315
                super.setXMLEntity(xml);
316
//                try {
317
//                        createLabels();
318
//                } catch (DriverException e) {
319
//                        // TODO Auto-generated catch block
320
//                        e.printStackTrace();
321
//                }
322
        }
323

    
324
        /**
325
         * Esto tiene el fallo de que obligas a una etiqueta por entidad, para poder
326
         * evitar esto, una posible soluci?n ser?a que un FLabel pudiera ser una
327
         * colecci?n de FLabel (Patr?n Composite)
328
         *
329
         * @param lyrVect
330
         * @throws DriverException
331
         */
332
//        private void createLabels() throws DriverException {
333
//                SelectableDataSource ds = getRecordset();
334
//                FSymbol symbol;
335
//                try {
336
//                        ReadableVectorial adapter = getSource();
337
//                        adapter.start();
338
//                        if (isJoined()){
339
//                                ds.start();
340
//                        }
341
//                        int sc;
342
//                        // El mapping[0] es el text
343
//                        int fieldId = mapping.getColumnText();
344
//                        // El mapping[1] es el ?ngulo
345
//                        int idFieldRotationText = mapping.getColumnRotate();
346
//                        // El mapping[2] es el color
347
//                        int idFieldColorText = mapping.getColumnColor();
348
//                        // El mapping[3] es el alto
349
//                        int idFieldHeightText = mapping.getColumnHeight();
350
//                        // El mapping[4] es el tipo de fuente
351
//                        int idFieldTypeFontText = mapping.getColumnTypeFont();
352
//                        // El mapping[5] es el estilo de fuente
353
//                        int idFieldStyleFontText = mapping.getColumnStyleFont();
354
//
355
//                        sc = (int) ds.getRowCount();
356
//                        m_labels = new ArrayList(sc);
357
//                        DriverAttributes attr = adapter.getDriverAttributes();
358
//                        boolean bMustClone = false;
359
//                        if (attr != null) {
360
//                                if (attr.isLoadedInMemory()) {
361
//                                        bMustClone = attr.isLoadedInMemory();
362
//                                }
363
//                        }
364
//                        ICoordTrans ct = getCoordTrans();
365
//                        FSymbol defaultSym = (FSymbol) getLegend().getDefaultSymbol();
366
//                        for (int i = 0; i < sc; i++) {
367
//                                IGeometry geom = adapter.getShape(i);
368
//
369
//                                if (geom == null) {
370
//                                        m_labels.add(null);
371
//                                        continue;
372
//                                }
373
//                                if (ct != null) {
374
//                                        if (bMustClone)
375
//                                                geom = geom.cloneGeometry();
376
//                                        geom.reProject(ct);
377
//                                }
378
//
379
//                                // TODO: El m?todo contenedor (createLabelLayer) debe recoger
380
//                                // los par?metros de posicionamiento y de allowDuplicates
381
//                                // if (i >= 328)
382
//                                // System.out.println("i= " + i + " " + val.toString());
383
//                                //ArrayList values=new ArrayList(4);
384
////                                String t=new String();
385
//                                Value val = ds.getFieldValue(i, fieldId);
386
//
387
//                                //Value total=ValueFactory.createValue((Value[])values.toArray(new Value[0]));
388
//                                Value total=getSymbolKey(i);//ValueFactory.createValue(t);
389
//                                if ((val instanceof NullValue) || (val == null)) {
390
//                                        m_labels.add(null);
391
//                                        continue;
392
//                                }
393
//                                FLabel[] lbls = geom.createLabels(0, true);
394
//                                for (int j = 0; j < lbls.length; j++) {
395
//                                        if (lbls[j] != null) {
396
//                                                lbls[j].setString(val.toString());
397
//                                                if (idFieldRotationText != -1) {
398
//                                                        NumericValue rotation = (NumericValue) ds
399
//                                                                        .getFieldValue(i, idFieldRotationText);
400
//                                                        lbls[j].setRotation(rotation.doubleValue());
401
//                                                } else {
402
//                                                        lbls[j].setRotation(defaultSym.getRotation());
403
//                                                }
404
//
405
//                                                float height;
406
//                                                if (idFieldHeightText != -1) {
407
//                                                        NumericValue h = (NumericValue) ds
408
//                                                                        .getFieldValue(i, idFieldHeightText);
409
//                                                        height=h.floatValue();
410
//                                                        lbls[j].setHeight(height);
411
//                                                } else {
412
//                                                        height=defaultSym.getFontSize();
413
//                                                        lbls[j].setHeight(height);
414
//                                                }
415
//
416
//
417
//
418
//                                                if (aLegend.getSymbolByValue(total)==null){
419
//                                                        Color color;
420
//                                                        if (idFieldColorText != -1) {
421
//                                                                NumericValue c = (NumericValue) ds.getFieldValue(
422
//                                                                                i, idFieldColorText);
423
//                                                                color=new Color(c.intValue());
424
//                                                        } else {
425
//                                                                color=defaultSym.getFontColor();
426
//                                                        }
427
//                                                        String typeFont;
428
//                                                        if (idFieldTypeFontText != -1) {
429
//                                                                StringValue tf = (StringValue) ds
430
//                                                                                .getFieldValue(i, idFieldTypeFontText);
431
//                                                                typeFont=tf.getValue();
432
//                                                        } else {
433
//                                                                typeFont=defaultSym.getFont().getFontName();
434
//                                                        }
435
//                                                        int style;
436
//                                                        if (idFieldStyleFontText != -1) {
437
//                                                                NumericValue sf = (NumericValue) ds
438
//                                                                                .getFieldValue(i, idFieldStyleFontText);
439
//                                                                style=sf.intValue();
440
//                                                        } else {
441
//                                                                style=defaultSym.getFont().getStyle();
442
//                                                        }
443
//                                                        symbol=new FSymbol(FConstant.SYMBOL_TYPE_TEXT);
444
//                                                        symbol.setFontSizeInPixels(isInPixels());
445
//                                                        symbol.setFont(new Font(typeFont,style,(int)height));
446
//                                                        symbol.setDescription(lbls[j].getString());
447
//                                                        symbol.setFontColor(color);
448
//                                                        aLegend.addSymbol(total,symbol);
449
//                                                }
450
//
451
//                                        }
452
//                                m_labels.add(lbls[j]);
453
//
454
//                                }
455
//                        }
456
//
457
//                        if (this.isJoined()){
458
//                                ds.stop();
459
//                        }
460
//                        adapter.stop();
461
//                } catch (DriverIOException e) {
462
//                        e.printStackTrace();
463
//                        throw new DriverException(e);
464
//                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
465
//                        e.printStackTrace();
466
//                        throw new DriverException(e);
467
//                }
468
//
469
//        }
470

    
471
        public Value getSymbolKey(int i) throws com.hardcode.gdbms.engine.data.driver.DriverException, DriverException {
472
                SelectableDataSource ds = getRecordset();
473
                String t=new String();
474
                Value val = ds.getFieldValue(i, mapping.getColumnText());
475
                t=val.toString();
476
                //values.add(val);
477
                if (mapping.getColumnColor()!=-1){
478
                        Value valColor=ds.getFieldValue(i,mapping.getColumnColor());
479
                        t=t.concat(valColor.toString());
480
                        //values.add(valColor);
481
                }
482
                if (mapping.getColumnTypeFont()!=-1){
483
                        Value valTypeFont=ds.getFieldValue(i,mapping.getColumnTypeFont());
484
                        t=t.concat(valTypeFont.toString());
485
                        //values.add(valTypeFont);
486
                }
487

    
488
                if (mapping.getColumnStyleFont()!=-1){
489
                        Value valStyleFont=ds.getFieldValue(i,mapping.getColumnStyleFont());
490
                        t=t.concat(valStyleFont.toString());
491
                        //values.add(valStyleFont);
492
                }
493
                //Value total=ValueFactory.createValue((Value[])values.toArray(new Value[0]));
494
                Value total=ValueFactory.createValue(t);
495
                return total;
496
        }
497

    
498
//        public FLabel getLabel(int numReg) {
499
//                if (m_labels == null || numReg == -1)
500
//                        return null;
501
//                if (getSource() instanceof Annotation_EditableAdapter){
502
//                        Annotation_EditableAdapter aea=((Annotation_EditableAdapter)getSource());
503
//                        return aea.getLabel(numReg,false);
504
//                }
505
//                return (FLabel)m_labels.get(numReg);
506
//        }
507
//        private FLabel getLabelNew(int numReg) throws DriverException{
508
//                SelectableDataSource ds = getRecordset();
509
//                FSymbol symbol;
510
//                try {
511
//                        ReadableVectorial adapter = getSource();
512
//                        adapter.start();
513
//                        if (isJoined()){
514
//                                ds.start();
515
//                        }
516
//                        int sc;
517
//                        // El mapping[0] es el text
518
//                        int fieldId = mapping.getColumnText();
519
//                        // El mapping[1] es el ?ngulo
520
//                        int idFieldRotationText = mapping.getColumnRotate();
521
//                        // El mapping[2] es el color
522
//                        int idFieldColorText = mapping.getColumnColor();
523
//                        // El mapping[3] es el alto
524
//                        int idFieldHeightText = mapping.getColumnHeight();
525
//                        // El mapping[4] es el tipo de fuente
526
//                        int idFieldTypeFontText = mapping.getColumnTypeFont();
527
//                        // El mapping[5] es el estilo de fuente
528
//                        int idFieldStyleFontText = mapping.getColumnStyleFont();
529
//
530
////                        sc = (int) ds.getRowCount();
531
////                        m_labels = new ArrayList(sc);
532
////                        DriverAttributes attr = adapter.getDriverAttributes();
533
//                        boolean bMustClone = false;
534
////                        if (attr != null) {
535
////                                if (attr.isLoadedInMemory()) {
536
////                                        bMustClone = attr.isLoadedInMemory();
537
////                                }
538
////                        }
539
//                        ICoordTrans ct = getCoordTrans();
540
//                        FSymbol defaultSym = (FSymbol) getLegend().getDefaultSymbol();
541
////                        for (int i = 0; i < sc; i++) {
542
//                                IGeometry geom = adapter.getShape(numReg);
543
//
544
//                                if (geom == null) {
545
////                                        m_labels.add(null);
546
//                                        return null;
547
//                                }
548
//                                if (ct != null) {
549
//                                        if (bMustClone)
550
//                                                geom = geom.cloneGeometry();
551
//                                        geom.reProject(ct);
552
//                                }
553
//
554
//                                // TODO: El m?todo contenedor (createLabelLayer) debe recoger
555
//                                // los par?metros de posicionamiento y de allowDuplicates
556
//                                // if (i >= 328)
557
//                                // System.out.println("i= " + i + " " + val.toString());
558
//                                //ArrayList values=new ArrayList(4);
559
//                                String t=new String();
560
//                                Value val = ds.getFieldValue(numReg, fieldId);
561
//                                t=val.toString();
562
//                                //values.add(val);
563
//                                if (idFieldColorText!=-1){
564
//                                        Value valColor=ds.getFieldValue(numReg,idFieldColorText);
565
//                                        t=t.concat(valColor.toString());
566
//                                        //values.add(valColor);
567
//                                }
568
//                                if (idFieldTypeFontText!=-1){
569
//                                        Value valTypeFont=ds.getFieldValue(numReg,idFieldTypeFontText);
570
//                                        t=t.concat(valTypeFont.toString());
571
//                                        //values.add(valTypeFont);
572
//                                }
573
//
574
//                                if (idFieldStyleFontText!=-1){
575
//                                        Value valStyleFont=ds.getFieldValue(numReg,idFieldStyleFontText);
576
//                                        t=t.concat(valStyleFont.toString());
577
//                                        //values.add(valStyleFont);
578
//                                }
579
//                                //Value total=ValueFactory.createValue((Value[])values.toArray(new Value[0]));
580
//                                Value total=ValueFactory.createValue(t);
581
//                                if ((val instanceof NullValue) || (val == null)) {
582
//                                        return null;
583
//                                }
584
//                                FLabel[] lbls = geom.createLabels(0, true);
585
//                                for (int j = 0; j < lbls.length; j++) {
586
//                                        if (lbls[j] != null) {
587
//                                                lbls[j].setString(val.toString());
588
//                                                if (idFieldRotationText != -1) {
589
//                                                        NumericValue rotation = (NumericValue) ds
590
//                                                                        .getFieldValue(numReg, idFieldRotationText);
591
//                                                        lbls[j].setRotation(rotation.doubleValue());
592
//                                                } else {
593
//                                                        lbls[j].setRotation(defaultSym.getRotation());
594
//                                                }
595
//
596
//                                                float height;
597
//                                                if (idFieldHeightText != -1) {
598
//                                                        NumericValue h = (NumericValue) ds
599
//                                                                        .getFieldValue(numReg, idFieldHeightText);
600
//                                                        height=h.floatValue();
601
//                                                        lbls[j].setHeight(height);
602
//                                                } else {
603
//                                                        height=defaultSym.getFontSize();
604
//                                                        lbls[j].setHeight(height);
605
//                                                }
606
//
607
//
608
//
609
//                                                if (aLegend.getSymbolByValue(total)==null){
610
//                                                        Color color;
611
//                                                        if (idFieldColorText != -1) {
612
//                                                                NumericValue c = (NumericValue) ds.getFieldValue(
613
//                                                                                numReg, idFieldColorText);
614
//                                                                color=new Color(c.intValue());
615
//                                                        } else {
616
//                                                                color=defaultSym.getFontColor();
617
//                                                        }
618
//                                                        String typeFont;
619
//                                                        if (idFieldTypeFontText != -1) {
620
//                                                                StringValue tf = (StringValue) ds
621
//                                                                                .getFieldValue(numReg, idFieldTypeFontText);
622
//                                                                typeFont=tf.getValue();
623
//                                                        } else {
624
//                                                                typeFont=defaultSym.getFont().getFontName();
625
//                                                        }
626
//                                                        int style;
627
//                                                        if (idFieldStyleFontText != -1) {
628
//                                                                NumericValue sf = (NumericValue) ds
629
//                                                                                .getFieldValue(numReg, idFieldStyleFontText);
630
//                                                                style=sf.intValue();
631
//                                                        } else {
632
//                                                                style=defaultSym.getFont().getStyle();
633
//                                                        }
634
//                                                        symbol=new FSymbol(FConstant.SYMBOL_TYPE_TEXT);
635
//                                                        symbol.setFontSizeInPixels(isInPixels());
636
//                                                        symbol.setFont(new Font(typeFont,style,(int)height));
637
//                                                        symbol.setDescription(lbls[j].getString());
638
//                                                        symbol.setFontColor(color);
639
//                                                        aLegend.addSymbol(total,symbol);
640
//                                                }
641
//
642
//                                        }
643
//                                return lbls[j];
644
//
645
//                                }
646
////                        }
647
//
648
//                        if (this.isJoined()){
649
//                                ds.stop();
650
//                        }
651
//                        adapter.stop();
652
//                } catch (DriverIOException e) {
653
//                        e.printStackTrace();
654
//                        throw new DriverException(e);
655
//                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
656
//                        e.printStackTrace();
657
//                        throw new DriverException(e);
658
//                }
659
//                return null;
660
//        }
661
        /*
662
         * (non-Javadoc)
663
         *
664
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.RandomVectorialData#createIndex()
665
         */
666
        public void createSpatialIndex() {
667
                // FJP: ESTO HABR? QUE CAMBIARLO. PARA LAS CAPAS SECUENCIALES, TENDREMOS
668
                // QUE ACCEDER CON UN WHILE NEXT. (O mejorar lo de los FeatureVisitor
669
                // para que acepten recorrer sin geometria, solo con rectangulos.
670

    
671
                //AZABALA: Como no tengo claro de donde se crean las capas de textos
672
                //el ?ndice espacial creado seguir? siendo el Quadtree en memoria
673
                //de JTS (QuadtreeJts es un adapter a nuestra api de indices)
674
                spatialIndex = new QuadtreeJts();
675
                ReadableVectorial va = getSource();
676
                ICoordTrans ct = getCoordTrans();
677
                BoundedShapes shapeBounds = (BoundedShapes) va.getDriver();
678
                try {
679
                        va.start();
680
                        for (int i = 0; i < va.getShapeCount(); i++) {
681
                                Rectangle2D r = null;
682
                                IGeometry geom=va.getShape(i);
683
//                                FSymbol symbol=(FSymbol)aLegend.getSymbol(i);
684
//                                IGeometry geom=aLegend.getTextWrappingGeometry(symbol,i);
685
//                                FLabel label=getLabel(i);
686
                                if (geom != null) {
687
                                        r = geom.getBounds2D();
688
                                } else {
689
                                        r = shapeBounds.getShapeBounds(i);
690
                                }
691
                                // TODO: MIRAR COMO SE TRAGAR?A ESTO LO DE LAS REPROYECCIONES
692
                                if (ct != null) {
693
                                        r = ct.convert(r);
694
                                }
695
                                if (r != null) {
696
//                                        Coordinate c1 = new Coordinate(r.getMinX(), r.getMinY());
697
//                                        Coordinate c2 = new Coordinate(r.getMaxX(), r.getMaxY());
698
//                                        Envelope env = new Envelope(c1, c2);
699
//                                        spatialIndex.insert(env, new Integer(i));
700
                                        spatialIndex.insert(r, i);
701
                                }
702
                        } // for
703
                        va.stop();
704
                } catch (DriverIOException e) {
705
                        e.printStackTrace();
706
                } catch (IOException e) {
707
                        e.printStackTrace();
708
                }
709
        }
710

    
711
        public void setSelectedEditing() throws DriverException {
712
                FBitSet bitSet = getRecordset().getSelection();
713
                if (bitSet.cardinality() == 0)
714
                        return;
715
                indexEditing = bitSet.nextSetBit(0);
716
        }
717

    
718
        public void setInPixels(boolean b) {
719
                inPixels = b;
720
                if (aLegend!=null)
721
                        aLegend.setInPixels(b);
722
        }
723

    
724
        public boolean isInPixels() {
725
                return inPixels;
726
        }
727

    
728
        public void setInEdition(int i) {
729
                indexEditing = i;
730

    
731
        }
732

    
733
        public int getInEdition() {
734
                return indexEditing;
735
        }
736

    
737
//        public ArrayList getLabels() {
738
//                return m_labels;
739
//        }
740

    
741

    
742
        public void setLegend() {
743
                try {
744
                        getSource().getRecordset().start();
745
                        aLegend.setFieldName(getSource().getRecordset().getFieldName(mapping.getColumnText()));
746
                        //aLegend.setDefaultSymbol(new FSymbol(FConstant.SYMBOL_TYPE_TEXT));
747
                        setLegend(aLegend);
748
                        getSource().getRecordset().stop();
749
                } catch (FieldNotFoundException e) {
750
                        e.printStackTrace();
751
                } catch (DriverException e) {
752
                        e.printStackTrace();
753
                } catch (DriverLoadException e) {
754
                        e.printStackTrace();
755
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
756
                        e.printStackTrace();
757
                }
758

    
759
        }
760

    
761
        public Strategy getStrategy() {
762
                return strategy;
763
        }
764

    
765
        public void setEditing(boolean b) throws EditionException {
766
                super.setEditing(b);
767
                try {
768
                if (b) {
769
                                Annotation_EditableAdapter aea = new Annotation_EditableAdapter(
770
                                                this);
771
                                aea.setOriginalVectorialAdapter(((VectorialEditableAdapter)super.getSource()).getOriginalAdapter());
772
                                ((VectorialEditableAdapter)super.getSource()).cancelEdition(EditionEvent.GRAPHIC);
773
                                aea.start();
774
                                // CHEMA
775
                                aea.startEdition(EditionEvent.GRAPHIC);
776
                                setSource(aea);
777
                                getRecordset().setSelectionSupport(
778
                                                aea.getOriginalAdapter().getRecordset()
779
                                                                .getSelectionSupport());
780
                        }else{
781
//                                createLabels();
782
                        }
783
                } catch (DriverIOException e) {
784
                        throw new EditionException();
785
                } catch (DriverLoadException e) {
786
                        throw new EditionException();
787
                } catch (DriverException e) {
788
                        throw new EditionException();
789
                } catch (IOException e) {
790
                        throw new EditionException();
791
                }
792
                deleteSpatialIndex();
793

    
794
        }
795

    
796
        public static FLayer createLayerFromVect(FLyrVect layer) throws DriverException, FieldNotFoundException {
797
                Annotation_Layer la=new Annotation_Layer();
798
                la.setSource(layer.getSource());
799
                la.setRecordset(layer.getRecordset());
800
                la.setProjection(layer.getProjection());
801
                la.setLegend((VectorialLegend)layer.getLegend());
802
                return la;
803
        }
804

    
805
        public void removingThisLayer() {
806
                super.removingThisLayer();
807
//                m_labels.clear();
808
                spatialIndex=null;
809
                aLegend=null;
810
                strategy=null;
811
                System.gc();
812
        }
813
         public IGeometry getTextWrappingGeometry(float height, String description,
814
                                double rotation, int numReg) throws DriverIOException {
815
                        ReadableVectorial adapter = getSource();
816
                        // adapter.start();
817
                        ICoordTrans ct = getCoordTrans();
818
                        IGeometry geom = adapter.getShape(numReg);
819
                        boolean bMustClone = false;
820
                        if (geom == null) {
821
                                return null;
822
                        }
823
                        if (ct != null) {
824
                                if (bMustClone)
825
                                        geom = geom.cloneGeometry();
826
                                geom.reProject(ct);
827
                        }
828
                        // FLabel[] lbls = geom.createLabels(0, true);
829
                        FPoint2D p = ((FPoint2D) geom.getInternalShape());
830
                        // assuming FShape is a point with the starting position of the text
831
                        // Font font = symbol.getFont();
832
                        double h = height;
833
                        if (isInPixels())
834
                                h=getMapContext().getViewPort().toMapDistance(
835
                                                (int) (height));
836
//                        else
837
//                                h=h*FConstant.FONT_HEIGHT_SCALE_FACTOR;
838

    
839
                        Font font = new Font("Arial", 0,
840
                                        (int) (h* FConstant.FONT_HEIGHT_SCALE_FACTOR));
841
                        FontRenderContext frc = new FontRenderContext(new AffineTransform(),
842
                                        false, true);// g.getFontRenderContext();
843
                        GlyphVector gv = font.createGlyphVector(frc, description);
844

    
845
                        /* p.transform(affineTransform); */
846
                        Shape shape = gv.getOutline((float) p.getX(), (float) (p.getY() + h));
847

    
848
                        IGeometry geomResult = ShapeFactory.createPolygon2D(new GeneralPathX(
849
                                        shape.getBounds2D()));
850
//                        IGeometry geomResult2 = ShapeFactory.createPolygon2D(new GeneralPathX(
851
//                                        shape.getBounds2D()));
852
//                        geomResult2.transform(layer.getMapContext().getViewPort().getAffineTransform());
853
//                        Rectangle r = geomResult2.getBounds();
854
                        if (rotation != 0) {
855
                                geomResult.transform(AffineTransform.getRotateInstance(rotation, p
856
                                                .getX(), p.getY()));
857
                        }
858
                        //                  adapter.stop();
859
//                        geomResult.transform(layer.getMapContext().getViewPort().getAffineTransform());
860
                        return geomResult;
861
                }
862
}