Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / org.gvsig.symbology / org.gvsig.symbology.lib / org.gvsig.symbology.lib.impl / src / main / java / org / gvsig / symbology / fmap / mapcontext / rendering / symbol / marker / impl / PictureMarkerSymbol.java @ 34294

History | View | Annotate | Download (12.4 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.mapcontext.rendering.symbol.marker.impl;
109

    
110
import java.awt.Graphics2D;
111
import java.awt.Rectangle;
112
import java.awt.geom.AffineTransform;
113
import java.io.IOException;
114
import java.net.URL;
115

    
116
import org.gvsig.fmap.dal.feature.Feature;
117
import org.gvsig.fmap.geom.Geometry;
118
import org.gvsig.fmap.mapcontext.MapContextLocator;
119
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
120
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException;
121
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager;
122
import org.gvsig.i18n.Messages;
123
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IPictureMarkerSymbol;
124
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.BackgroundFileStyle;
125
import org.gvsig.tools.ToolsLocator;
126
import org.gvsig.tools.dynobject.DynStruct;
127
import org.gvsig.tools.persistence.PersistenceManager;
128
import org.gvsig.tools.persistence.PersistentState;
129
import org.gvsig.tools.persistence.exception.PersistenceException;
130
import org.gvsig.tools.task.Cancellable;
131
import org.gvsig.tools.util.Callable;
132
import org.slf4j.Logger;
133
import org.slf4j.LoggerFactory;
134

    
135
/**
136
 * PictureMarkerSymbol allows to use an image file as a definition to be painted
137
 * instead of a marker symbol.
138
 * 
139
 * @author jaume dominguez faus - jaume.dominguez@iver.es
140
 */
141
public class PictureMarkerSymbol extends AbstractMarkerSymbol implements IPictureMarkerSymbol {
142

    
143
    private final Logger LOG =
144
        LoggerFactory.getLogger(PictureMarkerSymbol.class);
145

    
146
    public static final String PICTURE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME =
147
        "PictureMarkerSymbol";
148
    private static final String SELECTED = "selected";
149
    private static final String SELECTION_SYMBOL = "selectionSym";
150
    private static final String BACKGROUND_IMAGE = "bgImage";
151
    private static final String BACKGROUND_SELECTION_IMAGE = "bgSelImage";
152

    
153
//    private static final float SELECTION_OPACITY_FACTOR = .3F;
154
    
155
    private boolean selected;
156
    private PictureMarkerSymbol selectionSym;
157

    
158
    private BackgroundFileStyle bgImage;
159
    private BackgroundFileStyle bgSelImage;
160

    
161
    /**
162
     * Constructor method
163
     */
164
    public PictureMarkerSymbol() {
165
        super();
166
    }
167

    
168
    /**
169
     * Constructor method
170
     * 
171
     * @param imageURL
172
     *            , URL of the normal image
173
     * @param selImageURL
174
     *            , URL of the image when it is selected in the map
175
     * @throws IOException
176
     */
177
    public PictureMarkerSymbol(URL imageURL, URL selImageURL) throws IOException {
178
        setImage(imageURL);
179
        if (selImageURL != null)
180
            setSelImage(selImageURL);
181
        else
182
            setSelImage(imageURL);
183

    
184
    }
185

    
186
    /**
187
     * Sets the file for the image to be used as a marker symbol
188
     * 
189
     * @param imageFile
190
     *            , File
191
     * @throws IOException
192
     */
193
    public void setImage(URL imageUrl) throws IOException {
194

    
195
        bgImage = BackgroundFileStyle.createStyleByURL(imageUrl);
196
    }
197

    
198
    /**
199
     * Sets the file for the image to be used as a marker symbol (when it is
200
     * selected in the map)
201
     * 
202
     * @param imageFile
203
     *            , File
204
     * @throws IOException
205
     */
206
    public void setSelImage(URL imageFileUrl) throws IOException {
207

    
208
        bgSelImage = BackgroundFileStyle.createStyleByURL(imageFileUrl);
209
    }
210

    
211
    public ISymbol getSymbolForSelection() {
212
        if (selectionSym == null) {
213
            try {
214
                selectionSym = (PictureMarkerSymbol) this.clone();
215
            } catch (CloneNotSupportedException e) {
216
                LOG.error("Error creating the selection symbol for the symbol "
217
                    + this, e);
218
            }
219
            selectionSym.selected = true;
220
            selectionSym.selectionSym = selectionSym; // avoid too much lazy
221
                                                      // creations
222
        }
223
        return selectionSym;
224
    }
225

    
226
    public void draw(Graphics2D g,
227
        AffineTransform affineTransform,
228
        Geometry geom,
229
        Feature f,
230
        Cancellable cancel) {
231
        org.gvsig.fmap.geom.primitive.Point p =
232
            (org.gvsig.fmap.geom.primitive.Point) geom;
233
        double x, y;
234
        int size = (int) Math.round(getSize());
235
        double halfSize = getSize() / 2;
236
        x = p.getX() - halfSize;
237
        y = p.getY() - halfSize;
238
        int xOffset = (int) getOffset().getX();
239
        int yOffset = (int) getOffset().getY();
240

    
241
        if (size > 0) {
242
            BackgroundFileStyle bg = (!selected) ? bgImage : bgSelImage;
243
            Rectangle rect = new Rectangle(size, size);
244
            g.translate(x + xOffset, y + yOffset);
245
            double auxRotation = getRotation();
246
            g.rotate(auxRotation, halfSize, halfSize);
247
            if (bg != null) {
248
                try {
249
                    bg.drawInsideRectangle(g, rect);
250
                } catch (SymbolDrawingException e) {
251
                    LOG.warn(Messages.getText("label_style_could_not_be_painted")
252
                        + ": " + bg.getSource().toString(),
253
                        e);
254
                }
255
            } else {
256
                LOG.warn(Messages.getText("label_style_could_not_be_painted")
257
                    + ": bg is Null");
258
            }
259
            g.rotate(-auxRotation, halfSize, halfSize);
260
            g.translate(-(x + xOffset), -(y + yOffset));
261

    
262
        }
263

    
264
    }
265

    
266
    public String getClassName() {
267
        return getClass().getName();
268
    }
269

    
270
    // public void print(Graphics2D g, AffineTransform at, FShape shape)
271
    // throws ReadDriverException {
272
    // // TODO Implement it
273
    // throw new Error("Not yet implemented!");
274
    //
275
    // }
276
     /**
277
      * Returns the URL of the image that is used as a marker symbol
278
      * @return imagePath,URL
279
      */
280
    public URL getSource() {
281
            return bgImage.getSource();
282
    }
283
    /**
284
     * Returns the URL of the image that is used as a marker symbol (when it
285
     is selected in the map)
286
     * @return selimagePath,URL
287
     */
288
    public URL getSelectedSource(){
289
            return bgSelImage.getSource();
290
    }
291

    
292
    public Object clone() throws CloneNotSupportedException {
293
        PictureMarkerSymbol copy = (PictureMarkerSymbol) super.clone();
294

    
295
        // clone selection
296
        if (selectionSym != null) {
297
                //to avoid an infinite loop
298
                if (this == selectionSym){
299
                        copy.selectionSym = copy;
300
                } else {
301
                        copy.selectionSym = (PictureMarkerSymbol) selectionSym.clone();
302
                }
303
        }
304

    
305
        // clone brackground image
306
        if (bgImage != null) {
307
            copy.bgImage = (BackgroundFileStyle) bgImage.clone();
308
        }
309

    
310
        // clone selection brackground image
311
        if (bgSelImage != null) {
312
            copy.bgSelImage = (BackgroundFileStyle) bgSelImage.clone();
313
        }
314
        return copy;
315
    }
316

    
317
    public void loadFromState(PersistentState state) throws PersistenceException {
318
        // Set parent style properties
319
        super.loadFromState(state);
320

    
321
        this.selected = (Boolean) state.get(SELECTED);
322
        this.selectionSym = (PictureMarkerSymbol) state.get(SELECTION_SYMBOL);
323
        this.bgImage = (BackgroundFileStyle) state.get(BACKGROUND_IMAGE);
324
        this.bgSelImage =
325
            (BackgroundFileStyle) state.get(BACKGROUND_SELECTION_IMAGE);
326
    }
327

    
328
    public void saveToState(PersistentState state) throws PersistenceException {
329
        // Save parent fill symbol properties
330
        super.saveToState(state);
331

    
332
        // Save own properties
333
        state.set(SELECTED, this.selected);
334
        state.set(SELECTION_SYMBOL, this.getSymbolForSelection());
335
        state.set(BACKGROUND_IMAGE, this.bgImage);
336
        state.set(BACKGROUND_SELECTION_IMAGE, this.bgSelImage);
337
    }
338

    
339
    public static class RegisterPersistence implements Callable {
340

    
341
        public Object call() throws Exception {
342
            PersistenceManager manager = ToolsLocator.getPersistenceManager();
343
            if (manager.getDefinition(PICTURE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME) == null) {
344
                DynStruct definition =
345
                    manager.addDefinition(PictureMarkerSymbol.class,
346
                        PICTURE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME,
347
                        PICTURE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME
348
                            + " Persistence definition",
349
                        null,
350
                        null);
351

    
352
                // Extend the Style base definition
353
                definition.extend(manager.getDefinition(MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME));
354

    
355
                definition.addDynFieldBoolean(SELECTED).setMandatory(false);
356
                definition.addDynFieldObject(SELECTION_SYMBOL).setMandatory(false)
357
                    .setClassOfValue(PictureMarkerSymbol.class).setMandatory(false);
358
                definition.addDynFieldObject(BACKGROUND_IMAGE)
359
                    .setClassOfValue(BackgroundFileStyle.class).setMandatory(false);
360
                definition.addDynFieldObject(BACKGROUND_SELECTION_IMAGE)
361
                    .setClassOfValue(BackgroundFileStyle.class).setMandatory(false);
362
            }
363
            return Boolean.TRUE;
364
        }
365
    }
366
    
367
        public static class RegisterSymbol implements Callable {
368

    
369
                public Object call() throws Exception {
370
                        int[] shapeTypes;
371
                        SymbolManager manager = MapContextLocator.getSymbolManager();
372

    
373
                shapeTypes =
374
                    new int[] { Geometry.TYPES.POINT, Geometry.TYPES.MULTIPOINT };
375
                manager.registerMultiLayerSymbol(PictureMarkerSymbol.PICTURE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME,
376
                                shapeTypes,
377
                    PictureMarkerSymbol.class);
378

    
379
                        return Boolean.TRUE;
380
                }
381
                
382
        }
383

    
384
}