Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / project / DefaultProject.java @ 44204

History | View | Annotate | Download (45.5 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License as published by the Free Software
8
 * Foundation; either version 3 of the License, or (at your option) any later
9
 * version.
10
 *
11
 * This program is distributed in the hope that it will be useful, but WITHOUT
12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
 * details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with
17
 * this program; if not, write to the Free Software Foundation, Inc., 51
18
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.app.project;
24

    
25
import java.awt.Color;
26
import java.awt.Image;
27
import java.awt.geom.AffineTransform;
28
import java.awt.image.AffineTransformOp;
29
import java.awt.image.BufferedImage;
30
import java.beans.PropertyChangeEvent;
31
import java.beans.PropertyChangeListener;
32
import java.beans.PropertyChangeSupport;
33
import java.io.File;
34
import java.io.FileInputStream;
35
import java.io.FileNotFoundException;
36
import java.io.FileOutputStream;
37
import java.io.IOException;
38
import java.io.InputStream;
39
import java.io.OutputStream;
40
import java.io.Serializable;
41
import java.text.DateFormat;
42
import java.text.MessageFormat;
43
import java.text.SimpleDateFormat;
44
import java.util.ArrayList;
45
import java.util.Collections;
46
import java.util.Date;
47
import java.util.HashMap;
48
import java.util.HashSet;
49
import java.util.Iterator;
50
import java.util.List;
51
import java.util.Map;
52
import java.util.Set;
53
import java.util.logging.Level;
54
import java.util.zip.ZipEntry;
55
import java.util.zip.ZipException;
56
import java.util.zip.ZipFile;
57
import java.util.zip.ZipOutputStream;
58
import javax.imageio.ImageIO;
59
import org.apache.commons.io.FileUtils;
60
import org.apache.commons.io.FilenameUtils;
61
import org.apache.commons.io.IOUtils;
62

    
63
import org.cresques.cts.IProjection;
64

    
65
import org.gvsig.andami.PluginServices;
66
import org.gvsig.andami.ui.mdiManager.IWindow;
67
import org.gvsig.andami.ui.mdiManager.MDIManager;
68
import org.gvsig.andami.ui.mdiManager.SingletonWindow;
69
import org.gvsig.andami.ui.mdiManager.WindowInfo;
70
import org.gvsig.app.ApplicationLocator;
71
import org.gvsig.app.ApplicationManager;
72
import org.gvsig.app.extension.ProjectExtension;
73
import org.gvsig.app.extension.Version;
74
import org.gvsig.app.project.ProjectManager.NewProjectEvent;
75
import org.gvsig.app.project.documents.AbstractDocument;
76
import org.gvsig.app.project.documents.Document;
77
import org.gvsig.app.project.documents.exceptions.SaveException;
78
import org.gvsig.app.project.documents.gui.IDocumentWindow;
79
import org.gvsig.app.project.documents.gui.ProjectWindow;
80
import org.gvsig.app.project.documents.view.DefaultViewDocument;
81
import org.gvsig.app.project.documents.view.ViewManager;
82
import org.gvsig.fmap.mapcontext.MapContext;
83
import org.gvsig.fmap.mapcontext.layers.ExtendedPropertiesHelper;
84
import org.gvsig.fmap.mapcontext.layers.FLayer;
85
import org.gvsig.fmap.mapcontext.layers.FLayers;
86
import org.gvsig.tools.ToolsLocator;
87
import org.gvsig.tools.dynobject.DynStruct;
88
import org.gvsig.tools.observer.ObservableHelper;
89
import org.gvsig.tools.observer.Observer;
90
import org.gvsig.tools.persistence.PersistenceManager;
91
import org.gvsig.tools.persistence.Persistent;
92
import org.gvsig.tools.persistence.PersistentContext;
93
import org.gvsig.tools.persistence.PersistentState;
94
import org.gvsig.tools.persistence.exception.PersistenceException;
95
import org.gvsig.utils.StringUtilities;
96
import org.gvsig.utils.save.SaveEvent;
97

    
98
import org.slf4j.Logger;
99
import org.slf4j.LoggerFactory;
100

    
101
/**
102
 * Clase que representa un proyecto de gvSIG
103
 *
104
 */
105
public class DefaultProject implements Serializable, PropertyChangeListener,
106
        Project {
107

    
108
    private Logger LOG = LoggerFactory.getLogger(DefaultProject.class);
109
    /**
110
     * @deprecated see ApplicationLocator.getManager().getVersion()
111
     */
112
    public static String VERSION = Version.format();
113

    
114
    private ExtendedPropertiesHelper propertiesHelper = new ExtendedPropertiesHelper();
115

    
116
    private ObservableHelper observableHelper = new ObservableHelper();
117

    
118
    /**
119
     *
120
     */
121
    private static final long serialVersionUID = -4449622027521773178L;
122

    
123
    private static final Logger logger = LoggerFactory.getLogger(Project.class);
124

    
125
    private static ProjectPreferences preferences = new ProjectPreferences();
126

    
127
    /**
128
     * Index used by the generator of unique names of documents.
129
     */
130
    private Map<String, Integer> nextDocumentIndexByType = new HashMap<String, Integer>();
131

    
132
    private PropertyChangeSupport change;
133

    
134
    private boolean modified = false;
135

    
136
    private String name = null;
137

    
138
    private String creationDate = null;
139

    
140
    private String modificationDate = null;
141

    
142
    private String owner = null;
143

    
144
    private String comments = null;
145

    
146
    private Color selectionColor = null;
147

    
148
    private List<Document> documents = null;
149

    
150
    private List<ProjectExtent> extents = null;
151

    
152
    private IProjection projection;
153

    
154
    private File fname = null;
155

    
156
    private Set<String> unloadedObjects;
157
    private PersistenceException loadErrors = null;
158

    
159
    /**
160
     * Creates a new Project object.
161
     */
162
    DefaultProject() {
163
        this.change = new PropertyChangeSupport(this);
164
        this.clean();
165
        ProjectManager.getInstance().notifyProjectEvent(new NewProjectEvent() {
166
            @Override
167
            public Project getProject() {
168
                return DefaultProject.this;
169
            }
170
        });
171
    }
172

    
173
    protected void clean() {
174
        this.owner = "";
175
        this.comments = "";
176
        this.name = PluginServices.getText(this, "untitled");
177
        this.creationDate = DateFormat.getDateInstance().format(new Date());
178
        this.modificationDate = this.creationDate;
179

    
180
        this.documents = new ArrayList<Document>();
181
        this.extents = new ArrayList<ProjectExtent>();
182

    
183
        this.setSelectionColor(getPreferences().getDefaultSelectionColor());
184

    
185
        this.projection = null; // se inicializa en el getProjection()
186
    }
187

    
188
    public static ProjectPreferences getPreferences() {
189
        return preferences;
190
    }
191

    
192
    @Override
193
    public void propertyChange(PropertyChangeEvent evt) {
194
        change.firePropertyChange(evt);
195
    }
196

    
197
    @Override
198
    public synchronized void addPropertyChangeListener(
199
            PropertyChangeListener arg0) {
200
        change.addPropertyChangeListener(arg0);
201
    }
202

    
203
    @Override
204
    public void removePropertyChangeListener(PropertyChangeListener listener) {
205
        change.removePropertyChangeListener(listener);
206
    }
207
    
208
    /**
209
     * Return the creation date of the project
210
     *
211
     * @return
212
     */
213
    @Override
214
    public String getCreationDate() {
215
        return creationDate;
216
    }
217

    
218
    protected void setCreationDate(String creationDate) {
219
        this.creationDate = creationDate;
220
        change.firePropertyChange("setCreationDate", null, null);
221
    }
222

    
223
    @Override
224
    public Document createDocument(String type) {
225
        logger.info("createDocument('{}')", type);
226
        return ProjectManager.getInstance().createDocument(type);
227
    }
228

    
229
    /**
230
     * Return the name of the project
231
     *
232
     * @return
233
     */
234
    @Override
235
    public String getName() {
236
        return name;
237
    }
238

    
239
    /**
240
     * Set the name of he project.
241
     *
242
     * @param string
243
     */
244
    @Override
245
    public void setName(String name) {
246
        this.name = name;
247
        change.firePropertyChange("setName", null, null);
248
    }
249

    
250
    /**
251
     * Return the comments associateds with the project
252
     *
253
     * @return comments
254
     */
255
    @Override
256
    public String getComments() {
257
        return comments;
258
    }
259

    
260
    /**
261
     * Set the comments associateds with the project
262
     *
263
     * @param comments as string
264
     */
265
    @Override
266
    public void setComments(String string) {
267
        comments = string;
268
        change.firePropertyChange("setComments", null, null);
269
    }
270

    
271
    /**
272
     * Retuen the modification date of the project.
273
     *
274
     * @return modification date as string
275
     */
276
    @Override
277
    public String getModificationDate() {
278
        return modificationDate;
279
    }
280

    
281
    protected void setModificationDate(String string) {
282
        modificationDate = string;
283
        change.firePropertyChange("setModificationDate", null, null);
284
    }
285

    
286
    /**
287
     * Return the author of the project,
288
     *
289
     * @return author as string
290
     */
291
    @Override
292
    public String getOwner() {
293
        return owner;
294
    }
295

    
296
    /**
297
     * Sets the author of the project
298
     *
299
     * @param author name as string
300
     */
301
    @Override
302
    public void setOwner(String owner) {
303
        this.owner = owner;
304
        change.firePropertyChange("setOwner", null, null);
305
    }
306

    
307
    /**
308
     * Obtiene el color de selecci�n que se usar� en el proyecto
309
     *
310
     * @return
311
     */
312
    @Override
313
    public Color getSelectionColor() {
314
        if (selectionColor == null) {
315
            selectionColor = getPreferences().getDefaultSelectionColor();
316
        }
317
        return selectionColor;
318
    }
319

    
320
    /**
321
     * Sets the selecction color
322
     *
323
     * @param selection color as string
324
     */
325
    @Override
326
    public void setSelectionColor(String selectionColor) {
327
        this.setSelectionColor(StringUtilities.string2Color(selectionColor));
328
    }
329

    
330
    /**
331
     * Sets the selecction color
332
     *
333
     * @param selection color as Color
334
     */
335
    @Override
336
    public void setSelectionColor(Color selectionColor) {
337
        this.selectionColor = selectionColor;
338
        MapContext.setSelectionColor(selectionColor);
339
        change.firePropertyChange("selectionColor", null, selectionColor);
340
    }
341

    
342
    @Override
343
    public IProjection getProjection() {
344
        if (projection == null) {
345
            projection = getPreferences().getDefaultProjection();
346
        }
347
        return projection;
348
    }
349

    
350
    @Override
351
    public void setProjection(IProjection projection) {
352
        this.projection = projection;
353
    }
354

    
355
    /**
356
     * Sets the modified state of project.
357
     *
358
     * Can't set to not modified.
359
     *
360
     * @param modified as boolean
361
     */
362
    @Override
363
    public void setModified(boolean modified) {
364
        this.modified = modified;
365
        if (modified == false) {
366
            List<Document> documents = this.getDocuments();
367
            for (int i = 0; i < documents.size(); i++) {
368
                documents.get(i).setModified(false);
369
            }
370
        }
371
    }
372

    
373
    @Override
374
    public boolean hasChanged() {
375
                // we return true if the project is not empty (until we have a better
376
        // method...)
377
        if ((this.getDocuments().size() != 0) || modified) {
378
            return true;
379
        }
380
        return false;
381
    }
382

    
383
    /**
384
     * Return a list of documents in the project.
385
     *
386
     * @return documents as List of IProjectDocument
387
     */
388
    @Override
389
    public List<Document> getDocuments() {
390
        return Collections.unmodifiableList(documents);
391
    }
392

    
393
    /**
394
     * Return a list with all documents of especified type.
395
     *
396
     * @param type of document
397
     *
398
     * @return List of IProjectDocument
399
     */
400
    @Override
401
    public List<Document> getDocuments(String type) {
402
        List<Document> docs = new ArrayList<Document>();
403
        if (type != null) {
404
            for (Document document : this.documents) {
405
                if (type.equalsIgnoreCase(document.getTypeName())) {
406
                    docs.add(document);
407
                }
408
            }
409
        }
410
        return Collections.unmodifiableList(docs);
411
    }
412

    
413
    /**
414
     * Adds a document to the project
415
     *
416
     * @param document as Document
417
     */
418
    @Override
419
    public void add(Document document) {
420
        this.addDocument(document);
421
    }
422

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

    
427
        if (notifyObservers(ProjectNotification.BEFORE_ADD_DOCUMENT, document).isProcessCanceled()) {
428
            return;
429
        }
430
        document.addPropertyChangeListener(this);
431
        document.setProject(this);
432
        document.setName(this.getUniqueNameForDocument(document.getTypeName(),
433
                document.getName()));
434
        documents.add(document);
435
        document.afterAdd();
436
        this.setModified(true);
437
        notifyObservers(ProjectNotification.AFTER_ADD_DOCUMENT, document);
438
        change.firePropertyChange("addDocument", null, document);
439
    }
440

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

    
446
    @Override
447
    public void removeDocument(Document doc) {
448
        logger.info("remove('{}')", doc.toString());
449
        if (notifyObservers(ProjectNotification.BEFORE_REMOVE_DOCUMENT, doc).isProcessCanceled()) {
450
            return;
451
        }
452
        documents.remove(doc);
453
        this.setModified(true);
454
        change.firePropertyChange("delDocument", doc, null);
455
        doc.afterRemove();
456
        notifyObservers(ProjectNotification.AFTER_REMOVE_DOCUMENT, doc);
457
    }
458

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

    
464
    @Override
465
    public boolean isEmpty() {
466
        return documents.isEmpty();
467
    }
468

    
469
    /**
470
     * Return the view that contains the especified layer.
471
     *
472
     * @param layer
473
     *
474
     * @return name of the view that contains the layer
475
     *
476
     * @throws RuntimeException Si la capa que se pasa como par�metro no se
477
     * encuentra en ninguna vista
478
     */
479
    @Override
480
    public String getViewName(FLayer layer) {
481
        List<Document> views = getDocuments(ViewManager.TYPENAME);
482
        for (int v = 0; v < views.size(); v++) {
483
            DefaultViewDocument pView = (DefaultViewDocument) views.get(v);
484
            FLayers layers = pView.getMapContext().getLayers();
485
            if (isView(layers, layer)) {
486
                return pView.getName();
487
            }
488
        }
489

    
490
        throw new RuntimeException(MessageFormat.format(
491
                "The layer '{1}' is not in a view", layer.getName()));
492
    }
493

    
494
    private boolean isView(FLayers layers, FLayer layer) {
495
        for (int i = 0; i < layers.getLayersCount(); i++) {
496
            if (layers.getLayer(i) instanceof FLayers) {
497
                if (isView((FLayers) layers.getLayer(i), layer)) {
498
                    return true;
499
                }
500
            }
501
            if (layers.getLayer(i) == layer) {
502
                return true;
503
            }
504
        }
505
        return false;
506
    }
507

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

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

    
520
    @Override
521
    public ProjectExtent[] getExtents() {
522
        return (ProjectExtent[]) extents.toArray(new ProjectExtent[0]);
523
    }
524

    
525
    /**
526
     * Obtiene un documento a partir de su nombre y el nombre de registro en el
527
     * pointExtension, este �ltimo se puede obtener del
528
     * Project****Factory.registerName.
529
     *
530
     * @param name Nombre del documento
531
     * @param type nombre de registro en el extensionPoint
532
     *
533
     * @return Documento
534
     */
535
    @Override
536
    public Document getDocument(String name, String type) {
537
        if (type == null) {
538
            for (int i = 0; i < documents.size(); i++) {
539
                Document document = documents.get(i);
540
                if ( name.equalsIgnoreCase(document.getName())) {
541
                    return document;
542
                }
543
            }
544
        } else {
545
            for (int i = 0; i < documents.size(); i++) {
546
                Document document = documents.get(i);
547
                if (type.equalsIgnoreCase(document.getTypeName())
548
                        && name.equalsIgnoreCase(document.getName())) {
549
                    return document;
550
                }
551
            }
552
        }
553
        return null;
554
    }
555

    
556
    @Override
557
    public Document getDocument(String name) {
558
        return this.getDocument(name, null);
559
    }
560

    
561
    public String getUniqueNameForDocument(String type, String name) {
562
        Document document = getDocument(name, type);
563
        if (document == null) {
564
            return name;
565
        }
566

    
567
        String newName = null;
568
        int num = this.getNextDocumentIndex(type);
569
        while (document != null) {
570
            newName = name + " - " + num++;
571
            document = getDocument(newName, type);
572
        }
573
        this.setNextDocumentIndex(type, num);
574
        return newName;
575
    }
576

    
577
    private int getNextDocumentIndex(String type) {
578
        if (nextDocumentIndexByType.get(type) == null) {
579
            nextDocumentIndexByType.put(type, new Integer(1));
580
            return 1;
581
        }
582
        return nextDocumentIndexByType.get(type).intValue();
583
    }
584

    
585
    private void setNextDocumentIndex(String type, int newIndex) {
586
        if (nextDocumentIndexByType.get(type) == null) {
587
            nextDocumentIndexByType.put(type, new Integer(newIndex));
588
        } else {
589
            nextDocumentIndexByType.put(type, new Integer(newIndex));
590
        }
591
    }
592

    
593
    @Override
594
    public void saveState(File out) throws PersistenceException {
595
        FileOutputStream fout;
596
        if (notifyObservers(ProjectNotification.BEFORE_SAVE_TO_FILE, out).isProcessCanceled()) {
597
            return;
598
        }
599
        try {
600
            fout = new FileOutputStream(out);
601
            saveState(fout, new File(out.getParent()));
602
        } catch (FileNotFoundException e) {
603
            createProjectCopy(true);
604
            throw new PersistenceException(e);
605
        }
606
        if( !isValidZIP(out) ) {
607
            throw new PersistenceException(new ZipException());
608
        }
609
        this.fname = out;
610
        //sacar copia al bak
611
        createProjectCopy(false);
612
        notifyObservers(ProjectNotification.AFTER_SAVE_TO_FILE, out);
613
    }
614

    
615
    boolean isValidZIP(final File file) {
616
        ZipFile zipfile = null;
617
        try {
618
            zipfile = new ZipFile(file);
619
            return true;
620
        } catch (IOException e) {
621
            //sacar copia. 
622
            return false;
623
        } finally {
624
            try {
625
                if (zipfile != null) {
626
                    zipfile.close();
627
                    zipfile = null;
628
                }
629
            } catch (IOException e) {
630
            }
631
        }
632
    }
633
   
634
    @Override
635
    public File getFile() {
636
        return this.fname;
637
    }
638

    
639
    @Deprecated
640
    @Override
641
    public void saveState(OutputStream out) throws PersistenceException {
642
        saveState(out, null);
643
    }
644
    
645
    @Override
646
    public void saveState(File file, BufferedImage preview) {
647
        FileOutputStream fout=null;
648
        ZipOutputStream zout=null;
649
        try {
650
            fout = new FileOutputStream(file);
651
            zout = new ZipOutputStream(fout);
652
            this.saveState(zout, file.getParentFile());
653

    
654
            zout.putNextEntry(new ZipEntry("preview.jpg"));
655
            try {
656
                ImageIO.write(preview, "jpg", zout);
657
            } catch (IOException ex) {
658
                LOG.warn("Can't save preview image'.", ex);
659
            }
660
            IOUtils.closeQuietly(zout);
661
            IOUtils.closeQuietly(fout);
662

    
663
            if( !isValidZIP(file) ) {
664
                throw new ZipException("Invalid project file '"+file.getAbsolutePath()+"'");
665
            }
666
            this.fname = file;
667
            if (file.exists()) {
668
                createProjectCopy(false);
669
            }
670
        } catch (Exception ex) {
671
            createProjectCopy(true);
672
            throw new RuntimeException("Can't write project in '" + file.getAbsolutePath() + ".", ex);
673
        } finally {
674
            IOUtils.closeQuietly(zout);
675
            IOUtils.closeQuietly(fout);
676

    
677
        }
678
    }
679
    
680
    @Override
681
    public void saveState(OutputStream out, File rootFolder)
682
            throws PersistenceException {
683
        if (notifyObservers(ProjectNotification.BEFORE_SAVE_TO_STREAM, rootFolder).isProcessCanceled()) {
684
            return;
685
        }
686
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
687
        PersistentState state = null;
688
        state = manager.getState(this, true);
689
        try {
690
            if (rootFolder != null) {
691
                state.relativizeFiles(rootFolder);
692
            }
693
        } catch (Exception ex) {
694
            state.getContext().addError(ex);
695
        }
696
        manager.saveState(state, out, true);
697
        if (state.getContext().getErrors() != null) {
698
            createProjectCopy(true);
699
            throw state.getContext().getErrors();
700
        }
701
        this.fname = null;
702
        notifyObservers(ProjectNotification.AFTER_SAVE_TO_STREAM, rootFolder);
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";
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 bakProjectFileOld = getNameFileForProject(actualProjectFile, false);
755
            if (bakProjectFileOld.exists()) {
756
                try {
757
                    FileUtils.moveFile(bakProjectFileOld, bakProjectFile);
758
                } catch (IOException ex) {
759
                    LOG.error("Can't rename file", ex);
760
                }
761
            } else {
762
                LOG.warn("Can't find bak project. Probably doesn't exist or already has been renamed");
763
            }
764
        }
765
        
766
        return bakProjectFile;
767
    }
