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 @ 45527
History | View | Annotate | Download (6.47 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 |
import org.gvsig.expressionevaluator.SymbolTable; |
34 |
|
35 |
import org.gvsig.fmap.mapcontext.MapContextLocator; |
36 |
import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolDrawingException; |
37 |
import org.gvsig.fmap.mapcontext.rendering.symbols.styles.IBackgroundFileStyle; |
38 |
import org.gvsig.tools.ToolsLocator; |
39 |
import org.gvsig.tools.dynobject.DynStruct; |
40 |
import org.gvsig.tools.persistence.PersistenceManager; |
41 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
42 |
import org.gvsig.tools.util.Callable; |
43 |
|
44 |
/**
|
45 |
* Defines methods that allows the user to create an style based in a
|
46 |
* background file.For this reason, BackgroundFileStyle will
|
47 |
* have a parameter that will be an string in order to specify this source file.
|
48 |
*
|
49 |
* @author jaume dominguez faus - jaume.dominguez@iver.es
|
50 |
*/
|
51 |
public abstract class BackgroundFileStyle extends AbstractStyle |
52 |
implements IBackgroundFileStyle {
|
53 |
|
54 |
public static final String BACKGROUND_FILE_STYLE_PERSISTENCE_DEFINITION_NAME = "BackgroundFileStyle"; |
55 |
protected static final String SOURCE_SYMBOL_IN_LIBRARY = "sourceSymbolInLibrary"; |
56 |
|
57 |
public static BackgroundFileStyle createStyleByURL(URL url) throws IOException { |
58 |
BackgroundFileStyle bgImage; |
59 |
String l = url.toString().toLowerCase();
|
60 |
if (l.startsWith("http://") || |
61 |
l.startsWith("https://")) {
|
62 |
bgImage = new RemoteFileStyle();
|
63 |
} else if (l.toLowerCase().endsWith(".svg")) { |
64 |
bgImage = new SVGStyle();
|
65 |
} else {
|
66 |
bgImage = new ImageStyle();
|
67 |
} |
68 |
bgImage.setSource(url); |
69 |
return bgImage;
|
70 |
} |
71 |
protected URL source; |
72 |
protected SymbolTable symbolTable;
|
73 |
/**
|
74 |
* Sets the file that is used as a source to create the Background
|
75 |
* @param f, File
|
76 |
* @throws IOException
|
77 |
*/
|
78 |
public abstract void setSource(URL url) throws IOException; |
79 |
/**
|
80 |
* Gets the bounding <code>Rectangle</code> of this <code>Rectangle</code>.
|
81 |
* <p>
|
82 |
* This method is included for completeness, to parallel the
|
83 |
* <code>getBounds</code> method of
|
84 |
* {@link Component}.
|
85 |
* @return a new <code>Rectangle</code>, equal to the
|
86 |
* bounding <code>Rectangle</code> for this <code>Rectangle</code>.
|
87 |
* @see java.awt.Component#getBounds
|
88 |
* @see #setBounds(Rectangle)
|
89 |
* @see #setBounds(int, int, int, int)
|
90 |
* @since JDK1.1
|
91 |
*/
|
92 |
public abstract Rectangle getBounds(); |
93 |
|
94 |
/**
|
95 |
* Obtains the source of the file which is used to create the background
|
96 |
* @return
|
97 |
*/
|
98 |
public final URL getSource() { |
99 |
return source;
|
100 |
} |
101 |
|
102 |
public final void drawInsideRectangle(Graphics2D g, Rectangle r) throws SymbolDrawingException { |
103 |
drawInsideRectangle(g, r, true);
|
104 |
} |
105 |
|
106 |
public abstract void drawInsideRectangle(Graphics2D g, Rectangle r, boolean keepAspectRatio) throws SymbolDrawingException ; |
107 |
|
108 |
public Object clone() throws CloneNotSupportedException { |
109 |
return super.clone(); |
110 |
} |
111 |
|
112 |
protected boolean isLibrarySymbol() { |
113 |
return source.toString().startsWith(getSymbolLibraryURL().toString());
|
114 |
} |
115 |
|
116 |
protected URL getSymbolLibraryURL(){ |
117 |
String symbolLibraryPath = MapContextLocator.getSymbolManager().getSymbolPreferences().getSymbolLibraryPath();
|
118 |
URL symbolLibraryURL = null; |
119 |
try {
|
120 |
symbolLibraryURL = new File(symbolLibraryPath).toURI().toURL(); |
121 |
} catch (MalformedURLException e) { |
122 |
new RuntimeException(e); |
123 |
} |
124 |
return symbolLibraryURL;
|
125 |
} |
126 |
|
127 |
/**
|
128 |
* Return the final substring of the URL symbol in the library
|
129 |
*
|
130 |
* @return
|
131 |
*/
|
132 |
protected String getSourceSymbolInLibrary(){ |
133 |
return source.toString().substring(getSymbolLibraryURL().toString().length());
|
134 |
} |
135 |
|
136 |
@Override
|
137 |
public void setSymbolTable(SymbolTable symbolTable) { |
138 |
this.symbolTable = symbolTable;
|
139 |
} |
140 |
|
141 |
|
142 |
public static class RegisterPersistence implements Callable { |
143 |
|
144 |
public Object call() throws Exception { |
145 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
146 |
if( manager.getDefinition(BACKGROUND_FILE_STYLE_PERSISTENCE_DEFINITION_NAME)==null ) { |
147 |
DynStruct definition = manager.addDefinition( |
148 |
BackgroundFileStyle.class, |
149 |
BACKGROUND_FILE_STYLE_PERSISTENCE_DEFINITION_NAME, |
150 |
BACKGROUND_FILE_STYLE_PERSISTENCE_DEFINITION_NAME+" Persistence definition",
|
151 |
null,
|
152 |
null
|
153 |
); |
154 |
|
155 |
// Extend the Style base definition
|
156 |
definition.extend(manager.getDefinition(STYLE_PERSISTENCE_DEFINITION_NAME)); |
157 |
} |
158 |
return Boolean.TRUE; |
159 |
} |
160 |
|
161 |
} |
162 |
|
163 |
public class PersistenceCantSetSourceException extends PersistenceException{ |
164 |
|
165 |
/**
|
166 |
*
|
167 |
*/
|
168 |
private static final long serialVersionUID = -3783412193739147977L; |
169 |
/**
|
170 |
*
|
171 |
*/
|
172 |
private final static String MESSAGE_FORMAT = "Can't set source file."; |
173 |
private final static String MESSAGE_KEY = "_PersistenceCantSetSourceException"; |
174 |
|
175 |
public PersistenceCantSetSourceException() {
|
176 |
super(MESSAGE_FORMAT, MESSAGE_KEY, serialVersionUID);
|
177 |
} |
178 |
|
179 |
public PersistenceCantSetSourceException(Throwable cause) { |
180 |
super(MESSAGE_FORMAT, cause, MESSAGE_KEY, serialVersionUID);
|
181 |
} |
182 |
|
183 |
} |
184 |
} |