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 @ 42711
History | View | Annotate | Download (25.2 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 |
getProjectFrame().setProject(p); |
331 |
showProjectWindow(); |
332 |
PluginServices.getMainFrame().setTitle( |
333 |
PluginServices.getText(this, "sin_titulo")); |
334 |
|
335 |
} else if (actionCommand.equals("application-project-open")) { |
336 |
if (!askSave()) {
|
337 |
return;
|
338 |
} |
339 |
File projectFile = (File) ArrayUtils.get(args, 0, DataTypes.FILE); |
340 |
if ( projectFile != null && !projectFile.exists() ) { |
341 |
LOG.warn("Can't load project '"
|
342 |
+ projectFile.getAbsolutePath() |
343 |
+ "', file not exist.");
|
344 |
projectFile = null;
|
345 |
} |
346 |
|
347 |
if (projectFile == null) { |
348 |
Preferences prefs = Preferences.userRoot().node( |
349 |
"gvsig.foldering");
|
350 |
JFileChooser jfc = new JFileChooser(PROJECT_FILE_CHOOSER_ID, |
351 |
prefs.get("ProjectsFolder", null)); |
352 |
|
353 |
GenericFileFilter projExtensionFilter = new GenericFileFilter(
|
354 |
Project.FILE_EXTENSION, PluginServices.getText(this,
|
355 |
"tipo_fichero_proyecto"));
|
356 |
jfc.addChoosableFileFilter(projExtensionFilter); |
357 |
jfc.setFileFilter(projExtensionFilter); |
358 |
|
359 |
if (jfc.showOpenDialog((Component) PluginServices |
360 |
.getMainFrame()) != JFileChooser.APPROVE_OPTION) {
|
361 |
return;
|
362 |
} |
363 |
// ProjectDocument.initializeNUMS();
|
364 |
|
365 |
|
366 |
projectFile = jfc.getSelectedFile(); |
367 |
} |
368 |
|
369 |
PluginServices.getMDIManager().closeAllWindows(); |
370 |
|
371 |
Project o = readProject(projectFile); |
372 |
setPath(projectFile.getAbsolutePath()); |
373 |
// lastPath = getPath();
|
374 |
if (o != null) { |
375 |
setProject(o); |
376 |
} |
377 |
|
378 |
getProjectFrame().setProject(p); |
379 |
PluginServices.getMainFrame().setTitle(projectFile.getName()); |
380 |
getProjectFrame().refreshControls(); |
381 |
|
382 |
// p.restoreWindowProperties();
|
383 |
|
384 |
} else if (actionCommand.equals("application-project-save")) { |
385 |
// saveProject();
|
386 |
try {
|
387 |
Launcher.manageUnsavedData("there_are_unsaved_resources");
|
388 |
} catch (Exception e) { |
389 |
LOG.warn("Can't manage unsaved data", e);
|
390 |
} |
391 |
} else if (actionCommand.equals("application-project-save-as")) { |
392 |
File file = (File) ArrayUtils.get(args, 0, DataTypes.FILE); |
393 |
saveAsProject(file); |
394 |
} |
395 |
|
396 |
} |
397 |
|
398 |
|
399 |
private void createEmptyProject() { |
400 |
setProject(ProjectManager.getInstance().createProject()); |
401 |
p.setName(PluginServices.getText(this, "untitled")); |
402 |
p.setModified(false);
|
403 |
PluginServices.getMainFrame().setTitle( |
404 |
PluginServices.getText(this, "sin_titulo")); |
405 |
setProject(p); |
406 |
showProjectWindow(); |
407 |
} |
408 |
|
409 |
/**
|
410 |
* @see com.iver.mdiApp.plugins.IExtension#postInitialize()
|
411 |
*/
|
412 |
public void postInitialize() { |
413 |
try {
|
414 |
if( !SwingUtilities.isEventDispatchThread() ) { |
415 |
SwingUtilities.invokeAndWait(new Runnable() { |
416 |
public void run() { |
417 |
createEmptyProject(); |
418 |
} |
419 |
}); |
420 |
} else {
|
421 |
createEmptyProject(); |
422 |
} |
423 |
} catch (Exception e) { |
424 |
LOG.warn("Can't load initial project.",e);
|
425 |
} |
426 |
} |
427 |
|
428 |
|
429 |
/**
|
430 |
* Escribe el proyecto en XML.
|
431 |
*
|
432 |
* @param file
|
433 |
* Fichero.
|
434 |
* @param p
|
435 |
* Proyecto.
|
436 |
*/
|
437 |
public boolean writeProject(File file, Project p) { |
438 |
return writeProject(file, p, true); |
439 |
} |
440 |
|
441 |
/**
|
442 |
* Escribe el proyecto en XML. Pero permite decidir si se pide confirmaci?n
|
443 |
* para sobreescribir
|
444 |
*
|
445 |
* @param file
|
446 |
* Fichero.
|
447 |
* @param p
|
448 |
* Proyecto.
|
449 |
* @param askConfirmation
|
450 |
* boolean
|
451 |
*/
|
452 |
public boolean writeProject(File file, Project p, boolean askConfirmation) { |
453 |
if (askConfirmation && file.exists()) {
|
454 |
int resp = JOptionPane.showConfirmDialog((Component) PluginServices |
455 |
.getMainFrame(), PluginServices.getText(this,
|
456 |
"fichero_ya_existe_seguro_desea_guardarlo"), PluginServices
|
457 |
.getText(this, "guardar"), JOptionPane.YES_NO_OPTION); |
458 |
if (resp != JOptionPane.YES_OPTION) { |
459 |
return false; |
460 |
} |
461 |
} |
462 |
NotificationManager.addInfo(PluginServices.getText(this,
|
463 |
"writing_project") + ": " + file.getName()); |
464 |
|
465 |
// write it out as XML
|
466 |
try {
|
467 |
fireBeforeSavingFileEvent(new SaveEvent(this, |
468 |
SaveEvent.BEFORE_SAVING, file)); |
469 |
p.saveState(file); |
470 |
fireAfterSavingFileEvent(new SaveEvent(this, |
471 |
SaveEvent.AFTER_SAVING, file)); |
472 |
|
473 |
PluginServices.getMainFrame().setTitle(file.getName()); |
474 |
setPath(file.toString()); |
475 |
|
476 |
} catch (PersistenceException e) {
|
477 |
String messagestack = e.getLocalizedMessageStack();
|
478 |
NotificationManager.addError( |
479 |
PluginServices.getText(this, "error_writing_project") |
480 |
+ ": " + file.getName() + "\n" + messagestack, e); |
481 |
return false; |
482 |
} catch (Exception e) { |
483 |
NotificationManager.addError( |
484 |
PluginServices.getText(this, "error_writing_project") |
485 |
+ ": " + file.getName(), e);
|
486 |
return false; |
487 |
} |
488 |
NotificationManager.addInfo(PluginServices.getText(this,
|
489 |
"wrote_project") + ": " + file.getName()); |
490 |
return true; |
491 |
} |
492 |
|
493 |
public Project readProject(String path) { |
494 |
Project project = ProjectManager.getInstance().createProject(); |
495 |
|
496 |
project.loadState(new File(path)); |
497 |
return (Project) project;
|
498 |
} |
499 |
|
500 |
/**
|
501 |
* Lee del XML el proyecto.<br>
|
502 |
* <br>
|
503 |
*
|
504 |
* Reads the XML of the project.<br>
|
505 |
* It returns a project object holding all needed info that is not linked to
|
506 |
* the Project Dialog. <br>
|
507 |
* In case you want the project to be linked to the window you must set this
|
508 |
* object to the extension:<br>
|
509 |
*
|
510 |
* <b>Example:</b><br>
|
511 |
*
|
512 |
* ...<br>
|
513 |
* ...<br>
|
514 |
* Project p = ProjectExtension.readProject(projectFile);<br>
|
515 |
* ProjectExtension.setProject(p); ...<br>
|
516 |
* ...<br>
|
517 |
*
|
518 |
* @param file
|
519 |
* Fichero.
|
520 |
*
|
521 |
* @return Project
|
522 |
*
|
523 |
*/
|
524 |
public Project readProject(File file) { |
525 |
Project project = ProjectManager.getInstance().createProject(); |
526 |
|
527 |
project.loadState(file); |
528 |
Set<String> unloadedObjects = project.getUnloadedObjects(); |
529 |
if(unloadedObjects!=null && !unloadedObjects.isEmpty()){ |
530 |
StringBuilder builder = new StringBuilder(); |
531 |
builder.append("Unloaded elements loading the project:\n");
|
532 |
Iterator<String> it = unloadedObjects.iterator(); |
533 |
while(it.hasNext()){
|
534 |
builder.append("\t");
|
535 |
builder.append(it.next()); |
536 |
builder.append("\n");
|
537 |
} |
538 |
|
539 |
LOG.warn(builder.toString()); |
540 |
|
541 |
ApplicationManager application = ApplicationLocator.getManager(); |
542 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
543 |
|
544 |
application.messageDialog( |
545 |
i18nManager.getTranslation("_some_project_elements_could_not_be_loaded")+"\n"+ |
546 |
i18nManager.getTranslation("_maybe_you_need_to_install_any_plugins")+"\n\n"+ |
547 |
i18nManager.getTranslation("_see_error_log_for_more_information"),
|
548 |
i18nManager.getTranslation("warning"),
|
549 |
JOptionPane.WARNING_MESSAGE);
|
550 |
|
551 |
} |
552 |
return (Project) project;
|
553 |
} |
554 |
|
555 |
/**
|
556 |
* Devuelve el proyecto.
|
557 |
*
|
558 |
* @return Proyecto.
|
559 |
*/
|
560 |
public Project getProject() {
|
561 |
return p;
|
562 |
} |
563 |
|
564 |
/**
|
565 |
* @see org.gvsig.andami.plugins.IExtension#isEnabled()
|
566 |
*/
|
567 |
public boolean isEnabled() { |
568 |
return true; |
569 |
} |
570 |
|
571 |
/**
|
572 |
* @see org.gvsig.andami.plugins.IExtension#isVisible()
|
573 |
*/
|
574 |
public boolean isVisible() { |
575 |
return true; |
576 |
} |
577 |
|
578 |
/**
|
579 |
* Sets the project
|
580 |
*
|
581 |
* @param p
|
582 |
*/
|
583 |
public void setProject(Project p) { |
584 |
getProjectFrame().setProject(p); |
585 |
this.p = p;
|
586 |
} |
587 |
|
588 |
private void registerDocuments() { |
589 |
ViewManager.register(); |
590 |
} |
591 |
|
592 |
private void initializeDocumentActionsExtensionPoint() { |
593 |
ExtensionPointManager epMan = ToolsLocator.getExtensionPointManager(); |
594 |
epMan.add( |
595 |
"DocumentActions_View",
|
596 |
"Context menu options of the view document list"
|
597 |
+ " in the project window "
|
598 |
+ "(register instances of "
|
599 |
+ "org.gvsig.app.project.AbstractDocumentContextMenuAction)");
|
600 |
} |
601 |
|
602 |
public static String getPath() { |
603 |
return projectPath;
|
604 |
} |
605 |
|
606 |
public static void setPath(String path) { |
607 |
projectPath = path; |
608 |
} |
609 |
|
610 |
public IWindow getProjectWindow() {
|
611 |
return getProjectFrame();
|
612 |
} |
613 |
|
614 |
public IExtensionStatus getStatus() {
|
615 |
return this; |
616 |
} |
617 |
|
618 |
public boolean hasUnsavedData() { |
619 |
return p.hasChanged();
|
620 |
} |
621 |
|
622 |
public IUnsavedData[] getUnsavedData() { |
623 |
if (hasUnsavedData()) {
|
624 |
UnsavedProject data = new UnsavedProject(this); |
625 |
IUnsavedData[] dataArray = { data };
|
626 |
return dataArray;
|
627 |
} else {
|
628 |
return null; |
629 |
} |
630 |
} |
631 |
|
632 |
/**
|
633 |
* Implements the IUnsavedData interface to show unsaved projects in the
|
634 |
* Unsavad Data dialog.
|
635 |
*
|
636 |
* @author Cesar Martinez Izquierdo <cesar.martinez@iver.es>
|
637 |
*/
|
638 |
public class UnsavedProject extends UnsavedData { |
639 |
|
640 |
public UnsavedProject(IExtension extension) {
|
641 |
super(extension);
|
642 |
} |
643 |
|
644 |
public String getDescription() { |
645 |
if (getPath() == null) { |
646 |
return PluginServices.getText(ProjectExtension.this,
|
647 |
"Unnamed_new_gvsig_project_");
|
648 |
} else {
|
649 |
return PluginServices.getText(ProjectExtension.this,
|
650 |
"Modified_project_");
|
651 |
} |
652 |
} |
653 |
|
654 |
public String getResourceName() { |
655 |
if (getPath() == null) { |
656 |
return PluginServices.getText(ProjectExtension.this, "Unnamed"); |
657 |
} else {
|
658 |
return getPath();
|
659 |
} |
660 |
|
661 |
} |
662 |
|
663 |
public boolean saveData() { |
664 |
return saveProject();
|
665 |
} |
666 |
|
667 |
public String getIcon() { |
668 |
return "project-icon"; |
669 |
} |
670 |
} |
671 |
|
672 |
public IMonitorableTask[] getRunningProcesses() { |
673 |
// TODO Auto-generated method stub
|
674 |
return null; |
675 |
} |
676 |
|
677 |
public boolean hasRunningProcesses() { |
678 |
// TODO Auto-generated method stub
|
679 |
return false; |
680 |
} |
681 |
|
682 |
/**
|
683 |
* Adds the specified before saving listener to receive
|
684 |
* "before saving file events" from this component. If l is null, no
|
685 |
* exception is thrown and no action is performed.
|
686 |
*
|
687 |
* @author Pablo Piqueras Bartolom? <pablo.piqueras@iver.es>
|
688 |
*
|
689 |
* @param l
|
690 |
* the before saving listener.
|
691 |
* @see SaveEvent
|
692 |
* @see BeforeSavingListener
|
693 |
* @see #removeListener(BeforeSavingListener)
|
694 |
* @see #getBeforeSavingListeners
|
695 |
*/
|
696 |
public synchronized void addListener(BeforeSavingListener l) { |
697 |
if (l == null) { |
698 |
return;
|
699 |
} |
700 |
if (!this.beforeSavingListeners.contains(l)) { |
701 |
this.beforeSavingListeners.add(l);
|
702 |
} |
703 |
} |
704 |
|
705 |
/**
|
706 |
* Adds the specified after saving listener to receive
|
707 |
* "after saving file events" from this component. If l is null, no
|
708 |
* exception is thrown and no action is performed.
|
709 |
*
|
710 |
* @author Pablo Piqueras Bartolom? <pablo.piqueras@iver.es>
|
711 |
*
|
712 |
* @param l
|
713 |
* the after saving listener.
|
714 |
* @see SaveEvent
|
715 |
* @see AfterSavingListener
|
716 |
* @see #removeListener(AfterSavingListener)
|
717 |
* @see #getAfterSavingListeners()
|
718 |
*/
|
719 |
public synchronized void addListener(AfterSavingListener l) { |
720 |
if (l == null) { |
721 |
return;
|
722 |
} |
723 |
|
724 |
if (!this.afterSavingListeners.contains(l)) { |
725 |
this.afterSavingListeners.add(l);
|
726 |
} |
727 |
|
728 |
} |
729 |
|
730 |
/**
|
731 |
* Returns an array of all the before saving listeners registered on this
|
732 |
* component.
|
733 |
*
|
734 |
* @author Pablo Piqueras Bartolom? <pablo.piqueras@iver.es>
|
735 |
*
|
736 |
* @return all of this component's <code>BeforeSavingListener</code>s or an
|
737 |
* empty array if no key listeners are currently registered
|
738 |
*
|
739 |
* @see #addBeforeSavingListener(BeforeSavingListener)
|
740 |
* @see #removeBeforeSavingListener(BeforeSavingListener)
|
741 |
*/
|
742 |
public synchronized BeforeSavingListener[] getBeforeSavingListeners() { |
743 |
return this.beforeSavingListeners |
744 |
.toArray(new BeforeSavingListener[] {}); |
745 |
} |
746 |
|
747 |
/**
|
748 |
* Returns an array of all the after saving listeners registered on this
|
749 |
* component.
|
750 |
*
|
751 |
* @author Pablo Piqueras Bartolom? <pablo.piqueras@iver.es>
|
752 |
*
|
753 |
* @return all of this component's <code>AfterSavingListener</code>s or an
|
754 |
* empty array if no key listeners are currently registered
|
755 |
*
|
756 |
* @see #addAfterSavingListener(AfterSavingListener)
|
757 |
* @see #removeAfterSavingListener
|
758 |
*/
|
759 |
public synchronized AfterSavingListener[] getAfterSavingListeners() { |
760 |
return this.afterSavingListeners.toArray(new AfterSavingListener[] {}); |
761 |
|
762 |
} |
763 |
|
764 |
/**
|
765 |
* Removes the specified before saving listener so that it no longer
|
766 |
* receives save file events from this component. This method performs no
|
767 |
* function, nor does it throw an exception, if the listener specified by
|
768 |
* the argument was not previously added to this component. If listener
|
769 |
* <code>l</code> is <code>null</code>, no exception is thrown and no action
|
770 |
* is performed.
|
771 |
*
|
772 |
* @author Pablo Piqueras Bartolom? <pablo.piqueras@iver.es>
|
773 |
*
|
774 |
* @param l
|
775 |
* the before saving listener
|
776 |
* @see SaveEvent
|
777 |
* @see BeforeSavingListener
|
778 |
* @see #addListener(BeforeSavingListener)
|
779 |
* @see #getBeforeSavingListeners()
|
780 |
*/
|
781 |
public synchronized void removeListener(BeforeSavingListener l) { |
782 |
if (l == null) { |
783 |
return;
|
784 |
} |
785 |
|
786 |
this.beforeSavingListeners.remove(l);
|
787 |
} |
788 |
|
789 |
/**
|
790 |
* Removes the specified after saving listener so that it no longer receives
|
791 |
* save file events from this component. This method performs no function,
|
792 |
* nor does it throw an exception, if the listener specified by the argument
|
793 |
* was not previously added to this component. If listener <code>l</code> is
|
794 |
* <code>null</code>, no exception is thrown and no action is performed.
|
795 |
*
|
796 |
* @author Pablo Piqueras Bartolom? <pablo.piqueras@iver.es>
|
797 |
*
|
798 |
* @param l
|
799 |
* the after saving listener
|
800 |
* @see SaveEvent
|
801 |
* @see AfterSavingListener
|
802 |
* @see #addListener(AfterSavingListener)
|
803 |
* @see #getAfterSavingListeners()
|
804 |
*/
|
805 |
public synchronized void removeListener(AfterSavingListener l) { |
806 |
if (l == null) { |
807 |
return;
|
808 |
} |
809 |
|
810 |
this.afterSavingListeners.remove(l);
|
811 |
} |
812 |
|
813 |
/**
|
814 |
* Reports a before saving file event.
|
815 |
*
|
816 |
* @author Pablo Piqueras Bartolom? <pablo.piqueras@iver.es>
|
817 |
*
|
818 |
* @param evt
|
819 |
* the before saving file event
|
820 |
*/
|
821 |
protected void fireBeforeSavingFileEvent(SaveEvent evt) { |
822 |
if ((evt.getID() != SaveEvent.BEFORE_SAVING) || (evt.getFile() == null)) { |
823 |
return;
|
824 |
} |
825 |
|
826 |
Iterator<BeforeSavingListener> iter = this.beforeSavingListeners |
827 |
.iterator(); |
828 |
|
829 |
while (iter.hasNext()) {
|
830 |
iter.next().beforeSaving(evt); |
831 |
} |
832 |
} |
833 |
|
834 |
/**
|
835 |
* Reports a after saving file event.
|
836 |
*
|
837 |
* @author Pablo Piqueras Bartolom? <pablo.piqueras@iver.es>
|
838 |
*
|
839 |
* @param evt
|
840 |
* the after saving file event
|
841 |
*/
|
842 |
protected void fireAfterSavingFileEvent(SaveEvent evt) { |
843 |
if ((evt.getID() != SaveEvent.AFTER_SAVING) || (evt.getFile() == null)) { |
844 |
return;
|
845 |
} |
846 |
Iterator<AfterSavingListener> iter = this.afterSavingListeners |
847 |
.iterator(); |
848 |
|
849 |
while (iter.hasNext()) {
|
850 |
iter.next().afterSaving(evt); |
851 |
} |
852 |
|
853 |
} |
854 |
} |