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