Statistics
| Revision:

root / branches / gvSIG_GisPlanet / applications / appgvSIG / src / com / iver / cit / gvsig / gui / layout / fframes / FFrameText.java @ 2188

History | View | Annotate | Download (17.1 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.Rectangle;
51
import java.awt.font.FontRenderContext;
52
import java.awt.font.TextLayout;
53
import java.awt.geom.AffineTransform;
54
import java.awt.geom.Rectangle2D;
55
import java.awt.image.BufferedImage;
56
import java.util.ArrayList;
57

    
58
import com.iver.andami.PluginServices;
59
import com.iver.cit.gvsig.gui.layout.FLayoutUtilities;
60
import com.iver.cit.gvsig.gui.layout.Layout;
61
import com.iver.cit.gvsig.gui.layout.Size;
62
import com.iver.utiles.StringUtilities;
63
import com.iver.utiles.XMLEntity;
64

    
65

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

    
108
        /**
109
         * Inserta la fuente del texto.
110
         *
111
         * @param f Fuente del texto.
112
         */
113
        public void setFont(Font f) {
114
                m_f = f;
115
        }
116

    
117
        /**
118
         * Devuelve el color del texto del FFrameText.
119
         *
120
         * @return Color del texto.
121
         */
122
        public Color getTextColor() {
123
                return textColor;
124
        }
125

    
126
    /**
127
     * Obtiene el fixedFontSize
128
     * @return boolean
129
     */
130

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

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

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

    
173
        /**
174
         * Devuelve la fuente del texto.
175
         *
176
         * @return Fuente del texto.
177
         */
178
        public Font getFont() {
179
                return new Font(m_f.getFontName(), m_f.getStyle(), 9);
180
        }
181

    
182
        /*public void setSpace(int s){
183
           m_space=s;
184
           }
185
           public int getSpace(){
186
                   return m_space;
187
           }*/
188

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

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

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

    
249
                                        if (scaledFontSize > (int) ((raux.height * 10) / 4 / (m_text.size() * 1.3))) {
250
                                                scaledFontSize = (int) (((raux.height * 10) / 4) / (m_text.size() * 1.3));
251
                                        } 
252
                            }
253
                                
254
                                
255
                                
256
                                int ht = (int) (raux.height / m_text.size());
257

    
258
                                if (m_f != null) {
259
                                    // esto sirve de algo?
260
                                        m_f = new Font(m_f.getFontName(), m_f.getStyle(), scaledFontSize);
261
                                } else {
262
                                        m_f = new Font("SansSerif", Font.PLAIN, scaledFontSize);
263
                                }
264

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

    
319
                                                        case (CENTER):
320
                                                            float pos1 = (float) ((raux.width -
321
                                                                    ((l * scaledFontSize) / 2.2)) / 2);
322
                                                            textLayout.draw(g, (float) raux.getX() + pos1,
323
                                                                    (float) (raux.getY() + (ht * (i + 1))));
324
                                                            
325
                                                                break;
326

    
327
                                                        case (RIGTH):
328
                                                            float pos2 = (float) ((raux.width -
329
                                                                    ((l * scaledFontSize) / 2.2)));
330
                                                                textLayout.draw(g, (float) raux.getX() + pos2,
331
                                                                    (float) (raux.getY() + (ht * (i + 1)))); //- (ht / 2))));
332
                                                            break;
333
                                                }
334

    
335
                                                g.rotate(Math.toRadians(-m_rotation),
336
                                                        raux.x + (raux.width / 2), raux.y + (raux.height / 2));
337
                                                
338
                                        }
339
                                        
340
                                }
341
                        }
342
                }
343
                raux = null;
344
        }
345

    
346
        /**
347
         * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#print(java.awt.Graphics2D,
348
         *                 java.awt.geom.AffineTransform)
349
         */
350
        public void print(Graphics2D g, AffineTransform at) {
351
                draw(g, at, null, null);
352
        }
353

    
354
        /**
355
         * Rellenar el texto que se quiere a?adir al Layout.
356
         *
357
         * @param s String a a?adir.
358
         */
359
        public void addText(String s) {
360
                m_text.add(s);
361
        }
