Statistics
| Revision:

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

History | View | Annotate | Download (13.9 KB)

1 1103 fjp
/* 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 312 fernando
package com.iver.cit.gvsig;
42
43 2242 jaume
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 596 fernando
import com.iver.andami.PluginServices;
60 2242 jaume
import com.iver.andami.messages.Messages;
61 692 fernando
import com.iver.andami.messages.NotificationManager;
62 596 fernando
import com.iver.andami.plugins.Extension;
63 701 fernando
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
64 3115 caballero
import com.iver.cit.gvsig.gui.layout.Layout;
65 3185 caballero
import com.iver.cit.gvsig.gui.project.OpenException;
66 1221 fernando
import com.iver.cit.gvsig.gui.project.ProjectWindow;
67
import com.iver.cit.gvsig.project.Project;
68 312 fernando
import com.iver.cit.gvsig.project.ProjectFactory;
69 2708 fjp
import com.iver.cit.gvsig.project.ProjectMap;
70 2609 caballero
import com.iver.cit.gvsig.project.ProjectView;
71 596 fernando
import com.iver.utiles.GenericFileFilter;
72 436 vcaballero
import com.iver.utiles.XMLEntity;
73
import com.iver.utiles.xmlEntity.generate.XmlTag;
74 312 fernando
75 1219 vcaballero
76 312 fernando
/**
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 5005 jorpiell
public class ProjectExtension extends Extension {
84 1219 vcaballero
        private ProjectWindow projectFrame;
85
        private Project p;
86
        /**
87 5005 jorpiell
         * @see com.iver.mdiApp.plugins.IExtension#initialize()
88 1219 vcaballero
         */
89 5005 jorpiell
        public void initialize() {
90 2183 fernando
            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 4748 fjp
96
                LayerFactory.setWritersPath(PluginServices.getPluginServices(this)
97 3666 fjp
                                  .getPluginDirectory()
98
                                  .getAbsolutePath() +
99
                                  File.separator + "drivers");
100 6153 caballero
101 4748 fjp
        LayerFactory.setDriversPath(PluginServices.getPluginServices(this)
102 4140 caballero
                                  .getPluginDirectory()
103
                                  .getAbsolutePath() +
104 4748 fjp
                                  File.separator + "drivers");
105 4140 caballero
106 3666 fjp
        // Recuperamos el ?ltimo argumento, que se supone
107
        // que ser? el fichero .gvp que queremos abrir.
108 4140 caballero
        // (por enmedio pueden venir o no otros argumentos,
109 3666 fjp
        // por ejemplo el idioma)
110
        // TODO: Aqu? Jaume podr?a meter lo del backup del proyecto
111
        // que ha hecho para ValenciaUrban?stica
112 4140 caballero
113 3666 fjp
                /*
114
                pe = (ProjectExtension) PluginServices.getExtension(com.iver.cit.gvsig.ProjectExtension.class);
115
                if (projectFile.length()!=0){
116 4140 caballero
                        p=pe.readProject(projectFile);
117 3666 fjp
                }
118
                else
119
                        p = restorePreviousProject();
120
                pe.setProject(p); */
121 4140 caballero
        String[] theArgs = PluginServices.getArguments();
122 3666 fjp
        String lastArg = theArgs[theArgs.length-1];
123
        if ((lastArg.endsWith(".gvp")) ||
124
                        (lastArg.endsWith(".GVP")))
125
        {
126
                PluginServices.getLogger().debug("Intentando cargar el proyecto " + lastArg);
127
                File projectFile = new File(lastArg);
128
                p = readProject(projectFile);
129
                PluginServices.getMainFrame().setTitle(p.getName());
130
        }
131
        else
132
        {
133
                        p = ProjectFactory.createProject();
134
                        p.setName(PluginServices.getText(this, "untitled"));
135
                        p.setModified(false);
136
                        PluginServices.getMainFrame().setTitle(PluginServices.getText(this, "sin_titulo"));
137
        }
138 5028 cesar
            projectFrame = new ProjectWindow(this);
139 1219 vcaballero
                projectFrame.setProject(p);
140
                showProjectWindow();
141
        }
142
143
        /**
144
         * Muestra la ventana con el gestor de proyectos.
145
         */
146
        public void showProjectWindow() {
147 596 fernando
                PluginServices.getMDIManager().addView(projectFrame);
148 5028 cesar
                if (Project.getSeedProjectWindow()!=null) {
149
                        PluginServices.getMDIManager().changeViewInfo(projectFrame, Project.getSeedProjectWindow());
150
                }
151 312 fernando
        }
152
153 1219 vcaballero
        /**
154
         * Guarda el proyecto actual en disco.
155
         */
