Statistics
| Revision:

svn-gvsig-desktop / tags / Root_gvSIG_CAD_Layout_version / applications / appgvSIG / src / com / iver / cit / gvsig / ProjectExtension.java @ 1648

History | View | Annotate | Download (8.05 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
                }
126
        }
127
        private void guardarDialogo(){
128
                JFileChooser jfc = new JFileChooser();
129
                jfc.addChoosableFileFilter(new GenericFileFilter("gvp",
130
                                PluginServices.getText(this, "tipo_fichero_proyecto")));
131

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

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

    
160
                return true;
161
        }
162

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

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

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

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

    
193
                                Project o = leerProyecto(jfc.getSelectedFile());
194

    
195
                                if (o != null) {
196
                                        p = o;
197
                                }
198

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

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

    
233
                projectFrame.refreshControls();
234
        }
235

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

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

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

    
267
                return proj;
268
        }
269

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

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

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