Statistics
| Revision:

gvsig-vectorediting / org.gvsig.vectorediting / trunk / org.gvsig.vectorediting / org.gvsig.vectorediting.swing / org.gvsig.vectorediting.swing.impl / src / main / java / org / gvsig / vectorediting / swing / impl / DefaultEditingContext.java @ 1673

History | View | Annotate | Download (43.5 KB)

1 159 llmarques
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright ? 2007-2014 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 2
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 89 llmarques
 */
24 159 llmarques
25 89 llmarques
package org.gvsig.vectorediting.swing.impl;
26
27
import java.awt.BorderLayout;
28
import java.awt.Component;
29 150 llmarques
import java.lang.ref.WeakReference;
30 876 jjdelcerro
import java.lang.reflect.InvocationTargetException;
31 366 llmarques
import java.util.ArrayList;
32 226 llmarques
import java.util.Set;
33
import java.util.Stack;
34 876 jjdelcerro
import java.util.logging.Level;
35 366 llmarques
import java.util.prefs.PreferenceChangeEvent;
36
import java.util.prefs.PreferenceChangeListener;
37
import java.util.prefs.Preferences;
38 89 llmarques
39 376 llmarques
import javax.swing.JComponent;
40 1234 jjdelcerro
import javax.swing.JOptionPane;
41 876 jjdelcerro
import javax.swing.SwingUtilities;
42 376 llmarques
43 226 llmarques
import org.gvsig.fmap.dal.exception.DataException;
44 1143 fdiaz
import org.gvsig.fmap.dal.feature.FeatureSelection;
45 89 llmarques
import org.gvsig.fmap.dal.feature.FeatureStore;
46 226 llmarques
import org.gvsig.fmap.geom.Geometry;
47
import org.gvsig.fmap.geom.GeometryLocator;
48
import org.gvsig.fmap.geom.GeometryManager;
49
import org.gvsig.fmap.geom.primitive.Point;
50
import org.gvsig.fmap.geom.type.GeometryType;
51 89 llmarques
import org.gvsig.fmap.mapcontext.MapContext;
52
import org.gvsig.fmap.mapcontext.layers.CancelationException;
53
import org.gvsig.fmap.mapcontext.layers.FLayer;
54
import org.gvsig.fmap.mapcontext.layers.FLayers;
55
import org.gvsig.fmap.mapcontext.layers.LayerCollectionEvent;
56
import org.gvsig.fmap.mapcontext.layers.LayerCollectionListener;
57
import org.gvsig.fmap.mapcontext.layers.LayerEvent;
58
import org.gvsig.fmap.mapcontext.layers.LayerListener;
59
import org.gvsig.fmap.mapcontext.layers.LayerPositionEvent;
60
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect;
61
import org.gvsig.fmap.mapcontrol.MapControl;
62
import org.gvsig.fmap.mapcontrol.tools.CompoundBehavior;
63
import org.gvsig.fmap.mapcontrol.tools.Behavior.Behavior;
64 226 llmarques
import org.gvsig.tools.ToolsLocator;
65
import org.gvsig.tools.i18n.I18nManager;
66 303 fdiaz
import org.gvsig.tools.observer.BaseNotification;
67
import org.gvsig.tools.observer.Notification;
68 157 llmarques
import org.gvsig.tools.observer.ObservableHelper;
69
import org.gvsig.tools.observer.Observer;
70 89 llmarques
import org.gvsig.utils.console.JDockPanel;
71
import org.gvsig.utils.console.ResponseListener;
72 226 llmarques
import org.gvsig.vectorediting.lib.api.EditingLocator;
73 89 llmarques
import org.gvsig.vectorediting.lib.api.EditingManager;
74 226 llmarques
import org.gvsig.vectorediting.lib.api.EditingService;
75
import org.gvsig.vectorediting.lib.api.EditingServiceInfo;
76
import org.gvsig.vectorediting.lib.api.EditingServiceParameter;
77
import org.gvsig.vectorediting.lib.api.EditingServiceParameter.TYPE;
78 89 llmarques
import org.gvsig.vectorediting.lib.api.exceptions.CreateEditingBehaviorException;
79
import org.gvsig.vectorediting.lib.api.exceptions.EndEditingException;
80 226 llmarques
import org.gvsig.vectorediting.lib.api.exceptions.InvalidEntryException;
81
import org.gvsig.vectorediting.lib.api.exceptions.ParsePointException;
82
import org.gvsig.vectorediting.lib.api.exceptions.ParseValueException;
83 157 llmarques
import org.gvsig.vectorediting.lib.api.exceptions.ServiceInformationException;
84 89 llmarques
import org.gvsig.vectorediting.lib.api.exceptions.StartEditingException;
85 226 llmarques
import org.gvsig.vectorediting.lib.api.exceptions.StartServiceException;
86
import org.gvsig.vectorediting.lib.api.exceptions.StopServiceException;
87 89 llmarques
import org.gvsig.vectorediting.lib.api.exceptions.VectorEditingException;
88
import org.gvsig.vectorediting.swing.api.EditingContext;
89
import org.gvsig.vectorediting.swing.api.EditingSwingLocator;
90
import org.gvsig.vectorediting.swing.api.EditingSwingManager;
91 376 llmarques
import org.gvsig.vectorediting.swing.api.console.EditingConsole;
92
import org.gvsig.vectorediting.swing.impl.console.DefaultEditingConsole;
93 1673 jjdelcerro
import org.gvsig.fmap.geom.primitive.Point;
94 842 fdiaz
95
import org.apache.commons.lang3.StringUtils;
96 1673 jjdelcerro
import org.gvsig.expressionevaluator.ExpressionUtils;
97
import org.gvsig.expressionevaluator.MutableSymbolTable;
98
import org.gvsig.fmap.dal.DataTypes;
99 1143 fdiaz
100 844 jjdelcerro
import org.gvsig.fmap.dal.EditingNotification;
101
import org.gvsig.fmap.dal.EditingNotificationManager;
102
import org.gvsig.fmap.dal.swing.DALSwingLocator;
103 1673 jjdelcerro
import org.gvsig.temporarystorage.TemporaryStorageGroup;
104
import org.gvsig.temporarystorage.TemporaryStorageLocator;
105
import org.gvsig.temporarystorage.TemporaryStorageManager;
106
import org.gvsig.tools.dataTypes.DataTypesManager;
107 1234 jjdelcerro
import org.gvsig.tools.swing.api.ToolsSwingLocator;
108
import org.gvsig.tools.swing.api.threadsafedialogs.ThreadSafeDialogsManager;
109 1143 fdiaz
110 547 llmarques
import org.slf4j.Logger;
111
import org.slf4j.LoggerFactory;
112 89 llmarques
113
public class DefaultEditingContext implements EditingContext {
114
115 109 llmarques
    private static final Logger logger = LoggerFactory
116
        .getLogger(EditingManager.class);
117 89 llmarques
118 150 llmarques
    private WeakReference<MapControl> mapControlReference;
119 89 llmarques
120 383 llmarques
    private WeakReference<MapContext> mapContextReference;
121 150 llmarques
122 109 llmarques
    private EditingCompoundBehavior editingCompoundBehavior;
123 89 llmarques
124 155 llmarques
    private Behavior[] lastAdditionalBehaviors;
125 150 llmarques
126 157 llmarques
    private ObservableHelper observableHelper;
127
128 376 llmarques
    private EditingConsole console;
129 89 llmarques
130 109 llmarques
    private JDockPanel dockConsole = null;
131 89 llmarques
132 109 llmarques
    private boolean isShowConsole = false;
133 89 llmarques
134 226 llmarques
    private Stack<EditingService> serviceStack;
135
136
    private FLyrVect currentLayer;
137
138
    private EditingServiceParameter currentParam;
139
140 438 fdiaz
    private Behavior[] defaultBehaviors;
141
142 109 llmarques
    private LayerListener layerListener = new LayerListener() {
143 89 llmarques
144 876 jjdelcerro
        @Override
145
        public void activationChanged(final LayerEvent e) {
146
            if( !SwingUtilities.isEventDispatchThread() ) {
147
                SwingUtilities.invokeLater(new Runnable() {
148
149
                    @Override
150
                    public void run() {
151
                        activationChanged(e);
152
                    }
153
                });
154
                return;
155
            }
156 1095 fdiaz
157 109 llmarques
            FLayer layer = e.getSource();
158 89 llmarques
159 127 llmarques
            FLayer[] activeLayers =
160
                layer.getMapContext().getLayers().getActives();
161 130 llmarques
162 227 llmarques
            if ((activeLayers.length == 1) && (layer instanceof FLyrVect)) {
163 127 llmarques
                if (layer.isActive() && layer.isEditing()) {
164 438 fdiaz
                    getMapControl().setTool("VectorEditing");
165
                    setCurrentLayer((FLyrVect) layer);
166 127 llmarques
                    showConsole();
167
                    return;
168
                }
169
            }
170 109 llmarques
171 127 llmarques
            hideConsole();
172 227 llmarques
            if ((getMapControl().getCurrentTool() != null)
173 205 llmarques
                && getMapControl().getCurrentTool().equals("VectorEditing")) {
174 197 llmarques
                getMapControl().setPrevTool();
175
            }
176 89 llmarques
        }
177 109 llmarques
178
        public void drawValueChanged(LayerEvent e) {
179 89 llmarques
        }
180
181 876 jjdelcerro
        @Override
182
        public void editionChanged(final LayerEvent e) {
183
            if( !SwingUtilities.isEventDispatchThread() ) {
184
                SwingUtilities.invokeLater(new Runnable() {
185
186
                    @Override
187
                    public void run() {
188
                        editionChanged(e);
189
                    }
190
                });
191
                return;
192
            }
193 438 fdiaz
            FLayer layer = e.getSource();
194
195
            if (layer instanceof FLyrVect) {
196
                if (layer.isEditing()) {
197
                    synchronized (DefaultEditingContext.this) {
198 546 llmarques
                        beginEdition((FLyrVect) layer);
199 438 fdiaz
                        showConsole();
200
                    }
201
                } else {
202
                    hideConsole();
203
                }
204
            }
205
206 109 llmarques
        }
207 89 llmarques
208 109 llmarques
        public void nameChanged(LayerEvent e) {
209
        }
210 89 llmarques
211 109 llmarques
        public void visibilityChanged(LayerEvent e) {
212
        }
213
    };
214 89 llmarques
215 366 llmarques
    private PreferenceChangeListener preferenceChangeListener =
216
        new PreferenceChangeListener() {
217
218
            public void preferenceChange(PreferenceChangeEvent evt) {
219
                String key = evt.getKey();
220
                if (key.equalsIgnoreCase("apply-snappers")) {
221
                    boolean newValue = Boolean.parseBoolean(evt.getNewValue());
222
                    getMapControl().setRefentEnabled(newValue);
223
                }
224
            }
225
        };
226
227 109 llmarques
    public DefaultEditingContext(MapControl mapControl) {
228 226 llmarques
229 150 llmarques
        this.mapControlReference = new WeakReference<MapControl>(mapControl);
230 383 llmarques
        this.mapContextReference =
231
            new WeakReference<MapContext>(mapControl.getMapContext());
232 385 llmarques
        this.observableHelper = new ObservableHelper();
233 205 llmarques
234 226 llmarques
        this.serviceStack = new Stack<EditingService>();
235
236 163 llmarques
        addLayerListeners();
237 366 llmarques
        addPreferenceListener();
238 109 llmarques
    }
239 89 llmarques
240 366 llmarques
    private void addPreferenceListener() {
241
        Preferences prefs = Preferences.userRoot().node("snappers");
242
        prefs.addPreferenceChangeListener(preferenceChangeListener);
243
    }
244
245 109 llmarques
    public void activateService(String name) {
246 226 llmarques
247 227 llmarques
        if ((getMapControl() != null)
248
            && getMapControl().hasTool("VectorEditing")) {
249 226 llmarques
250 109 llmarques
            CompoundBehavior editingCompoundBehavior =
251
                getEditingCompoundBehavior();
252 142 llmarques
            getMapControl().setTool("VectorEditing");
253 109 llmarques
            editingCompoundBehavior.setDrawnBehavior(
254
                EditingCompoundBehavior.EDITING_INDEX, true);
255 226 llmarques
256
            EditingManager manager = EditingLocator.getManager();
257
258
            if (currentLayer != null) {
259
260
                EditingService service =
261 227 llmarques
                    manager.getEditingService(name,
262 383 llmarques
                        currentLayer.getFeatureStore(),
263
                        mapContextReference.get());
264 226 llmarques
265
                if (service != null) {
266
267
                    this.enableSelection(false);
268
269
                    try {
270
                        service.start();
271
                    } catch (StartServiceException e) {
272 252 llmarques
273 226 llmarques
                        logger.info(String.format(
274
                            "Can't start the service %1$s", service.getName()),
275
                            e);
276
                        cleanEditingContext();
277
                        return;
278 252 llmarques
279
                    } catch (InvalidEntryException e) {
280
281
                        I18nManager i18nManager = ToolsLocator.getI18nManager();
282 376 llmarques
                        showConsoleMessage("\n"
283
                            + i18nManager.getTranslation("invalid_option"));
284 226 llmarques
                    }
285
286
                    if (!serviceStack.isEmpty()
287
                        && !getActiveService().next().getTypes()
288 245 llmarques
                            .contains(TYPE.GEOMETRY)) {
289 226 llmarques
                        serviceStack.pop();
290
                    }
291
292
                    setActiveService(service);
293 252 llmarques
294 226 llmarques
                    getNextParameter();
295
                }
296
            }
297 109 llmarques
        }
298
    }
299 89 llmarques
300 227 llmarques
    private void addBehaviors(Behavior[] additionalBehavior)
301
        throws CreateEditingBehaviorException {
302
303
        DefaultEditingBehavior editingBehavior;
304
        EditingCompoundBehavior editingCompoundBehavior;
305
306
        if (!getMapControl().hasTool("VectorEditing")) {
307
308
            editingBehavior = new DefaultEditingBehavior(this);
309
            editingCompoundBehavior =
310
                new EditingCompoundBehavior(editingBehavior);
311
            setCompoundBehavior(editingCompoundBehavior);
312
313
            if (additionalBehavior != null) {
314
315
                Behavior[] behaviors =
316
                    new Behavior[additionalBehavior.length + 1];
317
                behaviors[0] = editingCompoundBehavior;
318
319
                for (int i = 0; i < additionalBehavior.length; i++) {
320
                    behaviors[i + 1] = additionalBehavior[i];
321
                }
322
323
                getMapControl().addBehavior("VectorEditing", behaviors);
324
325
                lastAdditionalBehaviors = additionalBehavior;
326
327
            } else {
328
                getMapControl().addBehavior("VectorEditing",
329
                    editingCompoundBehavior);
330
            }
331
332
        } else {
333
            editingCompoundBehavior = getEditingCompoundBehavior();
334
            editingBehavior =
335
                (DefaultEditingBehavior) editingCompoundBehavior
336 245 llmarques
                    .getBehavior(EditingCompoundBehavior.EDITING_INDEX);
337 227 llmarques
            setCompoundBehavior(editingCompoundBehavior);
338
            cleanEditingContext();
339
        }
340
341
    }
342
343
    private void addLayerListeners() {
344
345 383 llmarques
        FLayers layers = mapContextReference.get().getLayers();
346 227 llmarques
347
        layers.addLayerListener(layerListener);
348
349
        layers.addLayerCollectionListener(new LayerCollectionListener() {
350
351 546 llmarques
            public void addLayer(FLayer layer) {
352
                if (layer instanceof FLayers) {
353
                    FLayers layers = (FLayers) layer;
354
                    for (int i = 0; i < layers.getLayersCount(); i++) {
355
                        addLayer(layers.getLayer(i));
356 227 llmarques
                    }
357 546 llmarques
                } else if (layer instanceof FLyrVect) {
358
                    ((FLyrVect) layer).addLayerListener(layerListener);
359 227 llmarques
                }
360
            }
361 842 fdiaz
362 546 llmarques
            public void layerAdded(LayerCollectionEvent e) {
363
                addLayer(e.getLayers());
364
            }
365 227 llmarques
366
            public void layerAdding(LayerCollectionEvent e)
367
                throws CancelationException {
368
            }
369
370
            public void layerMoved(LayerPositionEvent e) {
371
            }
372
373
            public void layerMoving(LayerPositionEvent e)
374
                throws CancelationException {
375
            }
376 842 fdiaz
377 547 llmarques
            public void removeLayer(FLayer layer) {
378
                if (layer instanceof FLayers) {
379
                    FLayers layers = (FLayers) layer;
380
                    for (int i = 0; i < layers.getLayersCount(); i++) {
381
                        addLayer(layers.getLayer(i));
382 227 llmarques
                    }
383 547 llmarques
                } else if (layer instanceof FLyrVect) {
384
                    ((FLyrVect) layer).removeLayerListener(layerListener);
385 227 llmarques
                }
386
            }
387
388 547 llmarques
            public void layerRemoved(LayerCollectionEvent e) {
389
                removeLayer(e.getLayers());
390
            }
391
392 227 llmarques
            public void layerRemoving(LayerCollectionEvent e)
393
                throws CancelationException {
394
            }
395
396
            public void visibilityChanged(LayerCollectionEvent e)
397
                throws CancelationException {
398
            }
399
        });
400
    }
401
402
    public void addObserver(Observer o) {
403
        this.observableHelper.addObserver(o);
404
    }
405
406
    private void askQuestion(EditingServiceParameter param) {
407 245 llmarques
        I18nManager i18nManager = ToolsLocator.getI18nManager();
408
        String translation = i18nManager.getTranslation(param.getDescription());
409
        String activeServiceName =
410
            i18nManager.getTranslation(getActiveService().getName());
411 365 llmarques
412 377 fdiaz
        Object defaultValue = param.getDefaultValue();
413
        String strDefaultValue;
414
415
        if (defaultValue != null) {
416 383 llmarques
            if (defaultValue instanceof String) {
417
                strDefaultValue = (String) defaultValue;
418
                strDefaultValue =
419
                    i18nManager.getTranslation((String) defaultValue);
420 377 fdiaz
            } else {
421 842 fdiaz
                if(defaultValue instanceof Point){
422
                    Point point = (Point)defaultValue;
423
                    StringBuilder builder = new StringBuilder();
424
                    for(int i=0; i<point.getDimension(); i++){
425
                        builder.append(point.getCoordinateAt(i));
426
                        if(i < point.getDimension()-1){
427
                            builder.append(" , ");
428
                        }
429
                    }
430
                    strDefaultValue = builder.toString();
431
                } else {
432
                    strDefaultValue = defaultValue.toString();
433
                }
434 377 fdiaz
            }
435 383 llmarques
            showConsoleMessage("\n" + activeServiceName + "# " + translation
436
                + "<" + strDefaultValue + "> : ");
437 365 llmarques
        } else {
438
            showConsoleMessage("\n" + activeServiceName + "# " + translation
439
                + " : ");
440
        }
441 227 llmarques
    }
442 842 fdiaz
443 438 fdiaz
    public synchronized void beginEdition(FLyrVect layer,
444
        Behavior[] additionalBehaviors) {
445 842 fdiaz
446 546 llmarques
        try{
447
            throw new Exception("Deprecated method");
448
        } catch (Exception e){
449
            logger.info("Deprecated method", e);
450
        }
451 842 fdiaz
452 546 llmarques
        beginEdition(layer);
453
        try {
454
            addBehaviors(additionalBehaviors);
455
        } catch (CreateEditingBehaviorException e1) {
456
            logger.info("Problems adding behaviors to editing context", e1);
457
            getMapControl().setTool("pan");
458
            return;
459
        }
460
    }
461 842 fdiaz
462
    public synchronized void beginEdition(FLyrVect layer) {
463
464 109 llmarques
        EditingNotificationManager editingNotificationManager =
465 844 jjdelcerro
            DALSwingLocator.getEditingNotificationManager();
466 89 llmarques
467 109 llmarques
        EditingNotification notification =
468
            editingNotificationManager.notifyObservers(this,
469 986 jjdelcerro
                EditingNotification.BEFORE_ENTER_EDITING_STORE, null, layer,layer.getFeatureStore());
470 89 llmarques
471 109 llmarques
        if (notification.isCanceled()) {
472 130 llmarques
            String msg =
473
                String.format("Edit layer %1$s canceled by somme observer.",
474
                    layer.getName());
475
            logger.info(msg, new StartEditingException(msg, null));
476
            return;
477 109 llmarques
        }
478 842 fdiaz
479 226 llmarques
        setCurrentLayer(layer);
480 127 llmarques
481 438 fdiaz
        FeatureStore featureStore = layer.getFeatureStore();
482
        if (!featureStore.isEditing()) {
483
            try {
484
                featureStore.edit();
485
            } catch (Exception e) {
486
                String msg =
487
                    String.format("Can't set %1$s in edit mode",
488
                        featureStore.getName());
489
                logger.info(msg, new VectorEditingException(e));
490
                cleanEditingContext();
491
                return;
492
            }
493 109 llmarques
        }
494 89 llmarques
495 438 fdiaz
        featureStore.addObserver(getMapControl());
496 89 llmarques
497 109 llmarques
        editingNotificationManager.notifyObservers(this,
498 986 jjdelcerro
            EditingNotification.AFTER_ENTER_EDITING_STORE, null, layer, layer.getFeatureStore());
499 150 llmarques
500 366 llmarques
        enableSnapping();
501 89 llmarques
    }
502
503 366 llmarques
    @SuppressWarnings({ "rawtypes", "unchecked" })
504
    private void enableSnapping() {
505
        Preferences prefs = Preferences.userRoot().node("snappers");
506 1095 fdiaz
         getMapControl().setRefentEnabled(prefs.getBoolean("apply-snappers", false));
507
         if (currentLayer != null) {
508 366 llmarques
            ArrayList layersToSnap =
509
                getMapControl().getMapContext().getLayersToSnap();
510
            if (!layersToSnap.contains(currentLayer)) {
511
                layersToSnap.add(currentLayer);
512
            }
513
        }
514
    }
515
516
    @SuppressWarnings("rawtypes")
517
    private void disableSnapping() {
518
        ArrayList layersToSnap =
519
            getMapControl().getMapContext().getLayersToSnap();
520
        if (layersToSnap.contains(currentLayer)) {
521
            layersToSnap.remove(currentLayer);
522
        }
523
    }
524
525 245 llmarques
    private void changeSelectedTool(String name) {
526 316 llmarques
        if (name.equalsIgnoreCase(DEFAULT_TOOL)) {
527 303 fdiaz
            name = "selection-simple-select-view";
528
            this.getMapControl().setTool("pointSelection");
529
        }
530 365 llmarques
        Notification notification =
531
            new BaseNotification(
532
                EditingContext.CHANGE_SELECTED_TOOL_NOTIFICATION,
533
                new Object[] { name });
534 303 fdiaz
        this.observableHelper.notifyObservers(this, notification);
535 245 llmarques
    }
536
537 227 llmarques
    private void cleanEditingContext() {
538
        serviceStack.clear();
539
        currentParam = null;
540
        this.enableSelection(false);
541
        refreshMenusAndToolBars();
542 89 llmarques
543 227 llmarques
        I18nManager i18nManager = ToolsLocator.getI18nManager();
544
        showConsoleMessage("\n" + i18nManager.getTranslation("select_new_tool")
545
            + "\n");
546
    }
547 127 llmarques
548 227 llmarques
    public void deleteObserver(Observer o) {
549
        this.observableHelper.deleteObserver(o);
550 109 llmarques
    }
551 89 llmarques
552 227 llmarques
    public void deleteObservers() {
553
        this.observableHelper.deleteObservers();
554 142 llmarques
    }
555
556 109 llmarques
    private void discardChanges(FLyrVect layer) throws EndEditingException {
557
        FeatureStore featureStore = layer.getFeatureStore();
558
        try {
559
            featureStore.cancelEditing();
560
        } catch (Exception e) {
561
            throw new EndEditingException(e);
562 89 llmarques
        }
563
    }
564
565 109 llmarques
    private void doAction(FLyrVect layer, int option) {
566 1234 jjdelcerro
        ThreadSafeDialogsManager dialogs = ToolsSwingLocator.getThreadSafeDialogsManager();
567
        I18nManager i18n = ToolsLocator.getI18nManager();
568
569 109 llmarques
        switch (option) {
570
        case SAVE_CHANGES:
571
            try {
572
                saveChanges(layer);
573
            } catch (VectorEditingException e) {
574 130 llmarques
                String msg =
575
                    String.format("Changes can not be saved in %1$s",
576
                        layer.getName());
577
                logger.info(msg, e);
578 1234 jjdelcerro
                dialogs.messageDialog(
579
                    i18n.getTranslation("_There_are_problems_saving_changes")+"\n\n"+
580
                    i18n.getTranslation("_See_error_log_for_more_information"),
581
                    null,
582
                    i18n.getTranslation("_Warning"),
583
                    JOptionPane.WARNING_MESSAGE,
584
                    "Vectorediting_cant_save_changes"
585
                );
586 161 llmarques
                return;
587 109 llmarques
            }
588
            break;
589 130 llmarques
590 109 llmarques
        case DISCARD_CHANGES:
591
            try {
592
                discardChanges(layer);
593
            } catch (VectorEditingException e) {
594 130 llmarques
                String msg =
595
                    String.format("Changes can not be discared in %1$s",
596
                        layer.getName());
597
                logger.info(msg, e);
598 1234 jjdelcerro
                dialogs.messageDialog(
599
                    i18n.getTranslation("_There_are_problems_discarding_changes")+"\n"+
600
//                    i18n.getTranslation("_The_layer_is_marked_as_temporary_to_avoid_possible_problems_when_trying_to_save_the_project")+"\n"+
601
                    "\n" + i18n.getTranslation("_See_error_log_for_more_information"),
602
                    null,
603
                    i18n.getTranslation("_Warning"),
604
                    JOptionPane.WARNING_MESSAGE,
605
                    "Vectorediting_cant_discard_changes"
606
                );
607 161 llmarques
                return;
608 109 llmarques
            }
609
            break;
610 130 llmarques
611 109 llmarques
        case EXPORT_LAYER:
612
            try {
613
                exportLayer(layer);
614
            } catch (VectorEditingException e) {
615 130 llmarques
                String msg =
616
                    String.format("Changes of %1$s can not be exported",
617
                        layer.getName());
618
                logger.info(msg, e);
619 1234 jjdelcerro
                dialogs.messageDialog(
620
                    i18n.getTranslation("_There_are_problems_exporting_changes")+"\n\n"+
621
                    i18n.getTranslation("_See_error_log_for_more_information"),
622
                    null,
623
                    i18n.getTranslation("_Warning"),
624
                    JOptionPane.WARNING_MESSAGE,
625
                    "Vectorediting_cant_export_changes"
626
                );
627 161 llmarques
                return;
628 109 llmarques
            }
629
            break;
630 130 llmarques
631 109 llmarques
        case CANCEL:
632 126 llmarques
            return;
633 109 llmarques
        }
634 127 llmarques
635 226 llmarques
        cleanEditingContext();
636 126 llmarques
        hideConsole();
637 366 llmarques
        disableSnapping();
638 302 fdiaz
        changeSelectedTool(DEFAULT_TOOL);
639 127 llmarques
640 126 llmarques
        FeatureStore featureStore = layer.getFeatureStore();
641 142 llmarques
        featureStore.deleteObserver(getMapControl());
642 89 llmarques
643 109 llmarques
    }
644 89 llmarques
645 226 llmarques
    private void enableSelection(boolean enableSelection) {
646 109 llmarques
        this.editingCompoundBehavior.setDrawnBehavior(
647
            EditingCompoundBehavior.SELECTION_INDEX, enableSelection);
648 89 llmarques
    }
649
650 109 llmarques
    public void endEdition(FLyrVect layer) {
651
        if (layer.isEditing()) {
652
            EditingNotificationManager editingNotificationManager =
653 844 jjdelcerro
                DALSwingLocator.getEditingNotificationManager();
654 89 llmarques
655 109 llmarques
            EditingNotification notification =
656
                editingNotificationManager.notifyObservers(this,
657 986 jjdelcerro
                    EditingNotification.BEFORE_EXIT_EDITING_STORE, null, layer, layer.getFeatureStore());
658 89 llmarques
659 109 llmarques
            if (notification.isCanceled()) {
660 130 llmarques
                String msg =
661
                    String.format(
662
                        "Stop edit layer %1$s canceled by somme observer.",
663
                        layer.getName());
664
                logger.info(msg, new EndEditingException(msg, null));
665 89 llmarques
666 109 llmarques
            }
667 130 llmarques
668 142 llmarques
            getMapControl().getCanceldraw().setCanceled(true);
669 109 llmarques
            int option;
670
            EditingSwingManager swingManager =
671
                EditingSwingLocator.getSwingManager();
672 130 llmarques
673 842 fdiaz
674 672 jjdelcerro
            if (layer.isWritable() && getMapControl().getProjection().equals(layer.getProjection()) ) {
675 109 llmarques
                option =
676 142 llmarques
                    swingManager.showPanelSaveOrDiscard(getMapControl(),
677 109 llmarques
                        layer.getName());
678
            } else {
679
                option =
680 142 llmarques
                    swingManager.showPanelExportOrDiscard(getMapControl(),
681 109 llmarques
                        layer.getName());
682
            }
683 89 llmarques
684 109 llmarques
            doAction(layer, option);
685 89 llmarques
686 109 llmarques
            editingNotificationManager.notifyObservers(this,
687 986 jjdelcerro
                EditingNotification.AFTER_EXIT_EDITING_STORE, null, layer, layer.getFeatureStore());
688 89 llmarques
689 109 llmarques
        }
690 89 llmarques
691
    }
692
693 109 llmarques
    private void exportLayer(FLyrVect layer) throws EndEditingException {
694 673 jjdelcerro
        Notification notification = new BaseNotification(EditingContext.EXPORT_LAYER_NOTIFICATION,1);
695
        notification.setValue(layer);
696
        this.observableHelper.notifyObservers(this, notification);
697 109 llmarques
    }
698 89 llmarques
699 227 llmarques
    protected void finishService() {
700
        EditingService lastService = serviceStack.pop();
701
        try {
702
703
            if (!serviceStack.isEmpty()
704
                && getActiveService().next().getTypes().contains(TYPE.GEOMETRY)) {
705
                Geometry geometry = lastService.finish();
706
                if (geometry != null) {
707
                    getActiveService().setValue(geometry);
708
                }
709
            } else {
710
                lastService.finishAndStore();
711
                getMapControl().rePaintDirtyLayers();
712 252 llmarques
713
                I18nManager i18nManager = ToolsLocator.getI18nManager();
714
                showConsoleMessage("\n"
715 316 llmarques
                    + i18nManager.getTranslation(lastService.getName()) + "# "
716 252 llmarques
                    + i18nManager.getTranslation("finished") + "\n");
717 484 fdiaz
                lastService.stop();
718 227 llmarques
                setActiveService(lastService);
719 484 fdiaz
                lastService.start();
720 316 llmarques
                changeSelectedTool(getActiveService().getName());
721 227 llmarques
            }
722
723
        } catch (InvalidEntryException ex) {
724
            I18nManager i18nManager = ToolsLocator.getI18nManager();
725 376 llmarques
            showConsoleMessage("\n"
726
                + i18nManager.getTranslation("invalid_option"));
727 316 llmarques
            changeSelectedTool(getActiveService().getName());
728 227 llmarques
        } catch (VectorEditingException ex) {
729
            logger.info("Can't finish " + lastService.getName(), ex);
730
            cleanEditingContext();
731
            return;
732
        }
733
734
        getNextParameter();
735
    }
736
737
    protected EditingService getActiveService() {
738
        if (!serviceStack.isEmpty()) {
739
            return serviceStack.peek();
740
        }
741
        return null;
742
    }
743
744 376 llmarques
    private EditingConsole getConsolePanel() {
745 109 llmarques
        if (console == null) {
746 376 llmarques
            console = new DefaultEditingConsole(new ResponseListener() {
747
748
                public void acceptResponse(String response) {
749
                    textEntered(response);
750
                }
751
            });
752 109 llmarques
        }
753
        return console;
754
    }
755 89 llmarques
756 227 llmarques
    protected FLyrVect getCurrentLayer() {
757
        return this.currentLayer;
758
    }
759
760
    protected EditingServiceParameter getCurrentParam() {
761
        return this.currentParam;
762
    }
763
764 109 llmarques
    private Component getDockConsole() {
765
        if (dockConsole == null) {
766 376 llmarques
            dockConsole = new JDockPanel((JComponent) getConsolePanel());
767 109 llmarques
        }
768
        return dockConsole;
769 89 llmarques
    }
770
771 224 llmarques
    private DefaultEditingBehavior getEditingBehavior() {
772 160 llmarques
        if (editingCompoundBehavior != null) {
773 224 llmarques
            return (DefaultEditingBehavior) editingCompoundBehavior
774 160 llmarques
                .getBehavior(EditingCompoundBehavior.EDITING_INDEX);
775
        }
776
        return null;
777 89 llmarques
    }
778
779 109 llmarques
    private EditingCompoundBehavior getEditingCompoundBehavior() {
780
        if (editingCompoundBehavior != null) {
781
            return editingCompoundBehavior;
782
        } else {
783
            EditingCompoundBehavior editingCompoundBehavior;
784 89 llmarques
785 109 llmarques
            CompoundBehavior compoundBehavior =
786 142 llmarques
                (CompoundBehavior) getMapControl().getMapTool("VectorEditing");
787 127 llmarques
788 109 llmarques
            if (compoundBehavior instanceof EditingCompoundBehavior) {
789
                editingCompoundBehavior =
790
                    (EditingCompoundBehavior) compoundBehavior;
791
            } else {
792
                editingCompoundBehavior =
793
                    (EditingCompoundBehavior) compoundBehavior.getBehavior(0);
794
            }
795 127 llmarques
796 109 llmarques
            setCompoundBehavior(editingCompoundBehavior);
797
            return editingCompoundBehavior;
798
        }
799 89 llmarques
    }
800
801 227 llmarques
    public MapControl getMapControl() {
802 323 llmarques
        return mapControlReference.get();
803 227 llmarques
    }
804
805 226 llmarques
    protected void getNextParameter() {
806 1143 fdiaz
        if ((getMapControl().getCurrentTool() != null)
807
            && !getMapControl().getCurrentTool().equals("VectorEditing")) {
808
            getMapControl().setTool("VectorEditing");
809
        }
810 226 llmarques
        currentParam = getActiveService().next();
811
812
        if (currentParam == null) {
813
            finishService();
814
        } else {
815
            askQuestion(currentParam);
816
            if (currentParam.getTypes().contains(TYPE.SELECTION)) {
817
                enableSelection(true);
818
            } else if (currentParam.getTypes().contains(TYPE.GEOMETRY)) {
819
                refreshMenusAndToolBars();
820
            }
821
        }
822
    }
823
824 227 llmarques
    protected Stack<EditingService> getServiceStack() {
825
        return this.serviceStack;
826
    }
827
828 109 llmarques
    private void hideConsole() {
829
        isShowConsole = false;
830 876 jjdelcerro
        if( !SwingUtilities.isEventDispatchThread() ) {
831
            try {
832
                SwingUtilities.invokeAndWait(new Runnable() {
833 1095 fdiaz
834 876 jjdelcerro
                    @Override
835
                    public void run() {
836
                        hideConsole();
837
                    }
838
                });
839
                return;
840
            } catch (InterruptedException | InvocationTargetException ex) {
841
                logger.warn("Can't hide editing console.",ex);
842
            }
843
            return;
844 1095 fdiaz
        }
845 109 llmarques
        getDockConsole().setVisible(false);
846
    }
847 89 llmarques
848 157 llmarques
    public boolean isServiceCompatible(String name) {
849 224 llmarques
        DefaultEditingBehavior editingBehavior = getEditingBehavior();
850 157 llmarques
851
        if (editingBehavior != null) {
852
853
            try {
854 226 llmarques
                EditingManager manager = EditingLocator.getManager();
855
                EditingServiceInfo serviceInfo = manager.getServiceInfo(name);
856
                GeometryType geoType = null;
857
858
                for (EditingService editingService : getServiceStack()) {
859 316 llmarques
                    EditingServiceParameter parameter = editingService.next();
860
                    if (parameter != null
861
                        && parameter.getTypes().contains(TYPE.GEOMETRY)) {
862 226 llmarques
863 316 llmarques
                        int geometryType = parameter.getGeometryType();
864 226 llmarques
                        int subType = -1;
865 316 llmarques
866 226 llmarques
                        try {
867
                            subType =
868
                                getCurrentLayer().getFeatureStore()
869 245 llmarques
                                    .getDefaultFeatureType()
870
                                    .getDefaultGeometryAttribute()
871
                                    .getGeomType().getSubType();
872 226 llmarques
873
                            geoType =
874
                                GeometryLocator.getGeometryManager()
875 245 llmarques
                                    .getGeometryType(geometryType, subType);
876 226 llmarques
                        } catch (Exception e) {
877
878
                            String msg =
879 316 llmarques
                                String.format(
880
                                    "Problems getting default feature"
881
                                        + " type of %1$s or getting geometry"
882
                                        + " type of %2$s %3$s",
883
                                    getCurrentLayer().getName(), geometryType,
884
                                    subType);
885 226 llmarques
886
                            throw new ServiceInformationException(msg, e);
887
                        }
888
889
                        return serviceInfo.isCompatibleWith(geoType)
890
                            && serviceInfo.createsNewGeometries();
891
                    }
892
                }
893
894
                if (getCurrentLayer() != null) {
895
                    try {
896
                        geoType =
897
                            getCurrentLayer().getFeatureStore()
898 245 llmarques
                                .getDefaultFeatureType()
899
                                .getDefaultGeometryAttribute().getGeomType();
900 226 llmarques
                    } catch (DataException e) {
901
                        String msg =
902
                            String.format("Problems getting default "
903
                                + "feature type of %1$s", getCurrentLayer()
904
                                .getName());
905
                        throw new ServiceInformationException(msg, e);
906
                    }
907
908
                    return serviceInfo.isCompatibleWith(geoType);
909
                }
910
911
                return false;
912 157 llmarques
            } catch (ServiceInformationException e) {
913 365 llmarques
                logger.warn(
914
                    "Problems getting if editing context is compatible with "
915
                        + name, e);
916 157 llmarques
            }
917
        }
918
        return false;
919
    }
920 1673 jjdelcerro
921 227 llmarques
    private Point parsePoint(String response) throws ParsePointException {
922 1673 jjdelcerro
        TemporaryStorageManager manager = TemporaryStorageLocator.getTemporaryStorageManager();
923
        TemporaryStorageGroup storage = manager.create("Points",Point.class);
924 157 llmarques
925 1673 jjdelcerro
        MutableSymbolTable symbolTable = ExpressionUtils.createSymbolTable();
926
        symbolTable.setVar("LAST_POINT_ADDED", storage.get("LAST_POINT_ADDED_IN_EDTING"));
927
928
        try {
929
            Object x = ExpressionUtils.evaluate(symbolTable, response);
930
            if( x instanceof Point ) {
931
                storage.put("LAST_POINT_ADDED_IN_EDTING", x);
932
                return (Point) x;
933 227 llmarques
            }
934 1673 jjdelcerro
        } catch(Exception ex) {
935
936
        }
937
        String s = "ST_MakePoint("+response+")";
938
        try {
939
            Object x = ExpressionUtils.evaluate(symbolTable, s);
940
            if( x instanceof Point ) {
941
                storage.put("LAST_POINT_ADDED_IN_EDTING", x);
942
                return (Point) x;
943 227 llmarques
            }
944 1673 jjdelcerro
        } catch(Exception ex) {
945
            throw new ParsePointException(ex);
946 226 llmarques
        }
947 1673 jjdelcerro
        throw new ParsePointException(null);
948 226 llmarques
    }
949 1673 jjdelcerro
950
    private Double parseValue(String response) throws ParseValueException {
951
        TemporaryStorageManager manager = TemporaryStorageLocator.getTemporaryStorageManager();
952
        TemporaryStorageGroup storage = manager.create("Points",Point.class);
953 226 llmarques
954 1673 jjdelcerro
        MutableSymbolTable symbolTable = ExpressionUtils.createSymbolTable();
955
        symbolTable.setVar("LAST_POINT_ADDED", storage.get("LAST_POINT_ADDED_IN_EDTING"));
956
957 227 llmarques
        try {
958 1673 jjdelcerro
            Object x = ExpressionUtils.evaluate(symbolTable, response);
959
            if( x instanceof Double ) {
960
                return (Double) x;
961
            }
962
            if( x==null ) {
963
                throw new ParseValueException(new NullPointerException());
964
            }
965
            DataTypesManager.Coercion toDouble = ToolsLocator.getDataTypesManager().get(DataTypes.DOUBLE).getCoercion();
966
            return (Double) toDouble.coerce(x);
967
        } catch(Exception ex) {
968
            throw new ParseValueException(ex);
969 227 llmarques
        }
970 226 llmarques
971
    }
972
973 227 llmarques
    protected void refreshMenusAndToolBars() {
974 876 jjdelcerro
        if (!SwingUtilities.isEventDispatchThread()) {
975
            SwingUtilities.invokeLater(new Runnable() {
976
977
                @Override
978
                public void run() {
979
                    refreshMenusAndToolBars();
980
                }
981
            });
982
            return;
983
        }
984
        Notification notification = new BaseNotification(
985
                EditingContext.REFRESH_TOOLS_NOTIFICATION,
986
                null
987
        );
988 303 fdiaz
        this.observableHelper.notifyObservers(this, notification);
989 226 llmarques
    }
990
991 227 llmarques
    private void saveChanges(FLyrVect layer) throws EndEditingException {
992
        FeatureStore featureStore = layer.getFeatureStore();
993 226 llmarques
        try {
994 227 llmarques
            featureStore.finishEditing();
995
        } catch (Exception e) {
996
            throw new EndEditingException(e);
997 226 llmarques
        }
998 227 llmarques
    }
999 226 llmarques
1000 227 llmarques
    private void setActiveService(EditingService service) {
1001
        serviceStack.add(service);
1002 226 llmarques
    }
1003
1004 227 llmarques
    private void setCompoundBehavior(EditingCompoundBehavior compoundBehavior) {
1005
        this.editingCompoundBehavior = compoundBehavior;
1006
    }
1007
1008 226 llmarques
    private void setCurrentLayer(FLyrVect layer) {
1009
1010
        if (this.currentLayer != layer) {
1011 438 fdiaz
            this.currentLayer = layer;
1012 226 llmarques
            cleanEditingContext();
1013
        }
1014
1015
    }
1016
1017 227 llmarques
    public void setMapControl(MapControl mapControl) {
1018 226 llmarques
1019 227 llmarques
        this.mapControlReference = new WeakReference<MapControl>(mapControl);
1020 383 llmarques
        this.mapContextReference =
1021
            new WeakReference<MapContext>(mapControl.getMapContext());
1022 227 llmarques
1023
        // When mapControl is updated we have to add older additional behaviors
1024
        // to new mapControl
1025
        if (lastAdditionalBehaviors != null) {
1026
            try {
1027
                addBehaviors(lastAdditionalBehaviors);
1028
            } catch (CreateEditingBehaviorException e1) {
1029
                logger.info("Problems adding behaviors to editing context", e1);
1030
                getMapControl().setTool("pan");
1031
                return;
1032
            }
1033
        }
1034 226 llmarques
    }
1035
1036 227 llmarques
    private void showConsole() {
1037
        if (isShowConsole) {
1038
            return;
1039
        }
1040 876 jjdelcerro
        if( !SwingUtilities.isEventDispatchThread() ) {
1041
            try {
1042
                SwingUtilities.invokeAndWait(new Runnable() {
1043 1095 fdiaz
1044 876 jjdelcerro
                    @Override
1045
                    public void run() {
1046
                        showConsole();
1047
                    }
1048
                });
1049
                return;
1050
            } catch (InterruptedException | InvocationTargetException ex) {
1051
                logger.warn("Can't show editing console.",ex);
1052
            }
1053
            return;
1054 1095 fdiaz
        }
1055 227 llmarques
        isShowConsole = true;
1056
        getMapControl().remove(getDockConsole());
1057
        getMapControl().setLayout(new BorderLayout());
1058
        getMapControl().add(getDockConsole(), BorderLayout.SOUTH);
1059
        getDockConsole().setVisible(true);
1060 226 llmarques
    }
1061
1062 876 jjdelcerro
    protected void showConsoleMessage(final String text) {
1063
        if (!SwingUtilities.isEventDispatchThread()) {
1064
            SwingUtilities.invokeLater(new Runnable() {
1065
1066
                @Override
1067
                public void run() {
1068
                    showConsoleMessage(text);
1069
                }
1070
            });
1071
            return;
1072
        }
1073 376 llmarques
        getConsolePanel().addText(text);
1074 227 llmarques
    }
1075
1076 226 llmarques
    protected void textEntered(String response) {
1077 472 fdiaz
        FeatureStore featureStore = getCurrentLayer().getFeatureStore();
1078 842 fdiaz
        EditingService activeService = getActiveService();
1079 226 llmarques
        if (response == null) {
1080 842 fdiaz
            if (activeService != null) {
1081 226 llmarques
                try {
1082 842 fdiaz
                    activeService.stop();
1083 302 fdiaz
                    serviceStack.pop();
1084 316 llmarques
                    if (serviceStack.isEmpty()) {
1085 472 fdiaz
                        featureStore
1086 302 fdiaz
                            .getFeatureSelection().deselectAll();
1087
                        changeSelectedTool(DEFAULT_TOOL);
1088
                    } else {
1089 842 fdiaz
                        changeSelectedTool(activeService.getName());
1090 302 fdiaz
                    }
1091 226 llmarques
1092 245 llmarques
                    refreshMenusAndToolBars();
1093 842 fdiaz
                    activeService = getActiveService();
1094
                    if (activeService != null) {
1095 302 fdiaz
                        getNextParameter();
1096
                    } else {
1097
                        cleanEditingContext();
1098
                    }
1099 245 llmarques
1100 226 llmarques
                } catch (StopServiceException e) {
1101
                    logger
1102 842 fdiaz
                        .info("Can't stop " + activeService.getName(), e);
1103 226 llmarques
                    return;
1104 245 llmarques
                } catch (DataException e) {
1105
                    logger.info("Can't get selection of "
1106 472 fdiaz
                        + featureStore.getFullName(), e);
1107 245 llmarques
                    return;
1108 226 llmarques
                }
1109
            }
1110
        } else {
1111
1112
            if (getCurrentParam() != null) {
1113
                Set<TYPE> types = getCurrentParam().getTypes();
1114
                Point point = null;
1115
                Double value = null;
1116 842 fdiaz
                Point defaultPoint = null;
1117
                Object defaultValue = getCurrentParam().getDefaultValue();
1118
//                int dimension = 2;
1119
//                if(defaultValue != null && defaultValue instanceof Point){
1120
                    defaultPoint = (Point)defaultValue;
1121
//                    dimension = defaultPoint.getDimension();
1122
//                }
1123 226 llmarques
1124
                boolean insertedValue = false;
1125 227 llmarques
                if ((!insertedValue && types.contains(TYPE.POSITION))
1126 226 llmarques
                    || types.contains(TYPE.LIST_POSITIONS)) {
1127
1128
                    try {
1129 842 fdiaz
                        if(StringUtils.isEmpty(response.replace("\n", ""))){
1130
                            point = defaultPoint;
1131
                        } else {
1132
                            point = parsePoint(response);
1133
                        }
1134 226 llmarques
1135
                        if (point != null) {
1136 842 fdiaz
                            activeService.setValue(point);
1137 226 llmarques
                            insertedValue = true;
1138
                        }
1139
1140
                    } catch (VectorEditingException e) {
1141
                        // Do nothing to try other types
1142
                    }
1143
                }
1144
                if (!insertedValue && types.contains(TYPE.VALUE)) {
1145
1146
                    try {
1147
1148
                        value = parseValue(response);
1149
                        if (value != null) {
1150 842 fdiaz
                            activeService.setValue(value);
1151 226 llmarques
                            insertedValue = true;
1152
                        }
1153
1154
                    } catch (VectorEditingException e) {
1155
                        // Do nothing to try other types
1156
                    }
1157
1158
                }
1159
                if (!insertedValue && types.contains(TYPE.OPTION)) {
1160
1161
                    try {
1162
1163
                        response = response.replace("\n", "");
1164
                        if (response != null) {
1165 842 fdiaz
                            activeService.setValue(response);
1166 226 llmarques
                            insertedValue = true;
1167
                        }
1168
1169
                    } catch (VectorEditingException e) {
1170
                        // Do nothing to try other types
1171
                    }
1172
                }
1173
                if (!insertedValue && types.contains(TYPE.SELECTION)) {
1174
                    if (response.equalsIgnoreCase("\n")) {
1175
                        enableSelection(false);
1176
                        insertedValue = true;
1177
1178
                        try {
1179
1180 1143 fdiaz
                            FeatureSelection clonedSelection = (FeatureSelection)featureStore
1181
                                .getFeatureSelection().clone();
1182
                            if(clonedSelection.isEmpty()){
1183
                               throw new InvalidEntryException(null);
1184
                            }
1185 842 fdiaz
                            activeService.setValue(
1186 1143 fdiaz
                                clonedSelection);
1187 226 llmarques
1188
                        } catch (InvalidEntryException e) {
1189
                            I18nManager i18nManager =
1190
                                ToolsLocator.getI18nManager();
1191 376 llmarques
                            showConsoleMessage("\n"
1192
                                + i18nManager.getTranslation("invalid_option"));
1193 226 llmarques
                        } catch (Exception e) {
1194 472 fdiaz
                            logger.info("Can't access to selection.", e);
1195 226 llmarques
                            cleanEditingContext();
1196
                            return;
1197
                        }
1198
                    }
1199
                }
1200
                if (!insertedValue) {
1201
                    I18nManager i18nManager = ToolsLocator.getI18nManager();
1202 376 llmarques
                    showConsoleMessage("\n"
1203
                        + i18nManager.getTranslation("invalid_option"));
1204 226 llmarques
                }
1205
                getNextParameter();
1206
            }
1207
        }
1208
    }
1209 438 fdiaz
1210
    public void setDefaultBehaviors(Behavior[] defaultBehaviors) {
1211
        this.defaultBehaviors = defaultBehaviors;
1212 546 llmarques
        try {
1213
            addBehaviors(defaultBehaviors);
1214
        } catch (CreateEditingBehaviorException e1) {
1215
            logger.info("Problems adding behaviors to editing context", e1);
1216
            getMapControl().setTool("pan");
1217
            return;
1218
        }
1219 438 fdiaz
    }
1220
1221
    public Behavior[] getDefaultBehaviors() {
1222
        return this.defaultBehaviors;
1223
    }
1224 89 llmarques
}