768

    
769
    @Deprecated
770
    @Override
771
    public void loadState(InputStream in) {
772
        loadState(in, null);
773
    }
774

    
775
    public void loadState(InputStream in, File rootFolder) {
776
        if (notifyObservers(ProjectNotification.BEFORE_LOAD_FROM_STREAM, rootFolder).isProcessCanceled()) {
777
            return;
778
        }
779
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
780
        try {
781
            PersistentContext context = manager.getNewContext();
782
            context.setCollectErrors(true);
783
            PersistentState state = manager.loadState(in, context);
784
            try {
785
                if (rootFolder != null) {
786
                    state.derelativizeFiles(rootFolder);
787
                }
788
            } catch (Exception ex) {
789
                state.getContext().addError(ex);
790
            }
791
            this.loadFromState(state);
792
            this.unloadedObjects = getUnloadedObjects(state.getContext());
793
            this.loadErrors = state.getContext().getErrors();
794

    
795
        } catch (PersistenceException e) {
796
            LOG.info("Can't load project to stream", e);
797
        }
798
        this.fname = null;
799
        notifyObservers(ProjectNotification.AFTER_LOAD_FROM_STREAM, rootFolder);
800

    
801
    }
802

    
803

    
804

    
805
    /**
806
     * @param context
807
     * @return
808
     */
