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

History | View | Annotate | Download (25.5 KB)

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