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