Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / extEditing / src / org / gvsig / editing / StopEditing.java @ 38773

History | View | Annotate | Download (14.4 KB)

1
package org.gvsig.editing;
2

    
3
import java.awt.Component;
4
import java.util.ArrayList;
5

    
6
import javax.swing.JOptionPane;
7

    
8
import org.cresques.cts.IProjection;
9
import org.gvsig.andami.PluginServices;
10
import org.gvsig.andami.PluginsLocator;
11
import org.gvsig.andami.messages.NotificationManager;
12
import org.gvsig.andami.plugins.Extension;
13
import org.gvsig.andami.plugins.IExtension;
14
import org.gvsig.andami.plugins.status.IExtensionStatus;
15
import org.gvsig.andami.plugins.status.IUnsavedData;
16
import org.gvsig.andami.plugins.status.UnsavedData;
17
import org.gvsig.app.project.Project;
18
import org.gvsig.app.project.ProjectManager;
19
import org.gvsig.app.project.documents.view.DefaultViewDocument;
20
import org.gvsig.app.project.documents.view.ViewDocument;
21
import org.gvsig.app.project.documents.view.ViewManager;
22
import org.gvsig.app.project.documents.view.gui.DefaultViewPanel;
23
import org.gvsig.editing.gui.cad.CADToolAdapter;
24
import org.gvsig.editing.layers.VectorialLayerEdited;
25
import org.gvsig.exportto.app.extension.ExporttoLayerExtension;
26
import org.gvsig.fmap.dal.exception.DataException;
27
import org.gvsig.fmap.dal.exception.ReadException;
28
import org.gvsig.fmap.dal.exception.WriteException;
29
import org.gvsig.fmap.dal.feature.FeatureStore;
30
import org.gvsig.fmap.mapcontext.MapContext;
31
import org.gvsig.fmap.mapcontext.exceptions.CancelEditingLayerException;
32
import org.gvsig.fmap.mapcontext.exceptions.StartEditionLayerException;
33
import org.gvsig.fmap.mapcontext.layers.FLayer;
34
import org.gvsig.fmap.mapcontext.layers.FLayers;
35
import org.gvsig.fmap.mapcontext.layers.LayersIterator;
36
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
37
import org.gvsig.fmap.mapcontrol.MapControl;
38
import org.gvsig.utils.swing.threads.IMonitorableTask;
39

    
40
/**
41
 * @author Francisco Jos?
42
 * 
43
 *         Cuando un tema se pone en edici?n, puede que su driver implemente
44
 *         ISpatialWriter. En ese caso, es capaz de guardarse sobre s? mismo. Si
45
 *         no lo implementa, esta opci?n estar? deshabilitada y la ?nica
46
 *         posibilidad de guardar este tema ser? "Guardando como..."
47
 */
