Statistics
| Revision:

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

History | View | Annotate | Download (9.38 KB)

1 1103 fjp
/* 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 312 fernando
package com.iver.cit.gvsig;
42
43 2242 jaume
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 2419 caballero
import org.xml.sax.SAXException;
59
import org.xml.sax.SAXParseException;
60 2242 jaume
61
import com.iver.andami.Launcher;
62 596 fernando
import com.iver.andami.PluginServices;
63 2242 jaume
import com.iver.andami.messages.Messages;
64 692 fernando
import com.iver.andami.messages.NotificationManager;
65 596 fernando
import com.iver.andami.plugins.Extension;
66 2242 jaume
import com.iver.andami.ui.mdiFrame.MDIFrame;
67 652 fernando
import com.iver.cit.gvsig.fmap.DriverException;
68 1038 vcaballero
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
69 701 fernando
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
70 596 fernando
import com.iver.cit.gvsig.fmap.layers.XMLException;
71 1221 fernando
import com.iver.cit.gvsig.gui.project.ProjectWindow;
72
import com.iver.cit.gvsig.project.Project;
73 312 fernando
import com.iver.cit.gvsig.project.ProjectFactory;
74 596 fernando
import com.iver.utiles.GenericFileFilter;
75 2183 fernando
import com.iver.utiles.NotExistInXMLEntity;
76 436 vcaballero
import com.iver.utiles.XMLEntity;
77
import com.iver.utiles.xmlEntity.generate.XmlTag;
78 312 fernando
79 1219 vcaballero
80 312 fernando
/**
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 596 fernando
public class ProjectExtension implements Extension {
88 1219 vcaballero
        private ArrayList tableExtensions = new ArrayList();
89
        private ProjectWindow projectFrame;
90
        private Project p;
91 312 fernando
        private boolean modified = false;
92 1219 vcaballero
        /**
93
         * @see com.iver.mdiApp.plugins.Extension#inicializar()
94
         */
95
        public void inicializar() {
96 2183 fernando
            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 1219 vcaballero
                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 1412 fernando
                PluginServices.getMainFrame().setTitle(PluginServices.getText(this, "sin_titulo"));
109 312 fernando
110 1219 vcaballero
                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 596 fernando
                PluginServices.getMDIManager().addView(projectFrame);
121 312 fernando
        }
122
123 1219 vcaballero
        /**
124
         * Guarda el proyecto actual en disco.
125
         */
126
        private void guardar() {
127 312 fernando
                if (p.getPath() == null) {
128 1226 vcaballero
                        guardarDialogo();
129 312 fernando
                } else {
130
                        escribirProyecto(new File(p.getPath()), p);
131 1837 fernando
                        projectFrame.refreshControls();
132 312 fernando
                }
133
        }
134 1226 vcaballero
        private void guardarDialogo(){
135
                JFileChooser jfc = new JFileChooser();
136 1271 vcaballero
                jfc.addChoosableFileFilter(new GenericFileFilter("gvp",
137 1226 vcaballero
                                PluginServices.getText(this, "tipo_fichero_proyecto")));
138 1219 vcaballero
139 1226 vcaballero
                if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
140 1254 vcaballero
                        File file=jfc.getSelectedFile();
141 1271 vcaballero
                        if (!(file.getPath().endsWith(".gvp") || file.getPath().endsWith(".GVP"))){
142
                                file=new File(file.getPath()+".gvp");
143 1254 vcaballero
                        }
144
                        escribirProyecto(file, p);
145 1837 fernando
                        projectFrame.refreshControls();
146 1226 vcaballero
                }
147
        }
148 1219 vcaballero
        /**
149
         * Guarda si el proyecto ha sido modificado.
150
         *
151
         * @return True si se ha guardado correctamente.
152
         */
