Statistics
| Revision:

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

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

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

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

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

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

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

    
137
                                resp = JOptionPane
138
                                                .showOptionDialog(
139
                                                                (Component) PluginServices.getMainFrame(),
140
                                                                PluginServices.getText(this,
141
                                                                                "realmente_desea_guardar_la_capa")
142
                                                                                + " : " + layer.getName(),
143
                                                                PluginServices.getText(this, "stop_edition"),
144
                                                                JOptionPane.YES_NO_CANCEL_OPTION,
145
                                                                JOptionPane.QUESTION_MESSAGE, null, options,
146
                                                                options[2]);
147
                                if (resp == JOptionPane.YES_OPTION) {
148
                                    // SAVE
149
                                        saveLayer(layer);
150
                                        layer.setEditing(false);
151
                                } else if (resp == JOptionPane.NO_OPTION) {
152
                                    // CANCEL EDITING
153
                                        cancelEdition(layer);
154
                                        layer.setEditing(false);
155
                                        return true;
156
                                } else if (resp == JOptionPane.CANCEL_OPTION) {
157
                                    // CONTINUE EDITING
158
                                    return false;
159
                                } else {
160
                                    // should not get here
161
                                    // cancel editing
162
                    cancelEdition(layer);
163
                    layer.setEditing(false);
164
                    return true;
165
                                }
166
                                
167
                        }
168
                        // ========================================
169
                        // Layer cannot save changes:
170
                        
171
                        Object[] options = {
172
                                        PluginServices.getText(this, "export"),
173
                                        PluginServices.getText(this,
174
                                                        "finish_editing_without_saving_changes"),
175
                                        PluginServices.getText(this, "continue_editing") };
176

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

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

    
210
        }
211

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

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

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

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

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

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

    
256
        }
257

    
258
        public IExtensionStatus getStatus() {
259
                return new StopEditingStatus();
260
        }
261

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

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

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

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

    
352
        private class UnsavedLayer extends UnsavedData {
353

    
354
                private FLayer layer;
355

    
356
                public UnsavedLayer(IExtension extension) {
357
                        super(extension);
358
                }
359

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

    
364
                public String getResourceName() {
365
                        return layer.getName();
366
                }
367

    
368
                public boolean saveData() {
369
                        return executeSaveLayer((FLyrVect) layer);
370
                }
371

    
372
                public void setLayer(FLayer layer) {
373
                        this.layer = layer;
374

    
375
                }
376

    
377
                public String getIcon() {
378
                        return layer.getTocImageIcon();
379
                }
380

    
381
        }
382

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

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

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

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

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