809
    private Set<String> getUnloadedObjects(PersistentContext context) {
810
        Set unloadedObjects = new HashSet();
811

    
812
        Iterator statesIterator = context.iterator();
813
        String className = null;
814

    
815
        while (statesIterator.hasNext()) {
816
            PersistentState aState = (PersistentState) statesIterator.next();
817
            try {
818
                className = aState.getTheClassName();
819
                DynStruct definition = aState.getDefinition();
820
                if (definition == null) {
821
                    unloadedObjects.add(className);
822
                }
823
            } catch (Throwable e) {
824
                // do nothing
825
            }
826
        }
827

    
828
        if(unloadedObjects.isEmpty()){
829
            return null;
830
        }
831

    
832
        return unloadedObjects;
833
    }
834

    
835
    @Override
836
    public Set<String> getUnloadedObjects() {
837
        return this.unloadedObjects;
838
    }
839

    
840
    @Override
841
    public List<Exception> getLoadErrors() {
842
        return this.loadErrors;
843
    }
844
    
845
    @Override
846
    public void loadState(File in) {
847
        if (notifyObservers(ProjectNotification.BEFORE_LOAD_FROM_FILE, in).isProcessCanceled()) {
848
            return;
849
        }
850
        FileInputStream fin;
851
        try {
852
            fin = new FileInputStream(in);
853
            loadState(fin, new File(in.getParent()));
854
        } catch (FileNotFoundException e) {
855
            LOG.info("Can't load project to stream", e);
856
        }
857
        if ("bak".equals(FilenameUtils.getExtension(in.getAbsolutePath()))) {
858
            this.fname =  null;
859
        } else {
860
            this.fname = in;
861
        }
862
        notifyObservers(ProjectNotification.AFTER_LOAD_FROM_FILE, in);
863
    }
