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

History | View | Annotate | Download (45.3 KB)

1 40558 jjdelcerro
/**
2
 * gvSIG. Desktop Geographic Information System.
3 40435 jjdelcerro
 *
4 40558 jjdelcerro
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6 41148 jjdelcerro
 * 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 40558 jjdelcerro
 *
11 41148 jjdelcerro
 * 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 40558 jjdelcerro
 *
16 41148 jjdelcerro
 * 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 40558 jjdelcerro
 *
20 41148 jjdelcerro
 * For any additional information, do not hesitate to contact us at info AT
21
 * gvsig.com, or visit our website www.gvsig.com.
22 40435 jjdelcerro
 */
23
package org.gvsig.app.project;
24
25
import java.awt.Color;
26 43913 jjdelcerro
import java.awt.image.BufferedImage;
27 40435 jjdelcerro
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 43820 jjdelcerro
import java.io.IOException;
35 40435 jjdelcerro
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 44088 omartinez
import java.text.SimpleDateFormat;
41 40435 jjdelcerro
import java.util.ArrayList;
42
import java.util.Collections;
43
import java.util.Date;
44
import java.util.HashMap;
45 42711 fdiaz
import java.util.HashSet;
46 40435 jjdelcerro
import java.util.Iterator;
47
import java.util.List;
48
import java.util.Map;
49 42711 fdiaz
import java.util.Set;
50 43913 jjdelcerro
import java.util.zip.ZipEntry;
51 43820 jjdelcerro
import java.util.zip.ZipException;
52
import java.util.zip.ZipFile;
53 43913 jjdelcerro
import java.util.zip.ZipOutputStream;
54
import javax.imageio.ImageIO;
55 44088 omartinez
import org.apache.commons.io.FileUtils;
56
import org.apache.commons.io.FilenameUtils;
57 43913 jjdelcerro
import org.apache.commons.io.IOUtils;
58 42711 fdiaz
59 40435 jjdelcerro
import org.cresques.cts.IProjection;
60 42711 fdiaz
61 40435 jjdelcerro
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 43879 jjdelcerro
import org.gvsig.app.project.ProjectManager.NewProjectEvent;
71 40435 jjdelcerro
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 41350 jjdelcerro
import org.gvsig.fmap.mapcontext.layers.ExtendedPropertiesHelper;
80 40435 jjdelcerro
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 42711 fdiaz
import org.gvsig.tools.persistence.PersistentContext;
89 40435 jjdelcerro
import org.gvsig.tools.persistence.PersistentState;
90
import org.gvsig.tools.persistence.exception.PersistenceException;
91
import org.gvsig.utils.StringUtilities;
92 42711 fdiaz
93 40435 jjdelcerro
import org.slf4j.Logger;
94
import org.slf4j.LoggerFactory;
95
96
/**
97
 * Clase que representa un proyecto de gvSIG
98 41148 jjdelcerro
 *
99 40435 jjdelcerro
 */
100
public class DefaultProject implements Serializable, PropertyChangeListener,
101 41148 jjdelcerro
        Project {
102 40435 jjdelcerro
103 41148 jjdelcerro
    private Logger LOG = LoggerFactory.getLogger(DefaultProject.class);
104
    /**
105
     * @deprecated see ApplicationLocator.getManager().getVersion()
106
     */
107
    public static String VERSION = Version.format();
108 40435 jjdelcerro
109 41350 jjdelcerro
    private ExtendedPropertiesHelper propertiesHelper = new ExtendedPropertiesHelper();
110 42088 fdiaz
111 40435 jjdelcerro
    private ObservableHelper observableHelper = new ObservableHelper();
112
113 41148 jjdelcerro
    /**
114
     *
115
     */
116
    private static final long serialVersionUID = -4449622027521773178L;
117 40435 jjdelcerro
118 41148 jjdelcerro
    private static final Logger logger = LoggerFactory.getLogger(Project.class);
119 40435 jjdelcerro
120 41148 jjdelcerro
    private static ProjectPreferences preferences = new ProjectPreferences();
121 40435 jjdelcerro
122 41148 jjdelcerro
    /**
123
     * Index used by the generator of unique names of documents.
124
     */
125
    private Map<String, Integer> nextDocumentIndexByType = new HashMap<String, Integer>();
126 40435 jjdelcerro
127 41148 jjdelcerro
    private PropertyChangeSupport change;
128 40435 jjdelcerro
129 41148 jjdelcerro
    private boolean modified = false;
130 40435 jjdelcerro
131 41148 jjdelcerro
    private String name = null;
132 40435 jjdelcerro
133 41148 jjdelcerro
    private String creationDate = null;
134 40435 jjdelcerro
135 41148 jjdelcerro
    private String modificationDate = null;
136 40435 jjdelcerro
137 41148 jjdelcerro
    private String owner = null;
138 40435 jjdelcerro
139 41148 jjdelcerro
    private String comments = null;
140 40435 jjdelcerro
141 41148 jjdelcerro
    private Color selectionColor = null;
142 40435 jjdelcerro
143 41148 jjdelcerro
    private List<Document> documents = null;
144 40435 jjdelcerro
145 41148 jjdelcerro
    private List<ProjectExtent> extents = null;
146 40435 jjdelcerro
147 41148 jjdelcerro
    private IProjection projection;
148 40435 jjdelcerro
149 42466 jjdelcerro
    private File fname = null;
150 42711 fdiaz
151
    private Set<String> unloadedObjects;
152 43439 jjdelcerro
    private PersistenceException loadErrors = null;
153 42711 fdiaz
154 41148 jjdelcerro
    /**
155
     * Creates a new Project object.
156
     */
157
    DefaultProject() {
158
        this.change = new PropertyChangeSupport(this);
159
        this.clean();
160 43879 jjdelcerro
        ProjectManager.getInstance().notifyProjectEvent(new NewProjectEvent() {
161
            @Override
162
            public Project getProject() {
163
                return DefaultProject.this;
164
            }
165
        });
166 41148 jjdelcerro
    }
167 40435 jjdelcerro
168 41148 jjdelcerro
    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 40435 jjdelcerro
175 41148 jjdelcerro
        this.documents = new ArrayList<Document>();
176
        this.extents = new ArrayList<ProjectExtent>();
177 40435 jjdelcerro
178 41148 jjdelcerro
        this.setSelectionColor(getPreferences().getDefaultSelectionColor());
179 40435 jjdelcerro
180 41148 jjdelcerro
        this.projection = null; // se inicializa en el getProjection()
181
    }
