Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extAnnotations / src / com / iver / cit / gvsig / fmap / layers / Annotation_Layer.java @ 26937

History | View | Annotate | Download (26.1 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.font.LineMetrics;
52
import java.awt.geom.AffineTransform;
53
import java.awt.geom.Point2D;
54
import java.awt.geom.Rectangle2D;
55
import java.awt.image.BufferedImage;
56
import java.io.File;
57

    
58
import javax.print.attribute.PrintRequestAttributeSet;
59
import javax.print.attribute.standard.PrintQuality;
60

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

    
64
import com.hardcode.driverManager.Driver;
65
import com.hardcode.driverManager.DriverLoadException;
66
import com.hardcode.gdbms.driver.exceptions.InitializeDriverException;
67
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
68
import com.hardcode.gdbms.engine.data.driver.DriverException;
69
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
70
import com.hardcode.gdbms.engine.values.NullValue;
71
import com.hardcode.gdbms.engine.values.NumericValue;
72
import com.hardcode.gdbms.engine.values.StringValue;
73
import com.hardcode.gdbms.engine.values.Value;
74
import com.hardcode.gdbms.engine.values.ValueFactory;
75
import com.iver.andami.messages.NotificationManager;
76
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException;
77
import com.iver.cit.gvsig.exceptions.layers.CancelEditingLayerException;
78
import com.iver.cit.gvsig.exceptions.layers.LegendLayerException;
79
import com.iver.cit.gvsig.exceptions.layers.StartEditionLayerException;
80
import com.iver.cit.gvsig.exceptions.visitors.StartWriterVisitorException;
81
import com.iver.cit.gvsig.exceptions.visitors.VisitorException;
82
import com.iver.cit.gvsig.fmap.MapContext;
83
import com.iver.cit.gvsig.fmap.ViewPort;
84
import com.iver.cit.gvsig.fmap.core.FPoint2D;
85
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
86
import com.iver.cit.gvsig.fmap.core.IGeometry;
87
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
88
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
89
import com.iver.cit.gvsig.fmap.core.symbols.IMarkerSymbol;
90
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
91
import com.iver.cit.gvsig.fmap.core.symbols.ITextSymbol;
92
import com.iver.cit.gvsig.fmap.core.symbols.SimpleTextSymbol;
93
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
94
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
95
import com.iver.cit.gvsig.fmap.drivers.BoundedShapes;
96
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
97
import com.iver.cit.gvsig.fmap.edition.Annotation_EditableAdapter;
98
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
99
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
100
import com.iver.cit.gvsig.fmap.operation.strategies.Annotation_Strategy;
101
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy;
102
import com.iver.cit.gvsig.fmap.operations.strategies.StrategyManager;
103
import com.iver.cit.gvsig.fmap.rendering.Annotation_Legend;
104
import com.iver.cit.gvsig.fmap.rendering.IVectorLegend;
105
import com.iver.cit.gvsig.fmap.spatialindex.QuadtreeJts;
106
import com.iver.utiles.XMLEntity;
107
import com.iver.utiles.swing.threads.Cancellable;
108

    
109

    
110
/**
111
 * Annotation's layer.
112
 *
113
 * @author Vicente Caballero Navarro
114
 */
115
public class Annotation_Layer extends FLyrVect {
116
        private Annotation_Mapping mapping = null;
117
        private int indexEditing = -1;
118
        private Annotation_Legend aLegend;
119
        private Strategy strategy = null;
120
        private IMarkerSymbol symbolPoint = SymbologyFactory.createDefaultMarkerSymbol();
121
        private final static AffineTransform ati=new AffineTransform();
122
        /**
123
         * Crea un nuevo FLyrAnnotation.
124
         */
125
        public Annotation_Layer() {
126
                super();
127
        }
128

    
129
        /**
130
         * DOCUMENT ME!
131
         *
132
         * @param mapping DOCUMENT ME!
133
         * @throws ReadDriverException
134
         * @throws LegendLayerException
135
         * @throws DriverException
136
         * @throws FieldNotFoundException
137
         */
138
        public void setMapping(Annotation_Mapping mapping) throws LegendLayerException, ReadDriverException {
139
                this.mapping = mapping;
140
                aLegend = new Annotation_Legend();
141
                setLegend();
142
        }
143

    
144
        /**
145
         * DOCUMENT ME!
146
         *
147
         * @return DOCUMENT ME!
148
         */
149
        public Annotation_Mapping getAnnotatonMapping() {
150
                return mapping;
151
        }
152

    
153
        /**
154
         * @throws ReadDriverException
155
         * @throws InitializeDriverException
156
         * @see com.iver.cit.gvsig.fmap.layers.LayerOperations#draw(java.awt.image.BufferedImage,
157
         *      java.awt.Graphics2D, ISymbol)
158
         */
159
        public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
160
                        Cancellable cancel, double scale) throws InitializeDriverException, ReadDriverException {
161
                if (isWithinScale(scale)) {
162
                        // Las que solo tienen etiquetado sin pintar el shape,
163
                        // no pasamos por ellas
164
                        boolean bDrawShapes = true;
165

    
166
                        if (bDrawShapes) {
167
                                if (strategy == null) {
168
                                        strategy = new Annotation_Strategy(this);
169
                                }
170
                                g.setColor(Color.black);
171
                                ReadableVectorial adapter = getSource();
172
                                adapter.start();
173
                                drawAnnotations(image,g,viewPort,cancel,null);
174
                                adapter.stop();
175
                        }
176

    
177
                        if (getVirtualLayers() != null) {
178
                                getVirtualLayers().draw(image, g, viewPort, cancel, scale);
179
                        }
180
                }
181
        }
182
        private void drawAnnotations(BufferedImage image, Graphics2D g, ViewPort viewPort,
183
                        Cancellable cancel,PrintRequestAttributeSet properties) {
184
                BufferedImage bi=new BufferedImage(viewPort.getImageWidth(),viewPort.getImageHeight(),BufferedImage.TYPE_INT_ARGB);
185
                Graphics2D gBi=(Graphics2D)bi.getGraphics();
186
                Point2D offset=viewPort.getOffset();
187
                gBi.translate(-offset.getX(),-offset.getY());
188
                Annotation_Legend l = (Annotation_Legend) getLegend();
189
                ITextSymbol sym = (ITextSymbol) l.getDefaultSymbol();
190

    
191
                try {
192
                        ReadableVectorial source = getSource();
193
                        // limit the labeling to the visible extent
194
                        FBitSet bs = queryByRect(viewPort.getAdjustedExtent());
195

    
196
                        SelectableDataSource recordSet = source.getRecordset();
197
                        recordSet.start();
198
                        FBitSet bitSet = recordSet.getSelection();
199
                        Annotation_Mapping mapping = getAnnotatonMapping();
200
                        int idHeightField = mapping.getColumnHeight();
201
                        int idFontName = mapping.getColumnTypeFont();
202
                        int idFontStyle = mapping.getColumnStyleFont();
203
                        int idRotationField = mapping.getColumnRotate();
204
                        int idFontColor = mapping.getColumnColor();
205
                        int idTextField = mapping.getColumnText();
206

    
207
                        double rotation = 0D;
208
                        float size = sym.getFont().getSize();//* (float)FConstant.FONT_HEIGHT_SCALE_FACTOR;
209

    
210
                        String fontName = "Dialog";
211
                        int fontStyle = sym.getFont().getStyle();
212
                        int fontColor = sym.getTextColor().getRGB();
213
                        long t1 = System.currentTimeMillis();
214

    
215
                        for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i + 1)) {
216
                                if (cancel.isCanceled()) {
217
                                        break;
218
                                }
219

    
220
                                Value[] vv = recordSet.getRow(i);
221

    
222
                                if (idHeightField != -1) {
223
                                        // text size is defined in the table
224
                                        try {
225
                                                size = (float)(((NumericValue) vv[idHeightField]).doubleValue());// * FConstant.FONT_HEIGHT_SCALE_FACTOR);
226
                                        } catch (ClassCastException ccEx) {
227
                                                if (!NullValue.class.equals(
228
                                                                vv[idHeightField].getClass())) {
229
                                                }
230
                                                continue;
231
                                        }
232
                                }
233

    
234
                                if (idFontName != -1) {
235
                                        fontName = ((StringValue) vv[idFontName]).toString();
236
                                }
237

    
238
                                if (idFontStyle != -1) {
239
                                        fontStyle = ((NumericValue) vv[idFontStyle]).intValue();
240
                                }
241

    
242
                                if (idRotationField != -1) {
243
                                        // text rotation is defined in the table
244
                                        rotation = ((NumericValue) vv[idRotationField]).doubleValue();
245
                                        ((SimpleTextSymbol)sym).setRotation(Math.toRadians(rotation));
246
                                }
247

    
248
                                if (idFontColor != -1) {
249
                                        // text rotation is defined in the table
250
                                        fontColor = ((NumericValue) vv[idFontColor]).intValue();
251
                                        sym.setTextColor(new Color(fontColor));
252
                                }
253

    
254
                                if (bitSet.get(i)) {
255
                                        sym = (ITextSymbol) sym.getSymbolForSelection();
256
                                }
257

    
258
                                sym.setFont(new Font(fontName, fontStyle, (int) size));
259
                                source.start();
260
                                IGeometry geom = source.getShape(i);
261
                                source.stop();
262
                                ICoordTrans ct = getCoordTrans();
263

    
264
                                boolean bMustClone = false;
265

    
266
                                if (geom == null) {
267
                                        return;
268
                                }
269

    
270
                                if (ct != null) {
271
                                        if (bMustClone) {
272
                                                geom = geom.cloneGeometry();
273
                                        }
274

    
275
                                        geom.reProject(ct);
276
                                }
277
                                geom.transform(viewPort.getAffineTransform());
278
                                Shape shape=geom.getInternalShape();
279
                                FPoint2D fpPixels=null;
280
                                if (!(shape instanceof FPoint2D)) {
281
                                        Rectangle2D rP=shape.getBounds2D();
282
                                        fpPixels=new FPoint2D(rP.getX(),rP.getMaxY());
283
                                }else {
284
                                        fpPixels=(FPoint2D)shape;
285
                                }
286
                                //Si el tama?o de la fuente est? en unidades de mapa.
287
                                int unit=-1;
288

    
289
//                                if (this.getLabelingStrategy()!=null)
290
                                unit=l.getUnits();
291
                                if (unit!=-1){
292
                                        boolean draw=false;
293
                                        Rectangle2D r=getTextWrappingGeometryInPixels(unit,size,vv[idTextField].toString(),rotation,fontName, fontStyle,i,viewPort,geom).getBounds2D();
294
                                        Rectangle2D rPixels=new Rectangle2D.Double(r.getX(),r.getY()+r.getHeight(),r.getWidth(),r.getHeight());
295

    
296
                                        Point2D p=new Point2D.Double(fpPixels.getX(),fpPixels.getY());//-r.getHeight());
297

    
298
                                        if (!isEditing() && l.isAvoidOverLapping()){
299
                                                p=getPoint(bi,rPixels,l.isDelOverLapping());
300
                                                if (p!=null)
301
                                                        draw=true;
302
                                        }else{
303
                                                if (!isEditing() && l.isDelOverLapping()){
304
                                                        if (isOverlapping(bi,rPixels)){
305
                                                                draw=true;
306
                                                        }
307
                                                }else{
308
                                                        draw=true;
309
                                                }
310
                                        }
311
                                        if (draw){
312
                                                if (l.isPointVisible()) {
313
                                                        symbolPoint.draw(gBi,
314
                                                                        ati,
315
                                                                        fpPixels, cancel);
316
                                                }
317
                                                ((SimpleTextSymbol)l.getDefaultSymbol()).setFontSize(r.getHeight());
318
//                                                g.drawRect((int)rPixels.getX(),(int)rPixels.getY(),(int)rPixels.getWidth(),(int)rPixels.getHeight());
319
//FGraphicUtilities.DrawAnnotation(gBi,
320
//                ati, sym, aux[0], metrics, false);
321
                                                drawAnnotation(gBi,unit,this,l, p, vv[idTextField].toString(),viewPort,properties);
322
                                        }
323
                                }else{
324

    
325
                                        // Si el tama?o de la fuente est? en pixels.
326
                                        boolean draw=false;
327
                                        Rectangle2D r=getTextWrappingGeometryInPixels(unit,size,vv[idTextField].toString(),rotation,fontName,fontStyle,i,viewPort,geom).getBounds2D();
328
                                        Rectangle2D rPixels= new Rectangle2D.Double(r.getX(),r.getY()+r.getHeight(),r.getWidth(),r.getHeight());
329

    
330
                                        Point2D pSearch = new Point2D.Double(fpPixels.getX(),fpPixels.getY());//-r.getHeight());
331

    
332
                                        if (!isEditing() && l.isAvoidOverLapping()){
333
                                                pSearch =getPoint(bi,rPixels,l.isDelOverLapping());
334
                                                if (pSearch !=null){
335
                                                        draw=true;
336
                                                }
337
                                        }else{
338
                                                if (!isEditing() && l.isDelOverLapping()){
339
                                                        if (isOverlapping(bi,rPixels)){
340
                                                                draw=true;
341
                                                        }
342
                                                }else{
343
                                                        draw=true;
344
                                                }
345
                                        }
346
                                        if (draw){
347
                                                if (l.isPointVisible()) {
348
                                                        symbolPoint.draw(gBi,
349
                                                                        ati,
350
                                                                        fpPixels,
351
                                                                        cancel);
352
                                                }
353
                                                drawAnnotation(gBi,unit,this,l, pSearch , vv[idTextField].toString(),viewPort,properties);
354
                                        }
355
                                }
356
                                //Al dibujar esta imagen sobre el graphics la trasparencia no puede existir.
357

    
358
                                        if (i%1000==0){
359
                                                if (image!=null)
360
                                                        g.drawImage(bi,0,0,null);
361
                                        }
362
                        }
363
                        gBi.translate(offset.getX(),offset.getY());
364
                        g.drawImage(bi,(int)offset.getX(),(int)offset.getY(),null);
365
                        System.err.println(System.currentTimeMillis()-t1+"millis");
366
                        recordSet.stop();
367
                } catch (Exception e) {
368
                        NotificationManager.addError(e); // TODO esto no puede estar aqu?, pertenece a una capa de abtracci?n superior
369
                }
