Statistics
| Revision:

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

History | View | Annotate | Download (8.09 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(PluginServices.getText(this, "sin_titulo"));
103

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

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

    
117
        /**
118
         * Guarda el proyecto actual en disco.
119
         */
120
        private void guardar() {
121
                if (p.getPath() == null) {
122
                        guardarDialogo();
123
                } else {
124
                        escribirProyecto(new File(p.getPath()), p);
125
                        projectFrame.refreshControls();
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
                        projectFrame.refreshControls();
140
                }
141
        }
142
        /**
143
         * Guarda si el proyecto ha sido modificado.
144
         *
145
         * @return True si se ha guardado correctamente.
146
         */
147
        private boolean modificado() {
148
                if (p.isModified()) {
149
                        int res = JOptionPane.showConfirmDialog((Component) PluginServices.getMainFrame(),
150
                                        PluginServices.getText(this, "guardar_cambios"),
151
                                        PluginServices.getText(this, "nuevo_proyecto"),
152
                                        JOptionPane.YES_NO_CANCEL_OPTION,
153
                                        JOptionPane.INFORMATION_MESSAGE);
154

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

    
162
                return true;
163
        }
164

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

    
175
                        PluginServices.getMDIManager().closeAllViews();
176
                        p = ProjectFactory.createProject();
177
                        p.setName(PluginServices.getText(this, "untitled"));
178
                        p.setModified(false);
179
                        projectFrame.setProject(p);
180
                        showProjectWindow();
181
                        PluginServices.getMainFrame().setTitle(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(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
        public 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(file.getName());
231
                } catch (Exception e) {
232
                        NotificationManager.addError("Error guardando el proyecto", e);
233
                }
234

    
235
        }
236

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

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

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

    
268
                return proj;
269
        }
270

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

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

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