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 @ 41335

History | View | Annotate | Download (29.8 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.awt.BorderLayout;
27
import java.awt.Dimension;
28
import java.awt.ScrollPane;
29
import java.util.ArrayList;
30
import java.util.Iterator;
31
import java.util.List;
32
import java.util.logging.Level;
33

    
34
import javax.swing.JOptionPane;
35
import javax.swing.JPanel;
36
import javax.swing.SwingUtilities;
37

    
38
import org.gvsig.andami.PluginServices;
39
import org.gvsig.andami.messages.NotificationManager;
40
import org.gvsig.andami.ui.mdiManager.IWindow;
41
import org.gvsig.app.ApplicationLocator;
42
import org.gvsig.app.ApplicationManager;
43
import org.gvsig.app.project.documents.view.gui.DefaultViewPanel;
44
import org.gvsig.editing.layers.FactoryLayerEdited;
45
import org.gvsig.editing.layers.ILayerEdited;
46
import org.gvsig.editing.layers.VectorialLayerEdited;
47
import org.gvsig.fmap.dal.exception.DataException;
48
import org.gvsig.fmap.dal.exception.DataRuntimeException;
49
import org.gvsig.fmap.dal.exception.ReadException;
50
import org.gvsig.fmap.dal.feature.AbstractFeatureRule;
51
import org.gvsig.fmap.dal.feature.EditableFeature;
52
import org.gvsig.fmap.dal.feature.Feature;
53
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
54
import org.gvsig.fmap.dal.feature.FeatureRule;
55
import org.gvsig.fmap.dal.feature.FeatureRules;
56
import org.gvsig.fmap.dal.feature.FeatureSelection;
57
import org.gvsig.fmap.dal.feature.FeatureStore;
58
import org.gvsig.fmap.dal.feature.FeatureType;
59
import org.gvsig.fmap.dal.feature.rule.CheckMandatoryAttributesRule;
60
import org.gvsig.fmap.geom.Geometry;
61
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
62
import org.gvsig.fmap.geom.GeometryLocator;
63
import org.gvsig.fmap.geom.GeometryManager;
64
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
65
import org.gvsig.fmap.geom.primitive.Envelope;
66
import org.gvsig.fmap.mapcontext.exceptions.CancelEditingLayerException;
67
import org.gvsig.fmap.mapcontext.layers.CancelationException;
68
import org.gvsig.fmap.mapcontext.layers.FLayer;
69
import org.gvsig.fmap.mapcontext.layers.LayerCollectionEvent;
70
import org.gvsig.fmap.mapcontext.layers.LayerCollectionListener;
71
import org.gvsig.fmap.mapcontext.layers.LayerEvent;
72
import org.gvsig.fmap.mapcontext.layers.LayerListener;
73
import org.gvsig.fmap.mapcontext.layers.LayerPositionEvent;
74
import org.gvsig.fmap.mapcontext.layers.SpatialCache;
75
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
76
import org.gvsig.fmap.mapcontrol.MapControl;
77
import org.gvsig.fmap.mapcontrol.MapControlLocator;
78
import org.gvsig.fmap.mapcontrol.MapControlManager;
79
import org.gvsig.fmap.mapcontrol.swing.dynobject.DynObjectEditor;
80
import org.gvsig.gui.beans.buttonspanel.ButtonsPanel;
81
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelEvent;
82
import org.gvsig.gui.beans.buttonspanel.ButtonsPanelListener;
83
import org.gvsig.tools.dynobject.DynObject;
84
import org.gvsig.tools.observer.ObservableHelper;
85
import org.gvsig.tools.observer.Observer;
86
import org.gvsig.tools.service.ServiceException;
87
import org.gvsig.tools.swing.api.ToolsSwingLocator;
88
import org.gvsig.tools.swing.api.dynobject.DynObjectSwingManager;
89
import org.gvsig.tools.swing.api.dynobject.JDynObjectComponent;
90
import org.gvsig.tools.swing.api.windowmanager.WindowManager;
91
import org.gvsig.tools.task.RunnableWithParameters;
92
import org.slf4j.Logger;
93
import org.slf4j.LoggerFactory;
94

    
95
public class EditionManager implements LayerListener, LayerCollectionListener, IEditionManager {
96

    
97
    private static final Logger logger = LoggerFactory
98
            .getLogger(EditionManager.class);
99

    
100
    final private List<ILayerEdited> editedLayers = new ArrayList<ILayerEdited>();
101
    final private GeometryManager geomManager = GeometryLocator.getGeometryManager();
102
    final private MapControlManager mapControlManager = MapControlLocator.getMapControlManager();
103

    
104
    private MapControl mapCtrl = null;
105
    private ILayerEdited ile = null;
106

    
107
    public ILayerEdited getLayerEdited(FLayer lyr) {
108
        ILayerEdited aux = null;
109
        for ( int i = 0; i < editedLayers.size(); i++ ) {
110
            aux = editedLayers.get(i);
111
            if ( aux.getLayer() == lyr ) {
112
                return aux;
113
            }
114
        }
115
        return null;
116
    }
117

    
118
    public void editLayer(FLyrVect lv, DefaultViewPanel vista)
119
            throws DataException {
120

    
121
        EditingNotificationManager editingNotificationManager = MapControlLocator.getEditingNotificationManager();
122

    
123
        EditingNotification notification = editingNotificationManager.notifyObservers(
124
                this,
125
                EditingNotification.BEFORE_ENTER_EDITING_STORE,
126
                vista == null ? null : vista.getDocument(),
127
                lv);
128

    
129
        if ( notification.isCanceled() ) {
130
            logger.info("Edit layer '" + lv.getName() + "' canceled by somme observer.");
131
            throw new CancelException("Edit layer '" + lv.getName() + "' canceled.");
132
        }
133

    
134
        CADExtension.initFocus();
135
        vista.showConsole();
136
        MapControl mapControl = vista.getMapControl();
137
        this.setMapControl(mapControl);
138

    
139
        lv.addLayerListener(this);
140

    
141
        // This line also enables the spatial cache for snapping:
142
        lv.getFeatureStore().edit();
143
        lv.getFeatureStore().addObserver(vista);
144

    
145
        ApplicationLocator.getManager().refreshMenusAndToolBars();
146

    
147
        lv.getFeatureStore().addObserver(mapControl);
148
        StartEditing.startCommandsApplicable(vista, lv);
149

    
150
        // vista.repaintMap();
151
        // ----------------------------
152
        // this forces a true repaint:
153
        lv.drawValueChanged(LayerEvent.createDrawValuesChangedEvent(lv, ""));
154

    
155
        editingNotificationManager.notifyObservers(
156
                this,
157
                EditingNotification.AFTER_ENTER_EDITING_STORE,
158
                vista == null ? null : vista.getDocument(),
159
                lv);
160
    }
161

    
162
    public void visibilityChanged(LayerEvent e) {
163
    }
164

    
165
    public void activationChanged(LayerEvent e) {
166
        if ( e.getSource().isActive() ) {
167
            ile = getLayerEdited(e.getSource());
168
        }
169
        if ( ile == null || ile.getLayer().equals(e.getSource()) ) {
170

    
171
            if ( ile != null && !ile.getLayer().isActive() ) {
172
                VectorialLayerEdited lastVLE = (VectorialLayerEdited) ile;
173
                lastVLE.activationLost(e);
174
            }
175
            if ( e.getSource() instanceof FLyrVect ) {
176
                VectorialLayerEdited vle = null;
177
                vle = (VectorialLayerEdited) getLayerEdited(e.getSource());
178
                ile = vle;
179
                if ( getMapControl() != null && vle != null
180
                        && vle.getLayer().isActive() ) {
181
                    getMapControl().setTool("cadtooladapter");
182
                    vle.activationGained(e);
183
                    return;
184
                }
185
            }
186
            if ( getMapControl() != null ) {
187
                getMapControl().setTool("zoomIn");
188
                PluginServices.getMainFrame().setSelectedTool("ZOOM_IN");
189
            }
190
        }
191
    }
192

    
193
    public void nameChanged(LayerEvent e) {
194
    }
195

    
196
    public void editionChanged(LayerEvent e) {
197
        ILayerEdited lyrEdit = getLayerEdited(e.getSource());
198

    
199
        // Si no est? en la lista, comprobamos que est? en edici?n
200
        // y lo a?adimos
201
        if ( (lyrEdit == null) && e.getSource().isEditing() ) {
202
            lyrEdit
203
                    = FactoryLayerEdited.createLayerEdited(e.getSource(), mapCtrl);
204
            editedLayers.add(lyrEdit);
205
            if ( getMapControl() != null ) {
206
                getMapControl().setTool("cadtooladapter");
207
                CADExtension.setCADTool("_selection", true);
208
            }
209
            PluginServices.getMainFrame().setSelectedTool("_selection");
210
            // idActiveLayer = editedLayers.size() - 1;
211
            ile = getLayerEdited(e.getSource());
212
            logger.debug("NUEVA CAPA EN EDICION: {}", lyrEdit.getLayer().getName());
213

    
214
            // Ponemos el resto de temas desactivados
215
            if ( mapCtrl != null ) {
216
                mapCtrl.getMapContext().getLayers().setActive(false);
217
            }
218
            // y activamos el nuevo.
219
            e.getSource().setActive(true);
220

    
221
            if ( e.getSource() instanceof FLyrVect ) {
222
                FLyrVect fLyrVect = (FLyrVect) e.getSource();
223
                ((VectorialLayerEdited) lyrEdit)
224
                        .setEditionChangeManager(new EditionChangeManager(fLyrVect));
225
            }
226
        } else {
227
            for ( int i = 0; i < editedLayers.size(); i++ ) {
228
                VectorialLayerEdited vle
229
                        = (VectorialLayerEdited) editedLayers.get(i);
230
                if ( vle.equals(lyrEdit) ) {
231
                    editedLayers.remove(i);
232
                    ile = null;
233
                    return;
234
                }
235
            }
236
        }
237

    
238
    }
239

    
240
    public ILayerEdited getActiveLayerEdited() {
241
        return ile;
242
    }
243

    
244

    
245
    public MapControl getMapControl() {
246
        return mapCtrl;
247
    }
248

    
249
    public void setMapControl(MapControl mapCtrl) {
250
        if ( mapCtrl != null ) {
251
            this.mapCtrl = mapCtrl;
252
            mapCtrl.getMapContext().getLayers().addLayerListener(this);
253
            mapCtrl.getMapContext().getLayers()
254
                    .addLayerCollectionListener(this);
255
        }
256
    }
257

    
258
    public void layerAdded(LayerCollectionEvent e) {
259
        // Nothing to do
260

    
261
    }
262

    
263
    public void layerMoved(LayerPositionEvent e) {
264
        // Nothing to do
265
    }
266

    
267
    public void layerRemoved(LayerCollectionEvent e) {
268
        VectorialLayerEdited vle
269
                = (VectorialLayerEdited) getActiveLayerEdited();
270
        if ( vle != null && vle.getLayer().isActive() ) {
271
            try {
272
                vle.clearSelection();
273
            } catch (DataException e1) {
274
                NotificationManager.addError(e1);
275
            }
276
            editedLayers.remove(vle);
277
            getMapControl().setTool("zoomIn");
278
            FLyrVect lv = (FLyrVect) vle.getLayer();
279
            if ( e.getAffectedLayer().equals(lv) ) {
280
                IWindow window
281
                        = PluginServices.getMDIManager().getActiveWindow();
282
                if ( window instanceof DefaultViewPanel ) {
283
                    DefaultViewPanel view = (DefaultViewPanel) window;
284
                    view.hideConsole();
285
                    view.validate();
286
                    view.repaint();
287
                }
288
            }
289
        }
290
        PluginServices.getMainFrame().enableControls();
291
    }
292

    
293
    public void layerAdding(LayerCollectionEvent e) throws CancelationException {
294
        // Nothing to do
295
    }
296

    
297
    public void layerMoving(LayerPositionEvent e) throws CancelationException {
298
        // Nothing to do
299
    }
300

    
301
    public void layerRemoving(LayerCollectionEvent e)
302
            throws CancelationException {
303
        // Nothing to do
304
    }
305

    
306
    public void activationChanged(LayerCollectionEvent e)
307
            throws CancelationException {
308
        // Nothing to do
309
    }
310

    
311
    public void visibilityChanged(LayerCollectionEvent e)
312
            throws CancelationException {
313
        // Nothing to do
314
    }
315

    
316
    public void drawValueChanged(LayerEvent e) {
317
        // Nothing to do
318
    }
319

    
320
//    protected boolean validateFeature(EditableFeature feature, FeatureStore featureStore) {
321
//        while( true ) {
322
//            if( !needAskUser(feature, featureStore) ) {
323
//                // La feature se puede validar y no precisa de intervencion del
324
//                // usuario. Retornamos true.
325
//                return true;
326
//            }
327
//            // No se habia podido validar la feature, se piden al
328
//            // usuario los datos que faltan.
329
//            AskRequiredAttributtes ask = new AskRequiredAttributtes();
330
//            ask.showDialog(feature, featureStore);
331
//            if( ask.userCancel() ) {
332
//                // No se habia podido validar la feature, se le han pedido al
333
//                // usuario los datos que faltan y este ha pulsado en cancel,
334
//                // asi que la feature no se puede validar, y retornamos
335
//                // false.
336
//                return false;
337
//            }
338
//        }
339
//    }
340
//    
341
//    private boolean needAskUser(EditableFeature feature, FeatureStore featureStore) {
342
//        FeatureType featureType = feature.getType();
343
//        FeatureAttributeDescriptor[] attributeDescriptors = featureType.getAttributeDescriptors();
344
//
345
//        for ( int i = 0; i < attributeDescriptors.length; i++ ) {
346
//            FeatureAttributeDescriptor attrDesc = attributeDescriptors[i];
347
//            if ( attrDesc.isAutomatic() ) {
348
//                break;
349
//            }
350
//            if ( (attrDesc.isPrimaryKey() || !attrDesc.allowNull())
351
//                    && feature.get(attrDesc.getName()) == null ) {
352
//                return true;
353
//            }
354
//        }
355
//        return false;
356
//    }
357
//
358
//    private class AskRequiredAttributtes {
359
//
360
//        private boolean userCancelValue = true;
361
//
362
//        public boolean userCancel() {            
363
//            return this.userCancelValue;
364
//        }
365
//        
366
//        public void showDialog(final EditableFeature feature,
367
//                final FeatureStore store) {
368
//
369
//            if ( !SwingUtilities.isEventDispatchThread() ) {
370
//                try {
371
//                    SwingUtilities.invokeAndWait(new Runnable() {
372
//                        public void run() {
373
//                            showDialog(feature, store);
374
//                        }
375
//                    }
376
//                    );
377
//                } catch (Exception e1) {
378
//                    message("Can't show form to fill need data.",e1);
379
//                    this.userCancelValue = true;
380
//                    return;
381
//                }
382
//            }
383
//
384
//            try {
385
//                DynObject data = feature.getAsDynObject();
386
//                DynObjectEditor editor;
387
//                editor = new DynObjectEditor(data);
388
//                editor.editObject(true);
389
//                if ( editor.isCanceled() ) {
390
//                    this.userCancelValue = true;
391
//                    message("User canceled.", null);
392
//                } else {
393
//                    editor.getData(data);
394
//                    this.userCancelValue = false;
395
//                }
396
//            } catch (Exception ex) {
397
//                message("Can't show form to fill need data.",ex);
398
//                this.userCancelValue = true;
399
//            }
400
//        }
401
//        
402
//        private void message(String msg, Throwable ex) {
403
//            msg = msg + "\nSee to application log for more information.";
404
//            if( ex==null ) {
405
//                logger.warn(msg);
406
//            } else {
407
//                logger.warn(msg,ex);
408
//            }
409
//            ApplicationManager application = ApplicationLocator.getManager();
410
//            application.message(msg,JOptionPane.WARNING_MESSAGE);
411
//        }
412
//    }
413

    
414
    private VectorialLayerEdited getVLE() {
415
        return (VectorialLayerEdited) getActiveLayerEdited();
416
    }
417

    
418
    private void insertSpatialCache(Geometry geom)
419
            throws CreateEnvelopeException {
420
        VectorialLayerEdited vle = getVLE();
421
        SpatialCache spatialCache = ((FLyrVect) vle.getLayer())
422
                .getSpatialCache();
423
        Envelope r = geom.getEnvelope();
424
        if ( geom.getType() == Geometry.TYPES.POINT ) {
425
            r = geomManager.createEnvelope(r.getMinimum(0), r.getMinimum(1),
426
                    r.getMinimum(0) + 1, r.getMinimum(1) + 1, SUBTYPES.GEOM2D);
427
        }
428
        spatialCache.insert(r, geom);
429

    
430
    }
431

    
432
    private void updateSpatialCache(Geometry geom) {
433
        // TODO: no se actualizaban la cache espacial cuando se modifican las geometrias,
434
        // habria que hacerlo.
435
        logger.info("TODO: need update spatial cache");
436
    }
437

    
438
    private void removeSpatialCache(Geometry geom) {
439
        // TODO: no se actualizaban la cache espacial cuando se modifican las geometrias,
440
        // habria que hacerlo.
441
        logger.info("TODO: need update spatial cache");
442
    }
443

    
444
    private void draw(Geometry geometry, Feature feature) {
445
        if ( geometry != null ) {
446
            getMapControl().getMapControlDrawer()
447
                    .draw(geometry, mapControlManager.getGeometrySelectionSymbol());
448
        }
449
    }
450

    
451
    public Feature insertGeometry(Geometry geometry) {
452
        VectorialLayerEdited vle = getVLE();
453
        try {
454
            FeatureStore featureStore
455
                    = ((FLyrVect) vle.getLayer()).getFeatureStore();
456
            EditableFeature eFeature = featureStore.createNewFeature(true);
457

    
458
            //Reproject the geometry
459
            Geometry insertedGeometry = geometry;
460
            if ( getVLE().getLayer().getCoordTrans() != null ) {
461
                insertedGeometry = insertedGeometry.cloneGeometry();
462
                insertedGeometry.reProject(getVLE().getLayer().getCoordTrans().getInverted());
463
            }
464

    
465
            eFeature.setGeometry(featureStore.getDefaultFeatureType()
466
                    .getDefaultGeometryAttributeName(), insertedGeometry);
467

    
468
            EditingNotificationManager editingNotificationManager = MapControlLocator.getEditingNotificationManager();
469

    
470
            EditingNotification notification = editingNotificationManager.notifyObservers(
471
                    this,
472
                    EditingNotification.BEFORE_INSERT_FEATURE,
473
                    null,
474
                    vle.getLayer(),
475
                    featureStore,
476
                    eFeature
477
            );
478
            if ( notification.isCanceled() ) {
479
                return null;
480
            }
481
            if( !notification.shouldValidateTheFeature() ) {
482
                if ( !editingNotificationManager.validateFeature(eFeature) ) {
483
                    return null;
484
                }
485
            }
486

    
487
            featureStore.insert(eFeature);
488
            insertSpatialCache(insertedGeometry);
489
            draw(insertedGeometry, eFeature);
490

    
491
            editingNotificationManager.notifyObservers(
492
                    this,
493
                    EditingNotification.AFTER_INSERT_FEATURE,
494
                    null,
495
                    vle.getLayer(),
496
                    featureStore,
497
                    eFeature
498
            );
499

    
500
            return eFeature;
501
        } catch (Exception e) {
502
            NotificationManager.addError(e.getMessage(), e);
503
            return null;
504
        }
505
    }
506

    
507
    public Feature insertGeometry(Geometry geometry, Feature feature) {
508
        VectorialLayerEdited vle = getVLE();
509

    
510
        try {
511
            FeatureStore featureStore
512
                    = ((FLyrVect) vle.getLayer()).getFeatureStore();
513
            EditableFeature eFeature
514
                    = featureStore.createNewFeature(
515
                            featureStore.getDefaultFeatureType(), feature);
516
            eFeature.setGeometry(featureStore.getDefaultFeatureType()
517
                    .getDefaultGeometryAttributeName(), geometry);
518

    
519
            EditingNotificationManager editingNotificationManager = MapControlLocator.getEditingNotificationManager();
520

    
521
            EditingNotification notification = editingNotificationManager.notifyObservers(
522
                    this,
523
                    EditingNotification.BEFORE_INSERT_FEATURE,
524
                    null,
525
                    vle.getLayer(),
526
                    featureStore,
527
                    eFeature
528
            );
529
            if ( notification.isCanceled() ) {
530
                return null;
531
            }
532
            if( notification.shouldValidateTheFeature() ) {
533
                if ( !editingNotificationManager.validateFeature(eFeature) ) {
534
                    return null;
535
                }
536
            }
537

    
538
            featureStore.insert(eFeature);
539
            insertSpatialCache(geometry);
540
            draw(geometry, eFeature);
541

    
542
            editingNotificationManager.notifyObservers(
543
                    this,
544
                    EditingNotification.AFTER_INSERT_FEATURE,
545
                    null,
546
                    vle.getLayer(),
547
                    featureStore,
548
                    eFeature
549
            );
550

    
551
            return eFeature;
552
        } catch (Exception e) {
553
            NotificationManager.addError(e.getMessage(), e);
554
            return null;
555
        }
556
    }
557

    
558
    public Feature insertFeature(Feature feature) {
559
        VectorialLayerEdited vle = getVLE();
560

    
561
        try {
562

    
563
            FeatureStore featureStore
564
                    = ((FLyrVect) vle.getLayer()).getFeatureStore();
565
            EditableFeature eFeature
566
                    = featureStore.createNewFeature(
567
                            featureStore.getDefaultFeatureType(), feature);
568
            Geometry geometry = feature.getDefaultGeometry();
569

    
570
            EditingNotificationManager editingNotificationManager = MapControlLocator.getEditingNotificationManager();
571

    
572
            EditingNotification notification = editingNotificationManager.notifyObservers(
573
                    this,
574
                    EditingNotification.BEFORE_INSERT_FEATURE,
575
                    null,
576
                    vle.getLayer(),
577
                    featureStore,
578
                    eFeature
579
            );
580
            if ( notification.isCanceled() ) {
581
                return null;
582
            }
583
            if( notification.shouldValidateTheFeature() ) {
584
                if ( !editingNotificationManager.validateFeature(eFeature) ) {
585
                    return null;
586
                }
587
            }
588

    
589
            featureStore.insert(eFeature);
590
            insertSpatialCache(geometry);
591
            draw(geometry, eFeature);
592

    
593
            editingNotificationManager.notifyObservers(
594
                    this,
595
                    EditingNotification.AFTER_INSERT_FEATURE,
596
                    null,
597
                    vle.getLayer(),
598
                    featureStore,
599
                    eFeature
600
            );
601

    
602
            return eFeature;
603
        } catch (Exception e) {
604
            NotificationManager.addError(e.getMessage(), e);
605
            return null;
606
        }
607
    }
608

    
609
    public Feature insertAndSelectGeometry(String toolName, Geometry geometry) {
610
        Feature feature = null;
611
        try {
612
            FeatureStore featureStore = getVLE().getFeatureStore();
613
            featureStore.beginComplexNotification();
614
            featureStore.beginEditingGroup(toolName);
615
            FeatureSelection newSelection
616
                    = featureStore.createFeatureSelection();
617
            feature = insertGeometry(geometry);
618
            if ( feature != null ) {
619
                newSelection.select(feature);
620
                featureStore.setSelection(newSelection);
621
            }
622
            featureStore.endEditingGroup();
623
            featureStore.endComplexNotification();
624

    
625
        } catch (DataException e) {
626
            NotificationManager.showMessageError("insertAndSelectGeoemtry", e);
627
        }
628
        return feature;
629
    }
630

    
631
    public EditableFeature updateGeometry(FeatureStore store, Feature feature, Geometry geometry) {
632
        try {
633
            EditableFeature eFeature = feature.getEditable();
634
            eFeature.setGeometry(feature.getType()
635
                    .getDefaultGeometryAttributeName(), geometry);
636
            EditingNotificationManager editingNotificationManager = MapControlLocator.getEditingNotificationManager();
637

    
638
            EditingNotification notification = editingNotificationManager.notifyObservers(
639
                    this,
640
                    EditingNotification.BEFORE_UPDATE_FEATURE,
641
                    null,
642
                    getVLE().getLayer(),
643
                    store,
644
                    eFeature
645
            );
646
            if ( notification.isCanceled() ) {
647
                return null;
648
            }
649
            if( notification.shouldValidateTheFeature() ) {
650
                if ( !editingNotificationManager.validateFeature(eFeature) ) {
651
                    return null;
652
                }
653
            }
654

    
655
            store.update(eFeature);
656
            updateSpatialCache(geometry);
657

    
658
            editingNotificationManager.notifyObservers(
659
                    this,
660
                    EditingNotification.AFTER_UPDATE_FEATURE,
661
                    null,
662
                    getVLE().getLayer(),
663
                    store,
664
                    eFeature
665
            );
666

    
667
            return eFeature;
668
        } catch (Exception e) {
669
            NotificationManager.addError(e.getMessage(), e);
670
            return null;
671
        }
672

    
673
    }
674

    
675
    public EditableFeature updateFeature(FeatureStore store, Feature feature) {
676
        try {
677
            EditableFeature eFeature = feature.getEditable();
678
            EditingNotificationManager editingNotificationManager = MapControlLocator.getEditingNotificationManager();
679

    
680
            EditingNotification notification = editingNotificationManager.notifyObservers(
681
                    this,
682
                    EditingNotification.BEFORE_UPDATE_FEATURE,
683
                    null,
684
                    getVLE().getLayer(),
685
                    store,
686
                    eFeature
687
            );
688
            if ( notification.isCanceled() ) {
689
                return null;
690
            }
691
            if( notification.shouldValidateTheFeature() ) {
692
                if ( !editingNotificationManager.validateFeature(eFeature) ) {
693
                    return null;
694
                }
695
            }
696

    
697
            store.update(eFeature);
698
            updateSpatialCache(feature.getDefaultGeometry());
699
            editingNotificationManager.notifyObservers(
700
                    this,
701
                    EditingNotification.AFTER_UPDATE_FEATURE,
702
                    null,
703
                    getVLE().getLayer(),
704
                    store,
705
                    eFeature
706
            );
707

    
708
            return eFeature;
709
        } catch (Exception e) {
710
            NotificationManager.addError(e.getMessage(), e);
711
            return null;
712
        }
713

    
714
    }
715

    
716
    public void removeFeature(FeatureStore store, Feature feature) {
717
        try {
718
            EditingNotificationManager editingNotificationManager = MapControlLocator.getEditingNotificationManager();
719

    
720
            EditingNotification notification = editingNotificationManager.notifyObservers(
721
                    this,
722
                    EditingNotification.BEFORE_REMOVE_FEATURE,
723
                    null,
724
                    getVLE().getLayer(),
725
                    store,
726
                    feature
727
            );
728
            if ( notification.isCanceled() ) {
729
                return;
730
            }
731
            store.delete(feature);
732
            removeSpatialCache(feature.getDefaultGeometry());
733

    
734
            editingNotificationManager.notifyObservers(
735
                    this,
736
                    EditingNotification.AFTER_REMOVE_FEATURE,
737
                    null,
738
                    getVLE().getLayer(),
739
                    store,
740
                    feature
741
            );
742
        } catch (Exception e) {
743
            NotificationManager.addError(e.getMessage(), e);
744
        }
745

    
746
    }
747

    
748
    public void stopEditLayer(DefaultViewPanel view, FLyrVect lv, int mode)
749
            throws CancelEditingLayerException, ReadException {
750

    
751
        EditingNotificationManager editingNotificationManager = MapControlLocator.getEditingNotificationManager();
752

    
753
        EditingNotification notification = editingNotificationManager.notifyObservers(
754
                this,
755
                EditingNotification.BEFORE_EXIT_EDITING_STORE,
756
                view == null ? null : view.getDocument(),
757
                lv);
758
        if ( notification.isCanceled() ) {
759
            logger.info("Stop edit layer '" + lv.getName() + "' canceled by somme observer.");
760
            throw new CancelException("Stop edit layer '" + lv.getName() + "' canceled.");
761
        }
762

    
763
        VectorialLayerEdited lyrEd = null;
764
        FeatureStore featureStore = null;
765

    
766
        switch (mode) {
767

    
768
        case CANCEL_EDITING:
769
            lyrEd = (VectorialLayerEdited) this.getActiveLayerEdited();
770
            lv.getFeatureStore().deleteObserver(lyrEd);
771

    
772
            try {
773
                featureStore = lv.getFeatureStore();
774

    
775
                featureStore.cancelEditing();
776
            } catch (ReadException e) {
777
                throw new CancelEditingLayerException(lv.getName(), e);
778
            } catch (DataException e) {
779
                throw new CancelEditingLayerException(lv.getName(), e);
780
            }
781
            lv.getFeatureStore().deleteObserver(view);
782
            lv.removeLayerListener(this);
783

    
784
            if ( view != null ) {
785
                view.getMapControl().getMapContext().removeLayerDrawListener(lyrEd);
786
            }
787
            break;
788
        case ACCEPT_EDITING:
789
            featureStore = lv.getFeatureStore();
790
            try {
791

    
792
                /*
793
                 * This will throw a WriteException
794
                 * also if the provider does not accept
795
                 * the changes (added/updated/deleted/updated featuretype)  
796
                 */
797
                featureStore.finishEditing();
798

    
799
            } catch (DataException e) {
800
                throw new ReadException(lv.getName(), e);
801
            }
802
            lyrEd = (VectorialLayerEdited) this.getActiveLayerEdited();
803
            featureStore.deleteObserver(lyrEd);
804
            featureStore.deleteObserver(view);
805
            lv.removeLayerListener(this);
806
            if ( view != null ) {
807
                view.getMapControl().getMapContext().removeLayerDrawListener(lyrEd);
808
            }
809

    
810
            break;
811
        case CONTINUE_EDITING:
812
            break;
813
        }
814

    
815
        editingNotificationManager.notifyObservers(
816
                this,
817
                EditingNotification.AFTER_EXIT_EDITING_STORE,
818
                view == null ? null : view.getDocument(),
819
                lv);
820

    
821
    }
822

    
823
}