Statistics
| Revision:

root / branches / v2_0_0_prep / libFMap_mapcontext / src / org / gvsig / fmap / mapcontext / rendering / symbols / SimpleTextSymbol.java @ 21200

History | View | Annotate | Download (7.52 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package org.gvsig.fmap.mapcontext.rendering.symbols;
42

    
43
import java.awt.Color;
44
import java.awt.Font;
45
import java.awt.Graphics2D;
46
import java.awt.Rectangle;
47
import java.awt.RenderingHints;
48
import java.awt.Shape;
49
import java.awt.font.FontRenderContext;
50
import java.awt.font.GlyphVector;
51
import java.awt.geom.AffineTransform;
52
import java.awt.geom.Point2D;
53
import java.awt.geom.Rectangle2D;
54

    
55
import javax.print.attribute.PrintRequestAttributeSet;
56

    
57
import org.gvsig.fmap.geom.Geometry;
58
import org.gvsig.fmap.geom.primitive.GeneralPathX;
59
import org.gvsig.fmap.geom.primitive.Surface2D;
60
import org.gvsig.fmap.mapcontext.ViewPort;
61

    
62
import com.iver.utiles.StringUtilities;
63
import com.iver.utiles.XMLEntity;
64
import com.iver.utiles.swing.threads.Cancellable;
65

    
66
/**
67
 * SimpleTextSymbol is a class used to create symbols composed using a text defined by
68
 * the user.This text can be edited (changing the color, the font of the characters, and
69
 * the rotation of the text).
70
 * @author jaume dominguez faus - jaume.dominguez@iver.es
71
 *
72
 */
73
public class SimpleTextSymbol extends AbstractSymbol implements ITextSymbol {
74
        private String text = "";
75
        private Font font = new Font("Arial", Font.PLAIN, 12);
76
        private Color textColor = Color.BLACK;
77
        private double rotation;
78
        private FontRenderContext frc = new FontRenderContext(
79
                        new AffineTransform(), false, true);
80

    
81
        public void draw(Graphics2D g, AffineTransform affineTransform, Geometry geom, Cancellable cancel) {
82
                if (!isShapeVisible()) return;
83
                g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
84
                g.setColor(textColor);
85
                g.setFont(font);
86
                g.translate(((org.gvsig.fmap.geom.primitive.Point2D) geom).getX(), ((org.gvsig.fmap.geom.primitive.Point2D) geom).getY());
87

    
88
                g.rotate(rotation);
89
                g.drawString(getText(), 0, getBounds().height);
90
                Rectangle2D bounds = getBounds();
91
                bounds.setFrame(0, 0, bounds.getWidth(), bounds.getHeight());
92
                g.rotate(-rotation);
93
                g.translate(-((org.gvsig.fmap.geom.primitive.Point2D) geom).getX(), -((org.gvsig.fmap.geom.primitive.Point2D) geom).getY());
94
        }
95

    
96
        public void drawInsideRectangle(Graphics2D g,
97
                        AffineTransform scaleInstance, Rectangle r) throws SymbolDrawingException {
98
                int s = getFont().getSize();
99
                setFontSize(r.getHeight());
100
                draw(g, null, new org.gvsig.fmap.geom.primitive.Point2D(r.getX(), r.getY()), null);
101
                setFontSize(s);
102
        }
103

    
104
        public int getOnePointRgb() {
105
                return textColor.getRGB();
106
        }
107

    
108
        public void getPixExtentPlus(org.gvsig.fmap.geom.Geometry geom, float[] distances,
109
                        ViewPort viewPort, int dpi) {
110
                throw new Error("Not yet implemented!");
111

    
112
        }
113

    
114
        public ISymbol getSymbolForSelection() {
115
                return this; // a text is not selectable
116
        }
117

    
118
        public int getSymbolType() {
119
                return Geometry.TYPES.TEXT;
120
        }
121

    
122
        public XMLEntity getXMLEntity() {
123
                XMLEntity xml = new XMLEntity();
124
                xml.putProperty("className", getClassName());
125
                xml.putProperty("desc", getDescription());
126
                xml.putProperty("isShapeVisible", isShapeVisible());
127
                xml.putProperty("font", font.getName());
128
                xml.putProperty("fontStyle", font.getStyle());
129
                xml.putProperty("size", font.getSize());
130
                xml.putProperty("text", text);
131
                xml.putProperty("textColor", StringUtilities.color2String(textColor));
132
                xml.putProperty("unit", getUnit());
133
                xml.putProperty("referenceSystem", getReferenceSystem());
134
                return xml;
135
        }
136

    
137
        public boolean isSuitableFor(Geometry geom) {
138
                return true;
139
        }
140

    
141
        public void setXMLEntity(XMLEntity xml) {
142
                font = new Font(xml.getStringProperty("font"),
143
                                xml.getIntProperty("fontStyle"),
144
                                xml.getIntProperty("size"));
145
                setDescription(xml.getStringProperty("desc"));
146
                setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
147
                text = xml.getStringProperty("text");
148
                textColor = StringUtilities.string2Color(xml.getStringProperty("textColor"));
149
                setUnit(xml.getIntProperty("unit"));
150
                setReferenceSystem(xml.getIntProperty("referenceSystem"));
151
        }
152

    
153
        public String getClassName() {
154
                return getClass().getName();
155
        }
156

    
157
        public void print(Graphics2D g, AffineTransform at, org.gvsig.fmap.geom.Geometry geom, PrintRequestAttributeSet properties){
158
                // TODO Implement it
159
                throw new Error("Not yet implemented!");
160

    
161
        }
162

    
163
        public String getText() {
164
                return text;
165
        }
166

    
167
        public Font getFont() {
168
                return font;
169
        }
170

    
171
        public Color getTextColor() {
172
                return textColor;
173
        }
174

    
175
        public void setText(String text) {
176
                this.text = text;
177
        }
178

    
179
        public void setFont(Font font) {
180
                this.font = font;
181
        }
182

    
183
        public void setTextColor(Color color) {
184
                this.textColor = color;
185
        }
186

    
187
        public void setFontSize(double size) {
188
                this.font = new Font(font.getName(), font.getStyle(), (int) size);
189
        }
190

    
191
        /**
192
         * Defines the angle of rotation for the text that composes the symbol
193
         *
194
         * @param rotation
195
         */
196
        public void setRotation(double rotation) {
197
                this.rotation = rotation;
198
        }
199

    
200
        /**
201
         * Returns an FShape which represents a rectangle containing the text in
202
         * <b>screen</b> units.
203
         */
204
        public Geometry getTextWrappingShape(org.gvsig.fmap.geom.primitive.Point2D p) {
205
                Font font = getFont();
206
                GlyphVector gv = font.createGlyphVector(frc, text);
207

    
208
                Shape shape = gv.getOutline((float) p.getX(), (float) p.getY());
209
                Geometry myFShape = new Surface2D(new GeneralPathX(shape.getBounds2D()));
210

    
211
                myFShape.transform(AffineTransform.getTranslateInstance(p.getX(), p.getY()));
212

    
213
                if (rotation != 0) {
214
                        myFShape.transform(AffineTransform.getRotateInstance(rotation));
215
                }
216
                return myFShape;
217
        }
218

    
219
        public Rectangle getBounds() {
220
//                FontMetrics fm = g.getFontMetrics();
221
//                Rectangle2D rect = fm.getStringBounds("graphics", g);
222

    
223
                Rectangle bounds = getTextWrappingShape(new org.gvsig.fmap.geom.primitive.Point2D(0,0)).getBounds();
224
                return bounds;
225
        }
226

    
227
        public void setCartographicSize(double cartographicSize, Geometry geom) {
228
                setFontSize(cartographicSize);
229
        }
230

    
231
        public double toCartographicSize(ViewPort viewPort, double dpi, Geometry geom) {
232
                double oldSize = getFont().getSize();
233
                setCartographicSize(getCartographicSize(
234
                                                                viewPort,
235
                                                                dpi,
236
                                                                geom),
237
                                                        geom);
238
                return oldSize;
239
        }
240

    
241
        public double getCartographicSize(ViewPort viewPort, double dpi, Geometry geom) {
242
                return CartographicSupportToolkit.
243
                                        getCartographicLength(this,
244
                                                                                  getFont().getSize(),
245
                                                                                  viewPort,
246
                                                                                  dpi);
247
        }
248
}