Statistics
| Revision:

root / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / ProjectExtension.java @ 13456

History | View | Annotate | Download (24.5 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004-2007 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package com.iver.cit.gvsig;
42

    
43
import java.awt.Component;
44
import java.io.BufferedReader;
45
import java.io.File;
46
import java.io.FileInputStream;
47
import java.io.FileNotFoundException;
48
import java.io.FileOutputStream;
49
import java.io.FileReader;
50
import java.io.IOException;
51
import java.io.InputStream;
52
import java.io.InputStreamReader;
53
import java.io.OutputStreamWriter;
54
import java.io.Reader;
55
import java.io.UnsupportedEncodingException;
56
import java.net.MalformedURLException;
57
import java.net.URL;
58
import java.text.DateFormat;
59
import java.util.Date;
60
import java.util.prefs.Preferences;
61

    
62
import javax.swing.ImageIcon;
63
import javax.swing.JFileChooser;
64
import javax.swing.JOptionPane;
65

    
66
import org.exolab.castor.xml.MarshalException;
67
import org.exolab.castor.xml.Marshaller;
68
import org.exolab.castor.xml.ValidationException;
69

    
70
import com.iver.andami.Launcher;
71
import com.iver.andami.PluginServices;
72
import com.iver.andami.Launcher.TerminationProcess;
73
import com.iver.andami.messages.NotificationManager;
74
import com.iver.andami.plugins.Extension;
75
import com.iver.andami.plugins.IExtension;
76
import com.iver.andami.plugins.status.IExtensionStatus;
77
import com.iver.andami.plugins.status.IUnsavedData;
78
import com.iver.andami.plugins.status.UnsavedData;
79
import com.iver.andami.ui.mdiManager.IWindow;
80
import com.iver.andami.ui.mdiManager.WindowInfo;
81
import com.iver.andami.ui.wizard.UnsavedDataPanel;
82
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
83
import com.iver.cit.gvsig.project.Project;
84
import com.iver.cit.gvsig.project.ProjectFactory;
85
import com.iver.cit.gvsig.project.documents.ProjectDocument;
86
import com.iver.cit.gvsig.project.documents.exceptions.OpenException;
87
import com.iver.cit.gvsig.project.documents.gui.ProjectWindow;
88
import com.iver.cit.gvsig.project.documents.layout.ProjectMap;
89
import com.iver.cit.gvsig.project.documents.layout.ProjectMapFactory;
90
import com.iver.cit.gvsig.project.documents.layout.gui.Layout;
91
import com.iver.cit.gvsig.project.documents.table.ProjectTableFactory;
92
import com.iver.cit.gvsig.project.documents.view.ProjectViewFactory;
93
import com.iver.utiles.GenericFileFilter;
94
import com.iver.utiles.XMLEntity;
95
import com.iver.utiles.extensionPoints.ExtensionPoint;
96
import com.iver.utiles.extensionPoints.ExtensionPoints;
97
import com.iver.utiles.extensionPoints.ExtensionPointsSingleton;
98
import com.iver.utiles.swing.threads.IMonitorableTask;
99
import com.iver.utiles.xml.XMLEncodingUtils;
100
import com.iver.utiles.xmlEntity.generate.XmlTag;
101

    
102

    
103
/**
104
 * Extension que proporciona controles para crear proyectos nuevos, abrirlos y
105
 * guardarlos. Adem?s los tipos de tabla que soporta el proyecto son a?adidos
106
 * en esta clase.
107
 *
108
 * @author Fernando Gonz?lez Cort?s
109
 */
110
public class ProjectExtension extends Extension implements IExtensionStatus {
111
        private static String projectPath = null;
112
        //private ProjectWindow projectFrame;
113
        private ProjectWindow projectFrame;
114
        private Project p;
115
        private String lastPath;
116
        private String lastSavePath;
117
        private String templatesPath;
118
        private WindowInfo seedProjectWindow;
119
        /**
120
         * Use UTF-8 for encoding, as it can represent characters from
121
         * any language.
122
         *
123
         * Another sensible option would be
124
         * encoding = System.getProperty("file.encoding");
125
         * but this would need some extra testing.
126
         */
127
        public static String PROJECTENCODING = "UTF-8";
128

    
129
        /**
130
         * @see com.iver.mdiApp.plugins.IExtension#initialize()
131
         */
132
        public void initialize() {
133
            try {
134
            Class.forName("javax.media.jai.EnumeratedParameter");
135
        } catch (ClassNotFoundException e) {
136
            NotificationManager.addError("La m?quina virtual con la que se ejecuta gvSIG no tiene JAI instalado", e);
137
        }
138

    
139
                LayerFactory.setWritersPath(PluginServices.getPluginServices(this)
140
                                  .getPluginDirectory()
141
                                  .getAbsolutePath() +
142
                                  File.separator + "drivers");
143

    
144
        LayerFactory.setDriversPath(PluginServices.getPluginServices(this)
145
                                  .getPluginDirectory()
146
                                  .getAbsolutePath() +
147
                                  File.separator + "drivers");
148

    
149
        initializeDocumentActionsExtensionPoint();
150
        registerDocuments();
151

    
152
        // Recuperamos el ?ltimo argumento, que se supone
153
        // que ser? el fichero .gvp que queremos abrir.
154
        // (por enmedio pueden venir o no otros argumentos,
155
        // por ejemplo el idioma)
156
        // TODO: Aqu? Jaume podr?a meter lo del backup del proyecto
157
        // que ha hecho para ValenciaUrban?stica
158

    
159
                /*
160
                pe = (ProjectExtension) PluginServices.getExtension(com.iver.cit.gvsig.ProjectExtension.class);
161
                if (projectFile.length()!=0){
162
                        p=pe.readProject(projectFile);
163
                }
164
                else
165
                        p = restorePreviousProject();
166
                pe.setProject(p); */
167
        
168
        }
169
        /**
170
         * @see com.iver.mdiApp.plugins.IExtension#postInitialize()
171
         */
172
        public void postInitialize() {
173
                String[] theArgs = PluginServices.getArguments();
174
        String lastArg = theArgs[theArgs.length-1];
175
        if ((lastArg.endsWith(".gvp")) ||
176
                        (lastArg.endsWith(".GVP")))
177
        {
178
                PluginServices.getLogger().debug("Intentando cargar el proyecto " + lastArg);
179
                //File projectFile = new File(lastArg);
180
                setProject(readProject(lastArg));
181
                PluginServices.getMainFrame().setTitle(p.getName());
182
                projectPath = lastArg;
183
        }
184
        else
185
        {
186
                        setProject(ProjectFactory.createProject());
187
                        p.setName(PluginServices.getText(this, "untitled"));
188
                        p.setModified(false);
189
                        PluginServices.getMainFrame().setTitle(PluginServices.getText(this, "sin_titulo"));
190
        }
191
                getProjectFrame().setProject(p);
192
                p.restoreWindowProperties();
193
        }
194

    
195
        public ProjectWindow getProjectFrame() {
196
                if (projectFrame==null)
197
                                projectFrame = new ProjectWindow();
198
                return projectFrame;
199
        }
200
        /**
201
         * Muestra la ventana con el gestor de proyectos.
202
         */
203
        public void showProjectWindow() {
204
                if (seedProjectWindow!=null) {
205
                        if (seedProjectWindow.isClosed()) {
206
                                // if it was closed, we just don't open the window now
207
                                seedProjectWindow.setClosed(false);
208
                                return;
209
                        }
210
                        WindowInfo winProps = seedProjectWindow;
211
                        seedProjectWindow = null;
212
                        PluginServices.getMDIManager().addWindow(getProjectFrame());
213
                        PluginServices.getMDIManager().changeWindowInfo(getProjectFrame(), winProps);
214
                }
215
                else
216
                        PluginServices.getMDIManager().addWindow(getProjectFrame());
217
        }
218

    
219
        /**
220
         * Muestra la ventana con el gestor de proyectos, con las propiedades
221
         * de ventana especificadas.
222
         */
223
        public void showProjectWindow(WindowInfo wi) {
224
                seedProjectWindow = wi;
225
                showProjectWindow();
226
        }
227

    
228
        /**
229
         * Guarda el proyecto actual en disco.
230
         */
231
        private boolean guardar() {
232
                boolean saved=false;
233
//                if (p.getPath() == null) {
234
                if (projectPath == null) {
235
                        saved=guardarDialogo();
236
                } else {
237
                        long t1,t2;
238
                        t1 = System.currentTimeMillis();
239
                        saved=writeProject(new File(projectPath), p, false);
240
                        t2 = System.currentTimeMillis();
241
                        PluginServices.getLogger().info("Project saved. " +  (t2-t1) + " miliseconds");
242
                        getProjectFrame().refreshControls();
243
                }
244
                return saved;
245
        }
246

    
247
        private boolean guardarDialogo(){
248
                boolean saved=false;
249

    
250
                if (lastSavePath == null)
251
                        lastSavePath = projectPath;
252

    
253
                if (lastSavePath == null) {
254
                        Preferences prefs = Preferences.userRoot().node( "gvsig.foldering" );
255
                        lastSavePath = prefs.get("ProjectsFolder", null);
256
                }
257

    
258
                JFileChooser jfc = new JFileChooser(lastSavePath);
259
                jfc.setDialogTitle(PluginServices.getText(this, "guardar_proyecto"));
260
                jfc.addChoosableFileFilter(new GenericFileFilter("gvp",
261
                                PluginServices.getText(this, "tipo_fichero_proyecto")));
262

    
263
                if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
264
                        File file=jfc.getSelectedFile();
265
                        if (!(file.getPath().toLowerCase().endsWith(".gvp"))){
266
                                file=new File(file.getPath()+".gvp");
267
                        }
268
                        saved=writeProject(file, p);
269
                        String filePath = file.getAbsolutePath();
270
                        lastSavePath = filePath.substring(0, filePath.lastIndexOf(File.separatorChar));
271

    
272
                        getProjectFrame().refreshControls();
273
                }
274
                return saved;
275
        }
276

    
277
        /**
278
         * Checks whether the project and related unsaved data is modified,
279
         *  and allows the user to save it.
280
         *
281
         * @return true if the data has been correctly saved, false otherwise
282
         */
283
        private boolean askSave() {
284
                if (p != null && p.hasChanged()) {
285
                        TerminationProcess process = Launcher.getTerminationProcess();
286
                        UnsavedDataPanel panel = process.getUnsavedDataPanel();
287
                        panel.setHeaderText(PluginServices.getText(this, "Select_resources_to_save_before_closing_current_project"));
288
                        panel.setAcceptText(
289
                                        PluginServices.getText(this, "save_resources"),
290
                                        PluginServices.getText(this, "Save_the_selected_resources_and_close_current_project"));
291
                        panel.setCancelText(
292
                                        PluginServices.getText(this, "Dont_close"),
293
                                        PluginServices.getText(this, "Return_to_current_project"));
294
                        int closeCurrProj = process.manageUnsavedData();
295
                        if (closeCurrProj==JOptionPane.NO_OPTION) {
296
                                // the user chose to return to current project
297
                                return false;
298
                        }
299
                }
300
                return true;
301
        }
302

    
303
        /**
304
         * @see com.iver.mdiApp.plugins.IExtension#updateUI(java.lang.String)
305
         */
306
        public void execute(String actionCommand) {
307
                if (actionCommand.equals("NUEVO")) {
308
                        if (!askSave()) {
309
                                return;
310
                        }
311
                        
312
                        projectPath=null;
313
                        ProjectDocument.initializeNUMS();
314
                        PluginServices.getMDIManager().closeAllWindows();
315
                        setProject(ProjectFactory.createProject());
316
                        getProjectFrame().setProject(p);
317
                        showProjectWindow();
318
                        PluginServices.getMainFrame().setTitle(PluginServices.getText(this, "sin_titulo"));
319
                } else if (actionCommand.equals("ABRIR")) {
320
                        if (!askSave()) {
321
                                return;
322
                        }
323

    
324
                        if (lastPath == null) {
325
                                Preferences prefs = Preferences.userRoot().node( "gvsig.foldering" );
326
                                lastPath = prefs.get("ProjectsFolder", null);
327
                        }
328

    
329
                        JFileChooser jfc = new JFileChooser(lastPath);
330
                        jfc.addChoosableFileFilter(new GenericFileFilter("gvp",
331
                                        PluginServices.getText(this, "tipo_fichero_proyecto")));
332

    
333
                        if (jfc.showOpenDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
334
                                ProjectDocument.initializeNUMS();
335
                                PluginServices.getMDIManager().closeAllWindows();
336

    
337
                                File projectFile = jfc.getSelectedFile();
338
                                Project o = readProject(projectFile);
339
                                setPath(projectFile.getAbsolutePath());
340
                                lastPath = getPath();
341
                                if (o != null) {
342
                                        setProject(o);
343
                                }
344

    
345
                                getProjectFrame().setProject(p);
346
                                PluginServices.getMainFrame().setTitle(p.getName());
347
                                getProjectFrame().refreshControls();
348

    
349
//jaume                                p.setModified(true);
350
                                p.restoreWindowProperties();
351
                        }
352
                } else if (actionCommand.equals("GUARDAR")) {
353
                        guardar();
354
//jaume                        p.setModified(false);
355
                } else if (actionCommand.equals("GUARDAR_COMO")) {
356
                        guardarDialogo();
357
//jaume                        p.setModified(false);
358
                } else if (actionCommand.equals("SALIR")){
359
                        Launcher.closeApplication();
360
                } else if (actionCommand.compareTo("OPENTEMPLATE")==0){
361
                        openLayout();
362
//jaume                        p.setModified(true);
363
                }
364
        }
365
        public void openLayout() {
366
                //Project project = ((ProjectExtension) PluginServices.getExtension(ProjectExtension.class)).getProject();
367
                Layout layout=null;
368

    
369
                if (templatesPath == null) {
370
                        Preferences prefs = Preferences.userRoot().node( "gvsig.foldering" );
371
                        templatesPath = prefs.get("TemplatesFolder", null);
372
                }
373

    
374
                JFileChooser jfc = new JFileChooser(templatesPath);
375
                jfc.addChoosableFileFilter(new GenericFileFilter("gvt",
376
                                PluginServices.getText(this, "plantilla")));
377

    
378
                if (jfc.showOpenDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
379
                        File file=jfc.getSelectedFile();
380
                        if (!(file.getPath().endsWith(".gvt") || file.getPath().endsWith(".GVT"))){
381
                                file=new File(file.getPath()+".gvt");
382
                        }
383
                        try {
384
                                File xmlFile = new File(file.getAbsolutePath());
385
                                FileReader reader;
386
                                reader = new FileReader(xmlFile);
387

    
388
                                XmlTag tag = (XmlTag) XmlTag.unmarshal(reader);
389
                                try {
390
                                        layout = Layout.createLayout(new XMLEntity(tag),p);
391
                                } catch (OpenException e) {
392
                                        e.showError();
393
                                }
394
                                //fPanelLegendManager.setRenderer(LegendFactory.createFromXML(new XMLEntity(tag)));
395
                        } catch (FileNotFoundException e) {
396
                                NotificationManager.addError(PluginServices.getText(this, "Al_leer_la_leyenda"), e);
397
                        } catch (MarshalException e) {
398
                                NotificationManager.addError(PluginServices.getText(this, "Al_leer_la_leyenda"), e);
399
                        } catch (ValidationException e) {
400
                                NotificationManager.addError(PluginServices.getText(this, "Al_leer_la_leyenda"), e);
401
                        }
402
                        ProjectMap pmap = ProjectFactory.createMap(file.getName());
403
                        pmap.setModel(layout);
404
                        pmap.getModel().setProjectMap(pmap);
405
                        p.addDocument(pmap);
406
                        PluginServices.getMDIManager().addWindow(layout);
407

    
408

    
409
                }
410
        }
411
        /**
412
         * Escribe el proyecto en XML.
413
         *
414
         * @param file Fichero.
415
         * @param p Proyecto.
416
         */
417
        public boolean writeProject(File file, Project p) {
418
                return writeProject(file, p, true);
419
        }
420

    
421
        /**
422
         * Escribe el proyecto en XML. Pero permite decidir si se
423
         * pide confirmaci?n para sobreescribir
424
         *
425
         * @param file Fichero.
426
         * @param p Proyecto.
427
         * @param askConfirmation boolean
428
         */
429
        public boolean writeProject(File file, Project p, boolean askConfirmation) {
430
                if( askConfirmation && file.exists()){
431
                        int resp = JOptionPane.showConfirmDialog(
432
                                        (Component) PluginServices.getMainFrame(),PluginServices.getText(this,"fichero_ya_existe_seguro_desea_guardarlo"),
433
                                        PluginServices.getText(this,"guardar"), JOptionPane.YES_NO_OPTION);
434
                        if (resp != JOptionPane.YES_OPTION) {
435
                                return false;
436
                        }
437
                }
438
                // write it out as XML
439
                try {
440
            FileOutputStream fos = new FileOutputStream(file.getAbsolutePath());
441
            OutputStreamWriter writer = new OutputStreamWriter(fos, PROJECTENCODING);
442
                        Marshaller m = new Marshaller(writer);
443
                        m.setEncoding(PROJECTENCODING);
444
                        p.setName(file.getName());
445
                        // p.setPath(file.toString());
446
                        p.setModificationDate(DateFormat.getDateInstance().format(new Date()));
447
                        p.setModified(false);
448
                        XMLEntity xml = p.getXMLEntity();
449
                        xml.putProperty("followHeaderEncoding", true, false);
450
                        m.marshal(xml.getXmlTag());
451
                        PluginServices.getMainFrame().setTitle(file.getName());
452
                        setPath(file.toString());
453

    
454
                } catch (Exception e) {
455
                        NotificationManager.addError("Error guardando el proyecto", e);
456
                        return false;
457
                }
458
                return true;
459
        }
460

    
461
        public Project readProject(String path) {
462
                BufferedReader reader =null;
463
                try {
464
                URL url=null;
465
                        url = new URL(path);
466
                        String encoding = XMLEncodingUtils.getEncoding(url.openStream());
467
                        InputStream is = url.openStream();
468
                        if (encoding!=null) {
469
                                Project proj=null;
470
                                try {
471
                                        reader = new BufferedReader(new InputStreamReader(is, encoding));
472
                                        proj=readProject(reader, true);
473
                                } catch (UnsupportedEncodingException e) {
474
                                        reader = new BufferedReader(new InputStreamReader(is));
475
                                        try {
476
                                                Project p=readProject(reader, false);
477
                                        } catch (UnsupportedEncodingException e1) {
478
                                                JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this, e1.getLocalizedMessage()));
479
                                                return null;
480
                                        }
481
                                }
482
                                ProjectExtension.setPath(path);
483
                                return proj;
484
                        }
485
                        else {
486
                                reader = new BufferedReader(new InputStreamReader(is));
487
                                Project p;
488
                                try {
489
                                        p = readProject(reader, false);
490
                                } catch (UnsupportedEncodingException e) {
491
                                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this, e.getLocalizedMessage()));
492
                                        return null;
493
                                }
