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

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