182 40435 jjdelcerro
183 41148 jjdelcerro
    public static ProjectPreferences getPreferences() {
184
        return preferences;
185
    }
186 40435 jjdelcerro
187 44088 omartinez
    @Override
188 41148 jjdelcerro
    public void propertyChange(PropertyChangeEvent evt) {
189
        change.firePropertyChange(evt);
190
    }
191 40435 jjdelcerro
192 43913 jjdelcerro
    @Override
193 41148 jjdelcerro
    public synchronized void addPropertyChangeListener(
194
            PropertyChangeListener arg0) {
195
        change.addPropertyChangeListener(arg0);
196
    }
197 40435 jjdelcerro
198 43913 jjdelcerro
    @Override
199
    public void removePropertyChangeListener(PropertyChangeListener listener) {
200
        change.removePropertyChangeListener(listener);
201
    }
202
203 41148 jjdelcerro
    /**
204
     * Return the creation date of the project
205
     *
206
     * @return
207
     */
208 44088 omartinez
    @Override
209 41148 jjdelcerro
    public String getCreationDate() {
210
        return creationDate;
211
    }
212 40435 jjdelcerro
213 41148 jjdelcerro
    protected void setCreationDate(String creationDate) {
214
        this.creationDate = creationDate;
215
        change.firePropertyChange("setCreationDate", null, null);
216
    }
217 40435 jjdelcerro
218 44088 omartinez
    @Override
219 40435 jjdelcerro
    public Document createDocument(String type) {
220 41148 jjdelcerro
        logger.info("createDocument('{}')", type);
221 40435 jjdelcerro
        return ProjectManager.getInstance().createDocument(type);
222
    }
223
224
    /**
225
     * Return the name of the project
226 41148 jjdelcerro
     *
227 40435 jjdelcerro
     * @return
228
     */
229 44088 omartinez
    @Override
230 40435 jjdelcerro
    public String getName() {
231
        return name;
232
    }
233
234 41148 jjdelcerro
    /**
235
     * Set the name of he project.
236
     *
237
     * @param string
238
     */
239 44088 omartinez
    @Override
240 41148 jjdelcerro
    public void setName(String name) {
241
        this.name = name;
242
        change.firePropertyChange("setName", null, null);
243
    }
244 40435 jjdelcerro
245 41148 jjdelcerro
    /**
246
     * Return the comments associateds with the project
247
     *
248
     * @return comments
249
     */
250 44088 omartinez
    @Override
251 41148 jjdelcerro
    public String getComments() {
252
        return comments;
253
    }
254 40435 jjdelcerro
255 41148 jjdelcerro
    /**
256
     * Set the comments associateds with the project
257
     *
258
     * @param comments as string
259
     */
260 44088 omartinez
    @Override
261 41148 jjdelcerro
    public void setComments(String string) {
262
        comments = string;
263
        change.firePropertyChange("setComments", null, null);
264
    }
265 40435 jjdelcerro
266 41148 jjdelcerro
    /**
267
     * Retuen the modification date of the project.
268
     *
269
     * @return modification date as string
270
     */
271 44088 omartinez
    @Override
272 41148 jjdelcerro
    public String getModificationDate() {
273
        return modificationDate;
274
    }
275 40435 jjdelcerro
276 41148 jjdelcerro
    protected void setModificationDate(String string) {
277
        modificationDate = string;
278
        change.firePropertyChange("setModificationDate", null, null);
279
    }
280 40435 jjdelcerro
281 41148 jjdelcerro
    /**
282
     * Return the author of the project,
283
     *
284
     * @return author as string
285
     */
286 44088 omartinez
    @Override
287 41148 jjdelcerro
    public String getOwner() {
288
        return owner;
289
    }
290 40435 jjdelcerro
291 41148 jjdelcerro
    /**
292
     * Sets the author of the project
293
     *
294
     * @param author name as string
295
     */
296 44088 omartinez
    @Override