156 6190 caballero
        private boolean guardar() {
157
                boolean saved=false;
158 312 fernando
                if (p.getPath() == null) {
159 6190 caballero
                        saved=guardarDialogo();
160 312 fernando
                } else {
161 6190 caballero
                        saved=writeProject(new File(p.getPath()), p);
162 1837 fernando
                        projectFrame.refreshControls();
163 312 fernando
                }
164 6190 caballero
                return saved;
165 312 fernando
        }
166 6190 caballero
        private boolean guardarDialogo(){
167
                boolean saved=false;
168 1226 vcaballero
                JFileChooser jfc = new JFileChooser();
169 1271 vcaballero
                jfc.addChoosableFileFilter(new GenericFileFilter("gvp",
170 1226 vcaballero
                                PluginServices.getText(this, "tipo_fichero_proyecto")));
171 1219 vcaballero
172 1226 vcaballero
                if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
173 1254 vcaballero
                        File file=jfc.getSelectedFile();
174 1271 vcaballero
                        if (!(file.getPath().endsWith(".gvp") || file.getPath().endsWith(".GVP"))){
175
                                file=new File(file.getPath()+".gvp");
176 1254 vcaballero
                        }
177 6190 caballero
                        saved=writeProject(file, p);
178 1837 fernando
                        projectFrame.refreshControls();
179 1226 vcaballero
                }
180 6190 caballero
                return saved;
181 1226 vcaballero
        }
182 1219 vcaballero
        /**
183
         * Guarda si el proyecto ha sido modificado.
184
         *
185
         * @return True si se ha guardado correctamente.
186
         */
187
        private boolean modificado() {
188 3586 caballero
                if (p.isModified()) {
189
                ///if (true) {
190 4140 caballero
                        //TODO de momento se queda como modificado siempre menos cuando est? totalmente vacio que se tomar? como no modificado,
191
                        //para poder controlar perfectamente cuando un proyecto ha sido modificado
192 2785 caballero
                        //hay que recoger los eventos de cambio de leyenda, cambio de extent, a?adir una capa, etc que se encuentran en FMap.
193 4140 caballero
194 596 fernando
                        int res = JOptionPane.showConfirmDialog((Component) PluginServices.getMainFrame(),
195
                                        PluginServices.getText(this, "guardar_cambios"),
196 2419 caballero
                                        "gvSIG",
197 312 fernando
                                        JOptionPane.YES_NO_CANCEL_OPTION,
198
                                        JOptionPane.INFORMATION_MESSAGE);
199
200
                        if (res == JOptionPane.YES_OPTION) {
201
                                guardar();
202
                        } else if (res == JOptionPane.CANCEL_OPTION) {
203
                                return false;
204
                        }
205
                }
206 1219 vcaballero
207 312 fernando
                return true;
208
        }
209
210 1219 vcaballero
        /**
211 5005 jorpiell
         * @see com.iver.mdiApp.plugins.IExtension#updateUI(java.lang.String)
212 1219 vcaballero
         */
213
        public void execute(String actionCommand) {
214
                if (actionCommand.equals("NUEVO")) {
215
                        //Si est? modificado se pregunta si se quiere guardar el anterior
216
                        if (!modificado()) {
217
                                return;
218
                        }
219 2609 caballero
                        ProjectView.numViews=0;
220 2708 fjp
            ProjectMap.numMaps = 0;
221 1219 vcaballero
                        PluginServices.getMDIManager().closeAllViews();
222
                        p = ProjectFactory.createProject();
223
                        p.setName(PluginServices.getText(this, "untitled"));
224
                        p.setModified(false);
225
                        projectFrame.setProject(p);
226
                        showProjectWindow();
227 1412 fernando
                        PluginServices.getMainFrame().setTitle(PluginServices.getText(this, "sin_titulo"));
228 1219 vcaballero
                } else if (actionCommand.equals("ABRIR")) {
229
                        //Si est? modificado se pregunta si se quiere guardar el anterior
230
                        if (!modificado()) {
231 2609 caballero
                                ProjectView.numViews=0;
232 2708 fjp
                ProjectMap.numMaps = 0;
233 1219 vcaballero
                                return;
234
                        }
235 4140 caballero
236 1219 vcaballero
                        JFileChooser jfc = new JFileChooser();
237 1271 vcaballero
                        jfc.addChoosableFileFilter(new GenericFileFilter("gvp",
238 1219 vcaballero
                                        PluginServices.getText(this, "tipo_fichero_proyecto")));
239
240
                        if (jfc.showOpenDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
241 2609 caballero
                                ProjectView.numViews=0;
242 2708 fjp
                ProjectMap.numMaps = 0;
243 596 fernando
                                PluginServices.getMDIManager().closeAllViews();
244 3666 fjp
                                Project o = readProject(jfc.getSelectedFile());
245 1219 vcaballero
                                if (o != null) {
246
                                        p = o;
247 596 fernando
                                }
248 312 fernando
249 1219 vcaballero
                                projectFrame.setProject(p);
250 1412 fernando
                                PluginServices.getMainFrame().setTitle(p.getName());
251 1219 vcaballero
                                projectFrame.refreshControls();
252
                                showProjectWindow();
253
                        }
254
                } else if (actionCommand.equals("GUARDAR")) {
255
                        guardar();
256 1226 vcaballero
                } else if (actionCommand.equals("GUARDAR_COMO")) {
257
                        guardarDialogo();
258 2242 jaume
                } else if (actionCommand.equals("SALIR")){
259 6190 caballero
                        int option = JOptionPane.showConfirmDialog((Component)PluginServices.getMainFrame(),
260 2242 jaume
                                        Messages.getString("MDIFrame.quiere_salir"),
261
                                        Messages.getString("MDIFrame.salir"),
262
                                        JOptionPane.YES_NO_OPTION);
263
264
                        if (option == JOptionPane.YES_OPTION) {
265
                                Launcher.closeApplication();
266
                        }
267 3115 caballero
                } else if (actionCommand.compareTo("OPENTEMPLATE")==0){
268
                        openLayout();
269 1219 vcaballero
                }
270
        }
