Statistics
| Revision:

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

History | View | Annotate | Download (11.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 java.awt.Component;
44
import java.io.File;
45
import java.io.FileNotFoundException;
46
import java.io.FileReader;
47
import java.io.FileWriter;
48
import java.text.DateFormat;
49
import java.util.Date;
50

    
51
import javax.swing.JFileChooser;
52
import javax.swing.JOptionPane;
53

    
54
import org.exolab.castor.xml.MarshalException;
55
import org.exolab.castor.xml.Marshaller;
56
import org.exolab.castor.xml.ValidationException;
57

    
58
import com.iver.andami.Launcher;
59
import com.iver.andami.PluginServices;
60
import com.iver.andami.messages.Messages;
61
import com.iver.andami.messages.NotificationManager;
62
import com.iver.andami.plugins.Extension;
63
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
64
import com.iver.cit.gvsig.gui.layout.Layout;
65
import com.iver.cit.gvsig.gui.project.OpenException;
66
import com.iver.cit.gvsig.gui.project.ProjectWindow;
67
import com.iver.cit.gvsig.project.Project;
68
import com.iver.cit.gvsig.project.ProjectFactory;
69
import com.iver.cit.gvsig.project.ProjectMap;
70
import com.iver.cit.gvsig.project.ProjectView;
71
import com.iver.utiles.GenericFileFilter;
72
import com.iver.utiles.XMLEntity;
73
import com.iver.utiles.xmlEntity.generate.XmlTag;
74

    
75

    
76
/**
77
 * Extension que proporciona controles para crear proyectos nuevos, abrirlos y
78
 * guardarlos. Adem?s los tipos de tabla que soporta el proyecto son a?adidos
79
 * en esta clase.
80
 *
81
 * @author Fernando Gonz?lez Cort?s
82
 */
83
public class ProjectExtension implements Extension {
84
        private ProjectWindow projectFrame;
85
        private Project p;
86
        /**
87
         * @see com.iver.mdiApp.plugins.Extension#inicializar()
88
         */
89
        public void inicializar() {
90
            try {
91
            Class.forName("javax.media.jai.EnumeratedParameter");
92
        } catch (ClassNotFoundException e) {
93
            NotificationManager.addError("La m?quina virtual con la que se ejecuta gvSIG no tiene JAI instalado", e);
94
        }
95
            
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
                if (true) {
150
                        //TODO de momento se queda como modificado siempre, para poder controlar perfectamente cuando un proyecto ha sido modificado 
151
                        //hay que recoger los eventos de cambio de leyenda, cambio de extent, a?adir una capa, etc que se encuentran en FMap.
152
                        int res = JOptionPane.showConfirmDialog((Component) PluginServices.getMainFrame(),
153
                                        PluginServices.getText(this, "guardar_cambios"),
154
                                        "gvSIG",
155
                                        JOptionPane.YES_NO_CANCEL_OPTION,
156
                                        JOptionPane.INFORMATION_MESSAGE);
157

    
158
                        if (res == JOptionPane.YES_OPTION) {
159
                                guardar();
160
                        } else if (res == JOptionPane.CANCEL_OPTION) {
161
                                return false;
162
                        }
163
                }
164

    
165
                return true;
166
        }
167

    
168
        /**
169
         * @see com.iver.mdiApp.plugins.Extension#updateUI(java.lang.String)
170
         */
171
        public void execute(String actionCommand) {
172
                if (actionCommand.equals("NUEVO")) {
173
                        //Si est? modificado se pregunta si se quiere guardar el anterior
174
                        if (!modificado()) {
175
                                return;
176
                        }
177
                        ProjectView.numViews=0;
178
            ProjectMap.numMaps = 0;
179
                        PluginServices.getMDIManager().closeAllViews();
180
                        p = ProjectFactory.createProject();
181
                        p.setName(PluginServices.getText(this, "untitled"));
182
                        p.setModified(false);
183
                        projectFrame.setProject(p);
184
                        showProjectWindow();
185
                        PluginServices.getMainFrame().setTitle(PluginServices.getText(this, "sin_titulo"));
186
                } else if (actionCommand.equals("ABRIR")) {
187
                        //Si est? modificado se pregunta si se quiere guardar el anterior
188
                        if (!modificado()) {
189
                                ProjectView.numViews=0;
190
                ProjectMap.numMaps = 0;
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
                                ProjectView.numViews=0;
200
                ProjectMap.numMaps = 0;
201
                                PluginServices.getMDIManager().closeAllViews();
202
                                Project o = leerProyecto(jfc.getSelectedFile());
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
                } else if (actionCommand.equals("SALIR")){
217
                        int option = JOptionPane.showConfirmDialog(null,
218
                                        Messages.getString("MDIFrame.quiere_salir"),
219
                                        Messages.getString("MDIFrame.salir"),
220
                                        JOptionPane.YES_NO_OPTION);
221

    
222
                        if (option == JOptionPane.YES_OPTION) {
223
                                Launcher.closeApplication();
224
                        }
225
                } else if (actionCommand.compareTo("OPENTEMPLATE")==0){
226
                        openLayout();
227
                }
228
        }
229
        public void openLayout() {
230
                //Project project = ((ProjectExtension) PluginServices.getExtension(ProjectExtension.class)).getProject();
231
                Layout layout=null;
232
                JFileChooser jfc = new JFileChooser();
233
                jfc.addChoosableFileFilter(new GenericFileFilter("gvt",
234
                                PluginServices.getText(this, "plantilla")));
235

    
236
                if (jfc.showOpenDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
237
                        File file=jfc.getSelectedFile();
238
                        if (!(file.getPath().endsWith(".gvt") || file.getPath().endsWith(".GVT"))){
239
                                file=new File(file.getPath()+".gvt");
240
                        }
241
                        try {
242
                                File xmlFile = new File(file.getAbsolutePath());
243
                                FileReader reader;
244
                                reader = new FileReader(xmlFile);
245

    
246
                                XmlTag tag = (XmlTag) XmlTag.unmarshal(reader);
247
                                try {
248
                                        layout = Layout.createLayout(new XMLEntity(tag),p);
249
                                } catch (OpenException e) {
250
                                        e.showError();
251
                                }
252
                                //fPanelLegendManager.setRenderer(LegendFactory.createFromXML(new XMLEntity(tag)));
253
                        } catch (FileNotFoundException e) {
254
                                NotificationManager.addError(PluginServices.getText(this, "Al_leer_la_leyenda"), e);
255
                        } catch (MarshalException e) {
256
                                NotificationManager.addError(PluginServices.getText(this, "Al leer la leyenda"), e);
257
                        } catch (ValidationException e) {
258
                                NotificationManager.addError(PluginServices.getText(this, "Al leer la leyenda"), e);
259
                        }
260
                        ProjectMap pmap = ProjectFactory.createMap(file.getName());
261
                        pmap.setModel(layout);
262
                        pmap.getModel().setProjectMap(pmap);
263
                        
264
                        p.addMap(pmap);
265
                        PluginServices.getMDIManager().addView(layout);
266
                        
267
                        
268
                }
269
        }
270
        /**
271
         * Escribe el proyecto en XML.
272
         *
273
         * @param file Fichero.
274
         * @param p Proyecto.
275
         */
276
        public void escribirProyecto(File file, Project p) {
277
                // write it out as XML
278
                try {
279
                        FileWriter writer = new FileWriter(file.getAbsolutePath());
280
                        Marshaller m = new Marshaller(writer);
281
                        m.setEncoding("ISO-8859-1");
282
                        p.setName(file.getName());
283
                        p.setPath(file.toString());
284
                        p.setModificationDate(DateFormat.getDateInstance().format(new Date()));
285
                        p.setModified(false);
286
                        m.marshal(p.getXMLEntity().getXmlTag());
287
                        PluginServices.getMainFrame().setTitle(file.getName());
288
                } catch (Exception e) {
289
                        NotificationManager.addError("Error guardando el proyecto", e);
290
                }
291

    
292
        }
293

    
294
        /**
295
         * Lee del XML el proyecto.<br><br>
296
         * 
297
         * Reads the XML of the project.<br>
298
         * It returns n project object holding all needed info that is not linked to the Project Dialog. <br>In case you want the project to be
299
         * linked to the window you must to set this object to the extension:<br>
300
         * 
301
         * <b>Example:</b><br>
302
         *  
303
         * ...<br>
304
         * ...<br>
305
         * Project p = ProjectExtension.leerProyecto(projectFile);<br>
306
         * ProjectExtension.setProject(p);
307
         * ...<br>
308
         * ...<br>
309
         * @param file Fichero.
310
         *
311
         * @return Project
312
         * 
313
         */
314
        public Project leerProyecto(File file) {
315
                Project proj = null;
316

    
317
                try {
318
                        File xmlFile = new File(file.getAbsolutePath());
319
                        FileReader reader;
320
                        reader = new FileReader(xmlFile);
321
                        XmlTag tag = (XmlTag) XmlTag.unmarshal(reader);
322
                        XMLEntity xml=new XMLEntity(tag);
323
                        String VERSION=xml.getStringProperty("VERSION");
324
                        
325
                        try {
326
                                // if ((VERSION!=null) && (VERSION.equals("0.5") || VERSION.equals("0.4") || (VERSION.indexOf("GISPLANET") != -1))){
327
                                if (VERSION != null) {
328
                                        proj = Project.createFromXML(xml);
329
                                }else{
330
                                        proj = Project.createFromXML03(new XMLEntity(tag));
331
                                }
332
                                return proj;
333
                        } catch (OpenException e){
334
                                e.showError();
335
                                //NotificationManager.addInfo("Al leer el proyecto", e);
336
                        }
337
                } catch (FileNotFoundException e) {
338
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this, "fichero_incorrecto"));
339
                        //NotificationManager.addError("Al leer el proyecto", e);
340
                } catch (MarshalException e) {
341
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this, "formato_incorrecto"));
342
                        //NotificationManager.addError("Al leer el proyecto", e);
343
                } catch (ValidationException e) {
344
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this, "formato_incorrecto"));
345
                        //NotificationManager.addError("Al leer el proyecto", e);
346
                } 
347
                
348
                return null;
349
        }
350

    
351
        /**
352
         * Devuelve el proyecto.
353
         *
354
         * @return Proyecto.
355
         */
356
        public Project getProject() {
357
                return p;
358
        }
359

    
360
        /**
361
         * @see com.iver.andami.plugins.Extension#isEnabled()
362
         */
363
        public boolean isEnabled() {
364
                return true;
365
        }
366

    
367
        /**
368
         * @see com.iver.andami.plugins.Extension#isVisible()
369
         */
370
        public boolean isVisible() {
371
                return true;
372
        }
373
        
374
        /**
375
         * Sets the project
376
         * @param p
377
         */
378
        public void setProject(Project p){
379
                projectFrame.setProject(p);
380
                this.p=p;
381
        }
382
}