Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / symbols / SimpleTextSymbol.java @ 10994

History | View | Annotate | Download (5.56 KB)

1 10679 jaume
package com.iver.cit.gvsig.fmap.core.symbols;
2
3
import java.awt.Color;
4
import java.awt.Font;
5
import java.awt.Graphics2D;
6
import java.awt.Rectangle;
7
import java.awt.Shape;
8 10994 jaume
import java.awt.font.FontRenderContext;
9
import java.awt.font.GlyphVector;
10 10679 jaume
import java.awt.geom.AffineTransform;
11
import java.awt.geom.Point2D;
12
13 10908 jaume
import javax.print.attribute.PrintRequestAttributeSet;
14
15 10679 jaume
import org.apache.batik.ext.awt.geom.PathLength;
16
17 10994 jaume
import sun.reflect.ReflectionFactory.GetReflectionFactoryAction;
18
19 10679 jaume
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
20
import com.iver.cit.gvsig.fmap.core.FPoint2D;
21 10994 jaume
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
22
import com.iver.cit.gvsig.fmap.core.FPolyline2D;
23 10679 jaume
import com.iver.cit.gvsig.fmap.core.FShape;
24 10994 jaume
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
25 10679 jaume
import com.iver.cit.gvsig.fmap.core.IGeometry;
26
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
27
import com.iver.utiles.StringUtilities;
28
import com.iver.utiles.XMLEntity;
29
import com.vividsolutions.jts.geom.Geometry;
30
import com.vividsolutions.jts.geom.Point;
31
32
public class SimpleTextSymbol extends AbstractSymbol implements ITextSymbol {
33
        private String text = "";
34
        private Font font = new Font("Arial", Font.PLAIN, 10);
35
        private Color textColor = Color.BLACK;
36 10812 jaume
        private double rotation;
37 10679 jaume
38
        public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp) {
39 10959 jaume
                if (!isShapeVisible()) return;
40
41 10679 jaume
                Point2D p = null;
42 10994 jaume
                double rot=0;
43 10679 jaume
                shp.transform(affineTransform);
44
                switch (shp.getShapeType()) {
45
                case FShape.POINT:
46
                        FPoint2D fp=(FPoint2D) shp;
47
                        p = new Point2D.Double(fp.getX(),fp.getY());
48 10812 jaume
                        rot = rotation;
49 10679 jaume
                        break;
50
                case FShape.LINE:
51
                        PathLength pathLen = new PathLength(shp);
52
53
                        // if (pathLen.lengthOfPath() < width / mT.getScaleX()) return;
54
                        float midDistance = pathLen.lengthOfPath() / 2;
55
                        p = pathLen.pointAtLength(midDistance);
56
                        rot = pathLen.angleAtLength(midDistance);
57
                        break;
58
                case FShape.POLYGON:
59
                        Geometry geo = FConverter.java2d_to_jts(shp);
60
61
                        if (geo == null) {
62
                                return;
63
                        }
64
65
                        Point pJTS = geo.getCentroid();
66
67
                        if (pJTS != null) {
68
                                FShape pLabel = FConverter.jts_to_java2d(pJTS);
69
                                p= new Point2D.Double(((FPoint2D) pLabel).getX(),
70
                                                ((FPoint2D) pLabel).getY());
71
                        }
72
                        break;
73
                }
74 10812 jaume
75
                g.setColor(textColor);
76
                g.setFont(font);
77 10679 jaume
                g.translate(p.getX(), p.getY());
78 10812 jaume
79
                // TODO comprovar que els radians no estiga rotant-los al rev?s
80
                // en AttrInTableLabels est? posat a -rot per a que els veja b?
81
                // i ?s prou sospit?s. Mira on m?s es fa ?s d'este s?mbol i comprova
82
                // que all? no estiga fent-se tamb? -rot, si ?s aix?, ?s perqu? ac?
83
                // est? al reves (i en tots els llocs on es gasta)
84 10679 jaume
                g.rotate(rot);
85
                g.drawString(getText(), 0, 0);
86
                g.rotate(-rot);
87
                g.translate(-p.getX(), -p.getY());
88
        }