297 41148 jjdelcerro
    public void setOwner(String owner) {
298
        this.owner = owner;
299
        change.firePropertyChange("setOwner", null, null);
300
    }
301 40435 jjdelcerro
302 41148 jjdelcerro
    /**
303
     * Obtiene el color de selecci�n que se usar� en el proyecto
304
     *
305
     * @return
306
     */
307 44088 omartinez
    @Override
308 41148 jjdelcerro
    public Color getSelectionColor() {
309
        if (selectionColor == null) {
310
            selectionColor = getPreferences().getDefaultSelectionColor();
311
        }
312
        return selectionColor;
313
    }
314 40435 jjdelcerro
315 41148 jjdelcerro
    /**
316
     * Sets the selecction color
317
     *
318
     * @param selection color as string
319
     */
320 44088 omartinez
    @Override
321 41148 jjdelcerro
    public void setSelectionColor(String selectionColor) {
322
        this.setSelectionColor(StringUtilities.string2Color(selectionColor));
323
    }
324 40435 jjdelcerro
325 41148 jjdelcerro
    /**
326
     * Sets the selecction color
327
     *
328
     * @param selection color as Color
329
     */
330 44088 omartinez
    @Override
331 41148 jjdelcerro
    public void setSelectionColor(Color selectionColor) {
332
        this.selectionColor = selectionColor;
333
        MapContext.setSelectionColor(selectionColor);
334
        change.firePropertyChange("selectionColor", null, selectionColor);
335
    }
336 40435 jjdelcerro
337 44088 omartinez
    @Override
338 41148 jjdelcerro
    public IProjection getProjection() {
339
        if (projection == null) {
340
            projection = getPreferences().getDefaultProjection();
341
        }
342
        return projection;
343
    }
344 40435 jjdelcerro
345 44088 omartinez
    @Override
346 41148 jjdelcerro
    public void setProjection(IProjection projection) {
347
        this.projection = projection;
348
    }
349 40435 jjdelcerro
350 41148 jjdelcerro
    /**
351
     * Sets the modified state of project.
352
     *
353
     * Can't set to not modified.
354
     *
355
     * @param modified as boolean
356
     */
357 44088 omartinez
    @Override
358 41148 jjdelcerro
    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 40435 jjdelcerro
368 44088 omartinez
    @Override
369 41148 jjdelcerro
    public boolean hasChanged() {
370 40435 jjdelcerro
                // we return true if the project is not empty (until we have a better
371 41148 jjdelcerro
        // method...)
372
        if ((this.getDocuments().size() != 0) || modified) {
373
            return true;
374
        }
375
        return false;
376
    }
377 40435 jjdelcerro
378 41148 jjdelcerro
    /**
379
     * Return a list of documents in the project.
380
     *
381
     * @return documents as List of IProjectDocument
382
     */
383 44088 omartinez
    @Override
384 41148 jjdelcerro
    public List<Document> getDocuments() {
385
        return Collections.unmodifiableList(documents);
386
    }
387 40435 jjdelcerro
388 41148 jjdelcerro
    /**
389
     * Return a list with all documents of especified type.
390
     *
391
     * @param type of document
392
     *
393
     * @return List of IProjectDocument
394
     */
395 44088 omartinez
    @Override
396 41148 jjdelcerro
    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 40435 jjdelcerro
408 41148 jjdelcerro
    /**
409
     * Adds a document to the project
410
     *
411 41248 jjdelcerro
     * @param document as Document
412 41148 jjdelcerro
     */
413 44088 omartinez
    @Override
414 41148 jjdelcerro
    public void add(Document document) {
415 41248 jjdelcerro
        this.addDocument(document);
416
    }
417 42088 fdiaz
418 44088 omartinez
    @Override
419 41248 jjdelcerro
    public void addDocument(Document document) {
420 40435 jjdelcerro
        logger.info("add('{}')", document.toString());
421
422 41148 jjdelcerro
        if (notifyObservers(ProjectNotification.BEFORE_ADD_DOCUMENT, document).isProcessCanceled()) {
423
            return;
424 40435 jjdelcerro
        }
425 41148 jjdelcerro
        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 40435 jjdelcerro
        change.firePropertyChange("addDocument", null, document);
434 41148 jjdelcerro
    }
435 40435 jjdelcerro
436 44088 omartinez
    @Override
437 41148 jjdelcerro
    public void remove(Document doc) {
438 41248 jjdelcerro
        this.removeDocument(doc);
439
    }
440 42088 fdiaz
441 44088 omartinez
    @Override
442 41248 jjdelcerro
    public void removeDocument(Document doc) {
443 40435 jjdelcerro
        logger.info("remove('{}')", doc.toString());
444 41148 jjdelcerro
        if (notifyObservers(ProjectNotification.BEFORE_REMOVE_DOCUMENT, doc).isProcessCanceled()) {
445
            return;
446 40435 jjdelcerro
        }
447 41148 jjdelcerro
        documents.remove(doc);
448
        this.setModified(true);
449 40435 jjdelcerro
        change.firePropertyChange("delDocument", doc, null);
450 41148 jjdelcerro
        doc.afterRemove();
451
        notifyObservers(ProjectNotification.AFTER_REMOVE_DOCUMENT, doc);
452
    }
453 40435 jjdelcerro
454 44088 omartinez
    @Override
