Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.editing.app / org.gvsig.editing.app.mainplugin / src / main / java / org / gvsig / editing / StopEditing.java @ 41857

History | View | Annotate | Download (24.1 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA 02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.editing;
25

    
26
import java.awt.BorderLayout;
27
import java.awt.Component;
28
import java.awt.Font;
29
import java.awt.GridBagConstraints;
30
import java.awt.GridBagLayout;
31
import java.awt.Insets;
32
import java.util.ArrayList;
33
import java.util.Iterator;
34
import java.util.List;
35
import java.util.prefs.Preferences;
36

    
37
import javax.swing.JLabel;
38
import javax.swing.JOptionPane;
39
import javax.swing.JPanel;
40

    
41
import org.cresques.cts.IProjection;
42
import org.slf4j.Logger;
43
import org.slf4j.LoggerFactory;
44
import org.gvsig.andami.PluginServices;
45
import org.gvsig.andami.PluginsLocator;
46
import org.gvsig.andami.messages.NotificationManager;
47
import org.gvsig.andami.plugins.Extension;
48
import org.gvsig.andami.plugins.IExtension;
49
import org.gvsig.andami.plugins.IExtensionExecuteWithArgs;
50
import org.gvsig.andami.plugins.status.IExtensionStatus;
51
import org.gvsig.andami.plugins.status.IUnsavedData;
52
import org.gvsig.andami.plugins.status.UnsavedData;
53
import org.gvsig.app.ApplicationLocator;
54
import org.gvsig.app.gui.preferencespage.GridPage;
55
import org.gvsig.app.project.Project;
56
import org.gvsig.app.project.ProjectManager;
57
import org.gvsig.app.project.documents.view.DefaultViewDocument;
58
import org.gvsig.app.project.documents.view.ViewDocument;
59
import org.gvsig.app.project.documents.view.ViewManager;
60
import org.gvsig.app.project.documents.view.gui.DefaultViewPanel;
61
import org.gvsig.editing.gui.preferences.EditingPage;
62
import org.gvsig.editing.layers.ILayerEdited;
63
import org.gvsig.editing.layers.VectorialLayerEdited;
64
import org.gvsig.exportto.app.extension.ExporttoLayerExtension;
65
import org.gvsig.fmap.dal.exception.DataException;
66
import org.gvsig.fmap.dal.exception.ReadException;
67
import org.gvsig.fmap.mapcontext.MapContext;
68
import org.gvsig.fmap.mapcontext.layers.FLayer;
69
import org.gvsig.fmap.mapcontext.layers.FLayers;
70
import org.gvsig.fmap.mapcontext.layers.LayersIterator;
71
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
72
import org.gvsig.fmap.mapcontrol.MapControl;
73
import org.gvsig.fmap.mapcontrol.MapControlLocator;
74
import org.gvsig.fmap.mapcontrol.MapControlManager;
75
import org.gvsig.fmap.mapcontrol.tools.snapping.snappers.ISnapper;
76
import org.gvsig.i18n.Messages;
77
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
78
import org.gvsig.utils.swing.threads.IMonitorableTask;
79

    
80
/**
81
 * @author Francisco Jos?
82
 *
83
 * Cuando un tema se pone en edici?n, puede que su driver implemente
84
 * ISpatialWriter. En ese caso, es capaz de guardarse sobre s? mismo. Si
85
 * no lo implementa, esta opci?n estar? deshabilitada y la ?nica
86
 * posibilidad de guardar este tema ser? "Guardando como..."
87
 *
88
 */
89
public class StopEditing extends Extension implements IExtensionExecuteWithArgs {
90

    
91
    private final static Logger logger = LoggerFactory.getLogger(StopEditing.class);
92

    
93
    private DefaultViewPanel vista;
94

    
95
    /**
96
     * @see org.gvsig.andami.plugins.IExtension#initialize()
97
     */
98
    public void initialize() {
99
    }
100

    
101
    public void execute(String s) {
102
        this.execute(s, null);
103
    }
104

    
105
    public void execute(String s, Object[] args) {
106
        org.gvsig.andami.ui.mdiManager.IWindow f = PluginServices
107
                .getMDIManager().getActiveWindow();
108

    
109
        vista = (DefaultViewPanel) f;
110
        boolean isStop = false;
111
        ViewDocument model = vista.getModel();
112
        MapContext mapa = model.getMapContext();
113
        FLayers layers = mapa.getLayers();
114
        IEditionManager edMan = EditionLocator.getEditionManager();
115

    
116
        if ( s.equals("layer-stop-editing") ) {
117

    
118
            FLayer[] selectedItems = null;
119
            // Desde el menu contextual del TOC se le pasan las capas seleccionadas como primer argumento.
120
            if ( args != null && args.length > 0 && args[0] instanceof FLayer[] ) {
121
                selectedItems = (FLayer[]) args[0];
122
            }
123

    
124
            vista.getMapControl().getCanceldraw().setCanceled(true);
125
            FLayer[] actives = layers.getActives();
126
                        // TODO: Comprobar que solo hay una activa, o al menos
127
            // que solo hay una en edici?n que est? activa, etc, etc
128
            for ( int i = 0; i < actives.length; i++ ) {
129
                if ( actives[i] instanceof FLyrVect && actives[i].isEditing() ) {
130
                    FLyrVect lv = (FLyrVect) actives[i];
131
                    MapControl mapControl = vista.getMapControl();
132

    
133
                    int user_opt = confirmStop(lv, mapControl);
134
                    if ( user_opt != IEditionManager.CONTINUE_EDITING ) {
135
                        isStop = true;
136
                    }
137

    
138
                    try {
139
                        edMan.stopEditLayer(vista, lv, user_opt);
140
                    } catch (CancelException ex) {
141
                        // Do nothing
142
                        isStop = false;
143
                    } catch (Exception ex) {
144

    
145
                        /*
146
                         * Unable to end editing, inform user
147
                         * of the problem. Editing session will continue.
148
                         */
149
                        showUnableToEndEditingDialog(ex);
150
                        isStop = false;
151
                        logger.info("While stopping layer editing.", ex);
152
                    }
153
                }
154
            }
155

    
156
            if ( isStop ) {
157
                CADExtension.clearView();
158
                /*
159
                 * Make grid not visible and remove snapping to grid
160
                 * and disable snappers
161
                 */
162
                disableGridAndSnappers(vista.getMapControl());
163
            }
164
        }
165
        PluginServices.getMainFrame().enableControls();
166
    }
167

    
168
    private void disableGridAndSnappers(MapControl mco) {
169
        mco.setGridVisibility(false);
170
        mco.setAdjustGrid(false);
171
        Preferences prefs = GridPage.prefs;
172
        prefs.putBoolean("grid.showgrid", false);
173
        prefs.getBoolean("grid.adjustgrid", false);
174
        // Snapping to geometries disabled
175
        mco.setRefentEnabled(false);
176
        /*
177
         * Remove all layers to snap to (unless they are being edited).
178
         * If we don't do this, geometries will still be added
179
         * to the spatial cache of those layers when editing session ends 
180
         */
181
        List removelist = new ArrayList();
182
        FLyrVect item = null;
183
        Iterator snapiter = mco.getMapContext().getLayersToSnap().iterator();
184
        while ( snapiter.hasNext() ) {
185
            item = (FLyrVect) snapiter.next();
186
            if ( !item.isEditing() ) {
187
                /*
188
                 * Get list of layers to be removed from the list
189
                 */
190
                removelist.add(item);
191
            }
192
        }
193
        /*
194
         * Remove layers from list
195
         */
196
        mco.getMapContext().getLayersToSnap().removeAll(removelist);
197

    
198
    }
199

    
200
    private void showUnableToEndEditingDialog(Exception ex) {
201

    
202
        String msg = Messages.getText(
203
                "_Unable_to_save_edits_The_cause_is");
204
        msg = msg + ":\n\n" + EditionUtilities.getLastMessage(ex);
205
        JOptionPane.showMessageDialog(
206
                ApplicationLocator.getManager().getRootComponent(),
207
                msg,
208
                Messages.getText("_Guardar"),
209
                JOptionPane.ERROR_MESSAGE);
210

    
211
    }
212

    
213
    /**
214
     * @see org.gvsig.andami.plugins.IExtension#isEnabled()
215
     */
216
    public boolean isEnabled() {
217
        FLayer[] lyrs = EditionUtilities.getActiveAndEditedLayers();
218
        if ( lyrs == null ) {
219
            return false;
220
        }
221
        FLyrVect lyrVect = (FLyrVect) lyrs[0];
222
        if ( lyrVect.isEditing() ) {
223
            return true;
224
        }
225
        return false;
226
    }
227

    
228
    /**
229
     * DOCUMENT ME!
230
     */
231
    /*
232
     public boolean stopEditing(FLyrVect layer, MapControl mapControl) {
233
     int resp = JOptionPane.CANCEL_OPTION;
234

235
     try {
236
     if (layer.isWritable()) {
237
     Object[] options = {
238
     PluginServices.getText(this, "_Guardar"),
239
     "       " + PluginServices.getText(this, "_Descartar") + "       ",
240
     PluginServices.getText(this, "_Continuar") };
241
                                
242
     JPanel explanation_panel = getExplanationPanel(layer.getName());
243

244
     resp = JOptionPane
245
     .showOptionDialog(
246
     (Component) PluginServices.getMainFrame(),
247
     explanation_panel,
248
     PluginServices.getText(this, "stop_edition"),
249
     JOptionPane.YES_NO_CANCEL_OPTION,
250
     JOptionPane.QUESTION_MESSAGE, null, options,
251
     options[2]);
252
                                
253
     if (resp == JOptionPane.YES_OPTION) {
254
     // SAVE
255
     saveLayer(layer);
256
     // layer.setEditing(false);
257
     return true;
258
     } else if (resp == JOptionPane.NO_OPTION) {
259
     // CANCEL EDITING
260
     cancelEdition(layer);
261
     // layer.setEditing(false);
262
     return true;
263
     } else if (resp == JOptionPane.CANCEL_OPTION) {
264
     // CONTINUE EDITING
265
     return false;
266
     } else {
267
     // This happens when user clicks on [x]
268
     // to abruptly close previous JOptionPane dialog
269
     // We make it equivalent to CONTINUE EDITING
270
     return false;
271
     }
272
                                
273
     }
274
     // ========================================
275
     // Layer cannot save changes:
276
                        
277
     Object[] options = {
278
     PluginServices.getText(this, "export"),
279
     PluginServices.getText(this,
280
     "finish_editing_without_saving_changes"),
281
     PluginServices.getText(this, "continue_editing") };
282

283
     resp = JOptionPane
284
     .showOptionDialog(
285
     (Component) PluginServices.getMainFrame(),
286
     PluginServices
287
     .getText(
288
     this,
289
     "no_existe_writer_para_este_formato_de_capa_o_no_tiene_permisos_de_escritura_los_datos_que_desea_hacer")
290
     + " : " + layer.getName(), PluginServices
291
     .getText(this, "stop_edition"),
292
     JOptionPane.YES_NO_CANCEL_OPTION,
293
     JOptionPane.QUESTION_MESSAGE, null, options,
294
     options[2]);
295
     if (resp == JOptionPane.NO_OPTION) { // CANCEL EDITING
296
     cancelEdition(layer);
297
     // layer.setEditing(false);
298
     return true;
299
     } else if (resp == JOptionPane.YES_OPTION) {
300
     int status = exportLayer(layer);
301
     if (status == JOptionPane.OK_OPTION) {
302
     cancelEdition(layer);
303
     // layer.setEditing(false);
304
     }
305
     }
306

307
     } catch (ReadException e) {
308
     NotificationManager.addError(e);
309
     } catch (CancelEditingLayerException e) {
310
     NotificationManager.addError(e);
311
     }
312
     return false;
313

314
     }
315
     */
316
    public int confirmStop(FLyrVect layer, MapControl mapControl) {
317
        int resp = JOptionPane.CANCEL_OPTION;
318

    
319
        try {
320
            if ( layer.isWritable() ) {
321
                Object[] options = {
322
                    PluginServices.getText(this, "_Guardar"),
323
                    "       " + PluginServices.getText(this, "_Descartar") + "       ",
324
                    PluginServices.getText(this, "_Continuar")};
325

    
326
                String question = Messages.getText("realmente_desea_guardar_la_capa");
327
                question = question + " '" + layer.getName() + "'?";
328
                String firstLabel = Messages.getText("_Guardar");
329
                String firstDesc = Messages.getText("_Save_changes_performed");
330

    
331
                JPanel explanation_panel = getExplanationPanel(
332
                        question, firstLabel, firstDesc);
333

    
334
                resp = JOptionPane
335
                        .showOptionDialog(
336
                                (Component) PluginServices.getMainFrame(),
337
                                explanation_panel,
338
                                PluginServices.getText(this, "stop_edition"),
339
                                JOptionPane.YES_NO_CANCEL_OPTION,
340
                                JOptionPane.QUESTION_MESSAGE, null, options,
341
                                options[2]);
342

    
343
                if ( resp == JOptionPane.YES_OPTION ) {
344
                    return IEditionManager.ACCEPT_EDITING;
345
                } else if ( resp == JOptionPane.NO_OPTION ) {
346
                    return IEditionManager.CANCEL_EDITING;
347
                } else if ( resp == JOptionPane.CANCEL_OPTION ) {
348
                    return IEditionManager.CONTINUE_EDITING;
349
                } else {
350
                            // This happens when user clicks on [x]
351
                    // to abruptly close previous JOptionPane dialog
352
                    // We make it equivalent to CONTINUE EDITING
353
                    return IEditionManager.CONTINUE_EDITING;
354
                }
355

    
356
            }
357
                    // ========================================
358
            // Layer cannot save changes:
359

    
360
            Object[] options = {
361
                PluginServices.getText(this, "export"),
362
                "       " + PluginServices.getText(this, "_Descartar") + "       ",
363
                PluginServices.getText(this, "_Continuar")};
364

    
365
            String question = Messages.getText(
366
                    "no_existe_writer_para_este_formato_de_capa_o_no_tiene_permisos_de_escritura_los_datos_que_desea_hacer");
367
            String firstLabel = Messages.getText("export");
368
            String firstDesc = Messages.getText("_Export_to_another_format");
369

    
370
            JPanel explanation_panel = getExplanationPanel(
371
                    question, firstLabel, firstDesc);
372

    
373
            resp = JOptionPane
374
                    .showOptionDialog(
375
                            (Component) PluginServices.getMainFrame(),
376
                            explanation_panel,
377
                            PluginServices.getText(this, "stop_edition"),
378
                            JOptionPane.YES_NO_CANCEL_OPTION,
379
                            JOptionPane.QUESTION_MESSAGE, null, options,
380
                            options[2]);
381
            if ( resp == JOptionPane.NO_OPTION ) { // CANCEL EDITING
382
                return IEditionManager.CANCEL_EDITING;
383
            } else if ( resp == JOptionPane.YES_OPTION ) {
384
                int status = exportLayer(layer);
385
                if ( status == JOptionPane.OK_OPTION ) {
386
                    return IEditionManager.CANCEL_EDITING;
387
                }
388
            }
389

    
390
        } catch (ReadException e) {
391
            NotificationManager.addError(e);
392
        }
393
        return IEditionManager.CONTINUE_EDITING;
394

    
395
    }
396

    
397
    private JPanel getExplanationPanel(
398
            String translatedQuestion,
399
            String translatedFirstTag,
400
            String translatedFirstDescription) {
401

    
402
        JPanel resp = new JPanel(new BorderLayout(10, 10));
403

    
404
        JLabel topLabel = new JLabel(translatedQuestion);
405

    
406
        JPanel mainPanel = new JPanel(new GridBagLayout());
407
        GridBagConstraints cc = new GridBagConstraints();
408

    
409
        cc.gridx = 0;
410
        cc.gridy = 0;
411
        cc.anchor = GridBagConstraints.WEST;
412

    
413
        cc.insets = new Insets(3, 6, 3, 6);
414

    
415
        Font boldf = mainPanel.getFont().deriveFont(Font.BOLD);
416

    
417
        JLabel lbl = new JLabel(translatedFirstTag);
418
        lbl.setFont(boldf);
419
        mainPanel.add(lbl, cc);
420
        cc.gridx = 1;
421
        mainPanel.add(new JLabel(translatedFirstDescription), cc);
422

    
423
        cc.gridx = 0;
424
        cc.gridy = 1;
425
        lbl = new JLabel(Messages.getText("_Descartar"));
426
        lbl.setFont(boldf);
427
        mainPanel.add(lbl, cc);
428
        cc.gridx = 1;
429
        mainPanel.add(new JLabel(Messages.getText("_Discard_and_lose_changes")), cc);
430

    
431
        cc.gridx = 0;
432
        cc.gridy = 2;
433
        lbl = new JLabel(Messages.getText("_Continuar"));
434
        lbl.setFont(boldf);
435
        mainPanel.add(lbl, cc);
436
        cc.gridx = 1;
437
        mainPanel.add(new JLabel(Messages.getText("_Do_not_save_yet_Stay_in_editing_mode")), cc);
438

    
439
        resp.add(mainPanel, BorderLayout.CENTER);
440
        resp.add(topLabel, BorderLayout.NORTH);
441
        return resp;
442
    }
443

    
444

    
445
    /*
446
     private void saveLayer(FLyrVect layer) throws ReadException {
447
     FeatureStore featureStore = layer.getFeatureStore();
448
     try {
449
     featureStore.finishEditing();
450
     } catch (WriteException e) {
451
     throw new ReadException(featureStore.getName(), e);
452
     } catch (DataException e) {
453
     throw new ReadException(featureStore.getName(), e);
454
     }
455
     }
456

457
     private void cancelEdition(FLyrVect layer)
458
     throws CancelEditingLayerException {
459
     FeatureStore featureStore = null;
460
     try {
461
     featureStore = layer.getFeatureStore();
462

463
     featureStore.cancelEditing();
464
     } catch (ReadException e) {
465
     throw new CancelEditingLayerException(layer.getName(), e);
466
     } catch (DataException e) {
467
     throw new CancelEditingLayerException(layer.getName(), e);
468
     }
469
     }
470
     */
471
    private int exportLayer(FLyrVect layer) throws ReadException {
472
        ViewDocument model = vista.getModel();
473
        MapContext mapContext = model.getMapContext();
474
        IProjection projection = mapContext.getProjection();
475

    
476
        ExporttoLayerExtension extension = (ExporttoLayerExtension) PluginsLocator
477
                .getManager().getExtension(ExporttoLayerExtension.class);
478
        return extension.showExportto(layer, projection, mapContext, WindowManager.MODE.DIALOG);
479
    }
480

    
481
    /**
482
     * @see org.gvsig.andami.plugins.IExtension#isVisible()
483
     */
484
    public boolean isVisible() {
485
        if ( EditionUtilities.getEditionStatus() == EditionUtilities.EDITION_STATUS_ONE_VECTORIAL_LAYER_ACTIVE_AND_EDITABLE ) {
486
            return true;
487
        }
488
        return false;
489

    
490
    }
491

    
492
    public IExtensionStatus getStatus() {
493
        return new StopEditingStatus();
494
    }
495

    
496
    /**
497
     * Show the dialogs to save the layer without ask if don't like to save.
498
     *
499
     * @param layer
500
     * Layer to save.
501
     */
502
    public boolean executeSaveLayer(FLyrVect layer) {
503

    
504
        IEditionManager edMan = EditionLocator.getEditionManager(layer);
505

    
506
        VectorialLayerEdited lyrEd = (VectorialLayerEdited) edMan
507
                .getLayerEdited(layer);
508
        boolean isStop = false;
509
        try {
510
            lyrEd.clearSelection();
511

    
512
            if ( layer.isWritable() ) {
513

    
514
                try {
515
                    edMan.stopEditLayer(null, layer, IEditionManager.ACCEPT_EDITING);
516
                    isStop = true;
517
                } catch (CancelException ex) {
518
                    isStop = false;
519

    
520
                } catch (Exception ex) {
521
                    logger.error("While stopping layer editing.", ex);
522
                    isStop = false;
523
                }
524

    
525
            } else {
526
                // Si no existe writer para la capa que tenemos en edici?n
527
                int resp = JOptionPane
528
                        .showConfirmDialog(
529
                                (Component) PluginServices.getMainFrame(),
530
                                PluginServices
531
                                .getText(
532
                                        this,
533
                                        "no_existe_writer_para_este_formato_de_capa_o_no_tiene_permisos_de_escritura_los_datos_no_se_guardaran_desea_continuar")
534
                                + " : " + layer.getName(),
535
                                PluginServices
536
                                .getText(this, "cancelar_edicion"),
537
                                JOptionPane.YES_NO_OPTION);
538
                if ( resp == JOptionPane.YES_OPTION ) { // CANCEL EDITING
539

    
540
                    try {
541
                        edMan.stopEditLayer(null, layer, IEditionManager.CANCEL_EDITING);
542
                        isStop = true;
543
                    } catch (CancelException ex) {
544
                        isStop = false;
545

    
546
                    } catch (Exception ex) {
547
                        isStop = false;
548
                        logger.error("While stopping layer editing.", ex);
549
                    }
550

    
551
                }
552

    
553
            }
554
        } catch (ReadException e1) {
555
            NotificationManager.showMessageError(e1.getMessage(), e1);
556
        } catch (DataException e) {
557
            NotificationManager.showMessageError(e.getMessage(), e);
558
        }
559
        return isStop;
560
    }
561

    
562
    private class UnsavedLayer extends UnsavedData {
563

    
564
        private FLayer layer;
565

    
566
        public UnsavedLayer(IExtension extension) {
567
            super(extension);
568
        }
569

    
570
        public String getDescription() {
571
            return PluginServices.getText(this, "editing_layer_unsaved");
572
        }
573

    
574
        public String getResourceName() {
575
            return layer.getName();
576
        }
577

    
578
        public boolean saveData() {
579
            return executeSaveLayer((FLyrVect) layer);
580
        }
581

    
582
        public void setLayer(FLayer layer) {
583
            this.layer = layer;
584

    
585
        }
586

    
587
        public String getIcon() {
588
            return layer.getTocImageIcon();
589
        }
590

    
591
    }
592

    
593
    /**
594
     * <p>
595
     * This class provides the status of extensions. If this extension has some
596
     * unsaved editing layer (and save them), and methods to check if the
597
     * extension has some associated background tasks.
598
     *
599
     * @author Vicente Caballero Navarro
600
     *
601
     */
602
    private class StopEditingStatus implements IExtensionStatus {
603

    
604
        /**
605
         * This method is used to check if this extension has some unsaved
606
         * editing layer.
607
         *
608
         * @return true if the extension has some unsaved editing layer, false
609
         * otherwise.
610
         */
611
        public boolean hasUnsavedData() {
612
            Project project = ProjectManager.getInstance().getCurrentProject();
613
            DefaultViewDocument[] views = project.getDocuments(
614
                    ViewManager.TYPENAME).toArray(new DefaultViewDocument[0]);
615
            for ( int i = 0; i < views.length; i++ ) {
616
                FLayers layers = views[i].getMapContext().getLayers();
617
                LayersIterator iter = getEditingLayer(layers);
618
                if ( iter.hasNext() ) {
619
                    return true;
620
                }
621
            }
622
            return false;
623
        }
624

    
625
        /**
626
         * This method is used to check if the extension has some associated
627
         * background process which is currently running.
628
         *
629
         * @return true if the extension has some associated background process,
630
         * false otherwise.
631
         */
632
        public boolean hasRunningProcesses() {
633
            return false;
634
        }
635

    
636
        /**
637
         * <p>
638
         * Gets an array of the traceable background tasks associated with this
639
         * extension. These tasks may be tracked, canceled, etc.
640
         * </p>
641
         *
642
         * @return An array of the associated background tasks, or null in case
643
         * there is no associated background tasks.
644
         */
645
        public IMonitorableTask[] getRunningProcesses() {
646
            return null;
647
        }
648

    
649
        /**
650
         * <p>
651
         * Gets an array of the UnsavedData objects, which contain information
652
         * about the unsaved editing layers and allows to save it.
653
         * </p>
654
         *
655
         * @return An array of the associated unsaved editing layers, or null in
656
         * case the extension has not unsaved editing layers.
657
         */
658
        public IUnsavedData[] getUnsavedData() {
659
            Project project = ProjectManager.getInstance().getCurrentProject();
660
            DefaultViewDocument[] views = project.getDocuments(
661
                    ViewManager.TYPENAME).toArray(new DefaultViewDocument[0]);
662
            ArrayList unsavedLayers = new ArrayList();
663
            for ( int i = 0; i < views.length; i++ ) {
664
                FLayers layers = views[i].getMapContext().getLayers();
665
                LayersIterator iter = getEditingLayer(layers);
666
                while ( iter.hasNext() ) {
667
                    UnsavedLayer ul = new UnsavedLayer(StopEditing.this);
668
                    ul.setLayer(iter.nextLayer());
669
                    unsavedLayers.add(ul);
670
                }
671
            }
672
            return (IUnsavedData[]) unsavedLayers.toArray(new IUnsavedData[0]);
673
        }
674
    }
675

    
676
    private LayersIterator getEditingLayer(FLayers layers) {
677
        return new LayersIterator(layers) {
678
            public boolean evaluate(FLayer layer) {
679
                return layer.isEditing();
680
            }
681
        };
682
    }
683
}