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 @ 44443

History | View | Annotate | Download (45.3 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.BufferedImage;
27
import java.beans.PropertyChangeEvent;
28
import java.beans.PropertyChangeListener;
29
import java.beans.PropertyChangeSupport;
30
import java.io.File;
31
import java.io.FileInputStream;
32
import java.io.FileNotFoundException;
33
import java.io.FileOutputStream;
34
import java.io.IOException;
35
import java.io.InputStream;
36
import java.io.OutputStream;
37
import java.io.Serializable;
38
import java.text.DateFormat;
39
import java.text.MessageFormat;
40
import java.text.SimpleDateFormat;
41
import java.util.ArrayList;
42
import java.util.Collections;
43
import java.util.Date;
44
import java.util.HashMap;
45
import java.util.HashSet;
46
import java.util.Iterator;
47
import java.util.List;
48
import java.util.Map;
49
import java.util.Set;
50
import java.util.zip.ZipEntry;
51
import java.util.zip.ZipException;
52
import java.util.zip.ZipFile;
53
import java.util.zip.ZipOutputStream;
54
import javax.imageio.ImageIO;
55
import org.apache.commons.io.FileUtils;
56
import org.apache.commons.io.FilenameUtils;
57
import org.apache.commons.io.IOUtils;
58

    
59
import org.cresques.cts.IProjection;
60

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

    
93
import org.slf4j.Logger;
94
import org.slf4j.LoggerFactory;
95

    
96
/**
97
 * Clase que representa un proyecto de gvSIG
98
 *
99
 */
100
public class DefaultProject implements Serializable, PropertyChangeListener,
101
        Project {
102

    
103
    private Logger LOG = LoggerFactory.getLogger(DefaultProject.class);
104
    /**
105
     * @deprecated see ApplicationLocator.getManager().getVersion()
106
     */
107
    public static String VERSION = Version.format();
108

    
109
    private ExtendedPropertiesHelper propertiesHelper = new ExtendedPropertiesHelper();
110

    
111
    private ObservableHelper observableHelper = new ObservableHelper();
112

    
113
    /**
114
     *
115
     */
116
    private static final long serialVersionUID = -4449622027521773178L;
117

    
118
    private static final Logger logger = LoggerFactory.getLogger(Project.class);
119

    
120
    private static ProjectPreferences preferences = new ProjectPreferences();
121

    
122
    /**
123
     * Index used by the generator of unique names of documents.
124
     */
125
    private Map<String, Integer> nextDocumentIndexByType = new HashMap<String, Integer>();
126

    
127
    private PropertyChangeSupport change;
128

    
129
    private boolean modified = false;
130

    
131
    private String name = null;
132

    
133
    private String creationDate = null;
134

    
135
    private String modificationDate = null;
136

    
137
    private String owner = null;
138

    
139
    private String comments = null;
140

    
141
    private Color selectionColor = null;
142

    
143
    private List<Document> documents = null;
144

    
145
    private List<ProjectExtent> extents = null;
146

    
147
    private IProjection projection;
148

    
149
    private File fname = null;
150

    
151
    private Set<String> unloadedObjects;
152
    private PersistenceException loadErrors = null;
153

    
154
    /**
155
     * Creates a new Project object.
156
     */
157
    DefaultProject() {
158
        this.change = new PropertyChangeSupport(this);
159
        this.clean();
160
        ProjectManager.getInstance().notifyProjectEvent(new NewProjectEvent() {
161
            @Override
162
            public Project getProject() {
163
                return DefaultProject.this;
164
            }
165
        });
166
    }
167

    
168
    protected void clean() {
169
        this.owner = "";
170
        this.comments = "";
171
        this.name = PluginServices.getText(this, "untitled");
172
        this.creationDate = DateFormat.getDateInstance().format(new Date());
173
        this.modificationDate = this.creationDate;
174

    
175
        this.documents = new ArrayList<Document>();
176
        this.extents = new ArrayList<ProjectExtent>();
177

    
178
        this.setSelectionColor(getPreferences().getDefaultSelectionColor());
179

    
180
        this.projection = null; // se inicializa en el getProjection()
181
    }
182

    
183
    public static ProjectPreferences getPreferences() {
184
        return preferences;
185
    }
186

    
187
    @Override
188
    public void propertyChange(PropertyChangeEvent evt) {
189
        change.firePropertyChange(evt);
190
    }
191

    
192
    @Override
193
    public synchronized void addPropertyChangeListener(
194
            PropertyChangeListener arg0) {
195
        change.addPropertyChangeListener(arg0);
196
    }
197

    
198
    @Override
199
    public void removePropertyChangeListener(PropertyChangeListener listener) {
200
        change.removePropertyChangeListener(listener);
201
    }
202
    
203
    /**
204
     * Return the creation date of the project
205
     *
206
     * @return
207
     */
208
    @Override
209
    public String getCreationDate() {
210
        return creationDate;
211
    }
212

    
213
    protected void setCreationDate(String creationDate) {
214
        this.creationDate = creationDate;
215
        change.firePropertyChange("setCreationDate", null, null);
216
    }
217

    
218
    @Override
219
    public Document createDocument(String type) {
220
        logger.info("createDocument('{}')", type);
221
        return ProjectManager.getInstance().createDocument(type);
222
    }
223

    
224
    /**
225
     * Return the name of the project
226
     *
227
     * @return
228
     */
229
    @Override
230
    public String getName() {
231
        return name;
232
    }
233

    
234
    /**
235
     * Set the name of he project.
236
     *
237
     * @param string
238
     */
239
    @Override
240
    public void setName(String name) {
241
        this.name = name;
242
        change.firePropertyChange("setName", null, null);
243
    }
244

    
245
    /**
246
     * Return the comments associateds with the project
247
     *
248
     * @return comments
249
     */
250
    @Override
251
    public String getComments() {
252
        return comments;
253
    }
254

    
255
    /**
256
     * Set the comments associateds with the project
257
     *
258
     * @param comments as string
259
     */
260
    @Override
261
    public void setComments(String string) {
262
        comments = string;
263
        change.firePropertyChange("setComments", null, null);
264
    }
265

    
266
    /**
267
     * Retuen the modification date of the project.
268
     *
269
     * @return modification date as string
270
     */
271
    @Override
272
    public String getModificationDate() {
273
        return modificationDate;
274
    }
275

    
276
    protected void setModificationDate(String string) {
277
        modificationDate = string;
278
        change.firePropertyChange("setModificationDate", null, null);
279
    }
280

    
281
    /**
282
     * Return the author of the project,
283
     *
284
     * @return author as string
285
     */
286
    @Override
287
    public String getOwner() {
288
        return owner;
289
    }
290

    
291
    /**
292
     * Sets the author of the project
293
     *
294
     * @param author name as string
295
     */
296
    @Override
297
    public void setOwner(String owner) {
298
        this.owner = owner;
299
        change.firePropertyChange("setOwner", null, null);
300
    }
301

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

    
315
    /**
316
     * Sets the selecction color
317
     *
318
     * @param selection color as string
319
     */
320
    @Override
321
    public void setSelectionColor(String selectionColor) {
322
        this.setSelectionColor(StringUtilities.string2Color(selectionColor));
323
    }
324

    
325
    /**
326
     * Sets the selecction color
327
     *
328
     * @param selection color as Color
329
     */
330
    @Override
331
    public void setSelectionColor(Color selectionColor) {
332
        this.selectionColor = selectionColor;
333
        MapContext.setSelectionColor(selectionColor);
334
        change.firePropertyChange("selectionColor", null, selectionColor);
335
    }
336

    
337
    @Override
338
    public IProjection getProjection() {
339
        if (projection == null) {
340
            projection = getPreferences().getDefaultProjection();
341
        }
342
        return projection;
343
    }
344

    
345
    @Override
346
    public void setProjection(IProjection projection) {
347
        this.projection = projection;
348
    }
349

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

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

    
378
    /**
379
     * Return a list of documents in the project.
380
     *
381
     * @return documents as List of IProjectDocument
382
     */
383
    @Override
384
    public List<Document> getDocuments() {
385
        return Collections.unmodifiableList(documents);
386
    }
387

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

    
408
    /**
409
     * Adds a document to the project
410
     *
411
     * @param document as Document
412
     */
413
    @Override
414
    public void add(Document document) {
415
        this.addDocument(document);
416
    }
417

    
418
    @Override
419
    public void addDocument(Document document) {
420
        logger.info("add('{}')", document.toString());
421

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

    
436
    @Override
437
    public void remove(Document doc) {
438
        this.removeDocument(doc);
439
    }
440

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

    
454
    @Override
455
    public Iterator<Document> iterator() {
456
        return documents.iterator();
457
    }
458

    
459
    @Override
460
    public boolean isEmpty() {
461
        return documents.isEmpty();
462
    }
463

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

    
485
        throw new RuntimeException(MessageFormat.format(
486
                "The layer '{1}' is not in a view", layer.getName()));
487
    }
488

    
489
    private boolean isView(FLayers layers, FLayer layer) {
490
        for (int i = 0; i < layers.getLayersCount(); i++) {
491
            if (layers.getLayer(i) instanceof FLayers) {
492
                if (isView((FLayers) layers.getLayer(i), layer)) {
493
                    return true;
494
                }
495
            }
496
            if (layers.getLayer(i) == layer) {
497
                return true;
498
            }
499
        }
500
        return false;
501
    }
502

    
503
    @Override
504
    public void addExtent(ProjectExtent arg1) {
505
        extents.add(arg1);
506
        change.firePropertyChange("addExtent", null, null);
507
    }
508

    
509
    @Override
510
    public ProjectExtent removeExtent(int arg0) {
511
        change.firePropertyChange("delExtent", null, null);
512
        return extents.remove(arg0);
513
    }
514

    
515
    @Override
516
    public ProjectExtent[] getExtents() {
517
        return (ProjectExtent[]) extents.toArray(new ProjectExtent[0]);
518
    }
519

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

    
551
    @Override
552
    public Document getDocument(String name) {
553
        return this.getDocument(name, null);
554
    }
555

    
556
    public String getUniqueNameForDocument(String type, String name) {
557
        Document document = getDocument(name, type);
558
        if (document == null) {
559
            return name;
560
        }
561

    
562
        String newName = null;
563
        int num = this.getNextDocumentIndex(type);
564
        while (document != null) {
565
            newName = name + " - " + num++;
566
            document = getDocument(newName, type);
567
        }
568
        this.setNextDocumentIndex(type, num);
569
        return newName;
570
    }
571

    
572
    private int getNextDocumentIndex(String type) {
573
        if (nextDocumentIndexByType.get(type) == null) {
574
            nextDocumentIndexByType.put(type, new Integer(1));
575
            return 1;
576
        }
577
        return nextDocumentIndexByType.get(type).intValue();
578
    }
579

    
580
    private void setNextDocumentIndex(String type, int newIndex) {
581
        if (nextDocumentIndexByType.get(type) == null) {
582
            nextDocumentIndexByType.put(type, new Integer(newIndex));
583
        } else {
584
            nextDocumentIndexByType.put(type, new Integer(newIndex));
585
        }
586
    }
587

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

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

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

    
649
            zout.putNextEntry(new ZipEntry("preview.jpg"));
650
            try {
651
                ImageIO.write(preview, "jpg", zout);
652
            } catch (IOException ex) {
653
                LOG.warn("Can't save preview image'.", ex);
654
            }
655
            IOUtils.closeQuietly(zout);
656
            IOUtils.closeQuietly(fout);
657

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

    
672
        }
673
    }
