Statistics
| Revision:

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 @ 41106

History | View | Annotate | Download (22.3 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.lang.reflect.InvocationTargetException;
29
import java.text.MessageFormat;
30
import java.util.ArrayList;
31
import java.util.Iterator;
32
import java.util.List;
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
import org.apache.commons.io.FilenameUtils;
41
import org.gvsig.andami.IconThemeHelper;
42
import org.gvsig.andami.Launcher;
43
import org.gvsig.andami.Launcher.TerminationProcess;
44
import org.gvsig.andami.PluginServices;
45
import org.gvsig.andami.messages.NotificationManager;
46
import org.gvsig.andami.plugins.Extension;
47
import org.gvsig.andami.plugins.IExtension;
48
import org.gvsig.andami.plugins.status.IExtensionStatus;
49
import org.gvsig.andami.plugins.status.IUnsavedData;
50
import org.gvsig.andami.plugins.status.UnsavedData;
51
import org.gvsig.andami.ui.mdiManager.IWindow;
52
import org.gvsig.andami.ui.mdiManager.WindowInfo;
53
import org.gvsig.andami.ui.wizard.UnsavedDataPanel;
54
import org.gvsig.app.ApplicationLocator;
55
import org.gvsig.app.ApplicationManager;
56
import org.gvsig.app.project.Project;
57
import org.gvsig.app.project.ProjectManager;
58
import org.gvsig.app.project.documents.gui.ProjectWindow;
59
import org.gvsig.app.project.documents.view.ViewManager;
60
import org.gvsig.gui.beans.swing.JFileChooser;
61
import org.gvsig.tools.ToolsLocator;
62
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
63
import org.gvsig.tools.persistence.exception.PersistenceException;
64
import org.gvsig.utils.GenericFileFilter;
65
import org.gvsig.utils.save.AfterSavingListener;
66
import org.gvsig.utils.save.BeforeSavingListener;
67
import org.gvsig.utils.save.SaveEvent;
68
import org.gvsig.utils.swing.threads.IMonitorableTask;
69

    
70
/**
71
 * Extension que proporciona controles para crear proyectos nuevos, abrirlos y
72
 * guardarlos. Adem?s los tipos de tabla que soporta el proyecto son a?adidos en
73
 * esta clase.
74
 * 
75
 * @author Fernando Gonz?lez Cort?s
76
 */
77
public class ProjectExtension extends Extension implements IExtensionStatus {
78
        private static final Logger LOG = LoggerFactory
79
                        .getLogger(ProjectExtension.class);
80

    
81
        private static String projectPath = null;
82
        private ProjectWindow projectFrame;
83
        private Project p;
84
        private String lastSavePath;
85
        private WindowInfo seedProjectWindow;
86
        public static final String PROJECT_FILE_CHOOSER_ID = "PROJECT_FILECHOOSER_ID";
87
        /**
88
         * Use UTF-8 for encoding, as it can represent characters from any language.
89
         * 
90
         * Another sensible option would be encoding =
91
         * System.getProperty("file.encoding"); but this would need some extra
92
         * testing.
93
         * 
94
         * @deprecated see PersistentManager
95
         */
96
        public static String PROJECTENCODING = "UTF-8";
97

    
98
        private List<BeforeSavingListener> beforeSavingListeners = new ArrayList<BeforeSavingListener>();
99

    
100
        private List<AfterSavingListener> afterSavingListeners = new ArrayList<AfterSavingListener>();
101

    
102
        /**
103
         * @see com.iver.mdiApp.plugins.IExtension#initialize()
104
         */
105
        public void initialize() {
106
                // try {
107
                // Class.forName("javax.media.jai.EnumeratedParameter");
108
                // } catch (ClassNotFoundException e) {
109
                // NotificationManager
110
                // .addError(
111
                // "La m?quina virtual con la que se ejecuta gvSIG no tiene JAI instalado",
112
                // e);
113
                // }
114

    
115
                initializeDocumentActionsExtensionPoint();
116
                registerDocuments();
117
                registerIcons();
118
        }
119

    
120
        private void registerIcons() {
121
                IconThemeHelper.registerIcon("action", "application-project-new", this);
122
                IconThemeHelper
123
                                .registerIcon("action", "application-project-open", this);
124
                IconThemeHelper
125
                                .registerIcon("action", "application-project-save", this);
126
                IconThemeHelper.registerIcon("action", "application-project-save-as",
127
                                this);
128

    
129
                IconThemeHelper.registerIcon("project", "project-icon", this);
130
        }
131

    
132
        private void loadInitialProject() {
133
                String[] theArgs = PluginServices.getArguments();
134
                String lastArg = theArgs[theArgs.length - 1];
135
                if (lastArg != null) {
136
                        if (lastArg.toLowerCase().endsWith(
137
                                        Project.FILE_EXTENSION.toLowerCase())) {
138
                                LOG.info("Loading project '" + lastArg + "'.");
139
                                try {
140
                                        File projectFile = new File(lastArg);
141
                                        setProject(readProject(projectFile));
142
                                        PluginServices.getMainFrame().setTitle(
143
                                                        projectFile.getName());
144
                                        projectPath = projectFile.getAbsolutePath();
145
                                        return;
146
                                } catch (Exception ex) {
147
                                        LOG.warn("Can't load project from file '" + lastArg + "'.",
148
                                                        ex);
149
                                        ApplicationManager application = ApplicationLocator
150
                                                        .getManager();
151
                                        application.messageDialog("_Can_not_open_the_project",
152
                                                        "_Open_project", JOptionPane.WARNING_MESSAGE);
153
                                }
154
                        }
155
                }
156
                setProject(ProjectManager.getInstance().createProject());
157
                p.setName(PluginServices.getText(this, "untitled"));
158
                p.setModified(false);
159
                PluginServices.getMainFrame().setTitle(
160
                                PluginServices.getText(this, "sin_titulo"));
161
                setProject(p);
162
                showProjectWindow();
163
        }
164

    
165
        /**
166
         * @see com.iver.mdiApp.plugins.IExtension#postInitialize()
167
         */
168
        public void postInitialize() {
169
                try {
170
                        if( !SwingUtilities.isEventDispatchThread() ) {
171
                                SwingUtilities.invokeAndWait(new Runnable() {
172
                                        public void run() {
173
                                                loadInitialProject();
174
                                        }
175
                                });
176
                        } else {
177
                                loadInitialProject();
178
                        }
179
                } catch (Exception e) {
180
                        LOG.warn("Can't load initial project.",e);
181
                }
182
        }
183

    
184
        public ProjectWindow getProjectFrame() {
185
                if (projectFrame == null) {
186
                        projectFrame = new ProjectWindow();
187
                }
188
                return projectFrame;
189
        }
190

    
191
        /**
192
         * Muestra la ventana con el gestor de proyectos.
193
         */
194
        public void showProjectWindow() {
195
                if (seedProjectWindow != null) {
196
                        if (seedProjectWindow.isClosed()) {
197
                                // if it was closed, we just don't open the window now
198
                                seedProjectWindow.setClosed(false);
199
                                return;
200
                        }
201
                        WindowInfo winProps = seedProjectWindow;
202
                        seedProjectWindow = null;
203
                        PluginServices.getMDIManager().addWindow(getProjectFrame());
204
                        PluginServices.getMDIManager().changeWindowInfo(getProjectFrame(),
205
                                        winProps);
206
                } else {
207
                        PluginServices.getMDIManager().addWindow(getProjectFrame());
208
                }
209
        }
210

    
211
        /**
212
         * Muestra la ventana con el gestor de proyectos, con las propiedades de
213
         * ventana especificadas.
214
         */
215
        public void showProjectWindow(WindowInfo wi) {
216
                seedProjectWindow = wi;
217
                showProjectWindow();
218
        }
219

    
220
        /**
221
         * Guarda el proyecto actual en disco.
222
         */
223
        private boolean saveProject() {
224
                boolean saved = false;
225
                // if (p.getPath() == null) {
226
                if (projectPath == null) {
227
                        saved = saveAsProject(null);
228
                } else {
229
                        long t1, t2;
230
                        t1 = System.currentTimeMillis();
231
                        saved = writeProject(new File(projectPath), p, false);
232
                        t2 = System.currentTimeMillis();
233
                        PluginServices.getLogger().info(
234
                                        "Project saved. " + (t2 - t1) + " miliseconds");
235
                        getProjectFrame().refreshControls();
236
                }
237
                return saved;
238
        }
239

    
240
        private boolean saveAsProject(File file) {
241
                boolean saved = false;
242

    
243
                if (lastSavePath == null) {
244
                        lastSavePath = projectPath;
245
                }
246

    
247
                if (file == null) {
248
                        Preferences prefs = Preferences.userRoot().node("gvsig.foldering");
249
                        JFileChooser jfc = new JFileChooser(PROJECT_FILE_CHOOSER_ID,
250
                                        prefs.get("ProjectsFolder", null));
251

    
252
                        jfc.setDialogTitle(PluginServices.getText(this, "guardar_proyecto"));
253
                        jfc.addChoosableFileFilter(new GenericFileFilter(
254
                                        Project.FILE_EXTENSION, MessageFormat.format(PluginServices
255
                                                        .getText(this, "tipo_fichero_proyecto"),
256
                                                        Project.FILE_EXTENSION)));
257

    
258
                        if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) != JFileChooser.APPROVE_OPTION) {
259
                                return saved;
260
                        }
261
                        file = jfc.getSelectedFile();
262
                }
263

    
264
                if (!(file.getPath().toLowerCase().endsWith(Project.FILE_EXTENSION
265
                                .toLowerCase()))) {
266
                        file = new File(file.getPath() + Project.FILE_EXTENSION);
267
                }
268
                saved = writeProject(file, p);
269
                String filePath = file.getAbsolutePath();
270
                lastSavePath = filePath.substring(0,
271
                                filePath.lastIndexOf(File.separatorChar));
272

    
273
                getProjectFrame().refreshControls();
274
                return saved;
275
        }
