Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / DefaultProject.java @ 39128

History | View | Annotate | Download (33.4 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2009 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 * 
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 * 
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 * 
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
 * MA  02110-1301, USA.
20
 * 
21
 */
22

    
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2004-2009 IVER TI
26
 *   
27
 */
28

    
29
package org.gvsig.app.project;
30

    
31
import java.awt.Color;
32
import java.beans.PropertyChangeEvent;
33
import java.beans.PropertyChangeListener;
34
import java.beans.PropertyChangeSupport;
35
import java.io.File;
36
import java.io.FileInputStream;
37
import java.io.FileNotFoundException;
38
import java.io.FileOutputStream;
39
import java.io.InputStream;
40
import java.io.OutputStream;
41
import java.io.Serializable;
42
import java.net.MalformedURLException;
43
import java.net.URI;
44
import java.net.URISyntaxException;
45
import java.net.URL;
46
import java.text.DateFormat;
47
import java.text.MessageFormat;
48
import java.util.ArrayList;
49
import java.util.Collections;
50
import java.util.Date;
51
import java.util.HashMap;
52
import java.util.Iterator;
53
import java.util.List;
54
import java.util.Map;
55
import java.util.regex.Pattern;
56

    
57
import org.cresques.cts.IProjection;
58
import org.slf4j.Logger;
59
import org.slf4j.LoggerFactory;
60

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

    
91
/**
92
 * Clase que representa un proyecto de gvSIG
93
 * 
94
 * @author 2004-2005 Fernando Gonz?lez Cort?s
95
 * @author 2006-2009 Jose Manuel Vivo
96
 * @author 2005- Vicente Caballero
97
 * @author 2009- Joaquin del Cerro
98
 * 
99
 */
100

    
101
public class DefaultProject implements Serializable, PropertyChangeListener,
102
                Project {
103

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

    
110
        /**
111
         * 
112
         */
113
        private static final long serialVersionUID = -4449622027521773178L;
114

    
115
        private static final Logger logger = LoggerFactory.getLogger(Project.class);
116

    
117
        private static ProjectPreferences preferences = new ProjectPreferences();
118

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

    
124
        private PropertyChangeSupport change;
125

    
126
        private boolean modified = false;
127

    
128
        private String name = null;
129

    
130
        private String creationDate = null;
131

    
132
        private String modificationDate = null;
133

    
134
        private String owner = null;
135

    
136
        private String comments = null;
137

    
138
        private Color selectionColor = null;
139

    
140
        private List<Document> documents = null;
141

    
142
        private List<ProjectExtent> extents = null;
143

    
144
        private IProjection projection;
145

    
146
        /**
147
         * Creates a new Project object.
148
         */
149
        DefaultProject() {
150
                this.change = new PropertyChangeSupport(this);
151
                this.clean();
152
        }
153

    
154
        protected void clean() {
155
                this.owner = "";
156
                this.comments = "";
157
                this.name = PluginServices.getText(this, "untitled");
158
                this.creationDate = DateFormat.getDateInstance().format(new Date());
159
                this.modificationDate = this.creationDate;
160

    
161
                this.documents = new ArrayList<Document>();
162
                this.extents = new ArrayList<ProjectExtent>();
163

    
164
                this.setSelectionColor(getPreferences().getDefaultSelectionColor());
165

    
166
                this.projection = null; // se inicializa en el getProjection()
167
        }
168

    
169
        public static ProjectPreferences getPreferences() {
170
                return preferences;
171
        }
172

    
173
        public void propertyChange(PropertyChangeEvent evt) {
174
                change.firePropertyChange(evt);
175
        }
176

    
177
        public synchronized void addPropertyChangeListener(
178
                        PropertyChangeListener arg0) {
179
                change.addPropertyChangeListener(arg0);
180
        }
181

    
182
        /**
183
         * Return the creation date of the project
184
         * 
185
         * @return
186
         */
187
        public String getCreationDate() {
188
                return creationDate;
189
        }
190

    
191
        protected void setCreationDate(String creationDate) {
192
                this.creationDate = creationDate;
193
                change.firePropertyChange("setCreationDate", null, null);
194
        }
195

    
196
    public Document createDocument(String type) {
197
            logger.info("createDocument('{}')",type);
198
        return ProjectManager.getInstance().createDocument(type);
199
    }
200

    
201
    /**
202
     * Return the name of the project
203
     * 
204
     * @return
205
     */
206
    public String getName() {
207
        return name;
208
    }
209

    
210
        /**
211
         * Set the name of he project.
212
         * 
213
         * @param string
214
         */
215
        public void setName(String name) {
216
                this.name = name;
217
                change.firePropertyChange("setName", null, null);
218
        }
219

    
220
        /**
221
         * Return the comments associateds with the project
222
         * 
223
         * @return comments
224
         */
225
        public String getComments() {
226
                return comments;
227
        }
228

    
229
        /**
230
         * Set the comments associateds with the project
231
         * 
232
         * @param comments
233
         *            as string
234
         */
235
        public void setComments(String string) {
236
                comments = string;
237
                change.firePropertyChange("setComments", null, null);
238
        }
239

    
240
        /**
241
         * Retuen the modification date of the project.
242
         * 
243
         * @return modification date as string
244
         */
245
        public String getModificationDate() {
246
                return modificationDate;
247
        }
248

    
249
        protected void setModificationDate(String string) {
250
                modificationDate = string;
251
                change.firePropertyChange("setModificationDate", null, null);
252
        }
253

    
254
        /**
255
         * Return the author of the project,
256
         * 
257
         * @return author as string
258
         */
259
        public String getOwner() {
260
                return owner;
261
        }
262

    
263
        /**
264
         * Sets the author of the project
265
         * 
266
         * @param author
267
         *            name as string
268
         */
269
        public void setOwner(String owner) {
270
                this.owner = owner;
271
                change.firePropertyChange("setOwner", null, null);
272
        }
273

    
274
        /**
275
         * Obtiene el color de selecci?n que se usar? en el proyecto
276
         * 
277
         * @return
278
         */
279
        public Color getSelectionColor() {
280
                if (selectionColor == null) {
281
                        selectionColor = getPreferences().getDefaultSelectionColor();
282
                }
283
                return selectionColor;
284
        }
285

    
286
        /**
287
         * Sets the selecction color
288
         * 
289
         * @param selection
290
         *            color as string
291
         */
292
        public void setSelectionColor(String selectionColor) {
293
                this.setSelectionColor(StringUtilities.string2Color(selectionColor));
294
        }
295

    
296
        /**
297
         * Sets the selecction color
298
         * 
299
         * @param selection
300
         *            color as Color
301
         */
302
        public void setSelectionColor(Color selectionColor) {
303
                this.selectionColor = selectionColor;
304
                MapContext.setSelectionColor(selectionColor);
305
                change.firePropertyChange("selectionColor", null, selectionColor);
306
        }
307

    
308
        public IProjection getProjection() {
309
                if (projection == null) {
310
                        projection = getPreferences().getDefaultProjection();
311
                }
312
                return projection;
313
        }
314

    
315
        public void setProjection(IProjection projection) {
316
                this.projection = projection;
317
        }
318

    
319
        /**
320
         * Sets the modified state of project.
321
         * 
322
         * Can't set to not modified.
323
         * 
324
         * @param modified
325
         *            as boolean
326
         */
327
        public void setModified(boolean modified) {
328
                this.modified = modified;
329
                if (modified == false) {
330
                        List<Document> documents = this.getDocuments();
331
                        for (int i = 0; i < documents.size(); i++) {
332
                                documents.get(i).setModified(false);
333
                        }
334
                }
335
        }
336

    
337
        public boolean hasChanged() {
338
                // we return true if the project is not empty (until we have a better
339
                // method...)
340
                if ((this.getDocuments().size() != 0) || modified) {
341
                        return true;
342
                }
343
                return false;
344
        }
345

    
346
        /**
347
         * Return a list of documents in the project.
348
         * 
349
         * @return documents as List of IProjectDocument
350
         */
351
        public List<Document> getDocuments() {
352
                return Collections.unmodifiableList(documents);
353
        }
354

    
355
        /**
356
         * Return a list with all documents of especified type.
357
         * 
358
         * @param type
359
         *            of document
360
         * 
361
         * @return List of IProjectDocument
362
         */
363
        public List<Document> getDocuments(String type) {
364
                List<Document> docs = new ArrayList<Document>();
365
                if (type != null) {
366
                        for (Document document : this.documents) {
367
                                if (type.equalsIgnoreCase(document.getTypeName())) {
368
                                        docs.add(document);
369
                                }
370
                        }
371
                }
372
                return Collections.unmodifiableList(docs);
373
        }
374

    
375
        /**
376
         * Adds a document to the project
377
         * 
378
         * @param document
379
         *            as IProjectDocument
380
         */
381
        public void add(Document document) {
382
        logger.info("add('{}')", document.toString());
383

    
384
                document.addPropertyChangeListener(this);
385
                document.setProject(this);
386
                document.setName(this.getUniqueNameForDocument(document.getTypeName(),
387
                                document.getName()));
388
                documents.add(document);
389
                document.afterAdd();
390
                this.setModified(true);
391
        change.firePropertyChange("addDocument", null, document);
392
        }
393

    
394
        /**
395
         * Remove a document of the project
396
         * 
397
         * @param document
398
         *            as IProjectDocument
399
         */
400
        public void remove(Document doc) {
401
        logger.info("remove('{}')", doc.toString());
402
                documents.remove(doc);
403
                this.setModified(true);
404
        change.firePropertyChange("delDocument", doc, null);
405
                doc.afterRemove();
406
        }
407

    
408
        public Iterator<Document> iterator() {
409
                return documents.iterator();
410
        }
411

    
412
        public boolean isEmpty() {
413
                return documents.isEmpty();
414
        }
415

    
416
        /**
417
         * Return the view that contains the especified layer.
418
         * 
419
         * @param layer
420
         * 
421
         * @return name of the view that contains the layer
422
         * 
423
         * @throws RuntimeException
424
         *             Si la capa que se pasa como par?metro no se encuentra en
425
         *             ninguna vista
426
         */
427
        public String getViewName(FLayer layer) {
428
                List<Document> views = getDocuments(ViewManager.TYPENAME);
429
                for (int v = 0; v < views.size(); v++) {
430
                        DefaultViewDocument pView = (DefaultViewDocument) views.get(v);
431
                        FLayers layers = pView.getMapContext().getLayers();
432
                        if (isView(layers, layer)) {
433
                                return pView.getName();
434
                        }
435
                }
436

    
437
                throw new RuntimeException(MessageFormat.format(
438
                                "The layer '{1}' is not in a view", layer.getName()));
439
        }
440

    
441
        private boolean isView(FLayers layers, FLayer layer) {
442
                for (int i = 0; i < layers.getLayersCount(); i++) {
443
                        if (layers.getLayer(i) instanceof FLayers) {
444
                                if (isView((FLayers) layers.getLayer(i), layer)) {
445
                                        return true;
446
                                }
447
                        }
448
                        if (layers.getLayer(i) == layer) {
449
                                return true;
450
                        }
451
                }
452
                return false;
453
        }
454

    
455
        public void addExtent(ProjectExtent arg1) {
456
                extents.add(arg1);
457
                change.firePropertyChange("addExtent", null, null);
458
        }
459

    
460
        public ProjectExtent removeExtent(int arg0) {
461
                change.firePropertyChange("delExtent", null, null);
462
                return extents.remove(arg0);
463
        }
464

    
465
        public ProjectExtent[] getExtents() {
466
                return (ProjectExtent[]) extents.toArray(new ProjectExtent[0]);
467
        }
468

    
469
        /**
470
         * Obtiene un documento a partir de su nombre y el nombre de registro en el
471
         * pointExtension, este ?ltimo se puede obtener del
472
         * Project****Factory.registerName.
473
         * 
474
         * @param name
475
         *            Nombre del documento
476
         * @param type
477
         *            nombre de registro en el extensionPoint
478
         * 
479
         * @return Documento
480
         */
481
        public Document getDocument(String name, String type) {
482
                if (type != null) {
483
                        for (int i = 0; i < documents.size(); i++) {
484
                                Document document = documents.get(i);
485
                                if (type.equalsIgnoreCase(document.getTypeName())
486
                                                && name.equalsIgnoreCase(document.getName())) {
487
                                        return document;
488
                                }
489
                        }
490
                }
491
                return null;
492
        }
493

    
494
        public String getUniqueNameForDocument(String type, String name) {
495
                Document document = getDocument(name, type);
496
                if (document == null) {
497
                        return name;
498
                }
499

    
500
                String newName = null;
501
                int num = this.getNextDocumentIndex(type);
502
                while (document != null) {
503
                        newName = name + " - " + num++;
504
                        document = getDocument(newName, type);
505
                }
506
                this.setNextDocumentIndex(type, num);
507
                return newName;
508
        }
509

    
510
        private int getNextDocumentIndex(String type) {
511
                if (nextDocumentIndexByType.get(type) == null) {
512
                        nextDocumentIndexByType.put(type, new Integer(1));
513
                        return 1;
514
                }
515
                return nextDocumentIndexByType.get(type).intValue();
516
        }
517

    
518
        private void setNextDocumentIndex(String type, int newIndex) {
519
                if (nextDocumentIndexByType.get(type) == null) {
520
                        nextDocumentIndexByType.put(type, new Integer(newIndex));
521
                } else {
522
                        nextDocumentIndexByType.put(type, new Integer(newIndex));
523
                }
524
        }
525

    
526
        public void saveState(File out) throws PersistenceException {
527
                FileOutputStream fout;
528
                try {
529
                        fout = new FileOutputStream(out);
530
                        saveState(fout, new File(out.getParent()));
531
                } catch (FileNotFoundException e) {
532
                        throw new PersistenceException(e);
533
                }
534
        }
535

    
536
        public void saveState(OutputStream out) throws PersistenceException {
537
                saveState(out, null);
538
        }
539

    
540
        public void saveState(OutputStream out, File rootFolder)
541
                        throws PersistenceException {
542
                PersistenceManager manager = ToolsLocator.getPersistenceManager();
543
                PersistentState state = null;
544
                state = manager.getState(this, true);
545
                try {
546
                        if (rootFolder != null) {
547
                                relativizeFiles(state, rootFolder);
548
                        }
549
                } catch (Exception ex) {
550
                        state.getContext().addError(ex);
551
                }
552
                manager.saveState(state, out);
553
                if (state.getContext().getErrors() != null) {
554
                        throw state.getContext().getErrors();
555
                }
556
        }
557

    
558
        private void relativizeFiles(PersistentState state, File rootFolder) {
559
                PersistentContext context = state.getContext();
560

    
561
                URI cwd = new File(System.getProperty("user.dir")).toURI();
562
                @SuppressWarnings("unchecked")
563
                Iterator<PersistentState> statesIterator = context.iterator();
564
                while (statesIterator.hasNext()) {
565
                        PersistentState aState = statesIterator.next();
566
                        DynStruct definition = aState.getDefinition();
567
                        DynField[] fields = definition.getDynFields();
568
                        for (DynField field : fields) {
569
                                if (field.getType() == DataTypes.FILE
570
                                                || field.getType() == DataTypes.FOLDER) {
571
                                        try {
572
                                                File value = aState.getFile(field.getName());
573
                                                value = relativizeFile(value, rootFolder);
574
                                                aState.set(field.getName(), value);
575
                                        } catch (PersistenceException e) {
576
                                                LOG.warn(
577
                                                                "Can't relativice field '" + field.getName()
578
                                                                                + "' for class '"
579
                                                                                + definition.getName() + "'.", e);
580
                                        }
581
                                } else if (field.getType() == DataTypes.URL) {
582
                                        try {
583
                                                URL value = aState.getURL(field.getName());
584
                                                if ("FILE".equalsIgnoreCase(value.getProtocol())) {
585
                                                        File file = new File(value.toURI());
586
                                                        if (differentDrives(file, rootFolder)) {
587
                                /*
588
                                 * Different drives
589
                                 * (should happen only in Windows)
590
                                 */
591
                                value = file.toURI().toURL();
592
                                                        } else {
593
                                /*
594
                                * Both in the Same drive (C: D: etc) or Unix-style
595
                                */
596
                                file = relativizeFile(file, rootFolder);
597
                                value = new URL("file","",file.toURI().toString().substring(cwd.toString().length()));
598
                                                        }
599
                                                        aState.set(field.getName(), value);
600
                                                }
601
                                        } catch (PersistenceException e) {
602
                                                // do nothind
603
                                        } catch (MalformedURLException e) {
604
                                                // do nothind
605
                                        } catch (URISyntaxException e) {
606
                                                // do nothind
607
                                        }
608
                                }
609
                        }
610
                }
611
        }
612

    
613
    private boolean differentDrives(File file, File rootFolder) {
614

    
615
        if (rootFolder == null) {
616
            return false;
617
        }
618

    
619
        if (file == null) {
620
            return false;
621
        }
622
        String basePath = rootFolder.getAbsolutePath();
623
        String targetPath = file.getPath();
624
        String pathSeparator = File.separator;
625
        // We need the -1 argument to split to make sure we get a trailing
626
        // "" token if the base ends in the path separator and is therefore
627
        // a directory. We require directory paths to end in the path
628
        // separator -- otherwise they are indistinguishable from files.
629
        String[] base = basePath.split(Pattern.quote(pathSeparator), -1);
630
        String[] target = targetPath.split(Pattern.quote(pathSeparator), 0);
631
        /*
632
         * If first component is different then they are different
633
         * drives. In Unix-type file systems, the first for both
634
         * should be "" so this will not happen
635
         */
636
        return !target[0].equals(base[0]);
637
    }
638
        
639
        private File relativizeFile(File file, File rootFolder) {
640
                if (rootFolder == null) {
641
                        return file;
642
                }
643
                if (file == null) {
644
                        return null;
645
                }
646
                boolean isDir = false;
647
                // isDir = file.isDirectory();
648
                isDir = rootFolder.isDirectory();
649
                String basePath = rootFolder.getAbsolutePath();
650
                String targetPath = file.getPath();
651
                String pathSeparator = File.separator;
652
                // We need the -1 argument to split to make sure we get a trailing
653
                // "" token if the base ends in the path separator and is therefore
654
                // a directory. We require directory paths to end in the path
655
                // separator -- otherwise they are indistinguishable from files.
656
                String[] base = basePath.split(Pattern.quote(pathSeparator), -1);
657
                String[] target = targetPath.split(Pattern.quote(pathSeparator), 0);
658

    
659
                // First get all the common elements. Store them as a string,
660
                // and also count how many of them there are.
661
                String common = "";
662
                int commonIndex = 0;
663
                for (int i = 0; i < target.length && i < base.length; i++) {
664
                        if (target[i].equals(base[i])) {
665
                                common += target[i] + pathSeparator;
666
                                commonIndex++;
667
                        } else
668
                                break;
669
                }
670

    
671
                if (commonIndex == 0) {
672
                        // Whoops -- not even a single common path element. This most
673
                        // likely indicates differing drive letters, like C: and D:.
674
                        // These paths cannot be relativized. Return the target path.
675
                        return file;
676
                        // This should never happen when all absolute paths
677
                        // begin with / as in *nix.
678
                }
679

    
680
                String relative = "";
681
                if (base.length == commonIndex) {
682
                        // Comment this out if you prefer that a relative path not start
683
                        // with ./
684
                        relative = "." + pathSeparator;
685
                } else {
686
                        int numDirsUp = base.length - commonIndex - (isDir ? 0 : 1); /*
687
                                                                                                                                                 * only
688
                                                                                                                                                 * subtract
689
                                                                                                                                                 * 1 if
690
                                                                                                                                                 * it is
691
                                                                                                                                                 * a
692
                                                                                                                                                 * file.
693
                                                                                                                                                 */
694
                        // The number of directories we have to backtrack is the length of
695
                        // the base path MINUS the number of common path elements, minus
696
                        // one because the last element in the path isn't a directory.
697
                        for (int i = 1; i <= (numDirsUp); i++) {
698
                                relative += ".." + pathSeparator;
699
                        }
700
                }
701
                // if we are comparing directories then we
702
                if (targetPath.length() > common.length()) {
703
                        // it's OK, it isn't a directory
704
                        relative += targetPath.substring(common.length());
705
                }
706

    
707
                return new File(relative);
708
        }
709

    
710
        private void fixFiles(PersistentState state, File rootFolder) {
711
                PersistentContext context = state.getContext();
712
                URI cwd = new File(System.getProperty("user.dir")).toURI();
713

    
714
                @SuppressWarnings("unchecked")
715
                Iterator<PersistentState> statesIterator = context.iterator();
716
                while (statesIterator.hasNext()) {
717
                        PersistentState aState = statesIterator.next();
718
                        DynStruct definition = aState.getDefinition();
719
                        DynField[] fields = definition.getDynFields();
720
                        for (DynField field : fields) {
721
                                if (field.getType() == DataTypes.FILE
722
                                                || field.getType() == DataTypes.FOLDER) {
723
                                        try {
724
                                                File value = aState.getFile(field.getName());
725
                                                value = fixFile(value, rootFolder);
726
                                                aState.set(field.getName(), value);
727
                                        } catch (PersistenceException e) {
728
                                                LOG.warn(
729
                                                                "Can't fix field '" + field.getName()
730
                                                                                + "' for class '"
731
                                                                                + definition.getName() + "'.", e);
732
                                        }
733
                                } else if (field.getType() == DataTypes.URL) {
734
                                        try {
735
                                                URL value = aState.getURL(field.getName());
736
                                                if ("FILE".equalsIgnoreCase(value.getProtocol())) {
737
                                                        if (!value.getFile().startsWith("/")){
738
                                                                value = new URL("file","",cwd.getRawPath()+value.getFile());
739
                                                                File file = new File(value.toURI().getPath().substring(cwd.getPath().length()));
740
                                                                file = fixFile(file, rootFolder);
741
                                                                aState.set(field.getName(), file.toURI().toURL());
742
                                                        }
743
                                                }
744
                                        } catch (PersistenceException e) {
745
                                                // do nothing
746
                                        } catch (MalformedURLException e) {
747
                                                // do nothing
748
                                        } catch (URISyntaxException e) {
749
                                                // do nothing
750
                                        }
751

    
752
                                }
753
                        }
754
                }
755
        }
756

    
757
        private File fixFile(File file, File rootFolder) {
758
                if (file.isAbsolute()) {
759
                        return file;
760
                }
761
                return new File(rootFolder, file.getPath()).getAbsoluteFile();
762
        }
763

    
764
        public void loadState(InputStream in) {
765
                loadState(in, null);
766
        }
767

    
768
        public void loadState(InputStream in, File rootFolder) {
769
                PersistenceManager manager = ToolsLocator.getPersistenceManager();
770
                try {
771
                        PersistentState state = manager.loadState(in);
772
                        try {
773
                                if (rootFolder != null) {
774
                                        fixFiles(state, rootFolder);
775
                                }
776
                        } catch (Exception ex) {
777
                                state.getContext().addError(ex);
778
                        }
779
                        this.loadFromState(state);
780
                } catch (PersistenceException e) {
781
                        // TODO Auto-generated catch block
782
                        e.printStackTrace();
783
                }
784

    
785
        }
786

    
787
        public void loadState(File in) {
788
                FileInputStream fin;
789
                try {
790
                        fin = new FileInputStream(in);
791
                        loadState(fin, new File(in.getParent()));
792
                } catch (FileNotFoundException e) {
793
                        // TODO Auto-generated catch block
794
                        e.printStackTrace();
795
                }
796
        }
797

    
798
        @SuppressWarnings("unchecked")
799
        public void loadFromState(PersistentState state)
800
                        throws PersistenceException {
801
                this.clean();
802

    
803
                this.setComments(state.getString("comments"));
804
                this.setCreationDate(state.getString("creationDate"));
805
                this.setModificationDate(state.getString("modificationDate"));
806
                this.setName(state.getString("name"));
807
                this.setOwner(state.getString("owner"));
808
                this.setSelectionColor((Color) state.get("selectionColor"));
809
                this.setProjection((IProjection) state.get("projection"));
810

    
811
                List<ProjectExtent> extents = (List<ProjectExtent>) state
812
                                .get("extents");
813
                for (int i = 0; i < extents.size(); i++) {
814
                        this.addExtent(extents.get(i));
815
                }
816

    
817
                List<AbstractDocument> documents = (List<AbstractDocument>) state
818
                                .get("documents");
819
                for (int i = 0; i < documents.size(); i++) {
820
                        this.add(documents.get(i));
821
                }
822

    
823
                List<DocumentWindowInfo> persistentWindows = (List<DocumentWindowInfo>) state.get("documentWindowsInformation");
824
                                
825
                for (int i = 0; i < persistentWindows.size(); i++) {
826
                        DocumentWindowInfo persistentWindow = persistentWindows.get(i);
827
                        String docName = persistentWindow.getDocumentName();
828
                        String docType = persistentWindow.getDocumentType();
829
                        Document doc = this.getDocument(docName, docType);
830
                        IWindow win = doc.getFactory().getMainWindow(doc);
831
                        win.getWindowInfo().setWindowInfo(persistentWindow.getWindowInfo());
832
                        PluginServices.getMDIManager().addWindow(win);
833
                }
834
                
835
                if (state.hasValue("projectWindowInfo")){
836
                        WindowInfo projectWindowInfo = (WindowInfo)state.get("projectWindowInfo"); 
837
                        ProjectExtension pe = (ProjectExtension) PluginServices.getExtension(org.gvsig.app.extension.ProjectExtension.class);
838
                        pe.setProject(this);
839
                        pe.showProjectWindow(projectWindowInfo);
840
                }
841

    
842
        }
843

    
844
        public void saveToState(PersistentState state) throws PersistenceException {
845
                state.set("version", VERSION);
846
                state.set("comments", getComments());
847
                state.set("creationDate", this.getCreationDate());
848

    
849
                state.set("modificationDate", this.getModificationDate());
850
                state.set("name", this.getName());
851
                state.set("owner", this.getOwner());
852
                state.set("selectionColor", this.getSelectionColor());
853

    
854
                state.set("projection", this.getProjection());
855

    
856
                state.set("extents", this.extents);
857
                state.set("documents", this.getDocuments());
858

    
859
                List<DocumentWindowInfo> persistentWindows = new ArrayList<DocumentWindowInfo>();
860
                MDIManager mdiMan = PluginServices.getMDIManager();
861
                IWindow[] windows = mdiMan.getOrderedWindows();
862
                for (int i = windows.length - 1; i >= 0; i--) {
863
                        IWindow window = windows[i];
864
                        if (window instanceof IDocumentWindow){
865
                                WindowInfo wi =  mdiMan.getWindowInfo(window);
866
                                DocumentWindowInfo dwi = new DocumentWindowInfo(
867
                                                wi,
868
                                                ((IDocumentWindow) window).getDocument().getTypeName(),
869
                                                ((IDocumentWindow) window).getDocument().getName());
870
                                persistentWindows.add(dwi);
871
                        } else if (window instanceof ProjectWindow){
872
                                state.set("projectWindowInfo", mdiMan.getWindowInfo(window));
873
                        }
874
                }
875
                state.set("documentWindowsInformation", persistentWindows);
876

    
877
        }
878

    
879
        public static class DocumentWindowInfo implements Persistent {
880

    
881
                public static final String PERSISTENCE_DEFINITION_NAME = "DocumentWindowInfo";
882

    
883
                private WindowInfo windowInfo;
884
                private String documentType;
885
                private String documentName;
886

    
887
                public DocumentWindowInfo(){
888
                }
889
                
890
                DocumentWindowInfo(WindowInfo wi, String docType, String docName){
891
                        windowInfo = wi;
892
                        documentType = docType;
893
                        documentName = docName;
894
                }
895

    
896
                public WindowInfo getWindowInfo() {
897
                        return windowInfo;
898
                }
899
                
900
                public String getDocumentType() {
901
                        return documentType;
902
                }
903

    
904
                public String getDocumentName() {
905
                        return documentName;
906
                }
907

    
908
                public void saveToState(PersistentState state)
909
                                throws PersistenceException {
910
                        state.set("windowInfo", this.windowInfo);
911
                        state.set("documentType", this.documentType);
912
                        state.set("documentName", this.documentName);
913
                }
914

    
915
                public void loadFromState(PersistentState state)
916
                                throws PersistenceException {
917
                        this.windowInfo = (WindowInfo) state.get("windowInfo");
918
                        this.documentType = state.getString("documentType");
919
                        this.documentName = state.getString("documentName");
920
                }
921
                
922
                public static void registerPersistent() {
923
                        PersistenceManager manager = ToolsLocator.getPersistenceManager();
924
                        DynStruct definition = manager.getDefinition(PERSISTENCE_DEFINITION_NAME);
925
                        if ( definition == null ){
926
                                definition = manager.addDefinition(
927
                                                DocumentWindowInfo.class,
928
                                                PERSISTENCE_DEFINITION_NAME,
929
                                                "DocumentWindowInfo persistence definition",
930
                                                null, 
931
                                                null
932
                                );
933
                                definition.addDynFieldObject("windowInfo").setMandatory(true).setClassOfValue(WindowInfo.class);
934
                                definition.addDynFieldString("documentType").setMandatory(true);
935
                                definition.addDynFieldString("documentName").setMandatory(true);
936
                        }
937

    
938
                }
939
        }
940
        
941
        public static void registerPersistent() {
942
                AbstractDocument.registerPersistent();
943
                DocumentWindowInfo.registerPersistent();
944
                ProjectExtent.registerPersistent();
945

    
946
                PersistenceManager manager = ToolsLocator.getPersistenceManager();
947
                DynStruct definition = manager.addDefinition(DefaultProject.class,
948
                                "Project", "Project Persistence definition", null, null);
949
                definition.addDynFieldString("version").setMandatory(true);
950
                definition.addDynFieldString("comments").setMandatory(true);
951
                definition.addDynFieldString("creationDate").setMandatory(true);
952
                definition.addDynFieldString("modificationDate").setMandatory(true);
953
                definition.addDynFieldString("name").setMandatory(true);
954
                definition.addDynFieldString("owner").setMandatory(true);
955

    
956
                definition.addDynFieldObject("selectionColor")
957
                                .setClassOfValue(Color.class).setMandatory(true);
958
                definition.addDynFieldObject("projection")
959
                                .setClassOfValue(IProjection.class).setMandatory(true);
960

    
961
                definition.addDynFieldList("extents")
962
                                .setClassOfItems(ProjectExtent.class).setMandatory(true);
963

    
964
                definition.addDynFieldList("documents").setClassOfItems(Document.class)
965
                                .setMandatory(true);
966

    
967
                definition.addDynFieldObject("projectWindowInfo").setClassOfValue(WindowInfo.class).setMandatory(false);
968

    
969
                definition.addDynFieldList("documentWindowsInformation").setClassOfItems(WindowInfo.class).setMandatory(false);
970

    
971
        }
972

    
973
        /**
974
         * @deprecated use getPreferences().setDefaultSelectionColor()
975
         */
976
        public static void setDefaultSelectionColor(Color color) {
977
                getPreferences().setDefaultSelectionColor(color);
978
        }
979

    
980
        /**
981
         * @deprecated use getPreferences().getDefaultSelectionColor()
982
         */
983

    
984
        public static Color getDefaultSelectionColor() {
985
                return getPreferences().getDefaultSelectionColor();
986
        }
987

    
988
        /**
989
         * @deprecated use getPreferences().getDefaultMapUnits()
990
         */
991
        public static int getDefaultMapUnits() {
992
                return getPreferences().getDefaultMapUnits();
993
        }
994

    
995
        /**
996
         * @deprecated use getPreferences().getDefaultDistanceUnits()
997
         */
998
        public static int getDefaultDistanceUnits() {
999
                return getPreferences().getDefaultDistanceUnits();
1000
        }
1001

    
1002
        /**
1003
         * @deprecated use getPreferences().getDefaultDistanceArea()
1004
         */
1005
        public static int getDefaultDistanceArea() {
1006
                return getPreferences().getDefaultDistanceArea();
1007
        }
1008

    
1009
        /**
1010
         * @deprecated use getPreferences().setDefaultMapUnits()
1011
         */
1012
        public static void setDefaultMapUnits(int mapUnits) {
1013
                getPreferences().setDefaultMapUnits(mapUnits);
1014
        }
1015

    
1016
        /**
1017
         * @deprecated use getPreferences().setDefaultDistanceUnits()
1018
         */
1019
        public static void setDefaultDistanceUnits(int distanceUnits) {
1020
                getPreferences().setDefaultDistanceUnits(distanceUnits);
1021
        }
1022

    
1023
        /**
1024
         * @deprecated use getPreferences().setDefaultDistanceArea()
1025
         */
1026
        public static void setDefaultDistanceArea(int distanceArea) {
1027
                getPreferences().setDefaultDistanceArea(distanceArea);
1028
        }
1029

    
1030
        /**
1031
         * @deprecated use getPreferences().setDefaultProjection()
1032
         */
1033
        public static void setDefaultProjection(IProjection defaultProjection) {
1034
                getPreferences().setDefaultProjection(defaultProjection);
1035
        }
1036

    
1037
        /**
1038
         * @deprecated use getPreferences().getDefaultProjection()
1039
         */
1040
        public static IProjection getDefaultProjection() {
1041
                return getPreferences().getDefaultProjection();
1042
        }
1043

    
1044
        /**
1045
         * @deprecated see {@link #setSelectionColor(String)}, to be remove in gvSIG
1046
         *             2.1.0
1047
         */
1048
        public void setColor(String color) {
1049
                this.setSelectionColor(StringUtilities.string2Color(color));
1050
        }
1051

    
1052
        /**
1053
         * Return the selection color
1054
         * 
1055
         * @return selection color as string
1056
         * @deprecated use {@link #getSelectionColor()}
1057
         */
1058
        public String getColor() {
1059
                return StringUtilities.color2String(selectionColor);
1060
        }
1061

    
1062
        /**
1063
         * Return the list of views of the project
1064
         * 
1065
         * @return views as ArrayList of ProjectDocument
1066
         * 
1067
         * @deprecated see {@link #getDocumentsByType(String)}
1068
         */
1069
        public List<Document> getViews() {
1070
                return getDocuments(ViewManager.TYPENAME);
1071
        }
1072

    
1073
        /**
1074
         * Add a view to the project
1075
         * 
1076
         * @deprecated see {@link #add(AbstractDocument)}
1077
         */
1078
        public void addView(DefaultViewDocument v) {
1079
                add(v);
1080
        }
1081

    
1082
        /**
1083
         * Remove a view of the project
1084
         * 
1085
         * @param index
1086
         *            of the view as integer
1087
         * 
1088
         * @deprecated see {@link #remove(AbstractDocument)}
1089
         */
1090
        public void delView(int i) {
1091
                List<Document> list = getDocuments(ViewManager.TYPENAME);
1092
                remove(list.get(i));
1093
        }
1094

    
1095
        /**
1096
         * @deprecated see {@link #getDocument(String, String)}
1097
         */
1098
        public Document getProjectDocumentByName(String name, String type) {
1099
                return this.getDocument(name, type);
1100
        }
1101

    
1102
        /**
1103
         * @deprecated see {@link #getDocuments(String)}
1104
         */
1105
        public List<Document> getDocumentsByType(String type) {
1106
                return this.getDocuments(type);
1107
        }
1108

    
1109
        /**
1110
         * @deprecated aun por decidir que API darle al copy/paste
1111
         */
1112
        public String exportToXML(AbstractDocument[] selectedItems)
1113
                        throws SaveException {
1114
                // FIXME jjdc:hay que decirdir que API darle al copy/paste
1115
                throw new UnsupportedOperationException("This method is not supported");
1116
        }
1117

    
1118
        /**
1119
         * @deprecated aun por decidir que API darle al copy/paste
1120
         */
1121
        public void importFromXML(String sourceString, String docType) {
1122
                // FIXME jjdc:hay que decirdir que API darle al copy/paste
1123
                throw new UnsupportedOperationException("This method is not supported");
1124
        }
1125

    
1126
        /**
1127
         * @deprecated aun por decidir que API darle al copy/paste
1128
         */
1129
        public boolean isValidXMLForImport(String sourceString, String docType) {
1130
                // FIXME jjdc:hay que decirdir que API darle al copy/paste
1131
                throw new UnsupportedOperationException("This method is not supported");
1132
        }
1133

    
1134
        public boolean canImportDocuments(String data, String doctype) {
1135
                // TODO Auto-generated method stub
1136
                return false;
1137
        }
1138

    
1139
        public String exportDocumentsAsText(List<Document> documents) {
1140
                // TODO Auto-generated method stub
1141
                return null;
1142
        }
1143

    
1144
        public void importDocuments(String data, String doctype) {
1145
                // TODO Auto-generated method stub
1146

    
1147
        }
1148

    
1149
        public Document getActiveDocument() {
1150
                ApplicationManager application = ApplicationLocator.getManager();
1151

    
1152
                Document document = null;
1153
                IWindow[] windows = application.getUIManager().getOrderedWindows();
1154
                IWindow window = null;
1155
                for (int i = 0; i < windows.length; i++) {
1156
                    window = windows[i];
1157
                        if (window instanceof SingletonWindow) {
1158
                                // Cogemos no la primera ventana, si no la primera
1159
                                // ventana de tipo documento (SingletonWindow).
1160
                                // Y por si las mosca no es un documento, atrapamos
1161
                                // los errores y continuamos si no puede hacer un cast
1162
                                // del Model a Document
1163
                                try {
1164
                                        document = (Document) ((SingletonWindow) window).getWindowModel();
1165
                                        return document;
1166
                                } catch (ClassCastException e) {
1167
                                        // Do nothing, skip this window
1168
                                }
1169
                        }
1170
                }
1171
                return null;
1172
        } 
1173
}