674
    
675
    @Override
676
    public void saveState(OutputStream out, File rootFolder)
677
            throws PersistenceException {
678
        if (notifyObservers(ProjectNotification.BEFORE_SAVE_TO_STREAM, rootFolder).isProcessCanceled()) {
679
            return;
680
        }
681
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
682
        PersistentState state = null;
683
        state = manager.getState(this, true);
684
        try {
685
            if (rootFolder != null) {
686
                state.relativizeFiles(rootFolder);
687
            }
688
        } catch (Exception ex) {
689
            state.getContext().addError(ex);
690
        }
691
        manager.saveState(state, out, true);
692
        if (state.getContext().getErrors() != null) {
693
            createProjectCopy(true);
694
            throw state.getContext().getErrors();
695
        }
696
        this.fname = null;
697
        notifyObservers(ProjectNotification.AFTER_SAVE_TO_STREAM, rootFolder);
698
    }
699
    
700
    private File getNameFileForProject(File actualProjectFile, boolean error) {
701
        // if there is a error, creates a file with a unique timecode
702
        // not error: return path with .gvsproj.bak extension that could exists
703
               
704
        if (actualProjectFile==null) {
705
            LOG.info("Doesn't have previous project to create a bak");
706
            return null;
707
        }
708
        String noExt = FilenameUtils.removeExtension(actualProjectFile.getAbsolutePath());
709
        String timeCode = new SimpleDateFormat("_MMddHHmmss").format(new Date());
710
        String ext = ".gvsproj.bak";
711

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

    
764
    @Deprecated
765
    @Override
766
    public void loadState(InputStream in) {
767
        loadState(in, null);
768
    }
769

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

    
790
        } catch (PersistenceException e) {
791
            LOG.info("Can't load project to stream", e);
792
        }
