Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / symbols / SimpleTextSymbol.java @ 20700

History | View | Annotate | Download (7.45 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 com.iver.cit.gvsig.fmap.core.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.apache.batik.ext.awt.geom.PathLength;
58

    
59
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
60
import com.iver.cit.gvsig.fmap.ViewPort;
61
import com.iver.cit.gvsig.fmap.core.CartographicSupportToolkit;
62
import com.iver.cit.gvsig.fmap.core.FPoint2D;
63
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
64
import com.iver.cit.gvsig.fmap.core.FShape;
65
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
66
import com.iver.cit.gvsig.fmap.core.IGeometry;
67
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
68
import com.iver.utiles.StringUtilities;
69
import com.iver.utiles.XMLEntity;
70
import com.iver.utiles.swing.threads.Cancellable;
71
import com.vividsolutions.jts.geom.Geometry;
72
import com.vividsolutions.jts.geom.Point;
73

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

    
89
        public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp, Cancellable cancel) {
90
                if (!isShapeVisible()) return;
91
                g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
92
                g.setColor(textColor);
93
                g.setFont(font);
94
                g.translate(((FPoint2D) shp).getX(), ((FPoint2D) shp).getY());
95

    
96
                g.rotate(rotation);
97
                g.drawString(getText(), 0, getBounds().height);
98
                Rectangle2D bounds = getBounds();
99
                bounds.setFrame(0, 0, bounds.getWidth(), bounds.getHeight());
100
                g.rotate(-rotation);
101
                g.translate(-((FPoint2D) shp).getX(), -((FPoint2D) shp).getY());
102
        }
103

    
104
        public void drawInsideRectangle(Graphics2D g,
105
                        AffineTransform scaleInstance, Rectangle r) throws SymbolDrawingException {
106
                int s = getFont().getSize();
107
                setFontSize(r.getHeight());
108
                draw(g, null, new FPoint2D(r.getX(), r.getY()), null);
109
                setFontSize(s);
110
        }
111

    
112
        public int getOnePointRgb() {
113
                return textColor.getRGB();
114
        }
115

    
116
        public void getPixExtentPlus(FShape shp, float[] distances,
117
                        ViewPort viewPort, int dpi) {
118
                throw new Error("Not yet implemented!");
119

    
120
        }
121

    
122
        public ISymbol getSymbolForSelection() {
123
                return this; // a text is not selectable
124
        }
125

    
126
        public int getSymbolType() {
127
                return FShape.TEXT;
128
        }
129

    
130
        public XMLEntity getXMLEntity() {
131
                XMLEntity xml = new XMLEntity();
132
                xml.putProperty("className", getClassName());
133
                xml.putProperty("desc", getDescription());
134
                xml.putProperty("isShapeVisible", isShapeVisible());
135
                xml.putProperty("font", font.getName());
136
                xml.putProperty("fontStyle", font.getStyle());
137
                xml.putProperty("size", font.getSize());
138
                xml.putProperty("text", text);
139
                xml.putProperty("textColor", StringUtilities.color2String(textColor));
140
                xml.putProperty("unit", getUnit());
141
                xml.putProperty("referenceSystem", getReferenceSystem());
142
                return xml;
143
        }
144

    
145
        public boolean isSuitableFor(IGeometry geom) {
146
                return true;
147
        }
148

    
149
        public void setXMLEntity(XMLEntity xml) {
150
                font = new Font(xml.getStringProperty("font"),
151
                                xml.getIntProperty("fontStyle"),
152
                                xml.getIntProperty("size"));
153
                setDescription(xml.getStringProperty("desc"));
154
                setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
155
                text = xml.getStringProperty("text");
156
                textColor = StringUtilities.string2Color(xml.getStringProperty("textColor"));
157
                setUnit(xml.getIntProperty("unit"));
158
                setReferenceSystem(xml.getIntProperty("referenceSystem"));
159
        }
160

    
161
        public String getClassName() {
162
                return getClass().getName();
163
        }
164

    
165
        public void print(Graphics2D g, AffineTransform at, FShape shape, PrintRequestAttributeSet properties)
166
                        throws ReadDriverException {
167
                // TODO Implement it
168
                throw new Error("Not yet implemented!");
169

    
170
        }
171

    
172
        public String getText() {
173
                return text;
174
        }
175

    
176
        public Font getFont() {
177
                return font;
178
        }
179

    
180
        public Color getTextColor() {
181
                return textColor;
182
        }
183

    
184
        public void setText(String text) {
185
                this.text = text;
186
        }
187

    
188
        public void setFont(Font font) {
189
                this.font = font;
190
        }
191

    
192
        public void setTextColor(Color color) {
193
                this.textColor = color;
194
        }
195

    
196
        public void setFontSize(double size) {
197
                this.font = new Font(font.getName(), font.getStyle(), (int) size);
198
        }
199

    
200
        /**
201
         * Defines the angle of rotation for the text that composes the symbol
202
         *
203
         * @param rotation
204
         */
205
        public void setRotation(double rotation) {
206
                this.rotation = rotation;
207
        }
208

    
209
        /**
210
         * Returns an FShape which represents a rectangle containing the text in
211
         * <b>screen</b> units.
212
         */
213
        public FShape getTextWrappingShape(FPoint2D p) {
214
                Font font = getFont();
215
                GlyphVector gv = font.createGlyphVector(frc, text);
216

    
217
                Shape shape = gv.getOutline((float) p.getX(), (float) p.getY());
218
                FShape myFShape = new FPolygon2D(new GeneralPathX(shape.getBounds2D()));
219
                
220
                myFShape.transform(AffineTransform.getTranslateInstance(p.getX(), p.getY()));
221
                
222
                if (rotation != 0) {
223
                        myFShape.transform(AffineTransform.getRotateInstance(rotation));
224
                }
225
                return myFShape;
226
        }
227
        
228
        public Rectangle getBounds() {
229
//                FontMetrics fm = g.getFontMetrics();
230
//                Rectangle2D rect = fm.getStringBounds("graphics", g);
231
                
232
                Rectangle bounds = getTextWrappingShape(new FPoint2D(0,0)).getBounds();
233
                return bounds;
234
        }
235

    
236
        public void setCartographicSize(double cartographicSize, FShape shp) {
237
                setFontSize(cartographicSize);
238
        }
239

    
240
        public double toCartographicSize(ViewPort viewPort, double dpi, FShape shp) {
241
                double oldSize = getFont().getSize();
242
                setCartographicSize(getCartographicSize(
243
                                                                viewPort,
244
                                                                dpi,
245
                                                                shp),
246
                                                        shp);
247
                return oldSize;
248
        }
249

    
250
        public double getCartographicSize(ViewPort viewPort, double dpi, FShape shp) {
251
                return CartographicSupportToolkit.
252
                                        getCartographicLength(this,
253
                                                                                  getFont().getSize(),
254
                                                                                  viewPort,
255
                                                                                  dpi);
256
        }
257
}