455 41148 jjdelcerro
    public Iterator<Document> iterator() {
456
        return documents.iterator();
457
    }
458 40435 jjdelcerro
459 44088 omartinez
    @Override
460 41148 jjdelcerro
    public boolean isEmpty() {
461
        return documents.isEmpty();
462
    }
463 40435 jjdelcerro
464 41148 jjdelcerro
    /**
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 44088 omartinez
    @Override
475 41148 jjdelcerro
    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 40435 jjdelcerro
485 41148 jjdelcerro
        throw new RuntimeException(MessageFormat.format(
486
                "The layer '{1}' is not in a view", layer.getName()));
487
    }
488 40435 jjdelcerro
489 41148 jjdelcerro
    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 40435 jjdelcerro
503 44088 omartinez
    @Override
504 41148 jjdelcerro
    public void addExtent(ProjectExtent arg1) {
505
        extents.add(arg1);
506
        change.firePropertyChange("addExtent", null, null);
507
    }
508 40435 jjdelcerro
509 44088 omartinez
    @Override
510 41148 jjdelcerro
    public ProjectExtent removeExtent(int arg0) {
511
        change.firePropertyChange("delExtent", null, null);
512
        return extents.remove(arg0);
513
    }
514 40435 jjdelcerro
515 44088 omartinez
    @Override
516 41148 jjdelcerro
    public ProjectExtent[] getExtents() {
517
        return (ProjectExtent[]) extents.toArray(new ProjectExtent[0]);
518
    }
519 40435 jjdelcerro
520 41148 jjdelcerro
    /**
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 44088 omartinez
    @Override
531 41148 jjdelcerro
    public Document getDocument(String name, String type) {
532 42293 jjdelcerro
        if (type == null) {
533 41148 jjdelcerro
            for (int i = 0; i < documents.size(); i++) {
534
                Document document = documents.get(i);
535 42293 jjdelcerro
                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 41148 jjdelcerro
                if (type.equalsIgnoreCase(document.getTypeName())
543
                        && name.equalsIgnoreCase(document.getName())) {
544
                    return document;
545
                }
546
            }
547
        }
548
        return null;
549
    }
550 42711 fdiaz
551 44088 omartinez
    @Override
552 42293 jjdelcerro
    public Document getDocument(String name) {
553
        return this.getDocument(name, null);
554 42711 fdiaz
    }
555 40435 jjdelcerro
556 41148 jjdelcerro
    public String getUniqueNameForDocument(String type, String name) {
557
        Document document = getDocument(name, type);
558
        if (document == null) {
559
            return name;
560
        }
561 40435 jjdelcerro
562 41148 jjdelcerro
        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 40435 jjdelcerro
572 41148 jjdelcerro
    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 40435 jjdelcerro
580 41148 jjdelcerro
    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 40435 jjdelcerro
588 44088 omartinez
    @Override
589 41148 jjdelcerro
    public void saveState(File out) throws PersistenceException {
590
        FileOutputStream fout;
591
        if (notifyObservers(ProjectNotification.BEFORE_SAVE_TO_FILE, out).isProcessCanceled()) {
592
            return;
593 40435 jjdelcerro
        }
594 41148 jjdelcerro
        try {
595
            fout = new FileOutputStream(out);
596
            saveState(fout, new File(out.getParent()));
597
        } catch (FileNotFoundException e) {
598 44088 omartinez
            createProjectCopy(true);
599 41148 jjdelcerro
            throw new PersistenceException(e);
600
        }
601 43820 jjdelcerro
        if( !isValidZIP(out) ) {
602
            throw new PersistenceException(new ZipException());
603
        }
604 42466 jjdelcerro
        this.fname = out;
605 44088 omartinez
        //sacar copia al bak
606
        createProjectCopy(false);
607 41148 jjdelcerro
        notifyObservers(ProjectNotification.AFTER_SAVE_TO_FILE, out);
608
    }
609 40435 jjdelcerro
610 43820 jjdelcerro
    boolean isValidZIP(final File file) {
611
        ZipFile zipfile = null;
612
        try {
613
            zipfile = new ZipFile(file);
614
            return true;
615
        } catch (IOException e) {
616 44088 omartinez
            //sacar copia.
617 43820 jjdelcerro
            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 43913 jjdelcerro
    @Override
630 42466 jjdelcerro
    public File getFile() {
631
        return this.fname;
632
    }
633 42711 fdiaz
634 43913 jjdelcerro
    @Deprecated
635
    @Override
636 41148 jjdelcerro
    public void saveState(OutputStream out) throws PersistenceException {
637
        saveState(out, null);
638
    }
639 43913 jjdelcerro
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 40435 jjdelcerro
649 43913 jjdelcerro
            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 44088 omartinez
            if (file.exists()) {
663
                createProjectCopy(false);
664
            }
665 43913 jjdelcerro
        } catch (Exception ex) {
666 44088 omartinez
            createProjectCopy(true);
667
            throw new RuntimeException("Can't write project in '" + file.getAbsolutePath() + ".", ex);
668 43913 jjdelcerro
        } finally {
669
            IOUtils.closeQuietly(zout);
670 44088 omartinez
            IOUtils.closeQuietly(fout);
671
672 43913 jjdelcerro
        }
673
    }
674
675
    @Override
676 41148 jjdelcerro
    public void saveState(OutputStream out, File rootFolder)
677
            throws PersistenceException {
678 43913 jjdelcerro
        if (notifyObservers(ProjectNotification.BEFORE_SAVE_TO_STREAM, rootFolder).isProcessCanceled()) {
679 41148 jjdelcerro
            return;
680 40435 jjdelcerro
        }
681 41148 jjdelcerro
        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 43077 jjdelcerro
        manager.saveState(state, out, true);
692 41148 jjdelcerro
        if (state.getContext().getErrors() != null) {
693 44088 omartinez
            createProjectCopy(true);
694 41148 jjdelcerro
            throw state.getContext().getErrors();
695
        }
696 42466 jjdelcerro
        this.fname = null;
697 43913 jjdelcerro
        notifyObservers(ProjectNotification.AFTER_SAVE_TO_STREAM, rootFolder);
698 41148 jjdelcerro
    }
699 44088 omartinez
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 40435 jjdelcerro
712 44088 omartinez
        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 44204 omartinez
            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 44088 omartinez
            }
759
        }
760
761
        return bakProjectFile;
762
    }
763
764 43913 jjdelcerro
    @Deprecated
765 44088 omartinez
    @Override
766 41148 jjdelcerro
    public void loadState(InputStream in) {
767
        loadState(in, null);
768
    }
769 40435 jjdelcerro
770 41148 jjdelcerro
    public void loadState(InputStream in, File rootFolder) {
771 43913 jjdelcerro
        if (notifyObservers(ProjectNotification.BEFORE_LOAD_FROM_STREAM, rootFolder).isProcessCanceled()) {
772 41148 jjdelcerro
            return;
773
        }
774
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
775
        try {
776 43439 jjdelcerro
            PersistentContext context = manager.getNewContext();
777
            context.setCollectErrors(true);
778
            PersistentState state = manager.loadState(in, context);
779 41148 jjdelcerro
            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 42711 fdiaz
            this.unloadedObjects = getUnloadedObjects(state.getContext());
788 43439 jjdelcerro
            this.loadErrors = state.getContext().getErrors();
789 43879 jjdelcerro
790 41148 jjdelcerro
        } catch (PersistenceException e) {
791
            LOG.info("Can't load project to stream", e);
792
        }
793 42466 jjdelcerro
        this.fname = null;
794 43913 jjdelcerro
        notifyObservers(ProjectNotification.AFTER_LOAD_FROM_STREAM, rootFolder);
795 40435 jjdelcerro
796 41148 jjdelcerro
    }
797 40435 jjdelcerro
798 42711 fdiaz
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 43439 jjdelcerro
    @Override
831 42711 fdiaz
    public Set<String> getUnloadedObjects() {
832
        return this.unloadedObjects;
833
    }
834
835 43439 jjdelcerro
    @Override
836
    public List<Exception> getLoadErrors() {
837
        return this.loadErrors;
838
    }
839
840
    @Override
841 41148 jjdelcerro
    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 42466 jjdelcerro
            LOG.info("Can't load project to stream", e);
851 41148 jjdelcerro
        }
852 44088 omartinez
        if ("bak".equals(FilenameUtils.getExtension(in.getAbsolutePath()))) {
853
            this.fname =  null;
854
        } else {
855
            this.fname = in;
856
        }
857 41148 jjdelcerro
        notifyObservers(ProjectNotification.AFTER_LOAD_FROM_FILE, in);
858
    }
859 40435 jjdelcerro
860 43913 jjdelcerro
    //@SuppressWarnings("unchecked")
861
    @Override
862 41148 jjdelcerro
    public void loadFromState(PersistentState state)
863
            throws PersistenceException {
864
        this.clean();
865 43976 jjdelcerro
        PersistentContext context = state.getContext();
866
867 43913 jjdelcerro
        notifyObservers(ProjectNotification.BEFORE_LOAD_FROM_STATE, state);
868 40435 jjdelcerro
869 41148 jjdelcerro
        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 40435 jjdelcerro
877 41350 jjdelcerro
        this.propertiesHelper = (ExtendedPropertiesHelper) state.get("propertiesHelper");
878 42088 fdiaz
879 41148 jjdelcerro
        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 40435 jjdelcerro
885 41148 jjdelcerro
        List<AbstractDocument> documents = (List<AbstractDocument>) state
886
                .get("documents");
887
        for (int i = 0; i < documents.size(); i++) {
888 43913 jjdelcerro
            AbstractDocument doc = documents.get(i);
889
            if( doc != null ) {
890
                this.add(doc);
891
            }
892 41148 jjdelcerro
        }
893 40435 jjdelcerro
894 43976 jjdelcerro
        try {
895
            List<DocumentWindowInfo> persistentWindows = (List<DocumentWindowInfo>) state.get("documentWindowsInformation");
896 40435 jjdelcerro
897 43976 jjdelcerro
            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 42088 fdiaz
            }
915 40435 jjdelcerro
916 43976 jjdelcerro
            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 41148 jjdelcerro
        }
928 43913 jjdelcerro
        notifyObservers(ProjectNotification.AFTER_LOAD_FROM_STATE, state);
929 41148 jjdelcerro
    }
930 40435 jjdelcerro
931 44088 omartinez
    @Override
932 41148 jjdelcerro
    public void saveToState(PersistentState state) throws PersistenceException {
933
        state.set("version", VERSION);
934
        state.set("comments", getComments());
935
        state.set("creationDate", this.getCreationDate());
936 40435 jjdelcerro
937 41148 jjdelcerro
        state.set("modificationDate", this.getModificationDate());
938
        state.set("name", this.getName());
939
        state.set("owner", this.getOwner());
940
        state.set("selectionColor", this.getSelectionColor());
941 40435 jjdelcerro
942 41148 jjdelcerro
        state.set("projection", this.getProjection());
943 40435 jjdelcerro
944 41148 jjdelcerro
        state.set("extents", this.extents);
945 42088 fdiaz
        List<Document> docs = this.getDocuments();
946 44443 jjdelcerro
        List<Document> noTempDocs = new ArrayList<>();
947
        for (Document document : docs) {
948 42088 fdiaz
            if(!document.isTemporary()){
949
                noTempDocs.add(document);
950
            }
951
        }
952
        state.set("documents", noTempDocs);
953 40435 jjdelcerro
954 41350 jjdelcerro
        state.set("propertiesHelper",propertiesHelper);
955 42088 fdiaz
956 44443 jjdelcerro
        List<DocumentWindowInfo> persistentWindows = new ArrayList<>();
957 41148 jjdelcerro
        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 40435 jjdelcerro
974 41148 jjdelcerro
    }
975 40435 jjdelcerro
976 44088 omartinez
    @Override
977 41350 jjdelcerro
    public Object getProperty(Object key) {
978
        return this.propertiesHelper.getProperty(key);
979
    }
980
981 44088 omartinez
    @Override
982 41350 jjdelcerro
    public void setProperty(Object key, Object obj) {
983
        this.propertiesHelper.setProperty(key, obj);
984
    }
985
986 44088 omartinez
    @Override
987 41350 jjdelcerro
    public Map getExtendedProperties() {
988
        return this.propertiesHelper.getExtendedProperties();
989
    }
990
991 41148 jjdelcerro
    public static class DocumentWindowInfo implements Persistent {
992 40435 jjdelcerro
993 41148 jjdelcerro
        public static final String PERSISTENCE_DEFINITION_NAME = "DocumentWindowInfo";
994 40435 jjdelcerro
995 41148 jjdelcerro
        private WindowInfo windowInfo;
996
        private String documentType;
997
        private String documentName;
998 40435 jjdelcerro
999 41148 jjdelcerro
        public DocumentWindowInfo() {
1000
        }
1001 40435 jjdelcerro
1002 41148 jjdelcerro
        DocumentWindowInfo(WindowInfo wi, String docType, String docName) {
1003
            windowInfo = wi;
1004
            documentType = docType;
1005
            documentName = docName;
1006
        }
1007 40435 jjdelcerro
1008 41148 jjdelcerro
        public WindowInfo getWindowInfo() {
1009
            return windowInfo;
1010
        }
1011 40435 jjdelcerro
1012 41148 jjdelcerro
        public String getDocumentType() {
1013
            return documentType;
1014
        }
1015 40435 jjdelcerro
1016 41148 jjdelcerro
        public String getDocumentName() {
1017
            return documentName;
1018
        }
1019 40435 jjdelcerro
1020 41148 jjdelcerro
        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 40435 jjdelcerro
1027 41148 jjdelcerro
        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 40435 jjdelcerro
1034 41148 jjdelcerro
        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 40435 jjdelcerro
1050 41148 jjdelcerro
        }
1051
    }
1052 40435 jjdelcerro
1053 41148 jjdelcerro
    public static void registerPersistent() {
1054
        AbstractDocument.registerPersistent();
1055
        DocumentWindowInfo.registerPersistent();
1056
        ProjectExtent.registerPersistent();
1057 40435 jjdelcerro
1058 41148 jjdelcerro
        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 40435 jjdelcerro
1068 41148 jjdelcerro
        definition.addDynFieldObject("selectionColor")
1069
                .setClassOfValue(Color.class).setMandatory(true);
1070
        definition.addDynFieldObject("projection")
1071
                .setClassOfValue(IProjection.class).setMandatory(true);
1072 40435 jjdelcerro
1073 41148 jjdelcerro
        definition.addDynFieldList("extents")
1074
                .setClassOfItems(ProjectExtent.class).setMandatory(true);
1075 40435 jjdelcerro
1076 41148 jjdelcerro
        definition.addDynFieldList("documents").setClassOfItems(Document.class)
1077
                .setMandatory(true);
1078 40435 jjdelcerro
1079 41148 jjdelcerro
        definition.addDynFieldObject("projectWindowInfo").setClassOfValue(WindowInfo.class).setMandatory(false);
1080 40435 jjdelcerro
1081 41148 jjdelcerro
        definition.addDynFieldList("documentWindowsInformation").setClassOfItems(WindowInfo.class).setMandatory(false);
1082 40435 jjdelcerro
1083 42088 fdiaz
1084 41350 jjdelcerro
        definition.addDynFieldObject("propertiesHelper").setClassOfValue(ExtendedPropertiesHelper.class)
1085
                        .setMandatory(false);
1086
1087 41148 jjdelcerro
    }
1088 40435 jjdelcerro
1089 41148 jjdelcerro
    /**
1090
     * @deprecated use getPreferences().setDefaultSelectionColor()
1091
     */
