svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / extension / ProjectExtension.java @ 43293
History | View | Annotate | Download (25.5 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.app.extension; |
25 |
|
26 |
import java.awt.Component; |
27 |
import java.io.File; |
28 |
import java.text.MessageFormat; |
29 |
import java.util.ArrayList; |
30 |
import java.util.Iterator; |
31 |
import java.util.List; |
32 |
import java.util.Set; |
33 |
import java.util.prefs.Preferences; |
34 |
|
35 |
import javax.swing.JOptionPane; |
36 |
import javax.swing.SwingUtilities; |
37 |
|
38 |
import org.slf4j.Logger; |
39 |
import org.slf4j.LoggerFactory; |
40 |
|
41 |
import org.gvsig.tools.util.ArrayUtils; |
42 |
|
43 |
import org.apache.commons.lang.StringUtils; |
44 |
|
45 |
import org.gvsig.andami.IconThemeHelper; |
46 |
import org.gvsig.andami.Launcher; |
47 |
import org.gvsig.andami.Launcher.TerminationProcess; |
48 |
import org.gvsig.andami.PluginServices; |
49 |
import org.gvsig.andami.PluginsLocator; |
50 |
import org.gvsig.andami.actioninfo.ActionInfo; |
51 |
import org.gvsig.andami.actioninfo.ActionInfoManager; |
52 |
import org.gvsig.andami.messages.NotificationManager; |
53 |
import org.gvsig.andami.plugins.Extension; |
54 |
import org.gvsig.andami.plugins.IExtension; |
55 |
import org.gvsig.andami.plugins.status.IExtensionStatus; |
56 |
import org.gvsig.andami.plugins.status.IUnsavedData; |
57 |
import org.gvsig.andami.plugins.status.UnsavedData; |
58 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
59 |
import org.gvsig.andami.ui.mdiManager.WindowInfo; |
60 |
import org.gvsig.andami.ui.wizard.UnsavedDataPanel; |
61 |
import org.gvsig.app.ApplicationLocator; |
62 |
import org.gvsig.app.ApplicationManager; |
63 |
import org.gvsig.app.project.Project; |
64 |
import org.gvsig.app.project.ProjectManager; |
65 |
import org.gvsig.app.project.documents.gui.ProjectWindow; |
66 |
import org.gvsig.app.project.documents.view.ViewManager; |
67 |
import org.gvsig.gui.beans.swing.JFileChooser; |
68 |
import org.gvsig.tools.ToolsLocator; |
69 |
import org.gvsig.tools.dataTypes.DataTypes; |
70 |
import org.gvsig.tools.extensionpoint.ExtensionPointManager; |
71 |
import org.gvsig.tools.i18n.I18nManager; |
72 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
73 |
import org.gvsig.utils.GenericFileFilter; |
74 |
import org.gvsig.utils.save.AfterSavingListener; |
75 |
import org.gvsig.utils.save.BeforeSavingListener; |
76 |
import org.gvsig.utils.save.SaveEvent; |
77 |
import org.gvsig.utils.swing.threads.IMonitorableTask; |
78 |
|
79 |
|
80 |
/**
|
81 |
* Extension que proporciona controles para crear proyectos nuevos, abrirlos y
|
82 |
* guardarlos. Adem?s los tipos de tabla que soporta el proyecto son a?adidos en
|
83 |
* esta clase.
|
84 |
*
|
85 |
* @author Fernando Gonz?lez Cort?s
|
86 |
*/
|
87 |
public class ProjectExtension extends Extension implements IExtensionStatus { |
88 |
private static final Logger LOG = LoggerFactory |
89 |
.getLogger(ProjectExtension.class); |
90 |
|
91 |
private static String projectPath = null; |
92 |
private ProjectWindow projectFrame;
|
93 |
private Project p;
|
94 |
private String lastSavePath; |
95 |
private WindowInfo seedProjectWindow;
|
96 |
public static final String PROJECT_FILE_CHOOSER_ID = "PROJECT_FILECHOOSER_ID"; |
97 |
/**
|
98 |
* Use UTF-8 for encoding, as it can represent characters from any language.
|
99 |
*
|
100 |
* Another sensible option would be encoding =
|
101 |
* System.getProperty("file.encoding"); but this would need some extra
|
102 |
* testing.
|
103 |
*
|
104 |
* @deprecated see PersistentManager
|
105 |
*/
|
106 |
public static String PROJECTENCODING = "UTF-8"; |
107 |
|
108 |
private List<BeforeSavingListener> beforeSavingListeners = new ArrayList<BeforeSavingListener>(); |
109 |
|
110 |
private List<AfterSavingListener> afterSavingListeners = new ArrayList<AfterSavingListener>(); |
111 |
|
112 |
public void initialize() { |
113 |
initializeDocumentActionsExtensionPoint(); |
114 |
registerDocuments(); |
115 |
registerIcons(); |
116 |
|
117 |
File projectFile = getProjectFileFromArguments();
|
118 |
if( projectFile!=null ) { |
119 |
// Posponemos la apertura del proyecto ya que en este momento
|
120 |
// puede que no este inicializado algun plugin que precise el
|
121 |
// proyecto para poderse cargar.
|
122 |
PluginsLocator.getManager().addStartupTask( |
123 |
"Open project",
|
124 |
new OpenInitialProjectTask(projectFile), true, 1000); |
125 |
} |
126 |
} |
127 |
|
128 |
private void registerIcons() { |
129 |
IconThemeHelper.registerIcon("action", "application-project-new", this); |
130 |
IconThemeHelper |
131 |
.registerIcon("action", "application-project-open", this); |
132 |
IconThemeHelper |
133 |
.registerIcon("action", "application-project-save", this); |
134 |
IconThemeHelper.registerIcon("action", "application-project-save-as", |
135 |
this);
|
136 |
|
137 |
IconThemeHelper.registerIcon("project", "project-icon", this); |
138 |
} |
139 |
|
140 |
/**
|
141 |
* Returns the file to be opened or null if no parameter
|
142 |
* or file does not exist
|
143 |
*
|
144 |
* @return
|
145 |
*/
|
146 |
private File getProjectFileFromArguments() { |
147 |
String[] theArgs = PluginServices.getArguments(); |
148 |
if( theArgs.length< 3 ) { |
149 |
// application-name and extensions-folder are fixed arguments
|
150 |
return null; |
151 |
} |
152 |
String lastArg = theArgs[theArgs.length - 1]; |
153 |
if ( StringUtils.isEmpty(lastArg) ) {
|
154 |
return null; |
155 |
} |
156 |
if( lastArg.startsWith("-") ) { |
157 |
// Args starts with "-" are flags
|
158 |
return null; |
159 |
} |
160 |
if (!lastArg.toLowerCase().endsWith(Project.FILE_EXTENSION.toLowerCase())) {
|
161 |
LOG.info("Do not open project file, does not have the expected extension '" +
|
162 |
Project.FILE_EXTENSION +"' ("+lastArg+")."); |
163 |
return null; |
164 |
} |
165 |
File projectFile = new File(lastArg); |
166 |
if ( !projectFile.exists()) {
|
167 |
LOG.info("Do not open project file, '" +projectFile.getAbsolutePath() + "' do not exist."); |
168 |
return null; |
169 |
} |
170 |
return projectFile;
|
171 |
} |
172 |
|
173 |
private class OpenInitialProjectTask implements Runnable { |
174 |
private File projectFile; |
175 |
public OpenInitialProjectTask(File projectFile) { |
176 |
this.projectFile = projectFile;
|
177 |
} |
178 |
public void run() { |
179 |
if (this.projectFile == null) { |
180 |
return;
|
181 |
} |
182 |
ActionInfoManager actionManager = PluginsLocator.getActionInfoManager(); |
183 |
ActionInfo action = actionManager.getAction("application-project-open");
|
184 |
action.execute(this.projectFile);
|
185 |
} |
186 |
} |
187 |
|
188 |
public ProjectWindow getProjectFrame() {
|
189 |
if (projectFrame == null) { |
190 |
projectFrame = new ProjectWindow();
|
191 |
} |
192 |
return projectFrame;
|
193 |
} |
194 |
|
195 |
/**
|
196 |
* Muestra la ventana con el gestor de proyectos.
|
197 |
*/
|
198 |
public void showProjectWindow() { |
199 |
if (seedProjectWindow != null) { |
200 |
if (seedProjectWindow.isClosed()) {
|
201 |
// if it was closed, we just don't open the window now
|
202 |
seedProjectWindow.setClosed(false);
|
203 |
return;
|
204 |
} |
205 |
WindowInfo winProps = seedProjectWindow; |
206 |
seedProjectWindow = null;
|
207 |
PluginServices.getMDIManager().addWindow(getProjectFrame()); |
208 |
PluginServices.getMDIManager().changeWindowInfo(getProjectFrame(), |
209 |
winProps); |
210 |
} else {
|
211 |
PluginServices.getMDIManager().addWindow(getProjectFrame()); |
212 |
} |
213 |
} |
214 |
|
215 |
/**
|
216 |
* Muestra la ventana con el gestor de proyectos, con las propiedades de
|
217 |
* ventana especificadas.
|
218 |
*/
|
219 |
public void showProjectWindow(WindowInfo wi) { |
220 |
seedProjectWindow = wi; |
221 |
showProjectWindow(); |
222 |
} |
223 |
|
224 |
/**
|
225 |
* Guarda el proyecto actual en disco.
|
226 |
*/
|
227 |
private boolean saveProject() { |
228 |
boolean saved = false; |
229 |
// if (p.getPath() == null) {
|
230 |
if (projectPath == null) { |
231 |
saved = saveAsProject(null);
|
232 |
} else {
|
233 |
long t1, t2;
|
234 |
t1 = System.currentTimeMillis();
|
235 |
saved = writeProject(new File(projectPath), p, false); |
236 |
t2 = System.currentTimeMillis();
|
237 |
PluginServices.getLogger().info( |
238 |
"Project saved. " + (t2 - t1) + " miliseconds"); |
239 |
getProjectFrame().refreshControls(); |
240 |
} |
241 |
return saved;
|
242 |
} |
243 |
|
244 |
private boolean saveAsProject(File file) { |
245 |
boolean saved = false; |
246 |
|
247 |
if (lastSavePath == null) { |
248 |
lastSavePath = projectPath; |
249 |
} |
250 |
|
251 |
if (file == null) { |
252 |
Preferences prefs = Preferences.userRoot().node("gvsig.foldering"); |
253 |
JFileChooser jfc = new JFileChooser(PROJECT_FILE_CHOOSER_ID, |
254 |
prefs.get("ProjectsFolder", null)); |
255 |
|
256 |
jfc.setDialogTitle(PluginServices.getText(this, "guardar_proyecto")); |
257 |
|
258 |
GenericFileFilter projExtensionFilter = new GenericFileFilter(
|
259 |
Project.FILE_EXTENSION, MessageFormat.format(PluginServices
|
260 |
.getText(this, "tipo_fichero_proyecto"), |
261 |
Project.FILE_EXTENSION)); |
262 |
jfc.addChoosableFileFilter(projExtensionFilter); |
263 |
jfc.setFileFilter(projExtensionFilter); |
264 |
|
265 |
if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) != JFileChooser.APPROVE_OPTION) { |
266 |
return saved;
|
267 |
} |
268 |
file = jfc.getSelectedFile(); |
269 |
} |
270 |
|
271 |
if (!(file.getPath().toLowerCase().endsWith(Project.FILE_EXTENSION
|
272 |
.toLowerCase()))) { |
273 |
file = new File(file.getPath() + Project.FILE_EXTENSION); |
274 |
} |
275 |
saved = writeProject(file, p); |
276 |
String filePath = file.getAbsolutePath();
|
277 |
lastSavePath = filePath.substring(0,
|
278 |
filePath.lastIndexOf(File.separatorChar));
|
279 |
|
280 |
getProjectFrame().refreshControls(); |
281 |
return saved;
|
282 |
} |
283 |
|
284 |
/**
|
285 |
* Checks whether the project and related unsaved data is modified, and
|
286 |
* allows the user to save it.
|
287 |
*
|
288 |
* @return true if the data has been correctly saved, false otherwise
|
289 |
*/
|
290 |
private boolean askSave() { |
291 |
if (p != null && p.hasChanged()) { |
292 |
TerminationProcess process = Launcher.getTerminationProcess(); |
293 |
UnsavedDataPanel panel = process.getUnsavedDataPanel(); |
294 |
panel.setHeaderText(PluginServices.getText(this,
|
295 |
"_Select_resources_to_save_before_closing_current_project"));
|
296 |
panel.setAcceptText( |
297 |
PluginServices.getText(this, "save_resources"), |
298 |
PluginServices |
299 |
.getText(this,
|
300 |
"Save_the_selected_resources_and_close_current_project"));
|
301 |
panel.setCancelText(PluginServices.getText(this, "Cancel"), |
302 |
PluginServices.getText(this, "Return_to_current_project")); |
303 |
int closeCurrProj;
|
304 |
try {
|
305 |
closeCurrProj = process.manageUnsavedData(); |
306 |
if (closeCurrProj == JOptionPane.NO_OPTION) { |
307 |
// the user chose to return to current project
|
308 |
return false; |
309 |
} |
310 |
} catch (Exception e) { |
311 |
LOG.error("Some data can not be saved", e);
|
312 |
} |
313 |
} |
314 |
return true; |
315 |
} |
316 |
|
317 |
public void execute(String command) { |
318 |
this.execute(command, null); |
319 |
} |
320 |
|
321 |
public void execute(String actionCommand, Object[] args) { |
322 |
if (actionCommand.equals("application-project-new")) { |
323 |
if (!askSave()) {
|
324 |
return;
|
325 |
} |
326 |
|
327 |
projectPath = null;
|
328 |
PluginServices.getMDIManager().closeAllWindows(); |
329 |
setProject(ProjectManager.getInstance().createProject()); |
330 |
|
331 |
showProjectWindow(); |
332 |
PluginServices.getMainFrame().setTitle(PluginServices.getText(this, "sin_titulo")); |
333 |
|
334 |
} else if (actionCommand.equals("application-project-open")) { |
335 |
if (!askSave()) {
|
336 |
return;
|
337 |
} |
338 |
|
339 |
setProject(ProjectManager.getInstance().createProject()); |
340 |
|
341 |
File projectFile = (File) ArrayUtils.get(args, 0, DataTypes.FILE); |
342 |
if (projectFile != null && !projectFile.exists()) { |
343 |
LOG.warn("Can't load project '" + projectFile.getAbsolutePath() + "', file not exist."); |
344 |
projectFile = null;
|
345 |
} |
346 |
|
347 |
if (projectFile == null) { |
348 |
Preferences prefs = Preferences.userRoot().node("gvsig.foldering"); |
349 |
JFileChooser jfc = new JFileChooser(PROJECT_FILE_CHOOSER_ID, prefs.get("ProjectsFolder", null)); |
350 |
|
351 |
GenericFileFilter projExtensionFilter = |
352 |
new GenericFileFilter(Project.FILE_EXTENSION, PluginServices.getText(this, "tipo_fichero_proyecto")); |
353 |
jfc.addChoosableFileFilter(projExtensionFilter); |
354 |
jfc.setFileFilter(projExtensionFilter); |
355 |
|
356 |
if (jfc.showOpenDialog((Component) PluginServices.getMainFrame()) != JFileChooser.APPROVE_OPTION) { |
357 |
return;
|
358 |
} |
359 |
// ProjectDocument.initializeNUMS();
|
360 |
|
361 |
projectFile = jfc.getSelectedFile(); |
362 |
} |
363 |
|
364 |
PluginServices.getMDIManager().closeAllWindows(); |
365 |
|
366 |
Project o = readProject(projectFile); |
367 |
setPath(projectFile.getAbsolutePath()); |
368 |
// lastPath = getPath();
|
369 |
if (o != null) { |
370 |
setProject(o); |
371 |
} |
372 |
|
373 |
getProjectFrame().setProject(p); |
374 |
PluginServices.getMainFrame().setTitle(projectFile.getName()); |
375 |
getProjectFrame().refreshControls(); |
376 |
|
377 |
// p.restoreWindowProperties();
|
378 |
|
379 |
} else if (actionCommand.equals("application-project-save")) { |
380 |
// saveProject();
|
381 |
try {
|
382 |
Launcher.manageUnsavedData("there_are_unsaved_resources");
|
383 |
} catch (Exception e) { |
384 |
LOG.warn("Can't manage unsaved data", e);
|
385 |
} |
386 |
} else if (actionCommand.equals("application-project-save-as")) { |
387 |
File file = (File) ArrayUtils.get(args, 0, DataTypes.FILE); |
388 |
saveAsProject(file); |
389 |
} |
390 |
|
391 |
} |
392 |
|
393 |
|
394 |
private void createEmptyProject() { |
395 |
setProject(ProjectManager.getInstance().createProject()); |
396 |
p.setName(PluginServices.getText(this, "untitled")); |
397 |
p.setModified(false);
|
398 |
PluginServices.getMainFrame().setTitle( |
399 |
PluginServices.getText(this, "sin_titulo")); |
400 |
setProject(p); |
401 |
showProjectWindow(); |
402 |
} |
403 |
|
404 |
/**
|
405 |
* @see com.iver.mdiApp.plugins.IExtension#postInitialize()
|
406 |
*/
|
407 |
public void postInitialize() { |
408 |
try {
|
409 |
if( !SwingUtilities.isEventDispatchThread() ) { |
410 |
SwingUtilities.invokeAndWait(new Runnable() { |
411 |
public void run() { |
412 |
createEmptyProject(); |
413 |
} |
414 |
}); |
415 |
} else {
|
416 |
createEmptyProject(); |
417 |
} |
418 |
} catch (Exception e) { |
419 |
LOG.warn("Can't load initial project.",e);
|
420 |
} |
421 |
} |
422 |
|
423 |
|
424 |
/**
|
425 |
* Escribe el proyecto en XML.
|
426 |
*
|
427 |
* @param file
|
428 |
* Fichero.
|
429 |
* @param p
|
430 |
* Proyecto.
|
431 |
*/
|
432 |
public boolean writeProject(File file, Project p) { |
433 |
return writeProject(file, p, true); |
434 |
} |
435 |
|
436 |
/**
|
437 |
* Escribe el proyecto en XML. Pero permite decidir si se pide confirmaci?n
|
438 |
* para sobreescribir
|
439 |
*
|
440 |
* @param file
|
441 |
* Fichero.
|
442 |
* @param p
|
443 |
* Proyecto.
|
444 |
* @param askConfirmation
|
445 |
* boolean
|
446 |
*/
|
447 |
public boolean writeProject(File file, Project p, boolean askConfirmation) { |
448 |
if (askConfirmation && file.exists()) {
|
449 |
int resp = JOptionPane.showConfirmDialog((Component) PluginServices |
450 |
.getMainFrame(), PluginServices.getText(this,
|
451 |
"fichero_ya_existe_seguro_desea_guardarlo"), PluginServices
|
452 |
.getText(this, "guardar"), JOptionPane.YES_NO_OPTION); |
453 |
if (resp != JOptionPane.YES_OPTION) { |
454 |
return false; |
455 |
} |
456 |
} |
457 |
NotificationManager.addInfo(PluginServices.getText(this,
|
458 |
"writing_project") + ": " + file.getName()); |
459 |
|
460 |
// write it out as XML
|
461 |
try {
|
462 |
fireBeforeSavingFileEvent(new SaveEvent(this, |
463 |
SaveEvent.BEFORE_SAVING, file)); |
464 |
p.saveState(file); |
465 |
fireAfterSavingFileEvent(new SaveEvent(this, |
466 |
SaveEvent.AFTER_SAVING, file)); |
467 |
|
468 |
PluginServices.getMainFrame().setTitle(file.getName()); |
469 |
setPath(file.toString()); |
470 |
|
471 |
} catch (PersistenceException e) {
|
472 |
String messagestack = e.getLocalizedMessageStack();
|
473 |
NotificationManager.addError( |
474 |
PluginServices.getText(this, "error_writing_project") |
475 |
+ ": " + file.getName() + "\n" + messagestack, e); |
476 |
return false; |
477 |
} catch (Exception e) { |
478 |
NotificationManager.addError( |
479 |
PluginServices.getText(this, "error_writing_project") |
480 |
+ ": " + file.getName(), e);
|
481 |
return false; |
482 |
} |
483 |
NotificationManager.addInfo(PluginServices.getText(this,
|
484 |
"wrote_project") + ": " + file.getName()); |
485 |
return true; |
486 |
} |
487 |
|
488 |
public Project readProject(String path) { |
489 |
Project project = ProjectManager.getInstance().createProject(); |
490 |
|
491 |
project.loadState(new File(path)); |
492 |
return (Project) project;
|
493 |
} |
494 |
|
495 |
/**
|
496 |
* Lee del XML el proyecto.<br>
|
497 |
* <br>
|
498 |
*
|
499 |
* Reads the XML of the project.<br>
|
500 |
* It returns a project object holding all needed info that is not linked to
|
501 |
* the Project Dialog. <br>
|
502 |
* In case you want the project to be linked to the window you must set this
|
503 |
* object to the extension:<br>
|
504 |
*
|
505 |
* <b>Example:</b><br>
|
506 |
*
|
507 |
* ...<br>
|
508 |
* ...<br>
|
509 |
* Project p = ProjectExtension.readProject(projectFile);<br>
|
510 |
* ProjectExtension.setProject(p); ...<br>
|
511 |
* ...<br>
|
512 |
*
|
513 |
* @param file
|
514 |
* Fichero.
|
515 |
*
|
516 |
* @return Project
|
517 |
*
|
518 |
*/
|
519 |
public Project readProject(File file) { |
520 |
Project project = ProjectManager.getInstance().createProject(); |
521 |
|
522 |
project.loadState(file); |
523 |
Set<String> unloadedObjects = project.getUnloadedObjects(); |
524 |
if(unloadedObjects!=null && !unloadedObjects.isEmpty()){ |
525 |
StringBuilder builder = new StringBuilder(); |
526 |
builder.append("Unloaded elements loading the project:\n");
|
527 |
Iterator<String> it = unloadedObjects.iterator(); |
528 |
while(it.hasNext()){
|
529 |
builder.append("\t");
|
530 |
builder.append(it.next()); |
531 |
builder.append("\n");
|
532 |
} |
533 |
|
534 |
LOG.warn(builder.toString()); |
535 |
|
536 |
ApplicationManager application = ApplicationLocator.getManager(); |
537 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
538 |
|
539 |
application.messageDialog( |
540 |
i18nManager.getTranslation("_some_project_elements_could_not_be_loaded")+"\n"+ |
541 |
i18nManager.getTranslation("_maybe_you_need_to_install_any_plugins")+"\n\n"+ |
542 |
i18nManager.getTranslation("_see_error_log_for_more_information"),
|
543 |
i18nManager.getTranslation("warning"),
|
544 |
JOptionPane.WARNING_MESSAGE);
|
545 |
|
546 |
} |
547 |
return (Project) project;
|
548 |
} |
549 |
|
550 |
/**
|
551 |
* Devuelve el proyecto.
|
552 |
*
|
553 |
* @return Proyecto.
|
554 |
*/
|
555 |
public Project getProject() {
|
556 |
return p;
|
557 |
} |
558 |
|
559 |
/**
|
560 |
* @see org.gvsig.andami.plugins.IExtension#isEnabled()
|
561 |
*/
|
562 |
public boolean isEnabled() { |
563 |
return true; |
564 |
} |
565 |
|
566 |
/**
|
567 |
* @see org.gvsig.andami.plugins.IExtension#isVisible()
|
568 |
*/
|
569 |
public boolean isVisible() { |
570 |
return true; |
571 |
} |
572 |
|
573 |
/**
|
574 |
* Sets the project
|
575 |
*
|
576 |
* @param p
|
577 |
*/
|
578 |
public void setProject(Project p) { |
579 |
this.p = p;
|
580 |
getProjectFrame().setProject(p); |
581 |
} |
582 |
|
583 |
private void registerDocuments() { |
584 |
ViewManager.register(); |
585 |
} |
586 |
|
587 |
private void initializeDocumentActionsExtensionPoint() { |
588 |
ExtensionPointManager epMan = ToolsLocator.getExtensionPointManager(); |
589 |
epMan.add( |
590 |
"DocumentActions_View",
|
591 |
"Context menu options of the view document list"
|
592 |
+ " in the project window "
|
593 |
+ "(register instances of "
|
594 |
+ "org.gvsig.app.project.AbstractDocumentContextMenuAction)");
|
595 |
} |
596 |
|
597 |
public static String getPath() { |
598 |
return projectPath;
|
599 |
} |
600 |
|
601 |
public static void setPath(String path) { |
602 |
projectPath = path; |
603 |
} |
604 |
|
605 |
public IWindow getProjectWindow() {
|
606 |
return getProjectFrame();
|
607 |
} |
608 |
|
609 |
public IExtensionStatus getStatus() {
|
610 |
return this; |
611 |
} |
612 |
|
613 |
public boolean hasUnsavedData() { |
614 |
return p.hasChanged();
|
615 |
} |
616 |
|
617 |
public IUnsavedData[] getUnsavedData() { |
618 |
if (hasUnsavedData()) {
|
619 |
UnsavedProject data = new UnsavedProject(this); |
620 |
IUnsavedData[] dataArray = { data };
|
621 |
return dataArray;
|
622 |
} else {
|
623 |
return null; |
624 |
} |
625 |
} |
626 |
|
627 |
/**
|
628 |
* Implements the IUnsavedData interface to show unsaved projects in the
|
629 |
* Unsavad Data dialog.
|
630 |
*
|
631 |
* @author Cesar Martinez Izquierdo <cesar.martinez@iver.es>
|
632 |
*/
|
633 |
public class UnsavedProject extends UnsavedData { |
634 |
|
635 |
public UnsavedProject(IExtension extension) {
|
636 |
super(extension);
|
637 |
} |
638 |
|
639 |
public String getDescription() { |
640 |
if (getPath() == null) { |
641 |
return PluginServices.getText(ProjectExtension.this,
|
642 |
"Unnamed_new_gvsig_project_");
|
643 |
} else {
|
644 |
return PluginServices.getText(ProjectExtension.this,
|
645 |
"Modified_project_");
|
646 |
} |
647 |
} |
648 |
|
649 |
public String getResourceName() { |
650 |
if (getPath() == null) { |
651 |
return PluginServices.getText(ProjectExtension.this, "Unnamed"); |
652 |
} else {
|
653 |
return getPath();
|
654 |
} |
655 |
|
656 |
} |
657 |
|
658 |
public boolean saveData() { |
659 |
return saveProject();
|
660 |
} |
661 |
|
662 |
public String getIcon() { |
663 |
return "project-icon"; |
664 |
} |
665 |
} |
666 |
|
667 |
public IMonitorableTask[] getRunningProcesses() { |
668 |
// TODO Auto-generated method stub
|
669 |
return null; |
670 |
} |
671 |
|
672 |
public boolean hasRunningProcesses() { |
673 |
// TODO Auto-generated method stub
|
674 |
return false; |
675 |
} |
676 |
|
677 |
/**
|
678 |
* Adds the specified before saving listener to receive
|
679 |
* "before saving file events" from this component. If l is null, no
|
680 |
* exception is thrown and no action is performed.
|
681 |
*
|
682 |
* @author Pablo Piqueras Bartolom? <pablo.piqueras@iver.es>
|
683 |
*
|
684 |
* @param l
|
685 |
* the before saving listener.
|
686 |
* @see SaveEvent
|
687 |
* @see BeforeSavingListener
|
688 |
* @see #removeListener(BeforeSavingListener)
|
689 |
* @see #getBeforeSavingListeners
|
690 |
*/
|
691 |
public synchronized void addListener(BeforeSavingListener l) { |
692 |
if (l == null) { |
693 |
return;
|
694 |
} |
695 |
if (!this.beforeSavingListeners.contains(l)) { |
696 |
this.beforeSavingListeners.add(l);
|
697 |
} |
698 |
} |
699 |
|
700 |
/**
|
701 |
* Adds the specified after saving listener to receive
|
702 |
* "after saving file events" from this component. If l is null, no
|
703 |
* exception is thrown and no action is performed.
|
704 |
*
|
705 |
* @author Pablo Piqueras Bartolom? <pablo.piqueras@iver.es>
|
706 |
*
|
707 |
* @param l
|
708 |
* the after saving listener.
|
709 |
* @see SaveEvent
|
710 |
* @see AfterSavingListener
|
711 |
* @see #removeListener(AfterSavingListener)
|
712 |
* @see #getAfterSavingListeners()
|
713 |
*/
|
714 |
public synchronized void addListener(AfterSavingListener l) { |
715 |
if (l == null) { |
716 |
return;
|
717 |
} |
718 |
|
719 |
if (!this.afterSavingListeners.contains(l)) { |
720 |
this.afterSavingListeners.add(l);
|
721 |
} |
722 |
|
723 |
} |
724 |
|
725 |
/**
|
726 |
* Returns an array of all the before saving listeners registered on this
|
727 |
* component.
|
728 |
*
|
729 |
* @author Pablo Piqueras Bartolom? <pablo.piqueras@iver.es>
|
730 |
*
|
731 |
* @return all of this component's <code>BeforeSavingListener</code>s or an
|
732 |
* empty array if no key listeners are currently registered
|
733 |
*
|
734 |
* @see #addBeforeSavingListener(BeforeSavingListener)
|
735 |
* @see #removeBeforeSavingListener(BeforeSavingListener)
|
736 |
*/
|
737 |
public synchronized BeforeSavingListener[] getBeforeSavingListeners() { |
738 |
return this.beforeSavingListeners |
739 |
.toArray(new BeforeSavingListener[] {}); |
740 |
} |
741 |
|
742 |
/**
|
743 |
* Returns an array of all the after saving listeners registered on this
|
744 |
* component.
|
745 |
*
|
746 |
* @author Pablo Piqueras Bartolom? <pablo.piqueras@iver.es>
|
747 |
*
|
748 |
* @return all of this component's <code>AfterSavingListener</code>s or an
|
749 |
* empty array if no key listeners are currently registered
|
750 |
*
|
751 |
* @see #addAfterSavingListener(AfterSavingListener)
|
752 |
* @see #removeAfterSavingListener
|
753 |
*/
|
754 |
public synchronized AfterSavingListener[] getAfterSavingListeners() { |
755 |
return this.afterSavingListeners.toArray(new AfterSavingListener[] {}); |
756 |
|
757 |
} |
758 |
|
759 |
/**
|
760 |
* Removes the specified before saving listener so that it no longer
|
761 |
* receives save file events from this component. This method performs no
|
762 |
* function, nor does it throw an exception, if the listener specified by
|
763 |
* the argument was not previously added to this component. If listener
|
764 |
* <code>l</code> is <code>null</code>, no exception is thrown and no action
|
765 |
* is performed.
|
766 |
*
|
767 |
* @author Pablo Piqueras Bartolom? <pablo.piqueras@iver.es>
|
768 |
*
|
769 |
* @param l
|
770 |
* the before saving listener
|
771 |
* @see SaveEvent
|
772 |
* @see BeforeSavingListener
|
773 |
* @see #addListener(BeforeSavingListener)
|
774 |
* @see #getBeforeSavingListeners()
|
775 |
*/
|
776 |
public synchronized void removeListener(BeforeSavingListener l) { |
777 |
if (l == null) { |
778 |
return;
|
779 |
} |
780 |
|
781 |
this.beforeSavingListeners.remove(l);
|
782 |
} |
783 |
|
784 |
/**
|
785 |
* Removes the specified after saving listener so that it no longer receives
|
786 |
* save file events from this component. This method performs no function,
|
787 |
* nor does it throw an exception, if the listener specified by the argument
|
788 |
* was not previously added to this component. If listener <code>l</code> is
|
789 |
* <code>null</code>, no exception is thrown and no action is performed.
|
790 |
*
|
791 |
* @author Pablo Piqueras Bartolom? <pablo.piqueras@iver.es>
|
792 |
*
|
793 |
* @param l
|
794 |
* the after saving listener
|
795 |
* @see SaveEvent
|
796 |
* @see AfterSavingListener
|
797 |
* @see #addListener(AfterSavingListener)
|
798 |
* @see #getAfterSavingListeners()
|
799 |
*/
|
800 |
public synchronized void removeListener(AfterSavingListener l) { |
801 |
if (l == null) { |
802 |
return;
|
803 |
} |
804 |
|
805 |
this.afterSavingListeners.remove(l);
|
806 |
} |
807 |
|
808 |
/**
|
809 |
* Reports a before saving file event.
|
810 |
*
|
811 |
* @author Pablo Piqueras Bartolom? <pablo.piqueras@iver.es>
|
812 |
*
|
813 |
* @param evt
|
814 |
* the before saving file event
|
815 |
*/
|
816 |
protected void fireBeforeSavingFileEvent(SaveEvent evt) { |
817 |
if ((evt.getID() != SaveEvent.BEFORE_SAVING) || (evt.getFile() == null)) { |
818 |
return;
|
819 |
} |
820 |
|
821 |
Iterator<BeforeSavingListener> iter = this.beforeSavingListeners |
822 |
.iterator(); |
823 |
|
824 |
while (iter.hasNext()) {
|
825 |
iter.next().beforeSaving(evt); |
826 |
} |
827 |
} |
828 |
|
829 |
/**
|
830 |
* Reports a after saving file event.
|
831 |
*
|
832 |
* @author Pablo Piqueras Bartolom? <pablo.piqueras@iver.es>
|
833 |
*
|
834 |
* @param evt
|
835 |
* the after saving file event
|
836 |
*/
|
837 |
protected void fireAfterSavingFileEvent(SaveEvent evt) { |
838 |
if ((evt.getID() != SaveEvent.AFTER_SAVING) || (evt.getFile() == null)) { |
839 |
return;
|
840 |
} |
841 |
Iterator<AfterSavingListener> iter = this.afterSavingListeners |
842 |
.iterator(); |
843 |
|
844 |
while (iter.hasNext()) {
|
845 |
iter.next().afterSaving(evt); |
846 |
} |
847 |
|
848 |
} |
849 |
} |