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 @ 55
History | View | Annotate | Download (15.3 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.geom.Point2D; |
15 |
import java.awt.image.BufferedImage; |
16 |
import java.util.HashMap; |
17 |
import java.util.Iterator; |
18 |
import java.util.List; |
19 |
import java.util.Map; |
20 |
|
21 |
import org.cresques.cts.IProjection; |
22 |
import org.gvsig.andami.PluginServices; |
23 |
import org.gvsig.andami.ui.mdiFrame.MainFrame; |
24 |
import org.gvsig.app.project.documents.view.toolListeners.StatusBarListener; |
25 |
import org.gvsig.fmap.dal.exception.DataException; |
26 |
import org.gvsig.fmap.geom.Geometry; |
27 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
28 |
import org.gvsig.fmap.geom.primitive.Point; |
29 |
import org.gvsig.fmap.mapcontext.MapContext; |
30 |
import org.gvsig.fmap.mapcontext.ViewPort; |
31 |
import org.gvsig.fmap.mapcontext.layers.CancelationException; |
32 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
33 |
import org.gvsig.fmap.mapcontext.layers.FLayers; |
34 |
import org.gvsig.fmap.mapcontext.layers.LayerCollectionEvent; |
35 |
import org.gvsig.fmap.mapcontext.layers.LayerCollectionListener; |
36 |
import org.gvsig.fmap.mapcontext.layers.LayerEvent; |
37 |
import org.gvsig.fmap.mapcontext.layers.LayerListener; |
38 |
import org.gvsig.fmap.mapcontext.layers.LayerPositionEvent; |
39 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
40 |
import org.gvsig.fmap.mapcontrol.MapControl; |
41 |
import org.gvsig.fmap.mapcontrol.MapControlDrawer; |
42 |
import org.gvsig.fmap.mapcontrol.MapControlLocator; |
43 |
import org.gvsig.fmap.mapcontrol.tools.BehaviorException; |
44 |
import org.gvsig.fmap.mapcontrol.tools.Behavior.Behavior; |
45 |
import org.gvsig.fmap.mapcontrol.tools.Listeners.ToolListener; |
46 |
import org.gvsig.tools.exception.BaseException; |
47 |
import org.gvsig.utils.console.JConsole; |
48 |
import org.gvsig.utils.console.JDockPanel; |
49 |
import org.gvsig.utils.console.ResponseListener; |
50 |
import org.gvsig.utils.console.jedit.JEditTextArea; |
51 |
import org.gvsig.vectorediting.lib.api.DrawingStatus; |
52 |
import org.gvsig.vectorediting.lib.api.EditingLocator; |
53 |
import org.gvsig.vectorediting.lib.api.EditingManager; |
54 |
import org.gvsig.vectorediting.lib.api.EditingService; |
55 |
import org.gvsig.vectorediting.lib.api.EditingServiceParameter; |
56 |
import org.gvsig.vectorediting.lib.api.EditingServiceParameter.TYPE; |
57 |
import org.gvsig.vectorediting.lib.spi.DefaultDrawingStatus; |
58 |
import org.gvsig.vectorediting.swing.api.EditingBehavior; |
59 |
import org.slf4j.Logger; |
60 |
import org.slf4j.LoggerFactory; |
61 |
|
62 |
public class DefaultEditingBehavior extends Behavior implements EditingBehavior { |
63 |
|
64 |
private static final Logger logger = LoggerFactory.getLogger(DefaultEditingBehavior.class); |
65 |
|
66 |
private Map<FLyrVect, EditingService> serviceRegistration; |
67 |
|
68 |
private MapControl mapControl;
|
69 |
|
70 |
private FLyrVect currentLayer;
|
71 |
|
72 |
private EditingService activeService;
|
73 |
|
74 |
private EditingServiceParameter currentParam;
|
75 |
|
76 |
private Point adjustedPoint; |
77 |
|
78 |
private StatusBarListener sbl = null; |
79 |
|
80 |
private JConsole console;
|
81 |
|
82 |
private JDockPanel dockConsole = null; |
83 |
|
84 |
private boolean isShowConsole; |
85 |
|
86 |
protected ResponseAdapter consoleResponseAdapter;
|
87 |
|
88 |
private static final Image imageCursor = new BufferedImage(32, 32, |
89 |
BufferedImage.TYPE_INT_ARGB);
|
90 |
static {
|
91 |
Graphics g = imageCursor.getGraphics();
|
92 |
int size1 = 15; |
93 |
int x = 16; |
94 |
int y = 16; |
95 |
g.setColor(Color.MAGENTA);
|
96 |
g.drawLine((x - size1), (y), (x + size1), (y)); |
97 |
g.drawLine((x), (y - size1), (x), (y + size1)); |
98 |
g.drawRect((x - 6), (y - 6), 12, 12); |
99 |
g.drawRect((x - 3), (y - 3), 6, 6); |
100 |
} |
101 |
|
102 |
private LayerListener layerListener = new LayerListener() { |
103 |
|
104 |
public void activationChanged(LayerEvent e) { |
105 |
FLayer layer = e.getSource(); |
106 |
if (layer instanceof FLyrVect) { |
107 |
|
108 |
if (layer.isActive() && layer.isEditing()) {
|
109 |
showConsole(); |
110 |
getMapControl().setTool("VectorEditing");
|
111 |
} else if (!layer.isEditing() && layer.isActive()) { |
112 |
changeCurrentLayer((FLyrVect) layer); |
113 |
hideConsole(); |
114 |
getMapControl().setTool("zoomIn");
|
115 |
} |
116 |
changeCurrentLayer((FLyrVect) layer); |
117 |
|
118 |
} |
119 |
|
120 |
} |
121 |
|
122 |
public void drawValueChanged(LayerEvent e) { |
123 |
} |
124 |
|
125 |
public void editionChanged(LayerEvent e) { |
126 |
} |
127 |
|
128 |
public void nameChanged(LayerEvent e) { |
129 |
} |
130 |
|
131 |
public void visibilityChanged(LayerEvent e) { |
132 |
} |
133 |
}; |
134 |
|
135 |
public void cleanBehavior() { |
136 |
activeService = null;
|
137 |
currentParam = null;
|
138 |
|
139 |
getConsolePanel().addText("\nSeleccione un herramienta\n",
|
140 |
JConsole.MESSAGE); |
141 |
// MainFrame main = PluginServices.getMainFrame();
|
142 |
// main.setSelectedTool(""); We can define a default tool. I.E Zoom in.
|
143 |
} |
144 |
|
145 |
public DefaultEditingBehavior(MapControl mapControl) {
|
146 |
if (mapControl != null) { |
147 |
this.mapControl = mapControl;
|
148 |
serviceRegistration = new HashMap<FLyrVect, EditingService>(); |
149 |
sbl = new StatusBarListener(mapControl);
|
150 |
initMapControlListeners(mapControl); |
151 |
initConsolePanelListeners(getConsolePanel()); |
152 |
FLayers layers = mapControl.getMapContext().getLayers(); |
153 |
for (int i = 0; i < layers.getLayersCount(); i++) { |
154 |
if (layers.getLayer(i) instanceof FLyrVect |
155 |
&& layers.getLayer(i).isActive()) |
156 |
changeCurrentLayer((FLyrVect) layers.getLayer(i)); |
157 |
} |
158 |
|
159 |
} |
160 |
} |
161 |
|
162 |
private void initConsolePanelListeners(JConsole jConsole) { |
163 |
consoleResponseAdapter = new ResponseAdapter();
|
164 |
jConsole.addResponseListener(consoleResponseAdapter); |
165 |
addConsoleListener("ViewEditing", new ResponseListener() { |
166 |
|
167 |
public void acceptResponse(String response) { |
168 |
try {
|
169 |
textEntered(response); |
170 |
} catch (InvalidEntryException ex) {
|
171 |
getConsolePanel().addText(ex.getMessage(), JConsole.ERROR); |
172 |
getNextParameter(); |
173 |
} |
174 |
} |
175 |
}); |
176 |
|
177 |
} |
178 |
|
179 |
public void activateTool(String name) { |
180 |
|
181 |
EditingManager manager = EditingLocator.getManager(); |
182 |
|
183 |
if (currentLayer != null) { |
184 |
EditingService service = serviceRegistration.get(currentLayer); |
185 |
if(service == null || !service.getName().equals(name)){ |
186 |
service = (EditingService) manager.getEditingService(name, |
187 |
currentLayer.getFeatureStore()); |
188 |
serviceRegistration.put(currentLayer, service); |
189 |
} |
190 |
if (service != null) { |
191 |
this.activeService = service;
|
192 |
service.start(); |
193 |
getNextParameter(); |
194 |
} |
195 |
} |
196 |
} |
197 |
|
198 |
private void askQuestion(EditingServiceParameter param) { |
199 |
if (getConsolePanel() != null) { |
200 |
getConsolePanel().addText( |
201 |
"\n" + "#" + param.getDescription() + " > ", |
202 |
JConsole.MESSAGE); |
203 |
} |
204 |
} |
205 |
|
206 |
@Override
|
207 |
public ToolListener getListener() {
|
208 |
return new ToolListener() { |
209 |
|
210 |
/**
|
211 |
*
|
212 |
*/
|
213 |
public boolean cancelDrawing() { |
214 |
return false; |
215 |
} |
216 |
|
217 |
/**
|
218 |
*
|
219 |
*/
|
220 |
public Image getImageCursor() { |
221 |
return imageCursor;
|
222 |
} |
223 |
}; |
224 |
} |
225 |
|
226 |
private void getNextParameter() { |
227 |
currentParam = activeService.next(); |
228 |
if (currentParam == null) { |
229 |
try {
|
230 |
activeService.finish(); |
231 |
} catch (BaseException e) {
|
232 |
// TODO Auto-generated catch block
|
233 |
e.printStackTrace(); |
234 |
} |
235 |
mapControl.rePaintDirtyLayers(); |
236 |
activeService.start(); |
237 |
getNextParameter(); |
238 |
} else {
|
239 |
askQuestion(currentParam); |
240 |
setCaretPosition(); |
241 |
} |
242 |
|
243 |
} |
244 |
|
245 |
private void initMapControlListeners(MapControl mapControl) { |
246 |
|
247 |
MapContext context = mapControl.getMapContext(); |
248 |
FLayers layers = context.getLayers(); |
249 |
layers.addLayerListener(layerListener); |
250 |
|
251 |
layers.addLayerCollectionListener(new LayerCollectionListener() {
|
252 |
|
253 |
public void layerAdded(LayerCollectionEvent e) { |
254 |
FLayers layers2 = e.getLayers(); |
255 |
for (int i = 0; i < layers2.getLayersCount(); i++) { |
256 |
FLayer layer = layers2.getLayer(i); |
257 |
if (layer instanceof FLyrVect) { |
258 |
((FLyrVect) layer).addLayerListener(layerListener); |
259 |
} |
260 |
} |
261 |
} |
262 |
|
263 |
public void layerAdding(LayerCollectionEvent e) |
264 |
throws CancelationException {
|
265 |
} |
266 |
|
267 |
public void layerMoved(LayerPositionEvent e) { |
268 |
} |
269 |
|
270 |
public void layerMoving(LayerPositionEvent e) |
271 |
throws CancelationException {
|
272 |
} |
273 |
|
274 |
public void layerRemoved(LayerCollectionEvent e) { |
275 |
FLayers layers2 = e.getLayers(); |
276 |
for (int i = 0; i < layers2.getLayersCount(); i++) { |
277 |
FLayer layer = layers2.getLayer(i); |
278 |
if (layer instanceof FLyrVect) { |
279 |
((FLyrVect) layer).removeLayerListener(layerListener); |
280 |
} |
281 |
} |
282 |
} |
283 |
|
284 |
public void layerRemoving(LayerCollectionEvent e) |
285 |
throws CancelationException {
|
286 |
} |
287 |
|
288 |
public void visibilityChanged(LayerCollectionEvent e) |
289 |
throws CancelationException {
|
290 |
} |
291 |
}); |
292 |
} |
293 |
|
294 |
public void mouseClicked(MouseEvent e) throws BehaviorException { |
295 |
ViewPort vp = mapControl.getViewPort(); |
296 |
if (activeService != null) { |
297 |
if (currentParam != null) { |
298 |
|
299 |
List<TYPE> typesOfParam = currentParam.getTypes();
|
300 |
if (typesOfParam.contains(TYPE.POSITION)) {
|
301 |
Point point;
|
302 |
point = vp.convertToMapPoint(e.getX(), e.getY()); |
303 |
this.activeService.value(point);
|
304 |
} |
305 |
// TODO: Comprobar el resto de casos:
|
306 |
// TYPE.VALUE,
|
307 |
// TYPE.LIST_POSITIONS,
|
308 |
// TYPE.OPTION, TYPE.SELECTION,
|
309 |
// TYPE.GEOMETRY:
|
310 |
} |
311 |
getNextParameter(); |
312 |
} |
313 |
} |
314 |
|
315 |
public void mouseEntered(MouseEvent e) throws BehaviorException { |
316 |
// TODO Auto-generated method stub
|
317 |
} |
318 |
|
319 |
public void mouseMoved(MouseEvent e) throws BehaviorException { |
320 |
ViewPort vp = mapControl.getViewPort(); |
321 |
adjustedPoint = vp.convertToMapPoint(e.getX(), e.getY()); |
322 |
showCoords(e.getPoint()); |
323 |
|
324 |
getMapControl().repaint(); |
325 |
} |
326 |
|
327 |
public void mousePressed(MouseEvent e) throws BehaviorException { |
328 |
// TODO Auto-generated method stub
|
329 |
} |
330 |
|
331 |
public void mouseReleased(MouseEvent e) throws BehaviorException { |
332 |
// TODO Auto-generated method stub
|
333 |
} |
334 |
|
335 |
public void paintComponent(MapControlDrawer mapControlDrawer) { |
336 |
super.paintComponent(mapControlDrawer);
|
337 |
if (activeService == null || adjustedPoint == null) { |
338 |
return;
|
339 |
} |
340 |
|
341 |
DrawingStatus helperGeo = null;
|
342 |
try {
|
343 |
helperGeo = activeService.draw(adjustedPoint); |
344 |
} catch (BaseException e) {
|
345 |
// TODO Auto-generated catch block
|
346 |
e.printStackTrace(); |
347 |
} |
348 |
if (helperGeo != null) { |
349 |
for (Iterator iterator = helperGeo.getGeometries().iterator(); iterator.hasNext();) { |
350 |
Geometry geometry = (Geometry) iterator.next(); |
351 |
mapControl.getMapControlDrawer().draw( |
352 |
geometry, |
353 |
MapControlLocator.getMapControlManager() |
354 |
.getAxisReferenceSymbol()); |
355 |
} |
356 |
} |
357 |
} |
358 |
|
359 |
private void setCaretPosition() { |
360 |
|
361 |
JEditTextArea jeta = getConsolePanel().getTxt(); |
362 |
jeta.requestFocusInWindow(); |
363 |
jeta.setCaretPosition(jeta.getText().length()); |
364 |
|
365 |
} |
366 |
|
367 |
private void changeCurrentLayer(FLyrVect layer) { |
368 |
this.currentLayer = layer;
|
369 |
this.activeService = serviceRegistration.get(layer);
|
370 |
if (activeService != null) { |
371 |
getNextParameter(); |
372 |
MainFrame main = PluginServices.getMainFrame(); |
373 |
main.setSelectedTool(activeService.getName()); |
374 |
} |
375 |
} |
376 |
|
377 |
private void showCoords(Point2D point) { |
378 |
String[] axisText = new String[2]; |
379 |
axisText[0] = "X = "; |
380 |
axisText[1] = "Y = "; |
381 |
|
382 |
ViewPort vp = mapControl.getMapContext().getViewPort(); |
383 |
IProjection iProj = vp.getProjection(); |
384 |
Point p = vp.convertToMapPoint(point);
|
385 |
Point2D p2D = new Point2D.Double(p.getX(), p.getY()); |
386 |
|
387 |
sbl.setFractionDigits(p2D); |
388 |
axisText = sbl.setCoorDisplayText(axisText); |
389 |
MainFrame mF = PluginServices.getMainFrame(); |
390 |
|
391 |
if (mF != null) { |
392 |
mF.getStatusBar() |
393 |
.setMessage( |
394 |
"units",
|
395 |
PluginServices.getText(this, MapContext
|
396 |
.getDistanceNames()[vp.getDistanceUnits()])); |
397 |
mF.getStatusBar().setControlValue("scale",
|
398 |
String.valueOf(mapControl.getMapContext().getScaleView()));
|
399 |
mF.getStatusBar().setMessage("projection", iProj.getAbrev());
|
400 |
|
401 |
String[] coords = sbl.getCoords(p2D); |
402 |
mF.getStatusBar().setMessage("x", axisText[0] + coords[0]); |
403 |
mF.getStatusBar().setMessage("y", axisText[1] + coords[1]); |
404 |
} |
405 |
|
406 |
} |
407 |
|
408 |
private void textEntered(String response) throws InvalidEntryException { |
409 |
if (response == null) { |
410 |
cleanBehavior(); |
411 |
} else {
|
412 |
List<TYPE> types = currentParam.getTypes();
|
413 |
Point point = null; |
414 |
Double value = null; |
415 |
boolean insertedValue = false; |
416 |
if (!insertedValue && types.contains(TYPE.POSITION)) {
|
417 |
try {
|
418 |
point = parsePoint(response); |
419 |
} catch (BaseException e) {
|
420 |
logger.info("Can't parse string as a point", e);
|
421 |
point = null;
|
422 |
} |
423 |
if (point != null) { |
424 |
this.activeService.value(point);
|
425 |
insertedValue = true;
|
426 |
} |
427 |
} |
428 |
if (!insertedValue && types.contains(TYPE.VALUE)) {
|
429 |
try {
|
430 |
value = Double.valueOf(response);
|
431 |
} catch (NumberFormatException e) { |
432 |
logger.info(e.getMessage(), e); |
433 |
value = null;
|
434 |
} |
435 |
if(value != null){ |
436 |
this.activeService.value(value);
|
437 |
insertedValue = true;
|
438 |
} |
439 |
} |
440 |
// TODO: contemplar el resto de casos
|
441 |
// TYPE.LIST_POSITIONS,
|
442 |
// TYPE.OPTION,
|
443 |
// TYPE.SELECTION,
|
444 |
// TYPE.GEOMETRY
|
445 |
|
446 |
if(!insertedValue){
|
447 |
throw new InvalidEntryException("Invalid entry: " + response, null); |
448 |
} |
449 |
getNextParameter(); |
450 |
} |
451 |
} |
452 |
|
453 |
private Point parsePoint(String response) throws CreateGeometryException, |
454 |
ParsePointException, DataException { |
455 |
String[] numbers = new String[1]; |
456 |
numbers[0] = response;
|
457 |
numbers = response.split(",");
|
458 |
if (numbers.length == 2) { |
459 |
if (numbers[0].startsWith("(") && numbers[1].endsWith(")\n")) { // CCS |
460 |
numbers[0] = numbers[0].replace("(", ""); |
461 |
numbers[1] = numbers[1].replace(")\n", ""); |
462 |
} |
463 |
double[] values = new double[] { Double.parseDouble(numbers[0]), |
464 |
Double.parseDouble(numbers[1]) }; |
465 |
|
466 |
Point point;
|
467 |
point = geomManager.createPoint( |
468 |
values[0],
|
469 |
values[1],
|
470 |
currentLayer.getFeatureStore().getDefaultFeatureType().getDefaultGeometryAttribute().getGeomType().getSubType()); |
471 |
// TODO: Maybe do util class to get type and subtype of a featureStore or a method in manager
|
472 |
return point;
|
473 |
} else {
|
474 |
throw new ParsePointException("Can't create a point", null); |
475 |
} |
476 |
} |
477 |
|
478 |
private JDockPanel getDockConsole() {
|
479 |
if (dockConsole == null) { |
480 |
dockConsole = new JDockPanel(getConsolePanel());
|
481 |
} |
482 |
return dockConsole;
|
483 |
} |
484 |
|
485 |
public void showConsole() { |
486 |
if (isShowConsole) {
|
487 |
return;
|
488 |
} |
489 |
isShowConsole = true;
|
490 |
getMapControl().remove(getDockConsole()); |
491 |
getMapControl().setLayout(new BorderLayout()); |
492 |
getMapControl().add(getDockConsole(), BorderLayout.SOUTH);
|
493 |
getDockConsole().setVisible(true);
|
494 |
setCaretPosition(); |
495 |
} |
496 |
|
497 |
public void hideConsole() { |
498 |
isShowConsole = false;
|
499 |
getDockConsole().setVisible(false);
|
500 |
|
501 |
} |
502 |
|
503 |
private JConsole getConsolePanel() {
|
504 |
if (console == null) { |
505 |
console = new JConsole(true); |
506 |
// Para distinguir cuando se est? escribiendo sobre la consola y
|
507 |
// cuando no.
|
508 |
console.setJTextName("VectorEditingConsole");
|
509 |
} |
510 |
return console;
|
511 |
} |
512 |
|
513 |
private void addConsoleListener(String prefix, ResponseListener listener) { |
514 |
consoleResponseAdapter.putSpaceListener(prefix, listener); |
515 |
|
516 |
} |
517 |
|
518 |
static class ResponseAdapter implements ResponseListener { |
519 |
|
520 |
private HashMap<String, ResponseListener> spaceListener = new HashMap<String, ResponseListener>(); |
521 |
|
522 |
public void putSpaceListener(String namespace, ResponseListener listener) { |
523 |
spaceListener.put(namespace, listener); |
524 |
} |
525 |
|
526 |
public void acceptResponse(String response) { |
527 |
boolean nameSpace = false; |
528 |
int n = -1; |
529 |
if (response != null) { |
530 |
if ((n = response.indexOf(':')) != -1) { |
531 |
nameSpace = true;
|
532 |
} |
533 |
} |
534 |
|
535 |
if (nameSpace) {
|
536 |
ResponseListener listener = spaceListener.get(response |
537 |
.substring(0, n));
|
538 |
if (listener != null) { |
539 |
listener.acceptResponse(response.substring(n + 1));
|
540 |
} |
541 |
} else {
|
542 |
Iterator<ResponseListener> i = spaceListener.values()
|
543 |
.iterator(); |
544 |
while (i.hasNext()) {
|
545 |
ResponseListener listener = i.next(); |
546 |
listener.acceptResponse(response); |
547 |
} |
548 |
} |
549 |
} |
550 |
} |
551 |
} |