1092
    public static void setDefaultSelectionColor(Color color) {
1093
        getPreferences().setDefaultSelectionColor(color);
1094
    }
1095 40435 jjdelcerro
1096 41148 jjdelcerro
    /**
1097
     * @deprecated use getPreferences().getDefaultSelectionColor()
1098
     */
1099
    public static Color getDefaultSelectionColor() {
1100
        return getPreferences().getDefaultSelectionColor();
1101
    }
1102 40435 jjdelcerro
1103 41148 jjdelcerro
    /**
1104
     * @deprecated use getPreferences().getDefaultMapUnits()
1105
     */
1106
    public static int getDefaultMapUnits() {
1107
        return getPreferences().getDefaultMapUnits();
1108
    }
1109 40435 jjdelcerro
1110 41148 jjdelcerro
    /**
1111
     * @deprecated use getPreferences().getDefaultDistanceUnits()
1112
     */
1113
    public static int getDefaultDistanceUnits() {
1114
        return getPreferences().getDefaultDistanceUnits();
1115
    }
1116 40435 jjdelcerro
1117 41148 jjdelcerro
    /**
1118
     * @deprecated use getPreferences().getDefaultDistanceArea()
1119
     */
1120
    public static int getDefaultDistanceArea() {
1121
        return getPreferences().getDefaultDistanceArea();
1122
    }