864

    
865
    //@SuppressWarnings("unchecked")
866
    @Override
867
    public void loadFromState(PersistentState state)
868
            throws PersistenceException {
869
        this.clean();
870
        PersistentContext context = state.getContext();
871
        
872
        notifyObservers(ProjectNotification.BEFORE_LOAD_FROM_STATE, state);
873

    
874
        this.setComments(state.getString("comments"));
875
        this.setCreationDate(state.getString("creationDate"));
876
        this.setModificationDate(state.getString("modificationDate"));
877
        this.setName(state.getString("name"));
878
        this.setOwner(state.getString("owner"));
879
        this.setSelectionColor((Color) state.get("selectionColor"));
880
        this.setProjection((IProjection) state.get("projection"));
881

    
882
        this.propertiesHelper = (ExtendedPropertiesHelper) state.get("propertiesHelper");
883

    
884
        List<ProjectExtent> extents = (List<ProjectExtent>) state
885
                .get("extents");
886
        for (int i = 0; i < extents.size(); i++) {
887
            this.addExtent(extents.get(i));
888
        }
889

    
890
        List<AbstractDocument> documents = (List<AbstractDocument>) state
891
                .get("documents");
892
        for (int i = 0; i < documents.size(); i++) {
893
            AbstractDocument doc = documents.get(i);
894
            if( doc != null ) {
895
                this.add(doc);
896
            }
897
        }
898

    
899
        try {
900
            List<DocumentWindowInfo> persistentWindows = (List<DocumentWindowInfo>) state.get("documentWindowsInformation");
901

    
902
            for (int i = 0; i < persistentWindows.size(); i++) {
903
                try {
904
                    DocumentWindowInfo persistentWindow = persistentWindows.get(i);
905
                    String docName = persistentWindow.getDocumentName();
906
                    String docType = persistentWindow.getDocumentType();
907
                    Document doc = this.getDocument(docName, docType);
908
                    IWindow win = doc.getFactory().getMainWindow(doc);
909
                    if(win!=null){
910
                        win.getWindowInfo().setWindowInfo(persistentWindow.getWindowInfo());
911
                        PluginServices.getMDIManager().addWindow(win);
912
                    }
913
                } catch(Exception ex) {
914
                    if( !context.getCollectErrors() ) {
915
                        throw ex;
916
                    }
917
                    context.addError(ex);
918
                }
919
            }
920

    
921
            if (state.hasValue("projectWindowInfo")) {
922
                WindowInfo projectWindowInfo = (WindowInfo) state.get("projectWindowInfo");
923
                ProjectExtension pe = (ProjectExtension) PluginServices.getExtension(org.gvsig.app.extension.ProjectExtension.class);
924
                pe.setProject(this);
925
                pe.showProjectWindow(projectWindowInfo);
926
            }
927
        } catch(Exception ex) {
928
            if( !context.getCollectErrors() ) {
929
                throw ex;
930
            }
931
            context.addError(ex);
932
        }
933
        notifyObservers(ProjectNotification.AFTER_LOAD_FROM_STATE, state);
934
    }
