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