Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / extension / ProjectExtension.java @ 39104

History | View | Annotate | Download (24.4 KB)

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