276

    
277
        /**
278
         * Checks whether the project and related unsaved data is modified, and
279
         * allows the user to save it.
280
         * 
281
         * @return true if the data has been correctly saved, false otherwise
282
         */
283
        private boolean askSave() {
284
                if (p != null && p.hasChanged()) {
285
                        TerminationProcess process = Launcher.getTerminationProcess();
286
                        UnsavedDataPanel panel = process.getUnsavedDataPanel();
287
                        panel.setHeaderText(PluginServices.getText(this,
288
                                        "_Select_resources_to_save_before_closing_current_project"));
289
                        panel.setAcceptText(
290
                                        PluginServices.getText(this, "save_resources"),
291
                                        PluginServices
292
                                                        .getText(this,
293
                                                                        "Save_the_selected_resources_and_close_current_project"));
294
                        panel.setCancelText(PluginServices.getText(this, "Cancel"),
295
                                        PluginServices.getText(this, "Return_to_current_project"));
296
                        int closeCurrProj;
297
                        try {
298
                                closeCurrProj = process.manageUnsavedData();
299
                                if (closeCurrProj == JOptionPane.NO_OPTION) {
300
                                        // the user chose to return to current project
301
                                        return false;
302
                                }
303
                        } catch (Exception e) {
304
                                LOG.error("Some data can not be saved", e);
305
                        }
306
                }
307
                return true;
308
        }
