svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.mapcontext / org.gvsig.fmap.mapcontext.api / src / main / java / org / gvsig / fmap / mapcontext / rendering / symbols / impl / DefaultSymbolManager.java @ 42023
History | View | Annotate | Download (23.3 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40559 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | *
|
||
6 | 41397 | jjdelcerro | * This program is free software; you can redistribute it and/or modify it under
|
7 | * the terms of the GNU General Public License as published by the Free Software
|
||
8 | * Foundation; either version 3 of the License, or (at your option) any later
|
||
9 | * version.
|
||
10 | 40559 | jjdelcerro | *
|
11 | 41397 | jjdelcerro | * This program is distributed in the hope that it will be useful, but WITHOUT
|
12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||
13 | * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
||
14 | * details.
|
||
15 | 40559 | jjdelcerro | *
|
16 | 41397 | jjdelcerro | * You should have received a copy of the GNU General Public License along with
|
17 | * this program; if not, write to the Free Software Foundation, Inc., 51
|
||
18 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
19 | 40559 | jjdelcerro | *
|
20 | 41397 | jjdelcerro | * For any additional information, do not hesitate to contact us at info AT
|
21 | * gvsig.com, or visit our website www.gvsig.com.
|
||
22 | 40435 | jjdelcerro | */
|
23 | 42023 | jjdelcerro | package org.gvsig.fmap.mapcontext.rendering.symbols.impl; |
24 | |||
25 | 40435 | jjdelcerro | /*
|
26 | 42023 | jjdelcerro | * Based on portions of code from www.ColorBrewer.org
|
27 | *
|
||
28 | 40435 | jjdelcerro | * Colors from www.ColorBrewer.org by Cynthia A. Brewer,
|
29 | * Geography, Pennsylvania State University.
|
||
30 | * Using groups of 4 colors from the 9 Diverging Schemes
|
||
31 | * 4 * 9 = 36 colors
|
||
32 | */
|
||
33 | |||
34 | 42023 | jjdelcerro | |
35 | 40435 | jjdelcerro | import java.awt.Color; |
36 | import java.io.File; |
||
37 | import java.io.FileFilter; |
||
38 | import java.io.FileInputStream; |
||
39 | import java.io.FileOutputStream; |
||
40 | import java.io.IOException; |
||
41 | 41397 | jjdelcerro | import java.util.ArrayList; |
42 | 40435 | jjdelcerro | import java.util.Arrays; |
43 | 41397 | jjdelcerro | import java.util.Collection; |
44 | 40435 | jjdelcerro | import java.util.Collections; |
45 | import java.util.Comparator; |
||
46 | import java.util.HashMap; |
||
47 | 41397 | jjdelcerro | import java.util.List; |
48 | 40435 | jjdelcerro | import java.util.Map; |
49 | import java.util.Random; |
||
50 | 41397 | jjdelcerro | import org.apache.commons.io.FileUtils; |
51 | import org.apache.commons.io.FilenameUtils; |
||
52 | import org.apache.commons.io.filefilter.FileFilterUtils; |
||
53 | import org.apache.commons.lang3.StringUtils; |
||
54 | 40435 | jjdelcerro | import org.gvsig.fmap.mapcontext.MapContextRuntimeException; |
55 | import org.gvsig.fmap.mapcontext.impl.InvalidRegisteredClassException; |
||
56 | import org.gvsig.fmap.mapcontext.impl.RegisteredClassInstantiationException; |
||
57 | import org.gvsig.fmap.mapcontext.rendering.symbols.IMultiLayerSymbol; |
||
58 | import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
||
59 | 41397 | jjdelcerro | import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol_v2; |
60 | 40435 | jjdelcerro | import org.gvsig.fmap.mapcontext.rendering.symbols.IWarningSymbol; |
61 | import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolException; |
||
62 | import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager; |
||
63 | import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolPreferences; |
||
64 | import org.gvsig.tools.ToolsLocator; |
||
65 | 40970 | jjdelcerro | import org.gvsig.tools.exception.BaseException; |
66 | 40435 | jjdelcerro | import org.gvsig.tools.persistence.PersistenceManager; |
67 | import org.gvsig.tools.persistence.PersistentState; |
||
68 | import org.gvsig.tools.persistence.exception.PersistenceException; |
||
69 | 40970 | jjdelcerro | import org.gvsig.tools.task.AbstractMonitorableTask; |
70 | 41046 | jjdelcerro | import org.gvsig.tools.task.CancellableTask; |
71 | 40435 | jjdelcerro | import org.gvsig.tools.task.SimpleTaskStatus; |
72 | import org.gvsig.tools.task.TaskStatusManager; |
||
73 | 40970 | jjdelcerro | import org.gvsig.tools.visitor.VisitCanceledException; |
74 | import org.gvsig.tools.visitor.Visitor; |
||
75 | import org.slf4j.Logger; |
||
76 | import org.slf4j.LoggerFactory; |
||
77 | 40435 | jjdelcerro | |
78 | /**
|
||
79 | * Default {@link SymbolManager} implementation.
|
||
80 | 41397 | jjdelcerro | *
|
81 | 40435 | jjdelcerro | * @author gvSIG team
|
82 | */
|
||
83 | public class DefaultSymbolManager implements SymbolManager { |
||
84 | |||
85 | 41397 | jjdelcerro | private static final Logger logger = LoggerFactory.getLogger(DefaultSymbolManager.class); |
86 | 40435 | jjdelcerro | |
87 | 41397 | jjdelcerro | private SymbolPreferences symbolPreferences
|
88 | = new DefaultSymbolPreferences();
|
||
89 | 40435 | jjdelcerro | |
90 | 41397 | jjdelcerro | private Map symbolsByName = Collections.synchronizedMap(new HashMap()); |
91 | 40435 | jjdelcerro | |
92 | 41397 | jjdelcerro | private Map symbolsByShapeType = Collections.synchronizedMap(new HashMap()); |
93 | 40435 | jjdelcerro | |
94 | 41397 | jjdelcerro | private Map multiLineSymbolsByName |
95 | = Collections.synchronizedMap(new HashMap()); |
||
96 | 40435 | jjdelcerro | |
97 | 41397 | jjdelcerro | private Map multiLineSymbolsByShapeType |
98 | = Collections.synchronizedMap(new HashMap()); |
||
99 | 40435 | jjdelcerro | |
100 | 41397 | jjdelcerro | private IWarningSymbol warningSymbol;
|
101 | 40435 | jjdelcerro | |
102 | 41397 | jjdelcerro | private Object warningSymbolLock = new Object(); |
103 | 40435 | jjdelcerro | |
104 | 41397 | jjdelcerro | public ISymbol[] loadSymbols(File folder) throws SymbolException { |
105 | return loadSymbols(folder, null); |
||
106 | } |
||
107 | 40435 | jjdelcerro | |
108 | 41397 | jjdelcerro | public ISymbol[] loadSymbols(File folder, FileFilter fileFilter) |
109 | throws SymbolException {
|
||
110 | // TODO: add symbol caching
|
||
111 | 40435 | jjdelcerro | |
112 | 41397 | jjdelcerro | if (folder.exists()) {
|
113 | 40435 | jjdelcerro | |
114 | 41397 | jjdelcerro | File[] symbolFiles = null; |
115 | if (fileFilter == null) { |
||
116 | symbolFiles = folder.listFiles(); |
||
117 | } else {
|
||
118 | symbolFiles = folder.listFiles(fileFilter); |
||
119 | } |
||
120 | |||
121 | if (symbolFiles != null) { |
||
122 | TaskStatusManager tm = ToolsLocator.getTaskStatusManager(); |
||
123 | SimpleTaskStatus status = tm.createDefaultSimpleTaskStatus("Load symbols");
|
||
124 | status.setRangeOfValues(0, symbolFiles.length);
|
||
125 | status.setAutoremove(true);
|
||
126 | status.add(); |
||
127 | |||
128 | /*
|
||
129 | * Sorting by file name before loading.
|
||
130 | * The problem here is that some
|
||
131 | * descriptions can be empty, so sorting using description
|
||
132 | * can have strange behavior. Sorting by file name is not
|
||
133 | * very elegant, though.
|
||
134 | */
|
||
135 | 40611 | jldominguez | status.message("sorting symbols");
|
136 | 41397 | jjdelcerro | Arrays.sort(symbolFiles, new Comparator() { |
137 | public int compare(Object o1, Object o2) { |
||
138 | File f1 = (File) o1; |
||
139 | File f2 = (File) o2; |
||
140 | return f1.getName().compareTo(f2.getName());
|
||
141 | } |
||
142 | }); |
||
143 | 40435 | jjdelcerro | |
144 | 41397 | jjdelcerro | ISymbol[] symbols = null; |
145 | try {
|
||
146 | symbols = new ISymbol[symbolFiles.length];
|
||
147 | for (int i = 0; i < symbolFiles.length; i++) { |
||
148 | status.setCurValue(i); |
||
149 | 41550 | jjdelcerro | File symbolFile = symbolFiles[i];
|
150 | try {
|
||
151 | symbols[i] = loadSymbol(symbolFile); |
||
152 | } catch(Throwable th) { |
||
153 | logger.warn("Can't load symbol '"+symbolFile.getAbsolutePath()+"'.",th); |
||
154 | } |
||
155 | 41397 | jjdelcerro | } |
156 | 40435 | jjdelcerro | |
157 | 41397 | jjdelcerro | } finally {
|
158 | status.terminate(); |
||
159 | } |
||
160 | return symbols;
|
||
161 | } |
||
162 | } |
||
163 | 40435 | jjdelcerro | |
164 | 41397 | jjdelcerro | return null; |
165 | } |
||
166 | 40970 | jjdelcerro | |
167 | 41397 | jjdelcerro | public class SymbolsLoaderTask extends AbstractMonitorableTask { |
168 | 40970 | jjdelcerro | |
169 | 41397 | jjdelcerro | private File folder; |
170 | private FileFilter filter; |
||
171 | private Visitor visitor;
|
||
172 | 40970 | jjdelcerro | |
173 | 41397 | jjdelcerro | public SymbolsLoaderTask(File folder, FileFilter filter, Visitor visitor) { |
174 | super("Load symbols", true); |
||
175 | this.folder = folder;
|
||
176 | this.filter = filter;
|
||
177 | this.visitor = visitor;
|
||
178 | if (folder == null) { |
||
179 | throw new IllegalArgumentException("folder is null"); |
||
180 | } |
||
181 | if (visitor == null) { |
||
182 | throw new IllegalArgumentException("visitor is null"); |
||
183 | } |
||
184 | } |
||
185 | 40970 | jjdelcerro | |
186 | 41397 | jjdelcerro | private File[] getFiles() { |
187 | if (filter == null) { |
||
188 | return folder.listFiles();
|
||
189 | } else {
|
||
190 | return folder.listFiles(filter);
|
||
191 | } |
||
192 | } |
||
193 | 40970 | jjdelcerro | |
194 | 41397 | jjdelcerro | private String getVisitorName() { |
195 | String s = visitor.toString() + "/" + visitor.getClass().getName(); |
||
196 | return s;
|
||
197 | } |
||
198 | 40970 | jjdelcerro | |
199 | 41397 | jjdelcerro | private void visit(final File file, final ISymbol symbol) throws VisitCanceledException { |
200 | try {
|
||
201 | visitor.visit(symbol); |
||
202 | } catch (BaseException e) {
|
||
203 | logger.warn("Can't call visit '" + getVisitorName() + "' to offer the symbol '" + file.getAbsolutePath() + "'.", e); |
||
204 | } |
||
205 | } |
||
206 | 40970 | jjdelcerro | |
207 | 41397 | jjdelcerro | public void run() { |
208 | // TODO: add symbol caching
|
||
209 | try {
|
||
210 | logger.info("[SymbolsLoaderTask" + this.getId() + "] process initited."); |
||
211 | if (folder.exists()) {
|
||
212 | taskStatus.setAutoremove(true);
|
||
213 | taskStatus.message("preparing");
|
||
214 | File[] symbolFiles = getFiles(); |
||
215 | 40970 | jjdelcerro | |
216 | 41397 | jjdelcerro | if (symbolFiles != null) { |
217 | taskStatus.setRangeOfValues(0, symbolFiles.length);
|
||
218 | 40970 | jjdelcerro | |
219 | 41397 | jjdelcerro | /*
|
220 | * Sorting by file name before loading. The problem here
|
||
221 | * is that some descriptions can be empty, so sorting
|
||
222 | * using description can have strange behavior. Sorting
|
||
223 | * by file name is not very elegant, though.
|
||
224 | */
|
||
225 | taskStatus.message("sorting");
|
||
226 | Arrays.sort(symbolFiles, new Comparator() { |
||
227 | public int compare(Object o1, Object o2) { |
||
228 | File f1 = (File) o1; |
||
229 | File f2 = (File) o2; |
||
230 | return f1.getName().compareTo(f2.getName());
|
||
231 | } |
||
232 | }); |
||
233 | 40435 | jjdelcerro | |
234 | 41397 | jjdelcerro | taskStatus.message("loading");
|
235 | for (int i = 0; i < symbolFiles.length; i++) { |
||
236 | taskStatus.setCurValue(i); |
||
237 | if (taskStatus.isCancellationRequested()) {
|
||
238 | logger.info("[SymbolsLoaderTask" + this.getId() + "] process canceled."); |
||
239 | break;
|
||
240 | } |
||
241 | ISymbol symbol = null;
|
||
242 | try {
|
||
243 | symbol = loadSymbol(symbolFiles[i]); |
||
244 | visit(symbolFiles[i], symbol); |
||
245 | } catch (VisitCanceledException e) {
|
||
246 | break;
|
||
247 | 41550 | jjdelcerro | } catch (Throwable e) { |
248 | 41397 | jjdelcerro | logger.warn("Can't load symbol '"
|
249 | + symbolFiles[i].getAbsolutePath() |
||
250 | + "'.", e);
|
||
251 | } |
||
252 | } |
||
253 | taskStatus.message("");
|
||
254 | 40435 | jjdelcerro | |
255 | 41397 | jjdelcerro | } |
256 | } |
||
257 | 40435 | jjdelcerro | |
258 | 41397 | jjdelcerro | } finally {
|
259 | taskStatus.terminate(); |
||
260 | } |
||
261 | logger.info("[SymbolsLoaderTask" + this.getId() + "] process terminated."); |
||
262 | 40435 | jjdelcerro | |
263 | 41397 | jjdelcerro | } |
264 | } |
||
265 | 40435 | jjdelcerro | |
266 | 41397 | jjdelcerro | public CancellableTask loadSymbols(File folder, FileFilter filter, Visitor visitor) { |
267 | SymbolsLoaderTask task = new SymbolsLoaderTask(folder, filter, visitor);
|
||
268 | task.start(); |
||
269 | return task;
|
||
270 | } |
||
271 | 40435 | jjdelcerro | |
272 | 41397 | jjdelcerro | public void saveSymbol(ISymbol symbol, String fileName, File folder) |
273 | throws SymbolException {
|
||
274 | saveSymbol(symbol, fileName, folder, false);
|
||
275 | 40435 | jjdelcerro | |
276 | 41397 | jjdelcerro | } |
277 | 40435 | jjdelcerro | |
278 | 41397 | jjdelcerro | public void saveSymbol(ISymbol symbol, String fileName, File folder, |
279 | boolean overwrite) throws SymbolException { |
||
280 | // TODO: add symbol caching
|
||
281 | 40435 | jjdelcerro | |
282 | 41397 | jjdelcerro | PersistenceManager persistenceManager = ToolsLocator |
283 | .getPersistenceManager(); |
||
284 | 40435 | jjdelcerro | |
285 | 41397 | jjdelcerro | File symbolFile = new File(folder, fileName); |
286 | if (!overwrite && symbolFile.exists()) {
|
||
287 | throw new SymbolFileAlreadyExistsException(symbolFile); |
||
288 | } |
||
289 | 40435 | jjdelcerro | |
290 | 41397 | jjdelcerro | try {
|
291 | FileOutputStream fos = new FileOutputStream(symbolFile); |
||
292 | 40435 | jjdelcerro | |
293 | 41397 | jjdelcerro | persistenceManager.saveState(persistenceManager.getState(symbol), |
294 | fos); |
||
295 | 40435 | jjdelcerro | |
296 | 41397 | jjdelcerro | fos.flush(); |
297 | fos.close(); |
||
298 | } catch (PersistenceException e) {
|
||
299 | throw new SaveSymbolException(e); |
||
300 | } catch (IOException e) { |
||
301 | throw new SaveSymbolException(e); |
||
302 | } |
||
303 | if (symbol instanceof ISymbol_v2) { |
||
304 | ISymbol_v2 symbolv2 = (ISymbol_v2) symbol; |
||
305 | if (StringUtils.isBlank(symbolv2.getID())) {
|
||
306 | symbolv2.setID(FilenameUtils.getBaseName(fileName)); |
||
307 | } |
||
308 | } |
||
309 | } |
||
310 | 40435 | jjdelcerro | |
311 | 41397 | jjdelcerro | /**
|
312 | * Loads a persisted symbol from the given file.
|
||
313 | */
|
||
314 | private ISymbol loadSymbol(File file) throws SymbolException { |
||
315 | if (file.exists()) {
|
||
316 | try {
|
||
317 | FileInputStream fis = new FileInputStream(file); |
||
318 | 40435 | jjdelcerro | |
319 | 41397 | jjdelcerro | PersistenceManager persistenceManager = ToolsLocator |
320 | .getPersistenceManager(); |
||
321 | 40435 | jjdelcerro | |
322 | 41397 | jjdelcerro | PersistentState state = persistenceManager.loadState(fis); |
323 | ISymbol symbol = (ISymbol) persistenceManager.create(state); |
||
324 | 40435 | jjdelcerro | |
325 | 41397 | jjdelcerro | fis.close(); |
326 | if (symbol instanceof ISymbol_v2) { |
||
327 | ISymbol_v2 symbolv2 = (ISymbol_v2) symbol; |
||
328 | symbolv2.setID(FilenameUtils.getBaseName(file.getName())); |
||
329 | } |
||
330 | return symbol;
|
||
331 | } catch (PersistenceException e) {
|
||
332 | throw new LoadSymbolException(e); |
||
333 | } catch (IOException e) { |
||
334 | throw new LoadSymbolException(e); |
||
335 | } |
||
336 | } |
||
337 | 40435 | jjdelcerro | |
338 | 41397 | jjdelcerro | return null; |
339 | } |
||
340 | 40435 | jjdelcerro | |
341 | 41397 | jjdelcerro | public SymbolPreferences getSymbolPreferences() {
|
342 | return symbolPreferences;
|
||
343 | } |
||
344 | 40435 | jjdelcerro | |
345 | 41397 | jjdelcerro | public ISymbol createSymbol(String symbolName) |
346 | throws MapContextRuntimeException {
|
||
347 | return createSymbol(symbolName, (Class) symbolsByName.get(symbolName), |
||
348 | ISymbol.class); |
||
349 | } |
||
350 | 40435 | jjdelcerro | |
351 | 41397 | jjdelcerro | public ISymbol createSymbol(int shapeType) |
352 | throws MapContextRuntimeException {
|
||
353 | String symbolName
|
||
354 | = (String) symbolsByShapeType.get(new Integer(shapeType)); |
||
355 | 40435 | jjdelcerro | |
356 | 41397 | jjdelcerro | return symbolName == null ? null : createSymbol(symbolName); |
357 | } |
||
358 | 40435 | jjdelcerro | |
359 | 41397 | jjdelcerro | public ISymbol createSymbol(String symbolName, Color color) |
360 | throws MapContextRuntimeException {
|
||
361 | ISymbol symbol = createSymbol(symbolName); |
||
362 | 40435 | jjdelcerro | |
363 | 41397 | jjdelcerro | if (symbol != null) { |
364 | symbol.setColor(color); |
||
365 | } |
||
366 | 40435 | jjdelcerro | |
367 | 41397 | jjdelcerro | return symbol;
|
368 | } |
||
369 | 40435 | jjdelcerro | |
370 | 41397 | jjdelcerro | public ISymbol createSymbol(int shapeType, Color color) |
371 | throws MapContextRuntimeException {
|
||
372 | String symbolName
|
||
373 | = (String) symbolsByShapeType.get(new Integer(shapeType)); |
||
374 | 40435 | jjdelcerro | |
375 | 41397 | jjdelcerro | return symbolName == null ? null : createSymbol(symbolName, color); |
376 | } |
||
377 | 40435 | jjdelcerro | |
378 | 41397 | jjdelcerro | public IMultiLayerSymbol createMultiLayerSymbol(String symbolName) |
379 | throws MapContextRuntimeException {
|
||
380 | return (IMultiLayerSymbol) createSymbol(symbolName,
|
||
381 | (Class) multiLineSymbolsByName.get(symbolName),
|
||
382 | IMultiLayerSymbol.class); |
||
383 | } |
||
384 | 40435 | jjdelcerro | |
385 | 41397 | jjdelcerro | public IMultiLayerSymbol createMultiLayerSymbol(int shapeType) |
386 | throws MapContextRuntimeException {
|
||
387 | String symbolName
|
||
388 | = (String) multiLineSymbolsByShapeType.get(new Integer(shapeType)); |
||
389 | 40435 | jjdelcerro | |
390 | 41397 | jjdelcerro | return symbolName == null ? null : createMultiLayerSymbol(symbolName); |
391 | } |
||
392 | 40435 | jjdelcerro | |
393 | 41397 | jjdelcerro | public void registerSymbol(String symbolName, Class symbolClass) |
394 | throws MapContextRuntimeException {
|
||
395 | if (symbolClass == null || !ISymbol.class.isAssignableFrom(symbolClass)) { |
||
396 | throw new InvalidRegisteredClassException(ISymbol.class, |
||
397 | symbolClass, symbolName); |
||
398 | } |
||
399 | symbolsByName.put(symbolName, symbolClass); |
||
400 | } |
||
401 | 40435 | jjdelcerro | |
402 | 41397 | jjdelcerro | public void registerSymbol(String symbolName, int[] shapeTypes, |
403 | Class symbolClass) throws MapContextRuntimeException { |
||
404 | registerSymbol(symbolName, symbolClass); |
||
405 | if (shapeTypes != null) { |
||
406 | for (int i = 0; i < shapeTypes.length; i++) { |
||
407 | symbolsByShapeType.put(new Integer(shapeTypes[i]), symbolName); |
||
408 | } |
||
409 | } |
||
410 | } |
||
411 | 40435 | jjdelcerro | |
412 | 41397 | jjdelcerro | public void registerMultiLayerSymbol(String symbolName, Class symbolClass) |
413 | throws MapContextRuntimeException {
|
||
414 | if (symbolClass == null |
||
415 | || !IMultiLayerSymbol.class.isAssignableFrom(symbolClass)) { |
||
416 | throw new InvalidRegisteredClassException(IMultiLayerSymbol.class, |
||
417 | symbolClass, symbolName); |
||
418 | } |
||
419 | |||
420 | multiLineSymbolsByName.put(symbolName, symbolClass); |
||
421 | } |
||
422 | |||
423 | public void registerMultiLayerSymbol(String symbolName, int[] shapeTypes, |
||
424 | Class symbolClass) throws MapContextRuntimeException { |
||
425 | registerMultiLayerSymbol(symbolName, symbolClass); |
||
426 | if (shapeTypes != null) { |
||
427 | for (int i = 0; i < shapeTypes.length; i++) { |
||
428 | multiLineSymbolsByShapeType.put(new Integer(shapeTypes[i]), |
||
429 | symbolName); |
||
430 | } |
||
431 | } |
||
432 | } |
||
433 | |||
434 | public IWarningSymbol getWarningSymbol(String message, String symbolDesc, |
||
435 | int symbolDrawExceptionType) throws MapContextRuntimeException { |
||
436 | synchronized (warningSymbolLock) {
|
||
437 | if (warningSymbol == null) { |
||
438 | warningSymbol = (IWarningSymbol) createSymbol("warning");
|
||
439 | } |
||
440 | } |
||
441 | |||
442 | // TODO: set those values as parameter values in the draw method.
|
||
443 | warningSymbol.setDescription(symbolDesc); |
||
444 | warningSymbol.setMessage(message); |
||
445 | warningSymbol.setDrawExceptionType(symbolDrawExceptionType); |
||
446 | |||
447 | return warningSymbol;
|
||
448 | } |
||
449 | |||
450 | private ISymbol createSymbol(String symbolName, Class symbolClass, |
||
451 | Class expectedType) throws MapContextRuntimeException { |
||
452 | ISymbol symbol; |
||
453 | try {
|
||
454 | symbol |
||
455 | = (ISymbol) (symbolClass == null ? null |
||
456 | : symbolClass.newInstance()); |
||
457 | } catch (InstantiationException e) { |
||
458 | throw new RegisteredClassInstantiationException(expectedType, |
||
459 | symbolClass, symbolName, e); |
||
460 | } catch (IllegalAccessException e) { |
||
461 | throw new RegisteredClassInstantiationException(expectedType, |
||
462 | symbolClass, symbolName, e); |
||
463 | } |
||
464 | |||
465 | Color the_color = null; |
||
466 | |||
467 | 40435 | jjdelcerro | if (getSymbolPreferences().isDefaultSymbolFillColorAleatory()) {
|
468 | 41397 | jjdelcerro | |
469 | 40435 | jjdelcerro | the_color = getRandomBrewerBasedColor(); |
470 | 41397 | jjdelcerro | |
471 | 40435 | jjdelcerro | } else {
|
472 | // not random
|
||
473 | the_color = getSymbolPreferences().getDefaultSymbolFillColor(); |
||
474 | } |
||
475 | symbol.setColor(the_color); |
||
476 | 41397 | jjdelcerro | // Perform this initialization into the Symbol implementation
|
477 | // if (symbol instanceof CartographicSupport) {
|
||
478 | // CartographicSupport cs = (CartographicSupport) symbol;
|
||
479 | // cs.setUnit(getDefaultCartographicSupportMeasureUnit());
|
||
480 | // cs
|
||
481 | // .setReferenceSystem(getDefaultCartographicSupportReferenceSystem();
|
||
482 | // }
|
||
483 | 40435 | jjdelcerro | |
484 | 41397 | jjdelcerro | return symbol;
|
485 | } |
||
486 | 40435 | jjdelcerro | |
487 | 41397 | jjdelcerro | public void setSymbolPreferences(SymbolPreferences symbolPreferences) { |
488 | this.symbolPreferences = symbolPreferences;
|
||
489 | } |
||
490 | |||
491 | public List getSymbolLibraryNames() { |
||
492 | File rootfolder = new File(this.getSymbolPreferences().getSymbolLibraryPath()); |
||
493 | Collection libraries = FileUtils.listFiles(rootfolder, FileFilterUtils.directoryFileFilter(), null); |
||
494 | List l = new ArrayList(); |
||
495 | l.addAll(libraries); |
||
496 | return l;
|
||
497 | } |
||
498 | |||
499 | public ISymbol getSymbol(String libraryName, String symbolID) throws SymbolException { |
||
500 | 41416 | jjdelcerro | Collection symbols = null; |
501 | File rootfolder = null; |
||
502 | try {
|
||
503 | rootfolder = new File(this.getSymbolPreferences().getSymbolLibraryPath()); |
||
504 | symbols = FileUtils.listFiles(rootfolder, |
||
505 | FileFilterUtils.nameFileFilter(symbolID + getSymbolPreferences().getSymbolFileExtension()), |
||
506 | FileFilterUtils.trueFileFilter() |
||
507 | ); |
||
508 | } catch(Exception ex) { |
||
509 | logger.warn("Can't get symbol from symbol library (library:'"+libraryName+"', symbol:'"+symbolID+"', symbolLibraryPath'"+rootfolder+"')", ex); |
||
510 | } |
||
511 | 41397 | jjdelcerro | if (symbols == null) { |
512 | return null; |
||
513 | } |
||
514 | if (symbols.isEmpty()) {
|
||
515 | return null; |
||
516 | } |
||
517 | |||
518 | 41416 | jjdelcerro | File f = null; |
519 | try {
|
||
520 | f = (File) symbols.iterator().next();
|
||
521 | ISymbol symbol = loadSymbol(f); |
||
522 | return symbol;
|
||
523 | } catch(Exception ex) { |
||
524 | String fname = ((f==null)?"Null":f.getAbsolutePath()); |
||
525 | logger.warn("Can't load symbol from symbol library (library:'"+libraryName+"', symbol:'"+symbolID+"', symbolLibraryPath'"+rootfolder+"', symbolFile:'"+fname+"')", ex); |
||
526 | } |
||
527 | return null; |
||
528 | 41397 | jjdelcerro | } |
529 | |||
530 | /**
|
||
531 | *
|
||
532 | * @param col
|
||
533 | * @return color 1/3 closer to black
|
||
534 | */
|
||
535 | public static Color darker(Color col) { |
||
536 | 40435 | jjdelcerro | return new Color( |
537 | 41397 | jjdelcerro | (2 * col.getRed()) / 3, |
538 | (2 * col.getGreen()) / 3, |
||
539 | (2 * col.getBlue()) / 3, |
||
540 | col.getAlpha()); |
||
541 | } |
||
542 | |||
543 | /**
|
||
544 | *
|
||
545 | * @param col
|
||
546 | * @return color 1/3 closer to white
|
||
547 | */
|
||
548 | public static Color lighter(Color col) { |
||
549 | Color resp = invert(col);
|
||
550 | resp = darker(resp); |
||
551 | return invert(resp);
|
||
552 | } |
||
553 | |||
554 | /**
|
||
555 | *
|
||
556 | * @param col
|
||
557 | * @return inverted color (inverts each band, same alpha)
|
||
558 | */
|
||
559 | public static Color invert(Color col) { |
||
560 | return new Color( |
||
561 | 255 - col.getRed(),
|
||
562 | 255 - col.getGreen(),
|
||
563 | 255 - col.getBlue(),
|
||
564 | col.getAlpha()); |
||
565 | } |
||
566 | |||
567 | private static Color getRandomBrewerBasedColor() { |
||
568 | |||
569 | int ind = rnd.nextInt(BREWER_COLOR.length);
|
||
570 | Color resp = BREWER_COLOR[ind];
|
||
571 | ind = rnd.nextInt(100);
|
||
572 | if (ind > 66) { |
||
573 | resp = darker(resp); |
||
574 | } else {
|
||
575 | if (ind > 33) { |
||
576 | resp = lighter(resp); |
||
577 | } |
||
578 | // ...else resp remains the same
|
||
579 | } |
||
580 | |||
581 | // finally add some dark noise
|
||
582 | resp = addDarkNoise(resp); |
||
583 | return resp;
|
||
584 | } |
||
585 | |||
586 | 40435 | jjdelcerro | private static Color addDarkNoise(Color c) { |
587 | int r = Math.max(0, c.getRed() - rnd.nextInt(30)); |
||
588 | int g = Math.max(0, c.getGreen() - rnd.nextInt(30)); |
||
589 | int b = Math.max(0, c.getBlue() - rnd.nextInt(30)); |
||
590 | return new Color(r, g, b, c.getAlpha()); |
||
591 | } |
||
592 | |||
593 | private static Color[] BREWER_COLOR = new Color[36]; |
||
594 | private static final Random rnd = new Random(); |
||
595 | |||
596 | 41397 | jjdelcerro | static {
|
597 | /**
|
||
598 | * Colors from www.ColorBrewer.org by Cynthia A. Brewer, Geography,
|
||
599 | * Pennsylvania State University.
|
||
600 | *
|
||
601 | * Using groups of 4 colors from the 9 Diverging Schemes 4 * 9 = 36
|
||
602 | * colors
|
||
603 | */
|
||
604 | BREWER_COLOR[0] = new Color(230, 97, 1); |
||
605 | BREWER_COLOR[1] = new Color(253, 184, 99); |
||
606 | BREWER_COLOR[2] = new Color(178, 171, 210); |
||
607 | BREWER_COLOR[3] = new Color(94, 60, 153); |
||
608 | BREWER_COLOR[4] = new Color(166, 97, 26); |
||
609 | BREWER_COLOR[5] = new Color(223, 194, 125); |
||
610 | BREWER_COLOR[6] = new Color(128, 205, 193); |
||
611 | BREWER_COLOR[7] = new Color(1, 133, 113); |
||
612 | BREWER_COLOR[8] = new Color(123, 50, 148); |
||
613 | BREWER_COLOR[9] = new Color(194, 165, 207); |
||
614 | BREWER_COLOR[10] = new Color(166, 219, 160); |
||
615 | BREWER_COLOR[11] = new Color(0, 136, 55); |
||
616 | BREWER_COLOR[12] = new Color(208, 28, 139); |
||
617 | BREWER_COLOR[13] = new Color(241, 182, 218); |
||
618 | BREWER_COLOR[14] = new Color(184, 225, 134); |
||
619 | BREWER_COLOR[15] = new Color(77, 172, 38); |
||
620 | BREWER_COLOR[16] = new Color(202, 0, 32); |
||
621 | BREWER_COLOR[17] = new Color(244, 165, 130); |
||
622 | BREWER_COLOR[18] = new Color(146, 197, 222); |
||
623 | BREWER_COLOR[19] = new Color(5, 113, 176); |
||
624 | BREWER_COLOR[20] = new Color(202, 0, 32); |
||
625 | BREWER_COLOR[21] = new Color(244, 165, 130); |
||
626 | BREWER_COLOR[22] = new Color(186, 186, 186); |
||
627 | BREWER_COLOR[23] = new Color(64, 64, 64); |
||
628 | BREWER_COLOR[24] = new Color(215, 25, 28); |
||
629 | BREWER_COLOR[25] = new Color(253, 174, 97); |
||
630 | BREWER_COLOR[26] = new Color(171, 217, 233); |
||
631 | BREWER_COLOR[27] = new Color(44, 123, 182); |
||
632 | BREWER_COLOR[28] = new Color(215, 25, 28); |
||
633 | BREWER_COLOR[29] = new Color(253, 174, 97); |
||
634 | BREWER_COLOR[30] = new Color(171, 221, 164); |
||
635 | BREWER_COLOR[31] = new Color(43, 131, 186); |
||
636 | BREWER_COLOR[32] = new Color(215, 25, 28); |
||
637 | BREWER_COLOR[33] = new Color(253, 174, 97); |
||
638 | BREWER_COLOR[34] = new Color(166, 217, 106); |
||
639 | BREWER_COLOR[35] = new Color(26, 150, 65); |
||
640 | } |
||
641 | |||
642 | } |