root / trunk / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / CADToolAdapter.java @ 6773
History | View | Annotate | Download (26.9 KB)
1 |
package com.iver.cit.gvsig.gui.cad; |
---|---|
2 |
|
3 |
import java.awt.Color; |
4 |
import java.awt.Cursor; |
5 |
import java.awt.FontMetrics; |
6 |
import java.awt.Graphics; |
7 |
import java.awt.Graphics2D; |
8 |
import java.awt.Image; |
9 |
import java.awt.Point; |
10 |
import java.awt.Toolkit; |
11 |
import java.awt.event.InputEvent; |
12 |
import java.awt.event.MouseEvent; |
13 |
import java.awt.event.MouseWheelEvent; |
14 |
import java.awt.geom.Point2D; |
15 |
import java.awt.geom.Rectangle2D; |
16 |
import java.awt.image.MemoryImageSource; |
17 |
import java.io.IOException; |
18 |
import java.text.NumberFormat; |
19 |
import java.util.ArrayList; |
20 |
import java.util.HashMap; |
21 |
import java.util.Stack; |
22 |
import java.util.prefs.Preferences; |
23 |
|
24 |
import org.cresques.cts.IProjection; |
25 |
|
26 |
import com.iver.andami.PluginServices; |
27 |
import com.iver.andami.ui.mdiFrame.MainFrame; |
28 |
import com.iver.cit.gvsig.CADExtension; |
29 |
import com.iver.cit.gvsig.EditionManager; |
30 |
import com.iver.cit.gvsig.fmap.FMap; |
31 |
import com.iver.cit.gvsig.fmap.MapControl; |
32 |
import com.iver.cit.gvsig.fmap.ViewPort; |
33 |
import com.iver.cit.gvsig.fmap.core.v02.FConstant; |
34 |
import com.iver.cit.gvsig.fmap.core.v02.FConverter; |
35 |
import com.iver.cit.gvsig.fmap.core.v02.FSymbol; |
36 |
import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
37 |
import com.iver.cit.gvsig.fmap.edition.EditionEvent; |
38 |
import com.iver.cit.gvsig.fmap.edition.UtilFunctions; |
39 |
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter; |
40 |
import com.iver.cit.gvsig.fmap.layers.FBitSet; |
41 |
import com.iver.cit.gvsig.fmap.layers.FLyrVect; |
42 |
import com.iver.cit.gvsig.fmap.layers.SpatialCache; |
43 |
import com.iver.cit.gvsig.fmap.tools.BehaviorException; |
44 |
import com.iver.cit.gvsig.fmap.tools.Behavior.Behavior; |
45 |
import com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener; |
46 |
import com.iver.cit.gvsig.gui.View; |
47 |
import com.iver.cit.gvsig.gui.cad.snapping.FinalPointSnapper; |
48 |
import com.iver.cit.gvsig.gui.cad.snapping.ISnapper; |
49 |
import com.iver.cit.gvsig.gui.cad.snapping.ISnapperRaster; |
50 |
import com.iver.cit.gvsig.gui.cad.snapping.ISnapperVectorial; |
51 |
import com.iver.cit.gvsig.gui.cad.snapping.NearestPointSnapper; |
52 |
import com.iver.cit.gvsig.gui.cad.snapping.PixelSnapper; |
53 |
import com.iver.cit.gvsig.gui.cad.snapping.SnappingVisitor; |
54 |
import com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool; |
55 |
import com.iver.cit.gvsig.layers.ILayerEdited; |
56 |
import com.iver.cit.gvsig.layers.VectorialLayerEdited; |
57 |
import com.iver.utiles.console.JConsole; |
58 |
import com.vividsolutions.jts.geom.Envelope; |
59 |
|
60 |
public class CADToolAdapter extends Behavior { |
61 |
private static HashMap namesCadTools = new HashMap(); |
62 |
|
63 |
private EditionManager editionManager = new EditionManager(); |
64 |
|
65 |
public static final int ABSOLUTE = 0; |
66 |
|
67 |
public static final int RELATIVE_SCP = 1; |
68 |
|
69 |
public static final int RELATIVE_SCU = 2; |
70 |
|
71 |
public static final int POLAR_SCP = 3; |
72 |
|
73 |
public static final int POLAR_SCU = 4; |
74 |
|
75 |
private double[] previousPoint = null; |
76 |
|
77 |
private Stack cadToolStack = new Stack(); |
78 |
|
79 |
// Para pasarle las coordenadas cuando se produce un evento textEntered
|
80 |
private int lastX; |
81 |
|
82 |
private int lastY; |
83 |
|
84 |
private FSymbol symbol = new FSymbol(FConstant.SYMBOL_TYPE_POINT, Color.RED); |
85 |
|
86 |
private Point2D mapAdjustedPoint; |
87 |
|
88 |
private ISnapper usedSnap = null; |
89 |
|
90 |
private boolean questionAsked = false; |
91 |
|
92 |
private Point2D adjustedPoint; |
93 |
|
94 |
private boolean bRefent = true; |
95 |
|
96 |
private boolean bForceCoord = false; |
97 |
|
98 |
private CADGrid cadgrid = new CADGrid(); |
99 |
|
100 |
private boolean bOrtoMode; |
101 |
|
102 |
private Color theTipColor = new Color(255, 255, 155); |
103 |
private static Preferences prefs = Preferences.userRoot().node( "cadtooladapter" ); |
104 |
|
105 |
/**
|
106 |
* Pinta de alguna manera especial las geometrias seleccionadas para la
|
107 |
* edici?n. En caso de que el snapping est? activado, pintar? el efecto del
|
108 |
* mismo.
|
109 |
*
|
110 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#paintComponent(java.awt.Graphics)
|
111 |
*/
|
112 |
public void paintComponent(Graphics g) { |
113 |
super.paintComponent(g);
|
114 |
if (CADExtension.getCADToolAdapter()!=this) |
115 |
return;
|
116 |
drawCursor(g); |
117 |
getGrid().drawGrid(g); |
118 |
if (adjustedPoint != null) { |
119 |
Point2D p = null; |
120 |
if (mapAdjustedPoint != null) { |
121 |
p = mapAdjustedPoint; |
122 |
} else {
|
123 |
p = getMapControl().getViewPort().toMapPoint(adjustedPoint); |
124 |
} |
125 |
|
126 |
((CADTool) cadToolStack.peek()) |
127 |
.drawOperation(g, p.getX(), p.getY()); |
128 |
} |
129 |
} |
130 |
|
131 |
/**
|
132 |
* @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
|
133 |
*/
|
134 |
public void mouseClicked(MouseEvent e) throws BehaviorException { |
135 |
if (e.getButton() == MouseEvent.BUTTON3) { |
136 |
CADExtension.showPopup(e); |
137 |
} |
138 |
} |
139 |
|
140 |
/**
|
141 |
* @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
|
142 |
*/
|
143 |
public void mouseEntered(MouseEvent e) throws BehaviorException { |
144 |
clearMouseImage(); |
145 |
} |
146 |
|
147 |
/**
|
148 |
* @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
|
149 |
*/
|
150 |
public void mouseExited(MouseEvent e) throws BehaviorException { |
151 |
} |
152 |
|
153 |
/**
|
154 |
* @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
|
155 |
*/
|
156 |
public void mousePressed(MouseEvent e) throws BehaviorException { |
157 |
if (e.getButton() == MouseEvent.BUTTON1) { |
158 |
ViewPort vp = getMapControl().getMapContext().getViewPort(); |
159 |
Point2D p;
|
160 |
|
161 |
if (mapAdjustedPoint != null) { |
162 |
p = mapAdjustedPoint; |
163 |
} else {
|
164 |
p = vp.toMapPoint(adjustedPoint); |
165 |
} |
166 |
transition(new double[] { p.getX(), p.getY() }, e, ABSOLUTE); |
167 |
} |
168 |
} |
169 |
|
170 |
/**
|
171 |
* Ajusta un punto de la imagen que se pasa como par?metro al grid si ?ste
|
172 |
* est? activo y devuelve la distancia de un punto al punto ajustado
|
173 |
*
|
174 |
* @param point
|
175 |
* @param mapHandlerAdjustedPoint
|
176 |
* DOCUMENT ME!
|
177 |
*
|
178 |
* @return Distancia del punto que se pasa como
|
179 |
* par?metro al punto ajustado. Si no hay ajuste,
|
180 |
* devuelve Double.MAX_VALUE
|
181 |
*/
|
182 |
private double adjustToHandler(Point2D point, |
183 |
Point2D mapHandlerAdjustedPoint) {
|
184 |
|
185 |
if (!isRefentEnabled())
|
186 |
return Double.MAX_VALUE; |
187 |
|
188 |
ILayerEdited aux = CADExtension.getEditionManager().getActiveLayerEdited(); |
189 |
if (!(aux instanceof VectorialLayerEdited)) |
190 |
return Double.MAX_VALUE; |
191 |
VectorialLayerEdited vle = (VectorialLayerEdited) aux; |
192 |
|
193 |
ArrayList snappers = vle.getSnappers();
|
194 |
ArrayList layersToSnap = vle.getLayersToSnap();
|
195 |
|
196 |
|
197 |
ViewPort vp = getMapControl().getViewPort(); |
198 |
|
199 |
// TODO: PROVISIONAL. PONER ALGO COMO ESTO EN UN CUADRO DE DIALOGO
|
200 |
// DE CONFIGURACI?N DEL SNAPPING
|
201 |
FinalPointSnapper defaultSnap = new FinalPointSnapper();
|
202 |
NearestPointSnapper nearestSnap = new NearestPointSnapper();
|
203 |
// PixelSnapper pixSnap = new PixelSnapper();
|
204 |
snappers.clear(); |
205 |
snappers.add(defaultSnap); |
206 |
snappers.add(nearestSnap); |
207 |
// snappers.add(pixSnap);
|
208 |
|
209 |
double mapTolerance = vp.toMapDistance(SelectionCADTool.tolerance);
|
210 |
double minDist = mapTolerance;
|
211 |
// double rw = getMapControl().getViewPort().toMapDistance(5);
|
212 |
Point2D mapPoint = point;
|
213 |
Rectangle2D r = new Rectangle2D.Double(mapPoint.getX() - mapTolerance / 2, |
214 |
mapPoint.getY() - mapTolerance / 2, mapTolerance, mapTolerance);
|
215 |
|
216 |
Envelope e = FConverter.convertRectangle2DtoEnvelope(r); |
217 |
|
218 |
usedSnap = null;
|
219 |
Point2D lastPoint = null; |
220 |
if (previousPoint != null) |
221 |
{ |
222 |
lastPoint = new Point2D.Double(previousPoint[0], previousPoint[1]); |
223 |
} |
224 |
for (int j = 0; j < layersToSnap.size(); j++) |
225 |
{ |
226 |
FLyrVect lyrVect = (FLyrVect) layersToSnap.get(j); |
227 |
SpatialCache cache = lyrVect.getSpatialCache(); |
228 |
if (lyrVect.isVisible())
|
229 |
{ |
230 |
// La lista de snappers est? siempre ordenada por prioridad. Los de mayor
|
231 |
// prioridad est?n primero.
|
232 |
for (int i = 0; i < snappers.size(); i++) |
233 |
{ |
234 |
ISnapper theSnapper = (ISnapper) snappers.get(i); |
235 |
|
236 |
if (usedSnap != null) |
237 |
{ |
238 |
// Si ya tenemos un snap y es de alta prioridad, cogemos ese. (A no ser que en otra capa encontremos un snapper mejor)
|
239 |
if (theSnapper.getPriority() < usedSnap.getPriority())
|
240 |
break;
|
241 |
} |
242 |
SnappingVisitor snapVisitor = null;
|
243 |
Point2D theSnappedPoint = null; |
244 |
if (theSnapper instanceof ISnapperVectorial) |
245 |
{ |
246 |
snapVisitor = new SnappingVisitor((ISnapperVectorial) theSnapper, point, mapTolerance, lastPoint);
|
247 |
// System.out.println("Cache size = " + cache.size());
|
248 |
cache.query(e, snapVisitor); |
249 |
theSnappedPoint = snapVisitor.getSnapPoint(); |
250 |
} |
251 |
if (theSnapper instanceof ISnapperRaster) |
252 |
{ |
253 |
ISnapperRaster snapRaster = (ISnapperRaster) theSnapper; |
254 |
theSnappedPoint = snapRaster.getSnapPoint(getMapControl(), point, mapTolerance, lastPoint); |
255 |
} |
256 |
|
257 |
|
258 |
if (theSnappedPoint != null) { |
259 |
double distAux = theSnappedPoint.distance(point);
|
260 |
if (minDist > distAux)
|
261 |
{ |
262 |
minDist = distAux; |
263 |
usedSnap = theSnapper; |
264 |
mapHandlerAdjustedPoint.setLocation(theSnappedPoint); |
265 |
} |
266 |
} |
267 |
} |
268 |
} // visible
|
269 |
} |
270 |
if (usedSnap != null) |
271 |
return minDist;
|
272 |
return Double.MAX_VALUE; |
273 |
|
274 |
} |
275 |
|
276 |
/**
|
277 |
* @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
|
278 |
*/
|
279 |
public void mouseReleased(MouseEvent e) throws BehaviorException { |
280 |
getMapControl().repaint(); |
281 |
} |
282 |
|
283 |
/**
|
284 |
* @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
|
285 |
*/
|
286 |
public void mouseDragged(MouseEvent e) throws BehaviorException { |
287 |
lastX = e.getX(); |
288 |
lastY = e.getY(); |
289 |
|
290 |
calculateSnapPoint(e.getPoint()); |
291 |
} |
292 |
|
293 |
/**
|
294 |
* @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
|
295 |
*/
|
296 |
public void mouseMoved(MouseEvent e) throws BehaviorException { |
297 |
|
298 |
lastX = e.getX(); |
299 |
lastY = e.getY(); |
300 |
|
301 |
calculateSnapPoint(e.getPoint()); |
302 |
|
303 |
showCoords(e.getPoint()); |
304 |
|
305 |
getMapControl().repaint(); |
306 |
} |
307 |
|
308 |
private void showCoords(Point2D pPix) |
309 |
{ |
310 |
String[] axisText = new String[2]; |
311 |
NumberFormat nf = NumberFormat.getInstance(); |
312 |
MapControl mapControl = getMapControl(); |
313 |
ViewPort vp = mapControl.getMapContext().getViewPort(); |
314 |
IProjection iProj = vp.getProjection(); |
315 |
if (iProj.getAbrev().equals("EPSG:4326") || iProj.getAbrev().equals("EPSG:4230")) { |
316 |
axisText[0] = "Lon = "; |
317 |
axisText[1] = "Lat = "; |
318 |
nf.setMaximumFractionDigits(8);
|
319 |
} else {
|
320 |
axisText[0] = "X = "; |
321 |
axisText[1] = "Y = "; |
322 |
nf.setMaximumFractionDigits(2);
|
323 |
} |
324 |
Point2D p;
|
325 |
if (mapAdjustedPoint == null) |
326 |
{ |
327 |
p = vp.toMapPoint(pPix); |
328 |
} |
329 |
else
|
330 |
{ |
331 |
p = mapAdjustedPoint; |
332 |
} |
333 |
MainFrame mF = PluginServices.getMainFrame(); |
334 |
|
335 |
if (mF != null) |
336 |
{ |
337 |
mF.getStatusBar().setMessage("units",
|
338 |
FConstant.NAMES[vp.getDistanceUnits()]); |
339 |
mF.getStatusBar().setControlValue("scale",String.valueOf(mapControl.getMapContext().getScaleView())); |
340 |
mF.getStatusBar().setMessage("projection", iProj.getAbrev());
|
341 |
|
342 |
mF.getStatusBar().setMessage("x",
|
343 |
axisText[0] + String.valueOf(nf.format(p.getX()/FMap.CHANGEM[vp.getDistanceUnits()]))); |
344 |
mF.getStatusBar().setMessage("y",
|
345 |
axisText[1] + String.valueOf(nf.format(p.getY()/FMap.CHANGEM[vp.getDistanceUnits()]))); |
346 |
} |
347 |
} |
348 |
|
349 |
private void clearMouseImage() { |
350 |
int[] pixels = new int[16 * 16]; |
351 |
Image image = Toolkit.getDefaultToolkit().createImage( |
352 |
new MemoryImageSource(16, 16, pixels, 0, 16)); |
353 |
Cursor transparentCursor = Toolkit.getDefaultToolkit() |
354 |
.createCustomCursor(image, new Point(0, 0), "invisiblecursor"); |
355 |
|
356 |
getMapControl().setCursor(transparentCursor); |
357 |
} |
358 |
|
359 |
/**
|
360 |
* DOCUMENT ME!
|
361 |
*
|
362 |
* @param g
|
363 |
* DOCUMENT ME!
|
364 |
*/
|
365 |
private void drawCursor(Graphics g) { |
366 |
|
367 |
Point2D p = adjustedPoint;
|
368 |
|
369 |
if (p == null) { |
370 |
getGrid().setViewPort(getMapControl().getViewPort()); |
371 |
|
372 |
return;
|
373 |
} |
374 |
|
375 |
int size1 = 15; |
376 |
int size2 = 3; |
377 |
g.drawLine((int) (p.getX() - size1), (int) (p.getY()), |
378 |
(int) (p.getX() + size1), (int) (p.getY())); |
379 |
g.drawLine((int) (p.getX()), (int) (p.getY() - size1), |
380 |
(int) (p.getX()), (int) (p.getY() + size1)); |
381 |
|
382 |
// getMapControl().setToolTipText(null);
|
383 |
if (adjustedPoint != null) { |
384 |
if (bForceCoord) {
|
385 |
/* g.setColor(Color.ORANGE);
|
386 |
g.drawRect((int) (adjustedPoint.getX() - 6),
|
387 |
(int) (adjustedPoint.getY() - 6), 12, 12);
|
388 |
g.drawRect((int) (adjustedPoint.getX() - 3),
|
389 |
(int) (adjustedPoint.getY() - 3), 6, 6);
|
390 |
g.setColor(Color.MAGENTA);
|
391 |
g.drawRect((int) (adjustedPoint.getX() - 4),
|
392 |
(int) (adjustedPoint.getY() - 4), 8, 8); */
|
393 |
if (usedSnap != null) |
394 |
{ |
395 |
usedSnap.draw(g, adjustedPoint); |
396 |
|
397 |
Graphics2D g2 = (Graphics2D) g; |
398 |
FontMetrics metrics = g2.getFontMetrics();
|
399 |
int w = metrics.stringWidth(usedSnap.getToolTipText()) + 5; |
400 |
int h = metrics.getMaxAscent() + 5; |
401 |
int x = (int)p.getX()+9; |
402 |
int y = (int)p.getY()- 7; |
403 |
|
404 |
g2.setColor(theTipColor ); |
405 |
g2.fillRect(x, y-h, w, h); |
406 |
g2.setColor(Color.BLACK);
|
407 |
g2.drawRect(x, y-h, w, h); |
408 |
g2.drawString(usedSnap.getToolTipText(), x+3, y-3); |
409 |
|
410 |
|
411 |
// getMapControl().setToolTipText(usedSnap.getToolTipText());
|
412 |
} |
413 |
|
414 |
bForceCoord = false;
|
415 |
} else {
|
416 |
g.drawRect((int) (p.getX() - size2), (int) (p.getY() - size2), |
417 |
(int) (size2 * 2), (int) (size2 * 2)); |
418 |
} |
419 |
} |
420 |
} |
421 |
|
422 |
/**
|
423 |
* DOCUMENT ME!
|
424 |
*
|
425 |
* @param point
|
426 |
*/
|
427 |
private void calculateSnapPoint(Point point) { |
428 |
// Se comprueba el ajuste a rejilla
|
429 |
|
430 |
Point2D gridAdjustedPoint = getMapControl().getViewPort().toMapPoint(
|
431 |
point); |
432 |
double minDistance = Double.MAX_VALUE; |
433 |
CADTool ct = (CADTool) cadToolStack.peek(); |
434 |
if (ct instanceof SelectionCADTool |
435 |
&& ((SelectionCADTool) ct).getStatus().equals( |
436 |
"Selection.FirstPoint")) {
|
437 |
mapAdjustedPoint = gridAdjustedPoint; |
438 |
adjustedPoint = (Point2D) point.clone();
|
439 |
} else {
|
440 |
|
441 |
minDistance = getGrid().adjustToGrid(gridAdjustedPoint); |
442 |
if (minDistance < Double.MAX_VALUE) { |
443 |
adjustedPoint = getMapControl().getViewPort().fromMapPoint( |
444 |
gridAdjustedPoint); |
445 |
mapAdjustedPoint = gridAdjustedPoint; |
446 |
} else {
|
447 |
mapAdjustedPoint = null;
|
448 |
} |
449 |
} |
450 |
Point2D handlerAdjustedPoint = null; |
451 |
|
452 |
// Se comprueba el ajuste a los handlers
|
453 |
if (mapAdjustedPoint != null) { |
454 |
handlerAdjustedPoint = (Point2D) mapAdjustedPoint.clone(); // getMapControl().getViewPort().toMapPoint(point); |
455 |
} else {
|
456 |
handlerAdjustedPoint = getMapControl().getViewPort().toMapPoint( |
457 |
point); |
458 |
} |
459 |
|
460 |
Point2D mapPoint = new Point2D.Double(); |
461 |
double distance = adjustToHandler(handlerAdjustedPoint, mapPoint);
|
462 |
|
463 |
if (distance < minDistance) {
|
464 |
bForceCoord = true;
|
465 |
adjustedPoint = getMapControl().getViewPort().fromMapPoint(mapPoint); |
466 |
mapAdjustedPoint = mapPoint; |
467 |
minDistance = distance; |
468 |
} |
469 |
|
470 |
// Si no hay ajuste
|
471 |
if (minDistance == Double.MAX_VALUE) { |
472 |
adjustedPoint = point; |
473 |
mapAdjustedPoint = null;
|
474 |
} |
475 |
|
476 |
} |
477 |
|
478 |
/**
|
479 |
* @see java.awt.event.MouseWheelListener#mouseWheelMoved(java.awt.event.MouseWheelEvent)
|
480 |
*/
|
481 |
public void mouseWheelMoved(MouseWheelEvent e) throws BehaviorException { |
482 |
} |
483 |
|
484 |
/**
|
485 |
* M?todo que realiza las transiciones en las herramientas en funci?n de un
|
486 |
* texto introducido en la consola
|
487 |
*
|
488 |
* @param text
|
489 |
* DOCUMENT ME!
|
490 |
*/
|
491 |
public void textEntered(String text) { |
492 |
if (text == null) { |
493 |
transition(PluginServices.getText(this,"cancel")); |
494 |
} else {
|
495 |
/*
|
496 |
* if ("".equals(text)) { transition("aceptar"); } else {
|
497 |
*/
|
498 |
text = text.trim(); |
499 |
int type = ABSOLUTE;
|
500 |
String[] numbers = new String[1]; |
501 |
numbers[0] = text;
|
502 |
if (text.indexOf(",") != -1) { |
503 |
|
504 |
numbers = text.split(",");
|
505 |
if (numbers[0].substring(0, 1).equals("@")) { |
506 |
numbers[0] = numbers[0].substring(1, numbers[0].length()); |
507 |
type = RELATIVE_SCU; |
508 |
if (numbers[0].substring(0, 1).equals("*")) { |
509 |
type = RELATIVE_SCP; |
510 |
numbers[0] = numbers[0].substring(1, numbers[0] |
511 |
.length()); |
512 |
} |
513 |
} |
514 |
} else if (text.indexOf("<") != -1) { |
515 |
type = POLAR_SCP; |
516 |
numbers = text.split("<");
|
517 |
if (numbers[0].substring(0, 1).equals("@")) { |
518 |
numbers[0] = numbers[0].substring(1, numbers[0].length()); |
519 |
type = POLAR_SCU; |
520 |
if (numbers[0].substring(0, 1).equals("*")) { |
521 |
type = POLAR_SCP; |
522 |
numbers[0] = numbers[0].substring(1, numbers[0] |
523 |
.length()); |
524 |
} |
525 |
} |
526 |
} |
527 |
|
528 |
double[] values = null; |
529 |
|
530 |
try {
|
531 |
if (numbers.length == 2) { |
532 |
// punto
|
533 |
values = new double[] { Double.parseDouble(numbers[0]), |
534 |
Double.parseDouble(numbers[1]) }; |
535 |
transition(values, null, type);
|
536 |
} else if (numbers.length == 1) { |
537 |
// valor
|
538 |
values = new double[] { Double.parseDouble(numbers[0]) }; |
539 |
transition(values[0]);
|
540 |
} |
541 |
} catch (NumberFormatException e) { |
542 |
transition(text); |
543 |
} catch (NullPointerException e) { |
544 |
transition(text); |
545 |
} |
546 |
// }
|
547 |
} |
548 |
getMapControl().repaint(); |
549 |
} |
550 |
|
551 |
/**
|
552 |
* DOCUMENT ME!
|
553 |
*/
|
554 |
public void configureMenu() { |
555 |
String[] desc = ((CADTool) cadToolStack.peek()).getDescriptions(); |
556 |
// String[] labels = ((CADTool)
|
557 |
// cadToolStack.peek()).getCurrentTransitions();
|
558 |
CADExtension.clearMenu(); |
559 |
|
560 |
for (int i = 0; i < desc.length; i++) { |
561 |
if (desc[i] != null) { |
562 |
CADExtension |
563 |
.addMenuEntry(PluginServices.getText(this, desc[i]));// , |
564 |
// labels[i]);
|
565 |
} |
566 |
} |
567 |
|
568 |
} |
569 |
|
570 |
/**
|
571 |
* Recibe los valores de la transici?n (normalmente un punto) y el evento
|
572 |
* con el que se gener? (si fue de rat?n ser? MouseEvent, el que viene en el
|
573 |
* pressed) y si es de teclado, ser? un KeyEvent. Del evento se puede sacar
|
574 |
* informaci?n acerca de si estaba pulsada la tecla CTRL, o Alt, etc.
|
575 |
*
|
576 |
* @param values
|
577 |
* @param event
|
578 |
*/
|
579 |
private void transition(double[] values, InputEvent event, int type) { |
580 |
questionAsked = true;
|
581 |
if (!cadToolStack.isEmpty()) {
|
582 |
CADTool ct = (CADTool) cadToolStack.peek(); |
583 |
|
584 |
switch (type) {
|
585 |
case ABSOLUTE:
|
586 |
ct.transition(values[0], values[1], event); |
587 |
previousPoint = values; |
588 |
break;
|
589 |
case RELATIVE_SCU:
|
590 |
// Comprobar que tenemos almacenado el punto anterior
|
591 |
// y crear nuevo con coordenadas relativas a ?l.
|
592 |
double[] auxSCU = values; |
593 |
if (previousPoint != null) { |
594 |
auxSCU[0] = previousPoint[0] + values[0]; |
595 |
auxSCU[1] = previousPoint[1] + values[1]; |
596 |
} |
597 |
ct.transition(auxSCU[0], auxSCU[1], event); |
598 |
|
599 |
previousPoint = auxSCU; |
600 |
break;
|
601 |
case RELATIVE_SCP:
|
602 |
// TODO de momento no implementado.
|
603 |
ct.transition(values[0], values[1], event); |
604 |
previousPoint = values; |
605 |
break;
|
606 |
case POLAR_SCU:
|
607 |
// Comprobar que tenemos almacenado el punto anterior
|
608 |
// y crear nuevo con coordenadas relativas a ?l.
|
609 |
double[] auxPolarSCU = values; |
610 |
if (previousPoint != null) { |
611 |
Point2D point = UtilFunctions.getPoint(new Point2D.Double( |
612 |
previousPoint[0], previousPoint[1]), Math |
613 |
.toRadians(values[1]), values[0]); |
614 |
auxPolarSCU[0] = point.getX();
|
615 |
auxPolarSCU[1] = point.getY();
|
616 |
ct.transition(auxPolarSCU[0], auxPolarSCU[1], event); |
617 |
} else {
|
618 |
Point2D point = UtilFunctions.getPoint(new Point2D.Double( |
619 |
0, 0), Math.toRadians(values[1]), values[0]); |
620 |
auxPolarSCU[0] = point.getX();
|
621 |
auxPolarSCU[1] = point.getY();
|
622 |
ct.transition(auxPolarSCU[0], auxPolarSCU[1], event); |
623 |
} |
624 |
previousPoint = auxPolarSCU; |
625 |
break;
|
626 |
case POLAR_SCP:
|
627 |
double[] auxPolarSCP = values; |
628 |
if (previousPoint != null) { |
629 |
Point2D point = UtilFunctions.getPoint(new Point2D.Double( |
630 |
previousPoint[0], previousPoint[1]), values[1], |
631 |
values[0]);
|
632 |
auxPolarSCP[0] = point.getX();
|
633 |
auxPolarSCP[1] = point.getY();
|
634 |
ct.transition(auxPolarSCP[0], auxPolarSCP[1], event); |
635 |
} else {
|
636 |
Point2D point = UtilFunctions.getPoint(new Point2D.Double( |
637 |
0, 0), values[1], values[0]); |
638 |
auxPolarSCP[0] = point.getX();
|
639 |
auxPolarSCP[1] = point.getY();
|
640 |
ct.transition(auxPolarSCP[0], auxPolarSCP[1], event); |
641 |
} |
642 |
previousPoint = auxPolarSCP; |
643 |
break;
|
644 |
default:
|
645 |
break;
|
646 |
} |
647 |
askQuestion(); |
648 |
} |
649 |
configureMenu(); |
650 |
PluginServices.getMainFrame().enableControls(); |
651 |
} |
652 |
|
653 |
/**
|
654 |
* DOCUMENT ME!
|
655 |
*
|
656 |
* @param text
|
657 |
* DOCUMENT ME!
|
658 |
* @param source
|
659 |
* DOCUMENT ME!
|
660 |
* @param sel
|
661 |
* DOCUMENT ME!
|
662 |
* @param values
|
663 |
* DOCUMENT ME!
|
664 |
*/
|
665 |
private void transition(double value) { |
666 |
questionAsked = true;
|
667 |
if (!cadToolStack.isEmpty()) {
|
668 |
CADTool ct = (CADTool) cadToolStack.peek(); |
669 |
ct.transition(value); |
670 |
askQuestion(); |
671 |
} |
672 |
configureMenu(); |
673 |
PluginServices.getMainFrame().enableControls(); |
674 |
} |
675 |
|
676 |
public void transition(String option) { |
677 |
questionAsked = true;
|
678 |
if (!cadToolStack.isEmpty()) {
|
679 |
CADTool ct = (CADTool) cadToolStack.peek(); |
680 |
try {
|
681 |
ct.transition(option); |
682 |
} catch (Exception e) { |
683 |
View vista = (View) PluginServices.getMDIManager() |
684 |
.getActiveView(); |
685 |
vista.getConsolePanel().addText( |
686 |
"\n" + PluginServices.getText(this, "incorrect_option") |
687 |
+ " : " + option, JConsole.ERROR);
|
688 |
} |
689 |
askQuestion(); |
690 |
} |
691 |
configureMenu(); |
692 |
PluginServices.getMainFrame().enableControls(); |
693 |
} |
694 |
|
695 |
/**
|
696 |
* DOCUMENT ME!
|
697 |
*
|
698 |
* @param value
|
699 |
* DOCUMENT ME!
|
700 |
*/
|
701 |
public void setGridVisibility(boolean value) { |
702 |
getGrid().setShowGrid(value); |
703 |
getGrid().setViewPort(getMapControl().getViewPort()); |
704 |
getMapControl().repaint(); |
705 |
} |
706 |
|
707 |
public void setRefentEnabled(boolean activated) { |
708 |
bRefent = activated; |
709 |
} |
710 |
|
711 |
public boolean isRefentEnabled() |
712 |
{ |
713 |
return bRefent;
|
714 |
} |
715 |
|
716 |
/**
|
717 |
* @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#getListener()
|
718 |
*/
|
719 |
public ToolListener getListener() {
|
720 |
return new ToolListener() { |
721 |
/**
|
722 |
* @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
|
723 |
*/
|
724 |
public Cursor getCursor() { |
725 |
return null; |
726 |
} |
727 |
|
728 |
/**
|
729 |
* @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
|
730 |
*/
|
731 |
public boolean cancelDrawing() { |
732 |
return false; |
733 |
} |
734 |
}; |
735 |
} |
736 |
|
737 |
/**
|
738 |
* DOCUMENT ME!
|
739 |
*
|
740 |
* @return DOCUMENT ME!
|
741 |
*/
|
742 |
public CADTool getCadTool() {
|
743 |
return (CADTool) cadToolStack.peek();
|
744 |
} |
745 |
|
746 |
/**
|
747 |
* DOCUMENT ME!
|
748 |
*
|
749 |
* @param cadTool
|
750 |
* DOCUMENT ME!
|
751 |
*/
|
752 |
public void pushCadTool(CADTool cadTool) { |
753 |
cadToolStack.push(cadTool); |
754 |
cadTool.setCadToolAdapter(this);
|
755 |
// cadTool.initializeStatus();
|
756 |
// cadTool.setVectorialAdapter(vea);
|
757 |
/*
|
758 |
* int ret = cadTool.transition(null, editableFeatureSource, selection,
|
759 |
* new double[0]);
|
760 |
*
|
761 |
* if ((ret & Automaton.AUTOMATON_FINISHED) ==
|
762 |
* Automaton.AUTOMATON_FINISHED) { popCadTool();
|
763 |
*
|
764 |
* if (cadToolStack.isEmpty()) { pushCadTool(new
|
765 |
* com.iver.cit.gvsig.gui.cad.smc.gen.CADTool());//new
|
766 |
* SelectionCadTool());
|
767 |
* PluginServices.getMainFrame().setSelectedTool("selection"); }
|
768 |
*
|
769 |
* askQuestion();
|
770 |
*
|
771 |
* getMapControl().drawMap(false); }
|
772 |
*/
|
773 |
} |
774 |
|
775 |
/**
|
776 |
* DOCUMENT ME!
|
777 |
*/
|
778 |
public void popCadTool() { |
779 |
cadToolStack.pop(); |
780 |
} |
781 |
|
782 |
/**
|
783 |
* DOCUMENT ME!
|
784 |
*/
|
785 |
public void askQuestion() { |
786 |
CADTool cadtool = (CADTool) cadToolStack.peek(); |
787 |
/*
|
788 |
* if (cadtool..getStatus()==0){
|
789 |
* PluginServices.getMainFrame().addTextToConsole("\n"
|
790 |
* +cadtool.getName()); }
|
791 |
*/
|
792 |
if (PluginServices.getMDIManager().getActiveView() instanceof View) |
793 |
{ |
794 |
View vista = (View) PluginServices.getMDIManager().getActiveView(); |
795 |
vista.getConsolePanel().addText( |
796 |
"\n" + "#" + cadtool.getQuestion() + " > ", JConsole.MESSAGE); |
797 |
// ***PluginServices.getMainFrame().addTextToConsole("\n" +
|
798 |
// cadtool.getQuestion());
|
799 |
questionAsked = true;
|
800 |
} |
801 |
|
802 |
} |
803 |
|
804 |
/**
|
805 |
* DOCUMENT ME!
|
806 |
*
|
807 |
* @param cadTool
|
808 |
* DOCUMENT ME!
|
809 |
*/
|
810 |
public void setCadTool(CADTool cadTool) { |
811 |
cadToolStack.clear(); |
812 |
pushCadTool(cadTool); |
813 |
// askQuestion();
|
814 |
} |
815 |
|
816 |
|
817 |
/**
|
818 |
* Elimina las geometr?as seleccionadas actualmente
|
819 |
*/
|
820 |
private void delete() { |
821 |
ILayerEdited aux = CADExtension.getEditionManager().getActiveLayerEdited(); |
822 |
if (!(aux instanceof VectorialLayerEdited)) |
823 |
return;
|
824 |
VectorialLayerEdited vle = (VectorialLayerEdited) aux; |
825 |
VectorialEditableAdapter vea = vle.getVEA(); |
826 |
|
827 |
vea.startComplexRow(); |
828 |
FBitSet selection = vea.getSelection(); |
829 |
try {
|
830 |
int[] indexesToDel = new int[selection.cardinality()]; |
831 |
int j = 0; |
832 |
for (int i = selection.nextSetBit(0); i >= 0; i = selection |
833 |
.nextSetBit(i + 1)) {
|
834 |
indexesToDel[j++] = i; |
835 |
// /vea.removeRow(i);
|
836 |
} |
837 |
|
838 |
// ArrayList selectedRow = vle.getSelectedRow();
|
839 |
//
|
840 |
// int[] indexesToDel = new int[selectedRow.size()];
|
841 |
// for (int i = 0;i < selectedRow.size(); i++) {
|
842 |
// IRowEdited edRow = (IRowEdited) selectedRow.get(i);
|
843 |
// indexesToDel[i] = vea.getInversedIndex(edRow.getIndex());
|
844 |
// }
|
845 |
//
|
846 |
for (int i = indexesToDel.length - 1; i >= 0; i--) { |
847 |
vea.removeRow(indexesToDel[i], PluginServices.getText(this,
|
848 |
"deleted_feature"),EditionEvent.GRAPHIC);
|
849 |
} |
850 |
} catch (DriverIOException e) {
|
851 |
e.printStackTrace(); |
852 |
} catch (IOException e) { |
853 |
e.printStackTrace(); |
854 |
} finally {
|
855 |
try {
|
856 |
String description=PluginServices.getText(this,"remove_geometry"); |
857 |
vea.endComplexRow(description); |
858 |
} catch (IOException e1) { |
859 |
e1.printStackTrace(); |
860 |
} catch (DriverIOException e1) {
|
861 |
e1.printStackTrace(); |
862 |
} |
863 |
} |
864 |
System.out.println("clear Selection"); |
865 |
selection.clear(); |
866 |
vle.clearSelection(); |
867 |
/*
|
868 |
* if (getCadTool() instanceof SelectionCADTool) { SelectionCADTool
|
869 |
* selTool = (SelectionCADTool) getCadTool(); selTool.clearSelection(); }
|
870 |
*/
|
871 |
refreshEditedLayer(); |
872 |
} |
873 |
|
874 |
/**
|
875 |
* DOCUMENT ME!
|
876 |
*
|
877 |
* @param b
|
878 |
*/
|
879 |
public void setAdjustGrid(boolean b) { |
880 |
getGrid().setAdjustGrid(b); |
881 |
} |
882 |
|
883 |
/**
|
884 |
* DOCUMENT ME!
|
885 |
*
|
886 |
* @param actionCommand
|
887 |
*/
|
888 |
public void keyPressed(String actionCommand) { |
889 |
if (actionCommand.equals("eliminar")) { |
890 |
delete(); |
891 |
} else if (actionCommand.equals("escape")) { |
892 |
if (getMapControl().getCurrentTool().equals("cadtooladapter")) { |
893 |
CADTool ct = (CADTool) cadToolStack.peek(); |
894 |
ct.end(); |
895 |
cadToolStack.clear(); |
896 |
SelectionCADTool selCad = new SelectionCADTool();
|
897 |
selCad.init(); |
898 |
VectorialLayerEdited vle = (VectorialLayerEdited) CADExtension |
899 |
.getEditionManager().getActiveLayerEdited(); |
900 |
vle.clearSelection(); |
901 |
|
902 |
pushCadTool(selCad); |
903 |
// getVectorialAdapter().getSelection().clear();
|
904 |
|
905 |
refreshEditedLayer(); |
906 |
|
907 |
|
908 |
PluginServices.getMainFrame().setSelectedTool("_selection");
|
909 |
// askQuestion();
|
910 |
} else {
|
911 |
getMapControl().setPrevTool(); |
912 |
} |
913 |
} |
914 |
|
915 |
PluginServices.getMainFrame().enableControls(); |
916 |
|
917 |
} |
918 |
|
919 |
/**
|
920 |
* Provoca un repintado "soft" de la capa activa en edici?n.
|
921 |
* Las capas por debajo de ella no se dibujan de verdad, solo
|
922 |
* se dibuja la que est? en edici?n y las que est?n por encima
|
923 |
* de ella en el TOC.
|
924 |
*/
|
925 |
public void refreshEditedLayer() |
926 |
{ |
927 |
ILayerEdited edLayer = CADExtension.getEditionManager().getActiveLayerEdited(); |
928 |
if (edLayer != null) |
929 |
{ |
930 |
edLayer.getLayer().setDirty(true);
|
931 |
getMapControl().rePaintDirtyLayers(); |
932 |
} |
933 |
|
934 |
} |
935 |
|
936 |
public CADGrid getGrid() {
|
937 |
return cadgrid;
|
938 |
} |
939 |
|
940 |
public boolean isOrtoMode() { |
941 |
return bOrtoMode;
|
942 |
} |
943 |
|
944 |
public void setOrtoMode(boolean b) { |
945 |
bOrtoMode = b; |
946 |
} |
947 |
|
948 |
public static void addCADTool(String name, CADTool c) { |
949 |
namesCadTools.put(name, c); |
950 |
|
951 |
} |
952 |
public static CADTool[] getCADTools() { |
953 |
return (CADTool[]) CADToolAdapter.namesCadTools.values().toArray(new CADTool[0]); |
954 |
} |
955 |
public CADTool getCADTool(String text) { |
956 |
CADTool ct = (CADTool) namesCadTools.get(text); |
957 |
return ct;
|
958 |
} |
959 |
|
960 |
public EditionManager getEditionManager() {
|
961 |
return editionManager;
|
962 |
} |
963 |
|
964 |
public void initializeFlatness() { |
965 |
Preferences prefs = Preferences.userRoot().node( "gvsig.options.view" ); |
966 |
double flatness = prefs.getDouble("flatness",FConverter.flatness); |
967 |
FConverter.flatness=flatness; |
968 |
|
969 |
|
970 |
} |
971 |
public void initializeGrid(){ |
972 |
boolean showGrid = prefs.getBoolean("grid.showgrid",getGrid().isShowGrid()); |
973 |
boolean adjustGrid = prefs.getBoolean("grid.adjustgrid",getGrid().isAdjustGrid()); |
974 |
|
975 |
double dx = prefs.getDouble("grid.distancex",getGrid().getGridSizeX()); |
976 |
double dy = prefs.getDouble("grid.distancey",getGrid().getGridSizeY()); |
977 |
|
978 |
setGridVisibility(showGrid); |
979 |
setAdjustGrid(adjustGrid); |
980 |
getGrid().setGridSizeX(dx); |
981 |
getGrid().setGridSizeY(dy); |
982 |
} |
983 |
|
984 |
} |