935

    
936
    @Override
937
    public void saveToState(PersistentState state) throws PersistenceException {
938
        state.set("version", VERSION);
939
        state.set("comments", getComments());
940
        state.set("creationDate", this.getCreationDate());
941

    
942
        state.set("modificationDate", this.getModificationDate());
943
        state.set("name", this.getName());
944
        state.set("owner", this.getOwner());
945
        state.set("selectionColor", this.getSelectionColor());
946

    
947
        state.set("projection", this.getProjection());
948

    
949
        state.set("extents", this.extents);
950
        List<Document> docs = this.getDocuments();
951
        List<Document> noTempDocs = new ArrayList<Document>();
952
        for (Iterator iterator = docs.iterator(); iterator.hasNext();) {
953
            Document document = (Document) iterator.next();
954
            if(!document.isTemporary()){
955
                noTempDocs.add(document);
956
            }
957
        }
958
        state.set("documents", noTempDocs);
959

    
960
        state.set("propertiesHelper",propertiesHelper);
961

    
962
        List<DocumentWindowInfo> persistentWindows = new ArrayList<DocumentWindowInfo>();
963
        MDIManager mdiMan = PluginServices.getMDIManager();
964
        IWindow[] windows = mdiMan.getOrderedWindows();
965
        for (int i = windows.length - 1; i >= 0; i--) {
966
            IWindow window = windows[i];
967
            if (window instanceof IDocumentWindow) {
968
                WindowInfo wi = mdiMan.getWindowInfo(window);
969
                DocumentWindowInfo dwi = new DocumentWindowInfo(
970
                        wi,
971
                        ((IDocumentWindow) window).getDocument().getTypeName(),
972
                        ((IDocumentWindow) window).getDocument().getName());
973
                persistentWindows.add(dwi);
974
            } else if (window instanceof ProjectWindow) {
975
                state.set("projectWindowInfo", mdiMan.getWindowInfo(window));
976
            }
977
        }
978
        state.set("documentWindowsInformation", persistentWindows);
979

    
980
    }