494
                                ProjectExtension.setPath(path); //p.setPath(null);
495
                                return p;
496
                        }
497
                } catch (MalformedURLException e) {
498
                        File file=new File(path);
499
                        return readProject(file);
500
                } catch (IOException e) {
501
                        e.printStackTrace();
502
                        return null;
503
                }
504
        }
505

    
506
        /**
507
         * Lee del XML el proyecto.<br><br>
508
         *
509
         * Reads the XML of the project.<br>
510
         * It returns a project object holding all needed info that is not linked to the Project Dialog. <br>In case you want the project to be
511
         * linked to the window you must set this object to the extension:<br>
512
         *
513
         * <b>Example:</b><br>
514
         *
515
         * ...<br>
516
         * ...<br>
517
         * Project p = ProjectExtension.readProject(projectFile);<br>
518
         * ProjectExtension.setProject(p);
519
         * ...<br>
520
         * ...<br>
521
         * @param file Fichero.
522
         *
523
         * @return Project
524
         *
525
         */
526
        public Project readProject(File file) {
527
                File xmlFile = new File(file.getAbsolutePath());
528
                try {
529
                        String encoding = XMLEncodingUtils.getEncoding(new FileInputStream(xmlFile));
530
                        InputStreamReader reader=null;
531
                        if (encoding!=null) {
532
                                try {
533
                                        reader = new InputStreamReader(new FileInputStream(xmlFile), encoding);
534
                                        return readProject(reader, true);
535
                                } catch (UnsupportedEncodingException e) {
536
                                        reader = new InputStreamReader(new FileInputStream(xmlFile));
537
                                        try {
538
                                                return readProject(reader, false);
539
                                        } catch (UnsupportedEncodingException e1) {
540
                                                JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this, e1.getLocalizedMessage()));
541
                                                return null;
542
                                        }
543
                                }
544
                        }
