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

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;
27
import java.awt.geom.AffineTransform;
28
import java.awt.image.AffineTransformOp;
29
import java.awt.image.BufferedImage;
30 40435 jjdelcerro
import java.beans.PropertyChangeEvent;
31
import java.beans.PropertyChangeListener;
32
import java.beans.PropertyChangeSupport;
33
import java.io.File;
34
import java.io.FileInputStream;
35
import java.io.FileNotFoundException;
36
import java.io.FileOutputStream;
37 43820 jjdelcerro
import java.io.IOException;
38 40435 jjdelcerro
import java.io.InputStream;
39
import java.io.OutputStream;
40
import java.io.Serializable;
41
import java.text.DateFormat;
42
import java.text.MessageFormat;
43 44088 omartinez
import java.text.SimpleDateFormat;
44 40435 jjdelcerro
import java.util.ArrayList;
45
import java.util.Collections;
46
import java.util.Date;
47
import java.util.HashMap;
48 42711 fdiaz
import java.util.HashSet;
49 40435 jjdelcerro
import java.util.Iterator;
50
import java.util.List;
51
import java.util.Map;
52 42711 fdiaz
import java.util.Set;
53 43913 jjdelcerro
import java.util.logging.Level;
54
import java.util.zip.ZipEntry;
55 43820 jjdelcerro
import java.util.zip.ZipException;
56
import java.util.zip.ZipFile;
57 43913 jjdelcerro
import java.util.zip.ZipOutputStream;
58
import javax.imageio.ImageIO;
59 44088 omartinez
import org.apache.commons.io.FileUtils;
60
import org.apache.commons.io.FilenameUtils;
61 43913 jjdelcerro
import org.apache.commons.io.IOUtils;
62 42711 fdiaz
63 40435 jjdelcerro
import org.cresques.cts.IProjection;
64 42711 fdiaz
65 40435 jjdelcerro
import org.gvsig.andami.PluginServices;
66
import org.gvsig.andami.ui.mdiManager.IWindow;
67
import org.gvsig.andami.ui.mdiManager.MDIManager;
68
import org.gvsig.andami.ui.mdiManager.SingletonWindow;
69
import org.gvsig.andami.ui.mdiManager.WindowInfo;
70
import org.gvsig.app.ApplicationLocator;
71
import org.gvsig.app.ApplicationManager;
72
import org.gvsig.app.extension.ProjectExtension;
73
import org.gvsig.app.extension.Version;
74 43879 jjdelcerro
import org.gvsig.app.project.ProjectManager.NewProjectEvent;
75 40435 jjdelcerro
import org.gvsig.app.project.documents.AbstractDocument;
76
import org.gvsig.app.project.documents.Document;
77
import org.gvsig.app.project.documents.exceptions.SaveException;
78
import org.gvsig.app.project.documents.gui.IDocumentWindow;
79
import org.gvsig.app.project.documents.gui.ProjectWindow;
80
import org.gvsig.app.project.documents.view.DefaultViewDocument;
81
import org.gvsig.app.project.documents.view.ViewManager;
82
import org.gvsig.fmap.mapcontext.MapContext;
83 41350 jjdelcerro
import org.gvsig.fmap.mapcontext.layers.ExtendedPropertiesHelper;
84 40435 jjdelcerro
import org.gvsig.fmap.mapcontext.layers.FLayer;
85
import org.gvsig.fmap.mapcontext.layers.FLayers;
86
import org.gvsig.tools.ToolsLocator;
87
import org.gvsig.tools.dynobject.DynStruct;
88
import org.gvsig.tools.observer.ObservableHelper;
89
import org.gvsig.tools.observer.Observer;
90
import org.gvsig.tools.persistence.PersistenceManager;
91
import org.gvsig.tools.persistence.Persistent;
92 42711 fdiaz
import org.gvsig.tools.persistence.PersistentContext;
93 40435 jjdelcerro
import org.gvsig.tools.persistence.PersistentState;
94
import org.gvsig.tools.persistence.exception.PersistenceException;
95
import org.gvsig.utils.StringUtilities;
96 43913 jjdelcerro
import org.gvsig.utils.save.SaveEvent;
97 42711 fdiaz
98 40435 jjdelcerro
import org.slf4j.Logger;
99
import org.slf4j.LoggerFactory;
100
101
/**
102
 * Clase que representa un proyecto de gvSIG
103 41148 jjdelcerro
 *
104 40435 jjdelcerro
 */
