Statistics
| Revision:

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

History | View | Annotate | Download (33.5 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.apache.commons.io.FilenameUtils;
58
import org.cresques.cts.IProjection;
59
import org.slf4j.Logger;
60
import org.slf4j.LoggerFactory;
61

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

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

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

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

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

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

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

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

    
125
        private PropertyChangeSupport change;
126

    
127
        private boolean modified = false;
128

    
129
        private String name = null;
130

    
131
        private String creationDate = null;
132

    
133
        private String modificationDate = null;
134

    
135
        private String owner = null;
136

    
137
        private String comments = null;
138

    
139
        private Color selectionColor = null;
140

    
141
        private List<Document> documents = null;
142

    
143
        private List<ProjectExtent> extents = null;
144

    
145
        private IProjection projection;
146

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
711
                return new File(relative);
712
        }
713

    
714
        private void fixFiles(PersistentState state, File rootFolder) {
715
                PersistentContext context = state.getContext();
716
                URI cwd = new File(System.getProperty("user.dir")).toURI();
717

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

    
756
                                }
757
                        }
758
                }
759
        }
760

    
761
        private File fixFile(File file, File rootFolder) {
762
                if (file.isAbsolute()) {
763
                        return file;
764
                }
765
                return new File(rootFolder, file.getPath()).getAbsoluteFile();
766
        }
767

    
768
        public void loadState(InputStream in) {
769
                loadState(in, null);
770
        }
771

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

    
789
        }
790

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

    
802
        @SuppressWarnings("unchecked")
803
        public void loadFromState(PersistentState state)
804
                        throws PersistenceException {
805
                this.clean();
806

    
807
                this.setComments(state.getString("comments"));
808
                this.setCreationDate(state.getString("creationDate"));
809
                this.setModificationDate(state.getString("modificationDate"));
810
                this.setName(state.getString("name"));
811
                this.setOwner(state.getString("owner"));
812
                this.setSelectionColor((Color) state.get("selectionColor"));
813
                this.setProjection((IProjection) state.get("projection"));
814

    
815
                List<ProjectExtent> extents = (List<ProjectExtent>) state
816
                                .get("extents");
817
                for (int i = 0; i < extents.size(); i++) {
818
                        this.addExtent(extents.get(i));
819
                }
820

    
821
                List<AbstractDocument> documents = (List<AbstractDocument>) state
822
                                .get("documents");
823
                for (int i = 0; i < documents.size(); i++) {
824
                        this.add(documents.get(i));
825
                }
826

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

    
846
        }
847

    
848
        public void saveToState(PersistentState state) throws PersistenceException {
849
                state.set("version", VERSION);
850
                state.set("comments", getComments());
851
                state.set("creationDate", this.getCreationDate());
852

    
853
                state.set("modificationDate", this.getModificationDate());
854
                state.set("name", this.getName());
855
                state.set("owner", this.getOwner());
856
                state.set("selectionColor", this.getSelectionColor());
857

    
858
                state.set("projection", this.getProjection());
859

    
860
                state.set("extents", this.extents);
861
                state.set("documents", this.getDocuments());
862

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

    
881
        }
882

    
883
        public static class DocumentWindowInfo implements Persistent {
884

    
885
                public static final String PERSISTENCE_DEFINITION_NAME = "DocumentWindowInfo";
886

    
887
                private WindowInfo windowInfo;
888
                private String documentType;
889
                private String documentName;
890

    
891
                public DocumentWindowInfo(){
892
                }
893
                
894
                DocumentWindowInfo(WindowInfo wi, String docType, String docName){
895
                        windowInfo = wi;
896
                        documentType = docType;
897
                        documentName = docName;
898
                }
899

    
900
                public WindowInfo getWindowInfo() {
901
                        return windowInfo;
902
                }
903
                
904
                public String getDocumentType() {
905
                        return documentType;
906
                }
907

    
908
                public String getDocumentName() {
909
                        return documentName;
910
                }
911

    
912
                public void saveToState(PersistentState state)
913
                                throws PersistenceException {
914
                        state.set("windowInfo", this.windowInfo);
915
                        state.set("documentType", this.documentType);
916
                        state.set("documentName", this.documentName);
917
                }
918

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

    
942
                }
943
        }
944
        
