Statistics
| Revision:

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

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

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

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

    
65
                vista = (DefaultViewPanel) f;
66
                boolean isStop=false;
67
                ViewDocument model = vista.getModel();
68
                MapContext mapa = model.getMapContext();
69
                FLayers layers = mapa.getLayers();
70
                EditionManager edMan = CADExtension.getEditionManager();
71
                if (s.equals("STOPEDITING")) {
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.getActiveLayerEdited();
81
//                                        try {
82
                                                lv.getFeatureStore().deleteObserver(lyrEd);
83
//                                        } catch (ReadException e) {
84
//                                                NotificationManager.addError("Remove Selection Listener",e);
85
//                                        }
86
                                        isStop=stopEditing(lv, mapControl);
87
                                        if (isStop){
88
                                                lv.removeLayerListener(edMan);
89
                                                mapControl.getMapContext().removeLayerDrawListener(
90
                                                                lyrEd);
91
//                                                if (lv instanceof FLyrAnnotation){
92
//                                                        FLyrAnnotation lva=(FLyrAnnotation)lv;
93
//                                            lva.setMapping(lva.getMapping());
94
//                                                }
95
                                        } else {
96
//                                                try {
97
                                                        lv.getFeatureStore().addObserver(lyrEd);
98
//                                                } catch (ReadException e) {
99
//                                                        NotificationManager.addError(
100
//                                                                        "Remove Selection Listener", e);
101
//                                                }
102

    
103
                                        }
104
                                }
105
                        }
106
                        if (isStop) {
107
                                vista.getMapControl().setTool("zoomIn");
108
                                vista.hideConsole();
109
                                vista.repaintMap();
110
                                CADExtension.clearView();
111

    
112
                        }
113
                }
114
                PluginServices.getMainFrame().enableControls();
115
        }
116

    
117
        /**
118
         * @see org.gvsig.andami.plugins.IExtension#isEnabled()
119
         */
120
        public boolean isEnabled() {
121
                FLayer[] lyrs = EditionUtilities.getActiveAndEditedLayers();
122
                if (lyrs == null) {
123
                        return false;
124
                }
125
                FLyrVect lyrVect = (FLyrVect) lyrs[0];
126
                if (lyrVect.isEditing()) {
127
                        return true;
128
                }
129
                return false;
130
        }
131
        /**
132
         * DOCUMENT ME!
133
         */