370
        }
371
        private Point2D getPoint(BufferedImage bi, Rectangle2D rec, boolean b) {
372
                //La altura que se suma aqu? para calcular es posible que sea para corregir un error de SimpleTextSymbol
373
                //que en alg?n momento se solucionar?.
374
                Rectangle2D r=new Rectangle2D.Double(rec.getX(),rec.getY()-rec.getHeight(),rec.getWidth(),rec.getHeight());
375
                if (isOverlapping(bi,r))
376
                        return new Point2D.Double(r.getX(),r.getY()+rec.getHeight());
377

    
378
                r.setFrame(rec.getX(),rec.getY(),rec.getWidth(),rec.getHeight());
379
                if (isOverlapping(bi,r))
380
                        return new Point2D.Double(r.getX(),r.getY()+rec.getHeight());
381

    
382
                r.setFrame(rec.getX()-rec.getWidth(),rec.getY()-rec.getHeight(),rec.getWidth(),rec.getHeight());
383
                if (isOverlapping(bi,r))
384
                        return new Point2D.Double(r.getX(),r.getY()+rec.getHeight());
385

    
386
                r.setFrame(rec.getX()-rec.getWidth(),rec.getY(),rec.getWidth(),rec.getHeight());
387
                if (isOverlapping(bi,r))
388
                        return new Point2D.Double(r.getX(),r.getY()+rec.getHeight());
389

    
390
                r.setFrame(rec.getX()-rec.getWidth()/2,rec.getY(),rec.getWidth(),rec.getHeight());
391
                if (isOverlapping(bi,r))
392
                        return new Point2D.Double(r.getX(),r.getY()+rec.getHeight());
393

    
394
                r.setFrame(rec.getX()-rec.getWidth()/2,rec.getY()-rec.getHeight(),rec.getWidth(),rec.getHeight());
395
                if (isOverlapping(bi,r))
396
                        return new Point2D.Double(r.getX(),r.getY()+rec.getHeight());
397

    
398
                r.setFrame(rec.getX(),rec.getY()+rec.getHeight()/2,rec.getWidth(),rec.getHeight());
399
                if (isOverlapping(bi,r))
400
                        return new Point2D.Double(r.getX(),r.getY()+rec.getHeight());
401

    
402
                if (!b)
403
                        return new Point2D.Double(rec.getX(),rec.getY());
404
                else
405
                        return null;
406
        }
