Statistics
| Revision:

svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / ProjectExtension.java @ 5503

History | View | Annotate | Download (13 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 extends Extension {
84
        private ProjectWindow projectFrame;
85
        private Project p;
86
        /**
87
         * @see com.iver.mdiApp.plugins.IExtension#initialize()
88
         */
89
        public void initialize() {
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.setWritersPath(PluginServices.getPluginServices(this)
97
                                  .getPluginDirectory()
98
                                  .getAbsolutePath() +
99
                                  File.separator + "drivers");
100
        
101
        LayerFactory.setDriversPath(PluginServices.getPluginServices(this)
102
                                  .getPluginDirectory()
103
                                  .getAbsolutePath() +
104
                                  File.separator + "drivers");
105

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

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

    
143
        /**
144
         * Muestra la ventana con el gestor de proyectos.
145
         */
146
        public void showProjectWindow() {
147
                PluginServices.getMDIManager().addView(projectFrame);
148
                if (Project.getSeedProjectWindow()!=null) {
149
                        PluginServices.getMDIManager().changeViewInfo(projectFrame, Project.getSeedProjectWindow());
150
                }
151
        }
152

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

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

    
190
                        int res = JOptionPane.showConfirmDialog((Component) PluginServices.getMainFrame(),
191
                                        PluginServices.getText(this, "guardar_cambios"),
192
                                        "gvSIG",
193
                                        JOptionPane.YES_NO_CANCEL_OPTION,
194
                                        JOptionPane.INFORMATION_MESSAGE);
195

    
196
                        if (res == JOptionPane.YES_OPTION) {
197
                                guardar();
198
                        } else if (res == JOptionPane.CANCEL_OPTION) {
199
                                return false;
200
                        }
201
                }
202

    
203
                return true;
204
        }
205

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

    
232
                        JFileChooser jfc = new JFileChooser();
233
                        jfc.addChoosableFileFilter(new GenericFileFilter("gvp",
234
                                        PluginServices.getText(this, "tipo_fichero_proyecto")));
235

    
236
                        if (jfc.showOpenDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
237
                                ProjectView.numViews=0;
238
                ProjectMap.numMaps = 0;
239
                                PluginServices.getMDIManager().closeAllViews();
240
                                Project o = readProject(jfc.getSelectedFile());
241
                                if (o != null) {
242
                                        p = o;
243
                                }
244

    
245
                                projectFrame.setProject(p);
246
                                PluginServices.getMainFrame().setTitle(p.getName());
247
                                projectFrame.refreshControls();
248
                                showProjectWindow();
249
                        }
250
                } else if (actionCommand.equals("GUARDAR")) {
251
                        guardar();
252
                } else if (actionCommand.equals("GUARDAR_COMO")) {
253
                        guardarDialogo();
254
                } else if (actionCommand.equals("SALIR")){
255
                        int option = JOptionPane.showConfirmDialog(null,
256
                                        Messages.getString("MDIFrame.quiere_salir"),
257
                                        Messages.getString("MDIFrame.salir"),
258
                                        JOptionPane.YES_NO_OPTION);
259

    
260
                        if (option == JOptionPane.YES_OPTION) {
261
                                Launcher.closeApplication();
262
                        }
263
                } else if (actionCommand.compareTo("OPENTEMPLATE")==0){
264
                        openLayout();
265
                }
266
        }
267
        public void openLayout() {
268
                //Project project = ((ProjectExtension) PluginServices.getExtension(ProjectExtension.class)).getProject();
269
                Layout layout=null;
270
                JFileChooser jfc = new JFileChooser();
271
                jfc.addChoosableFileFilter(new GenericFileFilter("gvt",
272
                                PluginServices.getText(this, "plantilla")));
273

    
274
                if (jfc.showOpenDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
275
                        File file=jfc.getSelectedFile();
276
                        if (!(file.getPath().endsWith(".gvt") || file.getPath().endsWith(".GVT"))){
277
                                file=new File(file.getPath()+".gvt");
278
                        }
279
                        try {
280
                                File xmlFile = new File(file.getAbsolutePath());
281
                                FileReader reader;
282
                                reader = new FileReader(xmlFile);
283

    
284
                                XmlTag tag = (XmlTag) XmlTag.unmarshal(reader);
285
                                try {
286
                                        layout = Layout.createLayout(new XMLEntity(tag),p);
287
                                } catch (OpenException e) {
288
                                        e.showError();
289
                                }
290
                                //fPanelLegendManager.setRenderer(LegendFactory.createFromXML(new XMLEntity(tag)));
291
                        } catch (FileNotFoundException e) {
292
                                NotificationManager.addError(PluginServices.getText(this, "Al_leer_la_leyenda"), e);
293
                        } catch (MarshalException e) {
294
                                NotificationManager.addError(PluginServices.getText(this, "Al_leer_la_leyenda"), e);
295
                        } catch (ValidationException e) {
296
                                NotificationManager.addError(PluginServices.getText(this, "Al_leer_la_leyenda"), e);
297
                        }
298
                        ProjectMap pmap = ProjectFactory.createMap(file.getName());
299
                        pmap.setModel(layout);
300
                        pmap.getModel().setProjectMap(pmap);
301

    
302
                        p.addMap(pmap);
303
                        PluginServices.getMDIManager().addView(layout);
304

    
305

    
306
                }
307
        }
