Revision 44088

View differences:

trunk/org.gvsig.desktop/org.gvsig.desktop.plugin/org.gvsig.app/org.gvsig.app.mainplugin/src/main/resources-plugin/i18n/text.properties
720 720
tipo_de_intervalo_XcolonX=Tipo de intervalo\:
721 721
tipo_de_leyenda=Tipo de leyenda
722 722
tipo_fichero_proyecto=Fichero de proyecto gvSIG (*.gvsproj)
723
tipo_fichero_proyecto_bak=Copia de seguridad de proyecto gvSIG (*.gvsproj.bak)
723 724
tipo_linea=Tipo de L\u00ednea\:
724 725
tipo_relleno=Tipo de Relleno\:
725 726
tipo_simbolo=Tipo de s\u00edmbolo\:
trunk/org.gvsig.desktop/org.gvsig.desktop.plugin/org.gvsig.app/org.gvsig.app.mainplugin/src/main/resources-plugin/i18n/text_en.properties
679 679
tipo_de_intervalo_XcolonX=Interval type\:
680 680
tipo_de_leyenda=Type of legend
681 681
tipo_fichero_proyecto=gvSIG project file(*.gvsproj)
682
tipo_fichero_proyecto_bak=gvSIG project security file copy (*.gvsproj.bak)
682 683
tipo_linea=Stroke\:
683 684
tipo_relleno=Fill\:
684 685
tipo_simbolo=Type of symbol\:
trunk/org.gvsig.desktop/org.gvsig.desktop.plugin/org.gvsig.app/org.gvsig.app.mainplugin/src/main/java/org/gvsig/app/project/Project.java
47 47
    public static final String PROJECT_PROPERTIES_PAGE_GROUP = "Proyect";
48 48

  
49 49
    public static final String FILE_EXTENSION = ".gvsproj";
50
    
51
    public static final String FILE_BAK = FILE_EXTENSION + ".bak";
50 52

  
51 53
    public void addPropertyChangeListener(PropertyChangeListener listener);
52 54

  
trunk/org.gvsig.desktop/org.gvsig.desktop.plugin/org.gvsig.app/org.gvsig.app.mainplugin/src/main/java/org/gvsig/app/project/DefaultProject.java
40 40
import java.io.Serializable;
41 41
import java.text.DateFormat;
42 42
import java.text.MessageFormat;
43
import java.text.SimpleDateFormat;
43 44
import java.util.ArrayList;
44 45
import java.util.Collections;
45 46
import java.util.Date;
......
55 56
import java.util.zip.ZipFile;
56 57
import java.util.zip.ZipOutputStream;
57 58
import javax.imageio.ImageIO;
59
import org.apache.commons.io.FileUtils;
60
import org.apache.commons.io.FilenameUtils;
58 61
import org.apache.commons.io.IOUtils;
59 62

  
60 63
import org.cresques.cts.IProjection;
......
186 189
        return preferences;
187 190
    }
188 191

  
192
    @Override
189 193
    public void propertyChange(PropertyChangeEvent evt) {
190 194
        change.firePropertyChange(evt);
191 195
    }
......
206 210
     *
207 211
     * @return
208 212
     */
213
    @Override
209 214
    public String getCreationDate() {
210 215
        return creationDate;
211 216
    }
......
215 220
        change.firePropertyChange("setCreationDate", null, null);
216 221
    }
217 222

  
223
    @Override