981

    
982
    @Override
983
    public Object getProperty(Object key) {
984
        return this.propertiesHelper.getProperty(key);
985
    }
986

    
987
    @Override
988
    public void setProperty(Object key, Object obj) {
989
        this.propertiesHelper.setProperty(key, obj);
990
    }
991

    
992
    @Override
993
    public Map getExtendedProperties() {
994
        return this.propertiesHelper.getExtendedProperties();
995
    }
996

    
997
    public static class DocumentWindowInfo implements Persistent {
998

    
999
        public static final String PERSISTENCE_DEFINITION_NAME = "DocumentWindowInfo";
1000

    
1001
        private WindowInfo windowInfo;
1002
        private String documentType;
1003
        private String documentName;
1004

    
1005
        public DocumentWindowInfo() {
1006
        }
1007

    
1008
        DocumentWindowInfo(WindowInfo wi, String docType, String docName) {
1009
            windowInfo = wi;
1010
            documentType = docType;
1011
            documentName = docName;
1012
        }
1013

    
1014
        public WindowInfo getWindowInfo() {
1015
            return windowInfo;
1016
        }
1017

    
1018
        public String getDocumentType() {
1019
            return documentType;
1020
        }
1021

    
1022
        public String getDocumentName() {
1023
            return documentName;
1024
        }
1025

    
1026
        public void saveToState(PersistentState state)
1027
                throws PersistenceException {
1028
            state.set("windowInfo", this.windowInfo);
1029
            state.set("documentType", this.documentType);
1030
            state.set("documentName", this.documentName);
1031
        }
1032

    
1033
        public void loadFromState(PersistentState state)
1034
                throws PersistenceException {
1035
            this.windowInfo = (WindowInfo) state.get("windowInfo");
1036
            this.documentType = state.getString("documentType");
1037
            this.documentName = state.getString("documentName");
1038
        }
1039

    
1040
        public static void registerPersistent() {
1041
            PersistenceManager manager = ToolsLocator.getPersistenceManager();
1042
            DynStruct definition = manager.getDefinition(PERSISTENCE_DEFINITION_NAME);
1043
            if (definition == null) {
1044
                definition = manager.addDefinition(
1045
                        DocumentWindowInfo.class,
1046
                        PERSISTENCE_DEFINITION_NAME,
1047
                        "DocumentWindowInfo persistence definition",
1048
                        null,
1049
                        null
1050
                );
1051
                definition.addDynFieldObject("windowInfo").setMandatory(true).setClassOfValue(WindowInfo.class);
1052
                definition.addDynFieldString("documentType").setMandatory(true);
1053
                definition.addDynFieldString("documentName").setMandatory(true);
1054
            }
1055

    
1056
        }
1057
    }
1058

    
1059
    public static void registerPersistent() {
1060
        AbstractDocument.registerPersistent();
1061
        DocumentWindowInfo.registerPersistent();
1062
        ProjectExtent.registerPersistent();
1063

    
1064
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
1065
        DynStruct definition = manager.addDefinition(DefaultProject.class,
1066
                "Project", "Project Persistence definition", null, null);
1067
        definition.addDynFieldString("version").setMandatory(true);
1068
        definition.addDynFieldString("comments").setMandatory(true);
1069
        definition.addDynFieldString("creationDate").setMandatory(true);
1070
        definition.addDynFieldString("modificationDate").setMandatory(true);
1071
        definition.addDynFieldString("name").setMandatory(true);
1072
        definition.addDynFieldString("owner").setMandatory(true);
1073

    
1074
        definition.addDynFieldObject("selectionColor")
1075
                .setClassOfValue(Color.class).setMandatory(true);
1076
        definition.addDynFieldObject("projection")
1077
                .setClassOfValue(IProjection.class).setMandatory(true);
1078

    
1079
        definition.addDynFieldList("extents")
1080
                .setClassOfItems(ProjectExtent.class).setMandatory(true);
1081

    
1082
        definition.addDynFieldList("documents").setClassOfItems(Document.class)
1083
                .setMandatory(true);
1084

    
1085
        definition.addDynFieldObject("projectWindowInfo").setClassOfValue(WindowInfo.class).setMandatory(false);
1086

    
1087
        definition.addDynFieldList("documentWindowsInformation").setClassOfItems(WindowInfo.class).setMandatory(false);
1088

    
1089

    
1090
        definition.addDynFieldObject("propertiesHelper").setClassOfValue(ExtendedPropertiesHelper.class)
1091
                        .setMandatory(false);
1092

    
1093
    }
1094

    
1095
    /**
1096
     * @deprecated use getPreferences().setDefaultSelectionColor()
1097
     */
1098
    public static void setDefaultSelectionColor(Color color) {
1099
        getPreferences().setDefaultSelectionColor(color);
1100
    }
1101

    
1102
    /**
1103
     * @deprecated use getPreferences().getDefaultSelectionColor()
1104
     */
1105
    public static Color getDefaultSelectionColor() {
1106
        return getPreferences().getDefaultSelectionColor();
1107
    }
