Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extSymbology / src / org / gvsig / symbology / fmap / symbols / PictureMarkerSymbol.java @ 27476

History | View | Annotate | Download (9.6 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: PictureMarkerSymbol.java 15593 2007-10-29 13:01:13Z jdominguez $
45
 * $Log$
46
 * Revision 1.17  2007-09-21 12:25:32  jaume
47
 * cancellation support extended down to the IGeometry and ISymbol level
48
 *
49
 * Revision 1.16  2007/09/19 16:22:04  jaume
50
 * removed unnecessary imports
51
 *
52
 * Revision 1.15  2007/09/11 07:46:55  jaume
53
 * *** empty log message ***
54
 *
55
 * Revision 1.14  2007/08/16 06:55:19  jvidal
56
 * javadoc updated
57
 *
58
 * Revision 1.13  2007/08/09 06:42:24  jvidal
59
 * javadoc
60
 *
61
 * Revision 1.12  2007/08/08 12:05:17  jvidal
62
 * javadoc
63
 *
64
 * Revision 1.11  2007/07/18 06:54:35  jaume
65
 * continuing with cartographic support
66
 *
67
 * Revision 1.10  2007/07/03 10:58:29  jaume
68
 * first refactor on CartographicSupport
69
 *
70
 * Revision 1.9  2007/06/29 13:07:01  jaume
71
 * +PictureLineSymbol
72
 *
73
 * Revision 1.8  2007/06/11 12:25:48  jaume
74
 * ISymbol drawing integration tests (markers and lines)
75
 *
76
 * Revision 1.7  2007/06/07 06:50:40  jaume
77
 * *** empty log message ***
78
 *
79
 * Revision 1.6  2007/05/29 15:46:37  jaume
80
 * *** empty log message ***
81
 *
82
 * Revision 1.5  2007/05/08 08:47:40  jaume
83
 * *** empty log message ***
84
 *
85
 * Revision 1.4  2007/03/21 17:36:22  jaume
86
 * *** empty log message ***
87
 *
88
 * Revision 1.3  2007/03/09 11:20:57  jaume
89
 * Advanced symbology (start committing)
90
 *
91
 * Revision 1.1.2.4  2007/02/21 07:34:09  jaume
92
 * labeling starts working
93
 *
94
 * Revision 1.1.2.3  2007/02/16 10:54:12  jaume
95
 * multilayer splitted to multilayerline, multilayermarker,and  multilayerfill
96
 *
97
 * Revision 1.1.2.2  2007/02/15 16:23:44  jaume
98
 * *** empty log message ***
99
 *
100
 * Revision 1.1.2.1  2007/02/09 07:47:05  jaume
101
 * Isymbol moved
102
 *
103
 * Revision 1.1  2007/01/24 17:58:22  jaume
104
 * new features and architecture error fixes
105
 *
106
 *
107
 */
108
package org.gvsig.symbology.fmap.symbols;
109

    
110
import java.awt.Graphics2D;
111
import java.awt.Rectangle;
112
import java.awt.geom.AffineTransform;
113
import java.awt.geom.Point2D;
114
import java.io.File;
115
import java.io.IOException;
116
import java.net.MalformedURLException;
117
import java.net.URL;
118

    
119
import org.apache.log4j.Logger;
120
import org.gvsig.symbology.fmap.styles.BackgroundFileStyle;
121

    
122
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
123
import com.iver.cit.gvsig.fmap.Messages;
124
import com.iver.cit.gvsig.fmap.core.FPoint2D;
125
import com.iver.cit.gvsig.fmap.core.FShape;
126
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
127
import com.iver.cit.gvsig.fmap.core.symbols.AbstractMarkerSymbol;
128
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
129
import com.iver.cit.gvsig.fmap.core.symbols.SymbolDrawingException;
130
import com.iver.utiles.XMLEntity;
131
import com.iver.utiles.swing.threads.Cancellable;
132

    
133
/**
134
 * PictureMarkerSymbol allows to use an image file as a definition to be painted
135
 * instead of a marker symbol.
136
 *
137
 * @author   jaume dominguez faus - jaume.dominguez@iver.es
138
 */
139
public class PictureMarkerSymbol extends AbstractMarkerSymbol {
140
        private static final float SELECTION_OPACITY_FACTOR = .3F;
141
//        transient private Image img;
142
        private String imagePath;
143
        private boolean selected;
144
        private PictureMarkerSymbol selectionSym;
145
//        transient private Image selImg;
146
        private String selImagePath;
147

    
148
        private BackgroundFileStyle bgImage;
149
        private BackgroundFileStyle bgSelImage;
150

    
151
        /**
152
         * Constructor method
153
         */
154
        public PictureMarkerSymbol() {
155
                super();
156
        }
157
        /**
158
         * Constructor method
159
         * @param imageURL, URL of the normal image
160
         * @param selImageURL, URL of the image when it is selected in the map
161
         * @throws IOException
162
         */
163
        public PictureMarkerSymbol(URL imageURL, URL selImageURL) throws IOException {
164
                setImage(imageURL);
165
                if (selImageURL!=null)
166
                        setSelImage(selImageURL);
167
                else setSelImage(imageURL);
168

    
169
        }
170

    
171

    
172
        /**
173
         * Sets the file for the image to be used as a marker symbol
174
         * @param imageFile, File
175
         * @throws IOException
176
         */
177
        public void setImage(URL imageUrl) throws IOException{
178

    
179
                bgImage = BackgroundFileStyle.createStyleByURL(imageUrl);
180
                imagePath = imageUrl.toString();
181
        }
182

    
183
        /**
184
         * Sets the file for the image to be used as a  marker symbol (when it is selected in the map)
185
         * @param imageFile, File
186
         * @throws IOException
187
         */
188
        public void setSelImage(URL imageFileUrl) throws IOException{
189

    
190
                bgSelImage= BackgroundFileStyle.createStyleByURL(imageFileUrl);
191
                selImagePath =  imageFileUrl.toString();
192
        }
193

    
194

    
195
        public ISymbol getSymbolForSelection() {
196
                if (selectionSym == null) {
197
                        selectionSym = (PictureMarkerSymbol) SymbologyFactory.createSymbolFromXML(getXMLEntity(), getDescription());
198
                        selectionSym.selected=true;
199
                        selectionSym.selectionSym = selectionSym; // avoid too much lazy creations
200
                }
201
                return selectionSym;
202
        }
203

    
204
        public void draw(Graphics2D g, AffineTransform affineTransform, FShape shp, Cancellable cancel) {
205
                FPoint2D p = (FPoint2D) shp;
206
                double x, y;
207
                int size = (int) Math.round(getSize());
208
                double halfSize = getSize()/2;
209
                x = p.getX() - halfSize;
210
                y = p.getY() - halfSize;
211
                int xOffset = (int) getOffset().getX();
212
                int yOffset = (int) getOffset().getY();
213

    
214
                if (size > 0) {
215
                        BackgroundFileStyle bg = (!selected) ? bgImage : bgSelImage ;
216
                        Rectangle rect = new Rectangle(        size, size );
217
                        g.translate(x+xOffset, y+yOffset);
218
                        g.rotate(getRotation(), halfSize, halfSize);
219
                        if(bg!=null){
220
                                try {
221
                                        bg.drawInsideRectangle(g, rect);
222
                                } catch (SymbolDrawingException e) {
223
                                        Logger.getLogger(getClass()).warn(Messages.getString("label_style_could_not_be_painted")+": "+imagePath, e);
224
                                }
225
                        } else {
226
                                Logger.getLogger(getClass()).warn(Messages.getString("label_style_could_not_be_painted")+": "+imagePath);
227
                        }
228
                        g.rotate(-getRotation(), halfSize, halfSize);
229
                        g.translate(-(x+xOffset), -(y+yOffset));
230

    
231
                }
232

    
233
        }
234

    
235
        public XMLEntity getXMLEntity() {
236
                XMLEntity xml = new XMLEntity();
237
                xml.putProperty("className", getClassName());
238
                xml.putProperty("isShapeVisible", isShapeVisible());
239
                xml.putProperty("desc", getDescription());
240
                xml.putProperty("imagePath", imagePath);
241
                xml.putProperty("selImagePath", selImagePath);
242
                xml.putProperty("size", getSize());
243
                xml.putProperty("offsetX", getOffset().getX());
244
                xml.putProperty("offsetY", getOffset().getY());
245
                xml.putProperty("rotation", getRotation());
246

    
247
                // measure unit
248
                xml.putProperty("unit", getUnit());
249

    
250
                // reference system
251
                xml.putProperty("referenceSystem", getReferenceSystem());
252

    
253
                return xml;
254
        }
255

    
256
        public String getClassName() {
257
                return getClass().getName();
258
        }
259

    
260
        public void setXMLEntity(XMLEntity xml) {
261
                setDescription(xml.getStringProperty("desc"));
262
                setIsShapeVisible(xml.getBooleanProperty("isShapeVisible"));
263
                imagePath = xml.getStringProperty("imagePath");
264
                selImagePath = xml.getStringProperty("selImagePath");
265
                setSize(xml.getDoubleProperty("size"));
266
                double offsetX = 0.0;
267
                double offsetY = 0.0;
268
                if(xml.contains("offsetX")){
269
                        offsetX = xml.getDoubleProperty("offsetX");
270
                }
271
                if(xml.contains("offsetY")){
272
                        offsetY = xml.getDoubleProperty("offsetY");
273
                }
274
                setOffset(new Point2D.Double(offsetX,offsetY));
275
                setReferenceSystem(xml.getIntProperty("referenceSystem"));
276
                setUnit(xml.getIntProperty("unit"));
277
                if (xml.contains("rotation"))
278
                        setRotation(xml.getDoubleProperty("rotation"));
279
                File rootDir = new File(SymbologyFactory.SymbolLibraryPath);
280
                try {
281
                        try{
282
                                setImage(new URL(imagePath));
283
                        } catch (MalformedURLException e) {
284
                                setImage(new URL("file://"+ rootDir.getAbsolutePath() + File.separator +imagePath));
285
                        }
286
                } catch (MalformedURLException e) {
287
                        Logger.getLogger(getClass()).error(Messages.getString("invalid_url")+": "+imagePath);
288
                } catch (IOException e) {
289
                        Logger.getLogger(getClass()).error(Messages.getString("invalid_url")+": "+imagePath);
290
                }
291
                try {
292
                        try{
293
                                setSelImage(new URL(selImagePath));
294
                        } catch (MalformedURLException e) {
295
                                setSelImage(new URL("file://"+ rootDir.getAbsolutePath() + File.separator +selImagePath));
296
                        }
297
                } catch (MalformedURLException e) {
298
                        Logger.getLogger(getClass()).error(Messages.getString("invalid_url")+": "+selImagePath);
299
                } catch (IOException e) {
300
                        Logger.getLogger(getClass()).error(Messages.getString("invalid_url")+": "+selImagePath);
301
                }
302

    
303

    
304
        }
305

    
306
        public void print(Graphics2D g, AffineTransform at, FShape shape)
307
        throws ReadDriverException {
308
                // TODO Implement it
309
                throw new Error("Not yet implemented!");
310

    
311
        }
312
        /**
313
         * Returns the path of the image that is used as a marker symbol
314
         * @return imagePath,String
315
         */
316
        public String getImagePath() {
317
                return imagePath;
318
        }
319
        /**
320
         * Returns the path of the image that is used as a marker symbol (when it is selected in the map)
321
         * @return selimagePath,String
322
         */
323
        public String getSelImagePath() {
324
                return selImagePath;
325
        }
326

    
327
}