309

    
310
        public void execute(String command) {
311
                this.execute(command, null);
312
        }
313

    
314
        public void execute(String actionCommand, Object[] args) {
315
                if (actionCommand.equals("application-project-new")) {
316
                        if (!askSave()) {
317
                                return;
318
                        }
319

    
320
                        projectPath = null;
321
                        // ProjectDocument.initializeNUMS();
322
                        PluginServices.getMDIManager().closeAllWindows();
323
                        setProject(ProjectManager.getInstance().createProject());
324
                        getProjectFrame().setProject(p);
325
                        showProjectWindow();
326
                        PluginServices.getMainFrame().setTitle(
327
                                        PluginServices.getText(this, "sin_titulo"));
328
                } else if (actionCommand.equals("application-project-open")) {
329
                        if (!askSave()) {
330
                                return;
331
                        }
332
                        File projectFile = null;
333

    
334
                        if (args != null && args.length > 0 && args[0] != null) {
335
                                if (args[0] instanceof File) {
336
                                        projectFile = (File) args[0];
337
                                } else if (args[0] instanceof String) {
338
                                        projectFile = new File((String) args[0]);
339
                                        if (!projectFile.exists()) {
340
                                                LOG.warn("Can't load project '"
341
                                                                + projectFile.getAbsolutePath()
342
                                                                + "', file not exist.");
343
                                                projectFile = null;
344
                                        }
345
                                }
346
                        }
347

    
348
                        if (projectFile == null) {
349
                                Preferences prefs = Preferences.userRoot().node(
350
                                                "gvsig.foldering");
351
                                JFileChooser jfc = new JFileChooser(PROJECT_FILE_CHOOSER_ID,
352
                                                prefs.get("ProjectsFolder", null));
353
                                jfc.addChoosableFileFilter(new GenericFileFilter(
354
                                                Project.FILE_EXTENSION, PluginServices.getText(this,
355
                                                                "tipo_fichero_proyecto")));
356

    
357
                                if (jfc.showOpenDialog((Component) PluginServices
358
                                                .getMainFrame()) != JFileChooser.APPROVE_OPTION) {
359
                                        return;
360
                                }
361
                                // ProjectDocument.initializeNUMS();
362
                                PluginServices.getMDIManager().closeAllWindows();
363

    
364
                                projectFile = jfc.getSelectedFile();
365
                        }
366

    
367
                        Project o = readProject(projectFile);
368
                        setPath(projectFile.getAbsolutePath());
369
                        // lastPath = getPath();
370
                        if (o != null) {
371
                                setProject(o);
372
                        }
373

    
374
                        getProjectFrame().setProject(p);
375
                        PluginServices.getMainFrame().setTitle(projectFile.getName());
376
                        getProjectFrame().refreshControls();
377

    
378
                        // p.restoreWindowProperties();
379

    
380
                } else if (actionCommand.equals("application-project-save")) {
381
                        saveProject();
382
                } else if (actionCommand.equals("application-project-save-as")) {
383
                        File file = null;
384
                        if (args != null && args.length > 0 && args[0] != null) {
385
                                if (args[0] instanceof File) {
386
                                        file = (File) args[0];
387
                                } else if (args[0] instanceof String) {
388
                                        file = new File((String) args[0]);
389
                                }
390
                        }
391
                        saveAsProject(file);
392
                }
393

    
394
        }
