Statistics
| Revision:

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

History | View | Annotate | Download (8.08 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 com.iver.andami.PluginServices;
48

    
49
import com.iver.cit.gvsig.gui.layout.Layout;
50

    
51
import com.iver.utiles.XMLEntity;
52

    
53
import java.awt.Color;
54
import java.awt.Font;
55
import java.awt.Graphics2D;
56
import java.awt.font.FontRenderContext;
57
import java.awt.font.TextLayout;
58
import java.awt.geom.AffineTransform;
59
import java.awt.geom.Rectangle2D;
60
import java.awt.image.BufferedImage;
61

    
62
import java.util.ArrayList;
63

    
64

    
65
/**
66
 * FFrame para introducir un texto en el Layout.
67
 *
68
 * @author Vicente Caballero Navarro
69
 */
70
public class FFrameText extends FFrame {
71
        /** Localizaci?n del texto. */
72
        public static final int LEFT = 0;
73
        public static final int CENTER = 1;
74
        public static final int RIGTH = 2;
75
        private ArrayList m_text = new ArrayList();
76
        private boolean m_isFixed = true;
77
        private double m_rotation = 0;
78
        private int m_pos = LEFT;
79

    
80
        //private int m_space=0;
81
        private Font m_f = null;
82

    
83
        /**
84
         * Crea un nuevo FFrameText.
85
         */
86
        public FFrameText() {
87
                m_f = new Font("SansSerif", Font.PLAIN, 9);
88
        }
89

    
90
        /**
91
         * Inserta la fuente del texto.
92
         *
93
         * @param f Fuente del texto.
94
         */
95
        public void setFont(Font f) {
96
                m_f = f;
97
        }
98

    
99
        /**
100
         * Devuelve la fuente del texto.
101
         *
102
         * @return Fuente del texto.
103
         */
104
        public Font getFont() {
105
                return new Font(m_f.getFontName(), m_f.getStyle(), 9);
106
        }
107

    
108
        /*public void setSpace(int s){
109
           m_space=s;
110
           }
111
           public int getSpace(){
112
                   return m_space;
113
           }*/
114

    
115
        /**
116
         * Devuelve la posici?n izquierda, centro o derecha del texto.
117
         *
118
         * @return Posici?n del texto.
119
         */
120
        public int getPos() {
121
                return m_pos;
122
        }
123

    
124
        /**
125
         * Pone la posici?n izquierda, centro o derecha del texto.
126
         *
127
         * @param p 0=LEFT,1=CENTER,2=RIGTH.
128
         */
129
        public void setPos(int p) {
130
                m_pos = p;
131
        }
132

    
133
        /**
134
         * M?todo que dibuja sobre el graphics que se le pasa como par?metro, seg?n
135
         * la transformada afin que se debe de aplicar y el rect?ngulo que se debe
136
         * de dibujar.
137
         *
138
         * @param g Graphics
139
         * @param at Transformada af?n.
140
         * @param rv rect?ngulo sobre el que hacer un clip.
141
         * @param imgBase Imagen para acelerar el dibujado.
142
         */
143
        public void draw(Graphics2D g, AffineTransform at, Rectangle2D rv,
144
                BufferedImage imgBase) {
145
                Rectangle2D.Double re = getBoundingBox(at);
146
                int longmax = 1;
147

    
148
                if (intersects(rv, re)) {
149
                        if (m_text.isEmpty()) {
150
                                drawEmpty(g);
151
                        } else {
152
                                for (int i = 0; i < m_text.size(); i++) {
153
                                        if (((String) m_text.get(i)).length() > longmax) {
154
                                                longmax = ((String) m_text.get(i)).length();
155
                                        }
156
                                }
157

    
158
                                FontRenderContext frc = g.getFontRenderContext();
159
                                int scale = ((int) (re.width * 2.2)) / longmax;
160

    
161
                                if (scale > (int) ((re.height * 10) / 4 / (m_text.size() * 1.3))) {
162
                                        scale = (int) (((re.height * 10) / 4) / (m_text.size() * 1.3));
163
                                }
164

    
165
                                int ht = (int) (re.height / m_text.size());
166

    
167
                                if (m_f != null) {
168
                                        m_f = new Font(m_f.getFontName(), m_f.getStyle(), scale);
169
                                } else {
170
                                        m_f = new Font("SansSerif", Font.PLAIN, scale);
171
                                }
172

    
173
                                for (int i = 0; i < m_text.size(); i++) {
174
                                        if (!((String) m_text.get(i)).equals("")) {
175
                                                TextLayout layout = new TextLayout((String) m_text.get(
176
                                                                        i), m_f, frc);
177
                                                g.setColor(Color.black);
178
                                                g.rotate(Math.toRadians(m_rotation),
179
                                                        re.x + (re.width / 2), re.y + (re.height / 2));
180

    
181
                                                int l = ((String) m_text.get(i)).length();
182

    
183
                                                switch (m_pos) {
184
                                                        case (LEFT):
185
                                                                layout.draw(g, (float) re.getX(),
186
                                                                        (float) (re.getY() + (ht * (i + 1)))); //- (ht / 2))));
187

    
188
                                                                break;
189

    
190
                                                        case (CENTER):
191

    
192
                                                                float pos1 = (float) ((re.width -
193
                                                                        ((l * scale) / 2.2)) / 2);
194
                                                                layout.draw(g, (float) re.getX() + pos1,
195
                                                                        (float) (re.getY() + (ht * (i + 1)))); //- (ht / 2))));
196

    
197
                                                                break;
198

    
199
                                                        case (RIGTH):
200

    
201
                                                                float pos2 = (float) ((re.width -
202
                                                                        ((l * scale) / 2.2)));
203
                                                                layout.draw(g, (float) re.getX() + pos2,
204
                                                                        (float) (re.getY() + (ht * (i + 1)))); //- (ht / 2))));
205

    
206
                                                                break;
207
                                                }
208

    
209
                                                g.rotate(Math.toRadians(-m_rotation),
210
                                                        re.x + (re.width / 2), re.y + (re.height / 2));
211
                                        }
212
                                }
213
                        }
214
                }
215
        }
216

    
217
        /**
218
         * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#print(java.awt.Graphics2D,
219
         *                 java.awt.geom.AffineTransform)
220
         */
221
        public void print(Graphics2D g, AffineTransform at) {
222
                draw(g, at, null, null);
223
        }
224

    
225
        /**
226
         * Rellenar el texto que se quiere a?adir al Layout.
227
         *
228
         * @param s String a a?adir.
229
         */
230
        public void addText(String s) {
231
                m_text.add(s);
232
        }
233

    
234
        /**
235
         * Devuelve el ArrayList que contiene las l?neas de texto.
236
         *
237
         * @return ArrayList.
238
         */
239
        public ArrayList getText() {
240
                return m_text;
241
        }
242

    
243
        /**
244
         * Seleccionar si se quiere un tama?o fijo o adecuado a la escala.
245
         *
246
         * @param b true si se quiere tama?o fijo.
247
         */
248
        public void setSizeFixed(boolean b) {
249
                m_isFixed = b;
250
        }
251

    
252
        /**
253
         * Devuelve si est? fijado el tama?o.
254
         *
255
         * @return True si est? fijado el tama?o.
256
         */
257
        public boolean isSizeFixed() {
258
                return m_isFixed;
259
        }
260

    
261
        /**
262
         * Rellenar la rotaci?n para aplicar al texto.
263
         *
264
         * @param rotation rotaci?n que se quiere aplicar.
265
         */
266
        public void setRotation(double rotation) {
267
                m_rotation = rotation;
268
        }
269

    
270
        /**
271
         * Devuelve la rotaci?n del texto.
272
         *
273
         * @return Rotaci?n del texto.
274
         */
275
        public double getRotation() {
276
                return m_rotation;
277
        }
278

    
279
        /**
280
         * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#getXMLEntity()
281
         */
282
        public XMLEntity getXMLEntity() {
283
                XMLEntity xml = new XMLEntity();
284
                xml.putProperty("className",this.getClass().getName());
285
                xml.putProperty("m_name", m_name);
286
                xml.putProperty("x", getBoundBox().x);
287
                xml.putProperty("y", getBoundBox().y);
288
                xml.putProperty("w", getBoundBox().width);
289
                xml.putProperty("h", getBoundBox().height);
290
                xml.putProperty("m_Selected", m_Selected);
291
                xml.putProperty("type", Layout.RECTANGLETEXT);
292

    
293
                String[] s = (String[]) m_text.toArray(new String[0]);
294
                xml.putProperty("s", s);
295
                xml.putProperty("m_isFixed", m_isFixed);
296

    
297
                xml.putProperty("m_pos", m_pos);
298
                xml.putProperty("m_rotation", m_rotation);
299
                xml.putProperty("fontName", m_f.getFontName());
300
                xml.putProperty("fontStyle", m_f.getStyle());
301
                xml.putProperty("tag", getTag());
302

    
303
                return xml;
304
        }
305

    
306
        /**
307
         * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#setXMLEntity(com.iver.utiles.XMLEntity,
308
         *                 com.iver.cit.gvsig.project.Project)
309
         */
310
        public void setXMLEntity(XMLEntity xml, Layout l) {
311
                if (xml.getIntProperty("m_Selected") != 0) {
312
                        this.setSelected(true);
313
                } else {
314
                        this.setSelected(false);
315
                }
316

    
317
                String[] s = xml.getStringArrayProperty("s");
318

    
319
                for (int i = 0; i < s.length; i++) {
320
                        this.m_text.add(s[i]);
321
                }
322

    
323
                this.m_isFixed = xml.getBooleanProperty("m_isFixed");
324
                this.m_pos = xml.getIntProperty("m_pos");
325
                this.m_rotation = xml.getDoubleProperty("m_rotation");
326
                this.m_f = new Font(xml.getStringProperty("fontName"),
327
                                xml.getIntProperty("fontStyle"), 9);
328
        }
329

    
330
        /**
331
         * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#getNameFFrame()
332
         */
333
        public String getNameFFrame() {
334
                return PluginServices.getText(this, "texto") + num;
335
        }
336
}