Statistics
| Revision:

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

History | View | Annotate | Download (9.38 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.ArrayList;
50
import java.util.Date;
51

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

    
55
import org.exolab.castor.xml.MarshalException;
56
import org.exolab.castor.xml.Marshaller;
57
import org.exolab.castor.xml.ValidationException;
58
import org.xml.sax.SAXException;
59
import org.xml.sax.SAXParseException;
60

    
61
import com.iver.andami.Launcher;
62
import com.iver.andami.PluginServices;
63
import com.iver.andami.messages.Messages;
64
import com.iver.andami.messages.NotificationManager;
65
import com.iver.andami.plugins.Extension;
66
import com.iver.andami.ui.mdiFrame.MDIFrame;
67
import com.iver.cit.gvsig.fmap.DriverException;
68
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
69
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
70
import com.iver.cit.gvsig.fmap.layers.XMLException;
71
import com.iver.cit.gvsig.gui.project.ProjectWindow;
72
import com.iver.cit.gvsig.project.Project;
73
import com.iver.cit.gvsig.project.ProjectFactory;
74
import com.iver.utiles.GenericFileFilter;
75
import com.iver.utiles.NotExistInXMLEntity;
76
import com.iver.utiles.XMLEntity;
77
import com.iver.utiles.xmlEntity.generate.XmlTag;
78

    
79

    
80
/**
81
 * Extension que proporciona controles para crear proyectos nuevos, abrirlos y
82
 * guardarlos. Adem?s los tipos de tabla que soporta el proyecto son a?adidos
83
 * en esta clase.
84
 *
85
 * @author Fernando Gonz?lez Cort?s
86
 */
87
public class ProjectExtension implements Extension {
88
        private ArrayList tableExtensions = new ArrayList();
89
        private ProjectWindow projectFrame;
90
        private Project p;
91
        private boolean modified = false;
92
        /**
93
         * @see com.iver.mdiApp.plugins.Extension#inicializar()
94
         */
95
        public void inicializar() {
96
            try {
97
            Class.forName("javax.media.jai.EnumeratedParameter");
98
        } catch (ClassNotFoundException e) {
99
            NotificationManager.addError("La m?quina virtual con la que se ejecuta gvSIG no tiene JAI instalado", e);
100
        }
101
            
102
                p = ProjectFactory.createProject();
103
                p.setName(PluginServices.getText(this, "untitled"));
104
                p.setModified(false);
105
                projectFrame = new ProjectWindow(this);
106
                projectFrame.setProject(p);
107
                showProjectWindow();
108
                PluginServices.getMainFrame().setTitle(PluginServices.getText(this, "sin_titulo"));
109

    
110
                LayerFactory.setDriversPath(PluginServices.getPluginServices(this)
111
                                                                                                  .getPluginDirectory()
112
                                                                                                  .getAbsolutePath() +
113
                        File.separator + "drivers");
114
        }
115

    
116
        /**
117
         * Muestra la ventana con el gestor de proyectos.
118
         */
119
        public void showProjectWindow() {
120
                PluginServices.getMDIManager().addView(projectFrame);
121
        }
122

    
123
        /**
124
         * Guarda el proyecto actual en disco.
125
         */
126
        private void guardar() {
127
                if (p.getPath() == null) {
128
                        guardarDialogo();
129
                } else {
130
                        escribirProyecto(new File(p.getPath()), p);
131
                        projectFrame.refreshControls();
132
                }
133
        }
134
        private void guardarDialogo(){
135
                JFileChooser jfc = new JFileChooser();
136
                jfc.addChoosableFileFilter(new GenericFileFilter("gvp",
137
                                PluginServices.getText(this, "tipo_fichero_proyecto")));
138

    
139
                if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
140
                        File file=jfc.getSelectedFile();
141
                        if (!(file.getPath().endsWith(".gvp") || file.getPath().endsWith(".GVP"))){
142
                                file=new File(file.getPath()+".gvp");
143
                        }
144
                        escribirProyecto(file, p);
145
                        projectFrame.refreshControls();
146
                }
147
        }
148
        /**
149
         * Guarda si el proyecto ha sido modificado.
150
         *
151
         * @return True si se ha guardado correctamente.
152
         */
153
        private boolean modificado() {
154
                if (p.isModified()) {
155
                        int res = JOptionPane.showConfirmDialog((Component) PluginServices.getMainFrame(),
156
                                        PluginServices.getText(this, "guardar_cambios"),
157
                                        "gvSIG",
158
                                        JOptionPane.YES_NO_CANCEL_OPTION,
159
                                        JOptionPane.INFORMATION_MESSAGE);
160

    
161
                        if (res == JOptionPane.YES_OPTION) {
162
                                guardar();
163
                        } else if (res == JOptionPane.CANCEL_OPTION) {
164
                                return false;
165
                        }
166
                }
167

    
168
                return true;
169
        }
170

    
171
        /**
172
         * @see com.iver.mdiApp.plugins.Extension#updateUI(java.lang.String)
173
         */
174
        public void execute(String actionCommand) {
175
                if (actionCommand.equals("NUEVO")) {
176
                        //Si est? modificado se pregunta si se quiere guardar el anterior
177
                        if (!modificado()) {
178
                                return;
179
                        }
180

    
181
                        PluginServices.getMDIManager().closeAllViews();
182
                        p = ProjectFactory.createProject();
183
                        p.setName(PluginServices.getText(this, "untitled"));
184
                        p.setModified(false);
185
                        projectFrame.setProject(p);
186
                        showProjectWindow();
187
                        PluginServices.getMainFrame().setTitle(PluginServices.getText(this, "sin_titulo"));
188
                } else if (actionCommand.equals("ABRIR")) {
189
                        //Si est? modificado se pregunta si se quiere guardar el anterior
190
                        if (!modificado()) {
191
                                return;
192
                        }
193

    
194
                        JFileChooser jfc = new JFileChooser();
195
                        jfc.addChoosableFileFilter(new GenericFileFilter("gvp",
196
                                        PluginServices.getText(this, "tipo_fichero_proyecto")));
197

    
198
                        if (jfc.showOpenDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
199
                                PluginServices.getMDIManager().closeAllViews();
200

    
201
                                Project o = leerProyecto(jfc.getSelectedFile());
202

    
203
                                if (o != null) {
204
                                        p = o;
205
                                }
206

    
207
                                projectFrame.setProject(p);
208
                                PluginServices.getMainFrame().setTitle(p.getName());
209
                                projectFrame.refreshControls();
210
                                showProjectWindow();
211
                        }
212
                } else if (actionCommand.equals("GUARDAR")) {
213
                        guardar();
214
                } else if (actionCommand.equals("GUARDAR_COMO")) {
215
                        guardarDialogo();
216
                } else if (actionCommand.equals("SALIR")){
217
                        int option = JOptionPane.showConfirmDialog(null,
218
                                        Messages.getString("MDIFrame.quiere_salir"),
219
                                        Messages.getString("MDIFrame.salir"),
220
                                        JOptionPane.YES_NO_OPTION);
221

    
222
                        if (option == JOptionPane.YES_OPTION) {
223
                                Launcher.closeApplication();
224
                        }
225
                }
226
        }
227

    
228
        /**
229
         * Escribe el proyecto en XML.
230
         *
231
         * @param file Fichero.
232
         * @param p Proyecto.
233
         */
234
        public void escribirProyecto(File file, Project p) {
235
                // write it out as XML
236
                try {
237
                        FileWriter writer = new FileWriter(file.getAbsolutePath());
238
                        Marshaller m = new Marshaller(writer);
239
                        m.setEncoding("ISO-8859-1");
240
                        p.setName(file.getName());
241
                        p.setPath(file.toString());
242
                        p.setModificationDate(DateFormat.getDateInstance().format(new Date()));
243
                        p.setModified(false);
244
                        m.marshal(p.getXMLEntity().getXmlTag());
245
                        PluginServices.getMainFrame().setTitle(file.getName());
246
                } catch (Exception e) {
247
                        NotificationManager.addError("Error guardando el proyecto", e);
248
                }
249

    
250
        }
251

    
252
        /**
253
         * Lee del XML el proyecto.
254
         *
255
         * @param file Fichero.
256
         *
257
         * @return Proyecto.
258
         */
259
        public Project leerProyecto(File file) {
260
                Project proj = null;
261

    
262
                try {
263
                        File xmlFile = new File(file.getAbsolutePath());
264
                        FileReader reader;
265
                        reader = new FileReader(xmlFile);
266
                        XmlTag tag = (XmlTag) XmlTag.unmarshal(reader);
267

    
268
                        try {
269
                                proj = Project.createFromXML(new XMLEntity(tag));
270
                                return proj;
271
                        } catch (XMLException e) {
272
                        } catch (DriverException e) {
273
                        } catch (DriverIOException e) {
274
                        } catch (NotExistInXMLEntity e){
275
                        } 
276
                        try {
277
                proj = Project.createFromXML03(new XMLEntity(tag));
278
                            return proj;
279
            } catch (XMLException e1) {
280
                                NotificationManager.addError("Al leer el proyecto", e1);
281
            } catch (DriverException e1) {
282
                                NotificationManager.addError("Al leer el proyecto", e1);
283
            } catch (DriverIOException e1) {
284
                                NotificationManager.addError("Al leer el proyecto", e1);
285
            }
286
                        
287
                } catch (FileNotFoundException e) {
288
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),"fichero_incorrecto");
289
                        //NotificationManager.addError("Al leer el proyecto", e);
290
                } catch (MarshalException e) {
291
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),"formato_incorrecto");
292
                        //NotificationManager.addError("Al leer el proyecto", e);
293
                } catch (ValidationException e) {
294
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),"formato_incorrecto");
295
                        //NotificationManager.addError("Al leer el proyecto", e);
296
                } 
297
                
298
                return null;
299
        }
300

    
301
        /**
302
         * Devuelve el proyecto.
303
         *
304
         * @return Proyecto.
305
         */
306
        public Project getProject() {
307
                return p;
308
        }
309

    
310
        /**
311
         * @see com.iver.andami.plugins.Extension#isEnabled()
312
         */
313
        public boolean isEnabled() {
314
                return true;
315
        }
316

    
317
        /**
318
         * @see com.iver.andami.plugins.Extension#isVisible()
319
         */
320
        public boolean isVisible() {
321
                return true;
322
        }
323
}