Statistics
| Revision:

svn-gvsig-desktop / 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 @ 37966

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
public class PictureMarkerSymbol extends AbstractMarkerSymbol implements IPictureMarkerSymbol {
140

    
141
    private final Logger LOG =
142
        LoggerFactory.getLogger(PictureMarkerSymbol.class);
143

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

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

    
156
    private BackgroundFileStyle bgImage;
157
    private BackgroundFileStyle bgSelImage;
158

    
159
    /**
160
     * Constructor method
161
     */
162
    public PictureMarkerSymbol() {
163
        super();
164
        setSize(18);
165
    }
166

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

    
183
    }
184

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

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

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

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

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

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

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

    
263
        }
264

    
265
    }
266

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

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

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

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

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

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

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

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

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

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

    
340
    public static class RegisterPersistence implements Callable {
341

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

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

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

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

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

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

    
381
}