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

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

    
202
        
203

    
204
        private void showUnableToEndEditingDialog(Exception ex) {
205
                
206
                String msg = Messages.getText(
207
                                "_Unable_to_save_edits_The_cause_is");
208
                msg = msg + ":\n\n" + EditionUtilities.getLastMessage(ex);
209
                JOptionPane.showMessageDialog(
210
                                ApplicationLocator.getManager().getRootComponent(),
211
                                msg,
212
                                Messages.getText("_Guardar"),
213
                                JOptionPane.ERROR_MESSAGE);
214
                
215
        }
216

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

    
232
        /**
233
         * DOCUMENT ME!
234
         */
235
        
236
        /*
237
        public boolean stopEditing(FLyrVect layer, MapControl mapControl) {
238
                int resp = JOptionPane.CANCEL_OPTION;
239

240
                try {
241
                        if (layer.isWritable()) {
242
                                Object[] options = {
243
                                                PluginServices.getText(this, "_Guardar"),
244
                                                "       " + PluginServices.getText(this, "_Descartar") + "       ",
245
                                                PluginServices.getText(this, "_Continuar") };
246
                                
247
                                JPanel explanation_panel = getExplanationPanel(layer.getName());
248

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

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

312
                } catch (ReadException e) {
313
                        NotificationManager.addError(e);
314
                } catch (CancelEditingLayerException e) {
315
                        NotificationManager.addError(e);
316
                }
317
                return false;
318

319
        }
320
        */
321

    
322
        
323
           public int confirmStop(FLyrVect layer, MapControl mapControl) {
324
                int resp = JOptionPane.CANCEL_OPTION;
325

    
326
                try {
327
                    if (layer.isWritable()) {
328
                        Object[] options = {
329
                                PluginServices.getText(this, "_Guardar"),
330
                                "       " + PluginServices.getText(this, "_Descartar") + "       ",
331
                                PluginServices.getText(this, "_Continuar") };
332
                        
333
                        
334
                        String question = Messages.getText("realmente_desea_guardar_la_capa");
335
                        question = question + " '" + layer.getName() + "'?";
336
                        String firstLabel = Messages.getText("_Guardar");
337
                        String firstDesc = Messages.getText("_Save_changes_performed");
338
                        
339
                        JPanel explanation_panel = getExplanationPanel(
340
                            question, firstLabel, firstDesc);
341

    
342
                        resp = JOptionPane
343
                                .showOptionDialog(
344
                                        (Component) PluginServices.getMainFrame(),
345
                                        explanation_panel,
346
                                        PluginServices.getText(this, "stop_edition"),
347
                                        JOptionPane.YES_NO_CANCEL_OPTION,
348
                                        JOptionPane.QUESTION_MESSAGE, null, options,
349
                                        options[2]);
350
                        
351
                        if (resp == JOptionPane.YES_OPTION) {
352
                            return IEditionManager.ACCEPT_EDITING;
353
                        } else if (resp == JOptionPane.NO_OPTION) {
354
                        return IEditionManager.CANCEL_EDITING;
355
                        } else if (resp == JOptionPane.CANCEL_OPTION) {
356
                        return IEditionManager.CONTINUE_EDITING;
357
                        } else {
358
                            // This happens when user clicks on [x]
359
                            // to abruptly close previous JOptionPane dialog
360
                            // We make it equivalent to CONTINUE EDITING
361
                        return IEditionManager.CONTINUE_EDITING;
362
                        }
363
                        
364
                    }
365
                    // ========================================
366
                    // Layer cannot save changes:
367
                    
368
                Object[] options = {
369
                    PluginServices.getText(this, "export"),
370
                    "       " + PluginServices.getText(this, "_Descartar") + "       ",
371
                    PluginServices.getText(this, "_Continuar") };
372
                
373
                String question = Messages.getText(
374
                    "no_existe_writer_para_este_formato_de_capa_o_no_tiene_permisos_de_escritura_los_datos_que_desea_hacer");
375
                String firstLabel = Messages.getText("export");
376
                String firstDesc = Messages.getText("_Export_to_another_format");
377
                
378
                JPanel explanation_panel = getExplanationPanel(
379
                    question, firstLabel, firstDesc);
380

    
381
                    resp = JOptionPane
382
                            .showOptionDialog(
383
                                    (Component) PluginServices.getMainFrame(),
384
                                    explanation_panel,
385
                                    PluginServices.getText(this, "stop_edition"),
386
                                    JOptionPane.YES_NO_CANCEL_OPTION,
387
                                    JOptionPane.QUESTION_MESSAGE, null, options,
388
                                    options[2]);
389
                    if (resp == JOptionPane.NO_OPTION) { // CANCEL EDITING
390
                    return IEditionManager.CANCEL_EDITING;
391
                    } else if (resp == JOptionPane.YES_OPTION) {
392
                        int status = exportLayer(layer);
393
                        if (status == JOptionPane.OK_OPTION) {
394
                            return IEditionManager.CANCEL_EDITING;
395
                        }
396
                    }
397

    
398
                } catch (ReadException e) {
399
                    NotificationManager.addError(e);
400
                }
401
            return IEditionManager.CONTINUE_EDITING;
402

    
403

    
404
            }
