Statistics
| Revision:

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

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
                    return true;
152
                                } else if (resp == JOptionPane.NO_OPTION) {
153
                                    // CANCEL EDITING
154
                                        cancelEdition(layer);
155
                                        layer.setEditing(false);
156
                                        return true;
157
                                } else if (resp == JOptionPane.CANCEL_OPTION) {
158
                                    // CONTINUE EDITING
159
                                    return false;
160
                                } else {
161
                                    // should not get here
162
                                    // cancel editing
163
                    cancelEdition(layer);
164
                    layer.setEditing(false);
165
                    return true;
166
                                }
167
                                
168
                        }
169
                        // ========================================
170
                        // Layer cannot save changes:
171
                        
172
                        Object[] options = {
173
                                        PluginServices.getText(this, "export"),
174
                                        PluginServices.getText(this,
175
                                                        "finish_editing_without_saving_changes"),
176
                                        PluginServices.getText(this, "continue_editing") };
177

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

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

    
211
        }
212

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

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

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

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

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

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

    
257
        }
258

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

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

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

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

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

    
353
        private class UnsavedLayer extends UnsavedData {
354

    
355
                private FLayer layer;
356

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

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

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

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

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

    
376
                }
377

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

    
382
        }
383

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

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

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

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

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