Revision 43820 trunk/org.gvsig.desktop/org.gvsig.desktop.plugin/org.gvsig.app/org.gvsig.app.mainplugin/src/main/java/org/gvsig/app/extension/ProjectExtension.java
ProjectExtension.java | ||
---|---|---|
37 | 37 |
import java.util.Set; |
38 | 38 |
import java.util.prefs.Preferences; |
39 | 39 |
import java.util.zip.ZipEntry; |
40 |
import java.util.zip.ZipException; |
|
41 |
import java.util.zip.ZipFile; |
|
40 | 42 |
import java.util.zip.ZipOutputStream; |
41 | 43 |
import javax.imageio.ImageIO; |
42 | 44 |
|
... | ... | |
446 | 448 |
return writeProject(file, p, true); |
447 | 449 |
} |
448 | 450 |
|
449 |
/** |
|
450 |
* Escribe el proyecto en XML. Pero permite decidir si se pide confirmaci?n |
|
451 |
* para sobreescribir |
|
452 |
* |
|
453 |
* @param file |
|
454 |
* Fichero. |
|
455 |
* @param p |
|
456 |
* Proyecto. |
|
457 |
* @param askConfirmation |
|
458 |
* boolean |
|
459 |
*/ |
|
460 |
public boolean writeProject(File file, Project p, boolean askConfirmation) { |
|
461 |
if (askConfirmation && file.exists()) { |
|
462 |
int resp = JOptionPane.showConfirmDialog((Component) PluginServices |
|
463 |
.getMainFrame(), PluginServices.getText(this, |
|
464 |
"fichero_ya_existe_seguro_desea_guardarlo"), PluginServices |
|
465 |
.getText(this, "guardar"), JOptionPane.YES_NO_OPTION); |
|
466 |
if (resp != JOptionPane.YES_OPTION) { |
|
467 |
return false; |
|
468 |
} |
|
469 |
} |
|
470 |
NotificationManager.addInfo(PluginServices.getText(this, |
|
471 |
"writing_project") + ": " + file.getName()); |
|
451 |
/** |
|
452 |
* Escribe el proyecto en disco. |
|
453 |
* Pero permite decidir si se pide confirmaci?n para sobreescribir |
|
454 |
* |
|
455 |
* @param file Fichero. |
|
456 |
* @param p Proyecto. |
|
457 |
* @param askConfirmation boolean |
|
458 |
* @return |
|
459 |
*/ |
|
460 |
public boolean writeProject(File file, Project p, boolean askConfirmation) { |
|
461 |
I18nManager i18n = ToolsLocator.getI18nManager(); |
|
462 |
ApplicationManager application = ApplicationLocator.getManager(); |
|
463 |
if (askConfirmation && file.exists()) { |
|
464 |
int resp = application.confirmDialog( |
|
465 |
i18n.getTranslation("fichero_ya_existe_seguro_desea_guardarlo"), |
|
466 |
i18n.getTranslation("guardar"), |
|
467 |
JOptionPane.YES_NO_OPTION, |
|
468 |
JOptionPane.QUESTION_MESSAGE, |
|
469 |
"Overwrite_project_file" |
|
470 |
); |
|
471 |
if (resp != JOptionPane.YES_OPTION) { |
|
472 |
return false; |
|
473 |
} |
|
474 |
} |
|
475 |
FileOutputStream fout=null; |
|
476 |
ZipOutputStream zout=null; |
|
477 |
LOG.info("Writing project '"+ file.getAbsolutePath()+"'."); |
|
478 |
try { |
|
479 |
fireBeforeSavingFileEvent(new SaveEvent(this,SaveEvent.BEFORE_SAVING, file)); |
|
472 | 480 |
|
473 |
// write it out as XML |
|
474 |
try { |
|
475 |
fireBeforeSavingFileEvent(new SaveEvent(this, |
|
476 |
SaveEvent.BEFORE_SAVING, file)); |
|
477 |
|
|
478 |
FileOutputStream fout = new FileOutputStream(file); |
|
479 |
ZipOutputStream zout = new ZipOutputStream(fout); |
|
481 |
fout = new FileOutputStream(file); |
|
482 |
zout = new ZipOutputStream(fout); |
|
480 | 483 |
p.saveState(zout); |
481 |
|
|
484 |
|
|
482 | 485 |
zout.putNextEntry(new ZipEntry("preview.jpg")); |
483 | 486 |
BufferedImage img = ApplicationLocator.getManager().getUIManager().getImagePreview(); |
484 | 487 |
img = scale(img, 0.40); |
485 | 488 |
try { |
486 | 489 |
ImageIO.write(img, "jpg", zout); |
487 | 490 |
} catch (IOException ex) { |
488 |
LOG.warn("Can't save preview image'.",ex); |
|
491 |
LOG.warn("Can't save preview image'.", ex);
|
|
489 | 492 |
} |
490 |
|
|
493 |
fireAfterSavingFileEvent(new SaveEvent(this, SaveEvent.AFTER_SAVING, file)); |
|
491 | 494 |
IOUtils.closeQuietly(zout); |
492 | 495 |
IOUtils.closeQuietly(fout); |
496 |
|
|
497 |
if( !isValidZIP(file) ) { |
|
498 |
throw new ZipException("Invalid project file '"+file.getAbsolutePath()+"'"); |
|
499 |
} |
|
493 | 500 |
|
494 |
fireAfterSavingFileEvent(new SaveEvent(this,
|
|
495 |
SaveEvent.AFTER_SAVING, file));
|
|
501 |
PluginServices.getMainFrame().setTitle(file.getName());
|
|
502 |
setPath(file.toString());
|
|
496 | 503 |
|
497 |
PluginServices.getMainFrame().setTitle(file.getName()); |
|
498 |
setPath(file.toString()); |
|
504 |
} catch (Exception e) { |
|
505 |
application.messageDialog( |
|
506 |
i18n.getTranslation("_Problems_saving_the_project_XnlX_It_is_possible_that_this_was_not_saved_properly_and_can_not_be_loaded_again"), |
|
507 |
null, |
|
508 |
i18n.getTranslation("guardar"), |
|
509 |
JOptionPane.ERROR_MESSAGE, |
|
510 |
"Problems_saving_the_project" |
|
511 |
); |
|
512 |
LOG.warn("Error writing project '"+file.getAbsolutePath()+"'.", e); |
|
513 |
return false; |
|
514 |
} finally { |
|
515 |
IOUtils.closeQuietly(zout); |
|
516 |
IOUtils.closeQuietly(fout); |
|
517 |
} |
|
518 |
|
|
519 |
LOG.warn("Wrote project '"+file.getAbsolutePath()+"'."); |
|
520 |
return true; |
|
521 |
} |
|
499 | 522 |
|
500 |
} catch (PersistenceException e) { |
|
501 |
String messagestack = e.getLocalizedMessageStack(); |
|
502 |
NotificationManager.addError( |
|
503 |
PluginServices.getText(this, "error_writing_project") |
|
504 |
+ ": " + file.getName() + "\n" + messagestack, e); |
|
505 |
return false; |
|
506 |
} catch (Exception e) { |
|
507 |
NotificationManager.addError( |
|
508 |
PluginServices.getText(this, "error_writing_project") |
|
509 |
+ ": " + file.getName(), e); |
|
510 |
return false; |
|
511 |
} |
|
512 |
NotificationManager.addInfo(PluginServices.getText(this, |
|
513 |
"wrote_project") + ": " + file.getName()); |
|
514 |
return true; |
|
515 |
} |
|
523 |
boolean isValidZIP(final File file) { |
|
524 |
ZipFile zipfile = null; |
|
525 |
try { |
|
526 |
zipfile = new ZipFile(file); |
|
527 |
return true; |
|
528 |
} catch (IOException e) { |
|
529 |
return false; |
|
530 |
} finally { |
|
531 |
try { |
|
532 |
if (zipfile != null) { |
|
533 |
zipfile.close(); |
|
534 |
zipfile = null; |
|
535 |
} |
|
536 |
} catch (IOException e) { |
|
537 |
} |
|
538 |
} |
|
539 |
} |
|
516 | 540 |
|
517 | 541 |
private BufferedImage scale(BufferedImage before, double factor) { |
518 | 542 |
int w = (int) (before.getWidth()*factor); |
Also available in: Unified diff