545
                        else {
546
                                reader = new InputStreamReader(new FileInputStream(xmlFile));
547
                                try {
548
                                        return readProject(reader, false);
549
                                } catch (UnsupportedEncodingException e1) {
550
                                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this, e1.getLocalizedMessage()));
551
                                        return null;
552
                                }
553
                        }
554
                } catch (FileNotFoundException e) {
555
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this, "fichero_incorrecto"));
556
                        return null;
557
                }
558
        }
559

    
560
        /**
561
         * Lee del XML el proyecto.<br><br>
562
         *
563
         * Reads the XML of the project.<br>
564
         * It returns a project object holding all needed info that is not linked to the Project Dialog. <br>In case you want the project to be
565
         * linked to the window you must set this object to the extension:<br>
566
         *
567
         * <b>Example:</b><br>
568
         *
569
         * ...<br>
570
         * ...<br>
571
         * Project p = ProjectExtension.readProject(projectFile);<br>
572
         * ProjectExtension.setProject(p);
573
         * ...<br>
574
         * ...<br>
575
         * @param reader File reader
576
         * @param encodingFollowed Whether the encoded specified in the xml header was followed
577
         * when creating the reader. If the property followHeaderEncoding is false or not set,
578
         * then the encoding should have not been used when creating the reader, so it must be
579
         * recreated.
580
         *
581
         * @return Project
582
         * @throws UnsupportedEncodingException
583
         *
584
         */
