Statistics
| Revision:

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

History | View | Annotate | Download (7.71 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.layers.LayerFactory;
50
import com.iver.cit.gvsig.fmap.layers.XMLException;
51
import com.iver.cit.gvsig.project.ProjectFactory;
52
import com.iver.cit.gvsig.project.ProjectWindow;
53
import com.iver.cit.gvsig.project.castor.Project;
54

    
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
                        JFileChooser jfc = new JFileChooser();
124
                        jfc.addChoosableFileFilter(new GenericFileFilter("xml",
125
                                        PluginServices.getText(this, "tipo_fichero_proyecto")));
126

    
127
                        if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
128
                                escribirProyecto(jfc.getSelectedFile(), p);
129
                        }
130
                } else {
131
                        escribirProyecto(new File(p.getPath()), p);
132
                }
133
        }
134

    
135
        /**
136
         * Guarda si el proyecto ha sido modificado.
137
         *
138
         * @return True si se ha guardado correctamente.
139
         */
140
        private boolean modificado() {
141
                if (p.isModified()) {
142
                        int res = JOptionPane.showConfirmDialog((Component) PluginServices.getMainFrame(),
143
                                        PluginServices.getText(this, "guardar_cambios"),
144
                                        PluginServices.getText(this, "nuevo_proyecto"),
145
                                        JOptionPane.YES_NO_CANCEL_OPTION,
146
                                        JOptionPane.INFORMATION_MESSAGE);
147

    
148
                        if (res == JOptionPane.YES_OPTION) {
149
                                guardar();
150
                        } else if (res == JOptionPane.CANCEL_OPTION) {
151
                                return false;
152
                        }
153
                }
154

    
155
                return true;
156
        }
157

    
158
        /**
159
         * @see com.iver.mdiApp.plugins.Extension#updateUI(java.lang.String)
160
         */
161
        public void execute(String actionCommand) {
162
                if (actionCommand.equals("NUEVO")) {
163
                        //Si est? modificado se pregunta si se quiere guardar el anterior
164
                        if (!modificado()) {
165
                                return;
166
                        }
167

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

    
182
                        JFileChooser jfc = new JFileChooser();
183
                        jfc.addChoosableFileFilter(new GenericFileFilter("xml",
184
                                        PluginServices.getText(this, "tipo_fichero_proyecto")));
185

    
186
                        if (jfc.showOpenDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
187
                                PluginServices.getMDIManager().closeAllViews();
188

    
189
                                Project o = leerProyecto(jfc.getSelectedFile());
190

    
191
                                if (o != null) {
192
                                        p = o;
193
                                }
194

    
195
                                projectFrame.setProject(p);
196
                                projectFrame.refreshControls();
197
                                showProjectWindow();
198
                        }
199
                } else if (actionCommand.equals("GUARDAR")) {
200
                        guardar();
201
                }
202
        }
203

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

    
225
                projectFrame.refreshControls();
226
        }
227

    
228
        /**
229
         * Lee del XML el proyecto.
230
         *
231
         * @param file Fichero.
232
         *
233
         * @return Proyecto.
234
         */
235
        public Project leerProyecto(File file) {
236
                Project proj = null;
237

    
238
                try {
239
                        File xmlFile = new File(file.getAbsolutePath());
240
                        FileReader reader;
241
                        reader = new FileReader(xmlFile);
242

    
243
                        XmlTag tag = (XmlTag) XmlTag.unmarshal(reader);
244
                        p = Project.createFromXML(new XMLEntity(tag));
245
                } catch (FileNotFoundException e) {
246
                        NotificationManager.addError("Al leer el proyecto", e);
247
                } catch (MarshalException e) {
248
                        NotificationManager.addError("Al leer el proyecto", e);
249
                } catch (ValidationException e) {
250
                        NotificationManager.addError("Al leer el proyecto", e);
251
                } catch (XMLException e) {
252
                        NotificationManager.addError("Al leer el proyecto", e);
253
                } catch (DriverException e) {
254
                        NotificationManager.addError("Al leer el proyecto", e);
255
                } catch (DriverIOException e2) {
256
                        NotificationManager.addError("Al leer el proyecto", e2);
257
                }
258

    
259
                return proj;
260
        }
261

    
262
        /**
263
         * Devuelve el proyecto.
264
         *
265
         * @return Proyecto.
266
         */
267
        public Project getProject() {
268
                return p;
269
        }
270

    
271
        /**
272
         * @see com.iver.andami.plugins.Extension#isEnabled()
273
         */
274
        public boolean isEnabled() {
275
                return true;
276
        }
277

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