Statistics
| Revision:

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

History | View | Annotate | Download (8.7 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 com.iver.andami.PluginServices;
44
import com.iver.andami.messages.NotificationManager;
45
import com.iver.andami.plugins.Extension;
46

    
47
import com.iver.cit.gvsig.fmap.DriverException;
48
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
49
import com.iver.cit.gvsig.fmap.drivers.shp.SHP;
50
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
51
import com.iver.cit.gvsig.fmap.layers.XMLException;
52
import com.iver.cit.gvsig.gui.project.ProjectWindow;
53
import com.iver.cit.gvsig.project.Project;
54
import com.iver.cit.gvsig.project.ProjectFactory;
55
import com.iver.utiles.GenericFileFilter;
56
import com.iver.utiles.NotExistInXMLEntity;
57
import com.iver.utiles.XMLEntity;
58
import com.iver.utiles.xmlEntity.generate.XmlTag;
59

    
60
import org.exolab.castor.xml.MarshalException;
61
import org.exolab.castor.xml.Marshaller;
62
import org.exolab.castor.xml.ValidationException;
63

    
64
import java.awt.Component;
65

    
66
import java.io.File;
67
import java.io.FileNotFoundException;
68
import java.io.FileReader;
69
import java.io.FileWriter;
70

    
71
import java.text.DateFormat;
72

    
73
import java.util.ArrayList;
74
import java.util.Date;
75

    
76
import javax.swing.JFileChooser;
77
import javax.swing.JOptionPane;
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
                                        PluginServices.getText(this, "nuevo_proyecto"),
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
                }
217
        }
218

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

    
241
        }
242

    
243
        /**
244
         * Lee del XML el proyecto.
245
         *
246
         * @param file Fichero.
247
         *
248
         * @return Proyecto.
249
         */
250
        public Project leerProyecto(File file) {
251
                Project proj = null;
252

    
253
                try {
254
                        File xmlFile = new File(file.getAbsolutePath());
255
                        FileReader reader;
256
                        reader = new FileReader(xmlFile);
257
                        XmlTag tag = (XmlTag) XmlTag.unmarshal(reader);
258

    
259
                        try {
260
                                proj = Project.createFromXML(new XMLEntity(tag));
261
                                return proj;
262
                        } catch (XMLException e) {
263
                        } catch (DriverException e) {
264
                        } catch (DriverIOException e2) {
265
                        } catch (NotExistInXMLEntity e){
266
                            
267
                        }
268
                        try {
269
                proj = Project.createFromXML03(new XMLEntity(tag));
270
                            return proj;
271
            } catch (XMLException e1) {
272
                                NotificationManager.addError("Al leer el proyecto", e1);
273
            } catch (DriverException e1) {
274
                                NotificationManager.addError("Al leer el proyecto", e1);
275
            } catch (DriverIOException e1) {
276
                                NotificationManager.addError("Al leer el proyecto", e1);
277
            }
278
                        
279
                } catch (FileNotFoundException e) {
280
                        NotificationManager.addError("Al leer el proyecto", e);
281
                } catch (MarshalException e) {
282
                        NotificationManager.addError("Al leer el proyecto", e);
283
                } catch (ValidationException e) {
284
                        NotificationManager.addError("Al leer el proyecto", e);
285
                }
286
                
287
                return null;
288
        }
289

    
290
        /**
291
         * Devuelve el proyecto.
292
         *
293
         * @return Proyecto.
294
         */
295
        public Project getProject() {
296
                return p;
297
        }
298

    
299
        /**
300
         * @see com.iver.andami.plugins.Extension#isEnabled()
301
         */
302
        public boolean isEnabled() {
303
                return true;
304
        }
305

    
306
        /**
307
         * @see com.iver.andami.plugins.Extension#isVisible()
308
         */
309
        public boolean isVisible() {
310
                return true;
311
        }
312
}