svn-gvsig-desktop / trunk / applications / appgvSIG / src / com / iver / cit / gvsig / ProjectExtension.java @ 8455
History | View | Annotate | Download (22.6 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.BufferedReader; |
45 |
import java.io.File; |
46 |
import java.io.FileInputStream; |
47 |
import java.io.FileNotFoundException; |
48 |
import java.io.FileOutputStream; |
49 |
import java.io.FileReader; |
50 |
import java.io.FileWriter; |
51 |
import java.io.IOException; |
52 |
import java.io.InputStream; |
53 |
import java.io.InputStreamReader; |
54 |
import java.io.OutputStreamWriter; |
55 |
import java.io.Reader; |
56 |
import java.io.UnsupportedEncodingException; |
57 |
import java.net.MalformedURLException; |
58 |
import java.net.URL; |
59 |
import java.text.DateFormat; |
60 |
import java.util.Date; |
61 |
import java.util.prefs.Preferences; |
62 |
|
63 |
import javax.swing.JFileChooser; |
64 |
import javax.swing.JOptionPane; |
65 |
|
66 |
import org.exolab.castor.xml.MarshalException; |
67 |
import org.exolab.castor.xml.Marshaller; |
68 |
import org.exolab.castor.xml.ValidationException; |
69 |
|
70 |
import com.iver.andami.Launcher; |
71 |
import com.iver.andami.PluginServices; |
72 |
import com.iver.andami.messages.Messages; |
73 |
import com.iver.andami.messages.NotificationManager; |
74 |
import com.iver.andami.plugins.Extension; |
75 |
import com.iver.andami.ui.mdiManager.IWindow; |
76 |
import com.iver.andami.ui.mdiManager.WindowInfo; |
77 |
import com.iver.cit.gvsig.fmap.layers.LayerFactory; |
78 |
import com.iver.cit.gvsig.gui.layout.Layout; |
79 |
import com.iver.cit.gvsig.project.Project; |
80 |
import com.iver.cit.gvsig.project.ProjectFactory; |
81 |
import com.iver.cit.gvsig.project.documents.exceptions.OpenException; |
82 |
import com.iver.cit.gvsig.project.documents.gui.ProjectWindow; |
83 |
import com.iver.cit.gvsig.project.documents.layout.ProjectMap; |
84 |
import com.iver.cit.gvsig.project.documents.layout.ProjectMapFactory; |
85 |
import com.iver.cit.gvsig.project.documents.table.ProjectTableFactory; |
86 |
import com.iver.cit.gvsig.project.documents.view.ProjectView; |
87 |
import com.iver.cit.gvsig.project.documents.view.ProjectViewFactory; |
88 |
import com.iver.utiles.GenericFileFilter; |
89 |
import com.iver.utiles.XMLEntity; |
90 |
import com.iver.utiles.extensionPoints.ExtensionPoint; |
91 |
import com.iver.utiles.extensionPoints.ExtensionPoints; |
92 |
import com.iver.utiles.extensionPoints.ExtensionPointsSingleton; |
93 |
import com.iver.utiles.xml.XMLEncodingUtils; |
94 |
import com.iver.utiles.xmlEntity.generate.XmlTag; |
95 |
|
96 |
|
97 |
/**
|
98 |
* Extension que proporciona controles para crear proyectos nuevos, abrirlos y
|
99 |
* guardarlos. Adem?s los tipos de tabla que soporta el proyecto son a?adidos
|
100 |
* en esta clase.
|
101 |
*
|
102 |
* @author Fernando Gonz?lez Cort?s
|
103 |
*/
|
104 |
public class ProjectExtension extends Extension { |
105 |
private static String projectPath = null; |
106 |
//private ProjectWindow projectFrame;
|
107 |
private ProjectWindow projectFrame;
|
108 |
private Project p;
|
109 |
private String lastPath; |
110 |
private String lastSavePath; |
111 |
private String templatesPath; |
112 |
private WindowInfo seedProjectWindow;
|
113 |
/**
|
114 |
* Use UTF-8 for encoding, as it can represent characters from
|
115 |
* any language.
|
116 |
*
|
117 |
* Another sensible option would be
|
118 |
* encoding = System.getProperty("file.encoding");
|
119 |
* but this would need some extra testing.
|
120 |
*/
|
121 |
private String projectEncoding = "UTF-8"; |
122 |
|
123 |
/**
|
124 |
* @see com.iver.mdiApp.plugins.IExtension#initialize()
|
125 |
*/
|
126 |
public void initialize() { |
127 |
try {
|
128 |
Class.forName("javax.media.jai.EnumeratedParameter"); |
129 |
} catch (ClassNotFoundException e) { |
130 |
NotificationManager.addError("La m?quina virtual con la que se ejecuta gvSIG no tiene JAI instalado", e);
|
131 |
} |
132 |
|
133 |
LayerFactory.setWritersPath(PluginServices.getPluginServices(this)
|
134 |
.getPluginDirectory() |
135 |
.getAbsolutePath() + |
136 |
File.separator + "drivers"); |
137 |
|
138 |
LayerFactory.setDriversPath(PluginServices.getPluginServices(this)
|
139 |
.getPluginDirectory() |
140 |
.getAbsolutePath() + |
141 |
File.separator + "drivers"); |
142 |
|
143 |
initializeDocumentActionsExtensionPoint(); |
144 |
registerDocuments(); |
145 |
|
146 |
// Recuperamos el ?ltimo argumento, que se supone
|
147 |
// que ser? el fichero .gvp que queremos abrir.
|
148 |
// (por enmedio pueden venir o no otros argumentos,
|
149 |
// por ejemplo el idioma)
|
150 |
// TODO: Aqu? Jaume podr?a meter lo del backup del proyecto
|
151 |
// que ha hecho para ValenciaUrban?stica
|
152 |
|
153 |
/*
|
154 |
pe = (ProjectExtension) PluginServices.getExtension(com.iver.cit.gvsig.ProjectExtension.class);
|
155 |
if (projectFile.length()!=0){
|
156 |
p=pe.readProject(projectFile);
|
157 |
}
|
158 |
else
|
159 |
p = restorePreviousProject();
|
160 |
pe.setProject(p); */
|
161 |
String[] theArgs = PluginServices.getArguments(); |
162 |
String lastArg = theArgs[theArgs.length-1]; |
163 |
if ((lastArg.endsWith(".gvp")) || |
164 |
(lastArg.endsWith(".GVP")))
|
165 |
{ |
166 |
PluginServices.getLogger().debug("Intentando cargar el proyecto " + lastArg);
|
167 |
//File projectFile = new File(lastArg);
|
168 |
p = readProject(lastArg); |
169 |
PluginServices.getMainFrame().setTitle(p.getName()); |
170 |
projectPath = lastArg; |
171 |
} |
172 |
else
|
173 |
{ |
174 |
p = ProjectFactory.createProject(); |
175 |
p.setName(PluginServices.getText(this, "untitled")); |
176 |
p.setModified(false);
|
177 |
PluginServices.getMainFrame().setTitle(PluginServices.getText(this, "sin_titulo")); |
178 |
} |
179 |
} |
180 |
/**
|
181 |
* @see com.iver.mdiApp.plugins.IExtension#postInitialize()
|
182 |
*/
|
183 |
public void postInitialize() { |
184 |
projectFrame = new ProjectWindow();
|
185 |
projectFrame.setProject(p); |
186 |
p.restoreWindowProperties(); |
187 |
} |
188 |
|
189 |
/**
|
190 |
* Muestra la ventana con el gestor de proyectos.
|
191 |
*/
|
192 |
public void showProjectWindow() { |
193 |
if (seedProjectWindow!=null) { |
194 |
if (seedProjectWindow.isClosed()) {
|
195 |
// if it was closed, we just don't open the window now
|
196 |
seedProjectWindow.setClosed(false);
|
197 |
return;
|
198 |
} |
199 |
WindowInfo winProps = seedProjectWindow; |
200 |
seedProjectWindow = null;
|
201 |
PluginServices.getMDIManager().addWindow(projectFrame); |
202 |
PluginServices.getMDIManager().changeWindowInfo(projectFrame, winProps); |
203 |
} |
204 |
else
|
205 |
PluginServices.getMDIManager().addWindow(projectFrame); |
206 |
} |
207 |
|
208 |
/**
|
209 |
* Muestra la ventana con el gestor de proyectos, con las propiedades
|
210 |
* de ventana especificadas.
|
211 |
*/
|
212 |
public void showProjectWindow(WindowInfo wi) { |
213 |
seedProjectWindow = wi; |
214 |
showProjectWindow(); |
215 |
} |
216 |
|
217 |
/**
|
218 |
* Guarda el proyecto actual en disco.
|
219 |
*/
|
220 |
private boolean guardar() { |
221 |
boolean saved=false; |
222 |
// if (p.getPath() == null) {
|
223 |
if (projectPath == null) { |
224 |
saved=guardarDialogo(); |
225 |
} else {
|
226 |
saved=writeProject(new File(projectPath), p); //new File(p.getPath()), p); |
227 |
projectFrame.refreshControls(); |
228 |
} |
229 |
return saved;
|
230 |
} |
231 |
|
232 |
private boolean guardarDialogo(){ |
233 |
boolean saved=false; |
234 |
|
235 |
if (lastSavePath == null) |
236 |
lastSavePath = projectPath; |
237 |
|
238 |
if (lastSavePath == null) { |
239 |
Preferences prefs = Preferences.userRoot().node( "gvsig.foldering" ); |
240 |
lastSavePath = prefs.get("ProjectsFolder", null); |
241 |
} |
242 |
|
243 |
JFileChooser jfc = new JFileChooser(lastSavePath); |
244 |
jfc.addChoosableFileFilter(new GenericFileFilter("gvp", |
245 |
PluginServices.getText(this, "tipo_fichero_proyecto"))); |
246 |
|
247 |
if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) { |
248 |
File file=jfc.getSelectedFile();
|
249 |
if (!(file.getPath().toLowerCase().endsWith(".gvp"))){ |
250 |
file=new File(file.getPath()+".gvp"); |
251 |
} |
252 |
saved=writeProject(file, p); |
253 |
String filePath = file.getAbsolutePath();
|
254 |
lastSavePath = filePath.substring(0, filePath.lastIndexOf(File.separatorChar)); |
255 |
|
256 |
projectFrame.refreshControls(); |
257 |
} |
258 |
return saved;
|
259 |
} |
260 |
/**
|
261 |
* Guarda si el proyecto ha sido modificado.
|
262 |
*
|
263 |
* @return True si se ha guardado correctamente.
|
264 |
*/
|
265 |
private boolean modificado() { |
266 |
if (p.isModified()) {
|
267 |
///if (true) {
|
268 |
//TODO de momento se queda como modificado siempre menos cuando est? totalmente vacio que se tomar? como no modificado,
|
269 |
//para poder controlar perfectamente cuando un proyecto ha sido modificado
|
270 |
//hay que recoger los eventos de cambio de leyenda, cambio de extent, a?adir una capa, etc que se encuentran en FMap.
|
271 |
|
272 |
int res = JOptionPane.showConfirmDialog((Component) PluginServices.getMainFrame(), |
273 |
PluginServices.getText(this, "guardar_cambios"), |
274 |
"gvSIG",
|
275 |
JOptionPane.YES_NO_CANCEL_OPTION,
|
276 |
JOptionPane.INFORMATION_MESSAGE);
|
277 |
|
278 |
if (res == JOptionPane.YES_OPTION) { |
279 |
guardar(); |
280 |
} else if (res == JOptionPane.CANCEL_OPTION) { |
281 |
return false; |
282 |
} |
283 |
} |
284 |
|
285 |
return true; |
286 |
} |
287 |
|
288 |
/**
|
289 |
* @see com.iver.mdiApp.plugins.IExtension#updateUI(java.lang.String)
|
290 |
*/
|
291 |
public void execute(String actionCommand) { |
292 |
if (actionCommand.equals("NUEVO")) { |
293 |
//Si est? modificado se pregunta si se quiere guardar el anterior
|
294 |
if (!modificado()) {
|
295 |
return;
|
296 |
} |
297 |
ProjectView.numViews=0;
|
298 |
ProjectMap.numMaps = 0;
|
299 |
PluginServices.getMDIManager().closeAllWindows(); |
300 |
p = ProjectFactory.createProject(); |
301 |
p.setName(PluginServices.getText(this, "untitled")); |
302 |
p.setModified(false);
|
303 |
projectFrame.setProject(p); |
304 |
showProjectWindow(); |
305 |
PluginServices.getMainFrame().setTitle(PluginServices.getText(this, "sin_titulo")); |
306 |
} else if (actionCommand.equals("ABRIR")) { |
307 |
//Si est? modificado se pregunta si se quiere guardar el anterior
|
308 |
if (!modificado()) {
|
309 |
ProjectView.numViews=0;
|
310 |
ProjectMap.numMaps = 0;
|
311 |
return;
|
312 |
} |
313 |
|
314 |
if (lastPath == null) { |
315 |
Preferences prefs = Preferences.userRoot().node( "gvsig.foldering" ); |
316 |
lastPath = prefs.get("ProjectsFolder", null); |
317 |
} |
318 |
|
319 |
JFileChooser jfc = new JFileChooser(lastPath); |
320 |
jfc.addChoosableFileFilter(new GenericFileFilter("gvp", |
321 |
PluginServices.getText(this, "tipo_fichero_proyecto"))); |
322 |
|
323 |
if (jfc.showOpenDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) { |
324 |
ProjectView.numViews=0;
|
325 |
ProjectMap.numMaps = 0;
|
326 |
PluginServices.getMDIManager().closeAllWindows(); |
327 |
|
328 |
File projectFile = jfc.getSelectedFile();
|
329 |
Project o = readProject(projectFile); |
330 |
setPath(projectFile.getAbsolutePath()); |
331 |
lastPath = getPath(); |
332 |
if (o != null) { |
333 |
p = o; |
334 |
} |
335 |
|
336 |
projectFrame.setProject(p); |
337 |
PluginServices.getMainFrame().setTitle(p.getName()); |
338 |
projectFrame.refreshControls(); |
339 |
p.restoreWindowProperties(); |
340 |
} |
341 |
} else if (actionCommand.equals("GUARDAR")) { |
342 |
guardar(); |
343 |
} else if (actionCommand.equals("GUARDAR_COMO")) { |
344 |
guardarDialogo(); |
345 |
} else if (actionCommand.equals("SALIR")){ |
346 |
int option = JOptionPane.showConfirmDialog((Component)PluginServices.getMainFrame(), |
347 |
Messages.getString("MDIFrame.quiere_salir"),
|
348 |
Messages.getString("MDIFrame.salir"),
|
349 |
JOptionPane.YES_NO_OPTION);
|
350 |
|
351 |
if (option == JOptionPane.YES_OPTION) { |
352 |
Launcher.closeApplication(); |
353 |
} |
354 |
} else if (actionCommand.compareTo("OPENTEMPLATE")==0){ |
355 |
openLayout(); |
356 |
} |
357 |
} |
358 |
public void openLayout() { |
359 |
//Project project = ((ProjectExtension) PluginServices.getExtension(ProjectExtension.class)).getProject();
|
360 |
Layout layout=null;
|
361 |
|
362 |
if (templatesPath == null) { |
363 |
Preferences prefs = Preferences.userRoot().node( "gvsig.foldering" ); |
364 |
templatesPath = prefs.get("TemplatesFolder", null); |
365 |
} |
366 |
|
367 |
JFileChooser jfc = new JFileChooser(templatesPath); |
368 |
jfc.addChoosableFileFilter(new GenericFileFilter("gvt", |
369 |
PluginServices.getText(this, "plantilla"))); |
370 |
|
371 |
if (jfc.showOpenDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) { |
372 |
File file=jfc.getSelectedFile();
|
373 |
if (!(file.getPath().endsWith(".gvt") || file.getPath().endsWith(".GVT"))){ |
374 |
file=new File(file.getPath()+".gvt"); |
375 |
} |
376 |
try {
|
377 |
File xmlFile = new File(file.getAbsolutePath()); |
378 |
FileReader reader;
|
379 |
reader = new FileReader(xmlFile); |
380 |
|
381 |
XmlTag tag = (XmlTag) XmlTag.unmarshal(reader); |
382 |
try {
|
383 |
layout = Layout.createLayout(new XMLEntity(tag),p);
|
384 |
} catch (OpenException e) {
|
385 |
e.showError(); |
386 |
} |
387 |
//fPanelLegendManager.setRenderer(LegendFactory.createFromXML(new XMLEntity(tag)));
|
388 |
} catch (FileNotFoundException e) { |
389 |
NotificationManager.addError(PluginServices.getText(this, "Al_leer_la_leyenda"), e); |
390 |
} catch (MarshalException e) { |
391 |
NotificationManager.addError(PluginServices.getText(this, "Al_leer_la_leyenda"), e); |
392 |
} catch (ValidationException e) {
|
393 |
NotificationManager.addError(PluginServices.getText(this, "Al_leer_la_leyenda"), e); |
394 |
} |
395 |
ProjectMap pmap = ProjectFactory.createMap(file.getName()); |
396 |
pmap.setModel(layout); |
397 |
pmap.getModel().setProjectMap(pmap); |
398 |
p.addDocument(pmap); |
399 |
PluginServices.getMDIManager().addWindow(layout); |
400 |
|
401 |
|
402 |
} |
403 |
} |
404 |
/**
|
405 |
* Escribe el proyecto en XML.
|
406 |
*
|
407 |
* @param file Fichero.
|
408 |
* @param p Proyecto.
|
409 |
*/
|
410 |
public boolean writeProject(File file, Project p) { |
411 |
if( file.exists()){
|
412 |
int resp = JOptionPane.showConfirmDialog( |
413 |
(Component) PluginServices.getMainFrame(),PluginServices.getText(this,"fichero_ya_existe_seguro_desea_guardarlo"), |
414 |
PluginServices.getText(this,"guardar"), JOptionPane.YES_NO_OPTION); |
415 |
if (resp != JOptionPane.YES_OPTION) { |
416 |
return false; |
417 |
} |
418 |
} |
419 |
// write it out as XML
|
420 |
try {
|
421 |
FileOutputStream fos = new FileOutputStream(file.getAbsolutePath()); |
422 |
OutputStreamWriter writer = new OutputStreamWriter(fos, projectEncoding); |
423 |
Marshaller m = new Marshaller(writer);
|
424 |
m.setEncoding(projectEncoding); |
425 |
p.setName(file.getName()); |
426 |
// p.setPath(file.toString());
|
427 |
p.setModificationDate(DateFormat.getDateInstance().format(new Date())); |
428 |
p.setModified(false);
|
429 |
XMLEntity xml = p.getXMLEntity(); |
430 |
xml.putProperty("followHeaderEncoding", true); |
431 |
m.marshal(xml.getXmlTag()); |
432 |
PluginServices.getMainFrame().setTitle(file.getName()); |
433 |
setPath(file.toString()); |
434 |
|
435 |
} catch (Exception e) { |
436 |
NotificationManager.addError("Error guardando el proyecto", e);
|
437 |
return false; |
438 |
} |
439 |
return true; |
440 |
} |
441 |
|
442 |
public Project readProject(String path) { |
443 |
BufferedReader reader =null; |
444 |
try {
|
445 |
URL url=null; |
446 |
url = new URL(path); |
447 |
String encoding = XMLEncodingUtils.getEncoding(url.openStream());
|
448 |
InputStream is = url.openStream();
|
449 |
if (encoding!=null) { |
450 |
Project proj=null;
|
451 |
try {
|
452 |
reader = new BufferedReader(new InputStreamReader(is, encoding)); |
453 |
proj=readProject(reader, true);
|
454 |
} catch (UnsupportedEncodingException e) { |
455 |
reader = new BufferedReader(new InputStreamReader(is)); |
456 |
try {
|
457 |
Project p=readProject(reader, false);
|
458 |
} catch (UnsupportedEncodingException e1) { |
459 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this, e1.getLocalizedMessage())); |
460 |
return null; |
461 |
} |
462 |
} |
463 |
ProjectExtension.setPath(path); |
464 |
return proj;
|
465 |
} |
466 |
else {
|
467 |
reader = new BufferedReader(new InputStreamReader(is)); |
468 |
Project p; |
469 |
try {
|
470 |
p = readProject(reader, false);
|
471 |
} catch (UnsupportedEncodingException e) { |
472 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this, e.getLocalizedMessage())); |
473 |
return null; |
474 |
} |
475 |
ProjectExtension.setPath(path); //p.setPath(null);
|
476 |
return p;
|
477 |
} |
478 |
} catch (MalformedURLException e) { |
479 |
File file=new File(path); |
480 |
return readProject(file);
|
481 |
} catch (IOException e) { |
482 |
e.printStackTrace(); |
483 |
return null; |
484 |
} |
485 |
} |
486 |
|
487 |
/**
|
488 |
* Lee del XML el proyecto.<br><br>
|
489 |
*
|
490 |
* Reads the XML of the project.<br>
|
491 |
* It returns a project object holding all needed info that is not linked to the Project Dialog. <br>In case you want the project to be
|
492 |
* linked to the window you must set this object to the extension:<br>
|
493 |
*
|
494 |
* <b>Example:</b><br>
|
495 |
*
|
496 |
* ...<br>
|
497 |
* ...<br>
|
498 |
* Project p = ProjectExtension.readProject(projectFile);<br>
|
499 |
* ProjectExtension.setProject(p);
|
500 |
* ...<br>
|
501 |
* ...<br>
|
502 |
* @param file Fichero.
|
503 |
*
|
504 |
* @return Project
|
505 |
*
|
506 |
*/
|
507 |
public Project readProject(File file) { |
508 |
File xmlFile = new File(file.getAbsolutePath()); |
509 |
try {
|
510 |
String encoding = XMLEncodingUtils.getEncoding(new FileInputStream(xmlFile)); |
511 |
InputStreamReader reader=null; |
512 |
if (encoding!=null) { |
513 |
try {
|
514 |
reader = new InputStreamReader(new FileInputStream(xmlFile), encoding); |
515 |
return readProject(reader, true); |
516 |
} catch (UnsupportedEncodingException e) { |
517 |
reader = new InputStreamReader(new FileInputStream(xmlFile)); |
518 |
try {
|
519 |
return readProject(reader, false); |
520 |
} catch (UnsupportedEncodingException e1) { |
521 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this, e1.getLocalizedMessage())); |
522 |
return null; |
523 |
} |
524 |
} |
525 |
} |
526 |
else {
|
527 |
reader = new InputStreamReader(new FileInputStream(xmlFile)); |
528 |
try {
|
529 |
return readProject(reader, false); |
530 |
} catch (UnsupportedEncodingException e1) { |
531 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this, e1.getLocalizedMessage())); |
532 |
return null; |
533 |
} |
534 |
} |
535 |
} catch (FileNotFoundException e) { |
536 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this, "fichero_incorrecto")); |
537 |
return null; |
538 |
} |
539 |
} |
540 |
|
541 |
/**
|
542 |
* Lee del XML el proyecto.<br><br>
|
543 |
*
|
544 |
* Reads the XML of the project.<br>
|
545 |
* It returns a project object holding all needed info that is not linked to the Project Dialog. <br>In case you want the project to be
|
546 |
* linked to the window you must set this object to the extension:<br>
|
547 |
*
|
548 |
* <b>Example:</b><br>
|
549 |
*
|
550 |
* ...<br>
|
551 |
* ...<br>
|
552 |
* Project p = ProjectExtension.readProject(projectFile);<br>
|
553 |
* ProjectExtension.setProject(p);
|
554 |
* ...<br>
|
555 |
* ...<br>
|
556 |
* @param reader File reader
|
557 |
* @param encodingFollowed Whether the encoded specified in the xml header was followed
|
558 |
* when creating the reader. If the property followHeaderEncoding is false or not set,
|
559 |
* then the encoding should have not been used when creating the reader, so it must be
|
560 |
* recreated.
|
561 |
*
|
562 |
* @return Project
|
563 |
* @throws UnsupportedEncodingException
|
564 |
*
|
565 |
*/
|
566 |
private Project readProject(Reader reader, boolean encodingFollowed) throws UnsupportedEncodingException { |
567 |
Project proj = null;
|
568 |
|
569 |
try {
|
570 |
XmlTag tag = (XmlTag) XmlTag.unmarshal(reader); |
571 |
XMLEntity xml=new XMLEntity(tag);
|
572 |
String VERSION=xml.getStringProperty("VERSION"); |
573 |
|
574 |
if (encodingFollowed) {
|
575 |
if (xml.contains("followHeaderEncoding")) { |
576 |
boolean useEncoding = xml.getBooleanProperty("followHeaderEncoding"); |
577 |
if (!useEncoding) {
|
578 |
throw new UnsupportedEncodingException("the encoding specified in the xml header is not safe"); |
579 |
} |
580 |
} |
581 |
else {
|
582 |
// Old projects didn't contain followHeaderEncoding and they were
|
583 |
// not correctly encoded. We throw an exception now, and we'll try
|
584 |
// to reopen the project
|
585 |
// using the default system encoding.
|
586 |
throw new UnsupportedEncodingException("the encoding specified in the xml header is not safe"); |
587 |
} |
588 |
} |
589 |
|
590 |
try {
|
591 |
// if ((VERSION!=null) && (VERSION.equals("0.5") || VERSION.equals("0.4") || (VERSION.indexOf("GISPLANET") != -1))){
|
592 |
if (VERSION != null) { |
593 |
proj = Project.createFromXML(xml); |
594 |
}else{
|
595 |
proj = Project.createFromXML03(new XMLEntity(tag));
|
596 |
} |
597 |
return proj;
|
598 |
} catch (OpenException e){
|
599 |
e.showError(); |
600 |
//NotificationManager.addInfo("Al leer el proyecto", e);
|
601 |
} |
602 |
} catch (MarshalException e) { |
603 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this, "formato_incorrecto")); |
604 |
//NotificationManager.addError("Al leer el proyecto", e);
|
605 |
} catch (ValidationException e) {
|
606 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this, "formato_incorrecto")); |
607 |
//NotificationManager.addError("Al leer el proyecto", e);
|
608 |
} |
609 |
return null; |
610 |
} |
611 |
|
612 |
/**
|
613 |
* Devuelve el proyecto.
|
614 |
*
|
615 |
* @return Proyecto.
|
616 |
*/
|
617 |
public Project getProject() {
|
618 |
return p;
|
619 |
} |
620 |
|
621 |
/**
|
622 |
* @see com.iver.andami.plugins.IExtension#isEnabled()
|
623 |
*/
|
624 |
public boolean isEnabled() { |
625 |
return true; |
626 |
} |
627 |
|
628 |
/**
|
629 |
* @see com.iver.andami.plugins.IExtension#isVisible()
|
630 |
*/
|
631 |
public boolean isVisible() { |
632 |
return true; |
633 |
} |
634 |
|
635 |
/**
|
636 |
* Sets the project
|
637 |
* @param p
|
638 |
*/
|
639 |
public void setProject(Project p){ |
640 |
projectFrame.setProject(p); |
641 |
this.p=p;
|
642 |
} |
643 |
|
644 |
//Finalizar y preguntar si se quiere guardar el proyecto o cancelar.
|
645 |
public void terminate() { |
646 |
long t1,t2;
|
647 |
t1 = System.currentTimeMillis();
|
648 |
//if (getProject().hasChanged()) {
|
649 |
int option = JOptionPane.showConfirmDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"guardar_cambios"), |
650 |
PluginServices.getText(this,"guardar_proyecto"), |
651 |
JOptionPane.YES_NO_OPTION);
|
652 |
|
653 |
if (option == JOptionPane.YES_OPTION) { |
654 |
if (!guardar())
|
655 |
terminate(); |
656 |
} |
657 |
//}
|
658 |
t2 = System.currentTimeMillis();
|
659 |
System.err.println("ProjectExtension.terminate() " + (t2-t1) + "milisecs"); |
660 |
} |
661 |
|
662 |
private void registerDocuments() { |
663 |
ProjectViewFactory.register(); |
664 |
ProjectTableFactory.register(); |
665 |
ProjectMapFactory.register(); |
666 |
} |
667 |
private void initializeDocumentActionsExtensionPoint() { |
668 |
ExtensionPoints extensionPoints = ExtensionPointsSingleton.getInstance(); |
669 |
if (!extensionPoints.containsKey("DocumentActions_View")) { |
670 |
extensionPoints.put( |
671 |
new ExtensionPoint(
|
672 |
"DocumentActions_View",
|
673 |
"Context menu options of the view document list" +
|
674 |
" in the project window "+
|
675 |
"(register instances of " +
|
676 |
"com.iver.cit.gvsig.project.AbstractDocumentContextMenuAction)"
|
677 |
) |
678 |
); |
679 |
} |
680 |
if (!extensionPoints.containsKey("DocumentActions_Table")) { |
681 |
extensionPoints.put( |
682 |
new ExtensionPoint(
|
683 |
"DocumentActions_Table",
|
684 |
"Context menu options of the table document list" +
|
685 |
" in the project window "+
|
686 |
"(register instances of " +
|
687 |
"com.iver.cit.gvsig.project.AbstractDocumentContextMenuAction)"
|
688 |
) |
689 |
); |
690 |
} |
691 |
if (!extensionPoints.containsKey("DocumentActions_Map")) { |
692 |
extensionPoints.put( |
693 |
new ExtensionPoint(
|
694 |
"DocumentActions_Map",
|
695 |
"Context menu options of the map document list" +
|
696 |
" in the project window "+
|
697 |
"(register instances of " +
|
698 |
"com.iver.cit.gvsig.project.AbstractDocumentContextMenuAction)"
|
699 |
) |
700 |
); |
701 |
} |
702 |
} |
703 |
|
704 |
public static String getPath() { |
705 |
return projectPath;
|
706 |
} |
707 |
|
708 |
public static void setPath(String path) { |
709 |
projectPath = path; |
710 |
} |
711 |
|
712 |
public IWindow getProjectWindow() {
|
713 |
return projectFrame;
|
714 |
} |
715 |
} |