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 / BackgroundFileStyle.java @ 40560

History | View | Annotate | Download (6.16 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.Component;
27
import java.awt.Graphics2D;
28
import java.awt.Rectangle;
29
import java.io.File;
30
import java.io.IOException;
31
import java.net.MalformedURLException;
32
import java.net.URL;
33

    
34
import org.gvsig.fmap.mapcontext.MapContextLocator;
35
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException;
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.exception.PersistenceException;
40
import org.gvsig.tools.util.Callable;
41

    
42
/**
43
 * Defines methods that allows the user to create an style based in a
44
 * background file.For this reason, BackgroundFileStyle will
45
 * have a parameter that will be an string in order to specify this source file.
46
 *
47
 * @author jaume dominguez faus - jaume.dominguez@iver.es
48
 */
49
public abstract class BackgroundFileStyle extends AbstractStyle {
50

    
51
    public static final String BACKGROUND_FILE_STYLE_PERSISTENCE_DEFINITION_NAME = "BackgroundFileStyle";
52
    protected static final String SOURCE_SYMBOL_IN_LIBRARY = "sourceSymbolInLibrary";
53

    
54
    public static BackgroundFileStyle createStyleByURL(URL url) throws IOException {
55
        BackgroundFileStyle bgImage;
56
        String l = url.toString().toLowerCase();
57
        if (l.startsWith("http://") ||
58
            l.startsWith("https://"))  {
59
            bgImage = new RemoteFileStyle();
60
        } else if (l.toLowerCase().endsWith(".svg")) {
61
            bgImage = new SVGStyle();
62
        } else {
63
            bgImage = new ImageStyle();
64
        }
65
        bgImage.setSource(url);
66
        return bgImage;
67
    }
68
    protected URL source;
69
    /**
70
     * Sets the file that is used as a source to create the Background
71
     * @param f, File
72
     * @throws IOException
73
     */
74
    public abstract void setSource(URL url) throws IOException;
75
    /**
76
     * Gets the bounding <code>Rectangle</code> of this <code>Rectangle</code>.
77
     * <p>
78
     * This method is included for completeness, to parallel the
79
     * <code>getBounds</code> method of
80
     * {@link Component}.
81
     * @return    a new <code>Rectangle</code>, equal to the
82
     * bounding <code>Rectangle</code> for this <code>Rectangle</code>.
83
     * @see       java.awt.Component#getBounds
84
     * @see       #setBounds(Rectangle)
85
     * @see       #setBounds(int, int, int, int)
86
     * @since     JDK1.1
87
     */
88
    public abstract Rectangle getBounds();
89

    
90
    /**
91
     * Obtains the source of the file which is used to create the background
92
     * @return
93
     */
94
    public final URL getSource() {
95
        return source;
96
    }
97

    
98
    public final void drawInsideRectangle(Graphics2D g, Rectangle r) throws SymbolDrawingException {
99
        drawInsideRectangle(g, r, true);
100
    }
101

    
102
    public abstract void drawInsideRectangle(Graphics2D g, Rectangle r, boolean keepAspectRatio) throws SymbolDrawingException ;
103

    
104
    public Object clone() throws CloneNotSupportedException {
105
        return super.clone();
106
    }
107

    
108
    protected boolean isLibrarySymbol() {
109
            return source.toString().startsWith(getSymbolLibraryURL().toString());
110
    }
111
    
112
    protected URL getSymbolLibraryURL(){
113
            String symbolLibraryPath = MapContextLocator.getSymbolManager().getSymbolPreferences().getSymbolLibraryPath();
114
            URL symbolLibraryURL = null;
115
            try {
116
                        symbolLibraryURL = new File(symbolLibraryPath).toURI().toURL();
117
                } catch (MalformedURLException e) {
118
                        new RuntimeException(e);
119
                }
120
            return symbolLibraryURL;
121
    }
122
    
123
    /**
124
     * Return the final substring of the URL symbol in the library
125
     * 
126
     * @return
127
     */
128
    protected String getSourceSymbolInLibrary(){
129
            return source.toString().substring(getSymbolLibraryURL().toString().length());
130
    }
131

    
132
    public static class RegisterPersistence implements Callable {
133

    
134
        public Object call() throws Exception {
135
            PersistenceManager manager = ToolsLocator.getPersistenceManager();
136
            if( manager.getDefinition(BACKGROUND_FILE_STYLE_PERSISTENCE_DEFINITION_NAME)==null ) {
137
                DynStruct definition = manager.addDefinition(
138
                    BackgroundFileStyle.class,
139
                    BACKGROUND_FILE_STYLE_PERSISTENCE_DEFINITION_NAME,
140
                    BACKGROUND_FILE_STYLE_PERSISTENCE_DEFINITION_NAME+" Persistence definition",
141
                    null, 
142
                    null
143
                );
144

    
145
                // Extend the Style base definition
146
                definition.extend(manager.getDefinition(STYLE_PERSISTENCE_DEFINITION_NAME));
147
            }
148
            return Boolean.TRUE;
149
        }
150

    
151
    }
152

    
153
    public class PersistenceCantSetSourceException extends PersistenceException{
154

    
155
        /**
156
         * 
157
         */
158
        private static final long serialVersionUID = -3783412193739147977L;
159
        /**
160
         * 
161
         */
162
        private final static String MESSAGE_FORMAT = "Can't set source file.";
163
        private final static String MESSAGE_KEY = "_PersistenceCantSetSourceException";
164

    
165
        public PersistenceCantSetSourceException() {
166
            super(MESSAGE_FORMAT, MESSAGE_KEY, serialVersionUID);
167
        }
168
        
169
        public PersistenceCantSetSourceException(Throwable cause) {
170
            super(MESSAGE_FORMAT, cause, MESSAGE_KEY, serialVersionUID);
171
        }
172

    
173
    }
174
}