svn-gvsig-desktop / branches / v2_0_0_prep / extensions / org.gvsig.symbology.app / org.gvsig.symbology.app.importsymbols / src / main / java / org / gvsig / symbology / app / importsymbols / ImportPictureMarkerSymbolsProcess.java @ 39623
History | View | Annotate | Download (9.5 KB)
1 |
package org.gvsig.symbology.app.importsymbols; |
---|---|
2 |
|
3 |
import java.awt.Component; |
4 |
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 |
super(false); |
59 |
this.application = ApplicationLocator.getManager();
|
60 |
this.setTaskName(application.translate("_Import_picture_marker_symbols")); |
61 |
} |
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 |
status.add(); |
135 |
status.setRangeOfValues(1, this.getSelectedImageFiles().size()); |
136 |
for (int i = 0; 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.messageDialog( |
151 |
"_Import_of_symbols_completed",
|
152 |
this.getName(),
|
153 |
JOptionPane.INFORMATION_MESSAGE);
|
154 |
} |
155 |
} catch (Exception e) { |
156 |
logger.info("Error in process", e);
|
157 |
if (status != null) { |
158 |
status.abort(); |
159 |
} |
160 |
if (!this.isQuietMode()) { |
161 |
application.message( |
162 |
"_Import_of_symbols_aborted" + "\n\n" + e.getMessage(), |
163 |
JOptionPane.WARNING_MESSAGE);
|
164 |
application.messageDialog( |
165 |
"_Error_in_the_process_of_import_point_symbols_Some_symbols_cant_be_imported"
|
166 |
+ "\n\n" + e.getMessage(), this.getName(), |
167 |
JOptionPane.WARNING_MESSAGE);
|
168 |
} |
169 |
} finally {
|
170 |
if (status != null) { |
171 |
// Mark process as terminate if it is running.
|
172 |
if (status.isRunning()) {
|
173 |
status.terminate(); |
174 |
} |
175 |
} |
176 |
application.refreshMenusAndToolBars(); |
177 |
} |
178 |
} |
179 |
|
180 |
private void preProcess() { |
181 |
if (this.isQuietMode() && this.getOverwrite() == YesNoAsk.ASK) { |
182 |
this.setOverwrite(YesNoAsk.YES);
|
183 |
} else {
|
184 |
Component dlg = application.createComponent(ImportPictureMarkerSymbolsPanel.class, this); |
185 |
application.showDialog(dlg, this.getName());
|
186 |
} |
187 |
} |
188 |
|
189 |
private void processItem(File item) throws InterruptedException { |
190 |
File source_image;
|
191 |
File source_image_sel;
|
192 |
File target_image;
|
193 |
File target_image_sel;
|
194 |
File target_symbol;
|
195 |
IPictureMarkerSymbol symbol; |
196 |
PersistenceManager persistenceManager = ToolsLocator |
197 |
.getPersistenceManager(); |
198 |
|
199 |
source_image = this.getSourceImage(item);
|
200 |
source_image_sel = this.getSourceImageSelected(item);
|
201 |
target_image = this.getTargetImage(item);
|
202 |
target_image_sel = this.getTargetImageSelected(item);
|
203 |
target_symbol = this.getTargetSymbol(item);
|
204 |
|
205 |
if (!source_image_sel.exists()) {
|
206 |
source_image_sel = source_image; |
207 |
} |
208 |
|
209 |
if (target_symbol.exists()) {
|
210 |
switch (this.getOverwrite()) { |
211 |
case ASK:
|
212 |
int resp = application
|
213 |
.confirmDialog(application.translate( |
214 |
"_Symbol_{0}_already_exists_Overwrite",
|
215 |
target_symbol.getAbsolutePath()), this
|
216 |
.getName(), JOptionPane.YES_NO_OPTION,
|
217 |
JOptionPane.QUESTION_MESSAGE);
|
218 |
if (resp == JOptionPane.NO_OPTION) { |
219 |
return;
|
220 |
} |
221 |
break;
|
222 |
case NO:
|
223 |
return;
|
224 |
case YES:
|
225 |
break;
|
226 |
} |
227 |
} |
228 |
try {
|
229 |
if( !target_image.getParentFile().exists() ) {
|
230 |
target_image.getParentFile().mkdirs(); |
231 |
} |
232 |
copyFile(source_image, target_image); |
233 |
copyFile(source_image_sel, target_image_sel); |
234 |
symbol = this.getSymbologyManager().createPictureMarkerSymbol(
|
235 |
target_image.toURI().toURL(), |
236 |
target_image_sel.toURI().toURL()); |
237 |
symbol.setSize(this.getSymbolSize());
|
238 |
symbol.setDescription(FilenameUtils.getBaseName(target_image.getName())); |
239 |
|
240 |
PersistentState state = persistenceManager.getState(symbol); |
241 |
FileOutputStream out = new FileOutputStream(target_symbol); |
242 |
persistenceManager.saveState(state, out); |
243 |
} catch (Exception e) { |
244 |
logger.info("Can't create symbol from '" + item.getAbsolutePath()
|
245 |
+ ".", e);
|
246 |
this.notCreatedSymbols.add(item);
|
247 |
} |
248 |
} |
249 |
|
250 |
private void postPrecess() { |
251 |
if (!this.isQuietMode()) { |
252 |
if (this.notCreatedSymbols.size() > 0) { |
253 |
StringBuffer listOfFiles = new StringBuffer(); |
254 |
listOfFiles.append("\n\n");
|
255 |
for (File file : this.notCreatedSymbols) { |
256 |
listOfFiles.append(file.getAbsolutePath()); |
257 |
listOfFiles.append("\n");
|
258 |
} |
259 |
application.message( |
260 |
application.translate("_many_symbols_can_be_createds")
|
261 |
+ listOfFiles.toString(), |
262 |
JOptionPane.WARNING_MESSAGE);
|
263 |
} |
264 |
} |
265 |
} |
266 |
|
267 |
private void copyFile(File source, File target) throws IOException { |
268 |
if (target.exists()) {
|
269 |
switch (this.getOverwrite()) { |
270 |
case ASK:
|
271 |
int resp = application
|
272 |
.confirmDialog(application.translate( |
273 |
"_File_{0}_already_exists_Overwrite",
|
274 |
target.getAbsolutePath()), this.getName(),
|
275 |
JOptionPane.YES_NO_OPTION,
|
276 |
JOptionPane.QUESTION_MESSAGE);
|
277 |
if (resp == JOptionPane.YES_OPTION) { |
278 |
FileUtils.copyFile(source, target); |
279 |
} |
280 |
break;
|
281 |
case NO:
|
282 |
break;
|
283 |
case YES:
|
284 |
FileUtils.copyFile(source, target); |
285 |
break;
|
286 |
} |
287 |
} else {
|
288 |
FileUtils.copyFile(source, target); |
289 |
} |
290 |
|
291 |
} |
292 |
|
293 |
private File getTargetSymbol(File item) { |
294 |
File target = FileUtils.getFile(getSymbolsRepository()
|
295 |
.asFile(), this.getTargetFolderName(),
|
296 |
FilenameUtils.getBaseName(item.getName()) |
297 |
+ this.getSymbolManager().getSymbolPreferences()
|
298 |
.getSymbolFileExtension()); |
299 |
return target;
|
300 |
} |
301 |
|
302 |
private File getSourceImage(File item) { |
303 |
return item;
|
304 |
} |
305 |
|
306 |
private File getSourceImageSelected(File item) { |
307 |
String ext = FilenameUtils.getExtension(item.getName());
|
308 |
String name = FilenameUtils.getBaseName(item.getName());
|
309 |
File source = new File(item.getParentFile(), name + "_sel." + ext); |
310 |
return source;
|
311 |
} |
312 |
|
313 |
private File getTargetImage(File item) { |
314 |
File target = FileUtils.getFile(getSymbolsRepository()
|
315 |
.asFile(), this.getTargetFolderName(), item.getName());
|
316 |
return target;
|
317 |
} |
318 |
|
319 |
private File getTargetImageSelected(File item) { |
320 |
String ext = FilenameUtils.getExtension(item.getName());
|
321 |
String name = FilenameUtils.getBaseName(item.getName());
|
322 |
File target = FileUtils.getFile(getSymbolsRepository()
|
323 |
.asFile(), this.getTargetFolderName(), name + "_sel." + ext); |
324 |
return target;
|
325 |
} |
326 |
|
327 |
private SymbolManager getSymbolManager() {
|
328 |
if (symbolManager == null) { |
329 |
symbolManager = MapContextLocator.getSymbolManager(); |
330 |
} |
331 |
return symbolManager;
|
332 |
} |
333 |
|
334 |
private SymbologyManager getSymbologyManager() {
|
335 |
if (symbologyManager == null) { |
336 |
symbologyManager = SymbologyLocator.getSymbologyManager(); |
337 |
} |
338 |
return symbologyManager;
|
339 |
} |
340 |
|
341 |
} |