Statistics
| Revision:

svn-gvsig-desktop / tags / v1_1_Build_1008 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / FLyrText.java @ 12520

History | View | Annotate | Download (15.3 KB)

1
/*
2
 * Created on 28-dic-2004
3
 *
4
 */
5
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
6
 *
7
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
22
 *
23
 * For more information, contact:
24
 *
25
 *  Generalitat Valenciana
26
 *   Conselleria d'Infraestructures i Transport
27
 *   Av. Blasco Ib??ez, 50
28
 *   46010 VALENCIA
29
 *   SPAIN
30
 *
31
 *      +34 963862235
32
 *   gvsig@gva.es
33
 *      www.gvsig.gva.es
34
 *
35
 *    or
36
 *
37
 *   IVER T.I. S.A
38
 *   Salamanca 50
39
 *   46005 Valencia
40
 *   Spain
41
 *
42
 *   +34 963163400
43
 *   dac@iver.es
44
 */
45
package com.iver.cit.gvsig.fmap.layers;
46

    
47
import java.awt.Graphics2D;
48
import java.awt.geom.Point2D;
49
import java.awt.geom.Rectangle2D;
50
import java.awt.image.BufferedImage;
51
import java.util.ArrayList;
52

    
53
import javax.print.attribute.PrintRequestAttributeSet;
54

    
55
import org.cresques.cts.ICoordTrans;
56

    
57
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
58
import com.hardcode.gdbms.engine.values.DoubleValue;
59
import com.hardcode.gdbms.engine.values.NullValue;
60
import com.hardcode.gdbms.engine.values.NumericValue;
61
import com.hardcode.gdbms.engine.values.Value;
62
import com.iver.cit.gvsig.fmap.DriverException;
63
import com.iver.cit.gvsig.fmap.ViewPort;
64
import com.iver.cit.gvsig.fmap.core.FPoint2D;
65
import com.iver.cit.gvsig.fmap.core.FShape;
66
import com.iver.cit.gvsig.fmap.core.IGeometry;
67
import com.iver.cit.gvsig.fmap.core.ISymbol;
68
import com.iver.cit.gvsig.fmap.core.v02.FLabel;
69
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
70
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
71
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
72
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial;
73
import com.iver.cit.gvsig.fmap.rendering.Legend;
74
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
75
import com.iver.utiles.swing.threads.Cancellable;
76

    
77
/**
78
 * Capa de texto.
79
 *
80
 * @author FJP
81
 */
