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

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