793
        this.fname = null;
794
        notifyObservers(ProjectNotification.AFTER_LOAD_FROM_STREAM, rootFolder);
795

    
796
    }
797

    
798

    
799

    
800
    /**
801
     * @param context
802
     * @return
803
     */
804
    private Set<String> getUnloadedObjects(PersistentContext context) {
805
        Set unloadedObjects = new HashSet();
806

    
807
        Iterator statesIterator = context.iterator();
808
        String className = null;
809

    
810
        while (statesIterator.hasNext()) {
811
            PersistentState aState = (PersistentState) statesIterator.next();
812
            try {
813
                className = aState.getTheClassName();
814
                DynStruct definition = aState.getDefinition();
815
                if (definition == null) {
816
                    unloadedObjects.add(className);
817
                }
818
            } catch (Throwable e) {
819
                // do nothing
820
            }
821
        }
822

    
823
        if(unloadedObjects.isEmpty()){
824
            return null;
825
        }
826

    
827
        return unloadedObjects;
828
    }
829

    
830
    @Override
831
    public Set<String> getUnloadedObjects() {
832
        return this.unloadedObjects;
833
    }
834

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

    
860
    //@SuppressWarnings("unchecked")
861
    @Override
862
    public void loadFromState(PersistentState state)
863
            throws PersistenceException {
864
        this.clean();
865
        PersistentContext context = state.getContext();
866
        
867
        notifyObservers(ProjectNotification.BEFORE_LOAD_FROM_STATE, state);
868

    
869
        this.setComments(state.getString("comments"));
870
        this.setCreationDate(state.getString("creationDate"));
871
        this.setModificationDate(state.getString("modificationDate"));
872
        this.setName(state.getString("name"));
873
        this.setOwner(state.getString("owner"));
874
        this.setSelectionColor((Color) state.get("selectionColor"));
875
        this.setProjection((IProjection) state.get("projection"));
876

    
877
        this.propertiesHelper = (ExtendedPropertiesHelper) state.get("propertiesHelper");
878

    
879
        List<ProjectExtent> extents = (List<ProjectExtent>) state
880
                .get("extents");
881
        for (int i = 0; i < extents.size(); i++) {
882
            this.addExtent(extents.get(i));
883
        }
884

    
885
        List<AbstractDocument> documents = (List<AbstractDocument>) state
886
                .get("documents");
887
        for (int i = 0; i < documents.size(); i++) {
888
            AbstractDocument doc = documents.get(i);
889
            if( doc != null ) {
890
                this.add(doc);
891
            }
892
        }
893

    
894
        try {
895
            List<DocumentWindowInfo> persistentWindows = (List<DocumentWindowInfo>) state.get("documentWindowsInformation");
896

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

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

    
931
    @Override
932
    public void saveToState(PersistentState state) throws PersistenceException {
933
        state.set("version", VERSION);
934
        state.set("comments", getComments());
935
        state.set("creationDate", this.getCreationDate());
936

    
937
        state.set("modificationDate", this.getModificationDate());
938
        state.set("name", this.getName());
939
        state.set("owner", this.getOwner());
940
        state.set("selectionColor", this.getSelectionColor());
941

    
942
        state.set("projection", this.getProjection());
943

    
944
        state.set("extents", this.extents);
945
        List<Document> docs = this.getDocuments();
946
        List<Document> noTempDocs = new ArrayList<>();
947
        for (Document document : docs) {
948
            if(!document.isTemporary()){
949
                noTempDocs.add(document);
950
            }
951
        }
952
        state.set("documents", noTempDocs);
953

    
954
        state.set("propertiesHelper",propertiesHelper);
955

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

    
974
    }
975

    
976
    @Override
977
    public Object getProperty(Object key) {
978
        return this.propertiesHelper.getProperty(key);
979
    }
980

    
981
    @Override
982
    public void setProperty(Object key, Object obj) {
983
        this.propertiesHelper.setProperty(key, obj);
984
    }
985

    
986
    @Override
987
    public Map getExtendedProperties() {
988
        return this.propertiesHelper.getExtendedProperties();
989
    }
990

    
991
    public static class DocumentWindowInfo implements Persistent {
992

    
993
        public static final String PERSISTENCE_DEFINITION_NAME = "DocumentWindowInfo";
994

    
995
        private WindowInfo windowInfo;
996
        private String documentType;
997
        private String documentName;
998

    
999
        public DocumentWindowInfo() {
1000
        }
1001

    
1002
        DocumentWindowInfo(WindowInfo wi, String docType, String docName) {
1003
            windowInfo = wi;
1004
            documentType = docType;
1005
            documentName = docName;
1006
        }
1007

    
1008
        public WindowInfo getWindowInfo() {
1009
            return windowInfo;
1010
        }
1011

    
1012
        public String getDocumentType() {
1013
            return documentType;
1014
        }
1015

    
1016
        public String getDocumentName() {
1017
            return documentName;
1018
        }
1019

    
1020
        public void saveToState(PersistentState state)
1021
                throws PersistenceException {
1022
            state.set("windowInfo", this.windowInfo);
1023
            state.set("documentType", this.documentType);
1024
            state.set("documentName", this.documentName);
1025
        }
1026

    
1027
        public void loadFromState(PersistentState state)
1028
                throws PersistenceException {
1029
            this.windowInfo = (WindowInfo) state.get("windowInfo");
1030
            this.documentType = state.getString("documentType");
1031
            this.documentName = state.getString("documentName");
1032
        }
1033

    
1034
        public static void registerPersistent() {
1035
            PersistenceManager manager = ToolsLocator.getPersistenceManager();
1036
            DynStruct definition = manager.getDefinition(PERSISTENCE_DEFINITION_NAME);
1037
            if (definition == null) {
1038
                definition = manager.addDefinition(
1039
                        DocumentWindowInfo.class,
1040
                        PERSISTENCE_DEFINITION_NAME,
1041
                        "DocumentWindowInfo persistence definition",
1042
                        null,
1043
                        null
1044
                );
1045
                definition.addDynFieldObject("windowInfo").setMandatory(true).setClassOfValue(WindowInfo.class);
1046
                definition.addDynFieldString("documentType").setMandatory(true);
1047
                definition.addDynFieldString("documentName").setMandatory(true);
1048
            }
1049

    
1050
        }
1051
    }
1052

    
1053
    public static void registerPersistent() {
1054
        AbstractDocument.registerPersistent();
1055
        DocumentWindowInfo.registerPersistent();
1056
        ProjectExtent.registerPersistent();
1057

    
1058
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
1059
        DynStruct definition = manager.addDefinition(DefaultProject.class,
1060
                "Project", "Project Persistence definition", null, null);
1061
        definition.addDynFieldString("version").setMandatory(true);
1062
        definition.addDynFieldString("comments").setMandatory(true);
1063
        definition.addDynFieldString("creationDate").setMandatory(true);
1064
        definition.addDynFieldString("modificationDate").setMandatory(true);
1065
        definition.addDynFieldString("name").setMandatory(true);
1066
        definition.addDynFieldString("owner").setMandatory(true);
1067

    
1068
        definition.addDynFieldObject("selectionColor")
1069
                .setClassOfValue(Color.class).setMandatory(true);
1070
        definition.addDynFieldObject("projection")
1071
                .setClassOfValue(IProjection.class).setMandatory(true);
1072

    
1073
        definition.addDynFieldList("extents")
1074
                .setClassOfItems(ProjectExtent.class).setMandatory(true);
1075

    
1076
        definition.addDynFieldList("documents").setClassOfItems(Document.class)
1077
                .setMandatory(true);
1078

    
1079
        definition.addDynFieldObject("projectWindowInfo").setClassOfValue(WindowInfo.class).setMandatory(false);
1080

    
1081
        definition.addDynFieldList("documentWindowsInformation").setClassOfItems(WindowInfo.class).setMandatory(false);
1082

    
1083

    
1084
        definition.addDynFieldObject("propertiesHelper").setClassOfValue(ExtendedPropertiesHelper.class)
1085
                        .setMandatory(false);
1086

    
1087
    }
1088

    
1089
    /**
1090
     * @deprecated use getPreferences().setDefaultSelectionColor()
1091
     */
1092
    public static void setDefaultSelectionColor(Color color) {
1093
        getPreferences().setDefaultSelectionColor(color);
1094
    }
1095

    
1096
    /**
1097
     * @deprecated use getPreferences().getDefaultSelectionColor()
1098
     */
1099
    public static Color getDefaultSelectionColor() {
1100
        return getPreferences().getDefaultSelectionColor();
1101
    }
1102

    
1103
    /**
1104
     * @deprecated use getPreferences().getDefaultMapUnits()
1105
     */
1106
    public static int getDefaultMapUnits() {
1107
        return getPreferences().getDefaultMapUnits();
1108
    }
1109

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

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

    
1124
    /**
1125
     * @deprecated use getPreferences().setDefaultMapUnits()
1126
     */
1127
    public static void setDefaultMapUnits(int mapUnits) {
1128
        getPreferences().setDefaultMapUnits(mapUnits);
1129
    }
1130

    
1131
    /**
1132
     * @deprecated use getPreferences().setDefaultDistanceUnits()
1133
     */
1134
    public static void setDefaultDistanceUnits(int distanceUnits) {
1135
        getPreferences().setDefaultDistanceUnits(distanceUnits);
1136
    }
1137

    
1138
    /**
1139
     * @deprecated use getPreferences().setDefaultDistanceArea()
1140
     */
1141
    public static void setDefaultDistanceArea(int distanceArea) {
1142
        getPreferences().setDefaultDistanceArea(distanceArea);
1143
    }
1144

    
1145
    /**
1146
     * @deprecated use getPreferences().setDefaultProjection()
1147
     */
1148
    public static void setDefaultProjection(IProjection defaultProjection) {
1149
        getPreferences().setDefaultProjection(defaultProjection);
1150
    }
1151

    
1152
    /**
1153
     * @deprecated use getPreferences().getDefaultProjection()
1154
     */
1155
    public static IProjection getDefaultProjection() {
1156
        return getPreferences().getDefaultProjection();
1157
    }
1158

    
1159
    /**
1160
     * @deprecated see {@link #setSelectionColor(String)}, to be remove in gvSIG
1161
     * 2.1.0
1162
     */
1163
    public void setColor(String color) {
1164
        this.setSelectionColor(StringUtilities.string2Color(color));
1165
    }
1166

    
1167
    /**
1168
     * Return the selection color
1169
     *
1170
     * @return selection color as string
1171
     * @deprecated use {@link #getSelectionColor()}
1172
     */
1173
    public String getColor() {
1174
        return StringUtilities.color2String(selectionColor);
1175
    }
1176

    
1177
    /**
1178
     * Return the list of views of the project
1179
     *
1180
     * @return views as ArrayList of ProjectDocument
1181
     *
1182
     * @deprecated see {@link #getDocumentsByType(String)}
1183
     */
1184
    public List<Document> getViews() {
1185
        return getDocuments(ViewManager.TYPENAME);
1186
    }
1187

    
1188
    /**
1189
     * Add a view to the project
1190
     *
1191
     * @deprecated see {@link #add(AbstractDocument)}
1192
     */
1193
    public void addView(DefaultViewDocument v) {
1194
        add(v);
1195
    }
1196

    
1197
    /**
1198
     * Remove a view of the project
1199
     *
1200
     * @param index of the view as integer
1201
     *
1202
     * @deprecated see {@link #remove(AbstractDocument)}
1203
     */
1204
    public void delView(int i) {
1205
        List<Document> list = getDocuments(ViewManager.TYPENAME);
1206
        remove(list.get(i));
1207
    }
1208

    
1209
    /**
1210
     * @deprecated see {@link #getDocument(String, String)}
1211
     */
1212
    public Document getProjectDocumentByName(String name, String type) {
1213
        return this.getDocument(name, type);
1214
    }
1215

    
1216
    /**
1217
     * @deprecated see {@link #getDocuments(String)}
1218
     */
1219
    public List<Document> getDocumentsByType(String type) {
1220
        return this.getDocuments(type);
1221
    }
1222

    
1223
    /**
1224
     * @deprecated aun por decidir que API darle al copy/paste
1225
     */
1226
    public String exportToXML(AbstractDocument[] selectedItems)
1227
            throws SaveException {
1228
        // FIXME jjdc:hay que decirdir que API darle al copy/paste
1229
        throw new UnsupportedOperationException("This method is not supported");
1230
    }
1231

    
1232
    /**
1233
     * @deprecated aun por decidir que API darle al copy/paste
1234
     */
1235
    public void importFromXML(String sourceString, String docType) {
1236
        // FIXME jjdc:hay que decirdir que API darle al copy/paste
1237
        throw new UnsupportedOperationException("This method is not supported");
1238
    }
1239

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

    
1248
    @Override
1249
    public boolean canImportDocuments(String data, String doctype) {
1250
        // TODO Auto-generated method stub
1251
        return false;
1252
    }
1253

    
1254
    @Override
1255
    public String exportDocumentsAsText(List<Document> documents) {
1256
        // TODO Auto-generated method stub
1257
        return null;
1258
    }
1259

    
1260
    @Override
1261
    public void importDocuments(String data, String doctype) {
1262
        // TODO Auto-generated method stub
1263

    
1264
    }
1265

    
1266
    @Override
1267
    public Document getActiveDocument() {
1268
        return this.getActiveDocument((Class<? extends Document>)null);
1269
    }
1270

    
1271
    @Override
1272
    public Document getActiveDocument(String documentTypeName) {
1273
        ApplicationManager application = ApplicationLocator.getManager();
1274

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

    
1299
                } catch (ClassCastException e) {
1300
                    // Do nothing, skip this window
1301
                }
1302
            }
1303
        }