271 3115 caballero
        public void openLayout() {
272 3135 caballero
                //Project project = ((ProjectExtension) PluginServices.getExtension(ProjectExtension.class)).getProject();
273 3115 caballero
                Layout layout=null;
274
                JFileChooser jfc = new JFileChooser();
275
                jfc.addChoosableFileFilter(new GenericFileFilter("gvt",
276
                                PluginServices.getText(this, "plantilla")));
277 312 fernando
278 3115 caballero
                if (jfc.showOpenDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
279
                        File file=jfc.getSelectedFile();
280
                        if (!(file.getPath().endsWith(".gvt") || file.getPath().endsWith(".GVT"))){
281
                                file=new File(file.getPath()+".gvt");
282
                        }
283
                        try {
284
                                File xmlFile = new File(file.getAbsolutePath());
285
                                FileReader reader;
286
                                reader = new FileReader(xmlFile);
287
288
                                XmlTag tag = (XmlTag) XmlTag.unmarshal(reader);
289 3185 caballero
                                try {
290
                                        layout = Layout.createLayout(new XMLEntity(tag),p);
291
                                } catch (OpenException e) {
292
                                        e.showError();
293
                                }
294 3115 caballero
                                //fPanelLegendManager.setRenderer(LegendFactory.createFromXML(new XMLEntity(tag)));
295
                        } catch (FileNotFoundException e) {
296
                                NotificationManager.addError(PluginServices.getText(this, "Al_leer_la_leyenda"), e);
297
                        } catch (MarshalException e) {
298 5503 cesar
                                NotificationManager.addError(PluginServices.getText(this, "Al_leer_la_leyenda"), e);
299 3115 caballero
                        } catch (ValidationException e) {
300 5503 cesar
                                NotificationManager.addError(PluginServices.getText(this, "Al_leer_la_leyenda"), e);
301 3115 caballero
                        }
302
                        ProjectMap pmap = ProjectFactory.createMap(file.getName());
303
                        pmap.setModel(layout);
304
                        pmap.getModel().setProjectMap(pmap);
305 4140 caballero
306 3135 caballero
                        p.addMap(pmap);
307 3115 caballero
                        PluginServices.getMDIManager().addView(layout);
308 4140 caballero
309
310 3115 caballero
                }
311
        }
312 1219 vcaballero
        /**
313
         * Escribe el proyecto en XML.
314
         *
315
         * @param file Fichero.
316
         * @param p Proyecto.
317
         */
318 6190 caballero
        public boolean writeProject(File file, Project p) {
319 6153 caballero
                if( file.exists()){
320
                        int resp = JOptionPane.showConfirmDialog(
321
                                        (Component) PluginServices.getMainFrame(),PluginServices.getText(this,"fichero_ya_existe_seguro_desea_guardarlo"),
322
                                        PluginServices.getText(this,"guardar"), JOptionPane.YES_NO_OPTION);
323
                        if (resp != JOptionPane.YES_OPTION) {
324 6190 caballero
                                return false;
325 6153 caballero
                        }
326
                }
327 1219 vcaballero
                // write it out as XML
328 596 fernando
                try {
329 1219 vcaballero
                        FileWriter writer = new FileWriter(file.getAbsolutePath());
330
                        Marshaller m = new Marshaller(writer);
331
                        m.setEncoding("ISO-8859-1");
332 1254 vcaballero
                        p.setName(file.getName());
333 1219 vcaballero
                        p.setPath(file.toString());
334
                        p.setModificationDate(DateFormat.getDateInstance().format(new Date()));
335
                        p.setModified(false);
336 1254 vcaballero
                        m.marshal(p.getXMLEntity().getXmlTag());
337 1412 fernando
                        PluginServices.getMainFrame().setTitle(file.getName());
338 1219 vcaballero
                } catch (Exception e) {
339
                        NotificationManager.addError("Error guardando el proyecto", e);
340 6190 caballero
                        return false;
341 1219 vcaballero
                }
342 6190 caballero
                return true;
343 1219 vcaballero
        }
