Statistics
| Revision:

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

History | View | Annotate | Download (15.3 KB)

1 745 fjp
/*
2
 * Created on 28-dic-2004
3
 *
4
 */
5 1100 fjp
/* 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 745 fjp
package com.iver.cit.gvsig.fmap.layers;
46
47 3247 fjp
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 9013 caballero
import javax.print.attribute.PrintRequestAttributeSet;
54
55 3247 fjp
import org.cresques.cts.ICoordTrans;
56
57 1034 vcaballero
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
58 3247 fjp
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 745 fjp
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 3247 fjp
import com.iver.cit.gvsig.fmap.core.IGeometry;
67 8765 jjdelcerro
import com.iver.cit.gvsig.fmap.core.ISymbol;
68 745 fjp
import com.iver.cit.gvsig.fmap.core.v02.FLabel;
69
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
70 3247 fjp
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
71
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
72 745 fjp
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 5317 fjp
import com.iver.utiles.swing.threads.Cancellable;
76 745 fjp
77
/**
78 1034 vcaballero
 * Capa de texto.
79 3940 caballero
 *
80 745 fjp
 * @author FJP
81
 */
82
public class FLyrText extends FLyrDefault implements ClassifiableVectorial {
83 3247 fjp
    /**
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 745 fjp
89 3247 fjp
    private VectorialLegend legend;
90 745 fjp
91 3247 fjp
    // private Rectangle2D fullExtent;
92 3940 caballero
    private FLyrVect assocLyrVect = null;
93 745 fjp
94 3247 fjp
    /**
95
     * Crea un nuevo FLyrText.
96 3940 caballero
     *
97 3247 fjp
     * @param arrayLabels
98
     *            DOCUMENT ME!
99
     * @throws DriverException
100
     * @throws com.hardcode.gdbms.engine.data.driver.DriverException
101
     */
102
    public FLyrText() {
103
    }
104 745 fjp
105 3247 fjp
    /**
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 745 fjp
115 3247 fjp
        assocLyrVect = lyrVect;
116 3963 caballero
        SelectableDataSource ds=null;
117
                        ds = lyrVect.getRecordset();
118 3247 fjp
119 3940 caballero
        try {
120
                   ReadableVectorial adapter = lyrVect.getSource();
121 3247 fjp
            adapter.start();
122
            ds.start();
123
            int sc;
124 3769 fjp
            int fieldId = ds.getFieldIndexByName(legend.getLabelField());
125
            VectorialLegend l = (VectorialLegend) getLegend();
126
            int idFieldHeightText = -1;
127
            int idFieldRotationText = -1;
128 8765 jjdelcerro
129
            FSymbol defaultSym = (FSymbol) l.getDefaultSymbol();
130 3940 caballero
131 3769 fjp
            if (l.getLabelHeightField() != null) {
132
                idFieldHeightText = ds.getFieldIndexByName(l
133
                        .getLabelHeightField());
134 8765 jjdelcerro
                defaultSym.setFontSizeInPixels(false);
135 3769 fjp
            }
136 3940 caballero
137 3769 fjp
            if (l.getLabelRotationField() != null) {
138
                idFieldRotationText = ds.getFieldIndexByName(l
139
                        .getLabelRotationField());
140
            }
141 3247 fjp
142
            sc = (int) ds.getRowCount();
143
            m_labels = new ArrayList(sc);
144 1034 vcaballero
145 3940 caballero
146 3247 fjp
            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 1034 vcaballero
155 745 fjp
156 3247 fjp
            for (int i = 0; i < sc; i++) {
157
                IGeometry geom = adapter.getShape(i);
158 745 fjp
159 3247 fjp
                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 745 fjp
169 3247 fjp
                // 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 3769 fjp
                Value val = ds.getFieldValue(i, fieldId);
174
175
                if ((val instanceof NullValue) || (val == null)) {
176
                    continue;
177
                }
178
179 3247 fjp
                FLabel[] lbls = geom.createLabels(0, true);
180 3769 fjp
                                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 3940 caballero
                                                                        idFieldHeightText);
187 3769 fjp
                                                        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 8765 jjdelcerro
                                                        lbls[j].setHeight(defaultSym.getFontSize());
194 3769 fjp
                                                }
195
196
                                                if (idFieldRotationText != -1) {
197
                                                        DoubleValue rotation = (DoubleValue) ds.getFieldValue(i,
198
                                                                        idFieldRotationText);
199
                                                        lbls[j].setRotation(rotation.getValue());
200
                                                }
201 3940 caballero
                                                m_labels.add(lbls[j]);
202 3769 fjp
                                        }
203
204 3940 caballero
205 3769 fjp
                                }
206 3940 caballero
207 3769 fjp
                /* if (lbls[0] == null)
208 3247 fjp
                    m_labels.add(null);
209
                else
210 3769 fjp
                    m_labels.add(lbls[0].getOrig()); */
211 3247 fjp
            }
212 745 fjp
213 3247 fjp
            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 745 fjp
223 3247 fjp
    }