405

    
406

    
407
    private JPanel getExplanationPanel(
408
        String translatedQuestion,
409
        String translatedFirstTag,
410
        String translatedFirstDescription) {
411
        
412
        JPanel resp = new JPanel(new BorderLayout(10, 10));
413
        
414
        JLabel topLabel = new JLabel(translatedQuestion);
415
        
416
        JPanel mainPanel = new JPanel(new GridBagLayout());
417
        GridBagConstraints cc = new GridBagConstraints();
418
        
419
        cc.gridx = 0;
420
        cc.gridy = 0;
421
        cc.anchor = GridBagConstraints.WEST;
422
        
423
        cc.insets = new Insets(3, 6, 3, 6);
424
        
425
        Font boldf = mainPanel.getFont().deriveFont(Font.BOLD);
426
        
427
        JLabel lbl = new JLabel(translatedFirstTag);
428
        lbl.setFont(boldf);
429
        mainPanel.add(lbl, cc);
430
        cc.gridx = 1;
431
        mainPanel.add(new JLabel(translatedFirstDescription), cc);
432
        
433
        cc.gridx = 0;
434
        cc.gridy = 1;
435
        lbl = new JLabel(Messages.getText("_Descartar"));
436
        lbl.setFont(boldf);
437
        mainPanel.add(lbl, cc);
438
        cc.gridx = 1;
439
        mainPanel.add(new JLabel(Messages.getText("_Discard_and_lose_changes")), cc);
440

    
441
        cc.gridx = 0;
442
        cc.gridy = 2;
443
        lbl = new JLabel(Messages.getText("_Continuar"));
444
        lbl.setFont(boldf);
445
        mainPanel.add(lbl, cc);
446
        cc.gridx = 1;
447
        mainPanel.add(new JLabel(Messages.getText("_Do_not_save_yet_Stay_in_editing_mode")), cc);
448

    
449
        resp.add(mainPanel, BorderLayout.CENTER);
450
        resp.add(topLabel, BorderLayout.NORTH);
451
        return resp;
452
    }
453
    
454

    
455
    /*
456
    private void saveLayer(FLyrVect layer) throws ReadException {
457
                FeatureStore featureStore = layer.getFeatureStore();
458
                try {
459
                        featureStore.finishEditing();
460
                } catch (WriteException e) {
461
                        throw new ReadException(featureStore.getName(), e);
462
                } catch (DataException e) {
463
                        throw new ReadException(featureStore.getName(), e);
464
                }
465
        }
466

467
        private void cancelEdition(FLyrVect layer)
468
                        throws CancelEditingLayerException {
469
                FeatureStore featureStore = null;
470
                try {
471
                        featureStore = layer.getFeatureStore();
472

473
                        featureStore.cancelEditing();
474
                } catch (ReadException e) {
475
                        throw new CancelEditingLayerException(layer.getName(), e);
476
                } catch (DataException e) {
477
                        throw new CancelEditingLayerException(layer.getName(), e);
478
                }
479
        }
480
        */
481

    
482
        private int exportLayer(FLyrVect layer) throws ReadException {
483
                ViewDocument model = vista.getModel();
484
                MapContext mapContext = model.getMapContext();
485
                IProjection projection = mapContext.getProjection();
486

    
487
                ExporttoLayerExtension extension = (ExporttoLayerExtension) PluginsLocator
488
                                .getManager().getExtension(ExporttoLayerExtension.class);
489
                return extension.showExportto(layer, projection, mapContext, WindowManager.MODE.DIALOG);
490
        }
491

    
492
        /**
493
         * @see org.gvsig.andami.plugins.IExtension#isVisible()
494
         */
495
        public boolean isVisible() {
496
                if (EditionUtilities.getEditionStatus() == EditionUtilities.EDITION_STATUS_ONE_VECTORIAL_LAYER_ACTIVE_AND_EDITABLE) {
497
                        return true;
498
                }
499
                return false;
500

    
501
        }
502

    
503
        public IExtensionStatus getStatus() {
504
                return new StopEditingStatus();
505
        }
506

    
507
        /**
508
         * Show the dialogs to save the layer without ask if don't like to save.
509
         * 
510
         * @param layer
511
         *            Layer to save.
512
         */