362

    
363
        /**
364
         * Devuelve el ArrayList que contiene las l?neas de texto.
365
         *
366
         * @return ArrayList.
367
         */
368
        public ArrayList getText() {
369
                return m_text;
370
        }
371

    
372
        /**
373
         * Seleccionar si se quiere un tama?o fijo o adecuado a la escala.
374
         *
375
         * @param b true si se quiere tama?o fijo.
376
         */
377
        public void setSizeFixed(boolean b) {
378
                m_isFixed = b;
379
        }
380

    
381
        /**
382
         * Devuelve si est? fijado el tama?o.
383
         *
384
         * @return True si est? fijado el tama?o.
385
         */
386
        public boolean isSizeFixed() {
387
                return m_isFixed;
388
        }
389

    
390
        /**
391
         * Rellenar la rotaci?n para aplicar al texto.
392
         *
393
         * @param rotation rotaci?n que se quiere aplicar.
394
         */
395
        public void setRotation(double rotation) {
396
                m_rotation = rotation;
397
        }
398

    
399
        /**
400
         * Devuelve la rotaci?n del texto.
401
         *
402
         * @return Rotaci?n del texto.
403
         */
404
        public double getRotation() {
405
                return m_rotation;
406
        }
407

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

    
422
                String[] s = (String[]) m_text.toArray(new String[0]);
423
                xml.putProperty("s", s);
424
                xml.putProperty("m_isFixed", m_isFixed);
425

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

    
458
                String[] s = xml.getStringArrayProperty("s");
459

    
460
                for (int i = 0; i < s.length; i++) {
461
                        this.m_text.add(s[i]);
462
                }
463

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

    
485
                String[] s = xml.getStringArrayProperty("s");
486

    
487
                for (int i = 0; i < s.length; i++) {
488
                        this.m_text.add(s[i]);
489
                }
490

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

    
524
        /**
525
         * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#getNameFFrame()
526
         */
527
        public String getNameFFrame() {
528
                return PluginServices.getText(this, "texto") + num;
529
        }
530

    
531
    /**
532
     * Sets FFrameText to draw an involving rectangle
533
     * @param b
534
     */
535
    public void setSurrounded(boolean b) {
536
        surrounded = b;        
537
    }
538
    
539
    /**
540
     * True if the FFrameText is set to draw an involving rectangle, or false
541
     * if not.
542
     * @return boolean
543
     */
544
    public boolean isSurrounded(){
545
        return surrounded;
546
    }
547
    /**
548
     * Sets the gap between the involving rectangle and the text
549
     * @param i
550
     */
551
    public void setCellPadding(int i) {
552
        cellPadding = i;
553
    }
554
    
555
    public void setLayout(Layout l){
556
        layout = l;
557
    }
558

    
559
    /**
560
     * @return
561
     */
562
    public boolean hasTitle() {
563
        return hasTitle;
564
    }
565

    
566
    /**
567
     * @return
568
     */
569
    public String getTitle() {
570
        return title;
571
    }
572

    
573
    /**
574
     * @param b
575
     */
576
    public void setHasTitle(boolean b) {
577
       hasTitle = b;      
578
    }
579

    
580
    /**
581
     * @param text
582
     */
583
    public void setTitle(String text) {
584
        title = text;
585
    }
586

    
587
    /**
588
     * @return
589
     */
590
    public int getTitleSize() {
591
        return titleSize;
592
    }
593
    
594
    public void setTitleSize(int size) {
595
        titleSize = size;
596
    }
597

    
598
    /**
599
     * @param i
600
     */
601
    public void setFrameBorderSize(int size) {
602
        frameBorderSize = size;
603
    }
604

    
605
    /**
606
     * @return
607
     */
608
    public int getFrameBorderSize() {
609
        return frameBorderSize;
610
    }
611

    
612
    /**
613
     * @param frameColor
614
     */
615
    public void setFrameColor(Color frameColor) {
616
        this.frameColor = frameColor;
617
    }
618
    
619
    public void setTitleColor(Color titleColor) {
620
        this.titleColor = titleColor;
621
    }
622
    
623
    public Color getFrameColor(){
624
        return frameColor;
625
    }
626
    
627
    public Color getTitleColor(){
628
        return titleColor;
629
    }
630
}