395

    
396
        /**
397
         * Escribe el proyecto en XML.
398
         * 
399
         * @param file
400
         *            Fichero.
401
         * @param p
402
         *            Proyecto.
403
         */
404
        public boolean writeProject(File file, Project p) {
405
                return writeProject(file, p, true);
406
        }
407

    
408
        /**
409
         * Escribe el proyecto en XML. Pero permite decidir si se pide confirmaci?n
410
         * para sobreescribir
411
         * 
412
         * @param file
413
         *            Fichero.
414
         * @param p
415
         *            Proyecto.
416
         * @param askConfirmation
417
         *            boolean
418
         */
419
        public boolean writeProject(File file, Project p, boolean askConfirmation) {
420
                if (askConfirmation && file.exists()) {
421
                        int resp = JOptionPane.showConfirmDialog((Component) PluginServices
422
                                        .getMainFrame(), PluginServices.getText(this,
423
                                        "fichero_ya_existe_seguro_desea_guardarlo"), PluginServices
424
                                        .getText(this, "guardar"), JOptionPane.YES_NO_OPTION);
425
                        if (resp != JOptionPane.YES_OPTION) {
426
                                return false;
427
                        }
428
                }
429
                NotificationManager.addInfo(PluginServices.getText(this,
430
                                "writinng_project") + ": " + file.getName());
431

    
432
                // write it out as XML
433
                try {
434
                        fireBeforeSavingFileEvent(new SaveEvent(this,
435
                                        SaveEvent.BEFORE_SAVING, file));
436
                        p.saveState(file);
437
                        fireAfterSavingFileEvent(new SaveEvent(this,
438
                                        SaveEvent.AFTER_SAVING, file));
439

    
440
                        PluginServices.getMainFrame().setTitle(file.getName());
441
                        setPath(file.toString());
442

    
443
                } catch (PersistenceException e) {
444
                        String messagestack = e.getLocalizedMessageStack();
445
                        NotificationManager.addError(
446
                                        PluginServices.getText(this, "error_writing_project")
447
                                                        + ": " + file.getName() + "\n" + messagestack, e);
448
                        return false;
449
                } catch (Exception e) {
450
                        NotificationManager.addError(
451
                                        PluginServices.getText(this, "error_writing_project")
452
                                                        + ": " + file.getName(), e);
453
                        return false;
454
                }
455
                NotificationManager.addInfo(PluginServices.getText(this,
456
                                "wrote_project") + ": " + file.getName());
457
                return true;
458
        }
