svn-gvsig-desktop / tags / v2_0_0_Build_2052 / extensions / org.gvsig.symbology.app / org.gvsig.symbology.app.importsymbols / src / main / java / org / gvsig / symbology / app / importsymbols / ImportPictureMarkerSymbolsProcess.java @ 38862
History | View | Annotate | Download (9.46 KB)
1 | 38782 | jjdelcerro | package org.gvsig.symbology.app.importsymbols; |
---|---|---|---|
2 | |||
3 | 38806 | jjdelcerro | import java.awt.Component; |
4 | 38782 | jjdelcerro | import java.io.File; |
5 | import java.io.FileOutputStream; |
||
6 | import java.io.IOException; |
||
7 | import java.util.ArrayList; |
||
8 | import java.util.HashSet; |
||
9 | import java.util.List; |
||
10 | import java.util.Set; |
||
11 | |||
12 | import javax.swing.JOptionPane; |
||
13 | |||
14 | import org.apache.commons.io.FileUtils; |
||
15 | import org.apache.commons.io.FilenameUtils; |
||
16 | import org.gvsig.app.ApplicationLocator; |
||
17 | import org.gvsig.app.ApplicationManager; |
||
18 | import org.gvsig.fmap.mapcontext.MapContextLocator; |
||
19 | import org.gvsig.fmap.mapcontext.rendering.symbols.SymbolManager; |
||
20 | import org.gvsig.symbology.SymbologyLocator; |
||
21 | import org.gvsig.symbology.SymbologyManager; |
||
22 | import org.gvsig.symbology.fmap.mapcontext.rendering.symbol.marker.IPictureMarkerSymbol; |
||
23 | import org.gvsig.tools.ToolsLocator; |
||
24 | import org.gvsig.tools.persistence.PersistenceManager; |
||
25 | import org.gvsig.tools.persistence.PersistentState; |
||
26 | import org.gvsig.tools.task.AbstractMonitorableTask; |
||
27 | import org.gvsig.tools.task.SimpleTaskStatus; |
||
28 | import org.gvsig.tools.util.FolderSet; |
||
29 | import org.gvsig.tools.util.impl.DefaultFolderSet; |
||
30 | import org.slf4j.Logger; |
||
31 | import org.slf4j.LoggerFactory; |
||
32 | |||
33 | public class ImportPictureMarkerSymbolsProcess extends AbstractMonitorableTask { |
||
34 | private static Logger logger = LoggerFactory |
||
35 | .getLogger(ImportPictureMarkerSymbolsProcess.class); |
||
36 | |||
37 | public enum YesNoAsk { |
||
38 | YES, NO, ASK |
||
39 | }; |
||
40 | |||
41 | private ApplicationManager application = null; |
||
42 | private SymbolManager symbolManager = null; |
||
43 | private SymbologyManager symbologyManager = null; |
||
44 | |||
45 | private Set<String> allowedFileExtensions = null; |
||
46 | private List<File> notCreatedSymbols = new ArrayList<File>(); |
||
47 | |||
48 | private boolean quietMode = false; |
||
49 | |||
50 | private YesNoAsk overwrite = YesNoAsk.ASK;
|
||
51 | private List<File> selectedImageFiles = new ArrayList<File>(); |
||
52 | |||
53 | // Name of the folder in the symbols repository
|
||
54 | private String targetFolderName; |
||
55 | private double symbolSize = 18; |
||
56 | |||
57 | protected ImportPictureMarkerSymbolsProcess() {
|
||
58 | 38806 | jjdelcerro | super(false); |
59 | 38782 | jjdelcerro | this.application = ApplicationLocator.getManager();
|
60 | 38806 | jjdelcerro | this.setTaskName(application.translate("_Import_picture_marker_symbols")); |
61 | 38782 | jjdelcerro | } |
62 | |||
63 | public Set<String> getAllowedFileExtensions() { |
||
64 | if (allowedFileExtensions == null) { |
||
65 | allowedFileExtensions = new HashSet<String>(); |
||
66 | allowedFileExtensions.add("png");
|
||
67 | allowedFileExtensions.add("jpg");
|
||
68 | allowedFileExtensions.add("jpeg");
|
||
69 | allowedFileExtensions.add("gif");
|
||
70 | allowedFileExtensions.add("bmp");
|
||
71 | allowedFileExtensions.add("svg");
|
||
72 | } |
||
73 | return allowedFileExtensions;
|
||
74 | } |
||
75 | |||
76 | public YesNoAsk getOverwrite() {
|
||
77 | return overwrite;
|
||
78 | } |
||
79 | |||
80 | public void setOverwrite(YesNoAsk overwrite) { |
||
81 | this.overwrite = overwrite;
|
||
82 | } |
||
83 | |||
84 | public double getSymbolSize() { |
||
85 | return symbolSize;
|
||
86 | } |
||
87 | |||
88 | public void setSymbolSize(double symbolSize) { |
||
89 | this.symbolSize = symbolSize;
|
||
90 | } |
||
91 | |||
92 | public String getTargetFolderName() { |
||
93 | return targetFolderName;
|
||
94 | } |
||
95 | |||
96 | public void setTargetFolderName(String targetFolderName) { |
||
97 | this.targetFolderName = targetFolderName;
|
||
98 | } |
||
99 | |||
100 | public List<File> getSelectedImageFiles() { |
||
101 | return selectedImageFiles;
|
||
102 | } |
||
103 | |||
104 | public boolean isQuietMode() { |
||
105 | return quietMode;
|
||
106 | } |
||
107 | |||
108 | public void setQuietMode(boolean quietMode) { |
||
109 | this.quietMode = quietMode;
|
||
110 | } |
||
111 | |||
112 | public FolderSet getSymbolsRepository() {
|
||
113 | File symbolsFolder = new File(getSymbolManager().getSymbolPreferences() |
||
114 | .getSymbolLibraryPath()); |
||
115 | |||
116 | FolderSet repo = new DefaultFolderSet();
|
||
117 | repo.add(symbolsFolder); |
||
118 | return repo;
|
||
119 | } |
||
120 | |||
121 | public List<File> getNotCreatedSymbols() { |
||
122 | return notCreatedSymbols;
|
||
123 | } |
||
124 | |||
125 | public void run() { |
||
126 | SimpleTaskStatus status = null;
|
||
127 | try {
|
||
128 | status = (SimpleTaskStatus) this.getTaskStatus();
|
||
129 | this.preProcess();
|
||
130 | if (status.isCancellationRequested()) {
|
||
131 | status.cancel(); |
||
132 | return;
|
||
133 | } |
||
134 | 38806 | jjdelcerro | status.add(); |
135 | 38782 | jjdelcerro | status.setRangeOfValues(1, this.getSelectedImageFiles().size()); |
136 | for (int i = 1; i < this.getSelectedImageFiles().size(); i++) { |
||
137 | if (status.isCancellationRequested()) {
|
||
138 | status.cancel(); |
||
139 | return;
|
||
140 | } |
||
141 | status.setCurValue(i+1);
|
||
142 | this.processItem(this.getSelectedImageFiles().get(i)); |
||
143 | } |
||
144 | this.postPrecess();
|
||
145 | if (status.isCancellationRequested()) {
|
||
146 | status.cancel(); |
||
147 | return;
|
||
148 | } |
||
149 | if (!this.isQuietMode()) { |
||
150 | application.message("_import_of_symbols_completed",
|
||
151 | JOptionPane.INFORMATION_MESSAGE);
|
||
152 | } |
||
153 | } catch (Exception e) { |
||
154 | logger.info("Error in process", e);
|
||
155 | if (status != null) { |
||
156 | status.abort(); |
||
157 | } |
||
158 | if (!this.isQuietMode()) { |
||
159 | application.message( |
||
160 | "_Import_of_symbols_aborted" + "\n\n" + e.getMessage(), |
||
161 | JOptionPane.WARNING_MESSAGE);
|
||
162 | application.messageDialog( |
||
163 | "_Error_in_the_process_of_import_point_symbols_Some_symbols_cant_be_imported"
|
||
164 | + "\n\n" + e.getMessage(), this.getName(), |
||
165 | JOptionPane.WARNING_MESSAGE);
|
||
166 | } |
||
167 | } finally {
|
||
168 | if (status != null) { |
||
169 | // Mark process as terminate if it is running.
|
||
170 | if (status.isRunning()) {
|
||
171 | status.terminate(); |
||
172 | } |
||
173 | } |
||
174 | 38806 | jjdelcerro | application.refreshMenusAndToolBars(); |
175 | 38782 | jjdelcerro | } |
176 | } |
||
177 | |||
178 | private void preProcess() { |
||
179 | if (this.isQuietMode() && this.getOverwrite() == YesNoAsk.ASK) { |
||
180 | this.setOverwrite(YesNoAsk.YES);
|
||
181 | 38806 | jjdelcerro | } else {
|
182 | Component dlg = application.createComponent(ImportPictureMarkerSymbolsPanel.class, this); |
||
183 | application.showDialog(dlg, this.getName());
|
||
184 | 38782 | jjdelcerro | } |
185 | } |
||
186 | |||
187 | private void processItem(File item) throws InterruptedException { |
||
188 | File source_image;
|
||
189 | File source_image_sel;
|
||
190 | File target_image;
|
||
191 | File target_image_sel;
|
||
192 | File target_symbol;
|
||
193 | IPictureMarkerSymbol symbol; |
||
194 | PersistenceManager persistenceManager = ToolsLocator |
||
195 | .getPersistenceManager(); |
||
196 | |||
197 | source_image = this.getSourceImage(item);
|
||
198 | source_image_sel = this.getSourceImageSelected(item);
|
||
199 | target_image = this.getTargetImage(item);
|
||
200 | target_image_sel = this.getTargetImageSelected(item);
|
||
201 | target_symbol = this.getTargetSymbol(item);
|
||
202 | |||
203 | if (!source_image_sel.exists()) {
|
||
204 | source_image_sel = source_image; |
||
205 | } |
||
206 | |||
207 | if (target_symbol.exists()) {
|
||
208 | switch (this.getOverwrite()) { |
||
209 | case ASK:
|
||
210 | int resp = application
|
||
211 | .confirmDialog(application.translate( |
||
212 | "_Symbol_{}_already_exists_Overwrite",
|
||
213 | target_symbol.getAbsolutePath()), this
|
||
214 | .getName(), JOptionPane.YES_NO_OPTION,
|
||
215 | JOptionPane.QUESTION_MESSAGE);
|
||
216 | if (resp == JOptionPane.NO_OPTION) { |
||
217 | return;
|
||
218 | } |
||
219 | break;
|
||
220 | case NO:
|
||
221 | return;
|
||
222 | case YES:
|
||
223 | break;
|
||
224 | } |
||
225 | } |
||
226 | try {
|
||
227 | if( !target_image.getParentFile().exists() ) {
|
||
228 | target_image.getParentFile().mkdirs(); |
||
229 | } |
||
230 | copyFile(source_image, target_image); |
||
231 | copyFile(source_image_sel, target_image_sel); |
||
232 | symbol = this.getSymbologyManager().createPictureMarkerSymbol(
|
||
233 | target_image.toURI().toURL(), |
||
234 | target_image_sel.toURI().toURL()); |
||
235 | symbol.setSize(this.getSymbolSize());
|
||
236 | symbol.setDescription(FilenameUtils.getBaseName(target_image.getName())); |
||
237 | |||
238 | PersistentState state = persistenceManager.getState(symbol); |
||
239 | FileOutputStream out = new FileOutputStream(target_symbol); |
||
240 | persistenceManager.saveState(state, out); |
||
241 | } catch (Exception e) { |
||
242 | logger.info("Can't create symbol from '" + item.getAbsolutePath()
|
||
243 | + ".", e);
|
||
244 | this.notCreatedSymbols.add(item);
|
||
245 | } |
||
246 | } |
||
247 | |||
248 | private void postPrecess() { |
||
249 | if (!this.isQuietMode()) { |
||
250 | if (this.notCreatedSymbols.size() > 0) { |
||
251 | StringBuffer listOfFiles = new StringBuffer(); |
||
252 | listOfFiles.append("\n\n");
|
||
253 | for (File file : this.notCreatedSymbols) { |
||
254 | listOfFiles.append(file.getAbsolutePath()); |
||
255 | listOfFiles.append("\n");
|
||
256 | } |
||
257 | application.message( |
||
258 | application.translate("_many_symbols_can_be_createds")
|
||
259 | + listOfFiles.toString(), |
||
260 | JOptionPane.WARNING_MESSAGE);
|
||
261 | } |
||
262 | } |
||
263 | } |
||
264 | |||
265 | private void copyFile(File source, File target) throws IOException { |
||
266 | if (target.exists()) {
|
||
267 | switch (this.getOverwrite()) { |
||
268 | case ASK:
|
||
269 | int resp = application
|
||
270 | .confirmDialog(application.translate( |
||
271 | "_File_{}_already_exists_Overwrite",
|
||
272 | target.getAbsolutePath()), this.getName(),
|
||
273 | JOptionPane.YES_NO_OPTION,
|
||
274 | JOptionPane.QUESTION_MESSAGE);
|
||
275 | if (resp == JOptionPane.YES_OPTION) { |
||
276 | FileUtils.copyFile(source, target); |
||
277 | } |
||
278 | break;
|
||
279 | case NO:
|
||
280 | break;
|
||
281 | case YES:
|
||
282 | FileUtils.copyFile(source, target); |
||
283 | break;
|
||
284 | } |
||
285 | } else {
|
||
286 | FileUtils.copyFile(source, target); |
||
287 | } |
||
288 | |||
289 | } |
||
290 | |||
291 | private File getTargetSymbol(File item) { |
||
292 | File target = FileUtils.getFile(getSymbolsRepository()
|
||
293 | .asFile(), this.getTargetFolderName(),
|
||
294 | FilenameUtils.getBaseName(item.getName()) |
||
295 | + this.getSymbolManager().getSymbolPreferences()
|
||
296 | .getSymbolFileExtension()); |
||
297 | return target;
|
||
298 | } |
||
299 | |||
300 | private File getSourceImage(File item) { |
||
301 | return item;
|
||
302 | } |
||
303 | |||
304 | private File getSourceImageSelected(File item) { |
||
305 | String ext = FilenameUtils.getExtension(item.getName());
|
||
306 | String name = FilenameUtils.getBaseName(item.getName());
|
||
307 | File source = new File(item.getParentFile(), name + "_sel." + ext); |
||
308 | return source;
|
||
309 | } |
||
310 | |||
311 | private File getTargetImage(File item) { |
||
312 | File target = FileUtils.getFile(getSymbolsRepository()
|
||
313 | .asFile(), this.getTargetFolderName(), item.getName());
|
||
314 | return target;
|
||
315 | } |
||
316 | |||
317 | private File getTargetImageSelected(File item) { |
||
318 | String ext = FilenameUtils.getExtension(item.getName());
|
||
319 | String name = FilenameUtils.getBaseName(item.getName());
|
||
320 | File target = FileUtils.getFile(getSymbolsRepository()
|
||
321 | .asFile(), this.getTargetFolderName(), name + "_sel." + ext); |
||
322 | return target;
|
||
323 | } |
||
324 | |||
325 | private SymbolManager getSymbolManager() {
|
||
326 | if (symbolManager == null) { |
||
327 | symbolManager = MapContextLocator.getSymbolManager(); |
||
328 | } |
||
329 | return symbolManager;
|
||
330 | } |
||
331 | |||
332 | private SymbologyManager getSymbologyManager() {
|
||
333 | if (symbologyManager == null) { |
||
334 | symbologyManager = SymbologyLocator.getSymbologyManager(); |
||
335 | } |
||
336 | return symbologyManager;
|
||
337 | } |
||
338 | |||
339 | } |