Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / FLyrText.java @ 7659

History | View | Annotate | Download (15.2 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 org.cresques.cts.ICoordTrans;
54

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

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

    
87
    private VectorialLegend legend;
88

    
89
    // private Rectangle2D fullExtent;
90
    private FLyrVect assocLyrVect = null;
91

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

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

    
113
        assocLyrVect = lyrVect;
114
        SelectableDataSource ds=null;
115
                        ds = lyrVect.getRecordset();
116

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

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

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

    
140
            sc = (int) ds.getRowCount();
141
            m_labels = new ArrayList(sc);
142

    
143

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

    
153

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

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

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

    
173
                if ((val instanceof NullValue) || (val == null)) {
174
                    continue;
175
                }
176

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

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

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

    
202

    
203
                                }
204

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

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

    
221
    }
222

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

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

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

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

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

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

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

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

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

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

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

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

    
324
                Point2D thePoint = (Point2D) m_labels.get(numReg);
325

    
326

    
327

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

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

    
340
                    if ((val instanceof NullValue) || (val == null)) {
341
                        continue;
342
                    }
343

    
344
                    theSymbol = defaultSym;
345
                    theLabel.setString(val.toString());
346
                    theLabel.setOrig(thePoint);
347

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

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

    
374
    }
375

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

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

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

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

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

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

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

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

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

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