1123 40435 jjdelcerro
1124 41148 jjdelcerro
    /**
1125
     * @deprecated use getPreferences().setDefaultMapUnits()
1126
     */
1127
    public static void setDefaultMapUnits(int mapUnits) {
1128
        getPreferences().setDefaultMapUnits(mapUnits);
1129
    }
1130 40435 jjdelcerro
1131 41148 jjdelcerro
    /**
1132
     * @deprecated use getPreferences().setDefaultDistanceUnits()
1133
     */
1134
    public static void setDefaultDistanceUnits(int distanceUnits) {
1135
        getPreferences().setDefaultDistanceUnits(distanceUnits);
1136
    }
1137 40435 jjdelcerro
1138 41148 jjdelcerro
    /**
1139
     * @deprecated use getPreferences().setDefaultDistanceArea()
1140
     */
1141
    public static void setDefaultDistanceArea(int distanceArea) {
1142
        getPreferences().setDefaultDistanceArea(distanceArea);
1143
    }
1144 40435 jjdelcerro
1145 41148 jjdelcerro
    /**
1146
     * @deprecated use getPreferences().setDefaultProjection()
1147
     */
1148
    public static void setDefaultProjection(IProjection defaultProjection) {
1149
        getPreferences().setDefaultProjection(defaultProjection);
1150
    }
