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

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
                        logger.warn("Can't stop layer editing.", ex);
145
                        /*
146
                         * Unable to end editing, inform user
147
                         * of the problem. Editing session will continue.
148
                         */
149
                        showUnableToEndEditingDialog(ex);
150
                        isStop = false;
151
                    }
152
                }
153
            }
154

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

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

    
197
    }
198

    
199
    private void showUnableToEndEditingDialog(Exception ex) {
200

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

    
210
    }
211

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
394
    }
395

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

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

    
403
        JLabel topLabel = new JLabel(translatedQuestion);
404

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

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

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

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

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

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

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

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

    
443

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

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

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

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

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

    
489
    }
490

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

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

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

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

    
511
            if ( layer.isWritable() ) {
512

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

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

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

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

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

    
550
                }
551

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

    
561
    private class UnsavedLayer extends UnsavedData {
562

    
563
        private FLayer layer;
564

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

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

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

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

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

    
584
        }
585

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

    
590
    }
591

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

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

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

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

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

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