308
        /**
309
         * Escribe el proyecto en XML.
310
         *
311
         * @param file Fichero.
312
         * @param p Proyecto.
313
         */
314
        public void writeProject(File file, Project p) {
315
                // write it out as XML
316
                try {
317
                        FileWriter writer = new FileWriter(file.getAbsolutePath());
318
                        Marshaller m = new Marshaller(writer);
319
                        m.setEncoding("ISO-8859-1");
320
                        p.setName(file.getName());
321
                        p.setPath(file.toString());
322
                        p.setModificationDate(DateFormat.getDateInstance().format(new Date()));
323
                        p.setModified(false);
324
                        m.marshal(p.getXMLEntity().getXmlTag());
325
                        PluginServices.getMainFrame().setTitle(file.getName());
326
                } catch (Exception e) {
327
                        NotificationManager.addError("Error guardando el proyecto", e);
328
                }
329

    
330
        }
331

    
332
        /**
333
         * Lee del XML el proyecto.<br><br>
334
         *
335
         * Reads the XML of the project.<br>
336
         * 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
337
         * linked to the window you must to set this object to the extension:<br>
338
         *
339
         * <b>Example:</b><br>
340
         *
341
         * ...<br>
342
         * ...<br>
343
         * Project p = ProjectExtension.readProject(projectFile);<br>
344
         * ProjectExtension.setProject(p);
345
         * ...<br>
346
         * ...<br>
347
         * @param file Fichero.
348
         *
349
         * @return Project
350
         *
351
         */
352
        public Project readProject(File file) {
353
                Project proj = null;
354

    
355
                try {
356
                        File xmlFile = new File(file.getAbsolutePath());
357
                        FileReader reader;
358
                        reader = new FileReader(xmlFile);
359
                        XmlTag tag = (XmlTag) XmlTag.unmarshal(reader);
360
                        XMLEntity xml=new XMLEntity(tag);
361
                        String VERSION=xml.getStringProperty("VERSION");
362

    
363
                        try {
364
                                // if ((VERSION!=null) && (VERSION.equals("0.5") || VERSION.equals("0.4") || (VERSION.indexOf("GISPLANET") != -1))){
365
                                if (VERSION != null) {
366
                                        proj = Project.createFromXML(xml);
367
                                }else{
368
                                        proj = Project.createFromXML03(new XMLEntity(tag));
369
                                }
370
                                return proj;
371
                        } catch (OpenException e){
372
                                e.showError();
373
                                //NotificationManager.addInfo("Al leer el proyecto", e);
374
                        }
375
                } catch (FileNotFoundException e) {
376
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this, "fichero_incorrecto"));
377
                        //NotificationManager.addError("Al leer el proyecto", e);
378
                } catch (MarshalException e) {
379
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this, "formato_incorrecto"));
380
                        //NotificationManager.addError("Al leer el proyecto", e);
381
                } catch (ValidationException e) {
382
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this, "formato_incorrecto"));
383
                        //NotificationManager.addError("Al leer el proyecto", e);
384
                }
385

    
386
                return null;
387
        }
388

    
389
        /**
390
         * Devuelve el proyecto.
391
         *
392
         * @return Proyecto.
393
         */
394
        public Project getProject() {
395
                return p;
396
        }
397

    
398
        /**
399
         * @see com.iver.andami.plugins.IExtension#isEnabled()
400
         */
401
        public boolean isEnabled() {
402
                return true;
403
        }
404

    
405
        /**
406
         * @see com.iver.andami.plugins.IExtension#isVisible()
407
         */
408
        public boolean isVisible() {
409
                return true;
410
        }
411

    
412
        /**
413
         * Sets the project
414
         * @param p
415
         */
416
        public void setProject(Project p){
417
                projectFrame.setProject(p);
418
                this.p=p;
419
        }
420
}