Statistics
| Revision:

svn-gvsig-desktop / tags / gvSIGv0_6_1RELEASE / applications / appgvSIG / src / com / iver / cit / gvsig / ProjectExtension.java @ 5222

History | View | Annotate | Download (12.8 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 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.File;
45
import java.io.FileNotFoundException;
46
import java.io.FileReader;
47
import java.io.FileWriter;
48
import java.text.DateFormat;
49
import java.util.Date;
50

    
51
import javax.swing.JFileChooser;
52
import javax.swing.JOptionPane;
53

    
54
import org.exolab.castor.xml.MarshalException;
55
import org.exolab.castor.xml.Marshaller;
56
import org.exolab.castor.xml.ValidationException;
57

    
58
import com.iver.andami.Launcher;
59
import com.iver.andami.PluginServices;
60
import com.iver.andami.messages.Messages;
61
import com.iver.andami.messages.NotificationManager;
62
import com.iver.andami.plugins.Extension;
63
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
64
import com.iver.cit.gvsig.gui.layout.Layout;
65
import com.iver.cit.gvsig.gui.project.OpenException;
66
import com.iver.cit.gvsig.gui.project.ProjectWindow;
67
import com.iver.cit.gvsig.project.Project;
68
import com.iver.cit.gvsig.project.ProjectFactory;
69
import com.iver.cit.gvsig.project.ProjectMap;
70
import com.iver.cit.gvsig.project.ProjectView;
71
import com.iver.utiles.GenericFileFilter;
72
import com.iver.utiles.XMLEntity;
73
import com.iver.utiles.xmlEntity.generate.XmlTag;
74

    
75

    
76
/**
77
 * Extension que proporciona controles para crear proyectos nuevos, abrirlos y
78
 * guardarlos. Adem?s los tipos de tabla que soporta el proyecto son a?adidos
79
 * en esta clase.
80
 *
81
 * @author Fernando Gonz?lez Cort?s
82
 */
83
public class ProjectExtension implements Extension {
84
        private ProjectWindow projectFrame;
85
        private Project p;
86
        /**
87
         * @see com.iver.mdiApp.plugins.Extension#inicializar()
88
         */
89
        public void inicializar() {
90
            try {
91
            Class.forName("javax.media.jai.EnumeratedParameter");
92
        } catch (ClassNotFoundException e) {
93
            NotificationManager.addError("La m?quina virtual con la que se ejecuta gvSIG no tiene JAI instalado", e);
94
        }
95

    
96
                LayerFactory.setDriversPath(PluginServices.getPluginServices(this)
97
                                                                                                  .getPluginDirectory()
98
                                                                                                  .getAbsolutePath() +
99
                        File.separator + "drivers");
100

    
101
        // Recuperamos el ?ltimo argumento, que se supone
102
        // que ser? el fichero .gvp que queremos abrir.
103
        // (por enmedio pueden venir o no otros argumentos,
104
        // por ejemplo el idioma)
105
        // TODO: Aqu? Jaume podr?a meter lo del backup del proyecto
106
        // que ha hecho para ValenciaUrban?stica
107

    
108
                /*
109
                pe = (ProjectExtension) PluginServices.getExtension(com.iver.cit.gvsig.ProjectExtension.class);
110
                if (projectFile.length()!=0){
111
                        p=pe.readProject(projectFile);
112
                }
113
                else
114
                        p = restorePreviousProject();
115
                pe.setProject(p); */
116
        String[] theArgs = PluginServices.getArguments();
117
        String lastArg = theArgs[theArgs.length-1];
118
        if ((lastArg.endsWith(".gvp")) ||
119
                        (lastArg.endsWith(".GVP")))
120
        {
121
                File projectFile = new File(lastArg);
122
                p = leerProyecto(projectFile);
123
                PluginServices.getMainFrame().setTitle(p.getName());
124
        }
125
        else
126
        {
127
                        p = ProjectFactory.createProject();
128
                        p.setName(PluginServices.getText(this, "untitled"));
129
                        p.setModified(false);
130
                        PluginServices.getMainFrame().setTitle(PluginServices.getText(this, "sin_titulo"));
131
        }
132
            
133
                projectFrame = new ProjectWindow(this);
134
                projectFrame.setProject(p);
135
                showProjectWindow();
136
        }
137

    
138
        /**
139
         * Muestra la ventana con el gestor de proyectos.
140
         */
141
        public void showProjectWindow() {
142
                PluginServices.getMDIManager().addView(projectFrame);
143
                if (Project.getSeedProjectWindow()!=null) {
144
                        PluginServices.getMDIManager().changeViewInfo(projectFrame, Project.getSeedProjectWindow());
145
                }
146
        }
147

    
148
        /**
149
         * Guarda el proyecto actual en disco.
150
         */
151
        private void guardar() {
152
                if (p.getPath() == null) {
153
                        guardarDialogo();
154
                } else {
155
                        escribirProyecto(new File(p.getPath()), p);
156
                        projectFrame.refreshControls();
157
                }
158
        }
159
        private void guardarDialogo(){
160
                JFileChooser jfc = new JFileChooser();
161
                jfc.addChoosableFileFilter(new GenericFileFilter("gvp",
162
                                PluginServices.getText(this, "tipo_fichero_proyecto")));
163

    
164
                if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
165
                        File file=jfc.getSelectedFile();
166
                        if (!(file.getPath().endsWith(".gvp") || file.getPath().endsWith(".GVP"))){
167
                                file=new File(file.getPath()+".gvp");
168
                        }
169
                        escribirProyecto(file, p);
170
                        projectFrame.refreshControls();
171
                }
172
        }
173
        /**
174
         * Guarda si el proyecto ha sido modificado.
175
         *
176
         * @return True si se ha guardado correctamente.
177
         */
178
        private boolean modificado() {
179
                ///if (p.isModified()) {
180
                if (true) {
181
                        //TODO de momento se queda como modificado siempre, para poder controlar perfectamente cuando un proyecto ha sido modificado 
182
                        //hay que recoger los eventos de cambio de leyenda, cambio de extent, a?adir una capa, etc que se encuentran en FMap.
183
                        int res = JOptionPane.showConfirmDialog((Component) PluginServices.getMainFrame(),
184
                                        PluginServices.getText(this, "guardar_cambios"),
185
                                        "gvSIG",
186
                                        JOptionPane.YES_NO_CANCEL_OPTION,
187
                                        JOptionPane.INFORMATION_MESSAGE);
188

    
189
                        if (res == JOptionPane.YES_OPTION) {
190
                                guardar();
191
                        } else if (res == JOptionPane.CANCEL_OPTION) {
192
                                return false;
193
                        }
194
                }
195

    
196
                return true;
197
        }
198

    
199
        /**
200
         * @see com.iver.mdiApp.plugins.Extension#updateUI(java.lang.String)
201
         */
202
        public void execute(String actionCommand) {
203
                if (actionCommand.equals("NUEVO")) {
204
                        //Si est? modificado se pregunta si se quiere guardar el anterior
205
                        if (!modificado()) {
206
                                return;
207
                        }
208
                        ProjectView.numViews=0;
209
            ProjectMap.numMaps = 0;
210
                        PluginServices.getMDIManager().closeAllViews();
211
                        p = ProjectFactory.createProject();
212
                        p.setName(PluginServices.getText(this, "untitled"));
213
                        p.setModified(false);
214
                        projectFrame.setProject(p);
215
                        showProjectWindow();
216
                        PluginServices.getMainFrame().setTitle(PluginServices.getText(this, "sin_titulo"));
217
                } else if (actionCommand.equals("ABRIR")) {
218
                        //Si est? modificado se pregunta si se quiere guardar el anterior
219
                        if (!modificado()) {
220
                                ProjectView.numViews=0;
221
                ProjectMap.numMaps = 0;
222
                                return;
223
                        }
224
                        
225
                        JFileChooser jfc = new JFileChooser();
226
                        jfc.addChoosableFileFilter(new GenericFileFilter("gvp",
227
                                        PluginServices.getText(this, "tipo_fichero_proyecto")));
228

    
229
                        if (jfc.showOpenDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
230
                                ProjectView.numViews=0;
231
                ProjectMap.numMaps = 0;
232
                                PluginServices.getMDIManager().closeAllViews();
233
                                Project o = leerProyecto(jfc.getSelectedFile());
234
                                if (o != null) {
235
                                        p = o;
236
                                }
237

    
238
                                projectFrame.setProject(p);
239
                                PluginServices.getMainFrame().setTitle(p.getName());
240
                                projectFrame.refreshControls();
241
                                showProjectWindow();
242
                        }
243
                } else if (actionCommand.equals("GUARDAR")) {
244
                        guardar();
245
                } else if (actionCommand.equals("GUARDAR_COMO")) {
246
                        guardarDialogo();
247
                } else if (actionCommand.equals("SALIR")){
248
                        int option = JOptionPane.showConfirmDialog(null,
249
                                        Messages.getString("MDIFrame.quiere_salir"),
250
                                        Messages.getString("MDIFrame.salir"),
251
                                        JOptionPane.YES_NO_OPTION);
252

    
253
                        if (option == JOptionPane.YES_OPTION) {
254
                                Launcher.closeApplication();
255
                        }
256
                } else if (actionCommand.compareTo("OPENTEMPLATE")==0){
257
                        openLayout();
258
                }
259
        }
260
        public void openLayout() {
261
                //Project project = ((ProjectExtension) PluginServices.getExtension(ProjectExtension.class)).getProject();
262
                Layout layout=null;
263
                JFileChooser jfc = new JFileChooser();
264
                jfc.addChoosableFileFilter(new GenericFileFilter("gvt",
265
                                PluginServices.getText(this, "plantilla")));
266

    
267
                if (jfc.showOpenDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
268
                        File file=jfc.getSelectedFile();
269
                        if (!(file.getPath().endsWith(".gvt") || file.getPath().endsWith(".GVT"))){
270
                                file=new File(file.getPath()+".gvt");
271
                        }
272
                        try {
273
                                File xmlFile = new File(file.getAbsolutePath());
274
                                FileReader reader;
275
                                reader = new FileReader(xmlFile);
276

    
277
                                XmlTag tag = (XmlTag) XmlTag.unmarshal(reader);
278
                                try {
279
                                        layout = Layout.createLayout(new XMLEntity(tag),p);
280
                                } catch (OpenException e) {
281
                                        e.showError();
282
                                }
283
                                //fPanelLegendManager.setRenderer(LegendFactory.createFromXML(new XMLEntity(tag)));
284
                        } catch (FileNotFoundException e) {
285
                                NotificationManager.addError(PluginServices.getText(this, "Al_leer_la_leyenda"), e);
286
                        } catch (MarshalException e) {
287
                                NotificationManager.addError(PluginServices.getText(this, "Al leer la leyenda"), e);
288
                        } catch (ValidationException e) {
289
                                NotificationManager.addError(PluginServices.getText(this, "Al leer la leyenda"), e);
290
                        }
291
                        ProjectMap pmap = ProjectFactory.createMap(file.getName());
292
                        pmap.setModel(layout);
293
                        pmap.getModel().setProjectMap(pmap);
294
                        
295
                        p.addMap(pmap);
296
                        PluginServices.getMDIManager().addView(layout);
297
                        
298
                        
299
                }
300
        }
301
        /**
302
         * Escribe el proyecto en XML.
303
         *
304
         * @param file Fichero.
305
         * @param p Proyecto.
306
         */
307
        public void escribirProyecto(File file, Project p) {
308
                // write it out as XML
309
                try {
310
                        FileWriter writer = new FileWriter(file.getAbsolutePath());
311
                        Marshaller m = new Marshaller(writer);
312
                        m.setEncoding("ISO-8859-1");
313
                        p.setName(file.getName());
314
                        p.setPath(file.toString());
315
                        p.setModificationDate(DateFormat.getDateInstance().format(new Date()));
316
                        p.setModified(false);
317
                        m.marshal(p.getXMLEntity().getXmlTag());
318
                        PluginServices.getMainFrame().setTitle(file.getName());
319
                } catch (Exception e) {
320
                        NotificationManager.addError("Error guardando el proyecto", e);
321
                }
322

    
323
        }
324

    
325
        /**
326
         * Lee del XML el proyecto.<br><br>
327
         * 
328
         * Reads the XML of the project.<br>
329
         * It returns n project object holding all needed info that is not linked to the Project Dialog. <br>In case you want the project to be
330
         * linked to the window you must to set this object to the extension:<br>
331
         * 
332
         * <b>Example:</b><br>
333
         *  
334
         * ...<br>
335
         * ...<br>
336
         * Project p = ProjectExtension.leerProyecto(projectFile);<br>
337
         * ProjectExtension.setProject(p);
338
         * ...<br>
339
         * ...<br>
340
         * @param file Fichero.
341
         *
342
         * @return Project
343
         * 
344
         */
345
        public Project leerProyecto(File file) {
346
                Project proj = null;
347

    
348
                try {
349
                        File xmlFile = new File(file.getAbsolutePath());
350
                        FileReader reader;
351
                        reader = new FileReader(xmlFile);
352
                        XmlTag tag = (XmlTag) XmlTag.unmarshal(reader);
353
                        XMLEntity xml=new XMLEntity(tag);
354
                        String VERSION=xml.getStringProperty("VERSION");
355
                        
356
                        try {
357
                                // if ((VERSION!=null) && (VERSION.equals("0.5") || VERSION.equals("0.4") || (VERSION.indexOf("GISPLANET") != -1))){
358
                                if (VERSION != null) {
359
                                        proj = Project.createFromXML(xml);
360
                                }else{
361
                                        proj = Project.createFromXML03(new XMLEntity(tag));
362
                                }
363
                                return proj;
364
                        } catch (OpenException e){
365
                                e.showError();
366
                                //NotificationManager.addInfo("Al leer el proyecto", e);
367
                        }
368
                } catch (FileNotFoundException e) {
369
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this, "fichero_incorrecto"));
370
                        //NotificationManager.addError("Al leer el proyecto", e);
371
                } catch (MarshalException e) {
372
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this, "formato_incorrecto"));
373
                        //NotificationManager.addError("Al leer el proyecto", e);
374
                } catch (ValidationException e) {
375
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this, "formato_incorrecto"));
376
                        //NotificationManager.addError("Al leer el proyecto", e);
377
                } 
378
                
379
                return null;
380
        }
381

    
382
        /**
383
         * Devuelve el proyecto.
384
         *
385
         * @return Proyecto.
386
         */
387
        public Project getProject() {
388
                return p;
389
        }
390

    
391
        /**
392
         * @see com.iver.andami.plugins.Extension#isEnabled()
393
         */
394
        public boolean isEnabled() {
395
                return true;
396
        }
397

    
398
        /**
399
         * @see com.iver.andami.plugins.Extension#isVisible()
400
         */
401
        public boolean isVisible() {
402
                return true;
403
        }
404
        
405
        /**
406
         * Sets the project
407
         * @param p
408
         */
409
        public void setProject(Project p){
410
                projectFrame.setProject(p);
411
                this.p=p;
412
        }
413
}