153
        private boolean modificado() {
154 312 fernando
                if (p.isModified()) {
155 596 fernando
                        int res = JOptionPane.showConfirmDialog((Component) PluginServices.getMainFrame(),
156
                                        PluginServices.getText(this, "guardar_cambios"),
157 2419 caballero
                                        "gvSIG",
158 312 fernando
                                        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 1219 vcaballero
168 312 fernando
                return true;
169
        }
170
171 1219 vcaballero
        /**
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 312 fernando
181 1219 vcaballero
                        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 1412 fernando
                        PluginServices.getMainFrame().setTitle(PluginServices.getText(this, "sin_titulo"));
188 1219 vcaballero
                } 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 1271 vcaballero
                        jfc.addChoosableFileFilter(new GenericFileFilter("gvp",
196 1219 vcaballero
                                        PluginServices.getText(this, "tipo_fichero_proyecto")));
197
198
                        if (jfc.showOpenDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
199 596 fernando
                                PluginServices.getMDIManager().closeAllViews();
200 312 fernando
201 1219 vcaballero
                                Project o = leerProyecto(jfc.getSelectedFile());
202 312 fernando
203 1219 vcaballero
                                if (o != null) {
204
                                        p = o;
205 596 fernando
                                }
206 312 fernando
207 1219 vcaballero
                                projectFrame.setProject(p);
208 1412 fernando
                                PluginServices.getMainFrame().setTitle(p.getName());
209 1219 vcaballero
                                projectFrame.refreshControls();
210
                                showProjectWindow();
211
                        }
212
                } else if (actionCommand.equals("GUARDAR")) {
213
                        guardar();
214 1226 vcaballero
                } else if (actionCommand.equals("GUARDAR_COMO")) {
215
                        guardarDialogo();
216 2242 jaume
                } 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 1219 vcaballero
                }
226
        }
227 312 fernando
228 1219 vcaballero
        /**
229
         * Escribe el proyecto en XML.
230
         *
231
         * @param file Fichero.
232
         * @param p Proyecto.
233
         */
234 1837 fernando
        public void escribirProyecto(File file, Project p) {
235 1219 vcaballero
                // write it out as XML
236 596 fernando
                try {
237 1219 vcaballero
                        FileWriter writer = new FileWriter(file.getAbsolutePath());
238
                        Marshaller m = new Marshaller(writer);
239
                        m.setEncoding("ISO-8859-1");
240 1254 vcaballero
                        p.setName(file.getName());
241 1219 vcaballero
                        p.setPath(file.toString());
242
                        p.setModificationDate(DateFormat.getDateInstance().format(new Date()));
243
                        p.setModified(false);
244 1254 vcaballero
                        m.marshal(p.getXMLEntity().getXmlTag());
245 1412 fernando
                        PluginServices.getMainFrame().setTitle(file.getName());
246 1219 vcaballero
                } catch (Exception e) {
247
                        NotificationManager.addError("Error guardando el proyecto", e);
248
                }
249 312 fernando
250 1219 vcaballero
        }
251
252
        /**
253
         * Lee del XML el proyecto.
254
         *
255
         * @param file Fichero.
256
         *
257
         * @return Proyecto.
258 312 fernando
         */
259 1219 vcaballero
        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 2183 fernando
                        XmlTag tag = (XmlTag) XmlTag.unmarshal(reader);
267 1219 vcaballero
268 2183 fernando
                        try {
269
                                proj = Project.createFromXML(new XMLEntity(tag));
270
                                return proj;
271
                        } catch (XMLException e) {
272
                        } catch (DriverException e) {
273 2419 caballero
                        } catch (DriverIOException e) {
274 2183 fernando
                        } catch (NotExistInXMLEntity e){
275 2419 caballero
                        }
276 2183 fernando
                        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 1219 vcaballero
                } catch (FileNotFoundException e) {
288 2419 caballero
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),"fichero_incorrecto");
289
                        //NotificationManager.addError("Al leer el proyecto", e);
290 1219 vcaballero
                } catch (MarshalException e) {
291 2419 caballero
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),"formato_incorrecto");
292
                        //NotificationManager.addError("Al leer el proyecto", e);
293 1219 vcaballero
                } catch (ValidationException e) {
294 2419 caballero
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),"formato_incorrecto");
295
                        //NotificationManager.addError("Al leer el proyecto", e);
296
                }
297 2183 fernando
298
                return null;
299 1219 vcaballero
        }
300
301
        /**
302
         * Devuelve el proyecto.
303
         *
304
         * @return Proyecto.
305
         */
306 312 fernando
        public Project getProject() {
307
                return p;
308
        }
309
310 596 fernando
        /**
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 312 fernando
}