224 745 fjp
225 3247 fjp
    /**
226
     * Dibuja sobre el graphics los textos.
227 3940 caballero
     *
228 3247 fjp
     * @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 745 fjp
240 3247 fjp
        // int anchoMapa;
241
        // int altoMapa;
242
        // double anchoReal;
243
        // double altoReal;
244
        // double escala;
245 8765 jjdelcerro
        ISymbol theSymbol = null;
246 3247 fjp
        System.out.println("Dibujando etiquetas...");
247 745 fjp
248 3247 fjp
        for (numReg = 0; numReg < m_labels.size(); numReg++) {
249
            if (cancel.isCanceled()) {
250
                break;
251
            }
252 1034 vcaballero
253 3247 fjp
            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 8765 jjdelcerro
266
                theLabel.draw(g, viewPort.getAffineTransform(), shp, theSymbol);
267
                // FGraphicUtilities.DrawLabel(g, viewPort.getAffineTransform(),
268
                //         shp, theSymbol, theLabel);
269 3247 fjp
            }
270
        }
271
    }
272
273 3769 fjp
    /**
274
     * @deprecated use drawlabels (compatibility with postgis)
275
     * @param image
276
     * @param g
277
     * @param viewPort
278
     * @param cancel
279
     * @throws DriverException
280
     */
281 3247 fjp
    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 3963 caballero
        SelectableDataSource ds=null;
296
                        ds = assocLyrVect.getRecordset();
297
                try {
298 3247 fjp
            int fieldId = ds.getFieldIndexByName(legend.getLabelField());
299
            VectorialLegend l = (VectorialLegend) getLegend();
300
            int idFieldHeightText = -1;
301
            int idFieldRotationText = -1;
302 8765 jjdelcerro
            FSymbol defaultSym = (FSymbol) l.getDefaultSymbol();
303 3940 caballero
304 3247 fjp
            if (l.getLabelHeightField() != null) {
305
                // l.setBWithHeightText(true);
306
                idFieldHeightText = ds.getFieldIndexByName(l
307
                        .getLabelHeightField());
308 8765 jjdelcerro
                defaultSym.setFontSizeInPixels(false);
309 3247 fjp
            }
310
            /*
311
             * }else{ l.setBWithHeightText(false); }
312
             */
313 3940 caballero
314 3247 fjp
            // boolean bWithRotationText = false;
315
            if (l.getLabelRotationField() != null) {
316
                // bWithRotationText = true;
317
                idFieldRotationText = ds.getFieldIndexByName(l
318
                        .getLabelRotationField());
319
            }
320 3940 caballero
321 3247 fjp
            for (numReg = 0; numReg < m_labels.size(); numReg++) {
322
                if (cancel.isCanceled()) {
323
                    break;
324
                }
325 3940 caballero
326 3247 fjp
                Point2D thePoint = (Point2D) m_labels.get(numReg);
327 3940 caballero
328
329
330 3247 fjp
                if (thePoint == null)
331
                    continue;
332 3449 fjp
                FPoint2D theShape = new FPoint2D(thePoint);
333
                if (getCoordTrans() != null)
334
                    theShape.reProject(this.getCoordTrans());
335 3940 caballero
336 3438 caballero
                if (elExtent.contains(theShape.getX(), theShape.getY())) // TODO: Aqui hay que
337 3247 fjp
                // 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 8765 jjdelcerro
                    theSymbol = defaultSym;
347 3247 fjp
                    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 8765 jjdelcerro
                        if (defaultSym != null)
361
                            theLabel.setHeight(defaultSym.getFontSize());
362 3247 fjp
                    }
363
364
                    if (idFieldRotationText != -1) {
365
                        DoubleValue rotation = (DoubleValue) ds
366
                                .getFieldValue(numReg, idFieldRotationText);
367
                        theLabel.setRotation(rotation.getValue());
368
                    }
369 8765 jjdelcerro
                    theLabel.draw(g, viewPort.getAffineTransform(), theShape, theSymbol);
370 3247 fjp
                }
371
            }
372
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
373
            throw new DriverException(e);
374
        }
375 3940 caballero
376 3247 fjp
    }
377 3940 caballero
378 3247 fjp
    /*
379
     * (non-Javadoc)
380 3940 caballero
     *
381 3247 fjp
     * @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 3940 caballero
     *
390 3247 fjp
     * @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 3769 fjp
            drawLabels(image, g, viewPort, cancel);
398 3247 fjp
        }
399
    }
400
401
    /*
402
     * (non-Javadoc)
403 3940 caballero
     *
404 3247 fjp
     * @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 9013 caballero
            double scale, PrintRequestAttributeSet properties) throws DriverException {
410 3247 fjp
        if (isVisible() && isWithinScale(scale)) {
411 3769 fjp
            drawLabels(null, g, viewPort, cancel);
412 3247 fjp
        }
413
    }
414
415
    /*
416
     * (non-Javadoc)
417 3940 caballero
     *
418 3247 fjp
     * @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 3940 caballero
     *
428 3247 fjp
     * @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 3940 caballero
     *
437 3247 fjp
     * @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 3940 caballero
     *
446 3247 fjp
     * @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 3940 caballero
     *
455 3247 fjp
     * @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 3940 caballero
     *
464 3247 fjp
     * @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 3940 caballero
     *
473 3247 fjp
     * @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 745 fjp
}