1151 40435 jjdelcerro
1152 41148 jjdelcerro
    /**
1153
     * @deprecated use getPreferences().getDefaultProjection()
1154
     */
1155
    public static IProjection getDefaultProjection() {
1156
        return getPreferences().getDefaultProjection();
1157
    }
1158 40435 jjdelcerro
1159 41148 jjdelcerro
    /**
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 40435 jjdelcerro
1167 41148 jjdelcerro
    /**
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 40435 jjdelcerro
1177 41148 jjdelcerro
    /**
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 40435 jjdelcerro
1188 41148 jjdelcerro
    /**
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 40435 jjdelcerro
1197 41148 jjdelcerro
    /**
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 40435 jjdelcerro
1209 41148 jjdelcerro
    /**
1210
     * @deprecated see {@link #getDocument(String, String)}
1211
     */
1212
    public Document getProjectDocumentByName(String name, String type) {
1213
        return this.getDocument(name, type);
1214
    }
1215 40435 jjdelcerro
1216 41148 jjdelcerro
    /**
1217
     * @deprecated see {@link #getDocuments(String)}
1218
     */
1219
    public List<Document> getDocumentsByType(String type) {
1220
        return this.getDocuments(type);
1221
    }
1222 40435 jjdelcerro
1223 41148 jjdelcerro
    /**
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 40435 jjdelcerro
1232 41148 jjdelcerro
    /**
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 40435 jjdelcerro
1240 41148 jjdelcerro
    /**
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 44088 omartinez
    @Override
1249 41148 jjdelcerro
    public boolean canImportDocuments(String data, String doctype) {
1250
        // TODO Auto-generated method stub
1251
        return false;
1252
    }
1253
1254 44088 omartinez
    @Override
1255 41148 jjdelcerro
    public String exportDocumentsAsText(List<Document> documents) {
1256
        // TODO Auto-generated method stub
1257
        return null;
1258
    }
1259
1260 44088 omartinez
    @Override
1261 41148 jjdelcerro
    public void importDocuments(String data, String doctype) {
1262
        // TODO Auto-generated method stub
1263
1264
    }
1265
1266 44088 omartinez
    @Override
1267 41148 jjdelcerro
    public Document getActiveDocument() {
1268 41248 jjdelcerro
        return this.getActiveDocument((Class<? extends Document>)null);
1269 41148 jjdelcerro
    }
1270
1271 44088 omartinez
    @Override
1272 41248 jjdelcerro
    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 42088 fdiaz
1299 41248 jjdelcerro
                } catch (ClassCastException e) {
1300
                    // Do nothing, skip this window
1301
                }
1302
            }
1303
        }
1304
        return null;
1305
    }
1306 42088 fdiaz
1307 44088 omartinez
    @Override
1308 41148 jjdelcerro
    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 42165 fdiaz
            if (window instanceof SingletonWindow && window instanceof IDocumentWindow) {
1317 41248 jjdelcerro
                // Cogemos no la primera ventana, si no la primera
1318 41148 jjdelcerro
                // 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 41248 jjdelcerro
                    if( document instanceof DocumentsContainer ) {
1331
                        Document subdoc = ((DocumentsContainer)document).getActiveDocument(documentClass);
1332
                        return subdoc;
1333
                    }
1334 42088 fdiaz
1335 41148 jjdelcerro
                } catch (ClassCastException e) {
1336
                    // Do nothing, skip this window
1337
                }
1338
            }
1339
        }
1340
        return null;
1341
    }
1342 40435 jjdelcerro
1343 44088 omartinez
    @Override
1344 41148 jjdelcerro
    public void addObserver(Observer o) {
1345
        observableHelper.addObserver(o);
1346
    }
1347 40435 jjdelcerro
1348 44088 omartinez
    @Override
1349 41148 jjdelcerro
    public void deleteObserver(Observer o) {
1350
        observableHelper.deleteObserver(o);
1351
    }
1352
1353 44088 omartinez
    @Override
1354 41148 jjdelcerro
    public void deleteObservers() {
1355
        observableHelper.deleteObservers();
1356
    }
1357
1358
    private ProjectNotification notifyObservers(int type) {
1359 43913 jjdelcerro
        return notifyObservers(new DefaultProjectNotification(type));
1360 41148 jjdelcerro
    }
1361 40435 jjdelcerro
1362 41148 jjdelcerro
    private ProjectNotification notifyObservers(int type, Document document) {
1363 43913 jjdelcerro
        return notifyObservers(new DefaultProjectNotification(type, document));
1364 41148 jjdelcerro
    }
1365 43913 jjdelcerro
1366
    private ProjectNotification notifyObservers(int type, PersistentState state) {
1367
        return notifyObservers(new DefaultProjectNotification(type, state));
1368
    }
1369 40435 jjdelcerro
1370 41148 jjdelcerro
    private ProjectNotification notifyObservers(int type, File file) {
1371 43913 jjdelcerro
        return notifyObservers(new DefaultProjectNotification(type, file));
1372
    }
1373
1374
    private ProjectNotification notifyObservers(ProjectNotification notifycation) {
1375 41148 jjdelcerro
        try {
1376
            observableHelper.notifyObservers(this, notifycation);
1377
        } catch (Exception ex) {
1378
            LOG.info("Can't notify observers", ex);
1379
        }
1380
        return notifycation;
1381
    }
1382 43913 jjdelcerro
1383 41148 jjdelcerro
    class DefaultProjectNotification implements ProjectNotification {
1384 40435 jjdelcerro
1385 41148 jjdelcerro
        private int type;
1386
        private Document document;
1387
        private File file;
1388
        private boolean processCanceled = false;
1389 43913 jjdelcerro
        private PersistentState state = null;
1390 40435 jjdelcerro
1391 43913 jjdelcerro
        DefaultProjectNotification(int type) {
1392 41148 jjdelcerro
            this.type = type;
1393 43913 jjdelcerro
        }
1394
1395
        DefaultProjectNotification(int type, Document document) {
1396
            this(type);
1397 41148 jjdelcerro
            this.document = document;
1398 43913 jjdelcerro
        }
1399
1400
        DefaultProjectNotification(int type, File file) {
1401
            this(type);
1402 41148 jjdelcerro
            this.file = file;
1403
        }
1404 40435 jjdelcerro
1405 43913 jjdelcerro
        DefaultProjectNotification(int type, PersistentState state) {
1406
            this(type);
1407
            this.state = state;
1408
        }
1409
1410
        @Override
1411 41148 jjdelcerro
        public int getNotificationType() {
1412
            return type;
1413
        }
1414 40435 jjdelcerro
1415 43913 jjdelcerro
        @Override
1416 41148 jjdelcerro
        public Document getDocument() {
1417
            return document;
1418
        }
1419
1420 43913 jjdelcerro
        @Override
1421 41148 jjdelcerro
        public void cancelProcess() {
1422
            processCanceled = true;
1423
        }
1424
1425 43913 jjdelcerro
        @Override
1426 41148 jjdelcerro
        public boolean isProcessCanceled() {
1427
            return processCanceled;
1428
        }
1429
1430 43913 jjdelcerro
        @Override
1431 41148 jjdelcerro
        public File getFile() {
1432
            return file;
1433
        }
1434
1435 43913 jjdelcerro
        @Override
1436
        public PersistentState getState() {
1437
            return this.state;
1438
        }
1439
1440
        @Override
1441
        public Project getProject() {
1442
            return DefaultProject.this;
1443
        }
1444 41148 jjdelcerro
    }
1445
1446 40435 jjdelcerro
}