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 @ 45
History | View | Annotate | Download (12.7 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.Color; |
10 |
import java.awt.Graphics; |
11 |
import java.awt.Image; |
12 |
import java.awt.event.MouseEvent; |
13 |
import java.awt.geom.Point2D; |
14 |
import java.awt.image.BufferedImage; |
15 |
import java.util.HashMap; |
16 |
import java.util.Map; |
17 |
|
18 |
import org.cresques.cts.IProjection; |
19 |
import org.gvsig.andami.PluginServices; |
20 |
import org.gvsig.andami.ui.mdiFrame.MainFrame; |
21 |
import org.gvsig.app.project.documents.view.gui.DefaultViewPanel; |
22 |
import org.gvsig.app.project.documents.view.toolListeners.StatusBarListener; |
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.utils.console.JConsole; |
43 |
import org.gvsig.utils.console.ResponseListener; |
44 |
import org.gvsig.utils.console.jedit.JEditTextArea; |
45 |
import org.gvsig.vectorediting.lib.api.EditingLocator; |
46 |
import org.gvsig.vectorediting.lib.api.EditingManager; |
47 |
import org.gvsig.vectorediting.lib.api.EditingService; |
48 |
import org.gvsig.vectorediting.lib.api.EditingServiceParameter; |
49 |
import org.gvsig.vectorediting.lib.api.EditingServiceParameter.TYPE; |
50 |
import org.gvsig.vectorediting.swing.api.EditingBehavior; |
51 |
|
52 |
public class DefaultEditingBehavior extends Behavior implements EditingBehavior { |
53 |
|
54 |
private Map<FLyrVect, EditingService> serviceRegistration; |
55 |
|
56 |
private MapControl mapControl;
|
57 |
|
58 |
private FLyrVect currentLayer;
|
59 |
|
60 |
private EditingService activeService;
|
61 |
|
62 |
private EditingServiceParameter currentParam;
|
63 |
|
64 |
private Point adjustedPoint; |
65 |
|
66 |
private StatusBarListener sbl = null; |
67 |
|
68 |
private static final Image imageCursor = new BufferedImage(32, 32, |
69 |
BufferedImage.TYPE_INT_ARGB);
|
70 |
static {
|
71 |
Graphics g = imageCursor.getGraphics();
|
72 |
int size1 = 15; |
73 |
int x = 16; |
74 |
int y = 16; |
75 |
g.setColor(Color.MAGENTA);
|
76 |
g.drawLine((x - size1), (y), (x + size1), (y)); |
77 |
g.drawLine((x), (y - size1), (x), (y + size1)); |
78 |
g.drawRect((x - 6), (y - 6), 12, 12); |
79 |
g.drawRect((x - 3), (y - 3), 6, 6); |
80 |
} |
81 |
|
82 |
private LayerListener layerListener = new LayerListener() { |
83 |
|
84 |
public void activationChanged(LayerEvent e) { |
85 |
DefaultViewPanel view = getActiveView(); |
86 |
FLayer layer = e.getSource(); |
87 |
if (layer instanceof FLyrVect) { |
88 |
|
89 |
if (layer.isActive() && layer.isEditing()) {
|
90 |
view.showConsole(); |
91 |
getMapControl().setTool("VectorEditing");
|
92 |
// cleanBehavior();
|
93 |
} |
94 |
else if (!layer.isEditing() && layer.isActive()) { |
95 |
changeCurrentLayer((FLyrVect) layer); |
96 |
view.hideConsole(); |
97 |
getMapControl().setTool("zoomIn");
|
98 |
PluginServices.getMainFrame().setSelectedTool("ZOOM_IN");
|
99 |
} |
100 |
changeCurrentLayer((FLyrVect) layer); |
101 |
|
102 |
} |
103 |
|
104 |
} |
105 |
|
106 |
public void drawValueChanged(LayerEvent e) {} |
107 |
|
108 |
public void editionChanged(LayerEvent e) {} |
109 |
|
110 |
public void nameChanged(LayerEvent e) {} |
111 |
|
112 |
public void visibilityChanged(LayerEvent e) {} |
113 |
}; |
114 |
|
115 |
public void cleanBehavior() { |
116 |
// activeService = null;
|
117 |
// currentParam = null;
|
118 |
|
119 |
DefaultViewPanel view = getActiveView(); |
120 |
view.getConsolePanel().addText("\nSeleccione un herramienta\n",
|
121 |
JConsole.MESSAGE); |
122 |
// MainFrame main = PluginServices.getMainFrame();
|
123 |
// main.setSelectedTool(""); We can define a default tool. I.E Zoom in.
|
124 |
} |
125 |
|
126 |
public DefaultEditingBehavior(MapControl mapControl) {
|
127 |
// TODO Auto-generated constructor stub
|
128 |
if (mapControl != null) { |
129 |
this.mapControl = mapControl;
|
130 |
serviceRegistration = new HashMap<FLyrVect, EditingService>(); |
131 |
sbl = new StatusBarListener(mapControl);
|
132 |
initMapControlListeners(mapControl); |
133 |
FLayers layers = mapControl.getMapContext().getLayers(); |
134 |
for (int i = 0; i < layers.getLayersCount(); i++) { |
135 |
if (layers.getLayer(i) instanceof FLyrVect |
136 |
&& layers.getLayer(i).isActive()) |
137 |
changeCurrentLayer((FLyrVect) layers.getLayer(i)); |
138 |
} |
139 |
|
140 |
} |
141 |
} |
142 |
|
143 |
public void activateTool(String name) { |
144 |
|
145 |
EditingManager manager = EditingLocator.getManager(); |
146 |
|
147 |
if (currentLayer != null) { |
148 |
EditingService service = serviceRegistration.get(currentLayer); |
149 |
if (service == null) { |
150 |
service = (EditingService) manager |
151 |
.getEditingService(name, currentLayer.getFeatureStore()); |
152 |
serviceRegistration.put(currentLayer, service); |
153 |
} |
154 |
if (service != null) { |
155 |
this.activeService = service;
|
156 |
service.start(); |
157 |
getNextParameter(); |
158 |
} |
159 |
} |
160 |
} |
161 |
|
162 |
private void askQuestion(EditingServiceParameter param) { |
163 |
DefaultViewPanel viewActive = getActiveView(); |
164 |
if (viewActive != null) { |
165 |
viewActive.getConsolePanel().addText( |
166 |
"\n" + "#" + param.getDescription() + " > ", JConsole.MESSAGE); |
167 |
} |
168 |
} |
169 |
|
170 |
private DefaultViewPanel getActiveView() {
|
171 |
org.gvsig.andami.ui.mdiManager.IWindow activeWindow = PluginServices |
172 |
.getMDIManager().getActiveWindow(); |
173 |
|
174 |
if (activeWindow instanceof DefaultViewPanel) { |
175 |
return (DefaultViewPanel) activeWindow;
|
176 |
} |
177 |
return null; |
178 |
} |
179 |
|
180 |
@Override
|
181 |
public ToolListener getListener() {
|
182 |
return new ToolListener() { |
183 |
|
184 |
/**
|
185 |
*
|
186 |
*/
|
187 |
public boolean cancelDrawing() { |
188 |
return false; |
189 |
} |
190 |
|
191 |
/**
|
192 |
*
|
193 |
*/
|
194 |
public Image getImageCursor() { |
195 |
return imageCursor;
|
196 |
} |
197 |
}; |
198 |
} |
199 |
|
200 |
private void getNextParameter() { |
201 |
currentParam = activeService.next(); |
202 |
if (currentParam == null) { |
203 |
activeService.finish(); |
204 |
mapControl.rePaintDirtyLayers(); |
205 |
activeService.start(); |
206 |
getNextParameter(); |
207 |
} |
208 |
else {
|
209 |
askQuestion(currentParam); |
210 |
setCaretPosition(getActiveView()); |
211 |
} |
212 |
|
213 |
} |
214 |
|
215 |
private void initMapControlListeners(MapControl mapControl) { |
216 |
DefaultViewPanel view = getActiveView(); |
217 |
|
218 |
view.addConsoleListener("VectorEditingConsole", new ResponseListener() { |
219 |
|
220 |
public void acceptResponse(String response) { |
221 |
try {
|
222 |
textEntered(response); |
223 |
} |
224 |
catch (RuntimeException e) { |
225 |
showErrorConsole(e); |
226 |
getNextParameter(); |
227 |
} |
228 |
} |
229 |
|
230 |
private void showErrorConsole(RuntimeException e) { |
231 |
DefaultViewPanel view = getActiveView(); |
232 |
view.getConsolePanel().addText(e.getMessage(), JConsole.ERROR); |
233 |
|
234 |
} |
235 |
}); |
236 |
|
237 |
MapContext context = mapControl.getMapContext(); |
238 |
FLayers layers = context.getLayers(); |
239 |
layers.addLayerListener(layerListener); |
240 |
|
241 |
layers.addLayerCollectionListener(new LayerCollectionListener() {
|
242 |
|
243 |
public void layerAdded(LayerCollectionEvent e) { |
244 |
FLayers layers2 = e.getLayers(); |
245 |
for (int i = 0; i < layers2.getLayersCount(); i++) { |
246 |
FLayer layer = layers2.getLayer(i); |
247 |
if (layer instanceof FLyrVect) { |
248 |
((FLyrVect) layer).addLayerListener(layerListener); |
249 |
} |
250 |
} |
251 |
} |
252 |
|
253 |
public void layerAdding(LayerCollectionEvent e) |
254 |
throws CancelationException {}
|
255 |
|
256 |
public void layerMoved(LayerPositionEvent e) {} |
257 |
|
258 |
public void layerMoving(LayerPositionEvent e) throws CancelationException {} |
259 |
|
260 |
public void layerRemoved(LayerCollectionEvent e) { |
261 |
FLayers layers2 = e.getLayers(); |
262 |
for (int i = 0; i < layers2.getLayersCount(); i++) { |
263 |
FLayer layer = layers2.getLayer(i); |
264 |
if (layer instanceof FLyrVect) { |
265 |
((FLyrVect) layer).removeLayerListener(layerListener); |
266 |
} |
267 |
} |
268 |
} |
269 |
|
270 |
public void layerRemoving(LayerCollectionEvent e) |
271 |
throws CancelationException {}
|
272 |
|
273 |
public void visibilityChanged(LayerCollectionEvent e) |
274 |
throws CancelationException {}
|
275 |
}); |
276 |
} |
277 |
|
278 |
public void mouseClicked(MouseEvent e) throws BehaviorException { |
279 |
// TODO Auto-generated method stub
|
280 |
if (activeService != null) { |
281 |
if (currentParam != null) { |
282 |
switch (currentParam.getType()) {
|
283 |
case POSITION:
|
284 |
ViewPort vp = mapControl.getViewPort(); |
285 |
Point point;
|
286 |
point = vp.convertToMapPoint(e.getX(), e.getY()); |
287 |
this.activeService.value(point);
|
288 |
|
289 |
case LIST_POSITIONS:
|
290 |
case OPTION:
|
291 |
case SELECTION:
|
292 |
case GEOMETRY:
|
293 |
case VALUE:
|
294 |
} |
295 |
getNextParameter(); |
296 |
} |
297 |
|
298 |
} |
299 |
} |
300 |
|
301 |
public void mouseEntered(MouseEvent e) throws BehaviorException { |
302 |
// TODO Auto-generated method stub
|
303 |
} |
304 |
|
305 |
public void mouseMoved(MouseEvent e) throws BehaviorException { |
306 |
ViewPort vp = mapControl.getViewPort(); |
307 |
adjustedPoint = vp.convertToMapPoint(e.getX(),e.getY()); |
308 |
showCoords(e.getPoint()); |
309 |
|
310 |
getMapControl().repaint(); |
311 |
} |
312 |
|
313 |
public void mousePressed(MouseEvent e) throws BehaviorException { |
314 |
// TODO Auto-generated method stub
|
315 |
} |
316 |
|
317 |
public void mouseReleased(MouseEvent e) throws BehaviorException { |
318 |
// TODO Auto-generated method stub
|
319 |
} |
320 |
|
321 |
public void paintComponent(MapControlDrawer mapControlDrawer) { |
322 |
super.paintComponent(mapControlDrawer);
|
323 |
if (activeService == null || adjustedPoint == null) { |
324 |
return;
|
325 |
} |
326 |
|
327 |
Geometry helperGeo = activeService.draw(adjustedPoint); |
328 |
if (helperGeo != null) { |
329 |
mapControl.getMapControlDrawer().draw(helperGeo, |
330 |
MapControlLocator.getMapControlManager().getAxisReferenceSymbol()); |
331 |
} |
332 |
} |
333 |
|
334 |
private void setCaretPosition(DefaultViewPanel view) { |
335 |
|
336 |
JEditTextArea jeta = view.getConsolePanel().getTxt(); |
337 |
jeta.requestFocusInWindow(); |
338 |
jeta.setCaretPosition(jeta.getText().length()); |
339 |
|
340 |
} |
341 |
|
342 |
private void changeCurrentLayer(FLyrVect layer) { |
343 |
this.currentLayer = layer;
|
344 |
this.activeService = serviceRegistration.get(layer);
|
345 |
if (activeService != null) { |
346 |
getNextParameter(); |
347 |
MainFrame main = PluginServices.getMainFrame(); |
348 |
main.setSelectedTool(activeService.getName()); |
349 |
} |
350 |
} |
351 |
|
352 |
private void showCoords(Point2D point) { |
353 |
String[] axisText = new String[2]; |
354 |
axisText[0] = "X = "; |
355 |
axisText[1] = "Y = "; |
356 |
|
357 |
ViewPort vp = mapControl.getMapContext().getViewPort(); |
358 |
IProjection iProj = vp.getProjection(); |
359 |
Point p = vp.convertToMapPoint(point);
|
360 |
Point2D p2D = new Point2D.Double(p.getX(), p.getY()); |
361 |
|
362 |
sbl.setFractionDigits(p2D); |
363 |
axisText = sbl.setCoorDisplayText(axisText); |
364 |
MainFrame mF = PluginServices.getMainFrame(); |
365 |
|
366 |
if (mF != null) { |
367 |
mF.getStatusBar().setMessage( |
368 |
"units",
|
369 |
PluginServices.getText(this,
|
370 |
MapContext.getDistanceNames()[vp.getDistanceUnits()])); |
371 |
mF.getStatusBar().setControlValue("scale",
|
372 |
String.valueOf(mapControl.getMapContext().getScaleView()));
|
373 |
mF.getStatusBar().setMessage("projection", iProj.getAbrev());
|
374 |
|
375 |
String[] coords = sbl.getCoords(p2D); |
376 |
mF.getStatusBar().setMessage("x", axisText[0] + coords[0]); |
377 |
mF.getStatusBar().setMessage("y", axisText[1] + coords[1]); |
378 |
} |
379 |
|
380 |
} |
381 |
|
382 |
private void textEntered(String response) { |
383 |
if (response == null) { |
384 |
cleanBehavior(); |
385 |
} |
386 |
else {
|
387 |
TYPE type = currentParam.getType(); |
388 |
try {
|
389 |
switch (type) {
|
390 |
case POSITION:
|
391 |
String[] numbers = new String[1]; |
392 |
numbers[0] = response;
|
393 |
numbers = response.split(",");
|
394 |
if (numbers.length == 2) { |
395 |
if (numbers[0].startsWith("(") && numbers[1].endsWith(")\n")) { // CCS |
396 |
numbers[0] = numbers[0].replace("(", ""); |
397 |
numbers[1] = numbers[1].replace(")\n", ""); |
398 |
} |
399 |
double[] values = new double[] { Double.parseDouble(numbers[0]), |
400 |
Double.parseDouble(numbers[1]) }; |
401 |
Point2D point = new Point2D.Double(values[0], values[1]); |
402 |
this.activeService.value(point);
|
403 |
|
404 |
} |
405 |
else {
|
406 |
throw new RuntimeException("Datos inv?lidos: " + response); |
407 |
} |
408 |
case LIST_POSITIONS:
|
409 |
case OPTION:
|
410 |
case SELECTION:
|
411 |
case GEOMETRY:
|
412 |
case VALUE:
|
413 |
} |
414 |
getNextParameter(); |
415 |
} |
416 |
catch (Exception e) { |
417 |
throw new RuntimeException("Datos inv?lidos: " + response); |
418 |
} |
419 |
} |
420 |
} |
421 |
} |