407

    
408
        private boolean isOverlapping(BufferedImage bi, Rectangle2D rPixels) {
409
                for (int i=(int)rPixels.getX();i<rPixels.getMaxX();i++){
410
                        for (int j=(int)rPixels.getY();j<rPixels.getMaxY();j++){
411
                                if (i<0 || j<0 || bi.getWidth()<i+1 || bi.getHeight()<j+1)
412
                                        continue;
413
                                if (bi.getRGB(i,j)!=0){
414
                                        return false;
415
                                }
416
                        }
417
                }
418
                return true;
419
        }
420

    
421
        /**
422
         * @throws ReadDriverException
423
         * @throws InitializeDriverException
424
         * @throws ExpansionFileReadException
425
         * @see com.iver.cit.gvsig.fmap.layers.LayerOperations#getFullExtent()
426
         */
427
        public Rectangle2D getFullExtent() throws InitializeDriverException,
428
        ReadDriverException, ExpansionFileReadException {
429
                Rectangle2D rAux;
430
                getSource().start();
431
                rAux = getSource().getFullExtent();
432
                getSource().stop();
433

    
434
                // Si existe reproyecci?n, reproyectar el extent
435
                ICoordTrans ct = getCoordTrans();
436

    
437
                if (ct != null) {
438
                        Point2D pt1 = new Point2D.Double(rAux.getMinX(), rAux.getMinY());
439
                        Point2D pt2 = new Point2D.Double(rAux.getMaxX(), rAux.getMaxY());
440
                        pt1 = ct.convert(pt1, null);
441
                        pt2 = ct.convert(pt2, null);
442
                        rAux = new Rectangle2D.Double();
443
                        rAux.setFrameFromDiagonal(pt1, pt2);
444
                }
445

    
446
                return rAux;
447
        }
