Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / gui / layout / fframes / FFrameText.java @ 2273

History | View | Annotate | Download (17 KB)

1
/*
2
 * Created on 22-jun-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.gui.layout.fframes;
46

    
47
import java.awt.Color;
48
import java.awt.Font;
49
import java.awt.Graphics2D;
50
import java.awt.font.FontRenderContext;
51
import java.awt.font.TextLayout;
52
import java.awt.geom.AffineTransform;
53
import java.awt.geom.Rectangle2D;
54
import java.awt.image.BufferedImage;
55
import java.util.ArrayList;
56

    
57
import com.iver.andami.PluginServices;
58
import com.iver.cit.gvsig.gui.layout.Layout;
59
import com.iver.utiles.StringUtilities;
60
import com.iver.utiles.XMLEntity;
61

    
62

    
63
/**
64
 * FFrame para introducir un texto en el Layout.
65
 *
66
 * @author Vicente Caballero Navarro
67
 */
68
public class FFrameText extends FFrame {
69
        /** Localizaci?n del texto. */
70
        public static final int LEFT = 0;
71
        public static final int CENTER = 1;
72
        public static final int RIGTH = 2;
73
        private ArrayList m_text = new ArrayList();
74
        private boolean m_isFixed = false;
75
        private double m_rotation = 0;
76
        private int m_pos = LEFT;
77
        private Color textColor = Color.BLACK;
78
        //jaume
79
        private boolean surrounded = false;         // The text field is surrounded by a rectangle
80
        private int cellPadding = 0;                        // The gap between the the text field and the surrounding rectangle 
81
        private boolean fixedFontSize = false;        // The text field font size is constant fixed to the folio's size
82
        private int fontSize;                                        // Text field's font size
83
        private boolean hasTitle;                                // The text field has title
84
        private String title;                                        // The text for the title
85
        private int titleSize;                                        // The title's font size
86
        private int frameBorderSize;                        // The surrounding rectangle's border size
87
    private Color frameColor=Color.BLACK;        // The surrounding rectangle's color
88
    private Color titleColor=Color.BLACK;        // The title's color
89
    
90
        //private int m_space=0;
91
        private Font m_f = null;
92
   
93
        /**
94
         * Crea un nuevo FFrameText.
95
         */
96
//        public FFrameText(Layout l) {
97
//            layout=l;
98
//                m_f = new Font("SansSerif", Font.PLAIN, 9);
99
//        }
100
        public FFrameText() {
101
            m_f = new Font("SansSerif", Font.PLAIN, 9);
102
        }
103

    
104
        /**
105
         * Inserta la fuente del texto.
106
         *
107
         * @param f Fuente del texto.
108
         */
109
        public void setFont(Font f) {
110
                m_f = f;
111
        }
112

    
113
        /**
114
         * Devuelve el color del texto del FFrameText.
115
         *
116
         * @return Color del texto.
117
         */
118
        public Color getTextColor() {
119
                return textColor;
120
        }
121

    
122
    /**
123
     * Obtiene el fixedFontSize
124
     * @return boolean
125
     */
126

    
127
    public boolean isFontSizeFixed() {
128
        return fixedFontSize;
129
    }
130
    /**
131
     * Establece fixedFontSize
132
     * @param fixedFontSize
133
     */
134
    public void setFixedFontSize(boolean fixedFontSize) {
135
        this.fixedFontSize = fixedFontSize;
136
    }
137
    /**
138
     * Obtiene el fontSize
139
     * @return int
140
     */
141

    
142
    public int getFontSize() {
143
        return fontSize;
144
    }
145
    /**
146
     * Establece fontSize
147
     * @param fontSize
148
     */
149
    public void setFontSize(int fontSize) {
150
        this.fontSize = fontSize;
151
    }
152
    /**
153
     * Obtiene el cellPadding
154
     * @return int
155
     */
156

    
157
    public int getCellPadding() {
158
        return cellPadding;
159
    }
160
        /**
161
         * Inserta el color del texto a escribir.
162
         *
163
         * @param color Color del texto.
164
         */
165
        public void setTextColor(Color color) {
166
                textColor = color;
167
        }
168

    
169
        /**
170
         * Devuelve la fuente del texto.
171
         *
172
         * @return Fuente del texto.
173
         */
174
        public Font getFont() {
175
                return new Font(m_f.getFontName(), m_f.getStyle(), 9);
176
        }
177

    
178
        /*public void setSpace(int s){
179
           m_space=s;
180
           }
181
           public int getSpace(){
182
                   return m_space;
183
           }*/
184

    
185
        /**
186
         * Devuelve la posici?n izquierda, centro o derecha del texto.
187
         *
188
         * @return Posici?n del texto.
189
         */
190
        public int getPos() {
191
                return m_pos;
192
        }
193

    
194
        /**
195
         * Pone la posici?n izquierda, centro o derecha del texto.
196
         *
197
         * @param p 0=LEFT,1=CENTER,2=RIGTH.
198
         */
199
        public void setPos(int p) {
200
                m_pos = p;
201
        }
202

    
203
        /**
204
         * M?todo que dibuja sobre el graphics que se le pasa como par?metro, seg?n
205
         * la transformada afin que se debe de aplicar y el rect?ngulo que se debe
206
         * de dibujar.
207
         *
208
         * @param g Graphics
209
         * @param at Transformada af?n.
210
         * @param rv rect?ngulo sobre el que hacer un clip.
211
         * @param imgBase Imagen para acelerar el dibujado.
212
         */
213
        public void draw(Graphics2D g, AffineTransform at, Rectangle2D rv,
214
                BufferedImage imgBase) {
215
                g.setColor(Color.BLACK);
216
            Rectangle2D.Double rec = getBoundingBox(at);
217
                Rectangle2D.Double raux = (Rectangle2D.Double)rec.clone();
218
                int longmax = 1;
219
                if (intersects(rv, raux)) { // comprueba que no cae fuera de la pantalla
220
                        if (m_text.isEmpty()) {
221
                                drawEmpty(g);
222
                        } else {
223
                                for (int i = 0; i < m_text.size(); i++) {
224
                                        if (((String) m_text.get(i)).length() > longmax) {
225
                                                longmax = ((String) m_text.get(i)).length();
226
                                        }
227
                                }
228
                                FontRenderContext frc = g.getFontRenderContext();
229
                                int scaledFontSize;
230
                                
231
                                // TODO myScale es la escala sobre la que se extraen todos los escalados. Esto
232
                                // funciona bien tal cual est? si la ratio de aspecto (alto/ancho) del folio es constante
233
                                // porque se toma como medidas el ancho del folio real y el ancho del folio en pantalla.
234
                                // No se que pasar? si la ratio cambia, por ejemplo si se usan USLetter en lugar de DIN A4
235
                                
236
                                double myScale = at.getScaleX()*0.024;//FLayoutUtilities.fromSheetDistance(folio.getAncho(),at)/rv.getWidth();
237
                                
238
                                // Distinguish when the font has fixed size or not
239
                                if (isFontSizeFixed()){
240
                                scaledFontSize = (int)(myScale*fontSize);
241
                            } else {
242
                                scaledFontSize = ((int) (raux.width * 2.2)) / longmax;
243

    
244
                                        if (scaledFontSize > (int) ((raux.height * 10) / 4 / (m_text.size() * 1.3))) {
245
                                                scaledFontSize = (int) (((raux.height * 10) / 4) / (m_text.size() * 1.3));
246
                                        } 
247
                            }
248
                                
249
                                
250
                                
251
                                int ht = (int) (raux.height / m_text.size());
252

    
253
                                if (m_f != null) {
254
                                    // esto sirve de algo?
255
                                        m_f = new Font(m_f.getFontName(), m_f.getStyle(), scaledFontSize);
256
                                } else {
257
                                        m_f = new Font("SansSerif", Font.PLAIN, scaledFontSize);
258
                                }
259

    
260
                                // Draw the text title if it exists
261
                                if (hasTitle()){
262
                                    int scaledTitleFontSize = (int) (myScale*titleSize);
263
                                    int gap = 3;
264
                                    if (isSurrounded()){
265
                                        // Para evitar que el marco se pinte sobre el t?tulo
266
                                        gap += (int) (frameBorderSize*myScale);
267
                                    }
268
                                        
269
                                    g.setColor(titleColor);
270
                                    Font titleFont = new Font(m_f.getFontName(), m_f.getStyle(), scaledTitleFontSize);
271
                                    TextLayout titleTextLayout = new TextLayout(getTitle(), titleFont, frc);
272
                                    titleTextLayout.draw(g, (float) raux.getX(), (float) (raux.getY()-(gap*myScale)));
273
                                }
274
                                
275
                                // Draw the frame involving the text if it exists
276
                                if (isSurrounded()){
277
                                    g.setColor(frameColor);
278
                                    g.drawRect((int) raux.x, (int) raux.y, (int)raux.width, (int)raux.height);
279
                                    double scaledCellPadding = cellPadding*myScale;
280
                                    if (frameBorderSize > 1){
281
                                        System.out.println("borderSize = "+frameBorderSize);
282
                                        int scaledBorderSize = (int) (frameBorderSize*myScale);
283
                                        for (int i=scaledBorderSize-1; i>0; i--)
284
                                            g.drawRect((int) raux.x-i, (int) raux.y-i, (int) raux.width+2*i, (int) raux.height+2*i);
285
                                        
286
                                    }
287
                                    
288
                                    
289
                                    // Recalibro el rectangulo para establecer el ?rea donde se dibujan las fuentes
290
                                    // al ?rea marcada con el rat?n menos la distancia al marco especificada
291
                                    raux.setRect(raux.getX() + scaledCellPadding, raux.getY() - scaledCellPadding, 
292
                                               raux.getWidth()- scaledCellPadding*2, raux.getHeight()- scaledCellPadding*2);
293
                                    
294
                                }
295
                                
296
                                for (int i = 0; i < m_text.size(); i++) {
297
                                        if (!((String) m_text.get(i)).equals("")) {
298
                                            TextLayout textLayout = new TextLayout((String) m_text.get(
299
                                                                        i), m_f, frc);
300
                                                g.setColor(textColor);
301
                                                g.rotate(Math.toRadians(m_rotation),
302
                                                        raux.x + (raux.width / 2), raux.y + (raux.height / 2));
303
                                        
304
                                                int l = ((String) m_text.get(i)).length();
305
                                                
306
                                                switch (m_pos) {
307
                                                        case (LEFT):
308
                                                            textLayout.draw(g, (float) raux.getX(),
309
                                                                (float) (raux.getY() + (ht * (i +1))));
310
                                                           
311
                                                                break;
312

    
313
                                                        case (CENTER):
314
                                                            float pos1 = (float) ((raux.width -
315
                                                                    ((l * scaledFontSize) / 2.2)) / 2);
316
                                                            textLayout.draw(g, (float) raux.getX() + pos1,
317
                                                                    (float) (raux.getY() + (ht * (i + 1))));
318
                                                            
319
                                                                break;
320

    
321
                                                        case (RIGTH):
322
                                                            float pos2 = (float) ((raux.width -
323
                                                                    ((l * scaledFontSize) / 2.2)));
324
                                                                textLayout.draw(g, (float) raux.getX() + pos2,
325
                                                                    (float) (raux.getY() + (ht * (i + 1)))); //- (ht / 2))));
326
                                                            break;
327
                                                }
328

    
329
                                                g.rotate(Math.toRadians(-m_rotation),
330
                                                        raux.x + (raux.width / 2), raux.y + (raux.height / 2));
331
                                                
332
                                        }
333
                                        
334
                                }
335
                        }
336
                }
337
                raux = null;
338
        }
339

    
340
        /**
341
         * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#print(java.awt.Graphics2D,
342
         *                 java.awt.geom.AffineTransform)
343
         */
344
        public void print(Graphics2D g, AffineTransform at) {
345
                draw(g, at, null, null);
346
        }
347

    
348
        /**
349
         * Rellenar el texto que se quiere a?adir al Layout.
350
         *
351
         * @param s String a a?adir.
352
         */
353
        public void addText(String s) {
354
                m_text.add(s);
355
        }
356

    
357
        /**
358
         * Devuelve el ArrayList que contiene las l?neas de texto.
359
         *
360
         * @return ArrayList.
361
         */
362
        public ArrayList getText() {
363
                return m_text;
364
        }
365

    
366
        /**
367
         * Seleccionar si se quiere un tama?o fijo o adecuado a la escala.
368
         *
369
         * @param b true si se quiere tama?o fijo.
370
         */
371
        public void setSizeFixed(boolean b) {
372
                m_isFixed = b;
373
        }
374

    
375
        /**
376
         * Devuelve si est? fijado el tama?o.
377
         *
378
         * @return True si est? fijado el tama?o.
379
         */
380
        public boolean isSizeFixed() {
381
                return m_isFixed;
382
        }
383

    
384
        /**
385
         * Rellenar la rotaci?n para aplicar al texto.
386
         *
387
         * @param rotation rotaci?n que se quiere aplicar.
388
         */
389
        public void setRotation(double rotation) {
390
                m_rotation = rotation;
391
        }
392

    
393
        /**
394
         * Devuelve la rotaci?n del texto.
395
         *
396
         * @return Rotaci?n del texto.
397
         */
398
        public double getRotation() {
399
                return m_rotation;
400
        }
401

    
402
        /**
403
         * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#getXMLEntity()
404
         */
405
        public XMLEntity getXMLEntity() {
406
                XMLEntity xml = new XMLEntity();
407
                xml.putProperty("className", this.getClass().getName());
408
                xml.putProperty("m_name", m_name);
409
                xml.putProperty("x", getBoundBox().x);
410
                xml.putProperty("y", getBoundBox().y);
411
                xml.putProperty("w", getBoundBox().width);
412
                xml.putProperty("h", getBoundBox().height);
413
                xml.putProperty("m_Selected", m_Selected);
414
                xml.putProperty("type", Layout.RECTANGLETEXT);
415

    
416
                String[] s = (String[]) m_text.toArray(new String[0]);
417
                xml.putProperty("s", s);
418
                xml.putProperty("m_isFixed", m_isFixed);
419

    
420
                xml.putProperty("m_pos", m_pos);
421
                xml.putProperty("m_rotation", m_rotation);
422
                xml.putProperty("fontName", m_f.getFontName());
423
                xml.putProperty("fontStyle", m_f.getStyle());
424
                xml.putProperty("tag", getTag());
425
                xml.putProperty("textColor",StringUtilities.color2String(textColor));
426
                
427
                // jaume
428
                xml.putProperty("cellPadding", cellPadding);
429
                xml.putProperty("fontSize", fontSize);
430
                xml.putProperty("fixedFontSize", fixedFontSize);
431
                xml.putProperty("surrounded", surrounded);
432
                xml.putProperty("hasTitle", hasTitle);
433
                xml.putProperty("title", title);
434
                xml.putProperty("titleSize", titleSize);
435
                xml.putProperty("frameBorderSize", frameBorderSize);
436
                xml.putProperty("frameColor", StringUtilities.color2String(frameColor));
437
                xml.putProperty("titleColor", StringUtilities.color2String(titleColor));
438
                return xml;
439
        }
440
        
441
        /**
442
         * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#setXMLEntity(com.iver.utiles.XMLEntity,
443
         *                 com.iver.cit.gvsig.project.Project)
444
         */
445
        public void setXMLEntity03(XMLEntity xml, Layout l) {
446
                if (xml.getIntProperty("m_Selected") != 0) {
447
                        this.setSelected(true);
448
                } else {
449
                        this.setSelected(false);
450
                }
451

    
452
                String[] s = xml.getStringArrayProperty("s");
453

    
454
                for (int i = 0; i < s.length; i++) {
455
                        this.m_text.add(s[i]);
456
                }
457

    
458
                this.m_isFixed = xml.getBooleanProperty("m_isFixed");
459
                this.m_pos = xml.getIntProperty("m_pos");
460
                this.m_rotation = xml.getDoubleProperty("m_rotation");
461
                this.m_f = new Font(xml.getStringProperty("fontName"),
462
                                xml.getIntProperty("fontStyle"), 9);
463
                if (xml.contains("textColor"))
464
                        this.textColor = StringUtilities.string2Color(xml.getStringProperty("textColor"));
465
                
466
        }
467
        
468
        /**
469
         * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#setXMLEntity(com.iver.utiles.XMLEntity,
470
         *                 com.iver.cit.gvsig.project.Project)
471
         */
472
        public void setXMLEntity(XMLEntity xml, Layout l) {
473
                if (xml.getIntProperty("m_Selected") != 0) {
474
                        this.setSelected(true);
475
                } else {
476
                        this.setSelected(false);
477
                }
478

    
479
                String[] s = xml.getStringArrayProperty("s");
480

    
481
                for (int i = 0; i < s.length; i++) {
482
                        this.m_text.add(s[i]);
483
                }
484

    
485
                this.m_isFixed = xml.getBooleanProperty("m_isFixed");
486
                this.m_pos = xml.getIntProperty("m_pos");
487
                this.m_rotation = xml.getDoubleProperty("m_rotation");
488
                this.m_f = new Font(xml.getStringProperty("fontName"),
489
                                xml.getIntProperty("fontStyle"), 9);
490
                if (xml.contains("textColor")){
491
                        this.textColor = StringUtilities.string2Color(xml.getStringProperty("textColor"));
492
                }
493
                
494
                // jaume
495
                if (xml.contains("cellPadding"))
496
                    this.cellPadding = xml.getIntProperty("cellPadding");
497
                if (xml.contains("fontSize"))
498
                    this.fontSize = xml.getIntProperty("fontSize");
499
                if (xml.contains("fixedFontSize"))
500
                    this.fixedFontSize = xml.getBooleanProperty("fixedFontSize");
501
                if (xml.contains("surrounded"))
502
                    this.surrounded = xml.getBooleanProperty("surrounded");
503
                if (xml.contains("hasTitle"))
504
                    this.hasTitle = xml.getBooleanProperty("hasTitle");
505
                if (xml.contains("title"))
506
                    this.title = xml.getStringProperty("title");
507
                if (xml.contains("titleSize"))
508
                    this.titleSize = xml.getIntProperty("titleSize");
509
                if (xml.contains("frameBorderSize"))
510
                    this.frameBorderSize = xml.getIntProperty("frameBorderSize");
511
                if (xml.contains("frameColor"))
512
                    this.frameColor = StringUtilities.string2Color(xml.getStringProperty("frameColor"));
513
                if (xml.contains("titleColor"))
514
                    this.titleColor = StringUtilities.string2Color(xml.getStringProperty("titleColor"));
515
                
516
        }
517

    
518
        /**
519
         * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#getNameFFrame()
520
         */
521
        public String getNameFFrame() {
522
                return PluginServices.getText(this, "texto") + num;
523
        }
524

    
525
    /**
526
     * Sets FFrameText to draw an involving rectangle
527
     * @param b
528
     */
529
    public void setSurrounded(boolean b) {
530
        surrounded = b;        
531
    }
532
    
533
    /**
534
     * True if the FFrameText is set to draw an involving rectangle, or false
535
     * if not.
536
     * @return boolean
537
     */
538
    public boolean isSurrounded(){
539
        return surrounded;
540
    }
541
    /**
542
     * Sets the gap between the involving rectangle and the text
543
     * @param i
544
     */
545
    public void setCellPadding(int i) {
546
        cellPadding = i;
547
    }
548
    
549

    
550
    /**
551
     * @return
552
     */
553
    public boolean hasTitle() {
554
        return hasTitle;
555
    }
556

    
557
    /**
558
     * @return
559
     */
560
    public String getTitle() {
561
        return title;
562
    }
563

    
564
    /**
565
     * @param b
566
     */
567
    public void setHasTitle(boolean b) {
568
       hasTitle = b;      
569
    }
570

    
571
    /**
572
     * @param text
573
     */
574
    public void setTitle(String text) {
575
        title = text;
576
    }
577

    
578
    /**
579
     * @return
580
     */
581
    public int getTitleSize() {
582
        return titleSize;
583
    }
584
    
585
    public void setTitleSize(int size) {
586
        titleSize = size;
587
    }
588

    
589
    /**
590
     * @param i
591
     */
592
    public void setFrameBorderSize(int size) {
593
        frameBorderSize = size;
594
    }
595

    
596
    /**
597
     * @return
598
     */
599
    public int getFrameBorderSize() {
600
        return frameBorderSize;
601
    }
602

    
603
    /**
604
     * @param frameColor
605
     */
606
    public void setFrameColor(Color frameColor) {
607
        this.frameColor = frameColor;
608
    }
609
    
610
    public void setTitleColor(Color titleColor) {
611
        this.titleColor = titleColor;
612
    }
613
    
614
    public Color getFrameColor(){
615
        return frameColor;
616
    }
617
    
618
    public Color getTitleColor(){
619
        return titleColor;
620
    }
621
    
622
    /**
623
     * Use this method if you want the text in the FFrameText to be removed.
624
     *
625
     */
626
    public void clearText(){
627
            m_text.clear();
628
    }
629
}