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 / style / SVGStyle.java @ 43156

History | View | Annotate | Download (5.01 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
package org.gvsig.symbology.fmap.mapcontext.rendering.symbol.style;
25

    
26
import java.awt.Graphics2D;
27
import java.awt.Rectangle;
28
import java.awt.geom.Rectangle2D;
29
import java.io.IOException;
30
import java.net.URL;
31

    
32

    
33
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol;
34
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException;
35
import org.gvsig.svgsupport.DefaultSVGRenderer;
36
import org.gvsig.tools.ToolsLocator;
37
import org.gvsig.tools.dynobject.DynStruct;
38
import org.gvsig.tools.persistence.PersistenceManager;
39
import org.gvsig.tools.persistence.PersistentState;
40
import org.gvsig.tools.persistence.exception.PersistenceException;
41
import org.gvsig.tools.util.Callable;
42

    
43
/**
44
 * Style for a SVG files.This is a XML specification and file format for
45
 * describing two-dimensional vector graphics, both static and animated.
46
 * SVG can be purely declarative or may include scripting. Images can contain
47
 * hyperlinks using outbound simple XLinks.It is an open standard created
48
 * by the World Wide Web Consortium..
49
 * 
50
 */
51
public class SVGStyle extends BackgroundFileStyle {
52

    
53
    public static final String SVG_STYLE_PERSISTENCE_DEFINITION_NAME = "SVGStyle";
54
    private static final String SOURCE = "source";
55
    private DefaultSVGRenderer renderer;
56

    
57
    /**
58
     * Constructor method
59
     * 
60
     */
61
    public SVGStyle() {
62
        this.renderer = new DefaultSVGRenderer();
63
    }
64

    
65
    @Override
66
    public void drawInsideRectangle(Graphics2D g,
67
        Rectangle rect,
68
        boolean keepAspectRatio) throws SymbolDrawingException {
69
        this.renderer.drawInsideRectangle(g, rect, keepAspectRatio);
70
    }
71

    
72
    @Override
73
    public boolean isSuitableFor(ISymbol symbol) {
74
        return true;
75
    }
76

    
77
    @Override
78
    public void setSource(URL url) throws IOException {
79

    
80
            source = url;
81
        this.renderer.setSource(url);
82
    }
83

    
84
    @Override
85
    public Rectangle getBounds() {
86
        try {
87
            Rectangle2D r = this.renderer.getBounds();
88
            return new Rectangle((int) r.getX(),
89
                (int) r.getY(),
90
                (int) r.getWidth(),
91
                (int) r.getHeight());
92
        } catch (Exception e) {
93
            return new Rectangle();
94
        }
95
    }
96

    
97
    @Override
98
    public void drawOutline(Graphics2D g, Rectangle r) throws SymbolDrawingException {
99
        drawInsideRectangle(g, r);
100
    }
101

    
102
    
103
    @Override
104
    public void loadFromState(PersistentState state) throws PersistenceException {
105
            try {
106
                    String sourceSymbolInLibrary = state.getString(SOURCE_SYMBOL_IN_LIBRARY);
107
                    if (sourceSymbolInLibrary != null){
108
                            setSource(new URL(getSymbolLibraryURL().toString()+sourceSymbolInLibrary));
109
                    } else {
110
                            setSource(state.getURL(SOURCE));
111
                    }
112
            } catch (Exception e) {
113
                    throw new PersistenceCantSetSourceException(e);
114
            }
115
    }
116

    
117
    @Override
118
    public void saveToState(PersistentState state) throws PersistenceException {
119
            if (isLibrarySymbol()){
120
                    state.set(SOURCE_SYMBOL_IN_LIBRARY, getSourceSymbolInLibrary());
121
            } else {
122
                    state.setNull(SOURCE_SYMBOL_IN_LIBRARY);
123
            }
124
        state.set(SOURCE, source);
125
    }
126

    
127
    public static class RegisterPersistence implements Callable {
128

    
129
        @Override
130
        public Object call() throws Exception {
131
            PersistenceManager manager = ToolsLocator.getPersistenceManager();
132
            if (manager.getDefinition(SVG_STYLE_PERSISTENCE_DEFINITION_NAME) == null) {
133
                DynStruct definition =
134
                    manager.addDefinition(SVGStyle.class,
135
                        SVG_STYLE_PERSISTENCE_DEFINITION_NAME,
136
                        SVG_STYLE_PERSISTENCE_DEFINITION_NAME
137
                            + " Persistence definition",
138
                        null,
139
                        null);
140

    
141
                // Extend the Style base definition
142
                definition.extend(manager.getDefinition(BACKGROUND_FILE_STYLE_PERSISTENCE_DEFINITION_NAME));
143

    
144
                definition.addDynFieldURL(SOURCE).setMandatory(true);
145
                definition.addDynFieldString(SOURCE_SYMBOL_IN_LIBRARY).setMandatory(false);
146
            }
147
            return Boolean.TRUE;
148
        }
149

    
150
    }    
151
}