448

    
449
        /**
450
         * @throws ReadDriverException
451
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#print(java.awt.Graphics2D,
452
         *      com.iver.cit.gvsig.fmap.ViewPort,
453
         *      com.iver.utiles.swing.threads.Cancellable)
454
         */
455
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel,
456
                        double scale, PrintRequestAttributeSet properties) throws ReadDriverException{
457
                if (isVisible() && isWithinScale(scale)) {
458
                        drawAnnotations(null,g, viewPort, cancel, properties);
459
                }
460
        }
461

    
462
        /*
463
         * (non-Javadoc)
464
         *
465
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.RandomVectorialData#queryByRect(java.awt.geom.Rectangle2D)
466
         */
467
        public FBitSet queryByRect(Rectangle2D rect) throws ReadDriverException, VisitorException {
468
                Strategy s = StrategyManager.getStrategy(this);
469

    
470
                return s.queryByRect(rect);
471
        }
472

    
473
        /**
474
         * DOCUMENT ME!
475
         *
476
         * @param p DOCUMENT ME!
477
         * @param tolerance DOCUMENT ME!
478
         *
479
         * @return DOCUMENT ME!
480
         * @throws VisitorException
481
         * @throws ReadDriverException
482
         *
483
         * @throws DriverException DOCUMENT ME!
484
         */