585
        public Project readProject(Reader reader, boolean encodingFollowed) throws UnsupportedEncodingException {
586
                Project proj = null;
587

    
588
                try {
589
                        XmlTag tag = (XmlTag) XmlTag.unmarshal(reader);
590
                        XMLEntity xml=new XMLEntity(tag);
591
                        String VERSION=xml.getStringProperty("VERSION");
592

    
593
                        if (encodingFollowed) {
594
                                if (xml.contains("followHeaderEncoding")) {
595
                                        boolean useEncoding = xml.getBooleanProperty("followHeaderEncoding");
596
                                        if (!useEncoding) {
597
                                                throw new UnsupportedEncodingException("the encoding specified in the xml header is not safe");
598
                                        }
599
                                }
600
                                else {
601
                                        // Old projects didn't contain followHeaderEncoding and they were
602
                                        // not correctly encoded. We throw an exception now, and we'll try
603
                                        // to reopen the project
604
                                        // using the default system encoding.
605
                                        throw new UnsupportedEncodingException("the encoding specified in the xml header is not safe");
606
                                }
607
                        }
608

    
609
                        try {
610
                                // if ((VERSION!=null) && (VERSION.equals("0.5") || VERSION.equals("0.4") || (VERSION.indexOf("GISPLANET") != -1))){
611
                                if (VERSION != null) {
612
                                        proj = Project.createFromXML(xml);
613
                                }else{
614
                                        proj = Project.createFromXML03(new XMLEntity(tag));
615
                                }
616
                                return proj;
617
                        } catch (OpenException e){
618
                                e.showError();
619
                                //NotificationManager.addInfo("Al leer el proyecto", e);
620
                        }
621
                }  catch (MarshalException e) {
622
                        PluginServices.getLogger().error(PluginServices.getText(this, "formato_incorrecto"),e);
623
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this, "formato_incorrecto"));
624
                        //NotificationManager.addError("Al leer el proyecto", e);