82
public class FLyrText extends FLyrDefault implements ClassifiableVectorial {
83
    /**
84
     * <code>m_labels</code> es una arrayList de FLabel (string + punto de
85
     * inserci?n + rotaci?n + altura de texto
86
     */
87
    private ArrayList m_labels = new ArrayList();
88

    
89
    private VectorialLegend legend;
90

    
91
    // private Rectangle2D fullExtent;
92
    private FLyrVect assocLyrVect = null;
93

    
94
    /**
95
     * Crea un nuevo FLyrText.
96
     *
97
     * @param arrayLabels
98
     *            DOCUMENT ME!
99
     * @throws DriverException
100
     * @throws com.hardcode.gdbms.engine.data.driver.DriverException
101
     */
102
    public FLyrText() {
103
    }
104

    
105
    /**
106
     * Esto tiene el fallo de que obligas a una etiqueta por
107
     * entidad, para poder evitar esto, una posible soluci?n
108
     * ser?a que un FLabel pudiera ser una colecci?n de FLabel
109
     * (Patr?n Composite)
110
     * @param lyrVect
111
     * @throws DriverException
112
     */
113
    public void createLabels(FLyrVect lyrVect) throws DriverException {
114

    
115
        assocLyrVect = lyrVect;
116
        SelectableDataSource ds=null;
117
                        ds = lyrVect.getRecordset();
118

    
119
        try {
120
                   ReadableVectorial adapter = lyrVect.getSource();
121
            adapter.start();
122
            ds.start();
123
            int sc;
124
            int fieldId = ds.getFieldIndexByName(legend.getLabelField());
125
            VectorialLegend l = (VectorialLegend) getLegend();
126
            int idFieldHeightText = -1;
127
            int idFieldRotationText = -1;
128
            
129
            FSymbol defaultSym = (FSymbol) l.getDefaultSymbol(); 
130

    
131
            if (l.getLabelHeightField() != null) {
132
                idFieldHeightText = ds.getFieldIndexByName(l
133
                        .getLabelHeightField());
134
                defaultSym.setFontSizeInPixels(false);
135
            }
136

    
137
            if (l.getLabelRotationField() != null) {
138
                idFieldRotationText = ds.getFieldIndexByName(l
139
                        .getLabelRotationField());
140
            }
141

    
142
            sc = (int) ds.getRowCount();
143
            m_labels = new ArrayList(sc);
144

    
145

    
146
            DriverAttributes attr = adapter.getDriverAttributes();
147
            boolean bMustClone = false;
148
            if (attr != null) {
149
                if (attr.isLoadedInMemory()) {
150
                    bMustClone = attr.isLoadedInMemory();
151
                }
152
            }
153
            ICoordTrans ct = getCoordTrans();
154

    
155

    
156
            for (int i = 0; i < sc; i++) {
157
                IGeometry geom = adapter.getShape(i);
158

    
159
                if (geom == null) {
160
                    m_labels.add(null);
161
                    continue;
162
                }
163
                if (ct != null) {
164
                    if (bMustClone)
165
                        geom = geom.cloneGeometry();
166
                    geom.reProject(ct);
167
                }
168

    
169
                // TODO: El m?todo contenedor (createLabelLayer) debe recoger
170
                // los par?metros de posicionamiento y de allowDuplicates
171
                // if (i >= 328)
172
                // System.out.println("i= " + i + " " + val.toString());
173
                Value val = ds.getFieldValue(i, fieldId);
174

    
175
                if ((val instanceof NullValue) || (val == null)) {
176
                    continue;
177
                }
178

    
179
                FLabel[] lbls = geom.createLabels(0, true);
180
                                for (int j = 0; j < lbls.length; j++) {
181
                                        if (lbls[j] != null) {
182
                                                lbls[j].setString(val.toString());
183

    
184
                                                if (idFieldHeightText != -1) {
185
                                                        NumericValue height = (NumericValue) ds.getFieldValue(i,
186
                                                                        idFieldHeightText);
187
                                                        lbls[j].setHeight(height.floatValue());
188
                                                } else {
189
                            // El tama?o del texto va siempre en el simbolo por defecto
190
                            // cuando no hay un campo de altura de texto
191
                            // TODO: Todo esto cambiar? con el nuevo sistema de leyendas...
192
                                                        if (l.getDefaultSymbol()!=null)
193
                                                        lbls[j].setHeight(defaultSym.getFontSize());
194
                                                }
195

    
196
                                                if (idFieldRotationText != -1) {
197
                                                        DoubleValue rotation = (DoubleValue) ds.getFieldValue(i,
198
                                                                        idFieldRotationText);
199
                                                        lbls[j].setRotation(rotation.getValue());
200
                                                }
201
                                                m_labels.add(lbls[j]);
202
                                        }
203

    
204

    
205
                                }
206

    
207
                /* if (lbls[0] == null)
208
                    m_labels.add(null);
209
                else
210
                    m_labels.add(lbls[0].getOrig()); */
211
            }
212

    
213
            ds.stop();
214
            adapter.stop();
215
        } catch (DriverIOException e) {
216
            e.printStackTrace();
217
            throw new DriverException(e);
218
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
219
            e.printStackTrace();
220
            throw new DriverException(e);
221
        }
222

    
223
    }
224

    
225
    /**
226
     * Dibuja sobre el graphics los textos.
227
     *
228
     * @param image
229
     * @param g
230
     *            Graphics.
231
     * @param viewPort
232
     *            ViewPort.
233
     * @param cancel
234
     */
235
    private void drawLabels(BufferedImage image, Graphics2D g,
236
            ViewPort viewPort, Cancellable cancel) {
237
        int numReg;
238
        Rectangle2D elExtent = viewPort.getAdjustedExtent();
239

    
240
        // int anchoMapa;
241
        // int altoMapa;
242
        // double anchoReal;
243
        // double altoReal;
244
        // double escala;
245
        ISymbol theSymbol = null;
246
        System.out.println("Dibujando etiquetas...");
247

    
248
        for (numReg = 0; numReg < m_labels.size(); numReg++) {
249
            if (cancel.isCanceled()) {
250
                break;
251
            }
252

    
253
            FLabel theLabel = (FLabel) m_labels.get(numReg);
254
            if ((theLabel == null) || (theLabel.getOrig() == null))
255
                continue;
256

    
257
            if (elExtent.contains(theLabel.getOrig())) // TODO: Aqui hay que
258
            // ponerle al FLabel un
259
            // getExtent()
260
            {
261
                theSymbol = getLegend().getDefaultSymbol();
262

    
263
                FShape shp = new FPoint2D(theLabel.getOrig().getX(), theLabel
264
                        .getOrig().getY());
265
                
266
                theLabel.draw(g, viewPort.getAffineTransform(), shp, theSymbol);
267
                // FGraphicUtilities.DrawLabel(g, viewPort.getAffineTransform(),
268
                //         shp, theSymbol, theLabel);
269
            }
270
        }
271
    }
272

    
273
    /**
274
     * @deprecated use drawlabels (compatibility with postgis)
275
     * @param image
276
     * @param g
277
     * @param viewPort
278
     * @param cancel
279
     * @throws DriverException
280
     */
281
    private void drawLabels2(BufferedImage image, Graphics2D g,
282
            ViewPort viewPort, Cancellable cancel) throws DriverException {
283
        int numReg;
284
        Rectangle2D elExtent = viewPort.getAdjustedExtent();
285

    
286
        // int anchoMapa;
287
        // int altoMapa;
288
        // double anchoReal;
289
        // double altoReal;
290
        // double escala;
291
        FSymbol theSymbol = null;
292
        System.out.println("Dibujando etiquetas...");
293
        FLabel theLabel = new FLabel();
294

    
295
        SelectableDataSource ds=null;
296
                        ds = assocLyrVect.getRecordset();
297
                try {
298
            int fieldId = ds.getFieldIndexByName(legend.getLabelField());
299
            VectorialLegend l = (VectorialLegend) getLegend();
300
            int idFieldHeightText = -1;
301
            int idFieldRotationText = -1;
302
            FSymbol defaultSym = (FSymbol) l.getDefaultSymbol(); 
303

    
304
            if (l.getLabelHeightField() != null) {
305
                // l.setBWithHeightText(true);
306
                idFieldHeightText = ds.getFieldIndexByName(l
307
                        .getLabelHeightField());
308
                defaultSym.setFontSizeInPixels(false);
309
            }
310
            /*
311
             * }else{ l.setBWithHeightText(false); }
312
             */
313

    
314
            // boolean bWithRotationText = false;
315
            if (l.getLabelRotationField() != null) {
316
                // bWithRotationText = true;
317
                idFieldRotationText = ds.getFieldIndexByName(l
318
                        .getLabelRotationField());
319
            }
320

    
321
            for (numReg = 0; numReg < m_labels.size(); numReg++) {
322
                if (cancel.isCanceled()) {
323
                    break;
324
                }
325

    
326
                Point2D thePoint = (Point2D) m_labels.get(numReg);
327

    
328

    
329

    
330
                if (thePoint == null)
331
                    continue;
332
                FPoint2D theShape = new FPoint2D(thePoint);
333
                if (getCoordTrans() != null)
334
                    theShape.reProject(this.getCoordTrans());
335

    
336
                if (elExtent.contains(theShape.getX(), theShape.getY())) // TODO: Aqui hay que
337
                // ponerle al FLabel un
338
                // getExtent()
339
                {
340
                    Value val = ds.getFieldValue(numReg, fieldId);
341

    
342
                    if ((val instanceof NullValue) || (val == null)) {
343
                        continue;
344
                    }
345

    
346
                    theSymbol = defaultSym;
347
                    theLabel.setString(val.toString());
348
                    theLabel.setOrig(thePoint);
349

    
350
                    if (idFieldHeightText != -1) {
351
                        NumericValue height = (NumericValue) ds
352
                                .getFieldValue(numReg, idFieldHeightText);
353
                        theLabel.setHeight(height.floatValue());
354
                    } else {
355
                        // El tama?o del texto va siempre en el simbolo por
356
                        // defecto
357
                        // cuando no hay un campo de altura de texto
358
                        // TODO: Todo esto cambiar? con el nuevo sistema de
359
                        // leyendas...
360
                        if (defaultSym != null)
361
                            theLabel.setHeight(defaultSym.getFontSize());
362
                    }
363

    
364
                    if (idFieldRotationText != -1) {
365
                        DoubleValue rotation = (DoubleValue) ds
366
                                .getFieldValue(numReg, idFieldRotationText);
367
                        theLabel.setRotation(rotation.getValue());
368
                    }
369
                    theLabel.draw(g, viewPort.getAffineTransform(), theShape, theSymbol);
370
                }
371
            }
372
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
373
            throw new DriverException(e);
374
        }
375

    
376
    }
377

    
378
    /*
379
     * (non-Javadoc)
380
     *
381
     * @see com.iver.cit.gvsig.fmap.layers.FLayer#getFullExtent()
382
     */
383
    public Rectangle2D getFullExtent() throws DriverException {
384
        return assocLyrVect.getFullExtent();
385
    }
386

    
387
    /*
388
     * (non-Javadoc)
389
     *
390
     * @see com.iver.cit.gvsig.fmap.layers.FLayer#draw(java.awt.image.BufferedImage,
391
     *      java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort,
392
     *      com.iver.cit.gvsig.fmap.operations.Cancellable)
393
     */
394
    public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
395
            Cancellable cancel, double scale) throws DriverException {
396
        if (isVisible() && isWithinScale(scale)) {
397
            drawLabels(image, g, viewPort, cancel);
398
        }
399
    }