945
        public static void registerPersistent() {
946
                AbstractDocument.registerPersistent();
947
                DocumentWindowInfo.registerPersistent();
948
                ProjectExtent.registerPersistent();
949

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

    
960
                definition.addDynFieldObject("selectionColor")
961
                                .setClassOfValue(Color.class).setMandatory(true);
962
                definition.addDynFieldObject("projection")
963
                                .setClassOfValue(IProjection.class).setMandatory(true);
964

    
965
                definition.addDynFieldList("extents")
966
                                .setClassOfItems(ProjectExtent.class).setMandatory(true);
967

    
968
                definition.addDynFieldList("documents").setClassOfItems(Document.class)
969
                                .setMandatory(true);
970

    
971
                definition.addDynFieldObject("projectWindowInfo").setClassOfValue(WindowInfo.class).setMandatory(false);
972

    
973
                definition.addDynFieldList("documentWindowsInformation").setClassOfItems(WindowInfo.class).setMandatory(false);
974

    
975
        }
976

    
977
        /**
978
         * @deprecated use getPreferences().setDefaultSelectionColor()
979
         */
980
        public static void setDefaultSelectionColor(Color color) {
981
                getPreferences().setDefaultSelectionColor(color);
982
        }
983

    
984
        /**
985
         * @deprecated use getPreferences().getDefaultSelectionColor()
986
         */
987

    
988
        public static Color getDefaultSelectionColor() {
989
                return getPreferences().getDefaultSelectionColor();
990
        }
991

    
992
        /**
993
         * @deprecated use getPreferences().getDefaultMapUnits()
994
         */
995
        public static int getDefaultMapUnits() {
996
                return getPreferences().getDefaultMapUnits();
997
        }
998

    
999
        /**
1000
         * @deprecated use getPreferences().getDefaultDistanceUnits()
1001
         */
1002
        public static int getDefaultDistanceUnits() {
1003
                return getPreferences().getDefaultDistanceUnits();
1004
        }
1005

    
1006
        /**
1007
         * @deprecated use getPreferences().getDefaultDistanceArea()
1008
         */
1009
        public static int getDefaultDistanceArea() {
1010
                return getPreferences().getDefaultDistanceArea();
1011
        }
1012

    
1013
        /**
1014
         * @deprecated use getPreferences().setDefaultMapUnits()
1015
         */
1016
        public static void setDefaultMapUnits(int mapUnits) {
1017
                getPreferences().setDefaultMapUnits(mapUnits);
1018
        }
1019

    
1020
        /**
1021
         * @deprecated use getPreferences().setDefaultDistanceUnits()
1022
         */
1023
        public static void setDefaultDistanceUnits(int distanceUnits) {
1024
                getPreferences().setDefaultDistanceUnits(distanceUnits);
1025
        }
1026

    
1027
        /**
1028
         * @deprecated use getPreferences().setDefaultDistanceArea()
1029
         */
1030
        public static void setDefaultDistanceArea(int distanceArea) {
1031
                getPreferences().setDefaultDistanceArea(distanceArea);
1032
        }
1033

    
1034
        /**
1035
         * @deprecated use getPreferences().setDefaultProjection()
1036
         */
1037
        public static void setDefaultProjection(IProjection defaultProjection) {
1038
                getPreferences().setDefaultProjection(defaultProjection);
1039
        }
1040

    
1041
        /**
1042
         * @deprecated use getPreferences().getDefaultProjection()
1043
         */
1044
        public static IProjection getDefaultProjection() {
1045
                return getPreferences().getDefaultProjection();
1046
        }
1047

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

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

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

    
1077
        /**
1078
         * Add a view to the project
1079
         * 
1080
         * @deprecated see {@link #add(AbstractDocument)}
1081
         */
1082
        public void addView(DefaultViewDocument v) {
1083
                add(v);
1084
        }
1085

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

    
1099
        /**
1100
         * @deprecated see {@link #getDocument(String, String)}
1101
         */
1102
        public Document getProjectDocumentByName(String name, String type) {
1103
                return this.getDocument(name, type);
1104
        }
1105

    
1106
        /**
1107
         * @deprecated see {@link #getDocuments(String)}
1108
         */
1109
        public List<Document> getDocumentsByType(String type) {
1110
                return this.getDocuments(type);
1111
        }
1112

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

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

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

    
1138
        public boolean canImportDocuments(String data, String doctype) {
1139
                // TODO Auto-generated method stub
1140
                return false;
1141
        }
1142

    
1143
        public String exportDocumentsAsText(List<Document> documents) {
1144
                // TODO Auto-generated method stub
1145
                return null;
1146
        }
1147

    
1148
        public void importDocuments(String data, String doctype) {
1149
                // TODO Auto-generated method stub
1150

    
1151
        }
1152

    
1153
        public Document getActiveDocument() {
1154
                ApplicationManager application = ApplicationLocator.getManager();
1155

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