1304
        return null;
1305
    }
1306

    
1307
    @Override
1308
    public Document getActiveDocument(Class<? extends Document> documentClass) {
1309
        ApplicationManager application = ApplicationLocator.getManager();
1310

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

    
1335
                } catch (ClassCastException e) {
1336
                    // Do nothing, skip this window
1337
                }
1338
            }
1339
        }
1340
        return null;
1341
    }
1342

    
1343
    @Override
1344
    public void addObserver(Observer o) {
1345
        observableHelper.addObserver(o);
1346
    }
1347

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

    
1353
    @Override
1354
    public void deleteObservers() {
1355
        observableHelper.deleteObservers();
1356
    }
1357

    
1358
    private ProjectNotification notifyObservers(int type) {
1359
        return notifyObservers(new DefaultProjectNotification(type));
1360
    }
1361

    
1362
    private ProjectNotification notifyObservers(int type, Document document) {
1363
        return notifyObservers(new DefaultProjectNotification(type, document));
1364
    }
1365
    
1366
    private ProjectNotification notifyObservers(int type, PersistentState state) {
1367
        return notifyObservers(new DefaultProjectNotification(type, state));
1368
    }
1369

    
1370
    private ProjectNotification notifyObservers(int type, File file) {
1371
        return notifyObservers(new DefaultProjectNotification(type, file));
1372
    }
