Statistics
| Revision:

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

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