485
        public FBitSet queryByPoint(Point2D p, double tolerance) throws ReadDriverException, VisitorException {
486
                Strategy s = StrategyManager.getStrategy(this);
487

    
488
                return s.queryByPoint(p, tolerance);
489
        }
490

    
491
        /**
492
         * DOCUMENT ME!
493
         *
494
         * @param g DOCUMENT ME!
495
         * @param relationship DOCUMENT ME!
496
         *
497
         * @return DOCUMENT ME!
498
         * @throws VisitorException
499
         * @throws ReadDriverException
500
         *
501
         * @throws DriverException DOCUMENT ME!
502
         * @throws VisitException DOCUMENT ME!
503
         */
504
        public FBitSet queryByShape(IGeometry g, int relationship) throws ReadDriverException, VisitorException {
505
                Strategy s = StrategyManager.getStrategy(this);
506

    
507
                return s.queryByShape(g, relationship);
508
        }
509

    
510
        /**
511
         * DOCUMENT ME!
512
         *
513
         * @return DOCUMENT ME!
514
         *
515
         * @throws XMLException
516
         *
517
         * @see com.iver.cit.gvsig.fmap.layers.FLayer#getProperties()
518
         */
519
        public XMLEntity getXMLEntity() throws XMLException {
520
                XMLEntity xml = super.getXMLEntity();
521
                return xml;
522
        }
523

    
524
        /**
525
         * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setXMLEntity(com.iver.utiles.XMLEntity)
526
         */
