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