Statistics
| Revision:

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

History | View | Annotate | Download (8.02 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 9173 2006-12-04 17:13:39Z fjp $
45
 * $Log$
46
 * Revision 1.6  2006-12-04 17:13:39  fjp
47
 * *** empty log message ***
48
 *
49
 * Revision 1.5  2006/11/14 11:10:27  jaume
50
 * *** empty log message ***
51
 *
52
 * Revision 1.4  2006/11/09 18:39:05  jaume
53
 * *** empty log message ***
54
 *
55
 * Revision 1.3  2006/11/08 10:56:47  jaume
56
 * *** empty log message ***
57
 *
58
 * Revision 1.2  2006/11/06 17:08:45  jaume
59
 * *** empty log message ***
60
 *
61
 * Revision 1.1  2006/10/31 16:16:34  jaume
62
 * *** empty log message ***
63
 *
64
 * Revision 1.4  2006/10/30 19:30:35  jaume
65
 * *** empty log message ***
66
 *
67
 * Revision 1.3  2006/10/29 23:53:49  jaume
68
 * *** empty log message ***
69
 *
70
 * Revision 1.2  2006/10/26 16:27:33  jaume
71
 * support for composite marker symbols (not tested)
72
 *
73
 * Revision 1.1  2006/10/25 10:50:41  jaume
74
 * movement of classes and gui stuff
75
 *
76
 * Revision 1.3  2006/10/24 19:54:16  jaume
77
 * added IPersistence
78
 *
79
 * Revision 1.2  2006/10/24 08:02:51  jaume
80
 * *** empty log message ***
81
 *
82
 * Revision 1.1  2006/10/18 07:54:06  jaume
83
 * *** empty log message ***
84
 *
85
 *
86
 */
87
package com.iver.cit.gvsig.fmap.core.symbols;
88

    
89
import java.awt.Color;
90
import java.awt.Font;
91
import java.awt.Graphics2D;
92
import java.awt.Point;
93
import java.awt.Rectangle;
94
import java.awt.Shape;
95
import java.awt.geom.AffineTransform;
96
import java.awt.geom.Point2D;
97

    
98
import javax.print.attribute.PrintRequestAttributeSet;
99

    
100
import org.apache.batik.ext.awt.geom.PathLength;
101

    
102
import com.iver.cit.gvsig.fmap.core.FPoint2D;
103
import com.iver.cit.gvsig.fmap.core.FShape;
104
import com.iver.cit.gvsig.fmap.core.ISymbol;
105
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
106
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
107
import com.iver.utiles.StringUtilities;
108
import com.iver.utiles.XMLEntity;
109
import com.vividsolutions.jts.geom.Geometry;
110

    
111

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

    
122
        /**
123
         * Creates a new instance of CharacterMarker with default values
124
         *
125
         */
126
        public CharacterMarkerSymbol() {
127
                super();
128
        }
129

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

    
149
        public Font getFont() {
150
                return font;
151
        }
152

    
153
        public void setFont(Font font) {
154
                this.font = font;
155
        }
156

    
157
        public ISymbol getSymbolForSelection() {
158
                if (selectionSymbol == null) {
159
                        XMLEntity xml = getXMLEntity();
160
                        xml.putProperty("color", StringUtilities.color2String(Color.YELLOW));
161
                        selectionSymbol = SymbolFactory.createFromXML(xml, getDescription() + " version for selection.");
162
                }
163
                return selectionSymbol;
164
        }
165

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

    
188
                char[] text = new char[] { (char) symbol };
189
                g.drawChars(text, 0, text.length, (int) p.getX(), (int) p.getY());
190

    
191
                if (subSymbols!=null) {
192
                        for (int i = 0; i < subSymbols.length; i++) {
193
                                subSymbols[i].draw(g, affineTransform, shp);
194
                        }
195
                }
196
        }
197

    
198
        public int getPixExtentPlus(Graphics2D g, AffineTransform affineTransform,
199
                        Shape shp) {
200
                // TODO Auto-generated method stub
201
                throw new Error("Not yet implemented!");
202
        }
203

    
204
        public XMLEntity getXMLEntity() {
205
                XMLEntity xml = new XMLEntity();
206

    
207
                // the class name
208
                xml.putProperty("className", getClassName());
209

    
210
                // color
211
                xml.putProperty("color", StringUtilities.color2String(color));
212

    
213
                // font
214
                xml.putProperty("font", font.getFontName());
215

    
216
                // font style
217
                xml.putProperty("fontStyle", font.getStyle());
218

    
219
                // symbol code
220
                xml.putProperty("symbolCode", symbol);
221

    
222
                // description
223
                xml.putProperty("desc", desc);
224

    
225
                // is shape visible
226
                xml.putProperty("isShapeVisible", isShapeVisible);
227

    
228
                // x offset
229
                xml.putProperty("xOffset", getOffset().getX());
230

    
231
                // y offset
232
                xml.putProperty("yOffset", getOffset().getY());
233

    
234
                // rotation
235
                xml.putProperty("rotation", rotation);
236
                return xml;
237
        }
238

    
239
        public int getSymbolType() {
240
                return CHARACTER_MARKER;
241
        }
242

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

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

    
268
                // and apply the offset
269
                x = x + (int) offset.getX(); // TODO scale offset
270
                y = y + (int) offset.getY(); // TODO scale offset
271
                char[] text = new char[] { (char) symbol };
272
                g.drawChars(text, 0, text.length, x, y);
273

    
274
                // finally, draw the subsymbols if any
275
                if (subSymbols!=null) {
276
                        for (int i = 0; i < subSymbols.length; i++) {
277
                                subSymbols[i].drawInsideRectangle(g, scaleInstance, r);
278
                        }
279
                }
280
        }
281

    
282
        public void setCharacter(int symbol) {
283
                this.symbol = symbol;
284
        }
285

    
286
        public String getClassName() {
287
                return this.getClass().getName();
288
        }
289

    
290
        public void setXMLEntity(XMLEntity xml) {
291
                color = StringUtilities.string2Color(xml.getStringProperty("color"));
292
                Point p = new Point();
293
                p.setLocation(xml.getDoubleProperty("xOffset"), xml.getDoubleProperty("yOffset"));
294

    
295
                desc = xml.getStringProperty("desc");
296
                font = new Font(xml.getStringProperty("font"),
297
                                xml.getIntProperty("fontStyle"),
298
                                100 /* or whatever*/);
299
                isShapeVisible = xml.getBooleanProperty("isShapeVisible");
300
                symbol = xml.getIntProperty("symbolCode");
301
                offset = p;
302
                rotation = xml.getDoubleProperty("rotation");
303

    
304
        }
305

    
306
}