105
public class DefaultProject implements Serializable, PropertyChangeListener,
106 41148 jjdelcerro
        Project {
107 40435 jjdelcerro
108 41148 jjdelcerro
    private Logger LOG = LoggerFactory.getLogger(DefaultProject.class);
109
    /**
110
     * @deprecated see ApplicationLocator.getManager().getVersion()
111
     */
112
    public static String VERSION = Version.format();
113 40435 jjdelcerro
114 41350 jjdelcerro
    private ExtendedPropertiesHelper propertiesHelper = new ExtendedPropertiesHelper();
115 42088 fdiaz
116 40435 jjdelcerro
    private ObservableHelper observableHelper = new ObservableHelper();
117
118 41148 jjdelcerro
    /**
119
     *
120
     */
121
    private static final long serialVersionUID = -4449622027521773178L;
122 40435 jjdelcerro
123 41148 jjdelcerro
    private static final Logger logger = LoggerFactory.getLogger(Project.class);
124 40435 jjdelcerro
125 41148 jjdelcerro
    private static ProjectPreferences preferences = new ProjectPreferences();
126 40435 jjdelcerro
127 41148 jjdelcerro
    /**
128
     * Index used by the generator of unique names of documents.
129
     */
130
    private Map<String, Integer> nextDocumentIndexByType = new HashMap<String, Integer>();
131 40435 jjdelcerro
132 41148 jjdelcerro
    private PropertyChangeSupport change;
133 40435 jjdelcerro
134 41148 jjdelcerro
    private boolean modified = false;
135 40435 jjdelcerro
136 41148 jjdelcerro
    private String name = null;
137 40435 jjdelcerro
138 41148 jjdelcerro
    private String creationDate = null;
139 40435 jjdelcerro
140 41148 jjdelcerro
    private String modificationDate = null;
141 40435 jjdelcerro
142 41148 jjdelcerro
    private String owner = null;
143 40435 jjdelcerro
144 41148 jjdelcerro
    private String comments = null;
145 40435 jjdelcerro
146 41148 jjdelcerro
    private Color selectionColor = null;
147 40435 jjdelcerro
148 41148 jjdelcerro
    private List<Document> documents = null;
149 40435 jjdelcerro
150 41148 jjdelcerro
    private List<ProjectExtent> extents = null;
151 40435 jjdelcerro
152 41148 jjdelcerro
    private IProjection projection;
153 40435 jjdelcerro
154 42466 jjdelcerro
    private File fname = null;
155 42711 fdiaz
156
    private Set<String> unloadedObjects;
157 43439 jjdelcerro
    private PersistenceException loadErrors = null;
158 42711 fdiaz
159 41148 jjdelcerro
    /**
160
     * Creates a new Project object.
161
     */
162
    DefaultProject() {
163
        this.change = new PropertyChangeSupport(this);
164
        this.clean();
165 43879 jjdelcerro
        ProjectManager.getInstance().notifyProjectEvent(new NewProjectEvent() {
166
            @Override
167
            public Project getProject() {
168
                return DefaultProject.this;
169
            }
170
        });
171 41148 jjdelcerro
    }
172 40435 jjdelcerro
173 41148 jjdelcerro
    protected void clean() {
174
        this.owner = "";
175
        this.comments = "";
176
        this.name = PluginServices.getText(this, "untitled");
177
        this.creationDate = DateFormat.getDateInstance().format(new Date());
178
        this.modificationDate = this.creationDate;
179 40435 jjdelcerro
180 41148 jjdelcerro
        this.documents = new ArrayList<Document>();
181
        this.extents = new ArrayList<ProjectExtent>();
182 40435 jjdelcerro
183 41148 jjdelcerro
        this.setSelectionColor(getPreferences().getDefaultSelectionColor());
184 40435 jjdelcerro
185 41148 jjdelcerro
        this.projection = null; // se inicializa en el getProjection()
186
    }
187 40435 jjdelcerro
188 41148 jjdelcerro
    public static ProjectPreferences getPreferences() {
189
        return preferences;
190
    }
191 40435 jjdelcerro
192 44088 omartinez
    @Override
193 41148 jjdelcerro
    public void propertyChange(PropertyChangeEvent evt) {
194
        change.firePropertyChange(evt);
195
    }
196 40435 jjdelcerro
197 43913 jjdelcerro
    @Override
198 41148 jjdelcerro
    public synchronized void addPropertyChangeListener(
199
            PropertyChangeListener arg0) {
200
        change.addPropertyChangeListener(arg0);
201
    }
202 40435 jjdelcerro
203 43913 jjdelcerro
    @Override
204
    public void removePropertyChangeListener(PropertyChangeListener listener) {
205
        change.removePropertyChangeListener(listener);
206
    }
207
208 41148 jjdelcerro
    /**
209
     * Return the creation date of the project
210
     *
211
     * @return
212
     */
213 44088 omartinez
    @Override
214 41148 jjdelcerro
    public String getCreationDate() {
215
        return creationDate;
216
    }
217 40435 jjdelcerro
218 41148 jjdelcerro
    protected void setCreationDate(String creationDate) {
219
        this.creationDate = creationDate;
220
        change.firePropertyChange("setCreationDate", null, null);
221
    }
222 40435 jjdelcerro
223 44088 omartinez
    @Override
224 40435 jjdelcerro
    public Document createDocument(String type) {
225 41148 jjdelcerro
        logger.info("createDocument('{}')", type);
226 40435 jjdelcerro
        return ProjectManager.getInstance().createDocument(type);
227
    }
228
229
    /**
230
     * Return the name of the project
231 41148 jjdelcerro
     *
232 40435 jjdelcerro
     * @return
233
     */
234 44088 omartinez
    @Override
235 40435 jjdelcerro
    public String getName() {
236
        return name;
237
    }
238
239 41148 jjdelcerro
    /**
240
     * Set the name of he project.
241
     *
242
     * @param string
243
     */
244 44088 omartinez
    @Override
245 41148 jjdelcerro
    public void setName(String name) {
246
        this.name = name;
247
        change.firePropertyChange("setName", null, null);
248
    }
249 40435 jjdelcerro
250 41148 jjdelcerro
    /**
251
     * Return the comments associateds with the project
252
     *
253
     * @return comments
254
     */
255 44088 omartinez
    @Override
256 41148 jjdelcerro
    public String getComments() {
257
        return comments;
258
    }
259 40435 jjdelcerro
260 41148 jjdelcerro
    /**
261
     * Set the comments associateds with the project
262
     *
263
     * @param comments as string
264
     */
265 44088 omartinez
    @Override
266 41148 jjdelcerro
    public void setComments(String string) {
267
        comments = string;
268
        change.firePropertyChange("setComments", null, null);
269
    }
270 40435 jjdelcerro
271 41148 jjdelcerro
    /**
272
     * Retuen the modification date of the project.
273
     *
274
     * @return modification date as string
275
     */
276 44088 omartinez
    @Override
277 41148 jjdelcerro
    public String getModificationDate() {
278
        return modificationDate;
279
    }
280 40435 jjdelcerro
281 41148 jjdelcerro
    protected void setModificationDate(String string) {
282
        modificationDate = string;
283
        change.firePropertyChange("setModificationDate", null, null);
284
    }
285 40435 jjdelcerro
286 41148 jjdelcerro
    /**
287
     * Return the author of the project,
288
     *
289
     * @return author as string
290
     */
291 44088 omartinez
    @Override
292 41148 jjdelcerro
    public String getOwner() {
293
        return owner;
294
    }
295 40435 jjdelcerro
296 41148 jjdelcerro
    /**
297
     * Sets the author of the project
298
     *
299
     * @param author name as string
300
     */
301 44088 omartinez
    @Override
302 41148 jjdelcerro
    public void setOwner(String owner) {
303
        this.owner = owner;
304
        change.firePropertyChange("setOwner", null, null);
305
    }
306 40435 jjdelcerro
307 41148 jjdelcerro
    /**
308
     * Obtiene el color de selecci�n que se usar� en el proyecto
309
     *
310
     * @return
311
     */
312 44088 omartinez
    @Override
313 41148 jjdelcerro
    public Color getSelectionColor() {
314
        if (selectionColor == null) {
315
            selectionColor = getPreferences().getDefaultSelectionColor();
316
        }
317
        return selectionColor;
318
    }
319 40435 jjdelcerro
320 41148 jjdelcerro
    /**
321
     * Sets the selecction color
322
     *
323
     * @param selection color as string
324
     */
325 44088 omartinez
    @Override
326 41148 jjdelcerro
    public void setSelectionColor(String selectionColor) {
327
        this.setSelectionColor(StringUtilities.string2Color(selectionColor));
328
    }
329 40435 jjdelcerro
330 41148 jjdelcerro
    /**
331
     * Sets the selecction color
332
     *
333
     * @param selection color as Color
334
     */
335 44088 omartinez
    @Override
336 41148 jjdelcerro
    public void setSelectionColor(Color selectionColor) {
337
        this.selectionColor = selectionColor;
338
        MapContext.setSelectionColor(selectionColor);
339
        change.firePropertyChange("selectionColor", null, selectionColor);
340
    }
341 40435 jjdelcerro
342 44088 omartinez
    @Override
343 41148 jjdelcerro
    public IProjection getProjection() {
344
        if (projection == null) {
345
            projection = getPreferences().getDefaultProjection();
346
        }
347
        return projection;
348
    }
349 40435 jjdelcerro
350 44088 omartinez
    @Override
351 41148 jjdelcerro
    public void setProjection(IProjection projection) {
352
        this.projection = projection;
353
    }
354 40435 jjdelcerro
355 41148 jjdelcerro
    /**
356
     * Sets the modified state of project.
357
     *
358
     * Can't set to not modified.
359
     *
360
     * @param modified as boolean
361
     */
362 44088 omartinez
    @Override
363 41148 jjdelcerro
    public void setModified(boolean modified) {
364
        this.modified = modified;
365
        if (modified == false) {
366
            List<Document> documents = this.getDocuments();
367
            for (int i = 0; i < documents.size(); i++) {
368
                documents.get(i).setModified(false);
369
            }
370
        }
371
    }
372 40435 jjdelcerro
373 44088 omartinez
    @Override
374 41148 jjdelcerro
    public boolean hasChanged() {
375 40435 jjdelcerro
                // we return true if the project is not empty (until we have a better
376 41148 jjdelcerro
        // method...)
377
        if ((this.getDocuments().size() != 0) || modified) {
378
            return true;
379
        }
380
        return false;
381
    }
382 40435 jjdelcerro
383 41148 jjdelcerro
    /**
384
     * Return a list of documents in the project.
385
     *
386
     * @return documents as List of IProjectDocument
387
     */
388 44088 omartinez
    @Override
389 41148 jjdelcerro
    public List<Document> getDocuments() {
390
        return Collections.unmodifiableList(documents);
391
    }
392 40435 jjdelcerro
393 41148 jjdelcerro
    /**
394
     * Return a list with all documents of especified type.
395
     *
396
     * @param type of document
397
     *
398
     * @return List of IProjectDocument
399
     */
400 44088 omartinez
    @Override
401 41148 jjdelcerro
    public List<Document> getDocuments(String type) {
402
        List<Document> docs = new ArrayList<Document>();
403
        if (type != null) {
404
            for (Document document : this.documents) {
405
                if (type.equalsIgnoreCase(document.getTypeName())) {
406
                    docs.add(document);
407
                }
408
            }
409
        }
410
        return Collections.unmodifiableList(docs);
411
    }
412 40435 jjdelcerro
413 41148 jjdelcerro
    /**
414
     * Adds a document to the project
415
     *
416 41248 jjdelcerro
     * @param document as Document
417 41148 jjdelcerro
     */
418 44088 omartinez
    @Override
419 41148 jjdelcerro
    public void add(Document document) {
420 41248 jjdelcerro
        this.addDocument(document);
421
    }
422 42088 fdiaz
423 44088 omartinez
    @Override
424 41248 jjdelcerro
    public void addDocument(Document document) {
425 40435 jjdelcerro
        logger.info("add('{}')", document.toString());
426
427 41148 jjdelcerro
        if (notifyObservers(ProjectNotification.BEFORE_ADD_DOCUMENT, document).isProcessCanceled()) {
428
            return;
429 40435 jjdelcerro
        }
430 41148 jjdelcerro
        document.addPropertyChangeListener(this);
431
        document.setProject(this);
432
        document.setName(this.getUniqueNameForDocument(document.getTypeName(),
433
                document.getName()));
434
        documents.add(document);
435
        document.afterAdd();
436
        this.setModified(true);
437
        notifyObservers(ProjectNotification.AFTER_ADD_DOCUMENT, document);
438 40435 jjdelcerro
        change.firePropertyChange("addDocument", null, document);
439 41148 jjdelcerro
    }
440 40435 jjdelcerro
441 44088 omartinez
    @Override
442 41148 jjdelcerro
    public void remove(Document doc) {
443 41248 jjdelcerro
        this.removeDocument(doc);
444
    }
445 42088 fdiaz
446 44088 omartinez
    @Override
447 41248 jjdelcerro
    public void removeDocument(Document doc) {
448 40435 jjdelcerro
        logger.info("remove('{}')", doc.toString());
449 41148 jjdelcerro
        if (notifyObservers(ProjectNotification.BEFORE_REMOVE_DOCUMENT, doc).isProcessCanceled()) {
450
            return;
451 40435 jjdelcerro
        }
452 41148 jjdelcerro
        documents.remove(doc);
453
        this.setModified(true);
454 40435 jjdelcerro
        change.firePropertyChange("delDocument", doc, null);
455 41148 jjdelcerro
        doc.afterRemove();
456
        notifyObservers(ProjectNotification.AFTER_REMOVE_DOCUMENT, doc);
457
    }
458 40435 jjdelcerro
459 44088 omartinez
    @Override
460 41148 jjdelcerro
    public Iterator<Document> iterator() {
461
        return documents.iterator();
462
    }
463 40435 jjdelcerro
464 44088 omartinez
    @Override
465 41148 jjdelcerro
    public boolean isEmpty() {
466
        return documents.isEmpty();
467
    }
468 40435 jjdelcerro
469 41148 jjdelcerro
    /**
470
     * Return the view that contains the especified layer.
471
     *
472
     * @param layer
473
     *
474
     * @return name of the view that contains the layer
475
     *
476
     * @throws RuntimeException Si la capa que se pasa como par�metro no se
477
     * encuentra en ninguna vista
478
     */
479 44088 omartinez
    @Override
480 41148 jjdelcerro
    public String getViewName(FLayer layer) {
481
        List<Document> views = getDocuments(ViewManager.TYPENAME);
482
        for (int v = 0; v < views.size(); v++) {
483
            DefaultViewDocument pView = (DefaultViewDocument) views.get(v);
484
            FLayers layers = pView.getMapContext().getLayers();
485
            if (isView(layers, layer)) {
486
                return pView.getName();
487
            }
488
        }
489 40435 jjdelcerro
490 41148 jjdelcerro
        throw new RuntimeException(MessageFormat.format(
491
                "The layer '{1}' is not in a view", layer.getName()));
492
    }
493 40435 jjdelcerro
494 41148 jjdelcerro
    private boolean isView(FLayers layers, FLayer layer) {
495
        for (int i = 0; i < layers.getLayersCount(); i++) {
496
            if (layers.getLayer(i) instanceof FLayers) {
497
                if (isView((FLayers) layers.getLayer(i), layer)) {
498
                    return true;
499
                }
500
            }
501
            if (layers.getLayer(i) == layer) {
502
                return true;
503
            }
504
        }
505
        return false;
506
    }
507 40435 jjdelcerro
508 44088 omartinez
    @Override
509 41148 jjdelcerro
    public void addExtent(ProjectExtent arg1) {
510
        extents.add(arg1);
511
        change.firePropertyChange("addExtent", null, null);
512
    }
513 40435 jjdelcerro
514 44088 omartinez
    @Override
515 41148 jjdelcerro
    public ProjectExtent removeExtent(int arg0) {
516
        change.firePropertyChange("delExtent", null, null);
517
        return extents.remove(arg0);
518
    }
519 40435 jjdelcerro
520 44088 omartinez
    @Override
521 41148 jjdelcerro
    public ProjectExtent[] getExtents() {
522
        return (ProjectExtent[]) extents.toArray(new ProjectExtent[0]);
523
    }
524 40435 jjdelcerro
525 41148 jjdelcerro
    /**
526
     * Obtiene un documento a partir de su nombre y el nombre de registro en el
527
     * pointExtension, este �ltimo se puede obtener del
528
     * Project****Factory.registerName.
529
     *
530
     * @param name Nombre del documento
531
     * @param type nombre de registro en el extensionPoint
532
     *
533
     * @return Documento
534
     */
535 44088 omartinez
    @Override
536 41148 jjdelcerro
    public Document getDocument(String name, String type) {
537 42293 jjdelcerro
        if (type == null) {
538 41148 jjdelcerro
            for (int i = 0; i < documents.size(); i++) {
539
                Document document = documents.get(i);
540 42293 jjdelcerro
                if ( name.equalsIgnoreCase(document.getName())) {
541
                    return document;
542
                }
543
            }
544
        } else {
545
            for (int i = 0; i < documents.size(); i++) {
546
                Document document = documents.get(i);
547 41148 jjdelcerro
                if (type.equalsIgnoreCase(document.getTypeName())
548
                        && name.equalsIgnoreCase(document.getName())) {
549
                    return document;
550
                }
551
            }
552
        }
553
        return null;
554
    }
555 42711 fdiaz
556 44088 omartinez
    @Override
557 42293 jjdelcerro
    public Document getDocument(String name) {
558
        return this.getDocument(name, null);
559 42711 fdiaz
    }
560 40435 jjdelcerro
561 41148 jjdelcerro
    public String getUniqueNameForDocument(String type, String name) {
562
        Document document = getDocument(name, type);
563
        if (document == null) {
564
            return name;
565
        }
566 40435 jjdelcerro
567 41148 jjdelcerro
        String newName = null;
568
        int num = this.getNextDocumentIndex(type);
569
        while (document != null) {
570
            newName = name + " - " + num++;
571
            document = getDocument(newName, type);
572
        }
573
        this.setNextDocumentIndex(type, num);
574
        return newName;
575
    }
576 40435 jjdelcerro
577 41148 jjdelcerro
    private int getNextDocumentIndex(String type) {
578
        if (nextDocumentIndexByType.get(type) == null) {
579
            nextDocumentIndexByType.put(type, new Integer(1));
580
            return 1;
581
        }
582
        return nextDocumentIndexByType.get(type).intValue();
583
    }
584 40435 jjdelcerro
585 41148 jjdelcerro
    private void setNextDocumentIndex(String type, int newIndex) {
586
        if (nextDocumentIndexByType.get(type) == null) {
587
            nextDocumentIndexByType.put(type, new Integer(newIndex));
588
        } else {
589
            nextDocumentIndexByType.put(type, new Integer(newIndex));
590
        }
591
    }
592 40435 jjdelcerro
593 44088 omartinez
    @Override
594 41148 jjdelcerro
    public void saveState(File out) throws PersistenceException {
595
        FileOutputStream fout;
596
        if (notifyObservers(ProjectNotification.BEFORE_SAVE_TO_FILE, out).isProcessCanceled()) {
597
            return;
598 40435 jjdelcerro
        }
599 41148 jjdelcerro
        try {
600
            fout = new FileOutputStream(out);
601
            saveState(fout, new File(out.getParent()));
602
        } catch (FileNotFoundException e) {
603 44088 omartinez
            createProjectCopy(true);
604 41148 jjdelcerro
            throw new PersistenceException(e);
605
        }
606 43820 jjdelcerro
        if( !isValidZIP(out) ) {
607
            throw new PersistenceException(new ZipException());
608
        }
609 42466 jjdelcerro
        this.fname = out;
610 44088 omartinez
        //sacar copia al bak
611
        createProjectCopy(false);
612 41148 jjdelcerro
        notifyObservers(ProjectNotification.AFTER_SAVE_TO_FILE, out);
613
    }
614 40435 jjdelcerro
615 43820 jjdelcerro
    boolean isValidZIP(final File file) {
616
        ZipFile zipfile = null;
617
        try {
618
            zipfile = new ZipFile(file);
619
            return true;
620
        } catch (IOException e) {
621 44088 omartinez
            //sacar copia.
622 43820 jjdelcerro
            return false;
623
        } finally {
624
            try {
625
                if (zipfile != null) {
626
                    zipfile.close();
627
                    zipfile = null;
628
                }
629
            } catch (IOException e) {
630
            }
631
        }
632
    }
633
634 43913 jjdelcerro
    @Override
635 42466 jjdelcerro
    public File getFile() {
636
        return this.fname;
637
    }
638 42711 fdiaz
639 43913 jjdelcerro
    @Deprecated
640
    @Override
641 41148 jjdelcerro
    public void saveState(OutputStream out) throws PersistenceException {
642
        saveState(out, null);
643
    }
644 43913 jjdelcerro
645
    @Override
646
    public void saveState(File file, BufferedImage preview) {
647
        FileOutputStream fout=null;
648
        ZipOutputStream zout=null;
649
        try {
650
            fout = new FileOutputStream(file);
651
            zout = new ZipOutputStream(fout);
652
            this.saveState(zout, file.getParentFile());
653 40435 jjdelcerro
654 43913 jjdelcerro
            zout.putNextEntry(new ZipEntry("preview.jpg"));
655
            try {
656
                ImageIO.write(preview, "jpg", zout);
657
            } catch (IOException ex) {
658
                LOG.warn("Can't save preview image'.", ex);
659
            }
660
            IOUtils.closeQuietly(zout);
661
            IOUtils.closeQuietly(fout);
662
663
            if( !isValidZIP(file) ) {
664
                throw new ZipException("Invalid project file '"+file.getAbsolutePath()+"'");
665
            }
666
            this.fname = file;
667 44088 omartinez
            if (file.exists()) {
668
                createProjectCopy(false);
669
            }
670 43913 jjdelcerro
        } catch (Exception ex) {
671 44088 omartinez
            createProjectCopy(true);
672
            throw new RuntimeException("Can't write project in '" + file.getAbsolutePath() + ".", ex);
673 43913 jjdelcerro
        } finally {
674
            IOUtils.closeQuietly(zout);
675 44088 omartinez
            IOUtils.closeQuietly(fout);
676
677 43913 jjdelcerro
        }
678
    }
679
680
    @Override
681 41148 jjdelcerro
    public void saveState(OutputStream out, File rootFolder)
682
            throws PersistenceException {
683 43913 jjdelcerro
        if (notifyObservers(ProjectNotification.BEFORE_SAVE_TO_STREAM, rootFolder).isProcessCanceled()) {
684 41148 jjdelcerro
            return;
685 40435 jjdelcerro
        }
686 41148 jjdelcerro
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
687
        PersistentState state = null;
688
        state = manager.getState(this, true);
689
        try {
690
            if (rootFolder != null) {
691
                state.relativizeFiles(rootFolder);
692
            }
693
        } catch (Exception ex) {
694
            state.getContext().addError(ex);
695
        }
696 43077 jjdelcerro
        manager.saveState(state, out, true);
697 41148 jjdelcerro
        if (state.getContext().getErrors() != null) {
698 44088 omartinez
            createProjectCopy(true);
699 41148 jjdelcerro
            throw state.getContext().getErrors();
700
        }
701 42466 jjdelcerro
        this.fname = null;
702 43913 jjdelcerro
        notifyObservers(ProjectNotification.AFTER_SAVE_TO_STREAM, rootFolder);
703 41148 jjdelcerro
    }
704 44088 omartinez
705
    private File getNameFileForProject(File actualProjectFile, boolean error) {
706
        // if there is a error, creates a file with a unique timecode
707
        // not error: return path with .gvsproj.bak extension that could exists
708
709
        if (actualProjectFile==null) {
710
            LOG.info("Doesn't have previous project to create a bak");
711
            return null;
712
        }
713
        String noExt = FilenameUtils.removeExtension(actualProjectFile.getAbsolutePath());
714
        String timeCode = new SimpleDateFormat("_MMddHHmmss").format(new Date());
715
        String ext = ".gvsproj.bak";
716 40435 jjdelcerro
717 44088 omartinez
        int code = 0;
718
        if (noExt.length() > 230) {
719
            noExt = noExt.substring(0, 229);
720
        }
721
722
        File bakProjectFile;
723
        if (error) {
724
            bakProjectFile = new File(noExt + timeCode + ext);
725
            while (bakProjectFile.exists()) {
726
                code = code + 1;
727
                bakProjectFile = new File(noExt + timeCode + "_" + code + ext);
728
            }
729
        } else {
730
            bakProjectFile = new File(noExt + ext);
731
        }
732
        return bakProjectFile;
733
    }
734
    public File createProjectCopy(boolean error) {
735
        File actualProjectFile = ApplicationLocator.getProjectManager().getCurrentProject().getFile();
736
        File bakProjectFile = getNameFileForProject(actualProjectFile, error);
737
        if (bakProjectFile==null){
738
            return null;
739
        }
740
        if (!error){
741
            try {
742
                if (bakProjectFile.exists()){
743
                    FileUtils.deleteQuietly(bakProjectFile);
744
                }
745
            } catch (Exception ex) {
746
                LOG.error("Not possible to delete file", ex);
747
            }
748
            try {
749
                FileUtils.copyFile(actualProjectFile, bakProjectFile);
750
            } catch (IOException ex) {
751
                LOG.error("Can't create bak copy from project", ex);
752
            }
753
        } else {
754
            File bakProjectFile2 = getNameFileForProject(actualProjectFile, false);
755
            try {
756
                FileUtils.moveFile(bakProjectFile2, bakProjectFile);
757
            } catch (IOException ex) {
758
                LOG.error("Can't rename file", ex);
759
            }
760
        }
761
762
        return bakProjectFile;
763
    }
764
765 43913 jjdelcerro
    @Deprecated
766 44088 omartinez
    @Override
767 41148 jjdelcerro
    public void loadState(InputStream in) {
768
        loadState(in, null);
769
    }
770 40435 jjdelcerro
771 41148 jjdelcerro
    public void loadState(InputStream in, File rootFolder) {
772 43913 jjdelcerro
        if (notifyObservers(ProjectNotification.BEFORE_LOAD_FROM_STREAM, rootFolder).isProcessCanceled()) {
773 41148 jjdelcerro
            return;
774
        }
775
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
776
        try {
777 43439 jjdelcerro
            PersistentContext context = manager.getNewContext();
778
            context.setCollectErrors(true);
779
            PersistentState state = manager.loadState(in, context);
780 41148 jjdelcerro
            try {
781
                if (rootFolder != null) {
782
                    state.derelativizeFiles(rootFolder);
783
                }
784
            } catch (Exception ex) {
785
                state.getContext().addError(ex);
786
            }
787
            this.loadFromState(state);
788 42711 fdiaz
            this.unloadedObjects = getUnloadedObjects(state.getContext());
789 43439 jjdelcerro
            this.loadErrors = state.getContext().getErrors();
790 43879 jjdelcerro
791 41148 jjdelcerro
        } catch (PersistenceException e) {
792
            LOG.info("Can't load project to stream", e);
793
        }
794 42466 jjdelcerro
        this.fname = null;
795 43913 jjdelcerro
        notifyObservers(ProjectNotification.AFTER_LOAD_FROM_STREAM, rootFolder);
796 40435 jjdelcerro
797 41148 jjdelcerro
    }
798 40435 jjdelcerro
799 42711 fdiaz
800
801
    /**
802
     * @param context
803
     * @return
804
     */
805
    private Set<String> getUnloadedObjects(PersistentContext context) {
806
        Set unloadedObjects = new HashSet();
807
808
        Iterator statesIterator = context.iterator();
809
        String className = null;
810
811
        while (statesIterator.hasNext()) {
812
            PersistentState aState = (PersistentState) statesIterator.next();
813
            try {
814
                className = aState.getTheClassName();
815
                DynStruct definition = aState.getDefinition();
816
                if (definition == null) {
817
                    unloadedObjects.add(className);
818
                }
819
            } catch (Throwable e) {
820
                // do nothing
821
            }
822
        }
823
824
        if(unloadedObjects.isEmpty()){
825
            return null;
826
        }
827
828
        return unloadedObjects;
829
    }
830
831 43439 jjdelcerro
    @Override
832 42711 fdiaz
    public Set<String> getUnloadedObjects() {
833
        return this.unloadedObjects;
834
    }
835
836 43439 jjdelcerro
    @Override
837
    public List<Exception> getLoadErrors() {
838
        return this.loadErrors;
839
    }
840
841
    @Override
842 41148 jjdelcerro
    public void loadState(File in) {
843
        if (notifyObservers(ProjectNotification.BEFORE_LOAD_FROM_FILE, in).isProcessCanceled()) {
844
            return;
845
        }
846
        FileInputStream fin;
847
        try {
848
            fin = new FileInputStream(in);
849
            loadState(fin, new File(in.getParent()));
850
        } catch (FileNotFoundException e) {
851 42466 jjdelcerro
            LOG.info("Can't load project to stream", e);
852 41148 jjdelcerro
        }
853 44088 omartinez
        if ("bak".equals(FilenameUtils.getExtension(in.getAbsolutePath()))) {
854
            this.fname =  null;
855
        } else {
856
            this.fname = in;
857
        }
858 41148 jjdelcerro
        notifyObservers(ProjectNotification.AFTER_LOAD_FROM_FILE, in);
859
    }
860 40435 jjdelcerro
861 43913 jjdelcerro
    //@SuppressWarnings("unchecked")
862
    @Override
863 41148 jjdelcerro
    public void loadFromState(PersistentState state)
864
            throws PersistenceException {
865
        this.clean();
866 43976 jjdelcerro
        PersistentContext context = state.getContext();
867
868 43913 jjdelcerro
        notifyObservers(ProjectNotification.BEFORE_LOAD_FROM_STATE, state);
869 40435 jjdelcerro
870 41148 jjdelcerro
        this.setComments(state.getString("comments"));
871
        this.setCreationDate(state.getString("creationDate"));
872
        this.setModificationDate(state.getString("modificationDate"));
873
        this.setName(state.getString("name"));
874
        this.setOwner(state.getString("owner"));
875
        this.setSelectionColor((Color) state.get("selectionColor"));
876
        this.setProjection((IProjection) state.get("projection"));
877 40435 jjdelcerro
878 41350 jjdelcerro
        this.propertiesHelper = (ExtendedPropertiesHelper) state.get("propertiesHelper");
879 42088 fdiaz
880 41148 jjdelcerro
        List<ProjectExtent> extents = (List<ProjectExtent>) state
881
                .get("extents");
882
        for (int i = 0; i < extents.size(); i++) {
883
            this.addExtent(extents.get(i));
884
        }
885 40435 jjdelcerro
886 41148 jjdelcerro
        List<AbstractDocument> documents = (List<AbstractDocument>) state
887
                .get("documents");
888
        for (int i = 0; i < documents.size(); i++) {
889 43913 jjdelcerro
            AbstractDocument doc = documents.get(i);
890
            if( doc != null ) {
891
                this.add(doc);
892
            }
893 41148 jjdelcerro
        }
894 40435 jjdelcerro
895 43976 jjdelcerro
        try {
896
            List<DocumentWindowInfo> persistentWindows = (List<DocumentWindowInfo>) state.get("documentWindowsInformation");
897 40435 jjdelcerro
898 43976 jjdelcerro
            for (int i = 0; i < persistentWindows.size(); i++) {
899
                try {
900
                    DocumentWindowInfo persistentWindow = persistentWindows.get(i);
901
                    String docName = persistentWindow.getDocumentName();
902
                    String docType = persistentWindow.getDocumentType();
903
                    Document doc = this.getDocument(docName, docType);
904
                    IWindow win = doc.getFactory().getMainWindow(doc);
905
                    if(win!=null){
906
                        win.getWindowInfo().setWindowInfo(persistentWindow.getWindowInfo());
907
                        PluginServices.getMDIManager().addWindow(win);
908
                    }
909
                } catch(Exception ex) {
910
                    if( !context.getCollectErrors() ) {
911
                        throw ex;
912
                    }
913
                    context.addError(ex);
914
                }
915 42088 fdiaz
            }
916 40435 jjdelcerro
917 43976 jjdelcerro
            if (state.hasValue("projectWindowInfo")) {
918
                WindowInfo projectWindowInfo = (WindowInfo) state.get("projectWindowInfo");
919
                ProjectExtension pe = (ProjectExtension) PluginServices.getExtension(org.gvsig.app.extension.ProjectExtension.class);
920
                pe.setProject(this);
921
                pe.showProjectWindow(projectWindowInfo);
922
            }
923
        } catch(Exception ex) {
924
            if( !context.getCollectErrors() ) {
925
                throw ex;
926
            }
927
            context.addError(ex);
928 41148 jjdelcerro
        }
929 43913 jjdelcerro
        notifyObservers(ProjectNotification.AFTER_LOAD_FROM_STATE, state);
930 41148 jjdelcerro
    }
931 40435 jjdelcerro
932 44088 omartinez
    @Override
933 41148 jjdelcerro
    public void saveToState(PersistentState state) throws PersistenceException {
934
        state.set("version", VERSION);
935
        state.set("comments", getComments());
936
        state.set("creationDate", this.getCreationDate());
937 40435 jjdelcerro
938 41148 jjdelcerro
        state.set("modificationDate", this.getModificationDate());
939
        state.set("name", this.getName());
940
        state.set("owner", this.getOwner());
941
        state.set("selectionColor", this.getSelectionColor());
942 40435 jjdelcerro
943 41148 jjdelcerro
        state.set("projection", this.getProjection());
944 40435 jjdelcerro
945 41148 jjdelcerro
        state.set("extents", this.extents);
946 42088 fdiaz
        List<Document> docs = this.getDocuments();
947
        List<Document> noTempDocs = new ArrayList<Document>();
948
        for (Iterator iterator = docs.iterator(); iterator.hasNext();) {
949
            Document document = (Document) iterator.next();
950
            if(!document.isTemporary()){
951
                noTempDocs.add(document);
952
            }
953
        }
954
        state.set("documents", noTempDocs);
955 40435 jjdelcerro
956 41350 jjdelcerro
        state.set("propertiesHelper",propertiesHelper);
957 42088 fdiaz
958 41148 jjdelcerro
        List<DocumentWindowInfo> persistentWindows = new ArrayList<DocumentWindowInfo>();
959
        MDIManager mdiMan = PluginServices.getMDIManager();
960
        IWindow[] windows = mdiMan.getOrderedWindows();
961
        for (int i = windows.length - 1; i >= 0; i--) {
962
            IWindow window = windows[i];
963
            if (window instanceof IDocumentWindow) {
964
                WindowInfo wi = mdiMan.getWindowInfo(window);
965
                DocumentWindowInfo dwi = new DocumentWindowInfo(
966
                        wi,
967
                        ((IDocumentWindow) window).getDocument().getTypeName(),
968
                        ((IDocumentWindow) window).getDocument().getName());
969
                persistentWindows.add(dwi);
970
            } else if (window instanceof ProjectWindow) {
971
                state.set("projectWindowInfo", mdiMan.getWindowInfo(window));
972
            }
973
        }
974
        state.set("documentWindowsInformation", persistentWindows);
975 40435 jjdelcerro
976 41148 jjdelcerro
    }
977 40435 jjdelcerro
978 44088 omartinez
    @Override
979 41350 jjdelcerro
    public Object getProperty(Object key) {
980
        return this.propertiesHelper.getProperty(key);
981
    }
982
983 44088 omartinez
    @Override
984 41350 jjdelcerro
    public void setProperty(Object key, Object obj) {
985
        this.propertiesHelper.setProperty(key, obj);
986
    }
987
988 44088 omartinez
    @Override
989 41350 jjdelcerro
    public Map getExtendedProperties() {
990
        return this.propertiesHelper.getExtendedProperties();
991
    }
992
993 41148 jjdelcerro
    public static class DocumentWindowInfo implements Persistent {
994 40435 jjdelcerro
995 41148 jjdelcerro
        public static final String PERSISTENCE_DEFINITION_NAME = "DocumentWindowInfo";
996 40435 jjdelcerro
997 41148 jjdelcerro
        private WindowInfo windowInfo;
998
        private String documentType;
999
        private String documentName;
1000 40435 jjdelcerro
1001 41148 jjdelcerro
        public DocumentWindowInfo() {
1002
        }
1003 40435 jjdelcerro
1004 41148 jjdelcerro
        DocumentWindowInfo(WindowInfo wi, String docType, String docName) {
1005
            windowInfo = wi;
1006
            documentType = docType;
1007
            documentName = docName;
1008
        }
1009 40435 jjdelcerro
1010 41148 jjdelcerro
        public WindowInfo getWindowInfo() {
1011
            return windowInfo;
1012
        }
1013 40435 jjdelcerro
1014 41148 jjdelcerro
        public String getDocumentType() {
1015
            return documentType;
1016
        }
1017 40435 jjdelcerro
1018 41148 jjdelcerro
        public String getDocumentName() {
1019
            return documentName;
1020
        }
1021 40435 jjdelcerro
1022 41148 jjdelcerro
        public void saveToState(PersistentState state)
1023
                throws PersistenceException {
1024
            state.set("windowInfo", this.windowInfo);
1025
            state.set("documentType", this.documentType);
1026
            state.set("documentName", this.documentName);
1027
        }
1028 40435 jjdelcerro
1029 41148 jjdelcerro
        public void loadFromState(PersistentState state)
1030
                throws PersistenceException {
1031
            this.windowInfo = (WindowInfo) state.get("windowInfo");
1032
            this.documentType = state.getString("documentType");
1033
            this.documentName = state.getString("documentName");
1034
        }
1035 40435 jjdelcerro
1036 41148 jjdelcerro
        public static void registerPersistent() {
1037
            PersistenceManager manager = ToolsLocator.getPersistenceManager();
1038
            DynStruct definition = manager.getDefinition(PERSISTENCE_DEFINITION_NAME);
1039
            if (definition == null) {
1040
                definition = manager.addDefinition(
1041
                        DocumentWindowInfo.class,
1042
                        PERSISTENCE_DEFINITION_NAME,
1043
                        "DocumentWindowInfo persistence definition",
1044
                        null,
1045
                        null
1046
                );
1047
                definition.addDynFieldObject("windowInfo").setMandatory(true).setClassOfValue(WindowInfo.class);
1048
                definition.addDynFieldString("documentType").setMandatory(true);
1049
                definition.addDynFieldString("documentName").setMandatory(true);
1050
            }
1051 40435 jjdelcerro
1052 41148 jjdelcerro
        }
1053
    }
1054 40435 jjdelcerro
1055 41148 jjdelcerro
    public static void registerPersistent() {
1056
        AbstractDocument.registerPersistent();
1057
        DocumentWindowInfo.registerPersistent();
1058
        ProjectExtent.registerPersistent();
1059 40435 jjdelcerro
1060 41148 jjdelcerro
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
1061
        DynStruct definition = manager.addDefinition(DefaultProject.class,
1062
                "Project", "Project Persistence definition", null, null);
1063
        definition.addDynFieldString("version").setMandatory(true);
1064
        definition.addDynFieldString("comments").setMandatory(true);
1065
        definition.addDynFieldString("creationDate").setMandatory(true);
1066
        definition.addDynFieldString("modificationDate").setMandatory(true);
1067
        definition.addDynFieldString("name").setMandatory(true);
1068
        definition.addDynFieldString("owner").setMandatory(true);
1069 40435 jjdelcerro
1070 41148 jjdelcerro
        definition.addDynFieldObject("selectionColor")
1071
                .setClassOfValue(Color.class).setMandatory(true);
1072
        definition.addDynFieldObject("projection")
1073
                .setClassOfValue(IProjection.class).setMandatory(true);
1074 40435 jjdelcerro
1075 41148 jjdelcerro
        definition.addDynFieldList("extents")
1076
                .setClassOfItems(ProjectExtent.class).setMandatory(true);
1077 40435 jjdelcerro
1078 41148 jjdelcerro
        definition.addDynFieldList("documents").setClassOfItems(Document.class)
1079
                .setMandatory(true);
1080 40435 jjdelcerro
1081 41148 jjdelcerro
        definition.addDynFieldObject("projectWindowInfo").setClassOfValue(WindowInfo.class).setMandatory(false);
1082 40435 jjdelcerro
1083 41148 jjdelcerro
        definition.addDynFieldList("documentWindowsInformation").setClassOfItems(WindowInfo.class).setMandatory(false);
1084 40435 jjdelcerro
1085 42088 fdiaz
1086 41350 jjdelcerro
        definition.addDynFieldObject("propertiesHelper").setClassOfValue(ExtendedPropertiesHelper.class)
1087
                        .setMandatory(false);
1088
1089 41148 jjdelcerro
    }
1090 40435 jjdelcerro
1091 41148 jjdelcerro
    /**
1092
     * @deprecated use getPreferences().setDefaultSelectionColor()
1093
     */
1094
    public static void setDefaultSelectionColor(Color color) {
1095
        getPreferences().setDefaultSelectionColor(color);
1096
    }
1097 40435 jjdelcerro
1098 41148 jjdelcerro
    /**
1099
     * @deprecated use getPreferences().getDefaultSelectionColor()
1100
     */
1101
    public static Color getDefaultSelectionColor() {
1102
        return getPreferences().getDefaultSelectionColor();
1103
    }
1104 40435 jjdelcerro
1105 41148 jjdelcerro
    /**
1106
     * @deprecated use getPreferences().getDefaultMapUnits()
1107
     */
1108
    public static int getDefaultMapUnits() {
1109
        return getPreferences().getDefaultMapUnits();
1110
    }
1111 40435 jjdelcerro
1112 41148 jjdelcerro
    /**
1113
     * @deprecated use getPreferences().getDefaultDistanceUnits()
1114
     */
1115
    public static int getDefaultDistanceUnits() {
1116
        return getPreferences().getDefaultDistanceUnits();
1117
    }
1118 40435 jjdelcerro
1119 41148 jjdelcerro
    /**
1120
     * @deprecated use getPreferences().getDefaultDistanceArea()
1121
     */
1122
    public static int getDefaultDistanceArea() {
1123
        return getPreferences().getDefaultDistanceArea();
1124
    }
1125 40435 jjdelcerro
1126 41148 jjdelcerro
    /**
1127
     * @deprecated use getPreferences().setDefaultMapUnits()
1128
     */
1129
    public static void setDefaultMapUnits(int mapUnits) {
1130
        getPreferences().setDefaultMapUnits(mapUnits);
1131
    }
1132 40435 jjdelcerro
1133 41148 jjdelcerro
    /**
1134
     * @deprecated use getPreferences().setDefaultDistanceUnits()
1135
     */
1136
    public static void setDefaultDistanceUnits(int distanceUnits) {
1137
        getPreferences().setDefaultDistanceUnits(distanceUnits);
1138
    }
1139 40435 jjdelcerro
1140 41148 jjdelcerro
    /**
1141
     * @deprecated use getPreferences().setDefaultDistanceArea()
1142
     */
1143
    public static void setDefaultDistanceArea(int distanceArea) {
1144
        getPreferences().setDefaultDistanceArea(distanceArea);
1145
    }
1146 40435 jjdelcerro
1147 41148 jjdelcerro
    /**
1148
     * @deprecated use getPreferences().setDefaultProjection()
1149
     */
1150
    public static void setDefaultProjection(IProjection defaultProjection) {
1151
        getPreferences().setDefaultProjection(defaultProjection);
1152
    }
1153 40435 jjdelcerro
1154 41148 jjdelcerro
    /**
1155
     * @deprecated use getPreferences().getDefaultProjection()
1156
     */
1157
    public static IProjection getDefaultProjection() {
1158
        return getPreferences().getDefaultProjection();
1159
    }
1160 40435 jjdelcerro
1161 41148 jjdelcerro
    /**
1162
     * @deprecated see {@link #setSelectionColor(String)}, to be remove in gvSIG
1163
     * 2.1.0
1164
     */
1165
    public void setColor(String color) {
1166
        this.setSelectionColor(StringUtilities.string2Color(color));
1167
    }
1168 40435 jjdelcerro
1169 41148 jjdelcerro
    /**
1170
     * Return the selection color
1171
     *
1172
     * @return selection color as string
1173
     * @deprecated use {@link #getSelectionColor()}
1174
     */
1175
    public String getColor() {
1176
        return StringUtilities.color2String(selectionColor);
1177
    }
1178 40435 jjdelcerro
1179 41148 jjdelcerro
    /**
1180
     * Return the list of views of the project
1181
     *
1182
     * @return views as ArrayList of ProjectDocument
1183
     *
1184
     * @deprecated see {@link #getDocumentsByType(String)}
1185
     */
1186
    public List<Document> getViews() {
1187
        return getDocuments(ViewManager.TYPENAME);
1188
    }
1189 40435 jjdelcerro
1190 41148 jjdelcerro
    /**
1191
     * Add a view to the project
1192
     *
1193
     * @deprecated see {@link #add(AbstractDocument)}
1194
     */
1195
    public void addView(DefaultViewDocument v) {
1196
        add(v);
1197
    }
1198 40435 jjdelcerro
1199 41148 jjdelcerro
    /**
1200
     * Remove a view of the project
1201
     *
1202
     * @param index of the view as integer
1203
     *
1204
     * @deprecated see {@link #remove(AbstractDocument)}
1205
     */
1206
    public void delView(int i) {
1207
        List<Document> list = getDocuments(ViewManager.TYPENAME);
1208
        remove(list.get(i));
1209
    }
1210 40435 jjdelcerro
1211 41148 jjdelcerro
    /**
1212
     * @deprecated see {@link #getDocument(String, String)}
1213
     */
1214
    public Document getProjectDocumentByName(String name, String type) {
1215
        return this.getDocument(name, type);
1216
    }
1217 40435 jjdelcerro
1218 41148 jjdelcerro
    /**
1219
     * @deprecated see {@link #getDocuments(String)}
1220
     */
1221
    public List<Document> getDocumentsByType(String type) {
1222
        return this.getDocuments(type);
1223
    }
1224 40435 jjdelcerro
1225 41148 jjdelcerro
    /**
1226
     * @deprecated aun por decidir que API darle al copy/paste
1227
     */
1228
    public String exportToXML(AbstractDocument[] selectedItems)
1229
            throws SaveException {
1230
        // FIXME jjdc:hay que decirdir que API darle al copy/paste
1231
        throw new UnsupportedOperationException("This method is not supported");
1232
    }
1233 40435 jjdelcerro
1234 41148 jjdelcerro
    /**
1235
     * @deprecated aun por decidir que API darle al copy/paste
1236
     */
1237
    public void importFromXML(String sourceString, String docType) {
1238
        // FIXME jjdc:hay que decirdir que API darle al copy/paste
1239
        throw new UnsupportedOperationException("This method is not supported");
1240
    }
1241 40435 jjdelcerro
1242 41148 jjdelcerro
    /**
1243
     * @deprecated aun por decidir que API darle al copy/paste
1244
     */
1245
    public boolean isValidXMLForImport(String sourceString, String docType) {
1246
        // FIXME jjdc:hay que decirdir que API darle al copy/paste
1247
        throw new UnsupportedOperationException("This method is not supported");
1248
    }
1249
1250 44088 omartinez
    @Override
1251 41148 jjdelcerro
    public boolean canImportDocuments(String data, String doctype) {
1252
        // TODO Auto-generated method stub
1253
        return false;
1254
    }
1255
1256 44088 omartinez
    @Override
1257 41148 jjdelcerro
    public String exportDocumentsAsText(List<Document> documents) {
1258
        // TODO Auto-generated method stub
1259
        return null;
1260
    }
1261
1262 44088 omartinez
    @Override
1263 41148 jjdelcerro
    public void importDocuments(String data, String doctype) {
1264
        // TODO Auto-generated method stub
1265
1266
    }
1267
1268 44088 omartinez
    @Override
1269 41148 jjdelcerro
    public Document getActiveDocument() {
1270 41248 jjdelcerro
        return this.getActiveDocument((Class<? extends Document>)null);
1271 41148 jjdelcerro
    }
1272
1273 44088 omartinez
    @Override
1274 41248 jjdelcerro
    public Document getActiveDocument(String documentTypeName) {
1275
        ApplicationManager application = ApplicationLocator.getManager();
1276
1277
        Document document = null;
1278
        IWindow[] windows = application.getUIManager().getOrderedWindows();
1279
        IWindow window = null;
1280
        for (int i = 0; i < windows.length; i++) {
1281
            window = windows[i];
1282
            if (window instanceof SingletonWindow) {
1283
                // Cogemos no la primera ventana, si no la primera
1284
                // ventana de tipo documento (SingletonWindow).
1285
                // Y por si las mosca no es un documento, atrapamos
1286
                // los errores y continuamos si no puede hacer un cast
1287
                // del Model a Document
1288
                try {
1289
                    document = (Document) ((SingletonWindow) window).getWindowModel();
1290
                    if (documentTypeName == null) {
1291
                        return document;
1292
                    }
1293
                    if( document.getTypeName().equalsIgnoreCase(documentTypeName) ) {
1294
                        return document;
1295
                    }
1296
                    if( document instanceof DocumentsContainer ) {
1297
                        Document subdoc = ((DocumentsContainer)document).getActiveDocument(documentTypeName);
1298
                        return subdoc;
1299
                    }
1300 42088 fdiaz
1301 41248 jjdelcerro
                } catch (ClassCastException e) {
1302
                    // Do nothing, skip this window
1303
                }
1304
            }
1305
        }
1306
        return null;
1307
    }
1308 42088 fdiaz
1309 44088 omartinez
    @Override
1310 41148 jjdelcerro
    public Document getActiveDocument(Class<? extends Document> documentClass) {
1311
        ApplicationManager application = ApplicationLocator.getManager();
1312
1313
        Document document = null;
1314
        IWindow[] windows = application.getUIManager().getOrderedWindows();
1315
        IWindow window = null;
1316
        for (int i = 0; i < windows.length; i++) {
1317
            window = windows[i];
1318 42165 fdiaz
            if (window instanceof SingletonWindow && window instanceof IDocumentWindow) {
1319 41248 jjdelcerro
                // Cogemos no la primera ventana, si no la primera
1320 41148 jjdelcerro
                // ventana de tipo documento (SingletonWindow).
1321
                // Y por si las mosca no es un documento, atrapamos
1322
                // los errores y continuamos si no puede hacer un cast
1323
                // del Model a Document
1324
                try {
1325
                    document = (Document) ((SingletonWindow) window).getWindowModel();
1326
                    if (documentClass == null) {
1327
                        return document;
1328
                    }
1329
                    if (documentClass.isAssignableFrom(document.getClass())) {
1330
                        return document;
1331
                    }
1332 41248 jjdelcerro
                    if( document instanceof DocumentsContainer ) {
1333
                        Document subdoc = ((DocumentsContainer)document).getActiveDocument(documentClass);
1334
                        return subdoc;
1335
                    }
1336 42088 fdiaz
1337 41148 jjdelcerro
                } catch (ClassCastException e) {
1338
                    // Do nothing, skip this window
1339
                }
1340
            }
1341
        }
1342
        return null;
1343
    }
1344 40435 jjdelcerro
1345 44088 omartinez
    @Override
1346 41148 jjdelcerro
    public void addObserver(Observer o) {
1347
        observableHelper.addObserver(o);
1348
    }
1349 40435 jjdelcerro
1350 44088 omartinez
    @Override
1351 41148 jjdelcerro
    public void deleteObserver(Observer o) {
1352
        observableHelper.deleteObserver(o);
1353
    }
1354
1355 44088 omartinez
    @Override
1356 41148 jjdelcerro
    public void deleteObservers() {
1357
        observableHelper.deleteObservers();
1358
    }
1359
1360
    private ProjectNotification notifyObservers(int type) {
1361 43913 jjdelcerro
        return notifyObservers(new DefaultProjectNotification(type));
1362 41148 jjdelcerro
    }
1363 40435 jjdelcerro
1364 41148 jjdelcerro
    private ProjectNotification notifyObservers(int type, Document document) {
1365 43913 jjdelcerro
        return notifyObservers(new DefaultProjectNotification(type, document));
1366 41148 jjdelcerro
    }
1367 43913 jjdelcerro
1368
    private ProjectNotification notifyObservers(int type, PersistentState state) {
1369
        return notifyObservers(new DefaultProjectNotification(type, state));
1370
    }
1371 40435 jjdelcerro
1372 41148 jjdelcerro
    private ProjectNotification notifyObservers(int type, File file) {
1373 43913 jjdelcerro
        return notifyObservers(new DefaultProjectNotification(type, file));
1374
    }
1375
1376
    private ProjectNotification notifyObservers(ProjectNotification notifycation) {
1377 41148 jjdelcerro
        try {
1378
            observableHelper.notifyObservers(this, notifycation);
1379
        } catch (Exception ex) {
1380
            LOG.info("Can't notify observers", ex);
1381
        }
1382
        return notifycation;
1383
    }
1384 43913 jjdelcerro
1385 41148 jjdelcerro
    class DefaultProjectNotification implements ProjectNotification {
1386 40435 jjdelcerro
1387 41148 jjdelcerro
        private int type;
1388
        private Document document;
1389
        private File file;
1390
        private boolean processCanceled = false;
1391 43913 jjdelcerro
        private PersistentState state = null;
1392 40435 jjdelcerro
1393 43913 jjdelcerro
        DefaultProjectNotification(int type) {
1394 41148 jjdelcerro
            this.type = type;
1395 43913 jjdelcerro
        }
1396
1397
        DefaultProjectNotification(int type, Document document) {
1398
            this(type);
1399 41148 jjdelcerro
            this.document = document;
1400 43913 jjdelcerro
        }
1401
1402
        DefaultProjectNotification(int type, File file) {
1403
            this(type);
1404 41148 jjdelcerro
            this.file = file;
1405
        }
1406 40435 jjdelcerro
1407 43913 jjdelcerro
        DefaultProjectNotification(int type, PersistentState state) {
1408
            this(type);
1409
            this.state = state;
1410
        }
1411
1412
        @Override
1413 41148 jjdelcerro
        public int getNotificationType() {
1414
            return type;
1415
        }
1416 40435 jjdelcerro
1417 43913 jjdelcerro
        @Override
1418 41148 jjdelcerro
        public Document getDocument() {
1419
            return document;
1420
        }
1421
1422 43913 jjdelcerro
        @Override
1423 41148 jjdelcerro
        public void cancelProcess() {
1424
            processCanceled = true;
1425
        }
1426
1427 43913 jjdelcerro
        @Override
1428 41148 jjdelcerro
        public boolean isProcessCanceled() {
1429
            return processCanceled;
1430
        }
1431
1432 43913 jjdelcerro
        @Override
1433 41148 jjdelcerro
        public File getFile() {
1434
            return file;
1435
        }
1436
1437 43913 jjdelcerro
        @Override
1438
        public PersistentState getState() {
1439
            return this.state;
1440
        }
1441
1442
        @Override
1443
        public Project getProject() {
1444
            return DefaultProject.this;
1445
        }
1446 41148 jjdelcerro
    }
1447
1448 40435 jjdelcerro
}