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 @ 43326

History | View | Annotate | Download (5.05 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.SVGRenderer;
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
import org.gvsig.tools.util.ToolsUtilLocator;
43

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

    
54
    public static final String SVG_STYLE_PERSISTENCE_DEF_NAME = "SVGStyle";
55
    private static final String SOURCE = "source";
56
    private final SVGRenderer renderer;
57

    
58
    /**
59
     * Constructor method
60
     * 
61
     */
62
    public SVGStyle() {
63
        this.renderer = ToolsUtilLocator.getSVGSupportManager().createRenderer();
64
    }
65

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

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

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

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

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

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

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

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

    
128
    public static class RegisterPersistence implements Callable {
129

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

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

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

    
151
    }    
152
}