459

    
460
        public Project readProject(String path) {
461
                Project project = ProjectManager.getInstance().createProject();
462

    
463
                project.loadState(new File(path));
464
                return (Project) project;
465
        }
466

    
467
        /**
468
         * Lee del XML el proyecto.<br>
469
         * <br>
470
         * 
471
         * Reads the XML of the project.<br>
472
         * It returns a project object holding all needed info that is not linked to
473
         * the Project Dialog. <br>
474
         * In case you want the project to be linked to the window you must set this
475
         * object to the extension:<br>
476
         * 
477
         * <b>Example:</b><br>
478
         * 
479
         * ...<br>
480
         * ...<br>
481
         * Project p = ProjectExtension.readProject(projectFile);<br>
482
         * ProjectExtension.setProject(p); ...<br>
483
         * ...<br>
484
         * 
485
         * @param file
486
         *            Fichero.
487
         * 
488
         * @return Project
489
         * 
490
         */
491
        public Project readProject(File file) {
492
                Project project = ProjectManager.getInstance().createProject();
493

    
494
                project.loadState(file);
495
                return (Project) project;
496
        }
497

    
498
        /**
499
         * Devuelve el proyecto.
500
         * 
501
         * @return Proyecto.
502
         */
503
        public Project getProject() {
504
                return p;
505
        }
506

    
507
        /**
508
         * @see org.gvsig.andami.plugins.IExtension#isEnabled()
509
         */
510
        public boolean isEnabled() {
511
                return true;
512
        }
513

    
514
        /**
515
         * @see org.gvsig.andami.plugins.IExtension#isVisible()
516
         */
517
        public boolean isVisible() {
518
                return true;
519
        }
520

    
521
        /**
522
         * Sets the project
523
         * 
524
         * @param p
525
         */
526
        public void setProject(Project p) {
527
                getProjectFrame().setProject(p);
528
                this.p = p;
529
        }
530

    
531
        private void registerDocuments() {
532
                ViewManager.register();
533
        }
534

    
535
        private void initializeDocumentActionsExtensionPoint() {
536
                ExtensionPointManager epMan = ToolsLocator.getExtensionPointManager();
537
                epMan.add(
538
                                "DocumentActions_View",
539
                                "Context menu options of the view document list"
540
                                                + " in the project window "
541
                                                + "(register instances of "
542
                                                + "org.gvsig.app.project.AbstractDocumentContextMenuAction)");
543
        }
544

    
545
        public static String getPath() {
546
                return projectPath;
547
        }
548

    
549
        public static void setPath(String path) {
550
                projectPath = path;
551
        }
552

    
553
        public IWindow getProjectWindow() {
554
                return getProjectFrame();
555
        }
556

    
557
        public IExtensionStatus getStatus() {
558
                return this;
559
        }