527
        public void setXMLEntity(XMLEntity xml) throws XMLException {
528
                IProjection proj = null;
529

    
530
                if (xml.contains("proj")) {
531
                        proj = CRSFactory.getCRS(xml.getStringProperty("proj"));
532
                }
533

    
534
                if (xml.contains("file")) {
535
                        Driver d;
536

    
537
                        try {
538
                                d = LayerFactory.getDM().getDriver(xml.getStringProperty(
539
                                                "driverName"));
540
                        } catch (DriverLoadException e1) {
541
                                throw new XMLException(e1);
542
                        }
543

    
544
                        FLyrVect lv = (FLyrVect) LayerFactory.createLayer(xml.getName(),
545
                                        (VectorialFileDriver) d,
546
                                        new File(xml.getStringProperty("file")), proj);
547

    
548
                        try {
549
                                this.setSource(lv.getSource());
550
                                this.setRecordset(lv.getRecordset());
551
                                this.setProjection(lv.getProjection());
552
                                this.setLegend((IVectorLegend) lv.getLegend());
553

    
554

    
555
                                Annotation_Mapping.addAnnotationMapping(this);
556
                        } catch (ReadDriverException e) {
557
                                throw new XMLException(e);
558
                        } catch (LegendLayerException e) {
559
                                throw new XMLException(e);
560
                        }
561
                }
562
                super.setXMLEntity(xml);
563
//                if (this.getLabelingStrategy()==null){
564
//                        boolean isInPixel=((Annotation_Legend)this.getLegend()).isFontSizeInPixels();
565
//                        if (isInPixel){
566
//                                AttrInTableLabelingStrategy labeling = new AttrInTableLabelingStrategy();
567
//                                labeling.setUnit(-1);
568
//                                setLabelingStrategy(labeling);
569
//                        }else{
570
//                                AttrInTableLabelingStrategy labeling = new AttrInTableLabelingStrategy();
571
//                                labeling.setUnit(1);
572
//                                setLabelingStrategy(labeling);
573
//                        }
574
//                }
575
        }
576

    
577
        public Value getSymbolKey(int i) throws ReadDriverException {
578
                SelectableDataSource ds = getRecordset();
579
                String t = new String();
580
                Value val = ds.getFieldValue(i, mapping.getColumnText());
581
                t = val.toString();
582

    
583
                if (mapping.getColumnColor() != -1) {
584
                        Value valColor = ds.getFieldValue(i, mapping.getColumnColor());
585
                        t = t.concat(valColor.toString());
586
                }
587

    
588
                if (mapping.getColumnTypeFont() != -1) {
589
                        Value valTypeFont = ds.getFieldValue(i, mapping.getColumnTypeFont());
590
                        t = t.concat(valTypeFont.toString());
591
                }
592

    
593
                if (mapping.getColumnStyleFont() != -1) {
594
                        Value valStyleFont = ds.getFieldValue(i,
595
                                        mapping.getColumnStyleFont());
596
                        t = t.concat(valStyleFont.toString());
597
                }
598

    
599
                Value total = ValueFactory.createValue(t);
600

    
601
                return total;
602
        }
603

    
604
        /*
605
         * (non-Javadoc)
606
         *
607
         * @see com.iver.cit.gvsig.fmap.layers.layerOperations.RandomVectorialData#createIndex()
608
         */
609
        public void createSpatialIndex()  {
610
                // FJP: ESTO HABR? QUE CAMBIARLO. PARA LAS CAPAS SECUENCIALES, TENDREMOS
611
                // QUE ACCEDER CON UN WHILE NEXT. (O mejorar lo de los FeatureVisitor
612
                // para que acepten recorrer sin geometria, solo con rectangulos.
613
                // AZABALA: Como no tengo claro de donde se crean las capas de textos
614
                // el ?ndice espacial creado seguir? siendo el Quadtree en memoria
615
                // de JTS (QuadtreeJts es un adapter a nuestra api de indices)
616
                spatialIndex = new QuadtreeJts();
617

    
618
                ReadableVectorial va = getSource();
619
                ICoordTrans ct = getCoordTrans();
620
                BoundedShapes shapeBounds = (BoundedShapes) va.getDriver();
621

    
622
                try {
623
                        va.start();
624

    
625
                        for (int i = 0; i < va.getShapeCount(); i++) {
626
                                Rectangle2D r = null;
627
                                IGeometry geom = va.getShape(i);
628
                                if (geom != null) {
629
                                        r = geom.getBounds2D();
630
                                } else {
631
                                        r = shapeBounds.getShapeBounds(i);
632
                                }
633

    
634
                                // TODO: MIRAR COMO SE TRAGAR?A ESTO LO DE LAS REPROYECCIONES
635
                                if (ct != null) {
636
                                        r = ct.convert(r);
637
                                }
638

    
639
                                if (r != null) {
640
                                        spatialIndex.insert(r, i);
641
                                }
642
                        } // for
643

    
644
                        va.stop();
645
                } catch (ExpansionFileReadException e) {
646
                        NotificationManager.addError(this.getName(),e);
647
                } catch (ReadDriverException e) {
648
                        NotificationManager.addError(this.getName(),e);
649
                }
650
        }