134
        public boolean stopEditing(FLyrVect layer, MapControl mapControl) {
135
//                FeatureStore featureStore=null;
136
//                try {
137
//                        featureStore = layer.getFeatureStore();
138
//                } catch (ReadException e1) {
139
//                        // TODO Auto-generated catch block
140
//                        e1.printStackTrace();
141
//                }
142
                int resp = JOptionPane.CANCEL_OPTION;
143

    
144
                try {
145
                        if (layer.isWritable()) {
146
//                                resp = JOptionPane.showConfirmDialog((Component) PluginServices
147
//                                                .getMainFrame(), PluginServices.getText(this,
148
//                                                "realmente_desea_guardar_la_capa")
149
//                                                + " : " + layer.getName()+"?", PluginServices.getText(this,
150
//                                                "guardar"), JOptionPane.YES_NO_OPTION);
151
//                                if (resp == JOptionPane.YES_OPTION) { // GUARDAMOS EL TEMA
152
//                                        saveLayer(layer);
153
//
154
//                                } else if (resp == JOptionPane.NO_OPTION){ // CANCEL EDITING
155
//                                        cancelEdition(layer);
156
//                                } else {
157
//                                        return false;
158
//                                }
159
                                
160
                                
161
                                Object[] options = {PluginServices.getText(this, "guardar"),
162
                                                PluginServices.getText(this, "finish_editing_without_saving_changes"),
163
                                                PluginServices.getText(this, "continue_editing")};
164

    
165
                                resp = JOptionPane.showOptionDialog(
166
                                                (Component) PluginServices.getMainFrame(),
167
                                                PluginServices
168
                                                .getText(
169
                                                                this,
170
                                                                "realmente_desea_guardar_la_capa")
171
                                                + " : " + layer.getName(),
172
                                                PluginServices.getText(this, "stop_edition"),
173
                                                JOptionPane.YES_NO_CANCEL_OPTION,
174
                                                JOptionPane.QUESTION_MESSAGE,
175
                                                null, options, options[2]);
176
                                if (resp == JOptionPane.YES_OPTION) { //SAVE
177
                                        saveLayer(layer);
178
                                        layer.setEditing(false);
179
                                } else if (resp == JOptionPane.NO_OPTION) { // CANCEL EDITING
180
                                        cancelEdition(layer);
181
                                        layer.setEditing(false);
182
                                        return true;
183
                                }
184
                                
185
                                
186

    
187
//                                featureStore.deleteObservers();
188
//                                layer.setEditing(false);
189
//                                if (layer.isSpatiallyIndexed())
190
//                    {
191
//                            if(vea.getSpatialIndex() != null)
192
//                        {
193
//                                    layer.setISpatialIndex(vea.getSpatialIndex());
194
//                                    if(layer.getISpatialIndex() instanceof IPersistentSpatialIndex)
195
//                                ((IPersistentSpatialIndex) layer.getISpatialIndex()).flush();
196
//                                    PluginServices.
197
//                                                                cancelableBackgroundExecution(new CreateSpatialIndexMonitorableTask(layer));
198
//
199
//                        }
200
//                    }
201

    
202
                                return true;
203
                        }
204
                        // Si no existe writer para la capa que tenemos en edici?n
205
//                                resp = JOptionPane
206
//                                                .showConfirmDialog(
207
//                                                                (Component) PluginServices.getMainFrame(),
208
//                                                                PluginServices
209
//                                                                                .getText(
210
//                                                                                                this,
211
//                                                                                                "no_existe_writer_para_este_formato_de_capa_o_no_tiene_permisos_de_escritura_los_datos_no_se_guardaran_desea_continuar")
212
//                                                                                + " : " + layer.getName(),
213
//                                                                PluginServices.getText(this, "stop_edition"),
214
//                                                                JOptionPane.YES_NO_OPTION);
215
                                Object[] options = {PluginServices.getText(this, "export"),
216
                                                PluginServices.getText(this, "finish_editing_without_saving_changes"),
217
                                                PluginServices.getText(this, "continue_editing")};
218

    
219
                                resp = JOptionPane.showOptionDialog(
220
                                                (Component) PluginServices.getMainFrame(),
221
                                                PluginServices
222
                                                .getText(
223
                                                                this,
224
                                                                "no_existe_writer_para_este_formato_de_capa_o_no_tiene_permisos_de_escritura_los_datos_que_desea_hacer")
225
                                                + " : " + layer.getName(),
226
                                                PluginServices.getText(this, "stop_edition"),
227
                                                JOptionPane.YES_NO_CANCEL_OPTION,
228
                                                JOptionPane.QUESTION_MESSAGE,
229
                                                null, options, options[2]);
230
                                if (resp == JOptionPane.NO_OPTION) { // CANCEL EDITING
231
                                        cancelEdition(layer);
232
                                        layer.setEditing(false);
233
                                        return true;
234
                                } else if (resp == JOptionPane.YES_OPTION) {
235
                                        int status = exportLayer(layer);
236
                                        if (status == JOptionPane.OK_OPTION){
237
                                                cancelEdition(layer);
238
                                                layer.setEditing(false);
239
                                        }
240
                                }
241

    
242
                } catch (StartEditionLayerException e) {
243
                        NotificationManager.addError(e);
244
                } catch (ReadException e) {
245
                        NotificationManager.addError(e);
246
                } catch (CancelEditingLayerException e) {
247
                        NotificationManager.addError(e);
248
                }
249
                return false;
250

    
251
        }
252

    
253

    
254
        private void saveLayer(FLyrVect layer) throws ReadException{
255
                FeatureStore featureStore=layer.getFeatureStore();
256
                try {
257
                        featureStore.finishEditing();
258
                } catch (WriteException e) {
259
                        throw new ReadException(featureStore.getName(), e);
260
                } catch (DataException e) {
261
                        throw new ReadException(featureStore.getName(), e);
262
                }
263
        }
264

    
265
        private void cancelEdition(FLyrVect layer) throws CancelEditingLayerException {
266
                FeatureStore featureStore=null;
267
                try {
268
                        featureStore = layer.getFeatureStore();
269

    
270
                        featureStore.cancelEditing();
271
                } catch (ReadException e) {
272
                        throw new CancelEditingLayerException(layer.getName(), e);
273
                } catch (DataException e) {
274
                        throw new CancelEditingLayerException(layer.getName(), e);
275
                }
276
        }
277

    
278
        private int exportLayer(FLyrVect layer) throws ReadException{
279
                //FIXME
280
//                JOptionPane.showConfirmDialog((Component) PluginServices.getMainFrame(), "NO IMPLEMENTADO. Utilice la herramieneta de exportaci?n.");
281
                
282
                ViewDocument model = vista.getModel();
283
                MapContext mapContext = model.getMapContext();
284
                IProjection projection = mapContext.getProjection();
285
                
286
                ExporttoLayerExtension extension = (ExporttoLayerExtension) PluginsLocator.getManager().getExtension(ExporttoLayerExtension.class);
287
                return extension.showExportto(layer, projection, mapContext);
288
        }
