Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / extension / ProjectExtension.java @ 38608

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