Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / FLyrText.java @ 6311

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

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

    
88
    private VectorialLegend legend;
89

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

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

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

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

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

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

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

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

    
142

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

    
152

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

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

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

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

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

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

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

    
201

    
202
                                }
203

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

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

    
220
    }
221

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

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

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

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

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

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

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

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

    
290
        SelectableDataSource ds=null;
291
                        ds = assocLyrVect.getRecordset();
292
                try {
293
            int fieldId = ds.getFieldIndexByName(legend.getLabelField());
294
            VectorialLegend l = (VectorialLegend) getLegend();
295
            int idFieldHeightText = -1;
296
            int idFieldRotationText = -1;
297

    
298
            if (l.getLabelHeightField() != null) {
299
                // l.setBWithHeightText(true);
300
                idFieldHeightText = ds.getFieldIndexByName(l
301
                        .getLabelHeightField());
302
                l.getDefaultSymbol().setFontSizeInPixels(false);
303
            }
304
            /*
305
             * }else{ l.setBWithHeightText(false); }
306
             */
307

    
308
            // boolean bWithRotationText = false;
309
            if (l.getLabelRotationField() != null) {
310
                // bWithRotationText = true;
311
                idFieldRotationText = ds.getFieldIndexByName(l
312
                        .getLabelRotationField());
313
            }
314

    
315
            for (numReg = 0; numReg < m_labels.size(); numReg++) {
316
                if (cancel.isCanceled()) {
317
                    break;
318
                }
319

    
320
                Point2D thePoint = (Point2D) m_labels.get(numReg);
321

    
322

    
323

    
324
                if (thePoint == null)
325
                    continue;
326
                FPoint2D theShape = new FPoint2D(thePoint);
327
                if (getCoordTrans() != null)
328
                    theShape.reProject(this.getCoordTrans());
329

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

    
336
                    if ((val instanceof NullValue) || (val == null)) {
337
                        continue;
338
                    }
339

    
340
                    theSymbol = getLegend().getDefaultSymbol();
341
                    theLabel.setString(val.toString());
342
                    theLabel.setOrig(thePoint);
343

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

    
359
                    if (idFieldRotationText != -1) {
360
                        DoubleValue rotation = (DoubleValue) ds
361
                                .getFieldValue(numReg, idFieldRotationText);
362
                        theLabel.setRotation(rotation.getValue());
363
                    }
364

    
365
                    FGraphicUtilities.DrawLabel(g, viewPort.getAffineTransform(),
366
                            theShape, theSymbol, theLabel);
367
                }
368
            }
369
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
370
            throw new DriverException(e);
371
        }
372

    
373
    }
374

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

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

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

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

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

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

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

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

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

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