48
public class StopEditing extends Extension {
49
        private DefaultViewPanel vista;
50

    
51
        /**
52
         * @see org.gvsig.andami.plugins.IExtension#initialize()
53
         */
54
        public void initialize() {
55
        }
56

    
57
        /**
58
         * @see org.gvsig.andami.plugins.IExtension#execute(java.lang.String)
59
         */
60
        public void execute(String s) {
61
                org.gvsig.andami.ui.mdiManager.IWindow f = PluginServices
62
                                .getMDIManager().getActiveWindow();
63

    
64
                vista = (DefaultViewPanel) f;
65
                boolean isStop = false;
66
                ViewDocument model = vista.getModel();
67
                MapContext mapa = model.getMapContext();
68
                FLayers layers = mapa.getLayers();
69
                EditionManager edMan = CADExtension.getEditionManager();
70

    
71
                if (s.equals("layer-stop-editing")) {
72
                        vista.getMapControl().getCanceldraw().setCanceled(true);
73
                        FLayer[] actives = layers.getActives();
74
                        // TODO: Comprobar que solo hay una activa, o al menos
75
                        // que solo hay una en edici?n que est? activa, etc, etc
76
                        for (int i = 0; i < actives.length; i++) {
77
                                if (actives[i] instanceof FLyrVect && actives[i].isEditing()) {
78
                                        FLyrVect lv = (FLyrVect) actives[i];
79
                                        MapControl mapControl = vista.getMapControl();
80
                                        VectorialLayerEdited lyrEd = (VectorialLayerEdited) edMan
81
                                                        .getActiveLayerEdited();
82
                                        lv.getFeatureStore().deleteObserver(lyrEd);
83
                                        isStop = stopEditing(lv, mapControl);
84
                                        if (isStop) {
85
                                                lv.removeLayerListener(edMan);
86
                                                mapControl.getMapContext().removeLayerDrawListener(
87
                                                                lyrEd);
88
                                                // if (lv instanceof FLyrAnnotation){
89
                                                // FLyrAnnotation lva=(FLyrAnnotation)lv;
90
                                                // lva.setMapping(lva.getMapping());
91
                                                // }
92
                                        } else {
93
                                                lv.getFeatureStore().addObserver(lyrEd);
94

    
95
                                        }
96
                                }
97
                        }
98
                        if (isStop) {
99
                                vista.getMapControl().setTool("zoomIn");
100
                                vista.hideConsole();
101
                                vista.repaintMap();
102
                                CADExtension.clearView();
103

    
104
                        }
105
                }
106
                PluginServices.getMainFrame().enableControls();
107
        }
108

    
109
        /**
110
         * @see org.gvsig.andami.plugins.IExtension#isEnabled()
111
         */
112
        public boolean isEnabled() {
113
                FLayer[] lyrs = EditionUtilities.getActiveAndEditedLayers();
114
                if (lyrs == null) {
115
                        return false;
116
                }
117
                FLyrVect lyrVect = (FLyrVect) lyrs[0];
118
                if (lyrVect.isEditing()) {
119
                        return true;
120
                }
121
                return false;
122
        }
123

    
124
        /**
125
         * DOCUMENT ME!
126
         */
127
        public boolean stopEditing(FLyrVect layer, MapControl mapControl) {
128
                int resp = JOptionPane.CANCEL_OPTION;
129

    
130
                try {
131
                        if (layer.isWritable()) {
132
                                Object[] options = {
133
                                                PluginServices.getText(this, "guardar"),
134
                                                PluginServices.getText(this,
135
                                                                "finish_editing_without_saving_changes"),
136
                                                PluginServices.getText(this, "continue_editing") };
137

    
138
                                resp = JOptionPane
139
                                                .showOptionDialog(
140
                                                                (Component) PluginServices.getMainFrame(),
141
                                                                PluginServices.getText(this,
142
                                                                                "realmente_desea_guardar_la_capa")
143
                                                                                + " : " + layer.getName(),
144
                                                                PluginServices.getText(this, "stop_edition"),
145
                                                                JOptionPane.YES_NO_CANCEL_OPTION,
146
                                                                JOptionPane.QUESTION_MESSAGE, null, options,
147
                                                                options[2]);
148
                                
149
                                if (resp == JOptionPane.YES_OPTION) {
150
                                    // SAVE
151
                                        saveLayer(layer);
152
                                        layer.setEditing(false);
153
                    return true;
154
                                } else if (resp == JOptionPane.NO_OPTION) {
155
                                    // CANCEL EDITING
156
                                        cancelEdition(layer);
157
                                        layer.setEditing(false);
158
                                        return true;
159
                                } else if (resp == JOptionPane.CANCEL_OPTION) {
160
                                    // CONTINUE EDITING
161
                                    return false;
162
                                } else {
163
                                    // This happens when user clicks on [x]
164
                                    // to abruptly close previous JOptionPane dialog
165
                                    // We make it equivalent to CONTINUE EDITING
166
                    return false;
167
                                }
168
                                
169
                        }
170
                        // ========================================
171
                        // Layer cannot save changes:
172
                        
173
                        Object[] options = {
174
                                        PluginServices.getText(this, "export"),
175
                                        PluginServices.getText(this,
176
                                                        "finish_editing_without_saving_changes"),
177
                                        PluginServices.getText(this, "continue_editing") };
178

    
179
                        resp = JOptionPane
180
                                        .showOptionDialog(
181
                                                        (Component) PluginServices.getMainFrame(),
182
                                                        PluginServices
183
                                                                        .getText(
184
                                                                                        this,
185
                                                                                        "no_existe_writer_para_este_formato_de_capa_o_no_tiene_permisos_de_escritura_los_datos_que_desea_hacer")
186
                                                                        + " : " + layer.getName(), PluginServices
187
                                                                        .getText(this, "stop_edition"),
188
                                                        JOptionPane.YES_NO_CANCEL_OPTION,
189
                                                        JOptionPane.QUESTION_MESSAGE, null, options,
190
                                                        options[2]);
191
                        if (resp == JOptionPane.NO_OPTION) { // CANCEL EDITING
192
                                cancelEdition(layer);
193
                                layer.setEditing(false);
194
                                return true;
195
                        } else if (resp == JOptionPane.YES_OPTION) {
196
                                int status = exportLayer(layer);
197
                                if (status == JOptionPane.OK_OPTION) {
198
                                        cancelEdition(layer);
199
                                        layer.setEditing(false);
200
                                }
201
                        }
202

    
203
                } catch (StartEditionLayerException e) {
204
                        NotificationManager.addError(e);
205
                } catch (ReadException e) {
206
                        NotificationManager.addError(e);
207
                } catch (CancelEditingLayerException e) {
208
                        NotificationManager.addError(e);
209
                }
210
                return false;
211

    
212
        }
213

    
214
        private void saveLayer(FLyrVect layer) throws ReadException {
215
                FeatureStore featureStore = layer.getFeatureStore();
216
                try {
217
                        featureStore.finishEditing();
218
                } catch (WriteException e) {
219
                        throw new ReadException(featureStore.getName(), e);
220
                } catch (DataException e) {
221
                        throw new ReadException(featureStore.getName(), e);
222
                }
223
        }
224

    
225
        private void cancelEdition(FLyrVect layer)
226
                        throws CancelEditingLayerException {
227
                FeatureStore featureStore = null;
228
                try {
229
                        featureStore = layer.getFeatureStore();
230

    
231
                        featureStore.cancelEditing();
232
                } catch (ReadException e) {
233
                        throw new CancelEditingLayerException(layer.getName(), e);
234
                } catch (DataException e) {
235
                        throw new CancelEditingLayerException(layer.getName(), e);
236
                }
237
        }
238

    
239
        private int exportLayer(FLyrVect layer) throws ReadException {
240
                ViewDocument model = vista.getModel();
241
                MapContext mapContext = model.getMapContext();
242
                IProjection projection = mapContext.getProjection();
243

    
244
                ExporttoLayerExtension extension = (ExporttoLayerExtension) PluginsLocator
245
                                .getManager().getExtension(ExporttoLayerExtension.class);
246
                return extension.showExportto(layer, projection, mapContext);
247
        }
248

    
249
        /**
250
         * @see org.gvsig.andami.plugins.IExtension#isVisible()
251
         */
252
        public boolean isVisible() {
253
                if (EditionUtilities.getEditionStatus() == EditionUtilities.EDITION_STATUS_ONE_VECTORIAL_LAYER_ACTIVE_AND_EDITABLE) {
254
                        return true;
255
                }
256
                return false;
257

    
258
        }
259

    
260
        public IExtensionStatus getStatus() {
261
                return new StopEditingStatus();
262
        }
263

    
264
        /**
265
         * Show the dialogs to save the layer without ask if don't like to save.
266
         * 
267
         * @param layer
268
         *            Layer to save.
269
         */
270
        public boolean executeSaveLayer(FLyrVect layer) {
271
                CADToolAdapter cadtoolAdapter = CADExtension.getCADToolAdapter(layer);
272
                EditionManager edMan = cadtoolAdapter.getEditionManager();
273
                VectorialLayerEdited lyrEd = (VectorialLayerEdited) edMan
274
                                .getLayerEdited(layer);
275
                boolean isStop = false;
276
                try {
277
                        lyrEd.clearSelection();
278

    
279
                        if (layer.isWritable()) {
280
                                saveLayer(layer);
281
                                layer.setEditing(false);
282
                                // if (layer.isSpatiallyIndexed())
283
                                // {
284
                                // if(layer.getISpatialIndex() != null)
285
                                // {
286
                                // PluginServices.
287
                                // cancelableBackgroundExecution(new
288
                                // CreateSpatialIndexMonitorableTask((FLyrVect)layer));
289
                                // }
290
                                // }
291

    
292
                                isStop = true;
293
                        } else {
294
                                // Si no existe writer para la capa que tenemos en edici?n
295
                                int resp = JOptionPane
296
                                                .showConfirmDialog(
297
                                                                (Component) PluginServices.getMainFrame(),
298
                                                                PluginServices
299
                                                                                .getText(
300
                                                                                                this,
301
                                                                                                "no_existe_writer_para_este_formato_de_capa_o_no_tiene_permisos_de_escritura_los_datos_no_se_guardaran_desea_continuar")
302
                                                                                + " : " + layer.getName(),
303
                                                                PluginServices
304
                                                                                .getText(this, "cancelar_edicion"),
305
                                                                JOptionPane.YES_NO_OPTION);
306
                                if (resp == JOptionPane.YES_OPTION) { // CANCEL EDITING
307
                                        try {
308
                                                cancelEdition(layer);
309
                                                layer.setEditing(false);
310
                                                // if (!(layer.getSource().getDriver() instanceof
311
                                                // IndexedShpDriver)){
312
                                                // VectorialLayerEdited
313
                                                // vle=(VectorialLayerEdited)CADExtension.getEditionManager().getLayerEdited(layer);
314
                                                // layer.setLegend((IVectorLegend)vle.getLegend());
315
                                                // }
316
                                        } catch (CancelEditingLayerException e) {
317
                                                PluginServices.getLogger().error(e.getMessage(), e);
318
                                                return isStop;
319
                                        }
320
                                        isStop = true;
321
                                }
322

    
323
                        }
324
                        if (isStop) {
325
                                layer.removeLayerListener(edMan);
326
                                // if (layer instanceof FLyrAnnotation){
327
                                // FLyrAnnotation lva=(FLyrAnnotation)layer;
328
                                // lva.setMapping(lva.getMapping());
329
                                // }
330
                                org.gvsig.andami.ui.mdiManager.IWindow f = PluginServices
331
                                                .getMDIManager().getActiveWindow();
332
                                if (f instanceof DefaultViewPanel) {
333
                                        vista = (DefaultViewPanel) f;
334
                                        FLayer auxLayer = vista.getMapControl().getMapContext()
335
                                                        .getLayers().getLayer(layer.getName());
336
                                        if (auxLayer != null && auxLayer.equals(layer)) {
337
                                                vista.getMapControl().setTool("zoomIn");
338
                                                vista.hideConsole();
339
                                                vista.repaintMap();
340
                                                CADExtension.clearView();
341
                                        }
342
                                }
343
                        }
344
                } catch (ReadException e1) {
345
                        NotificationManager.showMessageError(e1.getMessage(), e1);
346
                } catch (StartEditionLayerException e) {
347
                        NotificationManager.showMessageError(e.getMessage(), e);
348
                } catch (DataException e) {
349
                        NotificationManager.showMessageError(e.getMessage(), e);
350
                }
351
                return isStop;
352
        }
353

    
354
        private class UnsavedLayer extends UnsavedData {
355

    
356
                private FLayer layer;
357

    
358
                public UnsavedLayer(IExtension extension) {
359
                        super(extension);
360
                }
361

    
362
                public String getDescription() {
363
                        return PluginServices.getText(this, "editing_layer_unsaved");
364
                }
365

    
366
                public String getResourceName() {
367
                        return layer.getName();
368
                }
369

    
370
                public boolean saveData() {
371
                        return executeSaveLayer((FLyrVect) layer);
372
                }
373

    
374
                public void setLayer(FLayer layer) {
375
                        this.layer = layer;
376

    
377
                }
378

    
379
                public String getIcon() {
380
                        return layer.getTocImageIcon();
381
                }
382

    
383
        }
384

    
385
        /**
386
         * <p>
387
         * This class provides the status of extensions. If this extension has some
388
         * unsaved editing layer (and save them), and methods to check if the
389
         * extension has some associated background tasks.
390
         * 
391
         * @author Vicente Caballero Navarro
392
         * 
393
         */
394
        private class StopEditingStatus implements IExtensionStatus {
395
                /**
396
                 * This method is used to check if this extension has some unsaved
397
                 * editing layer.
398
                 * 
399
                 * @return true if the extension has some unsaved editing layer, false
400
                 *         otherwise.
401
                 */
402
                public boolean hasUnsavedData() {
403
                        Project project = ProjectManager.getInstance().getCurrentProject();
404
                        DefaultViewDocument[] views = project.getDocuments(
405
                                        ViewManager.TYPENAME).toArray(new DefaultViewDocument[0]);
406
                        for (int i = 0; i < views.length; i++) {
407
                                FLayers layers = views[i].getMapContext().getLayers();
408
                                LayersIterator iter = getEditingLayer(layers);
409
                                if (iter.hasNext()) {
410
                                        return true;
411
                                }
412
                        }
413
                        return false;
414
                }
415

    
416
                /**
417
                 * This method is used to check if the extension has some associated
418
                 * background process which is currently running.
419
                 * 
420
                 * @return true if the extension has some associated background process,
421
                 *         false otherwise.
422
                 */
423
                public boolean hasRunningProcesses() {
424
                        return false;
425
                }
426

    
427
                /**
428
                 * <p>
429
                 * Gets an array of the traceable background tasks associated with this
430
                 * extension. These tasks may be tracked, canceled, etc.
431
                 * </p>
432
                 * 
433
                 * @return An array of the associated background tasks, or null in case
434
                 *         there is no associated background tasks.
435
                 */
436
                public IMonitorableTask[] getRunningProcesses() {
437
                        return null;
438
                }
439

    
440
                /**
441
                 * <p>
442
                 * Gets an array of the UnsavedData objects, which contain information
443
                 * about the unsaved editing layers and allows to save it.
444
                 * </p>
445
                 * 
446
                 * @return An array of the associated unsaved editing layers, or null in
447
                 *         case the extension has not unsaved editing layers.
448
                 */
449
                public IUnsavedData[] getUnsavedData() {
450
                        Project project = ProjectManager.getInstance().getCurrentProject();
451
                        DefaultViewDocument[] views = project.getDocuments(
452
                                        ViewManager.TYPENAME).toArray(new DefaultViewDocument[0]);
453
                        ArrayList unsavedLayers = new ArrayList();
454
                        for (int i = 0; i < views.length; i++) {
455
                                FLayers layers = views[i].getMapContext().getLayers();
456
                                LayersIterator iter = getEditingLayer(layers);
457
                                while (iter.hasNext()) {
458
                                        UnsavedLayer ul = new UnsavedLayer(StopEditing.this);
459
                                        ul.setLayer(iter.nextLayer());
460
                                        unsavedLayers.add(ul);
461
                                }
462
                        }
463
                        return (IUnsavedData[]) unsavedLayers.toArray(new IUnsavedData[0]);
464
                }
465
        }
466

    
467
        private LayersIterator getEditingLayer(FLayers layers) {
468
                return new LayersIterator(layers) {
469
                        public boolean evaluate(FLayer layer) {
470
                                return layer.isEditing();
471
                        }
472
                };
473
        }
474
}