Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / 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 @ 42439

History | View | Annotate | Download (12.8 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/* CVS MESSAGES:
25
 *
26
 * $Id: PictureMarkerSymbol.java 15593 2007-10-29 13:01:13Z jdominguez $
27
 * $Log$
28
 * Revision 1.17  2007-09-21 12:25:32  jaume
29
 * cancellation support extended down to the IGeometry and ISymbol level
30
 *
31
 * Revision 1.16  2007/09/19 16:22:04  jaume
32
 * removed unnecessary imports
33
 *
34
 * Revision 1.15  2007/09/11 07:46:55  jaume
35
 * *** empty log message ***
36
 *
37
 * Revision 1.14  2007/08/16 06:55:19  jvidal
38
 * javadoc updated
39
 *
40
 * Revision 1.13  2007/08/09 06:42:24  jvidal
41
 * javadoc
42
 *
43
 * Revision 1.12  2007/08/08 12:05:17  jvidal
44
 * javadoc
45
 *
46
 * Revision 1.11  2007/07/18 06:54:35  jaume
47
 * continuing with cartographic support
48
 *
49
 * Revision 1.10  2007/07/03 10:58:29  jaume
50
 * first refactor on CartographicSupport
51
 *
52
 * Revision 1.9  2007/06/29 13:07:01  jaume
53
 * +PictureLineSymbol
54
 *
55
 * Revision 1.8  2007/06/11 12:25:48  jaume
56
 * ISymbol drawing integration tests (markers and lines)
57
 *
58
 * Revision 1.7  2007/06/07 06:50:40  jaume
59
 * *** empty log message ***
60
 *
61
 * Revision 1.6  2007/05/29 15:46:37  jaume
62
 * *** empty log message ***
63
 *
64
 * Revision 1.5  2007/05/08 08:47:40  jaume
65
 * *** empty log message ***
66
 *
67
 * Revision 1.4  2007/03/21 17:36:22  jaume
68
 * *** empty log message ***
69
 *
70
 * Revision 1.3  2007/03/09 11:20:57  jaume
71
 * Advanced symbology (start committing)
72
 *
73
 * Revision 1.1.2.4  2007/02/21 07:34:09  jaume
74
 * labeling starts working
75
 *
76
 * Revision 1.1.2.3  2007/02/16 10:54:12  jaume
77
 * multilayer splitted to multilayerline, multilayermarker,and  multilayerfill
78
 *
79
 * Revision 1.1.2.2  2007/02/15 16:23:44  jaume
80
 * *** empty log message ***
81
 *
82
 * Revision 1.1.2.1  2007/02/09 07:47:05  jaume
83
 * Isymbol moved
84
 *
85
 * Revision 1.1  2007/01/24 17:58:22  jaume
86
 * new features and architecture error fixes
87
 *
88
 *
89
 */
90
package org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.impl;
91

    
92
import java.awt.Graphics2D;
93
import java.awt.Rectangle;
94
import java.awt.geom.AffineTransform;
95
import java.io.IOException;
96
import java.net.URL;
97

    
98
import org.gvsig.compat.print.PrintAttributes;
99
import org.gvsig.fmap.dal.feature.Feature;
100
import org.gvsig.fmap.geom.Geometry;
101
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
102
import org.gvsig.fmap.geom.Geometry.TYPES;
103
import org.gvsig.fmap.geom.exception.CreateGeometryException;
104
import org.gvsig.fmap.mapcontext.MapContext;
105
import org.gvsig.fmap.mapcontext.MapContextLocator;
106
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
107
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException;
108
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager;
109
import org.gvsig.i18n.Messages;
110
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IPictureMarkerSymbol;
111
import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style.BackgroundFileStyle;
112
import org.gvsig.tools.ToolsLocator;
113
import org.gvsig.tools.dynobject.DynStruct;
114
import org.gvsig.tools.persistence.PersistenceManager;
115
import org.gvsig.tools.persistence.PersistentState;
116
import org.gvsig.tools.persistence.exception.PersistenceException;
117
import org.gvsig.tools.task.Cancellable;
118
import org.gvsig.tools.util.Callable;
119
import org.slf4j.Logger;
120
import org.slf4j.LoggerFactory;
121

    
122
/**
123
 * PictureMarkerSymbol allows to use an image file as a definition to be painted
124
 * instead of a marker symbol.
125
 */
126
public class PictureMarkerSymbol extends AbstractMarkerSymbol implements IPictureMarkerSymbol {
127

    
128
    private final Logger LOG =
129
        LoggerFactory.getLogger(PictureMarkerSymbol.class);
130

    
131
    public static final String PICTURE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME =
132
        "PictureMarkerSymbol";
133
    private static final String SELECTED = "selected";
134
    private static final String SELECTION_SYMBOL = "selectionSym";
135
    private static final String BACKGROUND_IMAGE = "bgImage";
136
    private static final String BACKGROUND_SELECTION_IMAGE = "bgSelImage";
137

    
138
//    private static final float SELECTION_OPACITY_FACTOR = .3F;
139

    
140
    private boolean selected;
141
    private PictureMarkerSymbol selectionSym;
142

    
143
    private BackgroundFileStyle bgImage;
144
    private BackgroundFileStyle bgSelImage;
145

    
146
    /**
147
     * Constructor method
148
     */
149
    public PictureMarkerSymbol() {
150
        super();
151
        setSize(18);
152
    }
153

    
154
    /**
155
     * Constructor method
156
     *
157
     * @param imageURL
158
     *            , URL of the normal image
159
     * @param selImageURL
160
     *            , 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
168
            setSelImage(imageURL);
169

    
170
    }
171

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

    
181
        bgImage = BackgroundFileStyle.createStyleByURL(imageUrl);
182
    }
183

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

    
194
        bgSelImage = BackgroundFileStyle.createStyleByURL(imageFileUrl);
195
    }
196

    
197
    public ISymbol getSymbolForSelection() {
198
        if (selectionSym == null) {
199
            try {
200
                selectionSym = (PictureMarkerSymbol) this.clone();
201
            } catch (CloneNotSupportedException e) {
202
                LOG.error("Error creating the selection symbol for the symbol "
203
                    + this, e);
204
            }
205
            selectionSym.selected = true;
206
            selectionSym.selectionSym = selectionSym; // avoid too much lazy
207
                                                      // creations
208
        }else{
209
            selectionSym.setColor(MapContext.getSelectionColor());
210
        }
211
        return selectionSym;
212
    }
213

    
214
    public void draw(Graphics2D g,
215
        AffineTransform affineTransform,
216
        Geometry geom,
217
        Feature f,
218
        Cancellable cancel) {
219
                org.gvsig.fmap.geom.primitive.Point p = (org.gvsig.fmap.geom.primitive.Point)geom.cloneGeometry();
220
                if (affineTransform!=null) {
221
                        p.transform(affineTransform);
222
                }
223
        double x, y;
224
        int size = (int) Math.round(getSize());
225
        double halfSize = getSize() / 2;
226
        x = p.getX() - halfSize;
227
        y = p.getY() - halfSize;
228
        int xOffset = (int) getOffset().getX();
229
        int yOffset = (int) getOffset().getY();
230

    
231
        if (size > 0) {
232
            BackgroundFileStyle bg = (!selected) ? bgImage : bgSelImage;
233
            Rectangle rect = new Rectangle(size, size);
234
            g.translate(x + xOffset, y + yOffset);
235
            double auxRotation = getRotation();
236
            g.rotate(auxRotation, halfSize, halfSize);
237
            if (bg != null) {
238
                try {
239
                    bg.drawInsideRectangle(g, rect);
240
                } catch (SymbolDrawingException e) {
241
                    LOG.warn(Messages.getText("label_style_could_not_be_painted")
242
                        + ": " + bg.getSource().toString(),
243
                        e);
244
                }
245
            } else {
246
                LOG.warn(Messages.getText("label_style_could_not_be_painted")
247
                    + ": bg is Null");
248
            }
249
            g.rotate(-auxRotation, halfSize, halfSize);
250
            g.translate(-(x + xOffset), -(y + yOffset));
251

    
252
        }
253

    
254
    }
255

    
256

    
257
    public void drawInsideRectangle(Graphics2D g, AffineTransform scaleInstance, Rectangle r, PrintAttributes properties) throws SymbolDrawingException {
258

    
259
        /*
260
         * Marker symbols which are not simple (images, etc)
261
         * are resized when drawn inside a rectangle
262
         */
263
        double saved_size = this.getSize();
264
        this.setSize(r.getHeight());
265

    
266
        super.drawInsideRectangle(g, scaleInstance, r, properties);
267

    
268
        // =================== Restoring size
269
        this.setSize(saved_size);
270
    }
271

    
272

    
273
    public String getClassName() {
274
        return getClass().getName();
275
    }
276

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

    
299
    public Object clone() throws CloneNotSupportedException {
300
        PictureMarkerSymbol copy = (PictureMarkerSymbol) super.clone();
301

    
302
        // clone selection
303
        if (selectionSym != null) {
304
                //to avoid an infinite loop
305
                if (this == selectionSym){
306
                        copy.selectionSym = copy;
307
                } else {
308
                        copy.selectionSym = (PictureMarkerSymbol) selectionSym.clone();
309
                }
310
        }
311

    
312
        // clone brackground image
313
        if (bgImage != null) {
314
            copy.bgImage = (BackgroundFileStyle) bgImage.clone();
315
        }
316

    
317
        // clone selection brackground image
318
        if (bgSelImage != null) {
319
            copy.bgSelImage = (BackgroundFileStyle) bgSelImage.clone();
320
        }
321
        return copy;
322
    }
323

    
324
    public void loadFromState(PersistentState state) throws PersistenceException {
325
        // Set parent style properties
326
        super.loadFromState(state);
327

    
328
        this.selected = (Boolean) state.get(SELECTED);
329
        this.selectionSym = (PictureMarkerSymbol) state.get(SELECTION_SYMBOL);
330
        this.bgImage = (BackgroundFileStyle) state.get(BACKGROUND_IMAGE);
331
        this.bgSelImage =
332
            (BackgroundFileStyle) state.get(BACKGROUND_SELECTION_IMAGE);
333
    }
334

    
335
    public void saveToState(PersistentState state) throws PersistenceException {
336
        // Save parent fill symbol properties
337
        super.saveToState(state);
338

    
339
        // Save own properties
340
        state.set(SELECTED, this.selected);
341
        state.set(SELECTION_SYMBOL, this.getSymbolForSelection());
342
        state.set(BACKGROUND_IMAGE, this.bgImage);
343
        state.set(BACKGROUND_SELECTION_IMAGE, this.bgSelImage);
344
    }
345

    
346
    public static class RegisterPersistence implements Callable {
347

    
348
        public Object call() throws Exception {
349
            PersistenceManager manager = ToolsLocator.getPersistenceManager();
350
            if (manager.getDefinition(PICTURE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME) == null) {
351
                DynStruct definition =
352
                    manager.addDefinition(PictureMarkerSymbol.class,
353
                        PICTURE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME,
354
                        PICTURE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME
355
                            + " Persistence definition",
356
                        null,
357
                        null);
358

    
359
                // Extend the Style base definition
360
                definition.extend(manager.getDefinition(MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME));
361

    
362
                definition.addDynFieldBoolean(SELECTED).setMandatory(false);
363
                definition.addDynFieldObject(SELECTION_SYMBOL).setMandatory(false)
364
                    .setClassOfValue(PictureMarkerSymbol.class).setMandatory(false);
365
                definition.addDynFieldObject(BACKGROUND_IMAGE)
366
                    .setClassOfValue(BackgroundFileStyle.class).setMandatory(false);
367
                definition.addDynFieldObject(BACKGROUND_SELECTION_IMAGE)
368
                    .setClassOfValue(BackgroundFileStyle.class).setMandatory(false);
369
            }
370
            return Boolean.TRUE;
371
        }
372
    }
373

    
374
        public static class RegisterSymbol implements Callable {
375

    
376
                public Object call() throws Exception {
377
                        SymbolManager manager = MapContextLocator.getSymbolManager();
378

    
379
                manager.registerSymbol(PictureMarkerSymbol.PICTURE_MARKER_SYMBOL_PERSISTENCE_DEFINITION_NAME,
380
                    PictureMarkerSymbol.class);
381

    
382
                        return Boolean.TRUE;
383
                }
384

    
385
        }
386

    
387
}