89
90
        public void drawInsideRectangle(Graphics2D g,
91
                        AffineTransform scaleInstance, Rectangle r) {
92
                g.drawString("Not yet implemented", 0, r.height);
93
94
        }
95
96
        public int getOnePointRgb() {
97 10812 jaume
                return textColor.getRGB();
98 10679 jaume
        }
99
100
        public int getPixExtentPlus(Graphics2D g, AffineTransform affineTransform,
101
                        Shape shp) {
102
                // TODO Implement it
103
                throw new Error("Not yet implemented!");
104
105
        }
106
107
        public ISymbol getSymbolForSelection() {
108
                return this; // a text is not selectable
109
        }
110
111
        public int getSymbolType() {
112
                return FShape.TEXT;
113
        }
114
115
        public XMLEntity getXMLEntity() {
116
                XMLEntity xml = new XMLEntity();
117
                xml.putProperty("className", getClassName());
118
                xml.putProperty("desc", getDescription());
119 10844 jaume
                xml.putProperty("isShapeVisible", isShapeVisible());
120 10679 jaume
                xml.putProperty("font", font.getName());
121
                xml.putProperty("fontStyle", font.getStyle());
122 10812 jaume
                xml.putProperty("size", font.getSize());
123 10679 jaume
                xml.putProperty("text", text);
124
                xml.putProperty("textColor", StringUtilities.color2String(textColor));
125
                return xml;
126
        }
127
128
        public boolean isSuitableFor(IGeometry geom) {
129
                return true;
130
        }
131
132
        public void setXMLEntity(XMLEntity xml) {
133
                font = new Font(xml.getStringProperty("font"),
134
                                xml.getIntProperty("fontStyle"),
135
                                xml.getIntProperty("size"));
136 10844 jaume
                setDescription(xml.getStringProperty("desc"));
137
                setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
138 10679 jaume
                text = xml.getStringProperty("text");
139
                textColor = StringUtilities.string2Color(xml.getStringProperty("textColor"));
140
        }
141
142
        public String getClassName() {
143
                return getClass().getName();
144
        }
145
146 10908 jaume
        public void print(Graphics2D g, AffineTransform at, FShape shape, PrintRequestAttributeSet properties)
147 10679 jaume
                        throws ReadDriverException {
148
                // TODO Implement it
149
                throw new Error("Not yet implemented!");
150
151
        }
152
153
        public String getText() {
154
                return text;
155
        }
156
157
        public Font getFont() {
158
                return font;
159
        }
160
161
        public Color getTextColor() {
162
                return textColor;
163
        }
164
165
        public void setText(String text) {
166
                this.text = text;
167
        }
168
169
        public void setFont(Font font) {
170
                this.font = font;
171
        }
172
173
        public void setTextColor(Color color) {
174
                this.textColor = color;
175
        }
176
177 10812 jaume
        public void setFontSize(double size) {
178
                this.font = new Font(font.getName(), font.getStyle(), (int) size);
179
        }
180
181
        public void setRotation(double rotation) {
182
                this.rotation = rotation;
183
        }
184
185 10994 jaume
        public FShape getTextWrappingShape(Graphics2D g, AffineTransform affineTransform, FShape targetSymbolShape ) {
186
                // assuming FShape is a point with the starting position of the text
187
                Font font = getFont();
188 10977 jaume
189 10994 jaume
                FontRenderContext frc = g.getFontRenderContext();
190
191
                GlyphVector gv = font.createGlyphVector(frc, text);
192
                FPoint2D p = (FPoint2D) targetSymbolShape;
193
                p.transform(affineTransform);
194
                Shape shape = gv.getOutline((float) p.getX(), (float) p.getY());
195
196
                FShape myFShape = new FPolygon2D(new GeneralPathX(shape.getBounds2D()));
197
                if (rotation != 0) {
198
                        myFShape.transform(AffineTransform.getRotateInstance(rotation, p.getX(), p.getY()));
199
                }
200
                return myFShape;
201 10977 jaume
        }
202
203 10679 jaume
}