560

    
561
        public boolean hasUnsavedData() {
562
                return p.hasChanged();
563
        }
564

    
565
        public IUnsavedData[] getUnsavedData() {
566
                if (hasUnsavedData()) {
567
                        UnsavedProject data = new UnsavedProject(this);
568
                        IUnsavedData[] dataArray = { data };
569
                        return dataArray;
570
                } else {
571
                        return null;
572
                }
573
        }
574

    
575
        /**
576
         * Implements the IUnsavedData interface to show unsaved projects in the
577
         * Unsavad Data dialog.
578
         * 
579
         * @author Cesar Martinez Izquierdo <cesar.martinez@iver.es>
580
         */
581
        public class UnsavedProject extends UnsavedData {
582

    
583
                public UnsavedProject(IExtension extension) {
584
                        super(extension);
585
                }
586

    
587
                public String getDescription() {
588
                        if (getPath() == null) {
589
                                return PluginServices.getText(ProjectExtension.this,
590
                                                "Unnamed_new_gvsig_project_");
591
                        } else {
592
                                return PluginServices.getText(ProjectExtension.this,
593
                                                "Modified_project_");
594
                        }
595
                }
596

    
597
                public String getResourceName() {
598
                        if (getPath() == null) {
599
                                return PluginServices.getText(ProjectExtension.this, "Unnamed");
600
                        } else {
601
                                return getPath();
602
                        }
603

    
604
                }
605

    
606
                public boolean saveData() {
607
                        return saveProject();
608
                }
609

    
610
                public String getIcon() {
611
                        return "project-icon";
612
                }
613
        }
614

    
615
        public IMonitorableTask[] getRunningProcesses() {
616
                // TODO Auto-generated method stub
617
                return null;
618
        }
619

    
620
        public boolean hasRunningProcesses() {
621
                // TODO Auto-generated method stub
622
                return false;
623
        }
624

    
625
        /**
626
         * Adds the specified before saving listener to receive
627
         * "before saving file events" from this component. If l is null, no
628
         * exception is thrown and no action is performed.
629
         * 
630
         * @author Pablo Piqueras Bartolom? <pablo.piqueras@iver.es>
631
         * 
632
         * @param l
633
         *            the before saving listener.
634
         * @see SaveEvent
635
         * @see BeforeSavingListener
636
         * @see #removeListener(BeforeSavingListener)
637
         * @see #getBeforeSavingListeners
638
         */
639
        public synchronized void addListener(BeforeSavingListener l) {
640
                if (l == null) {
641
                        return;
642
                }
643
                if (!this.beforeSavingListeners.contains(l)) {
644
                        this.beforeSavingListeners.add(l);
645
                }
646
        }
647

    
648
        /**
649
         * Adds the specified after saving listener to receive
650
         * "after saving file events" from this component. If l is null, no
651
         * exception is thrown and no action is performed.
652
         * 
653
         * @author Pablo Piqueras Bartolom? <pablo.piqueras@iver.es>
654
         * 
655
         * @param l
656
         *            the after saving listener.
657
         * @see SaveEvent
658
         * @see AfterSavingListener
659
         * @see #removeListener(AfterSavingListener)
660
         * @see #getAfterSavingListeners()
661
         */
662
        public synchronized void addListener(AfterSavingListener l) {
663
                if (l == null) {
664
                        return;
665
                }
666

    
667
                if (!this.afterSavingListeners.contains(l)) {
668
                        this.afterSavingListeners.add(l);
669
                }
670

    
671
        }
672

    
673
        /**
674
         * Returns an array of all the before saving listeners registered on this
675
         * component.
676
         * 
677
         * @author Pablo Piqueras Bartolom? <pablo.piqueras@iver.es>
678
         * 
679
         * @return all of this component's <code>BeforeSavingListener</code>s or an
680
         *         empty array if no key listeners are currently registered
681
         * 
682
         * @see #addBeforeSavingListener(BeforeSavingListener)
683
         * @see #removeBeforeSavingListener(BeforeSavingListener)
684
         */
