Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extGraph_predes / src / com / iver / cit / gvsig / fmap / core / symbols / CharacterMarkerSymbol.java @ 8729

History | View | Annotate | Download (7.89 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

    
42
/* CVS MESSAGES:
43
 *
44
 * $Id: CharacterMarkerSymbol.java 8729 2006-11-14 11:12:48Z jaume $
45
 * $Log$
46
 * Revision 1.5  2006-11-14 11:10:27  jaume
47
 * *** empty log message ***
48
 *
49
 * Revision 1.4  2006/11/09 18:39:05  jaume
50
 * *** empty log message ***
51
 *
52
 * Revision 1.3  2006/11/08 10:56:47  jaume
53
 * *** empty log message ***
54
 *
55
 * Revision 1.2  2006/11/06 17:08:45  jaume
56
 * *** empty log message ***
57
 *
58
 * Revision 1.1  2006/10/31 16:16:34  jaume
59
 * *** empty log message ***
60
 *
61
 * Revision 1.4  2006/10/30 19:30:35  jaume
62
 * *** empty log message ***
63
 *
64
 * Revision 1.3  2006/10/29 23:53:49  jaume
65
 * *** empty log message ***
66
 *
67
 * Revision 1.2  2006/10/26 16:27:33  jaume
68
 * support for composite marker symbols (not tested)
69
 *
70
 * Revision 1.1  2006/10/25 10:50:41  jaume
71
 * movement of classes and gui stuff
72
 *
73
 * Revision 1.3  2006/10/24 19:54:16  jaume
74
 * added IPersistence
75
 *
76
 * Revision 1.2  2006/10/24 08:02:51  jaume
77
 * *** empty log message ***
78
 *
79
 * Revision 1.1  2006/10/18 07:54:06  jaume
80
 * *** empty log message ***
81
 *
82
 *
83
 */
84
package com.iver.cit.gvsig.fmap.core.symbols;
85

    
86
import java.awt.Color;
87
import java.awt.Font;
88
import java.awt.Graphics2D;
89
import java.awt.Point;
90
import java.awt.Rectangle;
91
import java.awt.Shape;
92
import java.awt.geom.AffineTransform;
93
import java.awt.geom.Point2D;
94

    
95
import org.apache.batik.ext.awt.geom.PathLength;
96

    
97
import com.iver.cit.gvsig.fmap.core.FPoint2D;
98
import com.iver.cit.gvsig.fmap.core.FShape;
99
import com.iver.cit.gvsig.fmap.core.ISymbol;
100
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
101
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
102
import com.iver.utiles.StringUtilities;
103
import com.iver.utiles.XMLEntity;
104
import com.vividsolutions.jts.geom.Geometry;
105

    
106

    
107
// TODO quitar batik-util.jar del classpath de extGraph cuando movamos esto a FMap
108
/**
109
 * Symbol that manages symbols from a TrueType font source
110
 * @author jaume dominguez faus - jaume.dominguez@iver.es
111
 */
112
public class CharacterMarkerSymbol extends AbstractMarkerSymbol {
113
        private Font font = new Font("Arial", Font.PLAIN, 20);
114
        private int symbol;
115
        private ISymbol selectionSymbol;
116

    
117
        /**
118
         * Creates a new instance of CharacterMarker with default values
119
         *
120
         */
121
        public CharacterMarkerSymbol() {
122
                super();
123
        }
124

    
125
        /**
126
         * Creates a new instance of CharacterMarker specifying the marker source
127
         * font, the character code corresponding to the symbol, and the color that
128
         * will be used in rendering time.
129
         *
130
         * @param font -
131
         *            src Font
132
         * @param charCode -
133
         *            character code of the symbol for this font
134
         * @param color -
135
         *            color to be used in when rendering.
136
         */
137
        public CharacterMarkerSymbol(Font font, int charCode, Color color) {
138
                super();
139
                this.font = font;
140
                symbol = charCode;
141
                this.color = color;
142
        }
143

    
144
        public Font getFont() {
145
                return font;
146
        }
147

    
148
        public void setFont(Font font) {
149
                this.font = font;
150
        }
151

    
152
        public ISymbol getSymbolForSelection() {
153
                if (selectionSymbol == null) {
154
                        XMLEntity xml = getXMLEntity();
155
                        xml.putProperty("color", StringUtilities.color2String(Color.YELLOW));
156
                        selectionSymbol = SymbolFactory.createFromXML(xml, getDescription() + " version for selection.");
157
                }
158
                return selectionSymbol;
159
        }
160

    
161
        public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp) {
162
                g.setFont(getFont());
163
                g.setColor(color);
164
                g.rotate(getRotation());
165
                Point2D p = null;
166
                switch (shp.getShapeType()) {
167
                case FShape.POINT:
168
                        p = new Point2D.Double(((FPoint2D) shp).getX(), ((FPoint2D) shp)
169
                                        .getY());
170
                        break;
171
                case FShape.LINE:
172
                        PathLength pathLen = new PathLength(shp);
173
                        float midDistance = pathLen.lengthOfPath() / 2;
174
                        p = pathLen.pointAtLength(midDistance);
175
                        break;
176
                case FShape.POLYGON:
177
                        Geometry geom = FConverter.java2d_to_jts(shp);
178
                        com.vividsolutions.jts.geom.Point centroid = geom.getCentroid();
179
                        p = new Point2D.Double(centroid.getX(), centroid.getY());
180
                }
181
                // p = affineTransform.transform(p, null);
182

    
183
                char[] text = new char[] { (char) symbol };
184
                g.drawChars(text, 0, text.length, (int) p.getX(), (int) p.getY());
185

    
186
                if (subSymbols!=null) {
187
                        for (int i = 0; i < subSymbols.length; i++) {
188
                                subSymbols[i].draw(g, affineTransform, shp);
189
                        }
190
                }
191
        }
192

    
193
        public int getPixExtentPlus(Graphics2D g, AffineTransform affineTransform,
194
                        Shape shp) {
195
                // TODO Auto-generated method stub
196
                throw new Error("Not yet implemented!");
197
        }
198

    
199
        public XMLEntity getXMLEntity() {
200
                XMLEntity xml = new XMLEntity();
201

    
202
                // the class name
203
                xml.putProperty("className", getClassName());
204

    
205
                // color
206
                xml.putProperty("color", StringUtilities.color2String(color));
207

    
208
                // font
209
                xml.putProperty("font", font.getFontName());
210

    
211
                // font style
212
                xml.putProperty("fontStyle", font.getStyle());
213

    
214
                // symbol code
215
                xml.putProperty("symbolCode", symbol);
216

    
217
                // description
218
                xml.putProperty("desc", desc);
219

    
220
                // is shape visible
221
                xml.putProperty("isShapeVisible", isShapeVisible);
222

    
223
                // x offset
224
                xml.putProperty("xOffset", getOffset().getX());
225

    
226
                // y offset
227
                xml.putProperty("yOffset", getOffset().getY());
228

    
229
                // rotation
230
                xml.putProperty("rotation", rotation);
231
                return xml;
232
        }
233

    
234
        public int getSymbolType() {
235
                return CHARACTER_MARKER;
236
        }
237

    
238
        public void drawInsideRectangle(Graphics2D g,
239
                        AffineTransform scaleInstance, Rectangle r) {
240
                g.rotate(getRotation());
241
                // Sirve de algo el scaleInstance???
242
                // g.setFont(font.deriveFont(scaleInstance));
243
                double h = r.getHeight();
244
                Font myFont = font.deriveFont(
245
                                (float) (h * FConstant.FONT_HEIGHT_SCALE_FACTOR)).deriveFont(
246
                                                scaleInstance);
247
                g.setFont(myFont);
248
                g.setColor(color);
249
                r.y = (int) h;
250

    
251
                // center character in case the rectangle is not square
252
                int x = r.x;
253
                int y = r.y;
254
                int width = (int) r.getHeight();
255
                int height = (int) r.getWidth();
256
                int diff = (width-height);
257
                if (diff<0) {
258
                        x -= diff /2;
259
                } else {
260
                        y += diff /2;
261
                }
262

    
263
                // and apply the offset
264
                x = x + (int) offset.getX(); // TODO scale offset
265
                y = y + (int) offset.getY(); // TODO scale offset
266
                char[] text = new char[] { (char) symbol };
267
                g.drawChars(text, 0, text.length, x, y);
268

    
269
                // finally, draw the subsymbols if any
270
                if (subSymbols!=null) {
271
                        for (int i = 0; i < subSymbols.length; i++) {
272
                                subSymbols[i].drawInsideRectangle(g, scaleInstance, r);
273
                        }
274
                }
275
        }
276

    
277
        public void setCharacter(int symbol) {
278
                this.symbol = symbol;
279
        }
280

    
281
        public String getClassName() {
282
                return this.getClass().getName();
283
        }
284

    
285
        public void setXMLEntity(XMLEntity xml) {
286
                color = StringUtilities.string2Color(xml.getStringProperty("color"));
287
                Point p = new Point();
288
                p.setLocation(xml.getDoubleProperty("xOffset"), xml.getDoubleProperty("yOffset"));
289

    
290
                desc = xml.getStringProperty("desc");
291
                font = new Font(xml.getStringProperty("font"),
292
                                xml.getIntProperty("fontStyle"),
293
                                100 /* or whatever*/);
294
                isShapeVisible = xml.getBooleanProperty("isShapeVisible");
295
                symbol = xml.getIntProperty("symbolCode");
296
                offset = p;
297
                rotation = xml.getDoubleProperty("rotation");
298

    
299
        }
300
}