218 224
    public Document createDocument(String type) {
219 225
        logger.info("createDocument('{}')", type);
220 226
        return ProjectManager.getInstance().createDocument(type);
......
225 231
     *
226 232
     * @return
227 233
     */
234
    @Override
228 235
    public String getName() {
229 236
        return name;
230 237
    }
......
234 241
     *
235 242
     * @param string
236 243
     */
244
    @Override
237 245
    public void setName(String name) {
238 246
        this.name = name;
239 247
        change.firePropertyChange("setName", null, null);
......
244 252
     *
245 253
     * @return comments
246 254
     */
255
    @Override
247 256
    public String getComments() {
248 257
        return comments;
249 258
    }
......
253 262
     *
254 263
     * @param comments as string
255 264
     */
265
    @Override
256 266
    public void setComments(String string) {
257 267
        comments = string;
258 268
        change.firePropertyChange("setComments", null, null);
......
263 273
     *
264 274
     * @return modification date as string
265 275
     */
276
    @Override
266 277
    public String getModificationDate() {
267 278
        return modificationDate;
268 279
    }
......
277 288
     *
278 289
     * @return author as string
279 290
     */
291
    @Override
280 292
    public String getOwner() {
281 293
        return owner;
282 294
    }
......
286 298
     *
287 299
     * @param author name as string
288 300
     */
301
    @Override
289 302
    public void setOwner(String owner) {
290 303
        this.owner = owner;
291 304
        change.firePropertyChange("setOwner", null, null);
......
296 309
     *
297 310
     * @return
298 311
     */
312
    @Override
299 313
    public Color getSelectionColor() {
300 314
        if (selectionColor == null) {
301 315
            selectionColor = getPreferences().getDefaultSelectionColor();
......
308 322
     *
309 323
     * @param selection color as string
310 324
     */
325
    @Override
311 326
    public void setSelectionColor(String selectionColor) {
312 327
        this.setSelectionColor(StringUtilities.string2Color(selectionColor));
313 328
    }
......
317 332
     *
318 333
     * @param selection color as Color
319 334
     */
335
    @Override
320 336
    public void setSelectionColor(Color selectionColor) {
321 337
        this.selectionColor = selectionColor;
322 338
        MapContext.setSelectionColor(selectionColor);
323 339
        change.firePropertyChange("selectionColor", null, selectionColor);
324 340
    }
325 341

  
342
    @Override
326 343
    public IProjection getProjection() {
327 344
        if (projection == null) {
328 345
            projection = getPreferences().getDefaultProjection();
......
330 347
        return projection;
331 348
    }
332 349

  
350
    @Override
333 351
    public void setProjection(IProjection projection) {
334 352
        this.projection = projection;
335 353
    }
......
341 359
     *
342 360
     * @param modified as boolean
343 361
     */
362
    @Override
344 363
    public void setModified(boolean modified) {
345 364
        this.modified = modified;
346 365
        if (modified == false) {
......
351 370
        }
352 371
    }
353 372

  
373
    @Override
354 374
    public boolean hasChanged() {
355 375
		// we return true if the project is not empty (until we have a better
356 376
        // method...)
......
365 385
     *
366 386
     * @return documents as List of IProjectDocument
367 387
     */
388
    @Override
368 389
    public List<Document> getDocuments() {
369 390
        return Collections.unmodifiableList(documents);
370 391
    }
......
376 397
     *
377 398
     * @return List of IProjectDocument
378 399
     */
400
    @Override
379 401
    public List<Document> getDocuments(String type) {
380 402
        List<Document> docs = new ArrayList<Document>();
381 403
        if (type != null) {
......
393 415
     *
394 416
     * @param document as Document
395 417
     */
418
    @Override
396 419
    public void add(Document document) {
397 420
        this.addDocument(document);
398 421
    }
399 422

  
423
    @Override
400 424
    public void addDocument(Document document) {
401 425
        logger.info("add('{}')", document.toString());
402 426

  
......
414 438
        change.firePropertyChange("addDocument", null, document);
415 439
    }
416 440

  
441
    @Override
417 442
    public void remove(Document doc) {
418 443
        this.removeDocument(doc);
419 444
    }
420 445

  
446
    @Override
421 447
    public void removeDocument(Document doc) {
422 448
        logger.info("remove('{}')", doc.toString());
423 449
        if (notifyObservers(ProjectNotification.BEFORE_REMOVE_DOCUMENT, doc).isProcessCanceled()) {
......
430 456
        notifyObservers(ProjectNotification.AFTER_REMOVE_DOCUMENT, doc);
431 457
    }
432 458

  
459
    @Override
433 460
    public Iterator<Document> iterator() {
434 461
        return documents.iterator();
435 462
    }
436 463

  
464
    @Override
437 465
    public boolean isEmpty() {
438 466
        return documents.isEmpty();
439 467
    }
......
448 476
     * @throws RuntimeException Si la capa que se pasa como par�metro no se
449 477
     * encuentra en ninguna vista
450 478
     */
479
    @Override
451 480
    public String getViewName(FLayer layer) {
452 481
        List<Document> views = getDocuments(ViewManager.TYPENAME);
453 482
        for (int v = 0; v < views.size(); v++) {
......
476 505
        return false;
477 506
    }
478 507

  
508
    @Override
479 509
    public void addExtent(ProjectExtent arg1) {
480 510
        extents.add(arg1);
481 511
        change.firePropertyChange("addExtent", null, null);
482 512
    }
483 513

  
514
    @Override
484 515
    public ProjectExtent removeExtent(int arg0) {
485 516
        change.firePropertyChange("delExtent", null, null);
486 517
        return extents.remove(arg0);
487 518
    }
488 519

  
520
    @Override
489 521
    public ProjectExtent[] getExtents() {
490 522
        return (ProjectExtent[]) extents.toArray(new ProjectExtent[0]);
491 523
    }
......
500 532
     *
501 533
     * @return Documento
502 534
     */
535
    @Override
503 536
    public Document getDocument(String name, String type) {
504 537
        if (type == null) {
505 538
            for (int i = 0; i < documents.size(); i++) {
......
520 553
        return null;
521 554
    }
522 555

  
556
    @Override
523 557
    public Document getDocument(String name) {
524 558
        return this.getDocument(name, null);
525 559
    }
......
556 590
        }
557 591
    }
558 592

  
593
    @Override
559 594
    public void saveState(File out) throws PersistenceException {
560 595
        FileOutputStream fout;
561 596
        if (notifyObservers(ProjectNotification.BEFORE_SAVE_TO_FILE, out).isProcessCanceled()) {
......
565 600
            fout = new FileOutputStream(out);
566 601
            saveState(fout, new File(out.getParent()));
567 602
        } catch (FileNotFoundException e) {
603
            createProjectCopy(true);
568 604
            throw new PersistenceException(e);
569 605
        }
570 606
        if( !isValidZIP(out) ) {
571 607
            throw new PersistenceException(new ZipException());
572 608
        }
573 609
        this.fname = out;
610
        //sacar copia al bak
611
        createProjectCopy(false);
574 612
        notifyObservers(ProjectNotification.AFTER_SAVE_TO_FILE, out);
575 613
    }
576 614

  
......
580 618
            zipfile = new ZipFile(file);
581 619
            return true;
582 620
        } catch (IOException e) {
621
            //sacar copia. 
583 622
            return false;
584 623
        } finally {
585 624
            try {
......
625 664
                throw new ZipException("Invalid project file '"+file.getAbsolutePath()+"'");
626 665
            }
627 666
            this.fname = file;
667
            if (file.exists()) {
668
                createProjectCopy(false);
669
            }
628 670
        } catch (Exception ex) {
629
            throw new RuntimeException("Can't write project in '"+file.getAbsolutePath()+".",ex);
671
            createProjectCopy(true);
672
            throw new RuntimeException("Can't write project in '" + file.getAbsolutePath() + ".", ex);
630 673
        } finally {
631 674
            IOUtils.closeQuietly(zout);
632
            IOUtils.closeQuietly(fout);            
675
            IOUtils.closeQuietly(fout);
676

  
633 677
        }
634 678
    }
635 679
    
......
651 695
        }
652 696
        manager.saveState(state, out, true);
653 697
        if (state.getContext().getErrors() != null) {
698
            createProjectCopy(true);
654 699
            throw state.getContext().getErrors();
655 700
        }
656 701
        this.fname = null;
657 702
        notifyObservers(ProjectNotification.AFTER_SAVE_TO_STREAM, rootFolder);
658 703
    }
704
    
705
    private File getNameFileForProject(File actualProjectFile, boolean error) {
706
        // if there is a error, creates a file with a unique timecode
707
        // not error: return path with .gvsproj.bak extension that could exists
708
               
709
        if (actualProjectFile==null) {
710
            LOG.info("Doesn't have previous project to create a bak");
711
            return null;
712
        }
713
        String noExt = FilenameUtils.removeExtension(actualProjectFile.getAbsolutePath());
714
        String timeCode = new SimpleDateFormat("_MMddHHmmss").format(new Date());
715
        String ext = ".gvsproj.bak";
659 716

  
717
        int code = 0;
718
        if (noExt.length() > 230) {
719
            noExt = noExt.substring(0, 229);
720
        }
721
        
722
        File bakProjectFile;
723
        if (error) {
724
            bakProjectFile = new File(noExt + timeCode + ext);
725
            while (bakProjectFile.exists()) {
726
                code = code + 1;
727
                bakProjectFile = new File(noExt + timeCode + "_" + code + ext);
728
            }
729
        } else {
730
            bakProjectFile = new File(noExt + ext);
731
        }
732
        return bakProjectFile;
733
    }
734
    public File createProjectCopy(boolean error) {
735
        File actualProjectFile = ApplicationLocator.getProjectManager().getCurrentProject().getFile();
736
        File bakProjectFile = getNameFileForProject(actualProjectFile, error);
737
        if (bakProjectFile==null){
738
            return null;
739
        }
740
        if (!error){
741
            try {
742
                if (bakProjectFile.exists()){
743
                    FileUtils.deleteQuietly(bakProjectFile);
744
                }
745
            } catch (Exception ex) {
746
                LOG.error("Not possible to delete file", ex);
747
            }
748
            try {
749
                FileUtils.copyFile(actualProjectFile, bakProjectFile);
750
            } catch (IOException ex) {
751
                LOG.error("Can't create bak copy from project", ex);
752
            }
753
        } else {
754
            File bakProjectFile2 = getNameFileForProject(actualProjectFile, false);
755
            try {
756
                FileUtils.moveFile(bakProjectFile2, bakProjectFile);
757
            } catch (IOException ex) {
758
                LOG.error("Can't rename file", ex);
759
            }
760
        }
761
        
762
        return bakProjectFile;
763
    }
764

  
660 765
    @Deprecated
766
    @Override
661 767
    public void loadState(InputStream in) {
662 768
        loadState(in, null);
663 769
    }
......
744 850
        } catch (FileNotFoundException e) {
745 851
            LOG.info("Can't load project to stream", e);
746 852
        }
747
        this.fname = in;
853
        if ("bak".equals(FilenameUtils.getExtension(in.getAbsolutePath()))) {
854
            this.fname =  null;
855
        } else {
856
            this.fname = in;
857
        }
748 858
        notifyObservers(ProjectNotification.AFTER_LOAD_FROM_FILE, in);
749 859
    }
750 860

  
......
819 929
        notifyObservers(ProjectNotification.AFTER_LOAD_FROM_STATE, state);
820 930
    }
821 931

  
932
    @Override
822 933
    public void saveToState(PersistentState state) throws PersistenceException {
823 934
        state.set("version", VERSION);
824 935
        state.set("comments", getComments());
......
864 975

  
865 976
    }
866 977

  
978
    @Override
867 979
    public Object getProperty(Object key) {
868 980
        return this.propertiesHelper.getProperty(key);
869 981
    }
870 982

  
983
    @Override
871 984
    public void setProperty(Object key, Object obj) {
872 985
        this.propertiesHelper.setProperty(key, obj);
873 986
    }
874 987

  
988
    @Override
875 989
    public Map getExtendedProperties() {
876 990
        return this.propertiesHelper.getExtendedProperties();
877 991
    }
......
1133 1247
        throw new UnsupportedOperationException("This method is not supported");
1134 1248
    }
1135 1249

  
1250
    @Override
1136 1251
    public boolean canImportDocuments(String data, String doctype) {
1137 1252
        // TODO Auto-generated method stub
1138 1253
        return false;
1139 1254
    }
1140 1255

  
1256
    @Override
1141 1257
    public String exportDocumentsAsText(List<Document> documents) {
1142 1258
        // TODO Auto-generated method stub
1143 1259
        return null;
1144 1260
    }
1145 1261

  
1262
    @Override
1146 1263
    public void importDocuments(String data, String doctype) {
1147 1264
        // TODO Auto-generated method stub
1148 1265

  
1149 1266
    }
1150 1267

  
1268
    @Override
1151 1269
    public Document getActiveDocument() {
1152 1270
        return this.getActiveDocument((Class<? extends Document>)null);
1153 1271
    }
1154 1272

  
1273
    @Override
1155 1274
    public Document getActiveDocument(String documentTypeName) {
1156 1275
        ApplicationManager application = ApplicationLocator.getManager();
1157 1276

  
......
1187 1306
        return null;
1188 1307
    }
1189 1308

  
1309
    @Override
1190 1310
    public Document getActiveDocument(Class<? extends Document> documentClass) {
1191 1311
        ApplicationManager application = ApplicationLocator.getManager();
1192 1312

  
......
1222 1342
        return null;
1223 1343
    }
1224 1344

  
1345
    @Override
1225 1346
    public void addObserver(Observer o) {
1226 1347
        observableHelper.addObserver(o);
1227 1348
    }
1228 1349

  
1350
    @Override
1229 1351
    public void deleteObserver(Observer o) {
1230 1352
        observableHelper.deleteObserver(o);
1231 1353
    }
1232 1354

  
1355
    @Override
1233 1356
    public void deleteObservers() {
1234 1357
        observableHelper.deleteObservers();
1235 1358
    }
trunk/org.gvsig.desktop/org.gvsig.desktop.plugin/org.gvsig.app/org.gvsig.app.mainplugin/src/main/java/org/gvsig/app/extension/ProjectExtension.java
28 28
import java.awt.image.AffineTransformOp;
29 29
import java.awt.image.BufferedImage;
30 30
import java.io.File;
31
import java.io.FileOutputStream;
32 31
import java.io.IOException;
33 32
import java.text.MessageFormat;
34 33
import java.util.ArrayList;
......
36 35
import java.util.List;
37 36
import java.util.Set;
38 37
import java.util.prefs.Preferences;
39
import java.util.zip.ZipEntry;
40
import java.util.zip.ZipException;
41 38
import java.util.zip.ZipFile;
42
import java.util.zip.ZipOutputStream;
43
import javax.imageio.ImageIO;
44 39

  
45 40
import javax.swing.JOptionPane;
46
import javax.swing.SwingUtilities;
47 41
import org.apache.commons.collections.CollectionUtils;
48
import org.apache.commons.io.IOUtils;
42
import org.apache.commons.io.FilenameUtils;
49 43

  
50 44
import org.slf4j.Logger;
51 45
import org.slf4j.LoggerFactory;
......
62 56
import org.gvsig.andami.PluginsManager;
63 57
import org.gvsig.andami.actioninfo.ActionInfo;
64 58
import org.gvsig.andami.actioninfo.ActionInfoManager;
65
import org.gvsig.andami.messages.NotificationManager;
66 59
import org.gvsig.andami.plugins.Extension;
67 60
import org.gvsig.andami.plugins.IExtension;
68 61
import org.gvsig.andami.plugins.status.IExtensionStatus;
......
85 78
import org.gvsig.tools.dataTypes.DataTypes;
86 79
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
87 80
import org.gvsig.tools.i18n.I18nManager;
88
import org.gvsig.tools.persistence.exception.PersistenceException;
89 81
import org.gvsig.tools.util.ToolsUtilLocator;
90 82
import org.gvsig.utils.GenericFileFilter;
91 83
import org.gvsig.utils.save.AfterSavingListener;
......
195 187
            public OpenInitialProjectTask(File projectFile) {
196 188
                this.projectFile = projectFile;
197 189
            }
190
            @Override
198 191
            public void run() {
199 192
                if (this.projectFile == null) {
200 193
                    return;
......
334 327
		return true;
335 328
	}
336 329

  
330
        @Override
337 331
	public void execute(String command) {
338 332
		this.execute(command, null);
339 333
	}
340 334

  
335
        @Override
341 336
    public void execute(String actionCommand, Object[] args) {
342 337
        if (actionCommand.equals("application-project-new")) {
343 338
            if (!askSave()) {
......
374 369
                GenericFileFilter projExtensionFilter =
375 370
                    new GenericFileFilter(Project.FILE_EXTENSION, PluginServices.getText(this, "tipo_fichero_proyecto"));
376 371
                jfc.addChoosableFileFilter(projExtensionFilter);
372
                GenericFileFilter bakExtensionFilter =
373
                    new GenericFileFilter(Project.FILE_BAK, PluginServices.getText(this, "tipo_fichero_proyecto_bak"));
374
                jfc.addChoosableFileFilter(bakExtensionFilter);
377 375
                jfc.setFileFilter(projExtensionFilter);
378 376

  
379 377
                if (jfc.showOpenDialog((Component) PluginServices.getMainFrame()) != JFileChooser.APPROVE_OPTION) {
......
387 385
            PluginServices.getMDIManager().closeAllWindows();
388 386

  
389 387
            Project o = readProject(projectFile);
390
            setPath(projectFile.getAbsolutePath());
388
            if ("bak".equals(FilenameUtils.getExtension(projectFile.getAbsolutePath()))) {
389
                setPath(null);
390
            } else {
391
                setPath(projectFile.getAbsolutePath());
392
            }
391 393
            // lastPath = getPath();
392 394
            if (o != null) {
393 395
                setProject(o);
......
596 598
		} else {
597 599
            
598 600
        }
599
		return (Project) project;
601
                return (Project) project;
600 602
	}
601 603

  
602 604
	/**
......
658 660
		return getProjectFrame();
659 661
	}
660 662

  
663
        @Override
661 664
	public IExtensionStatus getStatus() {
662 665
		return this;
663 666
	}
664 667

  
668
        @Override
665 669
	public boolean hasUnsavedData() {
666 670
		return p.hasChanged();
667 671
	}
668 672

  
673
        @Override
669 674
	public IUnsavedData[] getUnsavedData() {
670 675
		if (hasUnsavedData()) {
671 676
			UnsavedProject data = new UnsavedProject(this);
......
688 693
			super(extension);
689 694
		}
690 695

  
696
                @Override
691 697
		public String getDescription() {
692 698
			if (getPath() == null) {
693 699
				return PluginServices.getText(ProjectExtension.this,
......
698 704
			}
699 705
		}
700 706

  
707
                @Override
701 708
		public String getResourceName() {
702 709
			if (getPath() == null) {
703 710
				return PluginServices.getText(ProjectExtension.this, "Unnamed");
......
707 714

  
708 715
		}
709 716

  
717
                @Override
710 718
		public boolean saveData() {
711 719
			return saveProject();
712 720
		}
713 721

  
722
                @Override
714 723
		public String getIcon() {
715 724
			return "project-icon";
716 725
		}
717 726
	}
718 727

  
728
        @Override
719 729
	public IMonitorableTask[] getRunningProcesses() {
720 730
		// TODO Auto-generated method stub
721 731
		return null;
722 732
	}
723 733

  
734
        @Override
724 735
	public boolean hasRunningProcesses() {
725 736
		// TODO Auto-generated method stub
726 737
		return false;

Also available in: Unified diff