1108

    
1109
    /**
1110
     * @deprecated use getPreferences().getDefaultMapUnits()
1111
     */
1112
    public static int getDefaultMapUnits() {
1113
        return getPreferences().getDefaultMapUnits();
1114
    }
1115

    
1116
    /**
1117
     * @deprecated use getPreferences().getDefaultDistanceUnits()
1118
     */
1119
    public static int getDefaultDistanceUnits() {
1120
        return getPreferences().getDefaultDistanceUnits();
1121
    }
1122

    
1123
    /**
1124
     * @deprecated use getPreferences().getDefaultDistanceArea()
1125
     */
1126
    public static int getDefaultDistanceArea() {
1127
        return getPreferences().getDefaultDistanceArea();
1128
    }
1129

    
1130
    /**
1131
     * @deprecated use getPreferences().setDefaultMapUnits()
1132
     */
1133
    public static void setDefaultMapUnits(int mapUnits) {
1134
        getPreferences().setDefaultMapUnits(mapUnits);
1135
    }
1136

    
1137
    /**
1138
     * @deprecated use getPreferences().setDefaultDistanceUnits()
1139
     */
1140
    public static void setDefaultDistanceUnits(int distanceUnits) {
1141
        getPreferences().setDefaultDistanceUnits(distanceUnits);
1142
    }
1143

    
1144
    /**
1145
     * @deprecated use getPreferences().setDefaultDistanceArea()
1146
     */
1147
    public static void setDefaultDistanceArea(int distanceArea) {
1148
        getPreferences().setDefaultDistanceArea(distanceArea);
1149
    }
1150

    
1151
    /**
1152
     * @deprecated use getPreferences().setDefaultProjection()
1153
     */
1154
    public static void setDefaultProjection(IProjection defaultProjection) {
1155
        getPreferences().setDefaultProjection(defaultProjection);
1156
    }
1157

    
1158
    /**
1159
     * @deprecated use getPreferences().getDefaultProjection()
1160
     */
1161
    public static IProjection getDefaultProjection() {
1162
        return getPreferences().getDefaultProjection();
1163
    }
1164

    
1165
    /**
1166
     * @deprecated see {@link #setSelectionColor(String)}, to be remove in gvSIG
1167
     * 2.1.0
1168
     */
1169
    public void setColor(String color) {
1170
        this.setSelectionColor(StringUtilities.string2Color(color));
1171
    }
1172

    
1173
    /**
1174
     * Return the selection color
1175
     *
1176
     * @return selection color as string
1177
     * @deprecated use {@link #getSelectionColor()}
1178
     */
1179
    public String getColor() {
1180
        return StringUtilities.color2String(selectionColor);
1181
    }
1182

    
1183
    /**
1184
     * Return the list of views of the project
1185
     *
1186
     * @return views as ArrayList of ProjectDocument
1187
     *
1188
     * @deprecated see {@link #getDocumentsByType(String)}
1189
     */
1190
    public List<Document> getViews() {
1191
        return getDocuments(ViewManager.TYPENAME);
1192
    }
1193

    
1194
    /**
1195
     * Add a view to the project
1196
     *
1197
     * @deprecated see {@link #add(AbstractDocument)}
1198
     */
1199
    public void addView(DefaultViewDocument v) {
1200
        add(v);
1201
    }
1202

    
1203
    /**
1204
     * Remove a view of the project
1205
     *
1206
     * @param index of the view as integer
1207
     *
1208
     * @deprecated see {@link #remove(AbstractDocument)}
1209
     */
1210
    public void delView(int i) {
1211
        List<Document> list = getDocuments(ViewManager.TYPENAME);
1212
        remove(list.get(i));
1213
    }
1214

    
1215
    /**
1216
     * @deprecated see {@link #getDocument(String, String)}
1217
     */
1218
    public Document getProjectDocumentByName(String name, String type) {
1219
        return this.getDocument(name, type);
1220
    }
1221

    
1222
    /**
1223
     * @deprecated see {@link #getDocuments(String)}
1224
     */
1225
    public List<Document> getDocumentsByType(String type) {
1226
        return this.getDocuments(type);
1227
    }
1228

    
1229
    /**
1230
     * @deprecated aun por decidir que API darle al copy/paste
1231
     */
1232
    public String exportToXML(AbstractDocument[] selectedItems)
1233
            throws SaveException {
1234
        // FIXME jjdc:hay que decirdir que API darle al copy/paste
1235
        throw new UnsupportedOperationException("This method is not supported");
1236
    }
1237

    
1238
    /**
1239
     * @deprecated aun por decidir que API darle al copy/paste
1240
     */
1241
    public void importFromXML(String sourceString, String docType) {
1242
        // FIXME jjdc:hay que decirdir que API darle al copy/paste
1243
        throw new UnsupportedOperationException("This method is not supported");
1244
    }
1245

    
1246
    /**
1247
     * @deprecated aun por decidir que API darle al copy/paste
1248
     */
1249
    public boolean isValidXMLForImport(String sourceString, String docType) {
1250
        // FIXME jjdc:hay que decirdir que API darle al copy/paste
1251
        throw new UnsupportedOperationException("This method is not supported");
1252
    }
1253

    
1254
    @Override
1255
    public boolean canImportDocuments(String data, String doctype) {
1256
        // TODO Auto-generated method stub
1257
        return false;
1258
    }
1259

    
1260
    @Override
1261
    public String exportDocumentsAsText(List<Document> documents) {
1262
        // TODO Auto-generated method stub
1263
        return null;
1264
    }
1265

    
1266
    @Override
1267
    public void importDocuments(String data, String doctype) {
1268
        // TODO Auto-generated method stub
1269

    
1270
    }
1271

    
1272
    @Override
1273
    public Document getActiveDocument() {
1274
        return this.getActiveDocument((Class<? extends Document>)null);
1275
    }