625
                } catch (ValidationException e) {
626
                        PluginServices.getLogger().error(PluginServices.getText(this, "formato_incorrecto"),e);
627
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this, "formato_incorrecto"));
628
                        //NotificationManager.addError("Al leer el proyecto", e);
629
                }
630
                return null;
631
        }
632

    
633
        /**
634
         * Devuelve el proyecto.
635
         *
636
         * @return Proyecto.
637
         */
638
        public Project getProject() {
639
                return p;
640
        }
641

    
642
        /**
643
         * @see com.iver.andami.plugins.IExtension#isEnabled()
644
         */
645
        public boolean isEnabled() {
646
                return true;
647
        }
648

    
649
        /**
650
         * @see com.iver.andami.plugins.IExtension#isVisible()
651
         */
652
        public boolean isVisible() {
653
                return true;
654
        }
655

    
656
        /**
657
         * Sets the project
658
         * @param p
659
         */
660
        public void setProject(Project p){
661
                getProjectFrame().setProject(p);
662
                this.p=p;
663
        }
664

    
665
        private void registerDocuments() {
666
                ProjectViewFactory.register();
667
                ProjectTableFactory.register();
668
                ProjectMapFactory.register();
669
        }
670

    
671
        private void initializeDocumentActionsExtensionPoint() {
672
                ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance();
673
                if (!extensionPoints.containsKey("DocumentActions_View")) {
674
                        extensionPoints.put(
675
                                new ExtensionPoint(
676
                                        "DocumentActions_View",
677
                                        "Context menu options of the view document list" +
678
                                                " in the project window "+
679
                                                "(register instances of " +
680
                                                "com.iver.cit.gvsig.project.AbstractDocumentContextMenuAction)"
681
                                )
682
                        );
683
                }
684
                if (!extensionPoints.containsKey("DocumentActions_Table")) {
685
                        extensionPoints.put(
686
                                new ExtensionPoint(
687
                                        "DocumentActions_Table",
688
                                        "Context menu options of the table document list" +
689
                                                " in the project window "+
690
                                                "(register instances of " +
691
                                                "com.iver.cit.gvsig.project.AbstractDocumentContextMenuAction)"
692
                                )
693
                        );
694
                }
695
                if (!extensionPoints.containsKey("DocumentActions_Map")) {
696
                        extensionPoints.put(
697
                                new ExtensionPoint(
698
                                        "DocumentActions_Map",
699
                                        "Context menu options of the map document list" +
700
                                                " in the project window "+
701
                                                "(register instances of " +
702
                                                "com.iver.cit.gvsig.project.AbstractDocumentContextMenuAction)"
703
                                )
704
                        );
705
                }
706
        }
