root / tags / v2_0_0_Build_2049 / libraries / libFMap_mapcontext / src / org / gvsig / fmap / mapcontext / rendering / symbols / impl / DefaultSymbolManager.java @ 38488
History | View | Annotate | Download (9.81 KB)
1 | 30838 | cordinyana | /* gvSIG. Geographic Information System of the Valencian Government
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2007-2008 Infrastructures and Transports Department
|
||
4 | 31544 | cordinyana | * of the Valencian Government (CIT)
|
5 | 30838 | cordinyana | *
|
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 2
|
||
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 | */
|
||
22 | |||
23 | /*
|
||
24 | * AUTHORS (In addition to CIT):
|
||
25 | * 2009 {DiSiD Technologies} {{Task}}
|
||
26 | */
|
||
27 | package org.gvsig.fmap.mapcontext.rendering.symbols.impl; |
||
28 | |||
29 | 30892 | cordinyana | import java.awt.Color; |
30 | 30838 | cordinyana | import java.io.File; |
31 | import java.io.FileFilter; |
||
32 | import java.io.FileInputStream; |
||
33 | import java.io.FileOutputStream; |
||
34 | import java.io.IOException; |
||
35 | 30892 | cordinyana | import java.util.Collections; |
36 | import java.util.HashMap; |
||
37 | import java.util.Map; |
||
38 | 36666 | cordinyana | import java.util.Random; |
39 | 30838 | cordinyana | |
40 | 30892 | cordinyana | import org.gvsig.fmap.mapcontext.MapContextRuntimeException; |
41 | import org.gvsig.fmap.mapcontext.impl.InvalidRegisteredClassException; |
||
42 | import org.gvsig.fmap.mapcontext.impl.RegisteredClassInstantiationException; |
||
43 | import org.gvsig.fmap.mapcontext.rendering.symbols.IMultiLayerSymbol; |
||
44 | 30838 | cordinyana | import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
45 | 30892 | cordinyana | import org.gvsig.fmap.mapcontext.rendering.symbols.IWarningSymbol; |
46 | 30838 | cordinyana | import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolException; |
47 | import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager; |
||
48 | 30892 | cordinyana | import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolPreferences; |
49 | 30838 | cordinyana | import org.gvsig.tools.ToolsLocator; |
50 | import org.gvsig.tools.persistence.PersistenceManager; |
||
51 | import org.gvsig.tools.persistence.PersistentState; |
||
52 | 32880 | jjdelcerro | import org.gvsig.tools.persistence.exception.PersistenceException; |
53 | 30838 | cordinyana | |
54 | /**
|
||
55 | * Default {@link SymbolManager} implementation.
|
||
56 | *
|
||
57 | * @author gvSIG team
|
||
58 | */
|
||
59 | public class DefaultSymbolManager implements SymbolManager { |
||
60 | |||
61 | 30892 | cordinyana | private SymbolPreferences symbolPreferences =
|
62 | new DefaultSymbolPreferences();
|
||
63 | 30838 | cordinyana | |
64 | 30892 | cordinyana | private Map symbolsByName = Collections.synchronizedMap(new HashMap()); |
65 | 30838 | cordinyana | |
66 | 30892 | cordinyana | private Map symbolsByShapeType = Collections.synchronizedMap(new HashMap()); |
67 | 30838 | cordinyana | |
68 | 30892 | cordinyana | private Map multiLineSymbolsByName = |
69 | Collections.synchronizedMap(new HashMap()); |
||
70 | |||
71 | private Map multiLineSymbolsByShapeType = |
||
72 | Collections.synchronizedMap(new HashMap()); |
||
73 | |||
74 | private IWarningSymbol warningSymbol;
|
||
75 | |||
76 | private Object warningSymbolLock = new Object(); |
||
77 | |||
78 | 30838 | cordinyana | public ISymbol[] loadSymbols(File folder) throws SymbolException { |
79 | return loadSymbols(folder, null); |
||
80 | } |
||
81 | |||
82 | public ISymbol[] loadSymbols(File folder, FileFilter fileFilter) |
||
83 | throws SymbolException {
|
||
84 | // TODO: add symbol caching
|
||
85 | |||
86 | if (folder.exists()) {
|
||
87 | |||
88 | File[] symbolFiles = null; |
||
89 | if (fileFilter == null) { |
||
90 | symbolFiles = folder.listFiles(); |
||
91 | } else {
|
||
92 | symbolFiles = folder.listFiles(fileFilter); |
||
93 | } |
||
94 | |||
95 | if (symbolFiles != null) { |
||
96 | ISymbol[] symbols = new ISymbol[symbolFiles.length]; |
||
97 | for (int i = 0; i < symbolFiles.length; i++) { |
||
98 | symbols[i] = loadSymbol(symbolFiles[i]); |
||
99 | } |
||
100 | return symbols;
|
||
101 | } |
||
102 | } |
||
103 | |||
104 | return null; |
||
105 | } |
||
106 | |||
107 | public void saveSymbol(ISymbol symbol, String fileName, File folder) |
||
108 | throws SymbolException {
|
||
109 | saveSymbol(symbol, fileName, folder, false);
|
||
110 | } |
||
111 | |||
112 | public void saveSymbol(ISymbol symbol, String fileName, File folder, |
||
113 | boolean overwrite) throws SymbolException { |
||
114 | // TODO: add symbol caching
|
||
115 | |||
116 | PersistenceManager persistenceManager = ToolsLocator |
||
117 | .getPersistenceManager(); |
||
118 | |||
119 | File symbolFile = new File(folder, fileName); |
||
120 | if (!overwrite && symbolFile.exists()) {
|
||
121 | throw new SymbolFileAlreadyExistsException(symbolFile); |
||
122 | } |
||
123 | |||
124 | try {
|
||
125 | FileOutputStream fos = new FileOutputStream(symbolFile); |
||
126 | |||
127 | persistenceManager.saveState(persistenceManager.getState(symbol), |
||
128 | fos); |
||
129 | |||
130 | fos.flush(); |
||
131 | fos.close(); |
||
132 | } catch (PersistenceException e) {
|
||
133 | throw new SaveSymbolException(e); |
||
134 | } catch (IOException e) { |
||
135 | throw new SaveSymbolException(e); |
||
136 | } |
||
137 | } |
||
138 | |||
139 | /**
|
||
140 | * Loads a persisted symbol from the given file.
|
||
141 | */
|
||
142 | private ISymbol loadSymbol(File file) throws SymbolException { |
||
143 | if (file.exists()) {
|
||
144 | try {
|
||
145 | FileInputStream fis = new FileInputStream(file); |
||
146 | |||
147 | PersistenceManager persistenceManager = ToolsLocator |
||
148 | .getPersistenceManager(); |
||
149 | |||
150 | PersistentState state = persistenceManager.loadState(fis); |
||
151 | ISymbol symbol = (ISymbol) persistenceManager.create(state); |
||
152 | |||
153 | fis.close(); |
||
154 | return symbol;
|
||
155 | } catch (PersistenceException e) {
|
||
156 | throw new LoadSymbolException(e); |
||
157 | } catch (IOException e) { |
||
158 | throw new LoadSymbolException(e); |
||
159 | } |
||
160 | } |
||
161 | |||
162 | return null; |
||
163 | } |
||
164 | |||
165 | 30892 | cordinyana | public SymbolPreferences getSymbolPreferences() {
|
166 | return symbolPreferences;
|
||
167 | } |
||
168 | |||
169 | public ISymbol createSymbol(String symbolName) |
||
170 | throws MapContextRuntimeException {
|
||
171 | return createSymbol(symbolName, (Class) symbolsByName.get(symbolName), |
||
172 | ISymbol.class); |
||
173 | } |
||
174 | |||
175 | public ISymbol createSymbol(int shapeType) |
||
176 | throws MapContextRuntimeException {
|
||
177 | String symbolName =
|
||
178 | (String) symbolsByShapeType.get(new Integer(shapeType)); |
||
179 | |||
180 | return symbolName == null ? null : createSymbol(symbolName); |
||
181 | } |
||
182 | |||
183 | public ISymbol createSymbol(String symbolName, Color color) |
||
184 | throws MapContextRuntimeException {
|
||
185 | ISymbol symbol = createSymbol(symbolName); |
||
186 | |||
187 | if (symbol != null) { |
||
188 | symbol.setColor(color); |
||
189 | } |
||
190 | |||
191 | return symbol;
|
||
192 | } |
||
193 | |||
194 | public ISymbol createSymbol(int shapeType, Color color) |
||
195 | throws MapContextRuntimeException {
|
||
196 | String symbolName =
|
||
197 | (String) symbolsByShapeType.get(new Integer(shapeType)); |
||
198 | |||
199 | return symbolName == null ? null : createSymbol(symbolName, color); |
||
200 | } |
||
201 | |||
202 | public IMultiLayerSymbol createMultiLayerSymbol(String symbolName) |
||
203 | throws MapContextRuntimeException {
|
||
204 | return (IMultiLayerSymbol) createSymbol(symbolName,
|
||
205 | (Class) multiLineSymbolsByName.get(symbolName),
|
||
206 | IMultiLayerSymbol.class); |
||
207 | } |
||
208 | |||
209 | public IMultiLayerSymbol createMultiLayerSymbol(int shapeType) |
||
210 | throws MapContextRuntimeException {
|
||
211 | String symbolName =
|
||
212 | (String) multiLineSymbolsByShapeType.get(new Integer(shapeType)); |
||
213 | |||
214 | return symbolName == null ? null : createMultiLayerSymbol(symbolName); |
||
215 | } |
||
216 | |||
217 | public void registerSymbol(String symbolName, Class symbolClass) |
||
218 | throws MapContextRuntimeException {
|
||
219 | if (symbolClass == null || !ISymbol.class.isAssignableFrom(symbolClass)) { |
||
220 | throw new InvalidRegisteredClassException(ISymbol.class, |
||
221 | symbolClass, symbolName); |
||
222 | } |
||
223 | symbolsByName.put(symbolName, symbolClass); |
||
224 | } |
||
225 | |||
226 | public void registerSymbol(String symbolName, int[] shapeTypes, |
||
227 | Class symbolClass) throws MapContextRuntimeException { |
||
228 | registerSymbol(symbolName, symbolClass); |
||
229 | if (shapeTypes != null) { |
||
230 | for (int i = 0; i < shapeTypes.length; i++) { |
||
231 | symbolsByShapeType.put(new Integer(shapeTypes[i]), symbolName); |
||
232 | } |
||
233 | } |
||
234 | } |
||
235 | |||
236 | public void registerMultiLayerSymbol(String symbolName, Class symbolClass) |
||
237 | throws MapContextRuntimeException {
|
||
238 | if (symbolClass == null |
||
239 | || !IMultiLayerSymbol.class.isAssignableFrom(symbolClass)) { |
||
240 | throw new InvalidRegisteredClassException(IMultiLayerSymbol.class, |
||
241 | symbolClass, symbolName); |
||
242 | } |
||
243 | |||
244 | multiLineSymbolsByName.put(symbolName, symbolClass); |
||
245 | } |
||
246 | |||
247 | public void registerMultiLayerSymbol(String symbolName, int[] shapeTypes, |
||
248 | Class symbolClass) throws MapContextRuntimeException { |
||
249 | registerMultiLayerSymbol(symbolName, symbolClass); |
||
250 | if (shapeTypes != null) { |
||
251 | for (int i = 0; i < shapeTypes.length; i++) { |
||
252 | multiLineSymbolsByShapeType.put(new Integer(shapeTypes[i]), |
||
253 | symbolName); |
||
254 | } |
||
255 | } |
||
256 | } |
||
257 | |||
258 | public IWarningSymbol getWarningSymbol(String message, String symbolDesc, |
||
259 | int symbolDrawExceptionType) throws MapContextRuntimeException { |
||
260 | synchronized (warningSymbolLock) {
|
||
261 | if (warningSymbol == null) { |
||
262 | warningSymbol = (IWarningSymbol) createSymbol("warning");
|
||
263 | } |
||
264 | } |
||
265 | |||
266 | // TODO: set those values as parameter values in the draw method.
|
||
267 | warningSymbol.setDescription(symbolDesc); |
||
268 | warningSymbol.setMessage(message); |
||
269 | warningSymbol.setDrawExceptionType(symbolDrawExceptionType); |
||
270 | |||
271 | return warningSymbol;
|
||
272 | } |
||
273 | |||
274 | private ISymbol createSymbol(String symbolName, Class symbolClass, |
||
275 | Class expectedType) throws MapContextRuntimeException { |
||
276 | ISymbol symbol; |
||
277 | try {
|
||
278 | symbol = |
||
279 | (ISymbol) (symbolClass == null ? null |
||
280 | : symbolClass.newInstance()); |
||
281 | } catch (InstantiationException e) { |
||
282 | throw new RegisteredClassInstantiationException(expectedType, |
||
283 | symbolClass, symbolName, e); |
||
284 | } catch (IllegalAccessException e) { |
||
285 | throw new RegisteredClassInstantiationException(expectedType, |
||
286 | symbolClass, symbolName, e); |
||
287 | } |
||
288 | |||
289 | 36666 | cordinyana | Color symbolColor;
|
290 | if (getSymbolPreferences().isDefaultSymbolFillColorAleatory()) {
|
||
291 | Random rand = new Random(); |
||
292 | int numreg = rand.nextInt(255 / 2); |
||
293 | double div = (1 - rand.nextDouble() * 0.66) * 0.9; |
||
294 | symbolColor = |
||
295 | new Color( |
||
296 | ((int) (255 * div + (numreg * rand.nextDouble()))) % 255, |
||
297 | ((int) (255 * div + (numreg * rand.nextDouble()))) % 255, |
||
298 | ((int) (255 * div + (numreg * rand.nextDouble()))) % 255); |
||
299 | } |
||
300 | else {
|
||
301 | symbolColor = getSymbolPreferences().getDefaultSymbolFillColor(); |
||
302 | } |
||
303 | symbol.setColor(symbolColor); |
||
304 | 30892 | cordinyana | // Perform this initialization into the Symbol implementation
|
305 | // if (symbol instanceof CartographicSupport) {
|
||
306 | // CartographicSupport cs = (CartographicSupport) symbol;
|
||
307 | // cs.setUnit(getDefaultCartographicSupportMeasureUnit());
|
||
308 | // cs
|
||
309 | // .setReferenceSystem(getDefaultCartographicSupportReferenceSystem();
|
||
310 | // }
|
||
311 | |||
312 | return symbol;
|
||
313 | } |
||
314 | 38442 | nbrodin | |
315 | public void setSymbolPreferences(SymbolPreferences symbolPreferences) { |
||
316 | this.symbolPreferences = symbolPreferences;
|
||
317 | } |
||
318 | 30838 | cordinyana | } |