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