Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2040 / 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 @ 37287

History | View | Annotate | Download (12.3 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 = (org.gvsig.fmap.geom.primitive.Point)geom.cloneGeometry();
232
                if (affineTransform!=null) {
233
                        p.transform(affineTransform);
234
                }
235
        double x, y;
236
        int size = (int) Math.round(getSize());
237
        double halfSize = getSize() / 2;
238
        x = p.getX() - halfSize;
239
        y = p.getY() - halfSize;
240
        int xOffset = (int) getOffset().getX();
241
        int yOffset = (int) getOffset().getY();
242

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

    
264
        }
265

    
266
    }
267

    
268
    public String getClassName() {
269
        return getClass().getName();
270
    }
271

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

    
294
    public Object clone() throws CloneNotSupportedException {
295
        PictureMarkerSymbol copy = (PictureMarkerSymbol) super.clone();
296

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

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

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

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

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

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

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

    
341
    public static class RegisterPersistence implements Callable {
342

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

    
354
                // Extend the Style base definition
355
                definition.extend(manager.getDefinition(MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME));
356

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

    
371
                public Object call() throws Exception {
372
                        SymbolManager manager = MapContextLocator.getSymbolManager();
373

    
374
                manager.registerSymbol(PictureMarkerSymbol.PICTURE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME,
375
                    PictureMarkerSymbol.class);
376

    
377
                        return Boolean.TRUE;
378
                }
379
                
380
        }
381

    
382
}