685
        public synchronized BeforeSavingListener[] getBeforeSavingListeners() {
686
                return this.beforeSavingListeners
687
                                .toArray(new BeforeSavingListener[] {});
688
        }
689

    
690
        /**
691
         * Returns an array of all the after saving listeners registered on this
692
         * component.
693
         * 
694
         * @author Pablo Piqueras Bartolom? <pablo.piqueras@iver.es>
695
         * 
696
         * @return all of this component's <code>AfterSavingListener</code>s or an
697
         *         empty array if no key listeners are currently registered
698
         * 
699
         * @see #addAfterSavingListener(AfterSavingListener)
700
         * @see #removeAfterSavingListener
701
         */
702
        public synchronized AfterSavingListener[] getAfterSavingListeners() {
703
                return this.afterSavingListeners.toArray(new AfterSavingListener[] {});
704

    
705
        }
706

    
707
        /**
708
         * Removes the specified before saving listener so that it no longer
709
         * receives save file events from this component. This method performs no
710
         * function, nor does it throw an exception, if the listener specified by
711
         * the argument was not previously added to this component. If listener
712
         * <code>l</code> is <code>null</code>, no exception is thrown and no action
713
         * is performed.
714
         * 
715
         * @author Pablo Piqueras Bartolom? <pablo.piqueras@iver.es>
716
         * 
717
         * @param l
718
         *            the before saving listener
719
         * @see SaveEvent
720
         * @see BeforeSavingListener
721
         * @see #addListener(BeforeSavingListener)
722
         * @see #getBeforeSavingListeners()
723
         */
724
        public synchronized void removeListener(BeforeSavingListener l) {
725
                if (l == null) {
726
                        return;
727
                }
728

    
729
                this.beforeSavingListeners.remove(l);
730
        }
731

    
732
        /**
733
         * Removes the specified after saving listener so that it no longer receives
734
         * save file events from this component. This method performs no function,
735
         * nor does it throw an exception, if the listener specified by the argument
736
         * was not previously added to this component. If listener <code>l</code> is
737
         * <code>null</code>, no exception is thrown and no action is performed.
738
         * 
739
         * @author Pablo Piqueras Bartolom? <pablo.piqueras@iver.es>
740
         * 
741
         * @param l
742
         *            the after saving listener
743
         * @see SaveEvent
744
         * @see AfterSavingListener
745
         * @see #addListener(AfterSavingListener)
746
         * @see #getAfterSavingListeners()
747
         */
748
        public synchronized void removeListener(AfterSavingListener l) {
749
                if (l == null) {
750
                        return;
751
                }
752

    
753
                this.afterSavingListeners.remove(l);
754
        }
755

    
756
        /**
757
         * Reports a before saving file event.
758
         * 
759
         * @author Pablo Piqueras Bartolom? <pablo.piqueras@iver.es>
760
         * 
761
         * @param evt
762
         *            the before saving file event
763
         */
764
        protected void fireBeforeSavingFileEvent(SaveEvent evt) {
765
                if ((evt.getID() != SaveEvent.BEFORE_SAVING) || (evt.getFile() == null)) {
766
                        return;
767
                }
768

    
769
                Iterator<BeforeSavingListener> iter = this.beforeSavingListeners
770
                                .iterator();
771

    
772
                while (iter.hasNext()) {
773
                        iter.next().beforeSaving(evt);
774
                }
775
        }
776

    
777
        /**
778
         * Reports a after saving file event.
779
         * 
780
         * @author Pablo Piqueras Bartolom? <pablo.piqueras@iver.es>
781
         * 
782
         * @param evt
783
         *            the after saving file event
784
         */
785
        protected void fireAfterSavingFileEvent(SaveEvent evt) {
786
                if ((evt.getID() != SaveEvent.AFTER_SAVING) || (evt.getFile() == null)) {
787
                        return;
788
                }
789
                Iterator<AfterSavingListener> iter = this.afterSavingListeners
790
                                .iterator();
791

    
792
                while (iter.hasNext()) {
793
                        iter.next().afterSaving(evt);
794
                }
795

    
796
        }
797
}