Statistics
| Revision:

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

History | View | Annotate | Download (9.48 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 10832 2007-03-21 11:02:17Z jaume $
45
 * $Log$
46
 * Revision 1.8  2007-03-21 11:02:17  jaume
47
 * *** empty log message ***
48
 *
49
 * Revision 1.7  2007/03/09 11:20:56  jaume
50
 * Advanced symbology (start committing)
51
 *
52
 * Revision 1.5.2.8  2007/02/21 07:34:09  jaume
53
 * labeling starts working
54
 *
55
 * Revision 1.5.2.7  2007/02/16 10:54:12  jaume
56
 * multilayer splitted to multilayerline, multilayermarker,and  multilayerfill
57
 *
58
 * Revision 1.5.2.6  2007/02/15 16:23:44  jaume
59
 * *** empty log message ***
60
 *
61
 * Revision 1.5.2.5  2007/02/14 09:58:37  jaume
62
 * *** empty log message ***
63
 *
64
 * Revision 1.5.2.4  2007/02/12 15:15:20  jaume
65
 * refactored interval legend and added graduated symbol legend
66
 *
67
 * Revision 1.5.2.3  2007/02/09 07:47:04  jaume
68
 * Isymbol moved
69
 *
70
 * Revision 1.5.2.2  2007/02/05 14:59:04  jaume
71
 * *** empty log message ***
72
 *
73
 * Revision 1.5.2.1  2007/01/30 18:10:45  jaume
74
 * start commiting labeling stuff
75
 *
76
 * Revision 1.5  2007/01/25 16:25:23  jaume
77
 * *** empty log message ***
78
 *
79
 * Revision 1.4  2007/01/24 17:58:22  jaume
80
 * new features and architecture error fixes
81
 *
82
 * Revision 1.3  2007/01/16 11:50:44  jaume
83
 * *** empty log message ***
84
 *
85
 * Revision 1.2  2007/01/10 16:39:41  jaume
86
 * ISymbol now belongs to com.iver.cit.gvsig.fmap.core.symbols package
87
 *
88
 * Revision 1.1  2007/01/10 16:31:36  jaume
89
 * *** empty log message ***
90
 *
91
 * Revision 1.6  2006/12/04 17:13:39  fjp
92
 * *** empty log message ***
93
 *
94
 * Revision 1.5  2006/11/14 11:10:27  jaume
95
 * *** empty log message ***
96
 *
97
 * Revision 1.4  2006/11/09 18:39:05  jaume
98
 * *** empty log message ***
99
 *
100
 * Revision 1.3  2006/11/08 10:56:47  jaume
101
 * *** empty log message ***
102
 *
103
 * Revision 1.2  2006/11/06 17:08:45  jaume
104
 * *** empty log message ***
105
 *
106
 * Revision 1.1  2006/10/31 16:16:34  jaume
107
 * *** empty log message ***
108
 *
109
 * Revision 1.4  2006/10/30 19:30:35  jaume
110
 * *** empty log message ***
111
 *
112
 * Revision 1.3  2006/10/29 23:53:49  jaume
113
 * *** empty log message ***
114
 *
115
 * Revision 1.2  2006/10/26 16:27:33  jaume
116
 * support for composite marker symbols (not tested)
117
 *
118
 * Revision 1.1  2006/10/25 10:50:41  jaume
119
 * movement of classes and gui stuff
120
 *
121
 * Revision 1.3  2006/10/24 19:54:16  jaume
122
 * added IPersistence
123
 *
124
 * Revision 1.2  2006/10/24 08:02:51  jaume
125
 * *** empty log message ***
126
 *
127
 * Revision 1.1  2006/10/18 07:54:06  jaume
128
 * *** empty log message ***
129
 *
130
 *
131
 */
132
package com.iver.cit.gvsig.fmap.core.symbols;
133

    
134
import java.awt.Color;
135
import java.awt.Font;
136
import java.awt.FontMetrics;
137
import java.awt.Graphics2D;
138
import java.awt.Point;
139
import java.awt.Rectangle;
140
import java.awt.RenderingHints;
141
import java.awt.Shape;
142
import java.awt.geom.AffineTransform;
143
import java.awt.geom.Point2D;
144

    
145
import org.apache.batik.ext.awt.geom.PathLength;
146

    
147
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
148
import com.iver.cit.gvsig.fmap.core.FPoint2D;
149
import com.iver.cit.gvsig.fmap.core.FShape;
150
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
151
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
152
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
153
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
154
import com.iver.utiles.StringUtilities;
155
import com.iver.utiles.XMLEntity;
156
import com.vividsolutions.jts.geom.Geometry;
157

    
158

    
159
/**
160
 * Symbol that manages symbols from a TrueType font source
161
 * @author   jaume dominguez faus - jaume.dominguez@iver.es
162
 */
163
public class CharacterMarkerSymbol extends CartographicMarkerSymbol {
164
        /**
165
         * @uml.property  name="font"
166
         */
167
        private Font font = new Font("Arial", Font.PLAIN, 20);
168
        private int symbol;
169
        private ISymbol selectionSymbol;
170

    
171
        /**
172
         * Creates a new instance of CharacterMarker with default values
173
         *
174
         */
175
        public CharacterMarkerSymbol() {
176
                super();
177
        }
178

    
179
        /**
180
         * Creates a new instance of CharacterMarker specifying the marker source
181
         * font, the character code corresponding to the symbol, and the color that
182
         * will be used in rendering time.
183
         *
184
         * @param font -
185
         *            src Font
186
         * @param charCode -
187
         *            character code of the symbol for this font
188
         * @param color -
189
         *            color to be used in when rendering.
190
         */
191
        public CharacterMarkerSymbol(Font font, int charCode, Color color) {
192
                super();
193
                this.font = font;
194
                symbol = charCode;
195
                setColor(color);
196
        }
197

    
198
        /**
199
         * @return
200
         * @uml.property  name="font"
201
         */
202
        public Font getFont() {
203
                return font;
204
        }
205

    
206
        /**
207
         * @param font
208
         * @uml.property  name="font"
209
         */
210
        public void setFont(Font font) {
211
                this.font = font;
212
        }
213

    
214
        public ISymbol getSymbolForSelection() {
215
                if (selectionSymbol == null) {
216
                        XMLEntity xml = getXMLEntity();
217
                        xml.putProperty("color", FSymbol.getSelectionColor());
218
                        selectionSymbol = SymbologyFactory.createSymbolFromXML(xml, getDescription() + " version for selection.");
219
                }
220
                return selectionSymbol;
221
        }
222

    
223
        public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp) {
224
                g.setFont(getFont());
225
                g.setColor(getColor());
226
                g.rotate(getRotation());
227
                Point2D p = null;
228
                switch (shp.getShapeType()) {
229
                case FShape.POINT:
230
                        p = new Point2D.Double(((FPoint2D) shp).getX(), ((FPoint2D) shp)
231
                                        .getY());
232
                        break;
233
                case FShape.LINE:
234
                        final PathLength pathLen = new PathLength(shp);
235
                        final float midDistance = pathLen.lengthOfPath() / 2;
236
                        p = pathLen.pointAtLength(midDistance);
237
                        break;
238
                case FShape.POLYGON:
239
                        Geometry geom = FConverter.java2d_to_jts(shp);
240
                        com.vividsolutions.jts.geom.Point centroid = geom.getCentroid();
241
                        p = new Point2D.Double(centroid.getX(), centroid.getY());
242
                }
243

    
244
                char[] text = new char[] { (char) symbol };
245
                g.drawChars(text, 0, text.length, (int) p.getX(), (int) p.getY());
246
        }
247

    
248
        public int getPixExtentPlus(Graphics2D g, AffineTransform affineTransform,
249
                        Shape shp) {
250
                return 0;
251
        }
252

    
253
        public XMLEntity getXMLEntity() {
254
                XMLEntity xml = new XMLEntity();
255

    
256
                // the class name
257
                xml.putProperty("className", getClassName());
258

    
259
                // color
260
                xml.putProperty("color", StringUtilities.color2String(getColor()));
261

    
262
                // font
263
                xml.putProperty("font", font.getFontName());
264

    
265
                // font style
266
                xml.putProperty("fontStyle", font.getStyle());
267

    
268
                // marker size
269
                xml.putProperty("size", font.getSize());
270

    
271
                // symbol code
272
                xml.putProperty("symbolCode", symbol);
273

    
274
                // description
275
                xml.putProperty("desc", getDescription());
276

    
277
                // is shape visible
278
                xml.putProperty("isShapeVisible", isShapeVisible());
279

    
280
                // x offset
281
                xml.putProperty("xOffset", getOffset().getX());
282

    
283
                // y offset
284
                xml.putProperty("yOffset", getOffset().getY());
285

    
286
                // rotation
287
                xml.putProperty("rotation", getRotation());
288
                return xml;
289
        }
290

    
291
        public int getSymbolType() {
292
                return FShape.POINT;
293
        }
294

    
295
        public void drawInsideRectangle(Graphics2D g, AffineTransform scaleInstance, Rectangle r) {
296
                g.rotate(getRotation());
297
                // Sirve de algo el scaleInstance???
298
                RenderingHints old = g.getRenderingHints();
299
                g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
300
                double h = r.getHeight();
301
                g.setFont(font);
302
                g.setColor(getColor());
303
                r.y = (int) h;
304

    
305
                int x = r.x;
306
                int y = r.y;
307
                int width = (int) r.getHeight();
308
                int height = (int) r.getWidth();
309
                int diff = (width-height);
310
                if (diff<0) {
311
                        x -= diff /2;
312
                } else {
313
                        y += diff /2;
314
                }
315

    
316
                // and apply the offset
317
                x = x + (int) getOffset().getX(); // TODO scale offset
318
                y = y + (int) getOffset().getY(); // TODO scale offset
319
                char[] text = new char[] { (char) symbol };
320
                g.drawChars(text, 0, text.length, x, y);
321
                g.setRenderingHints(old);
322
        }
323

    
324
        public void setCharacter(int symbol) {
325
                this.symbol = symbol;
326
        }
327

    
328
        public String getClassName() {
329
                return this.getClass().getName();
330
        }
331

    
332
        public void setXMLEntity(XMLEntity xml) {
333
                setColor(StringUtilities.string2Color(xml.getStringProperty("color")));
334
                Point p = new Point();
335
                p.setLocation(xml.getDoubleProperty("xOffset"), xml.getDoubleProperty("yOffset"));
336

    
337
                setDescription(xml.getStringProperty("desc"));
338
                font = new Font(xml.getStringProperty("font"),
339
                                xml.getIntProperty("fontStyle"),
340
                                xml.getIntProperty("size") /* or whatever (in fact)*/);
341
                setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
342
                symbol = xml.getIntProperty("symbolCode");
343
                setOffset(p);
344
                setRotation(xml.getDoubleProperty("rotation"));
345

    
346
        }
347

    
348
        public void print(Graphics2D g, AffineTransform at, FShape shape) throws ReadDriverException {
349
                // TODO Implement it
350
                throw new Error("Not yet implemented!");
351

    
352
        }
353

    
354
        public double getSize() {
355
                return font.getSize();
356
        }
357

    
358
        public void setSize(double size) {
359
                font = new Font(font.getName(), font.getStyle(), (int) size);
360
        }
361
}