651

    
652
        /**
653
         * DOCUMENT ME!
654
         * @throws ReadDriverException
655
         *
656
         * @throws DriverException DOCUMENT ME!
657
         */
658
        public void setSelectedEditing() throws ReadDriverException {
659
                FBitSet bitSet = getRecordset().getSelection();
660

    
661
                if (bitSet.cardinality() == 0) {
662
                        return;
663
                }
664

    
665
                indexEditing = bitSet.nextSetBit(0);
666
        }
667

    
668
        public void setInEdition(int i) {
669
                indexEditing = i;
670
        }
671

    
672
        /**
673
         * DOCUMENT ME!
674
         *
675
         * @return DOCUMENT ME!
676
         */
677
        public int getInEdition() {
678
                return indexEditing;
679
        }
680

    
681
        protected void setLegend() throws ReadDriverException, LegendLayerException {
682
                getSource().getRecordset().start();
683
                aLegend.setFieldName(getSource().getRecordset().getFieldName(mapping.getColumnText()));
684
                setLegend(aLegend);
685
                getSource().getRecordset().stop();
686
        }
687

    
688
        /**
689
         * DOCUMENT ME!
690
         *
691
         * @return DOCUMENT ME!
692
         */
693
        public Strategy getStrategy() {
694
                return strategy;
695
        }
696

    
697
        /**
698
         * DOCUMENT ME!
699
         *
700
         * @param b DOCUMENT ME!
701
         * @throws StartEditionLayerException
702
         *
703
         * @throws EditionException DOCUMENT ME!
704
         */
705
        public void setEditing(boolean b) throws StartEditionLayerException {
706
                super.setEditing(b);
707
                try {
708
                        if (b) {
709
                                Annotation_EditableAdapter aea = new Annotation_EditableAdapter(this);
710

    
711
                                aea.setOriginalVectorialAdapter(((VectorialEditableAdapter) super.getSource()).getOriginalAdapter());
712

    
713
                                ((VectorialEditableAdapter) super.getSource()).cancelEdition(EditionEvent.GRAPHIC);
714
                                aea.start();
715

    
716
                                aea.setCoordTrans(getCoordTrans());
717
                                // CHEMA
718
                                aea.startEdition(EditionEvent.GRAPHIC);
719
                                setSource(aea);
720
                                getRecordset().setSelectionSupport(aea.getOriginalAdapter()
721
                                                .getRecordset()
722
                                                .getSelectionSupport());
723
                                aea.addEditionListener(this);
724
                        } else {
725
                        }
726

    
727
                } catch (ReadDriverException e) {
728
                        throw new StartEditionLayerException(this.getName(),e);
729
                } catch (CancelEditingLayerException e) {
730
                        throw new StartEditionLayerException(this.getName(),e);
731
                } catch (StartWriterVisitorException e) {
732
                        throw new StartEditionLayerException(this.getName(),e);
733
                }
734
                deleteSpatialIndex();
735
        }
736

    
737
        /**
738
         * DOCUMENT ME!
739
         *
740
         * @param layer DOCUMENT ME!
741
         *
742
         * @return DOCUMENT ME!
743
         * @throws ReadDriverException
744
         *
745
         * @throws DriverException DOCUMENT ME!
746
         * @throws FieldNotFoundException DOCUMENT ME!
747
         */
748
        public static Annotation_Layer createLayerFromVect(FLyrVect layer) throws ReadDriverException {
749
                Annotation_Layer la = new Annotation_Layer();
750
                FLyrVect lv=(FLyrVect)LayerFactory.createLayer(layer.getName(),layer.getSource().getDriver(),layer.getProjection());
751
                la.setSource(lv.getSource());
752
                la.setRecordset(lv.getRecordset());
753
                la.setProjection(layer.getProjection());
754
                la.getRecordset().setSelection(layer.getRecordset().getSelection());
755

    
756
                return la;
757
        }
