Statistics
| Revision:

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

History | View | Annotate | Download (7.72 KB)

1
/*
2
 * Created on 22-jun-2004
3
 *
4
 * To change the template for this generated file go to
5
 * Window>Preferences>Java>Code Generation>Code and Comments
6
 */
7
package com.iver.cit.gvsig.gui.layout.fframes;
8

    
9
import java.awt.Color;
10
import java.awt.Font;
11
import java.awt.Graphics2D;
12
import java.awt.font.FontRenderContext;
13
import java.awt.font.TextLayout;
14
import java.awt.geom.AffineTransform;
15
import java.awt.geom.Rectangle2D;
16
import java.awt.image.BufferedImage;
17
import java.util.ArrayList;
18

    
19
import com.iver.andami.PluginServices;
20
import com.iver.cit.gvsig.gui.layout.Layout;
21
import com.iver.utiles.XMLEntity;
22

    
23

    
24
/**
25
 * FFrame para introducir un texto en el Layout.
26
 *
27
 * @author Vicente Caballero Navarro
28
 */
29
public class FFrameText extends FFrame {
30
        /** DOCUMENT ME! */
31
        public static final int LEFT = 0;
32

    
33
        /** DOCUMENT ME! */
34
        public static final int CENTER = 1;
35

    
36
        /** DOCUMENT ME! */
37
        public static final int RIGTH = 2;
38
        private ArrayList m_text = new ArrayList();
39
        private boolean m_isFixed = true;
40
        private double m_rotation = 0;
41
        private int m_pos = LEFT;
42

    
43
        //private int m_space=0;
44
        private Font m_f = null;
45

    
46
        /**
47
         * Crea un nuevo FFrameText.
48
         */
49
        public FFrameText() {
50
                m_f = new Font("SansSerif", Font.PLAIN, 9);
51
        }
52

    
53
        /**
54
         * DOCUMENT ME!
55
         *
56
         * @param f DOCUMENT ME!
57
         */
58
        public void setFont(Font f) {
59
                m_f = f;
60
        }
61

    
62
        /**
63
         * DOCUMENT ME!
64
         *
65
         * @return DOCUMENT ME!
66
         */
67
        public Font getFont() {
68
                return new Font(m_f.getFontName(), m_f.getStyle(), 9);
69
        }
70

    
71
        /*public void setSpace(int s){
72
           m_space=s;
73
           }
74
           public int getSpace(){
75
                   return m_space;
76
           }*/
77

    
78
        /**
79
         * Devuelve la posici?n izquierda, centro o derecha del texto.
80
         *
81
         * @return DOCUMENT ME!
82
         */
83
        public int getPos() {
84
                return m_pos;
85
        }
86

    
87
        /**
88
         * Pone la posici?n izquierda, centro o derecha del texto.
89
         *
90
         * @param p 0=LEFT,1=CENTER,2=RIGTH.
91
         */
92
        public void setPos(int p) {
93
                m_pos = p;
94
        }
95

    
96
        /**
97
         * M?todo que dibuja sobre el graphics que se le pasa como par?metro, seg?n
98
         * la transformada afin que se debe de aplicar y el rect?ngulo que se debe
99
         * de dibujar.
100
         *
101
         * @param g Graphics
102
         * @param at Transformada af?n.
103
         * @param rv rect?ngulo sobre el que hacer un clip.
104
         * @param imgBase DOCUMENT ME!
105
         */
106
        public void draw(Graphics2D g, AffineTransform at, Rectangle2D rv,
107
                BufferedImage imgBase) {
108
                Rectangle2D.Double re = getBoundingBox(at);
109
                int longmax = 1;
110

    
111
                if (intersects(rv, re)) {
112
                        if (m_text.isEmpty()) {
113
                                drawEmpty(g);
114
                        } else {
115
                                for (int i = 0; i < m_text.size(); i++) {
116
                                        if (((String) m_text.get(i)).length() > longmax) {
117
                                                longmax = ((String) m_text.get(i)).length();
118
                                        }
119
                                }
120

    
121
                                FontRenderContext frc = g.getFontRenderContext();
122
                                int scale = ((int) (re.width * 2.2)) / longmax;
123

    
124
                                if (scale > (int) ((re.height * 10) / 4 / (m_text.size() * 1.3))) {
125
                                        scale = (int) (((re.height * 10) / 4) / (m_text.size() * 1.3));
126
                                }
127

    
128
                                int ht = (int) (re.height / m_text.size());
129

    
130
                                if (m_f != null) {
131
                                        m_f = new Font(m_f.getFontName(), m_f.getStyle(), scale);
132
                                } else {
133
                                        m_f = new Font("SansSerif", Font.PLAIN, scale);
134
                                }
135

    
136
                                for (int i = 0; i < m_text.size(); i++) {
137
                                        if (!((String) m_text.get(i)).equals("")) {
138
                                                TextLayout layout = new TextLayout((String) m_text.get(
139
                                                                        i), m_f, frc);
140
                                                g.setColor(Color.black);
141
                                                g.rotate(Math.toRadians(m_rotation),
142
                                                        re.x + (re.width / 2), re.y + (re.height / 2));
143

    
144
                                                int l = ((String) m_text.get(i)).length();
145

    
146
                                                switch (m_pos) {
147
                                                        case (LEFT):
148
                                                                layout.draw(g, (float) re.getX(),
149
                                                                        (float) (re.getY() + (ht * (i + 1)))); //- (ht / 2))));
150

    
151
                                                                break;
152

    
153
                                                        case (CENTER):
154

    
155
                                                                float pos1 = (float) ((re.width -
156
                                                                        ((l * scale) / 2.2)) / 2);
157
                                                                layout.draw(g, (float) re.getX() + pos1,
158
                                                                        (float) (re.getY() + (ht * (i + 1)))); //- (ht / 2))));
159

    
160
                                                                break;
161

    
162
                                                        case (RIGTH):
163

    
164
                                                                float pos2 = (float) ((re.width -
165
                                                                        ((l * scale) / 2.2)));
166
                                                                layout.draw(g, (float) re.getX() + pos2,
167
                                                                        (float) (re.getY() + (ht * (i + 1)))); //- (ht / 2))));
168

    
169
                                                                break;
170
                                                }
171

    
172
                                                g.rotate(Math.toRadians(-m_rotation),
173
                                                        re.x + (re.width / 2), re.y + (re.height / 2));
174
                                        }
175
                                }
176
                        }
177
                }
178
        }
179

    
180
        /**
181
         * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#print(java.awt.Graphics2D,
182
         *                 java.awt.geom.AffineTransform)
183
         */
184
        public void print(Graphics2D g, AffineTransform at) {
185
                draw(g, at, null, null);
186
        }
187

    
188
        /**
189
         * Rellenar el texto que se quiere a?adir al Layout.
190
         *
191
         * @param s String a a?adir.
192
         */
193
        public void addText(String s) {
194
                m_text.add(s);
195
        }
196

    
197
        /**
198
         * Devuelve el ArrayList que contiene las l?neas de texto.
199
         *
200
         * @return ArrayList.
201
         */
202
        public ArrayList getText() {
203
                return m_text;
204
        }
205

    
206
        /**
207
         * Seleccionar si se quiere un tama?o fijo o adecuado a la escala.
208
         *
209
         * @param b true si se quiere tama?o fijo.
210
         */
211
        public void setSizeFixed(boolean b) {
212
                m_isFixed = b;
213
        }
214

    
215
        /**
216
         * DOCUMENT ME!
217
         *
218
         * @return DOCUMENT ME!
219
         */
220
        public boolean isSizeFixed() {
221
                return m_isFixed;
222
        }
223

    
224
        /**
225
         * Rellenar la rotaci?n para aplicar al texto.
226
         *
227
         * @param rotation rotaci?n que se quiere aplicar.
228
         */
229
        public void setRotation(double rotation) {
230
                m_rotation = rotation;
231
        }
232

    
233
        /**
234
         * DOCUMENT ME!
235
         *
236
         * @return DOCUMENT ME!
237
         */
238
        public double getRotation() {
239
                return m_rotation;
240
        }
241

    
242
        /**
243
         * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#getXMLEntity()
244
         */
245
        public XMLEntity getXMLEntity() {
246
                XMLEntity xml = new XMLEntity();
247
                xml.putProperty("nameClass", this.getClass().getName());
248
                xml.putProperty("m_name", m_name);
249
                xml.putProperty("x", getBoundBox().x);
250
                xml.putProperty("y", getBoundBox().y);
251
                xml.putProperty("w", getBoundBox().width);
252
                xml.putProperty("h", getBoundBox().height);
253
                xml.putProperty("m_Selected", m_Selected);
254
                xml.putProperty("type", Layout.RECTANGLETEXT);
255

    
256
                String[] s = (String[]) m_text.toArray(new String[0]);
257
                xml.putProperty("s", s);
258
                xml.putProperty("m_isFixed", m_isFixed);
259

    
260
                xml.putProperty("m_pos", m_pos);
261
                xml.putProperty("m_rotation", m_rotation);
262
                xml.putProperty("fontName", m_f.getFontName());
263
                xml.putProperty("fontStyle", m_f.getStyle());
264
                xml.putProperty("tag", getTag());
265

    
266
                return xml;
267
        }
268

    
269
        /**
270
         * Crea un Objeto de esta clase a partir de la informaci?n del XMLEntity.
271
         *
272
         * @param xml XMLEntity
273
         * @param p DOCUMENT ME!
274
         */
275

    
276
        /*  public static FFrameText createFFrameText(XMLEntity xml) {
277
           FFrameText fframe = new FFrameText();
278
        
279
           if (xml.getIntProperty("m_Selected") != 0) {
280
               fframe.setSelected(true);
281
           } else {
282
               fframe.setSelected(false);
283
           }
284
           String[] s = xml.getStringArrayProperty("s");
285
           for (int i = 0; i < s.length; i++) {
286
               fframe.m_text.add(s[i]);
287
           }
288
           fframe.m_isFixed = xml.getBooleanProperty("m_isFixed");
289
           fframe.m_pos = xml.getIntProperty("m_pos");
290
           fframe.m_rotation = xml.getDoubleProperty("m_rotation");
291
           fframe.m_f = new Font(xml.getStringProperty("fontName"),
292
                   xml.getIntProperty("fontStyle"), 9);
293
           return fframe;
294
           }
295
         */
296

    
297
        /**
298
         * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#setXMLEntity(com.iver.utiles.XMLEntity,
299
         *                 com.iver.cit.gvsig.project.Project)
300
         */
301
        public void setXMLEntity(XMLEntity xml, Layout l) {
302
                if (xml.getIntProperty("m_Selected") != 0) {
303
                        this.setSelected(true);
304
                } else {
305
                        this.setSelected(false);
306
                }
307

    
308
                String[] s = xml.getStringArrayProperty("s");
309

    
310
                for (int i = 0; i < s.length; i++) {
311
                        this.m_text.add(s[i]);
312
                }
313

    
314
                this.m_isFixed = xml.getBooleanProperty("m_isFixed");
315
                this.m_pos = xml.getIntProperty("m_pos");
316
                this.m_rotation = xml.getDoubleProperty("m_rotation");
317
                this.m_f = new Font(xml.getStringProperty("fontName"),
318
                                xml.getIntProperty("fontStyle"), 9);
319
        }
320

    
321
        /**
322
         * @see com.iver.cit.gvsig.gui.layout.fframes.IFFrame#getNameFFrame()
323
         */
324
        public String getNameFFrame() {
325
                return PluginServices.getText(this, "texto") + num;
326
        }
327
}