344
345
        /**
346 2922 jaume
         * Lee del XML el proyecto.<br><br>
347 4140 caballero
         *
348 2922 jaume
         * Reads the XML of the project.<br>
349
         * 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
350
         * linked to the window you must to set this object to the extension:<br>
351 4140 caballero
         *
352 2922 jaume
         * <b>Example:</b><br>
353 4140 caballero
         *
354 2922 jaume
         * ...<br>
355
         * ...<br>
356 3666 fjp
         * Project p = ProjectExtension.readProject(projectFile);<br>
357 2922 jaume
         * ProjectExtension.setProject(p);
358
         * ...<br>
359
         * ...<br>
360 1219 vcaballero
         * @param file Fichero.
361
         *
362 2922 jaume
         * @return Project
363 4140 caballero
         *
364 312 fernando
         */
365 3666 fjp
        public Project readProject(File file) {
366 1219 vcaballero
                Project proj = null;
367
368
                try {
369
                        File xmlFile = new File(file.getAbsolutePath());
370
                        FileReader reader;
371
                        reader = new FileReader(xmlFile);
372 2183 fernando
                        XmlTag tag = (XmlTag) XmlTag.unmarshal(reader);
373 3167 caballero
                        XMLEntity xml=new XMLEntity(tag);
374
                        String VERSION=xml.getStringProperty("VERSION");
375 4140 caballero
376 2183 fernando
                        try {
377 3810 fjp
                                // if ((VERSION!=null) && (VERSION.equals("0.5") || VERSION.equals("0.4") || (VERSION.indexOf("GISPLANET") != -1))){
378
                                if (VERSION != null) {
379 3167 caballero
                                        proj = Project.createFromXML(xml);
380
                                }else{
381
                                        proj = Project.createFromXML03(new XMLEntity(tag));
382
                                }
383 2183 fernando
                                return proj;
384 3185 caballero
                        } catch (OpenException e){
385
                                e.showError();
386
                                //NotificationManager.addInfo("Al leer el proyecto", e);
387 3167 caballero
                        }
388 1219 vcaballero
                } catch (FileNotFoundException e) {
389 2695 nacho
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this, "fichero_incorrecto"));
390 2419 caballero
                        //NotificationManager.addError("Al leer el proyecto", e);
391 1219 vcaballero
                } catch (MarshalException e) {
392 2695 nacho
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this, "formato_incorrecto"));
393 2419 caballero
                        //NotificationManager.addError("Al leer el proyecto", e);
394 1219 vcaballero
                } catch (ValidationException e) {
395 2695 nacho
                        JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this, "formato_incorrecto"));
396 2419 caballero
                        //NotificationManager.addError("Al leer el proyecto", e);
397 4140 caballero
                }
398
399 2183 fernando
                return null;
400 1219 vcaballero
        }
401
402
        /**
403
         * Devuelve el proyecto.
404
         *
405
         * @return Proyecto.
406
         */
407 312 fernando
        public Project getProject() {
408
                return p;
409
        }
410
411 596 fernando
        /**
412 5005 jorpiell
         * @see com.iver.andami.plugins.IExtension#isEnabled()
413 596 fernando
         */
414
        public boolean isEnabled() {
415
                return true;
416
        }
417
418
        /**
419 5005 jorpiell
         * @see com.iver.andami.plugins.IExtension#isVisible()
420 596 fernando
         */
421
        public boolean isVisible() {
422
                return true;
423
        }
424 4140 caballero
425 2879 jaume
        /**
426 2922 jaume
         * Sets the project
427 2879 jaume
         * @param p
428
         */
429 2922 jaume
        public void setProject(Project p){
430 2879 jaume
                projectFrame.setProject(p);
431 2922 jaume
                this.p=p;
432 2879 jaume
        }
433 6190 caballero
434
        //Finalizar y preguntar si se quiere guardar el proyecto o cancelar.
435
        public void finalize() {
436
                int option = JOptionPane.showConfirmDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"guardar_cambios"),
437
                                PluginServices.getText(this,"guardar_proyecto"),
438
                                JOptionPane.YES_NO_OPTION);
439
440
                if (option == JOptionPane.YES_OPTION) {
441
                        if (!guardar())
442
                                finalize();
443
                }
444
        }
445 312 fernando
}