svn-gvsig-desktop / trunk / extensions / extGPE-gvSIG / src / org / gvsig / gpe / gui / dialogs / SelectVersionListener.java @ 27067
History | View | Annotate | Download (12.3 KB)
1 |
package org.gvsig.gpe.gui.dialogs; |
---|---|
2 |
|
3 |
import java.awt.Component; |
4 |
import java.awt.event.ActionEvent; |
5 |
import java.awt.event.ActionListener; |
6 |
import java.awt.event.ItemEvent; |
7 |
import java.awt.event.ItemListener; |
8 |
import java.io.File; |
9 |
import java.io.FileNotFoundException; |
10 |
import java.io.FileOutputStream; |
11 |
import java.io.IOException; |
12 |
import java.util.ArrayList; |
13 |
|
14 |
import javax.swing.JFileChooser; |
15 |
import javax.swing.JOptionPane; |
16 |
import javax.swing.filechooser.FileFilter; |
17 |
|
18 |
import org.gvsig.fmap.drivers.gpe.addlayer.GPEWriterFileFilter; |
19 |
import org.gvsig.fmap.drivers.gpe.addlayer.XMLSchemaFileFilter; |
20 |
import org.gvsig.fmap.drivers.gpe.handlers.FmapErrorHandler; |
21 |
import org.gvsig.fmap.drivers.gpe.writer.ExportTask; |
22 |
import org.gvsig.fmap.drivers.gpe.writer.schema.GMLSchemaCreator; |
23 |
import org.gvsig.gpe.GPERegister; |
24 |
import org.gvsig.gpe.exceptions.WriterHandlerCreationException; |
25 |
import org.gvsig.gpe.writer.GPEWriterHandler; |
26 |
|
27 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
28 |
import com.iver.andami.PluginServices; |
29 |
import com.iver.andami.messages.NotificationManager; |
30 |
import com.iver.andami.ui.mdiManager.IWindow; |
31 |
import com.iver.cit.gvsig.fmap.MapControl; |
32 |
import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
33 |
import com.iver.cit.gvsig.fmap.drivers.SHPLayerDefinition; |
34 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
35 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
36 |
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource; |
37 |
import com.iver.cit.gvsig.project.documents.view.gui.IView; |
38 |
import com.iver.utiles.FileUtils; |
39 |
import com.iver.utiles.Utils; |
40 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
41 |
*
|
42 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
43 |
*
|
44 |
* This program is free software; you can redistribute it and/or
|
45 |
* modify it under the terms of the GNU General Public License
|
46 |
* as published by the Free Software Foundation; either version 2
|
47 |
* of the License, or (at your option) any later version.
|
48 |
*
|
49 |
* This program is distributed in the hope that it will be useful,
|
50 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
51 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
52 |
* GNU General Public License for more details.
|
53 |
*
|
54 |
* You should have received a copy of the GNU General Public License
|
55 |
* along with this program; if not, write to the Free Software
|
56 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
57 |
*
|
58 |
* For more information, contact:
|
59 |
*
|
60 |
* Generalitat Valenciana
|
61 |
* Conselleria d'Infraestructures i Transport
|
62 |
* Av. Blasco Ib??ez, 50
|
63 |
* 46010 VALENCIA
|
64 |
* SPAIN
|
65 |
*
|
66 |
* +34 963862235
|
67 |
* gvsig@gva.es
|
68 |
* www.gvsig.gva.es
|
69 |
*
|
70 |
* or
|
71 |
*
|
72 |
* IVER T.I. S.A
|
73 |
* Salamanca 50
|
74 |
* 46005 Valencia
|
75 |
* Spain
|
76 |
*
|
77 |
* +34 963163400
|
78 |
* dac@iver.es
|
79 |
*/
|
80 |
/* CVS MESSAGES:
|
81 |
*
|
82 |
* $Id$
|
83 |
* $Log$
|
84 |
*
|
85 |
*/
|
86 |
/**
|
87 |
* @author Jorge Piera LLodr? (jorge.piera@iver.es)
|
88 |
*/
|
89 |
public class SelectVersionListener implements ActionListener, ItemListener{ |
90 |
public static final String CANCEL_BUTTON = "cancel"; |
91 |
public static final String EXPORT_BUTTON = "export"; |
92 |
public static final String WRITER_COMBO = "combo"; |
93 |
public static final String FILE_BUTTON = "file"; |
94 |
public static final String SCHEMA_BUTTON = "schema"; |
95 |
private SelectVersionWindow window = null; |
96 |
private static String lastPath = null; |
97 |
|
98 |
public SelectVersionListener(SelectVersionWindow window) {
|
99 |
super();
|
100 |
this.window = window;
|
101 |
writerComboSelectionChange(); |
102 |
if(lastPath != null){ |
103 |
window.setFile(lastPath); |
104 |
} |
105 |
} |
106 |
|
107 |
/*
|
108 |
* (non-Javadoc)
|
109 |
* @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
|
110 |
*/
|
111 |
public void actionPerformed(ActionEvent e) { |
112 |
if(e.getActionCommand().compareTo(CANCEL_BUTTON) == 0){ |
113 |
cancelButtonActionPerformed(); |
114 |
}else if (e.getActionCommand().compareTo(EXPORT_BUTTON) == 0){ |
115 |
try {
|
116 |
exportButtonActionPerformed(); |
117 |
} catch (WriterHandlerCreationException e1) {
|
118 |
NotificationManager.addError(e1); |
119 |
} catch (FileNotFoundException e1){ |
120 |
NotificationManager.addError(e1); |
121 |
} |
122 |
}else if(e.getActionCommand().compareTo(WRITER_COMBO) == 0){ |
123 |
writerComboSelectionChange(); |
124 |
}else if(e.getActionCommand().compareTo(FILE_BUTTON) == 0){ |
125 |
fileButtonActionPerformed(); |
126 |
}else if(e.getActionCommand().compareTo(SCHEMA_BUTTON) == 0){ |
127 |
schemaButtonActionPerformed(); |
128 |
} |
129 |
} |
130 |
|
131 |
/*
|
132 |
* (non-Javadoc)
|
133 |
* @see java.awt.event.ItemListener#itemStateChanged(java.awt.event.ItemEvent)
|
134 |
*/
|
135 |
public void itemStateChanged(ItemEvent e) { |
136 |
if (window.isXMLSchemaCreated()){
|
137 |
addDefaultSchema(); |
138 |
} |
139 |
} |
140 |
|
141 |
/**
|
142 |
* Create and add a default XML schema
|
143 |
*/
|
144 |
private void addDefaultSchema(){ |
145 |
String sFile = window.getSelectedFile();
|
146 |
if (sFile.length() > 0){ |
147 |
File file = new File(sFile); |
148 |
String extension = FileUtils.getFileExtension(file);
|
149 |
sFile = sFile.substring(0, sFile.length() - extension.length());
|
150 |
sFile = sFile + "xsd";
|
151 |
window.setXMLSchema(sFile); |
152 |
}else{
|
153 |
window.setXMLSchema("default.xsd");
|
154 |
} |
155 |
} |
156 |
|
157 |
/**
|
158 |
* @return a file filter for the selected driver
|
159 |
*/
|
160 |
private FileFilter getFileFilter(){ |
161 |
GPEWriterHandler writer = window.getSelectedWriter(); |
162 |
FileFilter filter = new GPEWriterFileFilter(writer); |
163 |
return filter;
|
164 |
} |
165 |
|
166 |
/**
|
167 |
* When the cancel button is clicked
|
168 |
*/
|
169 |
private boolean fileButtonActionPerformed(){ |
170 |
File file = getFile(lastPath, getFileFilter());
|
171 |
if (file == null){ |
172 |
return false; |
173 |
} |
174 |
String sFile = null; |
175 |
String extension = Utils.getExtension(file);
|
176 |
if (extension == null){ |
177 |
sFile = file.getPath() + "." + window.getSelectedFormat().toLowerCase();
|
178 |
}else{
|
179 |
sFile = file.getPath(); |
180 |
} |
181 |
window.setFile(sFile); |
182 |
return true; |
183 |
} |
184 |
|
185 |
/**
|
186 |
* When the schema button is clicked
|
187 |
*/
|
188 |
private boolean schemaButtonActionPerformed(){ |
189 |
File file = getFile(lastPath, new XMLSchemaFileFilter()); |
190 |
if (file == null){ |
191 |
return false; |
192 |
} |
193 |
String sFile = null; |
194 |
String extension = Utils.getExtension(file);
|
195 |
if (extension == null){ |
196 |
sFile = file.getPath() + "." + "xsd"; |
197 |
}else{
|
198 |
sFile = file.getPath(); |
199 |
} |
200 |
window.setXMLSchema(sFile); |
201 |
return true; |
202 |
} |
203 |
|
204 |
/**
|
205 |
*
|
206 |
* @param lastPath_
|
207 |
* @return
|
208 |
*/
|
209 |
private File getFile(String lastPath, FileFilter fileFilter){ |
210 |
JFileChooser jfc = new JFileChooser(lastPath); |
211 |
jfc.removeChoosableFileFilter(jfc.getAcceptAllFileFilter()); |
212 |
jfc.addChoosableFileFilter(fileFilter); |
213 |
jfc.showSaveDialog((Component) PluginServices.getMainFrame());
|
214 |
File f = jfc.getSelectedFile();
|
215 |
if (f == null){ |
216 |
return null; |
217 |
} |
218 |
return f;
|
219 |
} |
220 |
|
221 |
/**
|
222 |
* When the cancel button is clicked
|
223 |
*/
|
224 |
private void cancelButtonActionPerformed(){ |
225 |
PluginServices.getMDIManager().closeWindow(window); |
226 |
} |
227 |
|
228 |
/**
|
229 |
* When the export button is clicked
|
230 |
* @throws FileNotFoundException
|
231 |
* @throws GPEWriterHandlerCreationException
|
232 |
*/
|
233 |
private void exportButtonActionPerformed() throws WriterHandlerCreationException, FileNotFoundException{ |
234 |
if (!isExportable(window.getSelectedFile(), window.getSelectedXMLSchema())){
|
235 |
return;
|
236 |
} |
237 |
//Save the files that has been exported
|
238 |
ArrayList exportedFiles = new ArrayList(); |
239 |
//Closes the window
|
240 |
cancelButtonActionPerformed(); |
241 |
//Prepare the writerHandler to write
|
242 |
GPEWriterHandler writer = cloneWriterHandler(); |
243 |
FLayer[] actives = getActives();
|
244 |
if (actives.length > 0){ |
245 |
lastPath = window.getSelectedFile(); |
246 |
exportedFiles.add(lastPath); |
247 |
//It generates the XML schema
|
248 |
if (window.isXMLSchemaCreated()){
|
249 |
createXMLSchema(actives[0]);
|
250 |
} |
251 |
//Export the first selected layer
|
252 |
exportLayer(writer,actives[0]);
|
253 |
//Export the other layers
|
254 |
for (int i=1 ; i<actives.length ; i++){ |
255 |
//Gets a new file...
|
256 |
boolean isSelected = isNextFileSelected();
|
257 |
if (!isSelected){
|
258 |
continue;
|
259 |
} |
260 |
|
261 |
while (!isExportable(window.getSelectedFile(),window.getSelectedXMLSchema())
|
262 |
&& (isSelected)){ |
263 |
isSelected = isNextFileSelected(); |
264 |
} |
265 |
if (!isSelected){
|
266 |
continue;
|
267 |
} |
268 |
lastPath = window.getSelectedFile(); |
269 |
exportedFiles.add(lastPath); |
270 |
writer = cloneWriterHandler(); |
271 |
//It generates the XML schema
|
272 |
if (window.isXMLSchemaCreated()){
|
273 |
createXMLSchema(actives[i]); |
274 |
} |
275 |
exportLayer(writer,actives[i]); |
276 |
} |
277 |
} |
278 |
} |
279 |
|
280 |
/**
|
281 |
* Return true if the outputFile and the output schema
|
282 |
* are exportable
|
283 |
* @param outputFile
|
284 |
* The output file
|
285 |
* @param outputSchema
|
286 |
* the output schema
|
287 |
* @return
|
288 |
* If it is exportable
|
289 |
*/
|
290 |
private boolean isExportable(String outputFile, String outputSchema){ |
291 |
//If the output file doesn't exist or the user
|
292 |
//wants to override it continue
|
293 |
if (!fileIsOk(window.getSelectedFile())){
|
294 |
return false; |
295 |
} |
296 |
//If the output schema doesn't exist or the user
|
297 |
//wants to override it continue
|
298 |
if (window.isXMLSchemaCreated()){
|
299 |
if (!fileIsOk(window.getSelectedXMLSchema())){
|
300 |
return false; |
301 |
} |
302 |
} |
303 |
return true; |
304 |
} |
305 |
|
306 |
/**
|
307 |
* Select a new output file for the
|
308 |
* output file and for the XML schema
|
309 |
* @return
|
310 |
* true if the layer can be exported
|
311 |
*/
|
312 |
private boolean isNextFileSelected(){ |
313 |
boolean selectedFile = fileButtonActionPerformed();
|
314 |
if (!selectedFile){
|
315 |
return false; |
316 |
} |
317 |
//Gets a new Schema
|
318 |
if (window.isXMLSchemaCreated()){
|
319 |
boolean selectedSchema = schemaButtonActionPerformed();
|
320 |
if (!selectedSchema){
|
321 |
return false; |
322 |
} |
323 |
} |
324 |
return true; |
325 |
} |
326 |
|
327 |
/**
|
328 |
* Creates an XML schema
|
329 |
* @param the layer to create the schema
|
330 |
*/
|
331 |
private void createXMLSchema(FLayer layer){ |
332 |
if (layer instanceof FLyrVect){ |
333 |
File schema = new File(window.getSelectedXMLSchema()); |
334 |
try {
|
335 |
GMLSchemaCreator schemaCreator = new GMLSchemaCreator(schema);
|
336 |
SHPLayerDefinition lyrDef = new SHPLayerDefinition();
|
337 |
SelectableDataSource sds = ((FLyrVect)layer).getRecordset(); |
338 |
FieldDescription[] fieldsDescrip = sds.getFieldsDescription();
|
339 |
lyrDef.setFieldsDesc(fieldsDescrip); |
340 |
lyrDef.setShapeType(((FLyrVect)layer).getShapeType()); |
341 |
schemaCreator.createFile(lyrDef); |
342 |
schemaCreator.writeFile(); |
343 |
} catch (IOException e) { |
344 |
NotificationManager.addError(e); |
345 |
} catch (ReadDriverException e) {
|
346 |
NotificationManager.addError(e); |
347 |
} |
348 |
} |
349 |
} |
350 |
|
351 |
/**
|
352 |
* Clones the selected writerHandler
|
353 |
* @param writer
|
354 |
* @return
|
355 |
* @throws FileNotFoundException
|
356 |
* @throws GPEWriterHandlerCreationException
|
357 |
*/
|
358 |
private GPEWriterHandler cloneWriterHandler() throws WriterHandlerCreationException, FileNotFoundException{ |
359 |
GPEWriterHandler writer = GPERegister.createWriter(window.getSelectedWriter().getName()); |
360 |
writer.setOutputStream(new FileOutputStream(window.getSelectedFile())); |
361 |
writer.setErrorHandler(new FmapErrorHandler());
|
362 |
return writer;
|
363 |
} |
364 |
|
365 |
/**
|
366 |
* @return if the file can be written
|
367 |
*/
|
368 |
private boolean fileIsOk(String sFile){ |
369 |
File file = new File(sFile); |
370 |
//If the file exists...
|
371 |
if (file.exists()){
|
372 |
int resp = JOptionPane.showConfirmDialog( |
373 |
(Component) PluginServices.getMainFrame(),
|
374 |
PluginServices.getText(this,
|
375 |
"fichero_ya_existe_seguro_desea_guardarlo")+
|
376 |
"\n"+
|
377 |
file.getAbsolutePath(), |
378 |
PluginServices.getText(this,"guardar"), JOptionPane.YES_NO_OPTION); |
379 |
if (resp != JOptionPane.YES_OPTION) { |
380 |
return false; |
381 |
} |
382 |
} |
383 |
return true; |
384 |
} |
385 |
|
386 |
/**
|
387 |
* Export one layer
|
388 |
* @param writer
|
389 |
* The writerHandler
|
390 |
* @param layer
|
391 |
* The layer to export
|
392 |
*/
|
393 |
private void exportLayer(GPEWriterHandler writer, FLayer layer){ |
394 |
ExportTask task = new ExportTask(layer,
|
395 |
writer, |
396 |
getMapControl().getMapContext(), |
397 |
new File(window.getSelectedFile())); |
398 |
PluginServices.cancelableBackgroundExecution(task); |
399 |
} |
400 |
|
401 |
/**
|
402 |
* @return the layer to export
|
403 |
*/
|
404 |
private FLayer[] getActives(){ |
405 |
IWindow window = PluginServices.getMDIManager().getActiveWindow(); |
406 |
if (window instanceof IView){ |
407 |
return ((IView)window).getMapControl().getMapContext().getLayers().getActives();
|
408 |
} |
409 |
return null; |
410 |
} |
411 |
|
412 |
/**
|
413 |
* @return the current mapContext
|
414 |
*/
|
415 |
private MapControl getMapControl(){
|
416 |
IWindow window = PluginServices.getMDIManager().getActiveWindow(); |
417 |
if (window instanceof IView){ |
418 |
return ((IView)window).getMapControl();
|
419 |
} |
420 |
return null; |
421 |
} |
422 |
|
423 |
/**
|
424 |
* When the writer combo selection changes
|
425 |
*/
|
426 |
private void writerComboSelectionChange(){ |
427 |
window.initializeSelection(); |
428 |
GPEWriterHandler writer = window.getSelectedWriter(); |
429 |
//String[] formats = writer.getFormat();
|
430 |
//for (int i=0 ; i<formats.length ; i++){
|
431 |
window.addFormat(writer.getFormat()); |
432 |
//}
|
433 |
window.setSelectedFormat(writer.getFormat()); |
434 |
// String[] versions = writer.getVersions();
|
435 |
// for (int i=0 ; i<versions.length ; i++){
|
436 |
// window.addVersion(versions[i]);
|
437 |
// }
|
438 |
// window.setSelectedVersion(writer.getDefaultVersion());
|
439 |
} |
440 |
|
441 |
} |