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