289
/**
290
         * @see org.gvsig.andami.plugins.IExtension#isVisible()
291
         */
292
        public boolean isVisible() {
293
                if (EditionUtilities.getEditionStatus() == EditionUtilities.EDITION_STATUS_ONE_VECTORIAL_LAYER_ACTIVE_AND_EDITABLE) {
294
                        return true;
295
                }
296
                return false;
297

    
298
        }
299
        public IExtensionStatus getStatus() {
300
                return new StopEditingStatus();
301
        }
302
        /**
303
         * Show the dialogs to save the layer without ask if don't like to save.
304
         * @param layer Layer to save.
305
         */
306
        public boolean executeSaveLayer(FLyrVect layer ) {
307
//                EditionManager edMan = CADExtension.getEditionManager();
308
                CADToolAdapter cadtoolAdapter=CADExtension.getCADToolAdapter(layer);
309
                EditionManager edMan =cadtoolAdapter.getEditionManager();
310
                VectorialLayerEdited lyrEd = (VectorialLayerEdited)        edMan.getLayerEdited(layer);
311
                boolean isStop=false;
312
                try {
313
                        lyrEd.clearSelection();
314

    
315

    
316
                if (layer.isWritable()) {
317
                                saveLayer(layer);
318
                                layer.setEditing(false);
319
//                                if (layer.isSpatiallyIndexed())
320
//                            {
321
//                                    if(layer.getISpatialIndex() != null)
322
//                                {
323
//                                        PluginServices.
324
//                                                                        cancelableBackgroundExecution(new CreateSpatialIndexMonitorableTask((FLyrVect)layer));
325
//                                                }
326
//                            }
327

    
328
                        isStop=true;
329
                }else {
330
//                         Si no existe writer para la capa que tenemos en edici?n
331
                        int resp = JOptionPane
332
                                        .showConfirmDialog(
333
                                                        (Component) PluginServices.getMainFrame(),
334
                                                        PluginServices
335
                                                                        .getText(
336
                                                                                        this,
337
                                                                                        "no_existe_writer_para_este_formato_de_capa_o_no_tiene_permisos_de_escritura_los_datos_no_se_guardaran_desea_continuar")
338
                                                                        + " : " + layer.getName(),
339
                                                        PluginServices.getText(this, "cancelar_edicion"),
340
                                                        JOptionPane.YES_NO_OPTION);
341
                        if (resp == JOptionPane.YES_OPTION) { // CANCEL EDITING
342
                                try {
343
                                        cancelEdition(layer);
344
                                        layer.setEditing(false);
345
//                                if (!(layer.getSource().getDriver() instanceof IndexedShpDriver)){
346
//                                        VectorialLayerEdited vle=(VectorialLayerEdited)CADExtension.getEditionManager().getLayerEdited(layer);
347
//                                        layer.setLegend((IVectorLegend)vle.getLegend());
348
//                                }
349
                                } catch (CancelEditingLayerException e) {
350
                                        PluginServices.getLogger().error(e.getMessage(),e);
351
//                                        NotificationManager.addError(e.getMessage(),e);
352
                                        return isStop;
353
                                }
354
                                isStop=true;
355
                        }
356

    
357
                }
358
//                boolean isStop=stopEditing((FLyrVect)layer, null);
359
                if (isStop){
360
                        layer.removeLayerListener(edMan);
361
//                        if (layer instanceof FLyrAnnotation){
362
//                                FLyrAnnotation lva=(FLyrAnnotation)layer;
363
//                    lva.setMapping(lva.getMapping());
364
//                        }
365
                        org.gvsig.andami.ui.mdiManager.IWindow f = PluginServices.getMDIManager()
366
                                .getActiveWindow();
367
                        if (f instanceof DefaultViewPanel) {
368
                                vista = (DefaultViewPanel) f;
369
                                FLayer auxLayer=vista.getMapControl().getMapContext().getLayers().getLayer(layer.getName());
370
                                if (auxLayer!=null && auxLayer.equals(layer)) {
371
                                        vista.getMapControl().setTool("zoomIn");
372
                                        vista.hideConsole();
373
                                        vista.repaintMap();
374
                                        CADExtension.clearView();
375
                                }
376
                        }
377
                }
378
                } catch (ReadException e1) {
379
//                        PluginServices.getLogger().error(e1.getMessage(),e1);
380
                        NotificationManager.showMessageError(e1.getMessage(),e1);
381
                } catch (StartEditionLayerException e) {
382
//                        PluginServices.getLogger().error(e.getMessage(),e);
383
                        NotificationManager.showMessageError(e.getMessage(),e);
384
                } catch (DataException e) {
385
                        NotificationManager.showMessageError(e.getMessage(),e);
386
                }
387
                return isStop;
388
        }
