Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.editing.app / org.gvsig.editing.app.mainplugin / src / main / java / org / gvsig / editing / EditionManager.java @ 41564

History | View | Annotate | Download (28.5 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA 02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.editing;
25

    
26
import java.util.ArrayList;
27
import java.util.List;
28

    
29
import org.gvsig.andami.PluginServices;
30
import org.gvsig.andami.messages.NotificationManager;
31
import org.gvsig.andami.ui.mdiManager.IWindow;
32
import org.gvsig.app.ApplicationLocator;
33
import org.gvsig.app.project.documents.view.gui.DefaultViewPanel;
34
import org.gvsig.editing.layers.FactoryLayerEdited;
35
import org.gvsig.editing.layers.ILayerEdited;
36
import org.gvsig.editing.layers.VectorialLayerEdited;
37
import org.gvsig.fmap.dal.exception.DataException;
38
import org.gvsig.fmap.dal.exception.ReadException;
39
import org.gvsig.fmap.dal.feature.EditableFeature;
40
import org.gvsig.fmap.dal.feature.Feature;
41
import org.gvsig.fmap.dal.feature.FeatureSelection;
42
import org.gvsig.fmap.dal.feature.FeatureStore;
43
import org.gvsig.fmap.geom.Geometry;
44
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
45
import org.gvsig.fmap.geom.GeometryLocator;
46
import org.gvsig.fmap.geom.GeometryManager;
47
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
48
import org.gvsig.fmap.geom.primitive.Envelope;
49
import org.gvsig.fmap.mapcontext.exceptions.CancelEditingLayerException;
50
import org.gvsig.fmap.mapcontext.layers.CancelationException;
51
import org.gvsig.fmap.mapcontext.layers.FLayer;
52
import org.gvsig.fmap.mapcontext.layers.LayerCollectionEvent;
53
import org.gvsig.fmap.mapcontext.layers.LayerCollectionListener;
54
import org.gvsig.fmap.mapcontext.layers.LayerEvent;
55
import org.gvsig.fmap.mapcontext.layers.LayerListener;
56
import org.gvsig.fmap.mapcontext.layers.LayerPositionEvent;
57
import org.gvsig.fmap.mapcontext.layers.SpatialCache;
58
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
59
import org.gvsig.fmap.mapcontrol.MapControl;
60
import org.gvsig.fmap.mapcontrol.MapControlLocator;
61
import org.gvsig.fmap.mapcontrol.MapControlManager;
62
import org.slf4j.Logger;
63
import org.slf4j.LoggerFactory;
64

    
65
public class EditionManager implements LayerListener, LayerCollectionListener, IEditionManager {
66

    
67
    private static final Logger logger = LoggerFactory
68
            .getLogger(EditionManager.class);
69

    
70
    final private List<ILayerEdited> editedLayers = new ArrayList<ILayerEdited>();
71
    final private GeometryManager geomManager = GeometryLocator.getGeometryManager();
72
    final private MapControlManager mapControlManager = MapControlLocator.getMapControlManager();
73

    
74
    private MapControl mapCtrl = null;
75
    private ILayerEdited ile = null;
76

    
77
    public ILayerEdited getLayerEdited(FLayer lyr) {
78
        ILayerEdited aux = null;
79
        for ( int i = 0; i < editedLayers.size(); i++ ) {
80
            aux = editedLayers.get(i);
81
            if ( aux.getLayer() == lyr ) {
82
                return aux;
83
            }
84
        }
85
        return null;
86
    }
87

    
88
    public void editLayer(FLyrVect lv, DefaultViewPanel vista)
89
            throws DataException {
90

    
91
        EditingNotificationManager editingNotificationManager = MapControlLocator.getEditingNotificationManager();
92

    
93
        EditingNotification notification = editingNotificationManager.notifyObservers(
94
                this,
95
                EditingNotification.BEFORE_ENTER_EDITING_STORE,
96
                vista == null ? null : vista.getDocument(),
97
                lv);
98

    
99
        if ( notification.isCanceled() ) {
100
            logger.info("Edit layer '" + lv.getName() + "' canceled by somme observer.");
101
            throw new CancelException("Edit layer '" + lv.getName() + "' canceled.");
102
        }
103

    
104
        CADExtension.initFocus();
105
        vista.showConsole();
106
        MapControl mapControl = vista.getMapControl();
107
        this.setMapControl(mapControl);
108

    
109
        lv.addLayerListener(this);
110

    
111
        // This line also enables the spatial cache for snapping:
112
        lv.getFeatureStore().edit();
113
        lv.getFeatureStore().addObserver(vista);
114

    
115
        ApplicationLocator.getManager().refreshMenusAndToolBars();
116

    
117
        lv.getFeatureStore().addObserver(mapControl);
118
        StartEditing.startCommandsApplicable(vista, lv);
119

    
120
        // vista.repaintMap();
121
        // ----------------------------
122
        // this forces a true repaint:
123
        lv.drawValueChanged(LayerEvent.createDrawValuesChangedEvent(lv, ""));
124

    
125
        editingNotificationManager.notifyObservers(
126
                this,
127
                EditingNotification.AFTER_ENTER_EDITING_STORE,
128
                vista == null ? null : vista.getDocument(),
129
                lv);
130
    }
131

    
132
    public void visibilityChanged(LayerEvent e) {
133
    }
134

    
135
    public void activationChanged(LayerEvent e) {
136
        if ( e.getSource().isActive() ) {
137
            ile = getLayerEdited(e.getSource());
138
        }
139
        if ( ile == null || ile.getLayer().equals(e.getSource()) ) {
140

    
141
            if ( ile != null && !ile.getLayer().isActive() ) {
142
                VectorialLayerEdited lastVLE = (VectorialLayerEdited) ile;
143
                lastVLE.activationLost(e);
144
            }
145
            if ( e.getSource() instanceof FLyrVect ) {
146
                VectorialLayerEdited vle = null;
147
                vle = (VectorialLayerEdited) getLayerEdited(e.getSource());
148
                ile = vle;
149
                if ( getMapControl() != null && vle != null
150
                        && vle.getLayer().isActive() ) {
151
                    getMapControl().setTool("cadtooladapter");
152
                    vle.activationGained(e);
153
                    return;
154
                }
155
            }
156
            if ( getMapControl() != null ) {
157
                getMapControl().setTool("zoomIn");
158
                PluginServices.getMainFrame().setSelectedTool("ZOOM_IN");
159
            }
160
        }
161
    }
162

    
163
    public void nameChanged(LayerEvent e) {
164
    }
165

    
166
    public void editionChanged(LayerEvent e) {
167
        ILayerEdited lyrEdit = getLayerEdited(e.getSource());
168

    
169
        // Si no est? en la lista, comprobamos que est? en edici?n
170
        // y lo a?adimos
171
        if ( (lyrEdit == null) && e.getSource().isEditing() ) {
172
            lyrEdit
173
                    = FactoryLayerEdited.createLayerEdited(e.getSource(), mapCtrl);
174
            editedLayers.add(lyrEdit);
175
            if ( getMapControl() != null ) {
176
                getMapControl().setTool("cadtooladapter");
177
                CADExtension.setCADTool("_selection", true);
178
            }
179
            PluginServices.getMainFrame().setSelectedTool("_selection");
180
            // idActiveLayer = editedLayers.size() - 1;
181
            ile = getLayerEdited(e.getSource());
182
            logger.debug("NUEVA CAPA EN EDICION: {}", lyrEdit.getLayer().getName());
183

    
184
            // Ponemos el resto de temas desactivados
185
            if ( mapCtrl != null ) {
186
                mapCtrl.getMapContext().getLayers().setActive(false);
187
            }
188
            // y activamos el nuevo.
189
            e.getSource().setActive(true);
190

    
191
            if ( e.getSource() instanceof FLyrVect ) {
192
                FLyrVect fLyrVect = (FLyrVect) e.getSource();
193
                ((VectorialLayerEdited) lyrEdit)
194
                        .setEditionChangeManager(new EditionChangeManager(fLyrVect));
195
            }
196
        } else {
197
            for ( int i = 0; i < editedLayers.size(); i++ ) {
198
                VectorialLayerEdited vle
199
                        = (VectorialLayerEdited) editedLayers.get(i);
200
                if ( vle.equals(lyrEdit) ) {
201
                    editedLayers.remove(i);
202
                    ile = null;
203
                    return;
204
                }
205
            }
206
        }
207

    
208
    }
209

    
210
    public ILayerEdited getActiveLayerEdited() {
211
        return ile;
212
    }
213

    
214

    
215
    public MapControl getMapControl() {
216
        return mapCtrl;
217
    }
218

    
219
    public void setMapControl(MapControl mapCtrl) {
220
        if ( mapCtrl != null ) {
221
            this.mapCtrl = mapCtrl;
222
            mapCtrl.getMapContext().getLayers().addLayerListener(this);
223
            mapCtrl.getMapContext().getLayers()
224
                    .addLayerCollectionListener(this);
225
        }
226
    }
227

    
228
    public void layerAdded(LayerCollectionEvent e) {
229
        // Nothing to do
230

    
231
    }
232

    
233
    public void layerMoved(LayerPositionEvent e) {
234
        // Nothing to do
235
    }
236

    
237
    public void layerRemoved(LayerCollectionEvent e) {
238
        VectorialLayerEdited vle
239
                = (VectorialLayerEdited) getActiveLayerEdited();
240
        if ( vle != null && vle.getLayer().isActive() ) {
241
            try {
242
                vle.clearSelection();
243
            } catch (DataException e1) {
244
                NotificationManager.addError(e1);
245
            }
246
            editedLayers.remove(vle);
247
            getMapControl().setTool("zoomIn");
248
            FLyrVect lv = (FLyrVect) vle.getLayer();
249
            if ( e.getAffectedLayer().equals(lv) ) {
250
                IWindow window
251
                        = PluginServices.getMDIManager().getActiveWindow();
252
                if ( window instanceof DefaultViewPanel ) {
253
                    DefaultViewPanel view = (DefaultViewPanel) window;
254
                    view.hideConsole();
255
                    view.validate();
256
                    view.repaint();
257
                }
258
            }
259
        }
260
        PluginServices.getMainFrame().enableControls();
261
    }
262

    
263
    public void layerAdding(LayerCollectionEvent e) throws CancelationException {
264
        // Nothing to do
265
    }
266

    
267
    public void layerMoving(LayerPositionEvent e) throws CancelationException {
268
        // Nothing to do
269
    }
270

    
271
    public void layerRemoving(LayerCollectionEvent e)
272
            throws CancelationException {
273
        // Nothing to do
274
    }
275

    
276
    public void activationChanged(LayerCollectionEvent e)
277
            throws CancelationException {
278
        // Nothing to do
279
    }
280

    
281
    public void visibilityChanged(LayerCollectionEvent e)
282
            throws CancelationException {
283
        // Nothing to do
284
    }
285

    
286
    public void drawValueChanged(LayerEvent e) {
287
        // Nothing to do
288
    }
289

    
290
//    protected boolean validateFeature(EditableFeature feature, FeatureStore featureStore) {
291
//        while( true ) {
292
//            if( !needAskUser(feature, featureStore) ) {
293
//                // La feature se puede validar y no precisa de intervencion del
294
//                // usuario. Retornamos true.
295
//                return true;
296
//            }
297
//            // No se habia podido validar la feature, se piden al
298
//            // usuario los datos que faltan.
299
//            AskRequiredAttributtes ask = new AskRequiredAttributtes();
300
//            ask.showDialog(feature, featureStore);
301
//            if( ask.userCancel() ) {
302
//                // No se habia podido validar la feature, se le han pedido al
303
//                // usuario los datos que faltan y este ha pulsado en cancel,
304
//                // asi que la feature no se puede validar, y retornamos
305
//                // false.
306
//                return false;
307
//            }
308
//        }
309
//    }
310
//    
311
//    private boolean needAskUser(EditableFeature feature, FeatureStore featureStore) {
312
//        FeatureType featureType = feature.getType();
313
//        FeatureAttributeDescriptor[] attributeDescriptors = featureType.getAttributeDescriptors();
314
//
315
//        for ( int i = 0; i < attributeDescriptors.length; i++ ) {
316
//            FeatureAttributeDescriptor attrDesc = attributeDescriptors[i];
317
//            if ( attrDesc.isAutomatic() ) {
318
//                break;
319
//            }
320
//            if ( (attrDesc.isPrimaryKey() || !attrDesc.allowNull())
321
//                    && feature.get(attrDesc.getName()) == null ) {
322
//                return true;
323
//            }
324
//        }
325
//        return false;
326
//    }
327
//
328
//    private class AskRequiredAttributtes {
329
//
330
//        private boolean userCancelValue = true;
331
//
332
//        public boolean userCancel() {            
333
//            return this.userCancelValue;
334
//        }
335
//        
336
//        public void showDialog(final EditableFeature feature,
337
//                final FeatureStore store) {
338
//
339
//            if ( !SwingUtilities.isEventDispatchThread() ) {
340
//                try {
341
//                    SwingUtilities.invokeAndWait(new Runnable() {
342
//                        public void run() {
343
//                            showDialog(feature, store);
344
//                        }
345
//                    }
346
//                    );
347
//                } catch (Exception e1) {
348
//                    message("Can't show form to fill need data.",e1);
349
//                    this.userCancelValue = true;
350
//                    return;
351
//                }
352
//            }
353
//
354
//            try {
355
//                DynObject data = feature.getAsDynObject();
356
//                DynObjectEditor editor;
357
//                editor = new DynObjectEditor(data);
358
//                editor.editObject(true);
359
//                if ( editor.isCanceled() ) {
360
//                    this.userCancelValue = true;
361
//                    message("User canceled.", null);
362
//                } else {
363
//                    editor.getData(data);
364
//                    this.userCancelValue = false;
365
//                }
366
//            } catch (Exception ex) {
367
//                message("Can't show form to fill need data.",ex);
368
//                this.userCancelValue = true;
369
//            }
370
//        }
371
//        
372
//        private void message(String msg, Throwable ex) {
373
//            msg = msg + "\nSee to application log for more information.";
374
//            if( ex==null ) {
375
//                logger.warn(msg);
376
//            } else {
377
//                logger.warn(msg,ex);
378
//            }
379
//            ApplicationManager application = ApplicationLocator.getManager();
380
//            application.message(msg,JOptionPane.WARNING_MESSAGE);
381
//        }
382
//    }
383

    
384
    private VectorialLayerEdited getVLE() {
385
        return (VectorialLayerEdited) getActiveLayerEdited();
386
    }
387

    
388
    private void insertSpatialCache(Geometry geom)
389
            throws CreateEnvelopeException {
390
        VectorialLayerEdited vle = getVLE();
391
        SpatialCache spatialCache = ((FLyrVect) vle.getLayer())
392
                .getSpatialCache();
393
        Envelope r = geom.getEnvelope();
394
        if ( geom.getType() == Geometry.TYPES.POINT ) {
395
            r = geomManager.createEnvelope(r.getMinimum(0), r.getMinimum(1),
396
                    r.getMinimum(0) + 1, r.getMinimum(1) + 1, SUBTYPES.GEOM2D);
397
        }
398
        spatialCache.insert(r, geom);
399

    
400
    }
401

    
402
    private void updateSpatialCache(Geometry geom) {
403
        // TODO: no se actualizaban la cache espacial cuando se modifican las geometrias,
404
        // habria que hacerlo.
405
        logger.info("TODO: need update spatial cache");
406
    }
407

    
408
    private void removeSpatialCache(Geometry geom) {
409
        // TODO: no se actualizaban la cache espacial cuando se modifican las geometrias,
410
        // habria que hacerlo.
411
        logger.info("TODO: need update spatial cache");
412
    }
413

    
414
    private void draw(Geometry geometry, Feature feature) {
415
        if ( geometry != null ) {
416
            getMapControl().getMapControlDrawer()
417
                    .draw(geometry, mapControlManager.getGeometrySelectionSymbol());
418
        }
419
    }
420

    
421
    public Feature insertGeometry(Geometry geometry) {
422
        VectorialLayerEdited vle = getVLE();
423
        try {
424
            FeatureStore featureStore
425
                    = ((FLyrVect) vle.getLayer()).getFeatureStore();
426
            EditableFeature eFeature = featureStore.createNewFeature(true);
427

    
428
            //Reproject the geometry
429
            Geometry insertedGeometry = geometry;
430
            if ( getVLE().getLayer().getCoordTrans() != null ) {
431
                insertedGeometry = insertedGeometry.cloneGeometry();
432
                insertedGeometry.reProject(getVLE().getLayer().getCoordTrans().getInverted());
433
            }
434

    
435
            eFeature.setGeometry(featureStore.getDefaultFeatureType()
436
                    .getDefaultGeometryAttributeName(), insertedGeometry);
437

    
438
            EditingNotificationManager editingNotificationManager = MapControlLocator.getEditingNotificationManager();
439

    
440
            EditingNotification notification = editingNotificationManager.notifyObservers(
441
                    this,
442
                    EditingNotification.BEFORE_INSERT_FEATURE,
443
                    null,
444
                    vle.getLayer(),
445
                    featureStore,
446
                    eFeature
447
            );
448
            if ( notification.isCanceled() ) {
449
                return null;
450
            }
451
            if( !notification.shouldValidateTheFeature() ) {
452
                if ( !editingNotificationManager.validateFeature(eFeature) ) {
453
                    return null;
454
                }
455
            }
456

    
457
            featureStore.insert(eFeature);
458
            insertSpatialCache(insertedGeometry);
459
            draw(insertedGeometry, eFeature);
460

    
461
            editingNotificationManager.notifyObservers(
462
                    this,
463
                    EditingNotification.AFTER_INSERT_FEATURE,
464
                    null,
465
                    vle.getLayer(),
466
                    featureStore,
467
                    eFeature
468
            );
469

    
470
            return eFeature;
471
        } catch (Exception e) {
472
            NotificationManager.addError(e.getMessage(), e);
473
            return null;
474
        }
475
    }
476

    
477
    public Feature insertGeometry(Geometry geometry, Feature feature) {
478
        VectorialLayerEdited vle = getVLE();
479

    
480
        try {
481
            FeatureStore featureStore
482
                    = ((FLyrVect) vle.getLayer()).getFeatureStore();
483
            EditableFeature eFeature
484
                    = featureStore.createNewFeature(
485
                            featureStore.getDefaultFeatureType(), feature);
486
            eFeature.setGeometry(featureStore.getDefaultFeatureType()
487
                    .getDefaultGeometryAttributeName(), geometry);
488

    
489
            EditingNotificationManager editingNotificationManager = MapControlLocator.getEditingNotificationManager();
490

    
491
            EditingNotification notification = editingNotificationManager.notifyObservers(
492
                    this,
493
                    EditingNotification.BEFORE_INSERT_FEATURE,
494
                    null,
495
                    vle.getLayer(),
496
                    featureStore,
497
                    eFeature
498
            );
499
            if ( notification.isCanceled() ) {
500
                return null;
501
            }
502
            if( notification.shouldValidateTheFeature() ) {
503
                if ( !editingNotificationManager.validateFeature(eFeature) ) {
504
                    return null;
505
                }
506
            }
507

    
508
            featureStore.insert(eFeature);
509
            insertSpatialCache(geometry);
510
            draw(geometry, eFeature);
511

    
512
            editingNotificationManager.notifyObservers(
513
                    this,
514
                    EditingNotification.AFTER_INSERT_FEATURE,
515
                    null,
516
                    vle.getLayer(),
517
                    featureStore,
518
                    eFeature
519
            );
520

    
521
            return eFeature;
522
        } catch (Exception e) {
523
            NotificationManager.addError(e.getMessage(), e);
524
            return null;
525
        }
526
    }
527

    
528
    public Feature insertFeature(Feature feature) {
529
        VectorialLayerEdited vle = getVLE();
530

    
531
        try {
532

    
533
            FeatureStore featureStore
534
                    = ((FLyrVect) vle.getLayer()).getFeatureStore();
535
            EditableFeature eFeature
536
                    = featureStore.createNewFeature(
537
                            featureStore.getDefaultFeatureType(), feature);
538
            Geometry geometry = feature.getDefaultGeometry();
539

    
540
            EditingNotificationManager editingNotificationManager = MapControlLocator.getEditingNotificationManager();
541

    
542
            EditingNotification notification = editingNotificationManager.notifyObservers(
543
                    this,
544
                    EditingNotification.BEFORE_INSERT_FEATURE,
545
                    null,
546
                    vle.getLayer(),
547
                    featureStore,
548
                    eFeature
549
            );
550
            if ( notification.isCanceled() ) {
551
                return null;
552
            }
553
            if( notification.shouldValidateTheFeature() ) {
554
                if ( !editingNotificationManager.validateFeature(eFeature) ) {
555
                    return null;
556
                }
557
            }
558

    
559
            featureStore.insert(eFeature);
560
            insertSpatialCache(geometry);
561
            draw(geometry, eFeature);
562

    
563
            editingNotificationManager.notifyObservers(
564
                    this,
565
                    EditingNotification.AFTER_INSERT_FEATURE,
566
                    null,
567
                    vle.getLayer(),
568
                    featureStore,
569
                    eFeature
570
            );
571

    
572
            return eFeature;
573
        } catch (Exception e) {
574
            NotificationManager.addError(e.getMessage(), e);
575
            return null;
576
        }
577
    }
578

    
579
    public Feature insertAndSelectGeometry(String toolName, Geometry geometry) {
580
        Feature feature = null;
581
        try {
582
            FeatureStore featureStore = getVLE().getFeatureStore();
583
            featureStore.beginComplexNotification();
584
            featureStore.beginEditingGroup(toolName);
585
            FeatureSelection newSelection
586
                    = featureStore.createFeatureSelection();
587
            feature = insertGeometry(geometry);
588
            if ( feature != null ) {
589
                newSelection.select(feature);
590
                featureStore.setSelection(newSelection);
591
            }
592
            featureStore.endEditingGroup();
593
            featureStore.endComplexNotification();
594

    
595
        } catch (DataException e) {
596
            NotificationManager.showMessageError("insertAndSelectGeoemtry", e);
597
        }
598
        return feature;
599
    }
600

    
601
    public EditableFeature updateGeometry(FeatureStore store, Feature feature, Geometry geometry) {
602
        try {
603
            EditableFeature eFeature = feature.getEditable();
604
            eFeature.setGeometry(feature.getType()
605
                    .getDefaultGeometryAttributeName(), geometry);
606
            EditingNotificationManager editingNotificationManager = MapControlLocator.getEditingNotificationManager();
607

    
608
            EditingNotification notification = editingNotificationManager.notifyObservers(
609
                    this,
610
                    EditingNotification.BEFORE_UPDATE_FEATURE,
611
                    null,
612
                    getVLE().getLayer(),
613
                    store,
614
                    eFeature
615
            );
616
            if ( notification.isCanceled() ) {
617
                return null;
618
            }
619
            if( notification.shouldValidateTheFeature() ) {
620
                if ( !editingNotificationManager.validateFeature(eFeature) ) {
621
                    return null;
622
                }
623
            }
624

    
625
            store.update(eFeature);
626
            updateSpatialCache(geometry);
627

    
628
            editingNotificationManager.notifyObservers(
629
                    this,
630
                    EditingNotification.AFTER_UPDATE_FEATURE,
631
                    null,
632
                    getVLE().getLayer(),
633
                    store,
634
                    eFeature
635
            );
636

    
637
            return eFeature;
638
        } catch (Exception e) {
639
            NotificationManager.addError(e.getMessage(), e);
640
            return null;
641
        }
642

    
643
    }
644

    
645
    public EditableFeature updateFeature(FeatureStore store, Feature feature) {
646
        try {
647
            EditableFeature eFeature = feature.getEditable();
648
            EditingNotificationManager editingNotificationManager = MapControlLocator.getEditingNotificationManager();
649

    
650
            EditingNotification notification = editingNotificationManager.notifyObservers(
651
                    this,
652
                    EditingNotification.BEFORE_UPDATE_FEATURE,
653
                    null,
654
                    getVLE().getLayer(),
655
                    store,
656
                    eFeature
657
            );
658
            if ( notification.isCanceled() ) {
659
                return null;
660
            }
661
            if( notification.shouldValidateTheFeature() ) {
662
                if ( !editingNotificationManager.validateFeature(eFeature) ) {
663
                    return null;
664
                }
665
            }
666

    
667
            store.update(eFeature);
668
            updateSpatialCache(feature.getDefaultGeometry());
669
            editingNotificationManager.notifyObservers(
670
                    this,
671
                    EditingNotification.AFTER_UPDATE_FEATURE,
672
                    null,
673
                    getVLE().getLayer(),
674
                    store,
675
                    eFeature
676
            );
677

    
678
            return eFeature;
679
        } catch (Exception e) {
680
            NotificationManager.addError(e.getMessage(), e);
681
            return null;
682
        }
683

    
684
    }
685

    
686
    public void removeFeature(FeatureStore store, Feature feature) {
687
        try {
688
            EditingNotificationManager editingNotificationManager = MapControlLocator.getEditingNotificationManager();
689

    
690
            EditingNotification notification = editingNotificationManager.notifyObservers(
691
                    this,
692
                    EditingNotification.BEFORE_REMOVE_FEATURE,
693
                    null,
694
                    getVLE().getLayer(),
695
                    store,
696
                    feature
697
            );
698
            if ( notification.isCanceled() ) {
699
                return;
700
            }
701
            store.delete(feature);
702
            removeSpatialCache(feature.getDefaultGeometry());
703

    
704
            editingNotificationManager.notifyObservers(
705
                    this,
706
                    EditingNotification.AFTER_REMOVE_FEATURE,
707
                    null,
708
                    getVLE().getLayer(),
709
                    store,
710
                    feature
711
            );
712
        } catch (Exception e) {
713
            NotificationManager.addError(e.getMessage(), e);
714
        }
715

    
716
    }
717

    
718
    public void stopEditLayer(DefaultViewPanel view, FLyrVect lv, int mode)
719
            throws CancelEditingLayerException, ReadException {
720

    
721
        EditingNotificationManager editingNotificationManager = MapControlLocator.getEditingNotificationManager();
722

    
723
        EditingNotification notification = editingNotificationManager.notifyObservers(
724
                this,
725
                EditingNotification.BEFORE_EXIT_EDITING_STORE,
726
                view == null ? null : view.getDocument(),
727
                lv);
728
        if ( notification.isCanceled() ) {
729
            logger.info("Stop edit layer '" + lv.getName() + "' canceled by somme observer.");
730
            throw new CancelException("Stop edit layer '" + lv.getName() + "' canceled.");
731
        }
732

    
733
        VectorialLayerEdited lyrEd = null;
734
        FeatureStore featureStore = null;
735

    
736
        switch (mode) {
737

    
738
        case CANCEL_EDITING:
739
            lyrEd = (VectorialLayerEdited) this.getActiveLayerEdited();
740
            lv.getFeatureStore().deleteObserver(lyrEd);
741

    
742
            try {
743
                featureStore = lv.getFeatureStore();
744

    
745
                featureStore.cancelEditing();
746
            } catch (ReadException e) {
747
                throw new CancelEditingLayerException(lv.getName(), e);
748
            } catch (DataException e) {
749
                throw new CancelEditingLayerException(lv.getName(), e);
750
            }
751
            lv.getFeatureStore().deleteObserver(view);
752
            lv.removeLayerListener(this);
753

    
754
            if ( view != null ) {
755
                view.getMapControl().getMapContext().removeLayerDrawListener(lyrEd);
756
            }
757
            break;
758
        case ACCEPT_EDITING:
759
            featureStore = lv.getFeatureStore();
760
            try {
761

    
762
                /*
763
                 * This will throw a WriteException
764
                 * also if the provider does not accept
765
                 * the changes (added/updated/deleted/updated featuretype)  
766
                 */
767
                featureStore.finishEditing();
768

    
769
            } catch (DataException e) {
770
                throw new ReadException(lv.getName(), e);
771
            }
772
            lyrEd = (VectorialLayerEdited) this.getActiveLayerEdited();
773
            featureStore.deleteObserver(lyrEd);
774
            featureStore.deleteObserver(view);
775
            lv.removeLayerListener(this);
776
            if ( view != null ) {
777
                view.getMapControl().getMapContext().removeLayerDrawListener(lyrEd);
778
            }
779

    
780
            break;
781
        case CONTINUE_EDITING:
782
            break;
783
        }
784

    
785
        editingNotificationManager.notifyObservers(
786
                this,
787
                EditingNotification.AFTER_EXIT_EDITING_STORE,
788
                view == null ? null : view.getDocument(),
789
                lv);
790

    
791
    }
792

    
793
}