Statistics
| Revision:

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

History | View | Annotate | Download (8.11 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.XMLEntity;
57
import com.iver.utiles.xmlEntity.generate.XmlTag;
58

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

    
63
import java.awt.Component;
64

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

    
70
import java.text.DateFormat;
71

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

    
75
import javax.swing.JFileChooser;
76
import javax.swing.JOptionPane;
77

    
78

    
79
/**
80
 * Extension que proporciona controles para crear proyectos nuevos, abrirlos y
81
 * guardarlos. Adem?s los tipos de tabla que soporta el proyecto son a?adidos
82
 * en esta clase.
83
 *
84
 * @author Fernando Gonz?lez Cort?s
85
 */
86
public class ProjectExtension implements Extension {
87
        private ArrayList tableExtensions = new ArrayList();
88
        private ProjectWindow projectFrame;
89
        private Project p;
90
        private boolean modified = false;
91

    
92
        /**
93
         * @see com.iver.mdiApp.plugins.Extension#inicializar()
94
         */
95
        public void inicializar() {
96
                p = ProjectFactory.createProject();
97
                p.setName(PluginServices.getText(this, "untitled"));
98
                p.setModified(false);
99
                projectFrame = new ProjectWindow(this);
100
                projectFrame.setProject(p);
101
                showProjectWindow();
102
                PluginServices.getMainFrame().setTitle("gvSIG: " +
103
                        PluginServices.getText(this, "sin_titulo"));
104

    
105
                LayerFactory.setDriversPath(PluginServices.getPluginServices(this)
106
                                                                                                  .getPluginDirectory()
107
                                                                                                  .getAbsolutePath() +
108
                        File.separator + "drivers");
109
        }
110

    
111
        /**
112
         * Muestra la ventana con el gestor de proyectos.
113
         */
114
        public void showProjectWindow() {
115
                PluginServices.getMDIManager().addView(projectFrame);
116
        }
117

    
118
        /**
119
         * Guarda el proyecto actual en disco.
120
         */
121
        private void guardar() {
122
                if (p.getPath() == null) {
123
                        guardarDialogo();
124
                } else {
125
                        escribirProyecto(new File(p.getPath()), p);
126
                }
127
        }
128
        private void guardarDialogo(){
129
                JFileChooser jfc = new JFileChooser();
130
                jfc.addChoosableFileFilter(new GenericFileFilter("gvp",
131
                                PluginServices.getText(this, "tipo_fichero_proyecto")));
132

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

    
154
                        if (res == JOptionPane.YES_OPTION) {
155
                                guardar();
156
                        } else if (res == JOptionPane.CANCEL_OPTION) {
157
                                return false;
158
                        }
159
                }
160

    
161
                return true;
162
        }
163

    
164
        /**
165
         * @see com.iver.mdiApp.plugins.Extension#updateUI(java.lang.String)
166
         */
167
        public void execute(String actionCommand) {
168
                if (actionCommand.equals("NUEVO")) {
169
                        //Si est? modificado se pregunta si se quiere guardar el anterior
170
                        if (!modificado()) {
171
                                return;
172
                        }
173

    
174
                        PluginServices.getMDIManager().closeAllViews();
175
                        p = ProjectFactory.createProject();
176
                        p.setName(PluginServices.getText(this, "untitled"));
177
                        p.setModified(false);
178
                        projectFrame.setProject(p);
179
                        showProjectWindow();
180
                        PluginServices.getMainFrame().setTitle("gvSIG: " +
181
                                PluginServices.getText(this, "sin_titulo"));
182
                } else if (actionCommand.equals("ABRIR")) {
183
                        //Si est? modificado se pregunta si se quiere guardar el anterior
184
                        if (!modificado()) {
185
                                return;
186
                        }
187

    
188
                        JFileChooser jfc = new JFileChooser();
189
                        jfc.addChoosableFileFilter(new GenericFileFilter("gvp",
190
                                        PluginServices.getText(this, "tipo_fichero_proyecto")));
191

    
192
                        if (jfc.showOpenDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
193
                                PluginServices.getMDIManager().closeAllViews();
194

    
195
                                Project o = leerProyecto(jfc.getSelectedFile());
196

    
197
                                if (o != null) {
198
                                        p = o;
199
                                }
200

    
201
                                projectFrame.setProject(p);
202
                                PluginServices.getMainFrame().setTitle("gvSIG: " + p.getName());
203
                                projectFrame.refreshControls();
204
                                showProjectWindow();
205
                        }
206
                } else if (actionCommand.equals("GUARDAR")) {
207
                        guardar();
208
                } else if (actionCommand.equals("GUARDAR_COMO")) {
209
                        guardarDialogo();
210
                }
211
        }
212

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

    
235
                projectFrame.refreshControls();
236
        }
237

    
238
        /**
239
         * Lee del XML el proyecto.
240
         *
241
         * @param file Fichero.
242
         *
243
         * @return Proyecto.
244
         */
245
        public Project leerProyecto(File file) {
246
                Project proj = null;
247

    
248
                try {
249
                        File xmlFile = new File(file.getAbsolutePath());
250
                        FileReader reader;
251
                        reader = new FileReader(xmlFile);
252

    
253
                        XmlTag tag = (XmlTag) XmlTag.unmarshal(reader);
254
                        p = Project.createFromXML(new XMLEntity(tag));
255
                } catch (FileNotFoundException e) {
256
                        NotificationManager.addError("Al leer el proyecto", e);
257
                } catch (MarshalException e) {
258
                        NotificationManager.addError("Al leer el proyecto", e);
259
                } catch (ValidationException e) {
260
                        NotificationManager.addError("Al leer el proyecto", e);
261
                } catch (XMLException e) {
262
                        NotificationManager.addError("Al leer el proyecto", e);
263
                } catch (DriverException e) {
264
                        NotificationManager.addError("Al leer el proyecto", e);
265
                } catch (DriverIOException e2) {
266
                        NotificationManager.addError("Al leer el proyecto", e2);
267
                }
268

    
269
                return proj;
270
        }
271

    
272
        /**
273
         * Devuelve el proyecto.
274
         *
275
         * @return Proyecto.
276
         */
277
        public Project getProject() {
278
                return p;
279
        }
280

    
281
        /**
282
         * @see com.iver.andami.plugins.Extension#isEnabled()
283
         */
284
        public boolean isEnabled() {
285
                return true;
286
        }
287

    
288
        /**
289
         * @see com.iver.andami.plugins.Extension#isVisible()
290
         */
291
        public boolean isVisible() {
292
                return true;
293
        }
294
}