389

    
390
        private class UnsavedLayer extends UnsavedData{
391

    
392
                private FLayer layer;
393

    
394
                public UnsavedLayer(IExtension extension) {
395
                        super(extension);
396
                }
397

    
398
                public String getDescription() {
399
                        return PluginServices.getText(this,"editing_layer_unsaved");
400
                }
401

    
402
                public String getResourceName() {
403
                        return layer.getName();
404
                }
405

    
406

    
407

    
408
                public boolean saveData() {
409
                        return executeSaveLayer((FLyrVect)layer);
410
                }
411

    
412
                public void setLayer(FLayer layer) {
413
                        this.layer=layer;
414

    
415
                }
416

    
417
                public String getIcon() {
418
                        return layer.getTocImageIcon();
419
                }
420

    
421
        }
422

    
423
        /**
424
         * <p>This class provides the status of extensions.
425
         * If this extension has some unsaved editing layer (and save them), and methods
426
         * to check if the extension has some associated background tasks.
427
         *
428
         * @author Vicente Caballero Navarro
429
         *
430
         */
431
        private class StopEditingStatus implements IExtensionStatus {
432
                /**
433
             * This method is used to check if this extension has some unsaved editing layer.
434
             *
435
             * @return true if the extension has some unsaved editing layer, false otherwise.
436
             */
437
                public boolean hasUnsavedData() {
438
//                        ProjectExtension pe=(ProjectExtension)PluginServices.getExtension(ProjectExtension.class);
439
//                        IProject project = pe.getProject();
440
                        Project project = ProjectManager.getInstance().getCurrentProject();
441
                        DefaultViewDocument[] views=project.getDocuments(ViewManager.TYPENAME).toArray(new DefaultViewDocument[0]);
442
                        for (int i=0;i<views.length;i++) {
443
                                FLayers layers=views[i].getMapContext().getLayers();
444
                                LayersIterator iter = getEditingLayer(layers);
445
                                if (iter.hasNext()) {
446
                                        return true;
447
                                }
448
                        }
449
                        return false;
450
                }
451
                /**
452
             * This method is used to check if the extension has some associated
453
             * background process which is currently running.
454
             *
455
             * @return true if the extension has some associated background process,
456
             * false otherwise.
457
             */
458
                public boolean hasRunningProcesses() {
459
                        return false;
460
                }
461
                 /**
462
             * <p>Gets an array of the traceable background tasks associated with this
463
             * extension. These tasks may be tracked, canceled, etc.</p>
464
             *
465
             * @return An array of the associated background tasks, or null in case there is
466
             * no associated background tasks.
467
             */
468
                public IMonitorableTask[] getRunningProcesses() {
469
                        return null;
470
                }
471
                /**
472
             * <p>Gets an array of the UnsavedData objects, which contain information about
473
             * the unsaved editing layers and allows to save it.</p>
474
             *
475
             * @return An array of the associated unsaved editing layers, or null in case the extension
476
             * has not unsaved editing layers.
477
             */
478
                public IUnsavedData[] getUnsavedData() {
479
//                        ProjectExtension pe=(ProjectExtension)PluginServices.getExtension(ProjectExtension.class);
480
//                        org.gvsig.app.project.Project project=pe.getProject();
481
                        Project project = ProjectManager.getInstance().getCurrentProject();
482
                        DefaultViewDocument[] views=project.getDocuments(ViewManager.TYPENAME).toArray(new DefaultViewDocument[0]);
483
                        ArrayList unsavedLayers=new ArrayList();
484
                        for (int i=0;i<views.length;i++) {
485
                                FLayers layers = views[i].getMapContext().getLayers();
486
                                LayersIterator iter = getEditingLayer(layers);
487
                                while (iter.hasNext()){
488
                                        UnsavedLayer ul=new UnsavedLayer(StopEditing.this);
489
                                        ul.setLayer(iter.nextLayer());
490
                                        unsavedLayers.add(ul);
491
                                }
492
                        }
493
                        return (IUnsavedData[])unsavedLayers.toArray(new IUnsavedData[0]);
494
                }
495
        }
496
        private LayersIterator getEditingLayer(FLayers layers){
497
                return new LayersIterator(layers){
498
                        public boolean evaluate(FLayer layer) {
499
                                return layer.isEditing();
500
                        }
501
                };
502
        }
503
}
504