svn-gvsig-desktop / tags / Root_v061 / applications / appgvSIG / src / com / iver / cit / gvsig / ProjectExtension.java @ 4812
History | View | Annotate | Download (11.7 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 | 596 | fernando | public class ProjectExtension implements Extension { |
84 | 1219 | vcaballero | private ProjectWindow projectFrame;
|
85 | private Project p;
|
||
86 | /**
|
||
87 | * @see com.iver.mdiApp.plugins.Extension#inicializar()
|
||
88 | */
|
||
89 | public void inicializar() { |
||
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 | |||
96 | 1219 | vcaballero | 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 | 1412 | fernando | PluginServices.getMainFrame().setTitle(PluginServices.getText(this, "sin_titulo")); |
103 | 312 | fernando | |
104 | 1219 | vcaballero | 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 | 596 | fernando | PluginServices.getMDIManager().addView(projectFrame); |
115 | 312 | fernando | } |
116 | |||
117 | 1219 | vcaballero | /**
|
118 | * Guarda el proyecto actual en disco.
|
||
119 | */
|
||
120 | private void guardar() { |
||
121 | 312 | fernando | if (p.getPath() == null) { |
122 | 1226 | vcaballero | guardarDialogo(); |
123 | 312 | fernando | } else {
|
124 | escribirProyecto(new File(p.getPath()), p); |
||
125 | 1837 | fernando | projectFrame.refreshControls(); |
126 | 312 | fernando | } |
127 | } |
||
128 | 1226 | vcaballero | private void guardarDialogo(){ |
129 | JFileChooser jfc = new JFileChooser(); |
||
130 | 1271 | vcaballero | jfc.addChoosableFileFilter(new GenericFileFilter("gvp", |
131 | 1226 | vcaballero | PluginServices.getText(this, "tipo_fichero_proyecto"))); |
132 | 1219 | vcaballero | |
133 | 1226 | vcaballero | if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) { |
134 | 1254 | vcaballero | File file=jfc.getSelectedFile();
|
135 | 1271 | vcaballero | if (!(file.getPath().endsWith(".gvp") || file.getPath().endsWith(".GVP"))){ |
136 | file=new File(file.getPath()+".gvp"); |
||
137 | 1254 | vcaballero | } |
138 | escribirProyecto(file, p); |
||
139 | 1837 | fernando | projectFrame.refreshControls(); |
140 | 1226 | vcaballero | } |
141 | } |
||
142 | 1219 | vcaballero | /**
|
143 | * Guarda si el proyecto ha sido modificado.
|
||
144 | *
|
||
145 | * @return True si se ha guardado correctamente.
|
||
146 | */
|
||
147 | private boolean modificado() { |
||
148 | 2785 | caballero | ///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 | 596 | fernando | int res = JOptionPane.showConfirmDialog((Component) PluginServices.getMainFrame(), |
153 | PluginServices.getText(this, "guardar_cambios"), |
||
154 | 2419 | caballero | "gvSIG",
|
155 | 312 | fernando | 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 | 1219 | vcaballero | |
165 | 312 | fernando | return true; |
166 | } |
||
167 | |||
168 | 1219 | vcaballero | /**
|
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 | 2609 | caballero | ProjectView.numViews=0;
|
178 | 2708 | fjp | ProjectMap.numMaps = 0;
|
179 | 1219 | vcaballero | 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 | 1412 | fernando | PluginServices.getMainFrame().setTitle(PluginServices.getText(this, "sin_titulo")); |
186 | 1219 | vcaballero | } else if (actionCommand.equals("ABRIR")) { |
187 | //Si est? modificado se pregunta si se quiere guardar el anterior
|
||
188 | if (!modificado()) {
|
||
189 | 2609 | caballero | ProjectView.numViews=0;
|
190 | 2708 | fjp | ProjectMap.numMaps = 0;
|
191 | 1219 | vcaballero | return;
|
192 | } |
||
193 | 2609 | caballero | |
194 | 1219 | vcaballero | JFileChooser jfc = new JFileChooser(); |
195 | 1271 | vcaballero | jfc.addChoosableFileFilter(new GenericFileFilter("gvp", |
196 | 1219 | vcaballero | PluginServices.getText(this, "tipo_fichero_proyecto"))); |
197 | |||
198 | if (jfc.showOpenDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) { |
||
199 | 2609 | caballero | ProjectView.numViews=0;
|
200 | 2708 | fjp | ProjectMap.numMaps = 0;
|
201 | 596 | fernando | PluginServices.getMDIManager().closeAllViews(); |
202 | 1219 | vcaballero | Project o = leerProyecto(jfc.getSelectedFile()); |
203 | if (o != null) { |
||
204 | p = o; |
||
205 | 596 | fernando | } |
206 | 312 | fernando | |
207 | 1219 | vcaballero | projectFrame.setProject(p); |
208 | 1412 | fernando | PluginServices.getMainFrame().setTitle(p.getName()); |
209 | 1219 | vcaballero | projectFrame.refreshControls(); |
210 | showProjectWindow(); |
||
211 | } |
||
212 | } else if (actionCommand.equals("GUARDAR")) { |
||
213 | guardar(); |
||
214 | 1226 | vcaballero | } else if (actionCommand.equals("GUARDAR_COMO")) { |
215 | guardarDialogo(); |
||
216 | 2242 | jaume | } 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 | 3115 | caballero | } else if (actionCommand.compareTo("OPENTEMPLATE")==0){ |
226 | openLayout(); |
||
227 | 1219 | vcaballero | } |
228 | } |
||
229 | 3115 | caballero | public void openLayout() { |
230 | 3135 | caballero | //Project project = ((ProjectExtension) PluginServices.getExtension(ProjectExtension.class)).getProject();
|
231 | 3115 | caballero | Layout layout=null;
|
232 | JFileChooser jfc = new JFileChooser(); |
||
233 | jfc.addChoosableFileFilter(new GenericFileFilter("gvt", |
||
234 | PluginServices.getText(this, "plantilla"))); |
||
235 | 312 | fernando | |
236 | 3115 | caballero | 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 | 3185 | caballero | try {
|
248 | layout = Layout.createLayout(new XMLEntity(tag),p);
|
||
249 | } catch (OpenException e) {
|
||
250 | e.showError(); |
||
251 | } |
||
252 | 3115 | caballero | //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 | 3135 | caballero | |
264 | p.addMap(pmap); |
||
265 | 3115 | caballero | PluginServices.getMDIManager().addView(layout); |
266 | |||
267 | |||
268 | } |
||
269 | } |
||
270 | 1219 | vcaballero | /**
|
271 | * Escribe el proyecto en XML.
|
||
272 | *
|
||
273 | * @param file Fichero.
|
||
274 | * @param p Proyecto.
|
||
275 | */
|
||
276 | 1837 | fernando | public void escribirProyecto(File file, Project p) { |
277 | 1219 | vcaballero | // write it out as XML
|
278 | 596 | fernando | try {
|
279 | 1219 | vcaballero | FileWriter writer = new FileWriter(file.getAbsolutePath()); |
280 | Marshaller m = new Marshaller(writer);
|
||
281 | m.setEncoding("ISO-8859-1");
|
||
282 | 1254 | vcaballero | p.setName(file.getName()); |
283 | 1219 | vcaballero | p.setPath(file.toString()); |
284 | p.setModificationDate(DateFormat.getDateInstance().format(new Date())); |
||
285 | p.setModified(false);
|
||
286 | 1254 | vcaballero | m.marshal(p.getXMLEntity().getXmlTag()); |
287 | 1412 | fernando | PluginServices.getMainFrame().setTitle(file.getName()); |
288 | 1219 | vcaballero | } catch (Exception e) { |
289 | NotificationManager.addError("Error guardando el proyecto", e);
|
||
290 | } |
||
291 | 312 | fernando | |
292 | 1219 | vcaballero | } |
293 | |||
294 | /**
|
||
295 | 2922 | jaume | * 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 | 1219 | vcaballero | * @param file Fichero.
|
310 | *
|
||
311 | 2922 | jaume | * @return Project
|
312 | *
|
||
313 | 312 | fernando | */
|
314 | 1219 | vcaballero | 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 | 2183 | fernando | XmlTag tag = (XmlTag) XmlTag.unmarshal(reader); |
322 | 3167 | caballero | XMLEntity xml=new XMLEntity(tag);
|
323 | String VERSION=xml.getStringProperty("VERSION"); |
||
324 | |||
325 | 2183 | fernando | try {
|
326 | 3863 | luisw2 | // if ((VERSION!=null) && (VERSION.equals("0.5") || VERSION.equals("0.4") || (VERSION.indexOf("GISPLANET") != -1))){
|
327 | if (VERSION != null) { |
||
328 | 3167 | caballero | proj = Project.createFromXML(xml); |
329 | }else{
|
||
330 | proj = Project.createFromXML03(new XMLEntity(tag));
|
||
331 | } |
||
332 | 2183 | fernando | return proj;
|
333 | 3185 | caballero | } catch (OpenException e){
|
334 | e.showError(); |
||
335 | //NotificationManager.addInfo("Al leer el proyecto", e);
|
||
336 | 3167 | caballero | } |
337 | 1219 | vcaballero | } catch (FileNotFoundException e) { |
338 | 2695 | nacho | JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this, "fichero_incorrecto")); |
339 | 2419 | caballero | //NotificationManager.addError("Al leer el proyecto", e);
|
340 | 1219 | vcaballero | } catch (MarshalException e) { |
341 | 2695 | nacho | JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this, "formato_incorrecto")); |
342 | 2419 | caballero | //NotificationManager.addError("Al leer el proyecto", e);
|
343 | 1219 | vcaballero | } catch (ValidationException e) {
|
344 | 2695 | nacho | JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this, "formato_incorrecto")); |
345 | 2419 | caballero | //NotificationManager.addError("Al leer el proyecto", e);
|
346 | } |
||
347 | 2183 | fernando | |
348 | return null; |
||
349 | 1219 | vcaballero | } |
350 | |||
351 | /**
|
||
352 | * Devuelve el proyecto.
|
||
353 | *
|
||
354 | * @return Proyecto.
|
||
355 | */
|
||
356 | 312 | fernando | public Project getProject() {
|
357 | return p;
|
||
358 | } |
||
359 | |||
360 | 596 | fernando | /**
|
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 | 2879 | jaume | |
374 | /**
|
||
375 | 2922 | jaume | * Sets the project
|
376 | 2879 | jaume | * @param p
|
377 | */
|
||
378 | 2922 | jaume | public void setProject(Project p){ |
379 | 2879 | jaume | projectFrame.setProject(p); |
380 | 2922 | jaume | this.p=p;
|
381 | 2879 | jaume | } |
382 | 312 | fernando | } |