513
        public boolean executeSaveLayer(FLyrVect layer) {
514
            
515
            IEditionManager edMan = EditionLocator.getEditionManager(layer);
516

    
517
                VectorialLayerEdited lyrEd = (VectorialLayerEdited) edMan
518
                                .getLayerEdited(layer);
519
                boolean isStop = false;
520
                try {
521
                        lyrEd.clearSelection();
522

    
523
                        if (layer.isWritable()) {
524

    
525
                            try {
526
                                edMan.stopEditLayer(null, layer, IEditionManager.ACCEPT_EDITING);
527
                                        isStop = true;
528
                            } catch (CancelException ex) {
529
                                        isStop = false;
530
                                    
531
                            } catch (Exception ex) {
532
                                logger.error("While stopping layer editing.", ex);
533
                                        isStop = false;
534
                            }
535

    
536
                        } else {
537
                                // Si no existe writer para la capa que tenemos en edici?n
538
                                int resp = JOptionPane
539
                                                .showConfirmDialog(
540
                                                                (Component) PluginServices.getMainFrame(),
541
                                                                PluginServices
542
                                                                                .getText(
543
                                                                                                this,
544
                                                                                                "no_existe_writer_para_este_formato_de_capa_o_no_tiene_permisos_de_escritura_los_datos_no_se_guardaran_desea_continuar")
545
                                                                                + " : " + layer.getName(),
546
                                                                PluginServices
547
                                                                                .getText(this, "cancelar_edicion"),
548
                                                                JOptionPane.YES_NO_OPTION);
549
                                if (resp == JOptionPane.YES_OPTION) { // CANCEL EDITING
550

    
551
                                    try {
552
                                        edMan.stopEditLayer(null, layer, IEditionManager.CANCEL_EDITING);
553
                                                isStop = true;
554
                                    } catch(CancelException ex) {
555
                                                isStop = false;
556
                                            
557
                                    } catch (Exception ex) {
558
                                                isStop = false;
559
                                        logger.error("While stopping layer editing.", ex);
560
                                    }
561

    
562
                                }
563

    
564
                        }
565
                } catch (ReadException e1) {
566
                        NotificationManager.showMessageError(e1.getMessage(), e1);
567
                } catch (DataException e) {
568
                        NotificationManager.showMessageError(e.getMessage(), e);
569
                }
570
                return isStop;
571
        }
572

    
573
        private class UnsavedLayer extends UnsavedData {
574

    
575
                private FLayer layer;
576

    
577
                public UnsavedLayer(IExtension extension) {
578
                        super(extension);
579
                }
580

    
581
                public String getDescription() {
582
                        return PluginServices.getText(this, "editing_layer_unsaved");
583
                }
584

    
585
                public String getResourceName() {
586
                        return layer.getName();
587
                }
588

    
589
                public boolean saveData() {
590
                        return executeSaveLayer((FLyrVect) layer);
591
                }
592

    
593
                public void setLayer(FLayer layer) {
594
                        this.layer = layer;
595

    
596
                }
597

    
598
                public String getIcon() {
599
                        return layer.getTocImageIcon();
600
                }
601

    
602
        }
603

    
604
        /**
605
         * <p>
606
         * This class provides the status of extensions. If this extension has some
607
         * unsaved editing layer (and save them), and methods to check if the
608
         * extension has some associated background tasks.
609
         * 
610
         * @author Vicente Caballero Navarro
611
         * 
612
         */
613
        private class StopEditingStatus implements IExtensionStatus {
614
                /**
615
                 * This method is used to check if this extension has some unsaved
616
                 * editing layer.
617
                 * 
618
                 * @return true if the extension has some unsaved editing layer, false
619
                 *         otherwise.
620
                 */
621
                public boolean hasUnsavedData() {
622
                        Project project = ProjectManager.getInstance().getCurrentProject();
623
                        DefaultViewDocument[] views = project.getDocuments(
624
                                        ViewManager.TYPENAME).toArray(new DefaultViewDocument[0]);
625
                        for (int i = 0; i < views.length; i++) {
626
                                FLayers layers = views[i].getMapContext().getLayers();
627
                                LayersIterator iter = getEditingLayer(layers);
628
                                if (iter.hasNext()) {
629
                                        return true;
630
                                }
631
                        }
632
                        return false;
633
                }
634

    
635
                /**
636
                 * This method is used to check if the extension has some associated
637
                 * background process which is currently running.
638
                 * 
639
                 * @return true if the extension has some associated background process,
640
                 *         false otherwise.
641
                 */
642
                public boolean hasRunningProcesses() {
643
                        return false;
644
                }
645

    
646
                /**
647
                 * <p>
648
                 * Gets an array of the traceable background tasks associated with this
649
                 * extension. These tasks may be tracked, canceled, etc.
650
                 * </p>
651
                 * 
652
                 * @return An array of the associated background tasks, or null in case
653
                 *         there is no associated background tasks.
654
                 */
655
                public IMonitorableTask[] getRunningProcesses() {
656
                        return null;
657
                }
658

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

    
686
        private LayersIterator getEditingLayer(FLayers layers) {
687
                return new LayersIterator(layers) {
688
                        public boolean evaluate(FLayer layer) {
689
                                return layer.isEditing();
690
                        }
691
                };
692
        }
693
}