707

    
708
        public static String getPath() {
709
                return projectPath;
710
        }
711

    
712
        public static void setPath(String path) {
713
                projectPath = path;
714
        }
715

    
716
        public IWindow getProjectWindow() {
717
                return getProjectFrame();
718
        }
719
        
720
        
721
        public IExtensionStatus getStatus() {
722
                return this;
723
        }
724
        
725
        public boolean hasUnsavedData() {
726
                return p.hasChanged();
727
        }
728
        
729
        public IUnsavedData[] getUnsavedData() {
730
                if (hasUnsavedData()) {
731
                        UnsavedProject data = new UnsavedProject(this);
732
                        IUnsavedData[] dataArray = {data};
733
                        return dataArray;
734
                }
735
                else
736
                        return null;
737
        }
738
        
739
        /**
740
         * Implements the IUnsavedData interface to show unsaved projects
741
         * in the Unsavad Data dialog.
742
         * 
743
         * @author Cesar Martinez Izquierdo <cesar.martinez@iver.es>
744
         */
745
        public class UnsavedProject  extends UnsavedData {
746
                
747
                public UnsavedProject(IExtension extension) {
748
                        super(extension);
749
                }
750

    
751
                public String getDescription() {
752
                        if (getPath()==null) {
753
                                return PluginServices.getText(ProjectExtension.this, "Unnamed_new_gvsig_project_");
754
                        }
755
                        else {
756
                                return PluginServices.getText(ProjectExtension.this, "Modified_project_");
757
                        }
758
                }
759

    
760
                public String getResourceName() {
761
                        if (getPath()==null) {
762
                                return PluginServices.getText(ProjectExtension.this, "Unnamed");
763
                        }
764
                        else {
765
                                return getPath();
766
                        }
767
                        
768
                }
769

    
770
                public boolean saveData() {
771
                        return guardar();
772
                }
773
                
774
                public ImageIcon getIcon() {
775
                        try {
776
                                URL imagePath = PluginServices.getPluginServices(ProjectExtension.this).getClassLoader().getResource("images/logoGVA.gif");
777
                                return new ImageIcon(imagePath);
778
                        }
779
                        catch (Exception ex) {}
780
                        return null;
781
                }
782
        }
783

    
784
        public IMonitorableTask[] getRunningProcesses() {
785
                // TODO Auto-generated method stub
786
                return null;
787
        }
788
        public boolean hasRunningProcesses() {
789
                // TODO Auto-generated method stub
790
                return false;
791
        }
792

    
793
}