400

    
401
    /*
402
     * (non-Javadoc)
403
     *
404
     * @see com.iver.cit.gvsig.fmap.layers.FLayer#print(java.awt.Graphics2D,
405
     *      com.iver.cit.gvsig.fmap.ViewPort,
406
     *      com.iver.cit.gvsig.fmap.operations.Cancellable)
407
     */
408
    public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel,
409
            double scale, PrintRequestAttributeSet properties) throws DriverException {
410
        if (isVisible() && isWithinScale(scale)) {
411
            drawLabels(null, g, viewPort, cancel);
412
        }
413
    }
414

    
415
    /*
416
     * (non-Javadoc)
417
     *
418
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial#setLegend(com.iver.cit.gvsig.fmap.rendering.VectorialLegend)
419
     */
420
    public void setLegend(VectorialLegend r) throws DriverException,
421
            FieldNotFoundException {
422
        legend = r;
423
    }
424

    
425
    /*
426
     * (non-Javadoc)
427
     *
428
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.Classifiable#addLegendListener(com.iver.cit.gvsig.fmap.layers.LegendListener)
429
     */
430
    public void addLegendListener(LegendListener listener) {
431
        // TODO Auto-generated method stub
432
    }
433

    
434
    /*
435
     * (non-Javadoc)
436
     *
437
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.Classifiable#removeLegendListener(com.iver.cit.gvsig.fmap.layers.LegendListener)
438
     */
439
    public void removeLegendListener(LegendListener listener) {
440
        // TODO Auto-generated method stub
441
    }
442

    
443
    /*
444
     * (non-Javadoc)
445
     *
446
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.Classifiable#getLegend()
447
     */
448
    public Legend getLegend() {
449
        return legend;
450
    }
451

    
452
    /*
453
     * (non-Javadoc)
454
     *
455
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.Classifiable#getShapeType()
456
     */
457
    public int getShapeType() throws DriverException {
458
        return FShape.TEXT;
459
    }
460

    
461
    /**
462
     * Devuelve un ArrayList con los textos de la capa.
463
     *
464
     * @return Texto de la capa.
465
     */
466
    /* public ArrayList getLabels() {
467
        return m_labels;
468
    } */
469

    
470
    /**
471
     * Inserta los textos de la capa
472
     *
473
     * @param m_labels
474
     *            ArrayList con los textos de la capa.
475
     */
476
    /* public void setLabels(ArrayList m_labels) {
477
        this.m_labels = m_labels;
478
    } */
479
}