svn-gvsig-desktop / tags / v2_0_0_Build_2058 / applications / appgvSIG / src / org / gvsig / app / extension / ProjectExtension.java @ 39222
History | View | Annotate | Download (24.6 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
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 2
|
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 |
*/
|
22 |
package org.gvsig.app.extension; |
23 |
|
24 |
import java.awt.Component; |
25 |
import java.io.File; |
26 |
import java.text.MessageFormat; |
27 |
import java.util.ArrayList; |
28 |
import java.util.Iterator; |
29 |
import java.util.List; |
30 |
import java.util.prefs.Preferences; |
31 |
|
32 |
import javax.swing.JOptionPane; |
33 |
|
34 |
import org.slf4j.Logger; |
35 |
import org.slf4j.LoggerFactory; |
36 |
|
37 |
import org.gvsig.andami.IconThemeHelper; |
38 |
import org.gvsig.andami.Launcher; |
39 |
import org.gvsig.andami.Launcher.TerminationProcess; |
40 |
import org.gvsig.andami.PluginServices; |
41 |
import org.gvsig.andami.messages.NotificationManager; |
42 |
import org.gvsig.andami.plugins.Extension; |
43 |
import org.gvsig.andami.plugins.IExtension; |
44 |
import org.gvsig.andami.plugins.status.IExtensionStatus; |
45 |
import org.gvsig.andami.plugins.status.IUnsavedData; |
46 |
import org.gvsig.andami.plugins.status.UnsavedData; |
47 |
import org.gvsig.andami.ui.mdiManager.IWindow; |
48 |
import org.gvsig.andami.ui.mdiManager.WindowInfo; |
49 |
import org.gvsig.andami.ui.wizard.UnsavedDataPanel; |
50 |
import org.gvsig.app.project.Project; |
51 |
import org.gvsig.app.project.ProjectManager; |
52 |
import org.gvsig.app.project.documents.gui.ProjectWindow; |
53 |
import org.gvsig.app.project.documents.view.ViewManager; |
54 |
import org.gvsig.gui.beans.swing.JFileChooser; |
55 |
import org.gvsig.tools.ToolsLocator; |
56 |
import org.gvsig.tools.extensionpoint.ExtensionPointManager; |
57 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
58 |
import org.gvsig.utils.GenericFileFilter; |
59 |
import org.gvsig.utils.save.AfterSavingListener; |
60 |
import org.gvsig.utils.save.BeforeSavingListener; |
61 |
import org.gvsig.utils.save.SaveEvent; |
62 |
import org.gvsig.utils.swing.threads.IMonitorableTask; |
63 |
|
64 |
/**
|
65 |
* Extension que proporciona controles para crear proyectos nuevos, abrirlos y
|
66 |
* guardarlos. Adem?s los tipos de tabla que soporta el proyecto son a?adidos
|
67 |
* en esta clase.
|
68 |
*
|
69 |
* @author Fernando Gonz?lez Cort?s
|
70 |
*/
|
71 |
public class ProjectExtension extends Extension implements IExtensionStatus { |
72 |
private static final Logger LOG = |
73 |
LoggerFactory.getLogger(ProjectExtension.class); |
74 |
|
75 |
private static String projectPath = null; |
76 |
private ProjectWindow projectFrame;
|
77 |
private Project p;
|
78 |
private String lastSavePath; |
79 |
private WindowInfo seedProjectWindow;
|
80 |
public static final String PROJECT_FILE_CHOOSER_ID = |
81 |
"PROJECT_FILECHOOSER_ID";
|
82 |
/**
|
83 |
* Use UTF-8 for encoding, as it can represent characters from
|
84 |
* any language.
|
85 |
*
|
86 |
* Another sensible option would be
|
87 |
* encoding = System.getProperty("file.encoding");
|
88 |
* but this would need some extra testing.
|
89 |
*
|
90 |
* @deprecated see PersistentManager
|
91 |
*/
|
92 |
public static String PROJECTENCODING = "UTF-8"; |
93 |
|
94 |
private List<BeforeSavingListener> beforeSavingListeners = |
95 |
new ArrayList<BeforeSavingListener>(); |
96 |
|
97 |
private List<AfterSavingListener> afterSavingListeners = |
98 |
new ArrayList<AfterSavingListener>(); |
99 |
|
100 |
/**
|
101 |
* @see com.iver.mdiApp.plugins.IExtension#initialize()
|
102 |
*/
|
103 |
public void initialize() { |
104 |
try {
|
105 |
Class.forName("javax.media.jai.EnumeratedParameter"); |
106 |
} catch (ClassNotFoundException e) { |
107 |
NotificationManager |
108 |
.addError( |
109 |
"La m?quina virtual con la que se ejecuta gvSIG no tiene JAI instalado",
|
110 |
e); |
111 |
} |
112 |
|
113 |
initializeDocumentActionsExtensionPoint(); |
114 |
registerIcons(); |
115 |
} |
116 |
|
117 |
private void registerIcons() { |
118 |
IconThemeHelper.registerIcon("action", "application-project-new", this); |
119 |
IconThemeHelper.registerIcon("action", "application-project-open", this); |
120 |
IconThemeHelper.registerIcon("action", "application-project-save", this); |
121 |
IconThemeHelper.registerIcon("action", "application-project-save-as", this); |
122 |
IconThemeHelper.registerIcon("action", "application-exit", this); |
123 |
|
124 |
IconThemeHelper.registerIcon("project", "project-icon", this); |
125 |
} |
126 |
|
127 |
private void loadInitialProject() { |
128 |
String[] theArgs = PluginServices.getArguments(); |
129 |
String lastArg = theArgs[theArgs.length - 1]; |
130 |
if ((lastArg.toLowerCase().endsWith(Project.FILE_EXTENSION
|
131 |
.toLowerCase()))) { |
132 |
PluginServices.getLogger().debug( |
133 |
"Intentando cargar el proyecto " + lastArg);
|
134 |
// File projectFile = new File(lastArg);
|
135 |
setProject(readProject(lastArg)); |
136 |
|
137 |
int last_bar = lastArg.lastIndexOf(File.separator); |
138 |
String tit = lastArg.substring(last_bar);
|
139 |
|
140 |
PluginServices.getMainFrame().setTitle(tit); |
141 |
projectPath = lastArg; |
142 |
} else {
|
143 |
setProject(ProjectManager.getInstance().createProject()); |
144 |
p.setName(PluginServices.getText(this, "untitled")); |
145 |
p.setModified(false);
|
146 |
PluginServices.getMainFrame().setTitle( |
147 |
PluginServices.getText(this, "sin_titulo")); |
148 |
} |
149 |
|
150 |
} |
151 |
|
152 |
/**
|
153 |
* @see com.iver.mdiApp.plugins.IExtension#postInitialize()
|
154 |
*/
|
155 |
public void postInitialize() { |
156 |
registerDocuments(); |
157 |
loadInitialProject(); |
158 |
getProjectFrame().setProject(p); |
159 |
showProjectWindow(); |
160 |
} |
161 |
|
162 |
public ProjectWindow getProjectFrame() {
|
163 |
if (projectFrame == null) { |
164 |
projectFrame = new ProjectWindow();
|
165 |
} |
166 |
return projectFrame;
|
167 |
} |
168 |
|
169 |
/**
|
170 |
* Muestra la ventana con el gestor de proyectos.
|
171 |
*/
|
172 |
public void showProjectWindow() { |
173 |
if (seedProjectWindow != null) { |
174 |
if (seedProjectWindow.isClosed()) {
|
175 |
// if it was closed, we just don't open the window now
|
176 |
seedProjectWindow.setClosed(false);
|
177 |
return;
|
178 |
} |
179 |
WindowInfo winProps = seedProjectWindow; |
180 |
seedProjectWindow = null;
|
181 |
PluginServices.getMDIManager().addWindow(getProjectFrame()); |
182 |
PluginServices.getMDIManager().changeWindowInfo(getProjectFrame(), |
183 |
winProps); |
184 |
} else {
|
185 |
PluginServices.getMDIManager().addWindow(getProjectFrame()); |
186 |
} |
187 |
} |
188 |
|
189 |
/**
|
190 |
* Muestra la ventana con el gestor de proyectos, con las propiedades
|
191 |
* de ventana especificadas.
|
192 |
*/
|
193 |
public void showProjectWindow(WindowInfo wi) { |
194 |
seedProjectWindow = wi; |
195 |
showProjectWindow(); |
196 |
} |
197 |
|
198 |
/**
|
199 |
* Guarda el proyecto actual en disco.
|
200 |
*/
|
201 |
private boolean guardar() { |
202 |
boolean saved = false; |
203 |
// if (p.getPath() == null) {
|
204 |
if (projectPath == null) { |
205 |
saved = guardarDialogo(); |
206 |
} else {
|
207 |
long t1, t2;
|
208 |
t1 = System.currentTimeMillis();
|
209 |
saved = writeProject(new File(projectPath), p, false); |
210 |
t2 = System.currentTimeMillis();
|
211 |
PluginServices.getLogger().info( |
212 |
"Project saved. " + (t2 - t1) + " miliseconds"); |
213 |
getProjectFrame().refreshControls(); |
214 |
} |
215 |
return saved;
|
216 |
} |
217 |
|
218 |
private boolean guardarDialogo() { |
219 |
boolean saved = false; |
220 |
|
221 |
if (lastSavePath == null) { |
222 |
lastSavePath = projectPath; |
223 |
} |
224 |
|
225 |
Preferences prefs = Preferences.userRoot().node("gvsig.foldering"); |
226 |
JFileChooser jfc =
|
227 |
new JFileChooser(PROJECT_FILE_CHOOSER_ID, prefs.get( |
228 |
"ProjectsFolder", null)); |
229 |
|
230 |
jfc.setDialogTitle(PluginServices.getText(this, "guardar_proyecto")); |
231 |
jfc.addChoosableFileFilter(new GenericFileFilter(
|
232 |
Project.FILE_EXTENSION, MessageFormat.format(
|
233 |
PluginServices.getText(this, "_gvSIG_file_project_({0})"), |
234 |
Project.FILE_EXTENSION))); |
235 |
|
236 |
if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) { |
237 |
File file = jfc.getSelectedFile();
|
238 |
if (!(file.getPath().toLowerCase().endsWith(Project.FILE_EXTENSION
|
239 |
.toLowerCase()))) { |
240 |
file = new File(file.getPath() + Project.FILE_EXTENSION); |
241 |
} |
242 |
saved = writeProject(file, p); |
243 |
String filePath = file.getAbsolutePath();
|
244 |
lastSavePath = |
245 |
filePath.substring(0, filePath.lastIndexOf(File.separatorChar)); |
246 |
|
247 |
getProjectFrame().refreshControls(); |
248 |
} |
249 |
return saved;
|
250 |
} |
251 |
|
252 |
/**
|
253 |
* Checks whether the project and related unsaved data is modified,
|
254 |
* and allows the user to save it.
|
255 |
*
|
256 |
* @return true if the data has been correctly saved, false otherwise
|
257 |
*/
|
258 |
private boolean askSave() { |
259 |
if (p != null && p.hasChanged()) { |
260 |
TerminationProcess process = Launcher.getTerminationProcess(); |
261 |
UnsavedDataPanel panel = process.getUnsavedDataPanel(); |
262 |
panel.setHeaderText(PluginServices.getText(this,
|
263 |
"Select_resources_to_save_before_closing_current_project"));
|
264 |
panel.setAcceptText(PluginServices.getText(this, "save_resources"), |
265 |
PluginServices.getText(this,
|
266 |
"Save_the_selected_resources_and_close_current_project"));
|
267 |
panel.setCancelText(PluginServices.getText(this, "Dont_close"), |
268 |
PluginServices.getText(this, "Return_to_current_project")); |
269 |
int closeCurrProj;
|
270 |
try {
|
271 |
closeCurrProj = process.manageUnsavedData(); |
272 |
if (closeCurrProj == JOptionPane.NO_OPTION) { |
273 |
// the user chose to return to current project
|
274 |
return false; |
275 |
} |
276 |
} catch (Exception e) { |
277 |
LOG.error("Some data can not be saved", e);
|
278 |
} |
279 |
} |
280 |
return true; |
281 |
} |
282 |
|
283 |
/**
|
284 |
* @see com.iver.mdiApp.plugins.IExtension#updateUI(java.lang.String)
|
285 |
*/
|
286 |
public void execute(String actionCommand) { |
287 |
if (actionCommand.equals("application-project-new")) { |
288 |
if (!askSave()) {
|
289 |
return;
|
290 |
} |
291 |
|
292 |
projectPath = null;
|
293 |
// ProjectDocument.initializeNUMS();
|
294 |
PluginServices.getMDIManager().closeAllWindows(); |
295 |
setProject(ProjectManager.getInstance().createProject()); |
296 |
getProjectFrame().setProject(p); |
297 |
showProjectWindow(); |
298 |
PluginServices.getMainFrame().setTitle( |
299 |
PluginServices.getText(this, "sin_titulo")); |
300 |
} else
|
301 |
if (actionCommand.equals("application-project-open")) { |
302 |
if (!askSave()) {
|
303 |
return;
|
304 |
} |
305 |
|
306 |
Preferences prefs =
|
307 |
Preferences.userRoot().node("gvsig.foldering"); |
308 |
JFileChooser jfc =
|
309 |
new JFileChooser(PROJECT_FILE_CHOOSER_ID, prefs.get( |
310 |
"ProjectsFolder", null)); |
311 |
jfc.addChoosableFileFilter(new GenericFileFilter(
|
312 |
Project.FILE_EXTENSION, PluginServices.getText(this,
|
313 |
"tipo_fichero_proyecto")));
|
314 |
|
315 |
if (jfc.showOpenDialog((Component) PluginServices |
316 |
.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
|
317 |
// ProjectDocument.initializeNUMS();
|
318 |
PluginServices.getMDIManager().closeAllWindows(); |
319 |
|
320 |
File projectFile = jfc.getSelectedFile();
|
321 |
Project o = readProject(projectFile); |
322 |
setPath(projectFile.getAbsolutePath()); |
323 |
// lastPath = getPath();
|
324 |
if (o != null) { |
325 |
setProject(o); |
326 |
} |
327 |
|
328 |
getProjectFrame().setProject(p); |
329 |
PluginServices.getMainFrame().setTitle(projectFile.getName()); |
330 |
getProjectFrame().refreshControls(); |
331 |
|
332 |
// p.restoreWindowProperties();
|
333 |
} |
334 |
} else
|
335 |
if (actionCommand.equals("application-project-save")) { |
336 |
guardar(); |
337 |
} else
|
338 |
if (actionCommand.equals("application-project-save-as")) { |
339 |
guardarDialogo(); |
340 |
} else
|
341 |
if (actionCommand.equals("application-exit")) { |
342 |
Launcher.closeApplication(); |
343 |
} |
344 |
} |
345 |
|
346 |
/**
|
347 |
* Escribe el proyecto en XML.
|
348 |
*
|
349 |
* @param file
|
350 |
* Fichero.
|
351 |
* @param p
|
352 |
* Proyecto.
|
353 |
*/
|
354 |
public boolean writeProject(File file, Project p) { |
355 |
return writeProject(file, p, true); |
356 |
} |
357 |
|
358 |
/**
|
359 |
* Escribe el proyecto en XML. Pero permite decidir si se
|
360 |
* pide confirmaci?n para sobreescribir
|
361 |
*
|
362 |
* @param file
|
363 |
* Fichero.
|
364 |
* @param p
|
365 |
* Proyecto.
|
366 |
* @param askConfirmation
|
367 |
* boolean
|
368 |
*/
|
369 |
public boolean writeProject(File file, Project p, boolean askConfirmation) { |
370 |
if (askConfirmation && file.exists()) {
|
371 |
int resp =
|
372 |
JOptionPane.showConfirmDialog((Component) PluginServices |
373 |
.getMainFrame(), PluginServices.getText(this,
|
374 |
"fichero_ya_existe_seguro_desea_guardarlo"), PluginServices
|
375 |
.getText(this, "guardar"), JOptionPane.YES_NO_OPTION); |
376 |
if (resp != JOptionPane.YES_OPTION) { |
377 |
return false; |
378 |
} |
379 |
} |
380 |
NotificationManager.addInfo(PluginServices.getText(this,
|
381 |
"writinng_project") + ": " + file.getName()); |
382 |
|
383 |
// write it out as XML
|
384 |
try {
|
385 |
fireBeforeSavingFileEvent(new SaveEvent(this, |
386 |
SaveEvent.BEFORE_SAVING, file)); |
387 |
p.saveState(file); |
388 |
fireAfterSavingFileEvent(new SaveEvent(this, |
389 |
SaveEvent.AFTER_SAVING, file)); |
390 |
|
391 |
PluginServices.getMainFrame().setTitle(file.getName()); |
392 |
setPath(file.toString()); |
393 |
|
394 |
} catch (PersistenceException e) {
|
395 |
String messagestack = e.getLocalizedMessageStack();
|
396 |
NotificationManager.addError( |
397 |
PluginServices.getText(this, "error_writing_project") + ": " |
398 |
+ file.getName() + "\n" + messagestack, e);
|
399 |
return false; |
400 |
} catch (Exception e) { |
401 |
NotificationManager.addError( |
402 |
PluginServices.getText(this, "error_writing_project") + ": " |
403 |
+ file.getName(), e); |
404 |
return false; |
405 |
} |
406 |
NotificationManager.addInfo(PluginServices.getText(this,
|
407 |
"wrote_project") + ": " + file.getName()); |
408 |
return true; |
409 |
} |
410 |
|
411 |
public Project readProject(String path) { |
412 |
Project project = ProjectManager.getInstance().createProject(); |
413 |
|
414 |
project.loadState(new File(path)); |
415 |
return (Project) project;
|
416 |
} |
417 |
|
418 |
/**
|
419 |
* Lee del XML el proyecto.<br>
|
420 |
* <br>
|
421 |
*
|
422 |
* Reads the XML of the project.<br>
|
423 |
* It returns a project object holding all needed info that is not linked to
|
424 |
* the Project Dialog. <br>
|
425 |
* In case you want the project to be
|
426 |
* linked to the window you must set this object to the extension:<br>
|
427 |
*
|
428 |
* <b>Example:</b><br>
|
429 |
*
|
430 |
* ...<br>
|
431 |
* ...<br>
|
432 |
* Project p = ProjectExtension.readProject(projectFile);<br>
|
433 |
* ProjectExtension.setProject(p);
|
434 |
* ...<br>
|
435 |
* ...<br>
|
436 |
*
|
437 |
* @param file
|
438 |
* Fichero.
|
439 |
*
|
440 |
* @return Project
|
441 |
*
|
442 |
*/
|
443 |
public Project readProject(File file) { |
444 |
Project project = ProjectManager.getInstance().createProject(); |
445 |
|
446 |
project.loadState(file); |
447 |
return (Project) project;
|
448 |
} |
449 |
|
450 |
/**
|
451 |
* Devuelve el proyecto.
|
452 |
*
|
453 |
* @return Proyecto.
|
454 |
*/
|
455 |
public Project getProject() {
|
456 |
return p;
|
457 |
} |
458 |
|
459 |
/**
|
460 |
* @see org.gvsig.andami.plugins.IExtension#isEnabled()
|
461 |
*/
|
462 |
public boolean isEnabled() { |
463 |
return true; |
464 |
} |
465 |
|
466 |
/**
|
467 |
* @see org.gvsig.andami.plugins.IExtension#isVisible()
|
468 |
*/
|
469 |
public boolean isVisible() { |
470 |
return true; |
471 |
} |
472 |
|
473 |
/**
|
474 |
* Sets the project
|
475 |
*
|
476 |
* @param p
|
477 |
*/
|
478 |
public void setProject(Project p) { |
479 |
getProjectFrame().setProject(p); |
480 |
this.p = p;
|
481 |
} |
482 |
|
483 |
private void registerDocuments() { |
484 |
ViewManager.register(); |
485 |
} |
486 |
|
487 |
private void initializeDocumentActionsExtensionPoint() { |
488 |
ExtensionPointManager epMan = ToolsLocator.getExtensionPointManager(); |
489 |
epMan.add("DocumentActions_View",
|
490 |
"Context menu options of the view document list"
|
491 |
+ " in the project window " + "(register instances of " |
492 |
+ "org.gvsig.app.project.AbstractDocumentContextMenuAction)");
|
493 |
} |
494 |
|
495 |
public static String getPath() { |
496 |
return projectPath;
|
497 |
} |
498 |
|
499 |
public static void setPath(String path) { |
500 |
projectPath = path; |
501 |
} |
502 |
|
503 |
public IWindow getProjectWindow() {
|
504 |
return getProjectFrame();
|
505 |
} |
506 |
|
507 |
public IExtensionStatus getStatus() {
|
508 |
return this; |
509 |
} |
510 |
|
511 |
public boolean hasUnsavedData() { |
512 |
return p.hasChanged();
|
513 |
} |
514 |
|
515 |
public IUnsavedData[] getUnsavedData() { |
516 |
if (hasUnsavedData()) {
|
517 |
UnsavedProject data = new UnsavedProject(this); |
518 |
IUnsavedData[] dataArray = { data };
|
519 |
return dataArray;
|
520 |
} else {
|
521 |
return null; |
522 |
} |
523 |
} |
524 |
|
525 |
/**
|
526 |
* Implements the IUnsavedData interface to show unsaved projects
|
527 |
* in the Unsavad Data dialog.
|
528 |
*
|
529 |
* @author Cesar Martinez Izquierdo <cesar.martinez@iver.es>
|
530 |
*/
|
531 |
public class UnsavedProject extends UnsavedData { |
532 |
|
533 |
public UnsavedProject(IExtension extension) {
|
534 |
super(extension);
|
535 |
} |
536 |
|
537 |
public String getDescription() { |
538 |
if (getPath() == null) { |
539 |
return PluginServices.getText(ProjectExtension.this,
|
540 |
"Unnamed_new_gvsig_project_");
|
541 |
} else {
|
542 |
return PluginServices.getText(ProjectExtension.this,
|
543 |
"Modified_project_");
|
544 |
} |
545 |
} |
546 |
|
547 |
public String getResourceName() { |
548 |
if (getPath() == null) { |
549 |
return PluginServices.getText(ProjectExtension.this, "Unnamed"); |
550 |
} else {
|
551 |
return getPath();
|
552 |
} |
553 |
|
554 |
} |
555 |
|
556 |
public boolean saveData() { |
557 |
return guardar();
|
558 |
} |
559 |
|
560 |
public String getIcon() { |
561 |
return "project-icon"; |
562 |
} |
563 |
} |
564 |
|
565 |
public IMonitorableTask[] getRunningProcesses() { |
566 |
// TODO Auto-generated method stub
|
567 |
return null; |
568 |
} |
569 |
|
570 |
public boolean hasRunningProcesses() { |
571 |
// TODO Auto-generated method stub
|
572 |
return false; |
573 |
} |
574 |
|
575 |
/**
|
576 |
* Adds the specified before saving listener to receive
|
577 |
* "before saving file events" from
|
578 |
* this component.
|
579 |
* If l is null, no exception is thrown and no action is performed.
|
580 |
*
|
581 |
* @author Pablo Piqueras Bartolom? <pablo.piqueras@iver.es>
|
582 |
*
|
583 |
* @param l
|
584 |
* the before saving listener.
|
585 |
* @see SaveEvent
|
586 |
* @see BeforeSavingListener
|
587 |
* @see #removeListener(BeforeSavingListener)
|
588 |
* @see #getBeforeSavingListeners
|
589 |
*/
|
590 |
public synchronized void addListener(BeforeSavingListener l) { |
591 |
if (l == null) { |
592 |
return;
|
593 |
} |
594 |
if (!this.beforeSavingListeners.contains(l)) { |
595 |
this.beforeSavingListeners.add(l);
|
596 |
} |
597 |
} |
598 |
|
599 |
/**
|
600 |
* Adds the specified after saving listener to receive
|
601 |
* "after saving file events" from
|
602 |
* this component.
|
603 |
* If l is null, no exception is thrown and no action is performed.
|
604 |
*
|
605 |
* @author Pablo Piqueras Bartolom? <pablo.piqueras@iver.es>
|
606 |
*
|
607 |
* @param l
|
608 |
* the after saving listener.
|
609 |
* @see SaveEvent
|
610 |
* @see AfterSavingListener
|
611 |
* @see #removeListener(AfterSavingListener)
|
612 |
* @see #getAfterSavingListeners()
|
613 |
*/
|
614 |
public synchronized void addListener(AfterSavingListener l) { |
615 |
if (l == null) { |
616 |
return;
|
617 |
} |
618 |
|
619 |
if (!this.afterSavingListeners.contains(l)) { |
620 |
this.afterSavingListeners.add(l);
|
621 |
} |
622 |
|
623 |
} |
624 |
|
625 |
/**
|
626 |
* Returns an array of all the before saving listeners
|
627 |
* registered on this component.
|
628 |
*
|
629 |
* @author Pablo Piqueras Bartolom? <pablo.piqueras@iver.es>
|
630 |
*
|
631 |
* @return all of this component's <code>BeforeSavingListener</code>s
|
632 |
* or an empty array if no key
|
633 |
* listeners are currently registered
|
634 |
*
|
635 |
* @see #addBeforeSavingListener(BeforeSavingListener)
|
636 |
* @see #removeBeforeSavingListener(BeforeSavingListener)
|
637 |
*/
|
638 |
public synchronized BeforeSavingListener[] getBeforeSavingListeners() { |
639 |
return this.beforeSavingListeners |
640 |
.toArray(new BeforeSavingListener[] {}); |
641 |
} |
642 |
|
643 |
/**
|
644 |
* Returns an array of all the after saving listeners
|
645 |
* registered on this component.
|
646 |
*
|
647 |
* @author Pablo Piqueras Bartolom? <pablo.piqueras@iver.es>
|
648 |
*
|
649 |
* @return all of this component's <code>AfterSavingListener</code>s
|
650 |
* or an empty array if no key
|
651 |
* listeners are currently registered
|
652 |
*
|
653 |
* @see #addAfterSavingListener(AfterSavingListener)
|
654 |
* @see #removeAfterSavingListener
|
655 |
*/
|
656 |
public synchronized AfterSavingListener[] getAfterSavingListeners() { |
657 |
return this.afterSavingListeners.toArray(new AfterSavingListener[] {}); |
658 |
|
659 |
} |
660 |
|
661 |
/**
|
662 |
* Removes the specified before saving listener so that it no longer
|
663 |
* receives save file events from this component. This method performs
|
664 |
* no function, nor does it throw an exception, if the listener
|
665 |
* specified by the argument was not previously added to this component.
|
666 |
* If listener <code>l</code> is <code>null</code>,
|
667 |
* no exception is thrown and no action is performed.
|
668 |
*
|
669 |
* @author Pablo Piqueras Bartolom? <pablo.piqueras@iver.es>
|
670 |
*
|
671 |
* @param l
|
672 |
* the before saving listener
|
673 |
* @see SaveEvent
|
674 |
* @see BeforeSavingListener
|
675 |
* @see #addListener(BeforeSavingListener)
|
676 |
* @see #getBeforeSavingListeners()
|
677 |
*/
|
678 |
public synchronized void removeListener(BeforeSavingListener l) { |
679 |
if (l == null) { |
680 |
return;
|
681 |
} |
682 |
|
683 |
this.beforeSavingListeners.remove(l);
|
684 |
} |
685 |
|
686 |
/**
|
687 |
* Removes the specified after saving listener so that it no longer
|
688 |
* receives save file events from this component. This method performs
|
689 |
* no function, nor does it throw an exception, if the listener
|
690 |
* specified by the argument was not previously added to this component.
|
691 |
* If listener <code>l</code> is <code>null</code>,
|
692 |
* no exception is thrown and no action is performed.
|
693 |
*
|
694 |
* @author Pablo Piqueras Bartolom? <pablo.piqueras@iver.es>
|
695 |
*
|
696 |
* @param l
|
697 |
* the after saving listener
|
698 |
* @see SaveEvent
|
699 |
* @see AfterSavingListener
|
700 |
* @see #addListener(AfterSavingListener)
|
701 |
* @see #getAfterSavingListeners()
|
702 |
*/
|
703 |
public synchronized void removeListener(AfterSavingListener l) { |
704 |
if (l == null) { |
705 |
return;
|
706 |
} |
707 |
|
708 |
this.afterSavingListeners.remove(l);
|
709 |
} |
710 |
|
711 |
/**
|
712 |
* Reports a before saving file event.
|
713 |
*
|
714 |
* @author Pablo Piqueras Bartolom? <pablo.piqueras@iver.es>
|
715 |
*
|
716 |
* @param evt
|
717 |
* the before saving file event
|
718 |
*/
|
719 |
protected void fireBeforeSavingFileEvent(SaveEvent evt) { |
720 |
if ((evt.getID() != SaveEvent.BEFORE_SAVING) || (evt.getFile() == null)) { |
721 |
return;
|
722 |
} |
723 |
|
724 |
Iterator<BeforeSavingListener> iter =
|
725 |
this.beforeSavingListeners.iterator();
|
726 |
|
727 |
while (iter.hasNext()) {
|
728 |
iter.next().beforeSaving(evt); |
729 |
} |
730 |
} |
731 |
|
732 |
/**
|
733 |
* Reports a after saving file event.
|
734 |
*
|
735 |
* @author Pablo Piqueras Bartolom? <pablo.piqueras@iver.es>
|
736 |
*
|
737 |
* @param evt
|
738 |
* the after saving file event
|
739 |
*/
|
740 |
protected void fireAfterSavingFileEvent(SaveEvent evt) { |
741 |
if ((evt.getID() != SaveEvent.AFTER_SAVING) || (evt.getFile() == null)) { |
742 |
return;
|
743 |
} |
744 |
Iterator<AfterSavingListener> iter =
|
745 |
this.afterSavingListeners.iterator();
|
746 |
|
747 |
while (iter.hasNext()) {
|
748 |
iter.next().afterSaving(evt); |
749 |
} |
750 |
|
751 |
} |
752 |
} |