758

    
759
        /**
760
         * DOCUMENT ME!
761
         */
762
        public void removingThisLayer() {
763
//                super.removingThisLayer();
764
                spatialIndex = null;
765
                aLegend = null;
766
                strategy = null;
767
                System.gc();
768
        }
769

    
770
        public IGeometry getTextWrappingGeometryInPixels(int unit,double height, String description,
771
                        double rotation, String type, int style, int numReg,ViewPort vp, IGeometry geom) {
772

    
773
                Shape shapeP=geom.getInternalShape();
774
                FPoint2D p = null;
775
                if (!(shapeP instanceof FPoint2D)) {
776
                        Rectangle2D rP=shapeP.getBounds2D();
777
                        p=new FPoint2D(rP.getX(),rP.getMaxY());
778
                }else {
779
                        p=(FPoint2D)shapeP;
780
                }
781
                if (unit!=-1) {
782
                        height = vp.fromMapDistance((int) (height)*MapContext.getDistanceTrans2Meter()[unit]);
783
                }
784

    
785
                AffineTransform tx = null;
786
                if (rotation != 0) {
787
                        tx = AffineTransform.getRotateInstance(Math.toRadians(-rotation));
788
                }
789
                Font font = new Font(type, style,
790
                                (int) (height));
791
                FontRenderContext frc = new FontRenderContext(tx,
792
                                false, true);
793

    
794
                LineMetrics lm = font.getLineMetrics(description, frc);
795
                GlyphVector gv = font.createGlyphVector(frc, description);
796
                double visualBoundsHeight = gv.getVisualBounds().getHeight();
797
                Shape shape = gv.getOutline((float) p.getX(), (float) (p.getY()));// - height));
798
                double shapeBoundsHeight = shape.getBounds().getHeight();
799
                tx = AffineTransform.getTranslateInstance(0, shapeBoundsHeight);
800
                shape = tx.createTransformedShape(shape);
801

    
802
                Rectangle2D rGeom=shape.getBounds2D();
803
                IGeometry geomResult = ShapeFactory.createPolygon2D(new GeneralPathX(
804
                                rGeom));
805

    
806

    
807
                return geomResult;
808
        }
809

    
810
public FLayer cloneLayer() throws Exception {
811
                Annotation_Layer clonedLayer = new Annotation_Layer();
812
                clonedLayer.setSource(getSource());
813
                clonedLayer.setVisible(isVisible());
814
                clonedLayer.setISpatialIndex(getISpatialIndex());
815
                clonedLayer.setName(getName());
816
                clonedLayer.setCoordTrans(getCoordTrans());
817
                clonedLayer.setLegend((IVectorLegend)getLegend());
818
                clonedLayer.mapping=mapping;
819
                clonedLayer.aLegend=aLegend;
820
                return clonedLayer;
821
        }
822
        private static void drawAnnotation(Graphics2D g2,int unit,Annotation_Layer layer,
823
                        Annotation_Legend legend,Point2D pAux, String text,ViewPort viewPort,PrintRequestAttributeSet properties) {
824
                SimpleTextSymbol textSymbol=(SimpleTextSymbol)legend.getDefaultSymbol();
825
                textSymbol.setText(text);
826
//                float x;
827
//                float y;
828
                // Las etiquetas que pongamos a nulo ser? porque no la queremos dibujar.
829
                // ?til para cuando queramos eliminar duplicados.
830
                if (text == null) {
831
                        return;
832
                }
833
                int size=textSymbol.getFont().getSize();
834
                double resolutionPrinting=0;
835
                if (unit!=-1){
836
                        size=viewPort.fromMapDistance(size*MapContext.getDistanceTrans2Meter()[unit]);
837
                }
838
                if (properties!=null){
839
                        PrintQuality resolution=(PrintQuality)properties.get(PrintQuality.class);
840
                        if (resolution.equals(PrintQuality.NORMAL)){
841
                                resolutionPrinting=300/72;
842
                        }else if (resolution.equals(PrintQuality.HIGH)){
843
                                resolutionPrinting=600/72;
844
                        }else if (resolution.equals(PrintQuality.DRAFT)){
845
                                resolutionPrinting=72/72;
846
                        }
847
                        size=(int)(size*resolutionPrinting);
848
                }
849

    
850
                textSymbol.draw(g2,viewPort.getAffineTransform(),new FPoint2D(pAux.getX(),pAux.getY()), null);
851
        }
852
}