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