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 @ 316
History | View | Annotate | Download (32.9 KB)
1 |
/**
|
---|---|
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 |
*/
|
24 |
|
25 |
package org.gvsig.vectorediting.swing.impl; |
26 |
|
27 |
import java.awt.BorderLayout; |
28 |
import java.awt.Component; |
29 |
import java.lang.ref.WeakReference; |
30 |
import java.util.Set; |
31 |
import java.util.Stack; |
32 |
|
33 |
import org.slf4j.Logger; |
34 |
import org.slf4j.LoggerFactory; |
35 |
|
36 |
import org.gvsig.editing.EditingNotification; |
37 |
import org.gvsig.editing.EditingNotificationManager; |
38 |
import org.gvsig.fmap.dal.exception.DataException; |
39 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
40 |
import org.gvsig.fmap.geom.Geometry; |
41 |
import org.gvsig.fmap.geom.GeometryLocator; |
42 |
import org.gvsig.fmap.geom.GeometryManager; |
43 |
import org.gvsig.fmap.geom.primitive.Point; |
44 |
import org.gvsig.fmap.geom.type.GeometryType; |
45 |
import org.gvsig.fmap.mapcontext.MapContext; |
46 |
import org.gvsig.fmap.mapcontext.layers.CancelationException; |
47 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
48 |
import org.gvsig.fmap.mapcontext.layers.FLayers; |
49 |
import org.gvsig.fmap.mapcontext.layers.LayerCollectionEvent; |
50 |
import org.gvsig.fmap.mapcontext.layers.LayerCollectionListener; |
51 |
import org.gvsig.fmap.mapcontext.layers.LayerEvent; |
52 |
import org.gvsig.fmap.mapcontext.layers.LayerListener; |
53 |
import org.gvsig.fmap.mapcontext.layers.LayerPositionEvent; |
54 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
55 |
import org.gvsig.fmap.mapcontrol.MapControl; |
56 |
import org.gvsig.fmap.mapcontrol.MapControlLocator; |
57 |
import org.gvsig.fmap.mapcontrol.tools.CompoundBehavior; |
58 |
import org.gvsig.fmap.mapcontrol.tools.Behavior.Behavior; |
59 |
import org.gvsig.tools.ToolsLocator; |
60 |
import org.gvsig.tools.i18n.I18nManager; |
61 |
import org.gvsig.tools.observer.BaseNotification; |
62 |
import org.gvsig.tools.observer.Notification; |
63 |
import org.gvsig.tools.observer.ObservableHelper; |
64 |
import org.gvsig.tools.observer.Observer; |
65 |
import org.gvsig.utils.console.JConsole; |
66 |
import org.gvsig.utils.console.JDockPanel; |
67 |
import org.gvsig.utils.console.ResponseListener; |
68 |
import org.gvsig.utils.console.jedit.JEditTextArea; |
69 |
import org.gvsig.vectorediting.lib.api.EditingLocator; |
70 |
import org.gvsig.vectorediting.lib.api.EditingManager; |
71 |
import org.gvsig.vectorediting.lib.api.EditingService; |
72 |
import org.gvsig.vectorediting.lib.api.EditingServiceInfo; |
73 |
import org.gvsig.vectorediting.lib.api.EditingServiceParameter; |
74 |
import org.gvsig.vectorediting.lib.api.EditingServiceParameter.TYPE; |
75 |
import org.gvsig.vectorediting.lib.api.exceptions.CreateEditingBehaviorException; |
76 |
import org.gvsig.vectorediting.lib.api.exceptions.EndEditingException; |
77 |
import org.gvsig.vectorediting.lib.api.exceptions.InvalidEntryException; |
78 |
import org.gvsig.vectorediting.lib.api.exceptions.ParsePointException; |
79 |
import org.gvsig.vectorediting.lib.api.exceptions.ParseValueException; |
80 |
import org.gvsig.vectorediting.lib.api.exceptions.ServiceInformationException; |
81 |
import org.gvsig.vectorediting.lib.api.exceptions.StartEditingException; |
82 |
import org.gvsig.vectorediting.lib.api.exceptions.StartServiceException; |
83 |
import org.gvsig.vectorediting.lib.api.exceptions.StopServiceException; |
84 |
import org.gvsig.vectorediting.lib.api.exceptions.VectorEditingException; |
85 |
import org.gvsig.vectorediting.swing.api.EditingContext; |
86 |
import org.gvsig.vectorediting.swing.api.EditingSwingLocator; |
87 |
import org.gvsig.vectorediting.swing.api.EditingSwingManager; |
88 |
|
89 |
public class DefaultEditingContext implements EditingContext { |
90 |
|
91 |
private static final Logger logger = LoggerFactory |
92 |
.getLogger(EditingManager.class); |
93 |
|
94 |
private WeakReference<MapControl> mapControlReference; |
95 |
|
96 |
private MapContext mapContext;
|
97 |
|
98 |
private EditingCompoundBehavior editingCompoundBehavior;
|
99 |
|
100 |
private Behavior[] lastAdditionalBehaviors; |
101 |
|
102 |
private ObservableHelper observableHelper;
|
103 |
|
104 |
private JConsole console;
|
105 |
|
106 |
private JDockPanel dockConsole = null; |
107 |
|
108 |
private boolean isShowConsole = false; |
109 |
|
110 |
private Stack<EditingService> serviceStack; |
111 |
|
112 |
private FLyrVect currentLayer;
|
113 |
|
114 |
private EditingServiceParameter currentParam;
|
115 |
|
116 |
private LayerListener layerListener = new LayerListener() { |
117 |
|
118 |
public void activationChanged(LayerEvent e) { |
119 |
FLayer layer = e.getSource(); |
120 |
|
121 |
FLayer[] activeLayers =
|
122 |
layer.getMapContext().getLayers().getActives(); |
123 |
|
124 |
if ((activeLayers.length == 1) && (layer instanceof FLyrVect)) { |
125 |
if (layer.isActive() && layer.isEditing()) {
|
126 |
showConsole(); |
127 |
getMapControl().setTool("VectorEditing");
|
128 |
setCurrentLayer((FLyrVect) layer); |
129 |
return;
|
130 |
} |
131 |
} |
132 |
|
133 |
hideConsole(); |
134 |
if ((getMapControl().getCurrentTool() != null) |
135 |
&& getMapControl().getCurrentTool().equals("VectorEditing")) {
|
136 |
getMapControl().setPrevTool(); |
137 |
} |
138 |
} |
139 |
|
140 |
public void drawValueChanged(LayerEvent e) { |
141 |
} |
142 |
|
143 |
public void editionChanged(LayerEvent e) { |
144 |
} |
145 |
|
146 |
public void nameChanged(LayerEvent e) { |
147 |
} |
148 |
|
149 |
public void visibilityChanged(LayerEvent e) { |
150 |
} |
151 |
}; |
152 |
|
153 |
public DefaultEditingContext(MapControl mapControl) {
|
154 |
|
155 |
this.mapControlReference = new WeakReference<MapControl>(mapControl); |
156 |
this.mapContext = mapControl.getMapContext();
|
157 |
this.observableHelper = new ObservableHelper(); |
158 |
|
159 |
this.serviceStack = new Stack<EditingService>(); |
160 |
|
161 |
addLayerListeners(); |
162 |
addConsolePanelListeners(getConsolePanel()); |
163 |
} |
164 |
|
165 |
public void activateService(String name) { |
166 |
|
167 |
if ((getMapControl() != null) |
168 |
&& getMapControl().hasTool("VectorEditing")) {
|
169 |
|
170 |
CompoundBehavior editingCompoundBehavior = |
171 |
getEditingCompoundBehavior(); |
172 |
getMapControl().setTool("VectorEditing");
|
173 |
editingCompoundBehavior.setDrawnBehavior( |
174 |
EditingCompoundBehavior.EDITING_INDEX, true);
|
175 |
|
176 |
EditingManager manager = EditingLocator.getManager(); |
177 |
|
178 |
if (currentLayer != null) { |
179 |
|
180 |
EditingService service = |
181 |
manager.getEditingService(name, |
182 |
currentLayer.getFeatureStore()); |
183 |
|
184 |
if (service != null) { |
185 |
|
186 |
this.enableSelection(false); |
187 |
|
188 |
try {
|
189 |
service.start(); |
190 |
} catch (StartServiceException e) {
|
191 |
|
192 |
logger.info(String.format(
|
193 |
"Can't start the service %1$s", service.getName()),
|
194 |
e); |
195 |
cleanEditingContext(); |
196 |
return;
|
197 |
|
198 |
} catch (InvalidEntryException e) {
|
199 |
|
200 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
201 |
showConsoleMessage(i18nManager |
202 |
.getTranslation("invalid_option"));
|
203 |
} |
204 |
|
205 |
if (!serviceStack.isEmpty()
|
206 |
&& !getActiveService().next().getTypes() |
207 |
.contains(TYPE.GEOMETRY)) { |
208 |
serviceStack.pop(); |
209 |
} |
210 |
|
211 |
setActiveService(service); |
212 |
|
213 |
getNextParameter(); |
214 |
} |
215 |
} |
216 |
} |
217 |
} |
218 |
|
219 |
private void addBehaviors(Behavior[] additionalBehavior) |
220 |
throws CreateEditingBehaviorException {
|
221 |
|
222 |
DefaultEditingBehavior editingBehavior; |
223 |
EditingCompoundBehavior editingCompoundBehavior; |
224 |
|
225 |
if (!getMapControl().hasTool("VectorEditing")) { |
226 |
|
227 |
editingBehavior = new DefaultEditingBehavior(this); |
228 |
editingCompoundBehavior = |
229 |
new EditingCompoundBehavior(editingBehavior);
|
230 |
setCompoundBehavior(editingCompoundBehavior); |
231 |
|
232 |
if (additionalBehavior != null) { |
233 |
|
234 |
Behavior[] behaviors =
|
235 |
new Behavior[additionalBehavior.length + 1]; |
236 |
behaviors[0] = editingCompoundBehavior;
|
237 |
|
238 |
for (int i = 0; i < additionalBehavior.length; i++) { |
239 |
behaviors[i + 1] = additionalBehavior[i];
|
240 |
} |
241 |
|
242 |
getMapControl().addBehavior("VectorEditing", behaviors);
|
243 |
|
244 |
lastAdditionalBehaviors = additionalBehavior; |
245 |
|
246 |
} else {
|
247 |
getMapControl().addBehavior("VectorEditing",
|
248 |
editingCompoundBehavior); |
249 |
} |
250 |
|
251 |
} else {
|
252 |
editingCompoundBehavior = getEditingCompoundBehavior(); |
253 |
editingBehavior = |
254 |
(DefaultEditingBehavior) editingCompoundBehavior |
255 |
.getBehavior(EditingCompoundBehavior.EDITING_INDEX); |
256 |
setCompoundBehavior(editingCompoundBehavior); |
257 |
cleanEditingContext(); |
258 |
} |
259 |
|
260 |
} |
261 |
|
262 |
private void addConsolePanelListeners(JConsole jConsole) { |
263 |
jConsole.addResponseListener(new ResponseListener() {
|
264 |
|
265 |
public void acceptResponse(String response) { |
266 |
textEntered(response); |
267 |
} |
268 |
}); |
269 |
} |
270 |
|
271 |
private void addLayerListeners() { |
272 |
|
273 |
FLayers layers = mapContext.getLayers(); |
274 |
|
275 |
layers.addLayerListener(layerListener); |
276 |
|
277 |
layers.addLayerCollectionListener(new LayerCollectionListener() {
|
278 |
|
279 |
public void layerAdded(LayerCollectionEvent e) { |
280 |
FLayers layers2 = e.getLayers(); |
281 |
for (int i = 0; i < layers2.getLayersCount(); i++) { |
282 |
FLayer layer = layers2.getLayer(i); |
283 |
if (layer instanceof FLyrVect) { |
284 |
((FLyrVect) layer).addLayerListener(layerListener); |
285 |
} |
286 |
} |
287 |
} |
288 |
|
289 |
public void layerAdding(LayerCollectionEvent e) |
290 |
throws CancelationException {
|
291 |
} |
292 |
|
293 |
public void layerMoved(LayerPositionEvent e) { |
294 |
} |
295 |
|
296 |
public void layerMoving(LayerPositionEvent e) |
297 |
throws CancelationException {
|
298 |
} |
299 |
|
300 |
public void layerRemoved(LayerCollectionEvent e) { |
301 |
FLayers layers2 = e.getLayers(); |
302 |
for (int i = 0; i < layers2.getLayersCount(); i++) { |
303 |
FLayer layer = layers2.getLayer(i); |
304 |
if (layer instanceof FLyrVect) { |
305 |
((FLyrVect) layer).removeLayerListener(layerListener); |
306 |
} |
307 |
} |
308 |
} |
309 |
|
310 |
public void layerRemoving(LayerCollectionEvent e) |
311 |
throws CancelationException {
|
312 |
} |
313 |
|
314 |
public void visibilityChanged(LayerCollectionEvent e) |
315 |
throws CancelationException {
|
316 |
} |
317 |
}); |
318 |
} |
319 |
|
320 |
public void addObserver(Observer o) { |
321 |
this.observableHelper.addObserver(o);
|
322 |
} |
323 |
|
324 |
private void askQuestion(EditingServiceParameter param) { |
325 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
326 |
String translation = i18nManager.getTranslation(param.getDescription());
|
327 |
String activeServiceName =
|
328 |
i18nManager.getTranslation(getActiveService().getName()); |
329 |
showConsoleMessage("\n" + activeServiceName + "# " + translation |
330 |
+ " > ");
|
331 |
} |
332 |
|
333 |
public void beginEdition(FLyrVect layer, Behavior[] additionalBehaviors) { |
334 |
|
335 |
EditingNotificationManager editingNotificationManager = |
336 |
MapControlLocator.getEditingNotificationManager(); |
337 |
|
338 |
EditingNotification notification = |
339 |
editingNotificationManager.notifyObservers(this,
|
340 |
EditingNotification.BEFORE_ENTER_EDITING_STORE, null, layer);
|
341 |
|
342 |
if (notification.isCanceled()) {
|
343 |
String msg =
|
344 |
String.format("Edit layer %1$s canceled by somme observer.", |
345 |
layer.getName()); |
346 |
logger.info(msg, new StartEditingException(msg, null)); |
347 |
return;
|
348 |
} |
349 |
|
350 |
try {
|
351 |
addBehaviors(additionalBehaviors); |
352 |
} catch (CreateEditingBehaviorException e1) {
|
353 |
logger.info("Problems adding behaviors to editing context", e1);
|
354 |
getMapControl().setTool("pan");
|
355 |
return;
|
356 |
} |
357 |
|
358 |
setCurrentLayer(layer); |
359 |
|
360 |
try {
|
361 |
layer.getFeatureStore().edit(); |
362 |
} catch (Exception e) { |
363 |
String msg =
|
364 |
String.format("Can't set %1$s in edit mode", layer |
365 |
.getFeatureStore().getName()); |
366 |
logger.info(msg, new VectorEditingException(e));
|
367 |
cleanEditingContext(); |
368 |
return;
|
369 |
} |
370 |
|
371 |
layer.getFeatureStore().addObserver(getMapControl()); |
372 |
|
373 |
editingNotificationManager.notifyObservers(this,
|
374 |
EditingNotification.AFTER_ENTER_EDITING_STORE, null, layer);
|
375 |
|
376 |
getMapControl().setTool("VectorEditing");
|
377 |
showConsole(); |
378 |
} |
379 |
|
380 |
private void changeSelectedTool(String name) { |
381 |
if (name.equalsIgnoreCase(DEFAULT_TOOL)) {
|
382 |
name = "selection-simple-select-view";
|
383 |
this.getMapControl().setTool("pointSelection"); |
384 |
} |
385 |
Notification notification = new BaseNotification(EditingContext.CHANGE_SELECTED_TOOL_NOTIFICATION, new Object[]{name}); |
386 |
this.observableHelper.notifyObservers(this, notification); |
387 |
} |
388 |
|
389 |
private void cleanEditingContext() { |
390 |
serviceStack.clear(); |
391 |
currentParam = null;
|
392 |
this.enableSelection(false); |
393 |
refreshMenusAndToolBars(); |
394 |
|
395 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
396 |
showConsoleMessage("\n" + i18nManager.getTranslation("select_new_tool") |
397 |
+ "\n");
|
398 |
} |
399 |
|
400 |
public void deleteObserver(Observer o) { |
401 |
this.observableHelper.deleteObserver(o);
|
402 |
} |
403 |
|
404 |
public void deleteObservers() { |
405 |
this.observableHelper.deleteObservers();
|
406 |
} |
407 |
|
408 |
private void discardChanges(FLyrVect layer) throws EndEditingException { |
409 |
FeatureStore featureStore = layer.getFeatureStore(); |
410 |
try {
|
411 |
featureStore.cancelEditing(); |
412 |
} catch (Exception e) { |
413 |
throw new EndEditingException(e); |
414 |
} |
415 |
} |
416 |
|
417 |
private void doAction(FLyrVect layer, int option) { |
418 |
switch (option) {
|
419 |
case SAVE_CHANGES:
|
420 |
try {
|
421 |
saveChanges(layer); |
422 |
} catch (VectorEditingException e) {
|
423 |
String msg =
|
424 |
String.format("Changes can not be saved in %1$s", |
425 |
layer.getName()); |
426 |
logger.info(msg, e); |
427 |
return;
|
428 |
} |
429 |
break;
|
430 |
|
431 |
case DISCARD_CHANGES:
|
432 |
try {
|
433 |
discardChanges(layer); |
434 |
} catch (VectorEditingException e) {
|
435 |
String msg =
|
436 |
String.format("Changes can not be discared in %1$s", |
437 |
layer.getName()); |
438 |
logger.info(msg, e); |
439 |
return;
|
440 |
} |
441 |
break;
|
442 |
|
443 |
case EXPORT_LAYER:
|
444 |
try {
|
445 |
exportLayer(layer); |
446 |
} catch (VectorEditingException e) {
|
447 |
String msg =
|
448 |
String.format("Changes of %1$s can not be exported", |
449 |
layer.getName()); |
450 |
logger.info(msg, e); |
451 |
return;
|
452 |
} |
453 |
break;
|
454 |
|
455 |
case CANCEL:
|
456 |
return;
|
457 |
} |
458 |
|
459 |
cleanEditingContext(); |
460 |
hideConsole(); |
461 |
changeSelectedTool(DEFAULT_TOOL); |
462 |
|
463 |
FeatureStore featureStore = layer.getFeatureStore(); |
464 |
featureStore.deleteObserver(getMapControl()); |
465 |
|
466 |
} |
467 |
|
468 |
private void enableSelection(boolean enableSelection) { |
469 |
this.editingCompoundBehavior.setDrawnBehavior(
|
470 |
EditingCompoundBehavior.SELECTION_INDEX, enableSelection); |
471 |
} |
472 |
|
473 |
public void endEdition(FLyrVect layer) { |
474 |
if (layer.isEditing()) {
|
475 |
EditingNotificationManager editingNotificationManager = |
476 |
MapControlLocator.getEditingNotificationManager(); |
477 |
|
478 |
EditingNotification notification = |
479 |
editingNotificationManager.notifyObservers(this,
|
480 |
EditingNotification.BEFORE_EXIT_EDITING_STORE, null, layer);
|
481 |
|
482 |
if (notification.isCanceled()) {
|
483 |
String msg =
|
484 |
String.format(
|
485 |
"Stop edit layer %1$s canceled by somme observer.",
|
486 |
layer.getName()); |
487 |
logger.info(msg, new EndEditingException(msg, null)); |
488 |
|
489 |
} |
490 |
|
491 |
getMapControl().getCanceldraw().setCanceled(true);
|
492 |
int option;
|
493 |
EditingSwingManager swingManager = |
494 |
EditingSwingLocator.getSwingManager(); |
495 |
|
496 |
if (layer.isWritable()) {
|
497 |
option = |
498 |
swingManager.showPanelSaveOrDiscard(getMapControl(), |
499 |
layer.getName()); |
500 |
} else {
|
501 |
option = |
502 |
swingManager.showPanelExportOrDiscard(getMapControl(), |
503 |
layer.getName()); |
504 |
} |
505 |
|
506 |
doAction(layer, option); |
507 |
|
508 |
editingNotificationManager.notifyObservers(this,
|
509 |
EditingNotification.AFTER_EXIT_EDITING_STORE, null, layer);
|
510 |
|
511 |
} |
512 |
|
513 |
} |
514 |
|
515 |
private void exportLayer(FLyrVect layer) throws EndEditingException { |
516 |
// TODO Auto-generated method stub
|
517 |
|
518 |
} |
519 |
|
520 |
protected void finishService() { |
521 |
EditingService lastService = serviceStack.pop(); |
522 |
try {
|
523 |
|
524 |
if (!serviceStack.isEmpty()
|
525 |
&& getActiveService().next().getTypes().contains(TYPE.GEOMETRY)) { |
526 |
|
527 |
Geometry geometry = lastService.finish(); |
528 |
|
529 |
if (geometry != null) { |
530 |
|
531 |
getActiveService().setValue(geometry); |
532 |
|
533 |
} |
534 |
|
535 |
} else {
|
536 |
|
537 |
lastService.finishAndStore(); |
538 |
getMapControl().rePaintDirtyLayers(); |
539 |
|
540 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
541 |
showConsoleMessage("\n"
|
542 |
+ i18nManager.getTranslation(lastService.getName()) + "# "
|
543 |
+ i18nManager.getTranslation("finished") + "\n"); |
544 |
setActiveService(lastService); |
545 |
changeSelectedTool(getActiveService().getName()); |
546 |
} |
547 |
|
548 |
lastService.stop(); |
549 |
lastService.start(); |
550 |
|
551 |
} catch (InvalidEntryException ex) {
|
552 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
553 |
showConsoleMessage(i18nManager.getTranslation("invalid_option"));
|
554 |
changeSelectedTool(getActiveService().getName()); |
555 |
} catch (VectorEditingException ex) {
|
556 |
logger.info("Can't finish " + lastService.getName(), ex);
|
557 |
cleanEditingContext(); |
558 |
return;
|
559 |
} |
560 |
|
561 |
getNextParameter(); |
562 |
} |
563 |
|
564 |
protected EditingService getActiveService() {
|
565 |
if (!serviceStack.isEmpty()) {
|
566 |
return serviceStack.peek();
|
567 |
} |
568 |
return null; |
569 |
} |
570 |
|
571 |
private JConsole getConsolePanel() {
|
572 |
if (console == null) { |
573 |
console = new JConsole(true); |
574 |
// Para distinguir cuando se est? escribiendo sobre la consola y
|
575 |
// cuando no.
|
576 |
console.setJTextName("VectorEditingConsole");
|
577 |
} |
578 |
return console;
|
579 |
} |
580 |
|
581 |
protected FLyrVect getCurrentLayer() {
|
582 |
return this.currentLayer; |
583 |
} |
584 |
|
585 |
protected EditingServiceParameter getCurrentParam() {
|
586 |
return this.currentParam; |
587 |
} |
588 |
|
589 |
private Component getDockConsole() { |
590 |
if (dockConsole == null) { |
591 |
dockConsole = new JDockPanel(getConsolePanel());
|
592 |
} |
593 |
return dockConsole;
|
594 |
} |
595 |
|
596 |
private DefaultEditingBehavior getEditingBehavior() {
|
597 |
if (editingCompoundBehavior != null) { |
598 |
return (DefaultEditingBehavior) editingCompoundBehavior
|
599 |
.getBehavior(EditingCompoundBehavior.EDITING_INDEX); |
600 |
} |
601 |
return null; |
602 |
} |
603 |
|
604 |
private EditingCompoundBehavior getEditingCompoundBehavior() {
|
605 |
if (editingCompoundBehavior != null) { |
606 |
return editingCompoundBehavior;
|
607 |
} else {
|
608 |
EditingCompoundBehavior editingCompoundBehavior; |
609 |
|
610 |
CompoundBehavior compoundBehavior = |
611 |
(CompoundBehavior) getMapControl().getMapTool("VectorEditing");
|
612 |
|
613 |
if (compoundBehavior instanceof EditingCompoundBehavior) { |
614 |
editingCompoundBehavior = |
615 |
(EditingCompoundBehavior) compoundBehavior; |
616 |
} else {
|
617 |
editingCompoundBehavior = |
618 |
(EditingCompoundBehavior) compoundBehavior.getBehavior(0);
|
619 |
} |
620 |
|
621 |
setCompoundBehavior(editingCompoundBehavior); |
622 |
return editingCompoundBehavior;
|
623 |
} |
624 |
} |
625 |
|
626 |
public MapControl getMapControl() {
|
627 |
MapControl mapControl = mapControlReference.get(); |
628 |
if (mapControl != null) { |
629 |
return mapControl;
|
630 |
} |
631 |
return null; |
632 |
} |
633 |
|
634 |
protected void getNextParameter() { |
635 |
|
636 |
currentParam = getActiveService().next(); |
637 |
|
638 |
if (currentParam == null) { |
639 |
finishService(); |
640 |
} else {
|
641 |
askQuestion(currentParam); |
642 |
if (currentParam.getTypes().contains(TYPE.SELECTION)) {
|
643 |
enableSelection(true);
|
644 |
} else if (currentParam.getTypes().contains(TYPE.GEOMETRY)) { |
645 |
refreshMenusAndToolBars(); |
646 |
} |
647 |
} |
648 |
} |
649 |
|
650 |
protected Stack<EditingService> getServiceStack() { |
651 |
return this.serviceStack; |
652 |
} |
653 |
|
654 |
private void hideConsole() { |
655 |
isShowConsole = false;
|
656 |
getDockConsole().setVisible(false);
|
657 |
} |
658 |
|
659 |
public boolean isServiceCompatible(String name) { |
660 |
DefaultEditingBehavior editingBehavior = getEditingBehavior(); |
661 |
|
662 |
if (editingBehavior != null) { |
663 |
|
664 |
try {
|
665 |
EditingManager manager = EditingLocator.getManager(); |
666 |
EditingServiceInfo serviceInfo = manager.getServiceInfo(name); |
667 |
GeometryType geoType = null;
|
668 |
|
669 |
for (EditingService editingService : getServiceStack()) {
|
670 |
EditingServiceParameter parameter = editingService.next(); |
671 |
if (parameter != null |
672 |
&& parameter.getTypes().contains(TYPE.GEOMETRY)) { |
673 |
|
674 |
int geometryType = parameter.getGeometryType();
|
675 |
int subType = -1; |
676 |
|
677 |
try {
|
678 |
subType = |
679 |
getCurrentLayer().getFeatureStore() |
680 |
.getDefaultFeatureType() |
681 |
.getDefaultGeometryAttribute() |
682 |
.getGeomType().getSubType(); |
683 |
|
684 |
geoType = |
685 |
GeometryLocator.getGeometryManager() |
686 |
.getGeometryType(geometryType, subType); |
687 |
} catch (Exception e) { |
688 |
|
689 |
String msg =
|
690 |
String.format(
|
691 |
"Problems getting default feature"
|
692 |
+ " type of %1$s or getting geometry"
|
693 |
+ " type of %2$s %3$s",
|
694 |
getCurrentLayer().getName(), geometryType, |
695 |
subType); |
696 |
|
697 |
throw new ServiceInformationException(msg, e); |
698 |
} |
699 |
|
700 |
return serviceInfo.isCompatibleWith(geoType)
|
701 |
&& serviceInfo.createsNewGeometries(); |
702 |
} |
703 |
} |
704 |
|
705 |
if (getCurrentLayer() != null) { |
706 |
try {
|
707 |
geoType = |
708 |
getCurrentLayer().getFeatureStore() |
709 |
.getDefaultFeatureType() |
710 |
.getDefaultGeometryAttribute().getGeomType(); |
711 |
} catch (DataException e) {
|
712 |
String msg =
|
713 |
String.format("Problems getting default " |
714 |
+ "feature type of %1$s", getCurrentLayer()
|
715 |
.getName()); |
716 |
throw new ServiceInformationException(msg, e); |
717 |
} |
718 |
|
719 |
return serviceInfo.isCompatibleWith(geoType);
|
720 |
} |
721 |
|
722 |
return false; |
723 |
} catch (ServiceInformationException e) {
|
724 |
logger.warn("Problems getting if editing context is compatible with "+ name, e);
|
725 |
} |
726 |
} |
727 |
return false; |
728 |
} |
729 |
|
730 |
private Point parsePoint(String response) throws ParsePointException { |
731 |
String[] numbers = new String[1]; |
732 |
numbers[0] = response;
|
733 |
numbers = response.split(",");
|
734 |
if (numbers.length == 2) { |
735 |
|
736 |
if (numbers[0].startsWith("(") && numbers[1].endsWith(")\n")) { // CCS |
737 |
numbers[0] = numbers[0].replace("(", ""); |
738 |
numbers[1] = numbers[1].replace(")\n", ""); |
739 |
} |
740 |
|
741 |
double[] values; |
742 |
try {
|
743 |
values = |
744 |
new double[] { Double.parseDouble(numbers[0]), |
745 |
Double.parseDouble(numbers[1]) }; |
746 |
} catch (Exception e) { |
747 |
throw new ParsePointException(e); |
748 |
} |
749 |
|
750 |
Point point;
|
751 |
try {
|
752 |
GeometryManager geomManager = |
753 |
GeometryLocator.getGeometryManager(); |
754 |
int subType =
|
755 |
getCurrentLayer().getFeatureStore().getDefaultFeatureType() |
756 |
.getDefaultGeometryAttribute().getGeomType() |
757 |
.getSubType(); |
758 |
point = geomManager.createPoint(values[0], values[1], subType); |
759 |
|
760 |
} catch (Exception e) { |
761 |
throw new ParsePointException(e); |
762 |
} |
763 |
|
764 |
return point;
|
765 |
} else {
|
766 |
throw new ParsePointException(null); |
767 |
} |
768 |
} |
769 |
|
770 |
private Double parseValue(String response) throws ParseValueException { |
771 |
try {
|
772 |
return Double.valueOf(response); |
773 |
} catch (Exception e) { |
774 |
throw new ParseValueException(e); |
775 |
} |
776 |
|
777 |
} |
778 |
|
779 |
protected void refreshMenusAndToolBars() { |
780 |
Notification notification = new BaseNotification(EditingContext.REFRESH_TOOLS_NOTIFICATION, null); |
781 |
this.observableHelper.notifyObservers(this, notification); |
782 |
} |
783 |
|
784 |
private void saveChanges(FLyrVect layer) throws EndEditingException { |
785 |
FeatureStore featureStore = layer.getFeatureStore(); |
786 |
try {
|
787 |
featureStore.finishEditing(); |
788 |
} catch (Exception e) { |
789 |
throw new EndEditingException(e); |
790 |
} |
791 |
} |
792 |
|
793 |
private void setActiveService(EditingService service) { |
794 |
serviceStack.add(service); |
795 |
} |
796 |
|
797 |
private void setCaretPosition() { |
798 |
JEditTextArea jeta = getConsolePanel().getTxt(); |
799 |
jeta.requestFocusInWindow(); |
800 |
jeta.setCaretPosition(jeta.getText().length()); |
801 |
} |
802 |
|
803 |
private void setCompoundBehavior(EditingCompoundBehavior compoundBehavior) { |
804 |
this.editingCompoundBehavior = compoundBehavior;
|
805 |
} |
806 |
|
807 |
private void setCurrentLayer(FLyrVect layer) { |
808 |
|
809 |
if (this.currentLayer != layer) { |
810 |
cleanEditingContext(); |
811 |
this.currentLayer = layer;
|
812 |
} |
813 |
|
814 |
} |
815 |
|
816 |
public void setMapControl(MapControl mapControl) { |
817 |
|
818 |
this.mapControlReference = new WeakReference<MapControl>(mapControl); |
819 |
this.mapContext = mapControl.getMapContext();
|
820 |
|
821 |
// When mapControl is updated we have to add older additional behaviors
|
822 |
// to new mapControl
|
823 |
if (lastAdditionalBehaviors != null) { |
824 |
try {
|
825 |
addBehaviors(lastAdditionalBehaviors); |
826 |
} catch (CreateEditingBehaviorException e1) {
|
827 |
logger.info("Problems adding behaviors to editing context", e1);
|
828 |
getMapControl().setTool("pan");
|
829 |
return;
|
830 |
} |
831 |
} |
832 |
} |
833 |
|
834 |
private void showConsole() { |
835 |
if (isShowConsole) {
|
836 |
return;
|
837 |
} |
838 |
isShowConsole = true;
|
839 |
getMapControl().remove(getDockConsole()); |
840 |
getMapControl().setLayout(new BorderLayout()); |
841 |
getMapControl().add(getDockConsole(), BorderLayout.SOUTH);
|
842 |
getDockConsole().setVisible(true);
|
843 |
setCaretPosition(); |
844 |
} |
845 |
|
846 |
protected void showConsoleMessage(String text) { |
847 |
getConsolePanel().addText(text, JConsole.MESSAGE); |
848 |
} |
849 |
|
850 |
protected void textEntered(String response) { |
851 |
if (response == null) { |
852 |
if (getActiveService() != null) { |
853 |
try {
|
854 |
getActiveService().stop(); |
855 |
serviceStack.pop(); |
856 |
if (serviceStack.isEmpty()) {
|
857 |
getCurrentLayer().getFeatureStore() |
858 |
.getFeatureSelection().deselectAll(); |
859 |
changeSelectedTool(DEFAULT_TOOL); |
860 |
} else {
|
861 |
changeSelectedTool(getActiveService().getName()); |
862 |
} |
863 |
|
864 |
refreshMenusAndToolBars(); |
865 |
|
866 |
if (getActiveService() != null) { |
867 |
getNextParameter(); |
868 |
} else {
|
869 |
cleanEditingContext(); |
870 |
} |
871 |
|
872 |
} catch (StopServiceException e) {
|
873 |
logger |
874 |
.info("Can't stop " + getActiveService().getName(), e);
|
875 |
return;
|
876 |
} catch (DataException e) {
|
877 |
logger.info("Can't get selection of "
|
878 |
+ getCurrentLayer().getFeatureStore().getFullName(), e); |
879 |
return;
|
880 |
} |
881 |
} |
882 |
} else {
|
883 |
|
884 |
if (getCurrentParam() != null) { |
885 |
Set<TYPE> types = getCurrentParam().getTypes();
|
886 |
Point point = null; |
887 |
Double value = null; |
888 |
|
889 |
boolean insertedValue = false; |
890 |
if ((!insertedValue && types.contains(TYPE.POSITION))
|
891 |
|| types.contains(TYPE.LIST_POSITIONS)) { |
892 |
|
893 |
try {
|
894 |
|
895 |
point = parsePoint(response); |
896 |
if (point != null) { |
897 |
getActiveService().setValue(point); |
898 |
insertedValue = true;
|
899 |
} |
900 |
|
901 |
} catch (VectorEditingException e) {
|
902 |
// Do nothing to try other types
|
903 |
} |
904 |
} |
905 |
if (!insertedValue && types.contains(TYPE.VALUE)) {
|
906 |
|
907 |
try {
|
908 |
|
909 |
value = parseValue(response); |
910 |
if (value != null) { |
911 |
getActiveService().setValue(value); |
912 |
insertedValue = true;
|
913 |
} |
914 |
|
915 |
} catch (VectorEditingException e) {
|
916 |
// Do nothing to try other types
|
917 |
} |
918 |
|
919 |
} |
920 |
if (!insertedValue && types.contains(TYPE.OPTION)) {
|
921 |
|
922 |
try {
|
923 |
|
924 |
response = response.replace("\n", ""); |
925 |
if (response != null) { |
926 |
getActiveService().setValue(response); |
927 |
insertedValue = true;
|
928 |
} |
929 |
|
930 |
} catch (VectorEditingException e) {
|
931 |
// Do nothing to try other types
|
932 |
} |
933 |
} |
934 |
if (!insertedValue && types.contains(TYPE.SELECTION)) {
|
935 |
if (response.equalsIgnoreCase("\n")) { |
936 |
enableSelection(false);
|
937 |
insertedValue = true;
|
938 |
|
939 |
try {
|
940 |
|
941 |
getActiveService().setValue( |
942 |
getCurrentLayer().getFeatureStore() |
943 |
.getFeatureSelection().clone()); |
944 |
|
945 |
} catch (InvalidEntryException e) {
|
946 |
I18nManager i18nManager = |
947 |
ToolsLocator.getI18nManager(); |
948 |
showConsoleMessage(i18nManager |
949 |
.getTranslation("invalid_option"));
|
950 |
} catch (Exception e) { |
951 |
logger.info("Can't access to selecction.", e);
|
952 |
cleanEditingContext(); |
953 |
return;
|
954 |
} |
955 |
} |
956 |
} |
957 |
if (!insertedValue) {
|
958 |
I18nManager i18nManager = ToolsLocator.getI18nManager(); |
959 |
showConsoleMessage(i18nManager |
960 |
.getTranslation("invalid_option"));
|
961 |
} |
962 |
getNextParameter(); |
963 |
} |
964 |
} |
965 |
} |
966 |
} |