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 / DefaultEditingBehavior.java @ 83
History | View | Annotate | Download (22.6 KB)
1 |
/*
|
---|---|
2 |
* Copyright 2014 DiSiD Technologies S.L.L. All rights reserved.
|
3 |
*
|
4 |
* Project : DiSiD org.gvsig.vectorediting.lib.impl
|
5 |
* SVN Id : $Id$
|
6 |
*/
|
7 |
package org.gvsig.vectorediting.swing.impl; |
8 |
|
9 |
import java.awt.BorderLayout; |
10 |
import java.awt.Color; |
11 |
import java.awt.Graphics; |
12 |
import java.awt.Image; |
13 |
import java.awt.event.MouseEvent; |
14 |
import java.awt.image.BufferedImage; |
15 |
import java.util.HashMap; |
16 |
import java.util.Iterator; |
17 |
import java.util.List; |
18 |
import java.util.Map; |
19 |
import java.util.Stack; |
20 |
|
21 |
import org.gvsig.fmap.dal.exception.DataException; |
22 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
23 |
import org.gvsig.fmap.geom.Geometry; |
24 |
import org.gvsig.fmap.geom.primitive.Point; |
25 |
import org.gvsig.fmap.mapcontext.MapContext; |
26 |
import org.gvsig.fmap.mapcontext.ViewPort; |
27 |
import org.gvsig.fmap.mapcontext.layers.CancelationException; |
28 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
29 |
import org.gvsig.fmap.mapcontext.layers.FLayers; |
30 |
import org.gvsig.fmap.mapcontext.layers.LayerCollectionEvent; |
31 |
import org.gvsig.fmap.mapcontext.layers.LayerCollectionListener; |
32 |
import org.gvsig.fmap.mapcontext.layers.LayerEvent; |
33 |
import org.gvsig.fmap.mapcontext.layers.LayerListener; |
34 |
import org.gvsig.fmap.mapcontext.layers.LayerPositionEvent; |
35 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
36 |
import org.gvsig.fmap.mapcontrol.MapControl; |
37 |
import org.gvsig.fmap.mapcontrol.MapControlDrawer; |
38 |
import org.gvsig.fmap.mapcontrol.MapControlLocator; |
39 |
import org.gvsig.fmap.mapcontrol.tools.BehaviorException; |
40 |
import org.gvsig.fmap.mapcontrol.tools.Behavior.Behavior; |
41 |
import org.gvsig.fmap.mapcontrol.tools.Listeners.ToolListener; |
42 |
import org.gvsig.tools.ToolsLocator; |
43 |
import org.gvsig.tools.i18n.I18nManager; |
44 |
import org.gvsig.utils.console.JConsole; |
45 |
import org.gvsig.utils.console.JDockPanel; |
46 |
import org.gvsig.utils.console.ResponseListener; |
47 |
import org.gvsig.utils.console.jedit.JEditTextArea; |
48 |
import org.gvsig.vectorediting.lib.api.DrawingStatus; |
49 |
import org.gvsig.vectorediting.lib.api.EditingLocator; |
50 |
import org.gvsig.vectorediting.lib.api.EditingManager; |
51 |
import org.gvsig.vectorediting.lib.api.EditingService; |
52 |
import org.gvsig.vectorediting.lib.api.EditingServiceParameter; |
53 |
import org.gvsig.vectorediting.lib.api.EditingServiceParameter.TYPE; |
54 |
import org.gvsig.vectorediting.lib.api.exceptions.CreateEditingBehaviorException; |
55 |
import org.gvsig.vectorediting.lib.api.exceptions.FinishServiceException; |
56 |
import org.gvsig.vectorediting.lib.api.exceptions.InvalidEntryException; |
57 |
import org.gvsig.vectorediting.lib.api.exceptions.ParsePointException; |
58 |
import org.gvsig.vectorediting.lib.api.exceptions.ParseValueException; |
59 |
import org.gvsig.vectorediting.lib.api.exceptions.StartServiceException; |
60 |
import org.gvsig.vectorediting.lib.api.exceptions.StopServiceException; |
61 |
import org.gvsig.vectorediting.lib.api.exceptions.VectorEditingException; |
62 |
import org.gvsig.vectorediting.swing.api.EditingBehavior; |
63 |
import org.slf4j.Logger; |
64 |
import org.slf4j.LoggerFactory; |
65 |
|
66 |
public class DefaultEditingBehavior extends Behavior implements EditingBehavior { |
67 |
|
68 |
private static final Logger logger = LoggerFactory |
69 |
.getLogger(DefaultEditingBehavior.class); |
70 |
|
71 |
private Map<FLyrVect, EditingService> serviceRegistration; |
72 |
|
73 |
private Stack<EditingService> serviceStack; |
74 |
|
75 |
private MapControl mapControl;
|
76 |
|
77 |
private FLyrVect currentLayer;
|
78 |
|
79 |
private FeatureStore featureStore;
|
80 |
|
81 |
private EditingServiceParameter currentParam;
|
82 |
|
83 |
private Point adjustedPoint; |
84 |
|
85 |
// private StatusBarListener sbl = null;
|
86 |
|
87 |
private JConsole console;
|
88 |
|
89 |
private JDockPanel dockConsole = null; |
90 |
|
91 |
private boolean isShowConsole; |
92 |
|
93 |
protected ResponseAdapter consoleResponseAdapter;
|
94 |
|
95 |
private I18nManager i18nManager = ToolsLocator.getI18nManager();
|
96 |
|
97 |
private EditingCompoundBehavior compoundBehavior;
|
98 |
|
99 |
private static final Image imageCursor = new BufferedImage(32, 32, |
100 |
BufferedImage.TYPE_INT_ARGB);
|
101 |
static {
|
102 |
Graphics g = imageCursor.getGraphics();
|
103 |
int size1 = 15; |
104 |
int x = 16; |
105 |
int y = 16; |
106 |
g.setColor(Color.MAGENTA);
|
107 |
g.drawLine((x - size1), (y), (x + size1), (y)); |
108 |
g.drawLine((x), (y - size1), (x), (y + size1)); |
109 |
g.drawRect((x - 6), (y - 6), 12, 12); |
110 |
g.drawRect((x - 3), (y - 3), 6, 6); |
111 |
} |
112 |
|
113 |
private LayerListener layerListener = new LayerListener() { |
114 |
|
115 |
public void activationChanged(LayerEvent e) { |
116 |
FLayer layer = e.getSource(); |
117 |
if (layer instanceof FLyrVect) { |
118 |
|
119 |
if (layer.isActive() && layer.isEditing()) {
|
120 |
showConsole(); |
121 |
getMapControl().setTool("VectorEditing");
|
122 |
} |
123 |
else if (!layer.isEditing() && layer.isActive()) { |
124 |
hideConsole(); |
125 |
getMapControl().setTool("zoomIn");
|
126 |
} |
127 |
changeCurrentLayer((FLyrVect) layer); |
128 |
} |
129 |
|
130 |
} |
131 |
|
132 |
public void drawValueChanged(LayerEvent e) {} |
133 |
|
134 |
public void editionChanged(LayerEvent e) {} |
135 |
|
136 |
public void nameChanged(LayerEvent e) {} |
137 |
|
138 |
public void visibilityChanged(LayerEvent e) {} |
139 |
}; |
140 |
|
141 |
public void cleanBehavior() { |
142 |
serviceStack.clear(); |
143 |
currentParam = null;
|
144 |
this.enableSelection(false); |
145 |
|
146 |
showConsoleMessage("\n" + i18nManager.getTranslation("select_new_tool") |
147 |
+ "\n");
|
148 |
} |
149 |
|
150 |
public DefaultEditingBehavior(MapControl mapControl)
|
151 |
throws CreateEditingBehaviorException {
|
152 |
if (mapControl != null) { |
153 |
this.mapControl = mapControl;
|
154 |
serviceRegistration = new HashMap<FLyrVect, EditingService>(); |
155 |
serviceStack = new Stack<EditingService>(); |
156 |
// sbl = new StatusBarListener(mapControl);
|
157 |
initMapControlListeners(mapControl); |
158 |
initConsolePanelListeners(getConsolePanel()); |
159 |
FLayers layers = mapControl.getMapContext().getLayers(); |
160 |
for (int i = 0; i < layers.getLayersCount(); i++) { |
161 |
if (layers.getLayer(i) instanceof FLyrVect |
162 |
&& layers.getLayer(i).isActive()) |
163 |
changeCurrentLayer((FLyrVect) layers.getLayer(i)); |
164 |
} |
165 |
} |
166 |
} |
167 |
|
168 |
private void initConsolePanelListeners(JConsole jConsole) { |
169 |
consoleResponseAdapter = new ResponseAdapter();
|
170 |
jConsole.addResponseListener(consoleResponseAdapter); |
171 |
addConsoleListener("ViewEditing", new ResponseListener() { |
172 |
|
173 |
public void acceptResponse(String response) { |
174 |
try {
|
175 |
textEntered(response); |
176 |
} |
177 |
catch (InvalidEntryException e) {
|
178 |
showConsoleMessage(i18nManager.getTranslation("invalid_option"));
|
179 |
} |
180 |
catch (StopServiceException e) {
|
181 |
logger.error(i18nManager.getTranslation("cant_stop_the_service")
|
182 |
+ " " + getActiveService().getName(), e);
|
183 |
} |
184 |
finally {
|
185 |
if (getActiveService() != null) { |
186 |
getNextParameter(); |
187 |
} |
188 |
} |
189 |
} |
190 |
}); |
191 |
|
192 |
} |
193 |
|
194 |
public void activateService(String name) { |
195 |
|
196 |
EditingManager manager = EditingLocator.getManager(); |
197 |
|
198 |
if (currentLayer != null) { |
199 |
EditingService service = serviceRegistration.get(currentLayer); |
200 |
if (service == null || !service.getName().equals(name)) { |
201 |
service = (EditingService) manager.getEditingService(name, |
202 |
currentLayer.getFeatureStore()); |
203 |
serviceRegistration.put(currentLayer, service); |
204 |
} |
205 |
if (service != null) { |
206 |
this.enableSelection(false); |
207 |
try {
|
208 |
service.start(); |
209 |
} |
210 |
catch (StartServiceException e) {
|
211 |
logger.error( |
212 |
String.format("Can't start the service %(s)", service.getName()), |
213 |
e); |
214 |
} |
215 |
if (serviceStack.isEmpty()
|
216 |
|| getActiveService().next().getTypes() |
217 |
.contains(EditingServiceParameter.TYPE.GEOMETRY)) { |
218 |
setActiveService(service); |
219 |
} |
220 |
else {
|
221 |
serviceStack.clear(); |
222 |
setActiveService(service); |
223 |
} |
224 |
getNextParameter(); |
225 |
} |
226 |
} |
227 |
} |
228 |
|
229 |
/**
|
230 |
* Shows description of parameter on console.
|
231 |
*/
|
232 |
private void askQuestion(EditingServiceParameter param) { |
233 |
if (getConsolePanel() != null) { |
234 |
showConsoleMessage("\n#" + param.getDescription() + " > "); |
235 |
} |
236 |
} |
237 |
|
238 |
private void showConsoleMessage(String text) { |
239 |
getConsolePanel().addText(text, JConsole.MESSAGE); |
240 |
} |
241 |
|
242 |
@Override
|
243 |
public ToolListener getListener() {
|
244 |
return new ToolListener() { |
245 |
|
246 |
/**
|
247 |
*
|
248 |
*/
|
249 |
public boolean cancelDrawing() { |
250 |
return false; |
251 |
} |
252 |
|
253 |
/**
|
254 |
*
|
255 |
*/
|
256 |
public Image getImageCursor() { |
257 |
return imageCursor;
|
258 |
} |
259 |
}; |
260 |
} |
261 |
|
262 |
/**
|
263 |
* Gets next parameter of {@link #activateService(String)} and sets
|
264 |
* {@link #currentParam}. If currentParam is null means all service parameters
|
265 |
* have value and service doesn't need more values.
|
266 |
*/
|
267 |
private void getNextParameter() { |
268 |
currentParam = getActiveService().next(); |
269 |
if (currentParam == null) { |
270 |
finishService(); |
271 |
} |
272 |
else {
|
273 |
askQuestion(currentParam); |
274 |
if (currentParam.getTypes().contains(
|
275 |
EditingServiceParameter.TYPE.SELECTION)) { |
276 |
enableSelection(true);
|
277 |
} |
278 |
setCaretPosition(); |
279 |
} |
280 |
|
281 |
} |
282 |
|
283 |
private EditingService getActiveService() {
|
284 |
if (!serviceStack.isEmpty()) {
|
285 |
return serviceStack.peek();
|
286 |
} |
287 |
return null; |
288 |
} |
289 |
|
290 |
private void setActiveService(EditingService service) { |
291 |
serviceStack.add(service); |
292 |
} |
293 |
|
294 |
private void initMapControlListeners(MapControl mapControl) { |
295 |
|
296 |
MapContext context = mapControl.getMapContext(); |
297 |
FLayers layers = context.getLayers(); |
298 |
layers.addLayerListener(layerListener); |
299 |
|
300 |
layers.addLayerCollectionListener(new LayerCollectionListener() {
|
301 |
|
302 |
public void layerAdded(LayerCollectionEvent e) { |
303 |
FLayers layers2 = e.getLayers(); |
304 |
for (int i = 0; i < layers2.getLayersCount(); i++) { |
305 |
FLayer layer = layers2.getLayer(i); |
306 |
if (layer instanceof FLyrVect) { |
307 |
((FLyrVect) layer).addLayerListener(layerListener); |
308 |
} |
309 |
} |
310 |
} |
311 |
|
312 |
public void layerAdding(LayerCollectionEvent e) |
313 |
throws CancelationException {}
|
314 |
|
315 |
public void layerMoved(LayerPositionEvent e) {} |
316 |
|
317 |
public void layerMoving(LayerPositionEvent e) throws CancelationException {} |
318 |
|
319 |
public void layerRemoved(LayerCollectionEvent e) { |
320 |
FLayers layers2 = e.getLayers(); |
321 |
for (int i = 0; i < layers2.getLayersCount(); i++) { |
322 |
FLayer layer = layers2.getLayer(i); |
323 |
if (layer instanceof FLyrVect) { |
324 |
((FLyrVect) layer).removeLayerListener(layerListener); |
325 |
} |
326 |
} |
327 |
} |
328 |
|
329 |
public void layerRemoving(LayerCollectionEvent e) |
330 |
throws CancelationException {}
|
331 |
|
332 |
public void visibilityChanged(LayerCollectionEvent e) |
333 |
throws CancelationException {}
|
334 |
}); |
335 |
} |
336 |
|
337 |
public void mouseClicked(MouseEvent e) { |
338 |
ViewPort vp = mapControl.getViewPort(); |
339 |
if (getActiveService() != null) { |
340 |
if (currentParam != null) { |
341 |
List<TYPE> typesOfParam = currentParam.getTypes();
|
342 |
if (typesOfParam.contains(TYPE.LIST_POSITIONS)) {
|
343 |
if (e.getClickCount() == 2) { |
344 |
finishService(); |
345 |
return;
|
346 |
} |
347 |
Point point;
|
348 |
point = vp.convertToMapPoint(e.getX(), e.getY()); |
349 |
try {
|
350 |
getActiveService().value(point); |
351 |
} |
352 |
catch (VectorEditingException ex) {
|
353 |
logger.error("Invalid value %s", new Object[] { point }); |
354 |
} |
355 |
} |
356 |
if (typesOfParam.contains(TYPE.POSITION)) {
|
357 |
Point point;
|
358 |
point = vp.convertToMapPoint(e.getX(), e.getY()); |
359 |
try {
|
360 |
getActiveService().value(point); |
361 |
} |
362 |
catch (VectorEditingException ex) {
|
363 |
logger.error("Invalid value %s", new Object[] { point }); |
364 |
} |
365 |
} |
366 |
if (typesOfParam.contains(TYPE.SELECTION)) {
|
367 |
// Do nothing
|
368 |
} |
369 |
getNextParameter(); |
370 |
} |
371 |
} |
372 |
} |
373 |
|
374 |
private void finishService() { |
375 |
EditingService lastService = serviceStack.pop(); |
376 |
try {
|
377 |
if (serviceStack.isEmpty() || !getActiveService().next().getTypes()
|
378 |
.contains(EditingServiceParameter.TYPE.GEOMETRY)) { |
379 |
lastService.finishAndStore(); |
380 |
mapControl.rePaintDirtyLayers(); |
381 |
setActiveService(lastService); |
382 |
} |
383 |
else if (!serviceStack.isEmpty() && getActiveService().next().getTypes() |
384 |
.contains(EditingServiceParameter.TYPE.GEOMETRY)) { |
385 |
Geometry geometry = lastService.finish(); |
386 |
if(geometry != null){ |
387 |
getActiveService().value(geometry); |
388 |
} else {
|
389 |
lastService.finishAndStore(); |
390 |
mapControl.rePaintDirtyLayers(); |
391 |
setActiveService(lastService); |
392 |
} |
393 |
} |
394 |
lastService.stop(); |
395 |
lastService.start(); |
396 |
} |
397 |
catch (InvalidEntryException ex) {
|
398 |
showConsoleMessage(i18nManager.getTranslation("invalid_option"));
|
399 |
} |
400 |
catch (FinishServiceException ex) {
|
401 |
logger.error("Can't finish "+ lastService.getName(), ex);
|
402 |
} |
403 |
catch (StopServiceException ex) {
|
404 |
logger.error("Can't stop "+ lastService.getName(), ex);
|
405 |
} |
406 |
catch (StartServiceException ex) {
|
407 |
logger.error("Can't start "+ lastService.getName(), ex);
|
408 |
} |
409 |
getNextParameter(); |
410 |
} |
411 |
|
412 |
public void mouseEntered(MouseEvent e) throws BehaviorException {} |
413 |
|
414 |
public void mouseMoved(MouseEvent e) throws BehaviorException { |
415 |
ViewPort vp = mapControl.getViewPort(); |
416 |
adjustedPoint = vp.convertToMapPoint(e.getX(), e.getY()); |
417 |
// showCoords(e.getPoint());
|
418 |
getMapControl().repaint(); |
419 |
} |
420 |
|
421 |
public void mousePressed(MouseEvent e) throws BehaviorException {} |
422 |
|
423 |
public void mouseReleased(MouseEvent e) throws BehaviorException {} |
424 |
|
425 |
public void paintComponent(MapControlDrawer mapControlDrawer) { |
426 |
super.paintComponent(mapControlDrawer);
|
427 |
if (getActiveService() == null || adjustedPoint == null) { |
428 |
return;
|
429 |
} |
430 |
|
431 |
DrawingStatus helperGeo = null;
|
432 |
try {
|
433 |
helperGeo = getActiveService().draw(adjustedPoint); |
434 |
} |
435 |
catch (VectorEditingException e) {
|
436 |
logger.error("An error ocurred when draw service geometries", e);
|
437 |
} |
438 |
if (helperGeo != null) { |
439 |
for (@SuppressWarnings("rawtypes") |
440 |
Iterator iterator = helperGeo.getGeometries().iterator(); iterator
|
441 |
.hasNext();) { |
442 |
Geometry geometry = (Geometry) iterator.next(); |
443 |
mapControl.getMapControlDrawer().draw(geometry, |
444 |
MapControlLocator.getMapControlManager().getAxisReferenceSymbol()); |
445 |
} |
446 |
} |
447 |
} |
448 |
|
449 |
private void setCaretPosition() { |
450 |
|
451 |
JEditTextArea jeta = getConsolePanel().getTxt(); |
452 |
jeta.requestFocusInWindow(); |
453 |
jeta.setCaretPosition(jeta.getText().length()); |
454 |
|
455 |
} |
456 |
|
457 |
/**
|
458 |
* Sets the layer received as parameter as {@link #currentLayer}
|
459 |
*
|
460 |
* @param layer
|
461 |
*/
|
462 |
private void changeCurrentLayer(FLyrVect layer) |
463 |
{ |
464 |
this.currentLayer = layer;
|
465 |
if (currentLayer != null) { |
466 |
this.featureStore = (FeatureStore) currentLayer.getFeatureStore();
|
467 |
} |
468 |
|
469 |
serviceStack.clear(); |
470 |
if (serviceRegistration.get(layer) != null) { |
471 |
setActiveService(serviceRegistration.get(layer)); |
472 |
} |
473 |
if (getActiveService() != null && layer.isActive()) { |
474 |
getNextParameter(); |
475 |
} |
476 |
} |
477 |
|
478 |
// /**
|
479 |
// * Show coordinates at status bar
|
480 |
// *
|
481 |
// * @param point
|
482 |
// */
|
483 |
// private void showCoords(Point2D point) {
|
484 |
// String[] axisText = new String[2];
|
485 |
// axisText[0] = "X = ";
|
486 |
// axisText[1] = "Y = ";
|
487 |
//
|
488 |
// ViewPort vp = mapControl.getMapContext().getViewPort();
|
489 |
// IProjection iProj = vp.getProjection();
|
490 |
// Point p = vp.convertToMapPoint(point);
|
491 |
// Point2D p2D = new Point2D.Double(p.getX(), p.getY());
|
492 |
//
|
493 |
// sbl.setFractionDigits(p2D);
|
494 |
// axisText = sbl.setCoorDisplayText(axisText);
|
495 |
// MainFrame mF = PluginServices.getMainFrame();
|
496 |
//
|
497 |
// if (mF != null) {
|
498 |
// mF.getStatusBar()
|
499 |
// .setMessage(
|
500 |
// "units",
|
501 |
// PluginServices.getText(this, MapContext
|
502 |
// .getDistanceNames()[vp.getDistanceUnits()]));
|
503 |
// mF.getStatusBar().setControlValue("scale",
|
504 |
// String.valueOf(mapControl.getMapContext().getScaleView()));
|
505 |
// mF.getStatusBar().setMessage("projection", iProj.getAbrev());
|
506 |
//
|
507 |
// String[] coords = sbl.getCoords(p2D);
|
508 |
// mF.getStatusBar().setMessage("x", axisText[0] + coords[0]);
|
509 |
// mF.getStatusBar().setMessage("y", axisText[1] + coords[1]);
|
510 |
// }
|
511 |
//
|
512 |
// }
|
513 |
|
514 |
/**
|
515 |
* Processes console entries from console. Tries to process entry in each type
|
516 |
* of {@link #currentParam}. If there is some error, It will continue
|
517 |
* checking. If entry has been processed, It will set value in
|
518 |
* {@link #activeService}.
|
519 |
*
|
520 |
* @param response Console entry.
|
521 |
* @throws InvalidEntryException If console entries has not been able to
|
522 |
* process, it will throw an exception to indicate that entry is not
|
523 |
* valid.
|
524 |
* @throws StopServiceException If there are some errors stopping service.
|
525 |
*/
|
526 |
private void textEntered(String response) throws InvalidEntryException, |
527 |
StopServiceException { |
528 |
if (response == null) { |
529 |
if (getActiveService() != null) { |
530 |
getActiveService().stop(); |
531 |
cleanBehavior(); |
532 |
} |
533 |
} |
534 |
else {
|
535 |
List<TYPE> types = currentParam.getTypes();
|
536 |
Point point = null; |
537 |
Double value = null; |
538 |
|
539 |
boolean insertedValue = false; |
540 |
if (!insertedValue && types.contains(TYPE.POSITION)
|
541 |
|| types.contains(TYPE.LIST_POSITIONS)) { |
542 |
try {
|
543 |
point = parsePoint(response); |
544 |
if (point != null) { |
545 |
getActiveService().value(point); |
546 |
insertedValue = true;
|
547 |
} |
548 |
} |
549 |
catch (VectorEditingException e) {
|
550 |
// Do nothing to try other types
|
551 |
} |
552 |
} |
553 |
if (!insertedValue && types.contains(TYPE.VALUE)) {
|
554 |
try {
|
555 |
value = parseValue(response); |
556 |
if (value != null) { |
557 |
getActiveService().value(value); |
558 |
insertedValue = true;
|
559 |
} |
560 |
} |
561 |
catch (VectorEditingException e) {
|
562 |
// Do nothing to try other types
|
563 |
} |
564 |
|
565 |
} |
566 |
if (!insertedValue && types.contains(TYPE.OPTION)) {
|
567 |
try {
|
568 |
response = response.replace("\n", ""); |
569 |
if (response != null) { |
570 |
getActiveService().value(response); |
571 |
insertedValue = true;
|
572 |
} |
573 |
} |
574 |
catch (VectorEditingException e) {
|
575 |
// Do nothing to try other types
|
576 |
} |
577 |
} |
578 |
if (!insertedValue && types.contains(TYPE.SELECTION)) {
|
579 |
if (response.equalsIgnoreCase("\n") |
580 |
&& currentParam.getTypes().contains( |
581 |
EditingServiceParameter.TYPE.SELECTION)) { |
582 |
enableSelection(false);
|
583 |
insertedValue = true;
|
584 |
try {
|
585 |
getActiveService() |
586 |
.value(featureStore.getFeatureSelection().clone()); |
587 |
} |
588 |
catch (DataException e) {
|
589 |
getActiveService().stop(); |
590 |
cleanBehavior(); |
591 |
logger.error("Can't access to selecction.", e);
|
592 |
} |
593 |
catch (CloneNotSupportedException e) { |
594 |
// Do nothing to try other types
|
595 |
} |
596 |
} |
597 |
} |
598 |
if (!insertedValue) {
|
599 |
throw new InvalidEntryException(null); |
600 |
} |
601 |
} |
602 |
} |
603 |
|
604 |
/**
|
605 |
* Parse String to value.
|
606 |
*
|
607 |
* @param response String to be parsed.
|
608 |
* @return Values of string.
|
609 |
* @throws ParseValueException If there is error trying to parse
|
610 |
* <code>String</code>.
|
611 |
*/
|
612 |
private Double parseValue(String response) throws ParseValueException { |
613 |
try {
|
614 |
return Double.valueOf(response); |
615 |
} |
616 |
catch (Exception e) { |
617 |
throw new ParseValueException(e); |
618 |
} |
619 |
|
620 |
} |
621 |
|
622 |
/**
|
623 |
* Parse String to point. Formats accepted: (x,y) and x,y.
|
624 |
*
|
625 |
* @param response String to be parsed.
|
626 |
* @return Point of string.
|
627 |
* @throws ParsePointException If there is error trying to parse
|
628 |
* <code>String</code>.
|
629 |
*/
|
630 |
private Point parsePoint(String response) throws ParsePointException { |
631 |
String[] numbers = new String[1]; |
632 |
numbers[0] = response;
|
633 |
numbers = response.split(",");
|
634 |
if (numbers.length == 2) { |
635 |
if (numbers[0].startsWith("(") && numbers[1].endsWith(")\n")) { // CCS |
636 |
numbers[0] = numbers[0].replace("(", ""); |
637 |
numbers[1] = numbers[1].replace(")\n", ""); |
638 |
} |
639 |
double[] values; |
640 |
try {
|
641 |
values = new double[] { Double.parseDouble(numbers[0]), |
642 |
Double.parseDouble(numbers[1]) }; |
643 |
} |
644 |
catch (Exception e) { |
645 |
throw new ParsePointException(e); |
646 |
} |
647 |
|
648 |
Point point;
|
649 |
try {
|
650 |
point = geomManager.createPoint(values[0], values[1], currentLayer |
651 |
.getFeatureStore().getDefaultFeatureType() |
652 |
.getDefaultGeometryAttribute().getGeomType().getSubType()); |
653 |
// TODO: Maybe do util class to get type and subtype of a featureStore
|
654 |
// or a method in manager
|
655 |
} |
656 |
catch (Exception e) { |
657 |
throw new ParsePointException(e); |
658 |
} |
659 |
return point;
|
660 |
} |
661 |
else {
|
662 |
throw new ParsePointException(null); |
663 |
} |
664 |
} |
665 |
|
666 |
private JDockPanel getDockConsole() {
|
667 |
if (dockConsole == null) { |
668 |
dockConsole = new JDockPanel(getConsolePanel());
|
669 |
} |
670 |
return dockConsole;
|
671 |
} |
672 |
|
673 |
public void showConsole() { |
674 |
if (isShowConsole) {
|
675 |
return;
|
676 |
} |
677 |
isShowConsole = true;
|
678 |
getMapControl().remove(getDockConsole()); |
679 |
getMapControl().setLayout(new BorderLayout()); |
680 |
getMapControl().add(getDockConsole(), BorderLayout.SOUTH);
|
681 |
getDockConsole().setVisible(true);
|
682 |
setCaretPosition(); |
683 |
} |
684 |
|
685 |
public void hideConsole() { |
686 |
isShowConsole = false;
|
687 |
getDockConsole().setVisible(false);
|
688 |
|
689 |
} |
690 |
|
691 |
private JConsole getConsolePanel() {
|
692 |
if (console == null) { |
693 |
console = new JConsole(true); |
694 |
// Para distinguir cuando se est? escribiendo sobre la consola y
|
695 |
// cuando no.
|
696 |
console.setJTextName("VectorEditingConsole");
|
697 |
} |
698 |
return console;
|
699 |
} |
700 |
|
701 |
private void addConsoleListener(String prefix, ResponseListener listener) { |
702 |
consoleResponseAdapter.putSpaceListener(prefix, listener); |
703 |
|
704 |
} |
705 |
|
706 |
static class ResponseAdapter implements ResponseListener { |
707 |
|
708 |
private HashMap<String, ResponseListener> spaceListener = new HashMap<String, ResponseListener>(); |
709 |
|
710 |
public void putSpaceListener(String namespace, ResponseListener listener) { |
711 |
spaceListener.put(namespace, listener); |
712 |
} |
713 |
|
714 |
public void acceptResponse(String response) { |
715 |
boolean nameSpace = false; |
716 |
int n = -1; |
717 |
if (response != null) { |
718 |
if ((n = response.indexOf(':')) != -1) { |
719 |
nameSpace = true;
|
720 |
} |
721 |
} |
722 |
|
723 |
if (nameSpace) {
|
724 |
ResponseListener listener = spaceListener.get(response.substring(0, n));
|
725 |
if (listener != null) { |
726 |
listener.acceptResponse(response.substring(n + 1));
|
727 |
} |
728 |
} |
729 |
else {
|
730 |
Iterator<ResponseListener> i = spaceListener.values().iterator();
|
731 |
while (i.hasNext()) {
|
732 |
ResponseListener listener = i.next(); |
733 |
listener.acceptResponse(response); |
734 |
} |
735 |
} |
736 |
} |
737 |
} |
738 |
|
739 |
public void setCompoundBehavior(EditingCompoundBehavior compoundBehavior) { |
740 |
this.compoundBehavior = compoundBehavior;
|
741 |
} |
742 |
|
743 |
public void enableSelection(boolean enableSelection) { |
744 |
this.compoundBehavior.setDrawnBehavior(
|
745 |
EditingCompoundBehavior.SELECTION_INDEX, enableSelection); |
746 |
} |
747 |
} |