1373

    
1374
    private ProjectNotification notifyObservers(ProjectNotification notifycation) {
1375
        try {
1376
            observableHelper.notifyObservers(this, notifycation);
1377
        } catch (Exception ex) {
1378
            LOG.info("Can't notify observers", ex);
1379
        }
1380
        return notifycation;
1381
    }
1382
    
1383
    class DefaultProjectNotification implements ProjectNotification {
1384

    
1385
        private int type;
1386
        private Document document;
1387
        private File file;
1388
        private boolean processCanceled = false;
1389
        private PersistentState state = null;
1390

    
1391
        DefaultProjectNotification(int type) {
1392
            this.type = type;
1393
        }
1394

    
1395
        DefaultProjectNotification(int type, Document document) {
1396
            this(type);
1397
            this.document = document;
1398
        }
1399

    
1400
        DefaultProjectNotification(int type, File file) {
1401
            this(type);
1402
            this.file = file;
1403
        }
1404

    
1405
        DefaultProjectNotification(int type, PersistentState state) {
1406
            this(type);
1407
            this.state = state;
1408
        }
1409

    
1410
        @Override
1411
        public int getNotificationType() {
1412
            return type;
1413
        }
1414

    
1415
        @Override
1416
        public Document getDocument() {
1417
            return document;
1418
        }
1419

    
1420
        @Override
1421
        public void cancelProcess() {
1422
            processCanceled = true;
1423
        }
1424

    
1425
        @Override
1426
        public boolean isProcessCanceled() {
1427
            return processCanceled;
1428
        }
1429

    
1430
        @Override
1431
        public File getFile() {
1432
            return file;
1433
        }
1434

    
1435
        @Override
1436
        public PersistentState getState() {
1437
            return this.state;
1438
        }
1439

    
1440
        @Override
1441
        public Project getProject() {
1442
            return DefaultProject.this;
1443
        }
1444
    }
1445

    
1446
}