1276

    
1277
    @Override
1278
    public Document getActiveDocument(String documentTypeName) {
1279
        ApplicationManager application = ApplicationLocator.getManager();
1280

    
1281
        Document document = null;
1282
        IWindow[] windows = application.getUIManager().getOrderedWindows();
1283
        IWindow window = null;
1284
        for (int i = 0; i < windows.length; i++) {
1285
            window = windows[i];
1286
            if (window instanceof SingletonWindow) {
1287
                // Cogemos no la primera ventana, si no la primera
1288
                // ventana de tipo documento (SingletonWindow).
1289
                // Y por si las mosca no es un documento, atrapamos
1290
                // los errores y continuamos si no puede hacer un cast
1291
                // del Model a Document
1292
                try {
1293
                    document = (Document) ((SingletonWindow) window).getWindowModel();
1294
                    if (documentTypeName == null) {
1295
                        return document;
1296
                    }
1297
                    if( document.getTypeName().equalsIgnoreCase(documentTypeName) ) {
1298
                        return document;
1299
                    }
1300
                    if( document instanceof DocumentsContainer ) {
1301
                        Document subdoc = ((DocumentsContainer)document).getActiveDocument(documentTypeName);
1302
                        return subdoc;
1303
                    }
1304

    
1305
                } catch (ClassCastException e) {
1306
                    // Do nothing, skip this window
1307
                }
1308
            }
1309
        }
1310
        return null;
1311
    }
1312

    
1313
    @Override
1314
    public Document getActiveDocument(Class<? extends Document> documentClass) {
1315
        ApplicationManager application = ApplicationLocator.getManager();
1316

    
1317
        Document document = null;
1318
        IWindow[] windows = application.getUIManager().getOrderedWindows();
1319
        IWindow window = null;
1320
        for (int i = 0; i < windows.length; i++) {
1321
            window = windows[i];
1322
            if (window instanceof SingletonWindow && window instanceof IDocumentWindow) {
1323
                // Cogemos no la primera ventana, si no la primera
1324
                // ventana de tipo documento (SingletonWindow).
1325
                // Y por si las mosca no es un documento, atrapamos
1326
                // los errores y continuamos si no puede hacer un cast
1327
                // del Model a Document
1328
                try {
1329
                    document = (Document) ((SingletonWindow) window).getWindowModel();
1330
                    if (documentClass == null) {
1331
                        return document;
1332
                    }
1333
                    if (documentClass.isAssignableFrom(document.getClass())) {
1334
                        return document;
1335
                    }
1336
                    if( document instanceof DocumentsContainer ) {
1337
                        Document subdoc = ((DocumentsContainer)document).getActiveDocument(documentClass);
1338
                        return subdoc;
1339
                    }
1340

    
1341
                } catch (ClassCastException e) {
1342
                    // Do nothing, skip this window
1343
                }
1344
            }
1345
        }
1346
        return null;
1347
    }
1348

    
1349
    @Override
1350
    public void addObserver(Observer o) {
1351
        observableHelper.addObserver(o);
1352
    }
1353

    
1354
    @Override
1355
    public void deleteObserver(Observer o) {
1356
        observableHelper.deleteObserver(o);
1357
    }
1358

    
1359
    @Override
1360
    public void deleteObservers() {
1361
        observableHelper.deleteObservers();
1362
    }
1363

    
1364
    private ProjectNotification notifyObservers(int type) {
1365
        return notifyObservers(new DefaultProjectNotification(type));
1366
    }
1367

    
1368
    private ProjectNotification notifyObservers(int type, Document document) {
1369
        return notifyObservers(new DefaultProjectNotification(type, document));
1370
    }
1371
    
1372
    private ProjectNotification notifyObservers(int type, PersistentState state) {
1373
        return notifyObservers(new DefaultProjectNotification(type, state));
1374
    }
1375

    
1376
    private ProjectNotification notifyObservers(int type, File file) {
1377
        return notifyObservers(new DefaultProjectNotification(type, file));
1378
    }
1379

    
1380
    private ProjectNotification notifyObservers(ProjectNotification notifycation) {
1381
        try {
1382
            observableHelper.notifyObservers(this, notifycation);
1383
        } catch (Exception ex) {
1384
            LOG.info("Can't notify observers", ex);
1385
        }
1386
        return notifycation;
1387
    }
1388
    
1389
    class DefaultProjectNotification implements ProjectNotification {
1390

    
1391
        private int type;
1392
        private Document document;
1393
        private File file;
1394
        private boolean processCanceled = false;
1395
        private PersistentState state = null;
1396

    
1397
        DefaultProjectNotification(int type) {
1398
            this.type = type;
1399
        }
1400

    
1401
        DefaultProjectNotification(int type, Document document) {
1402
            this(type);
1403
            this.document = document;
1404
        }
1405

    
1406
        DefaultProjectNotification(int type, File file) {
1407
            this(type);
1408
            this.file = file;
1409
        }
1410

    
1411
        DefaultProjectNotification(int type, PersistentState state) {
1412
            this(type);
1413
            this.state = state;
1414
        }
1415

    
1416
        @Override
1417
        public int getNotificationType() {
1418
            return type;
1419
        }
1420

    
1421
        @Override
1422
        public Document getDocument() {
1423
            return document;
1424
        }
1425

    
1426
        @Override
1427
        public void cancelProcess() {
1428
            processCanceled = true;
1429
        }
1430

    
1431
        @Override
1432
        public boolean isProcessCanceled() {
1433
            return processCanceled;
1434
        }
1435

    
1436
        @Override
1437
        public File getFile() {
1438
            return file;
1439
        }
1440

    
1441
        @Override
1442
        public PersistentState getState() {
1443
            return this.state;
1444
        }
1445

    
1446
        @Override
1447
        public Project getProject() {
1448
            return DefaultProject.this;
1449
        }
1450
    }
1451

    
1452
}