Statistics
| Revision:

root / trunk / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / CADToolAdapter.java @ 6071

History | View | Annotate | Download (24.8 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.Stack;
21

    
22
import org.cresques.cts.IProjection;
23

    
24
import com.iver.andami.PluginServices;
25
import com.iver.andami.ui.mdiFrame.MainFrame;
26
import com.iver.cit.gvsig.CADExtension;
27
import com.iver.cit.gvsig.fmap.FMap;
28
import com.iver.cit.gvsig.fmap.MapControl;
29
import com.iver.cit.gvsig.fmap.ViewPort;
30
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
31
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
32
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
33
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
34
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
35
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
36
import com.iver.cit.gvsig.fmap.edition.UtilFunctions;
37
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
38
import com.iver.cit.gvsig.fmap.layers.FBitSet;
39
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
40
import com.iver.cit.gvsig.fmap.layers.SpatialCache;
41
import com.iver.cit.gvsig.fmap.tools.BehaviorException;
42
import com.iver.cit.gvsig.fmap.tools.Behavior.Behavior;
43
import com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener;
44
import com.iver.cit.gvsig.gui.View;
45
import com.iver.cit.gvsig.gui.cad.snapping.ISnapper;
46
import com.iver.cit.gvsig.gui.cad.snapping.FinalPointSnapper;
47
import com.iver.cit.gvsig.gui.cad.snapping.NearestPointSnapper;
48
import com.iver.cit.gvsig.gui.cad.snapping.SnappingVisitor;
49
import com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool;
50
import com.iver.cit.gvsig.layers.ILayerEdited;
51
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
52
import com.iver.utiles.console.JConsole;
53
import com.vividsolutions.jts.geom.Envelope;
54

    
55
public class CADToolAdapter extends Behavior {
56

    
57
        public static final int ABSOLUTE = 0;
58

    
59
        public static final int RELATIVE_SCP = 1;
60

    
61
        public static final int RELATIVE_SCU = 2;
62

    
63
        public static final int POLAR_SCP = 3;
64

    
65
        public static final int POLAR_SCU = 4;
66

    
67
        private double[] previousPoint = null;
68

    
69
        private Stack cadToolStack = new Stack();
70

    
71
        // Para pasarle las coordenadas cuando se produce un evento textEntered
72
        private int lastX;
73

    
74
        private int lastY;
75

    
76
        private FSymbol symbol = new FSymbol(FConstant.SYMBOL_TYPE_POINT, Color.RED);
77

    
78
        private Point2D mapAdjustedPoint;
79

    
80
        private ISnapper usedSnap = null;
81

    
82
        private boolean questionAsked = false;
83

    
84
        private Point2D adjustedPoint;
85

    
86
        private boolean bRefent = true;
87

    
88
        private boolean bForceCoord = false;
89

    
90
        private CADGrid cadgrid = new CADGrid();
91

    
92
        private boolean bOrtoMode;
93

    
94
        private Color theTipColor = new Color(255, 255, 155);
95

    
96
        /**
97
         * Pinta de alguna manera especial las geometrias seleccionadas para la
98
         * edici?n. En caso de que el snapping est? activado, pintar? el efecto del
99
         * mismo.
100
         *
101
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#paintComponent(java.awt.Graphics)
102
         */
103
        public void paintComponent(Graphics g) {
104
                super.paintComponent(g);
105
                drawCursor(g);
106
                getGrid().drawGrid(g);
107
                if (adjustedPoint != null) {
108
                        Point2D p = null;
109
                        if (mapAdjustedPoint != null) {
110
                                p = mapAdjustedPoint;
111
                        } else {
112
                                p = getMapControl().getViewPort().toMapPoint(adjustedPoint);
113
                        }
114
                        ((CADTool) cadToolStack.peek())
115
                                        .drawOperation(g, p.getX(), p.getY());
116
                }
117
        }
118

    
119
        /**
120
         * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
121
         */
122
        public void mouseClicked(MouseEvent e) throws BehaviorException {
123
                if (e.getButton() == MouseEvent.BUTTON3) {
124
                        CADExtension.showPopup(e);
125
                }
126
        }
127

    
128
        /**
129
         * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
130
         */
131
        public void mouseEntered(MouseEvent e) throws BehaviorException {
132
                clearMouseImage();
133
        }
134

    
135
        /**
136
         * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
137
         */
138
        public void mouseExited(MouseEvent e) throws BehaviorException {
139
        }
140

    
141
        /**
142
         * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
143
         */
144
        public void mousePressed(MouseEvent e) throws BehaviorException {
145
                if (e.getButton() == MouseEvent.BUTTON1) {
146
                        ViewPort vp = getMapControl().getMapContext().getViewPort();
147
                        Point2D p;
148

    
149
                        if (mapAdjustedPoint != null) {
150
                                p = mapAdjustedPoint;
151
                        } else {
152
                                p = vp.toMapPoint(adjustedPoint);
153
                        }
154
                        transition(new double[] { p.getX(), p.getY() }, e, ABSOLUTE);
155
                }
156
        }
157

    
158
        /**
159
         * Ajusta un punto de la imagen que se pasa como par?metro al grid si ?ste
160
         * est? activo y devuelve la distancia de un punto al punto ajustado
161
         *
162
         * @param point
163
         * @param mapHandlerAdjustedPoint
164
         *            DOCUMENT ME!
165
         *
166
         * @return Distancia del punto que se pasa como
167
         *  par?metro al punto ajustado. Si no hay ajuste,
168
         *  devuelve Double.MAX_VALUE
169
         */
170
        private double adjustToHandler(Point2D point,
171
                        Point2D mapHandlerAdjustedPoint) {
172

    
173
                if (!isRefentEnabled())
174
                        return Double.MAX_VALUE;
175

    
176
                ILayerEdited aux = CADExtension.getEditionManager().getActiveLayerEdited();
177
                if (!(aux instanceof VectorialLayerEdited))
178
                        return Double.MAX_VALUE;
179
                VectorialLayerEdited vle = (VectorialLayerEdited) aux;
180

    
181
                ArrayList snappers = vle.getSnappers();
182
                ArrayList layersToSnap = vle.getLayersToSnap();
183

    
184

    
185
                ViewPort vp = getMapControl().getViewPort();
186

    
187
                // TODO: PROVISIONAL. PONER ALGO COMO ESTO EN UN CUADRO DE DIALOGO
188
                // DE CONFIGURACI?N DEL SNAPPING
189
                FinalPointSnapper defaultSnap = new FinalPointSnapper();
190
                NearestPointSnapper nearestSnap = new NearestPointSnapper();
191
                snappers.clear();
192
                snappers.add(defaultSnap);
193
                snappers.add(nearestSnap);
194

    
195
                double mapTolerance = vp.toMapDistance(SelectionCADTool.tolerance);
196
                double minDist = mapTolerance;
197
//                double rw = getMapControl().getViewPort().toMapDistance(5);
198
                Point2D mapPoint = point;
199
                Rectangle2D r = new Rectangle2D.Double(mapPoint.getX() - mapTolerance / 2,
200
                                mapPoint.getY() - mapTolerance / 2, mapTolerance, mapTolerance);
201

    
202
                Envelope e = FConverter.convertRectangle2DtoEnvelope(r);
203

    
204
                usedSnap = null;
205
                Point2D lastPoint = null;
206
                if (previousPoint != null)
207
                {
208
                        lastPoint = new Point2D.Double(previousPoint[0], previousPoint[1]);
209
                }
210
                for (int j = 0; j < layersToSnap.size(); j++)
211
                {
212
                        FLyrVect lyrVect = (FLyrVect) layersToSnap.get(j);
213
                        SpatialCache cache = lyrVect.getSpatialCache();
214
                        if (lyrVect.isVisible())
215
                        {
216
                                // La lista de snappers est? siempre ordenada por prioridad. Los de mayor
217
                                // prioridad est?n primero.
218
                                for (int i = 0; i < snappers.size(); i++)
219
                                {
220
                                        ISnapper theSnapper = (ISnapper) snappers.get(i);
221

    
222
                                        if (usedSnap != null)
223
                                        {
224
                                                // Si ya tenemos un snap y es de alta prioridad, cogemos ese. (A no ser que en otra capa encontremos un snapper mejor)
225
                                                if (theSnapper.getPriority() < usedSnap.getPriority())
226
                                                        break;
227
                                        }
228

    
229
                                        SnappingVisitor snapVisitor = new SnappingVisitor(theSnapper, point, mapTolerance, lastPoint);
230
                                        // System.out.println("Cache size = " + cache.size());
231
                                        cache.query(e, snapVisitor);
232

    
233
                                        if (snapVisitor.getSnapPoint() != null) {
234
                                                if (minDist > snapVisitor.getMinDist())
235
                                                {
236
                                                        minDist = snapVisitor.getMinDist();
237
                                                        usedSnap = theSnapper;
238
                                                        mapHandlerAdjustedPoint.setLocation(snapVisitor.getSnapPoint());
239
                                                }
240
                                        }
241
                                }
242
                        } // visible
243
                }
244
                if (usedSnap != null)
245
                        return minDist;
246
                return Double.MAX_VALUE;
247

    
248
        }
249

    
250
        /**
251
         * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
252
         */
253
        public void mouseReleased(MouseEvent e) throws BehaviorException {
254
                getMapControl().repaint();
255
        }
256

    
257
        /**
258
         * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
259
         */
260
        public void mouseDragged(MouseEvent e) throws BehaviorException {
261
                lastX = e.getX();
262
                lastY = e.getY();
263

    
264
                calculateSnapPoint(e.getPoint());
265
        }
266

    
267
        /**
268
         * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
269
         */
270
        public void mouseMoved(MouseEvent e) throws BehaviorException {
271

    
272
                lastX = e.getX();
273
                lastY = e.getY();
274

    
275
                calculateSnapPoint(e.getPoint());
276

    
277
                showCoords(e.getPoint());
278

    
279
                getMapControl().repaint();
280
        }
281

    
282
        private void showCoords(Point2D pPix)
283
        {
284
                String[] axisText = new String[2];
285
                NumberFormat nf = NumberFormat.getInstance();
286
                MapControl mapControl = getMapControl();
287
                ViewPort vp = mapControl.getMapContext().getViewPort();
288
                IProjection iProj = vp.getProjection();
289
                if (iProj.getAbrev().equals("EPSG:4326") || iProj.getAbrev().equals("EPSG:4230")) {
290
                        axisText[0] = "Lon = ";
291
                        axisText[1] = "Lat = ";
292
                        nf.setMaximumFractionDigits(8);
293
                } else {
294
                        axisText[0] = "X = ";
295
                        axisText[1] = "Y = ";
296
                        nf.setMaximumFractionDigits(2);
297
                }
298
                Point2D p;
299
                if (mapAdjustedPoint == null)
300
                {
301
                        p = vp.toMapPoint(pPix);
302
                }
303
                else
304
                {
305
                        p = mapAdjustedPoint;
306
                }
307
                MainFrame mF = PluginServices.getMainFrame();
308

    
309
                if (mF != null)
310
                {
311
            mF.getStatusBar().setMessage("1",
312
                    FConstant.NAMES[vp.getDistanceUnits()]);
313
            mF.getStatusBar().setMessage("6", "1:"+mapControl.getMapContext().getScaleView());
314
            mF.getStatusBar().setMessage("7", iProj.getAbrev());
315

    
316
                        mF.getStatusBar().setMessage("2",
317
                                        axisText[0] + String.valueOf(nf.format(p.getX()/FMap.CHANGEM[vp.getDistanceUnits()])));
318
                        mF.getStatusBar().setMessage("3",
319
                                        axisText[1] + String.valueOf(nf.format(p.getY()/FMap.CHANGEM[vp.getDistanceUnits()])));
320
                }
321
        }
322

    
323
        private void clearMouseImage() {
324
                int[] pixels = new int[16 * 16];
325
                Image image = Toolkit.getDefaultToolkit().createImage(
326
                                new MemoryImageSource(16, 16, pixels, 0, 16));
327
                Cursor transparentCursor = Toolkit.getDefaultToolkit()
328
                                .createCustomCursor(image, new Point(0, 0), "invisiblecursor");
329

    
330
                getMapControl().setCursor(transparentCursor);
331
        }
332

    
333
        /**
334
         * DOCUMENT ME!
335
         *
336
         * @param g
337
         *            DOCUMENT ME!
338
         */
339
        private void drawCursor(Graphics g) {
340

    
341
                Point2D p = adjustedPoint;
342

    
343
                if (p == null) {
344
                        getGrid().setViewPort(getMapControl().getViewPort());
345

    
346
                        return;
347
                }
348

    
349
                int size1 = 15;
350
                int size2 = 3;
351
                g.drawLine((int) (p.getX() - size1), (int) (p.getY()),
352
                                (int) (p.getX() + size1), (int) (p.getY()));
353
                g.drawLine((int) (p.getX()), (int) (p.getY() - size1),
354
                                (int) (p.getX()), (int) (p.getY() + size1));
355

    
356
                // getMapControl().setToolTipText(null);
357
                if (adjustedPoint != null) {
358
                        if (bForceCoord) {
359
                                /* g.setColor(Color.ORANGE);
360
                                g.drawRect((int) (adjustedPoint.getX() - 6),
361
                                                (int) (adjustedPoint.getY() - 6), 12, 12);
362
                                g.drawRect((int) (adjustedPoint.getX() - 3),
363
                                                (int) (adjustedPoint.getY() - 3), 6, 6);
364
                                g.setColor(Color.MAGENTA);
365
                                g.drawRect((int) (adjustedPoint.getX() - 4),
366
                                                (int) (adjustedPoint.getY() - 4), 8, 8); */
367
                                if (usedSnap != null)
368
                                {
369
                                        usedSnap.draw(g, adjustedPoint);
370

    
371
                                        Graphics2D g2 = (Graphics2D) g;
372
                                FontMetrics metrics = g2.getFontMetrics();
373
                                int w = metrics.stringWidth(usedSnap.getToolTipText()) + 5;
374
                                int h = metrics.getMaxAscent() + 5;
375
                                int x = (int)p.getX()+9;
376
                                int y = (int)p.getY()- 7;
377

    
378
                                g2.setColor(theTipColor );
379
                                g2.fillRect(x, y-h, w, h);
380
                                g2.setColor(Color.BLACK);
381
                                g2.drawRect(x, y-h, w, h);
382
                                        g2.drawString(usedSnap.getToolTipText(), x+3, y-3);
383

    
384

    
385
                                        // getMapControl().setToolTipText(usedSnap.getToolTipText());
386
                                }
387

    
388
                                bForceCoord = false;
389
                        } else {
390
                                g.drawRect((int) (p.getX() - size2), (int) (p.getY() - size2),
391
                                                (int) (size2 * 2), (int) (size2 * 2));
392
                        }
393
                }
394
        }
395

    
396
        /**
397
         * DOCUMENT ME!
398
         *
399
         * @param point
400
         */
401
        private void calculateSnapPoint(Point point) {
402
                // Se comprueba el ajuste a rejilla
403

    
404
                Point2D gridAdjustedPoint = getMapControl().getViewPort().toMapPoint(
405
                                point);
406
                double minDistance = Double.MAX_VALUE;
407
                CADTool ct = (CADTool) cadToolStack.peek();
408
                if (ct instanceof SelectionCADTool
409
                                && ((SelectionCADTool) ct).getStatus().equals(
410
                                                "Selection.FirstPoint")) {
411
                        mapAdjustedPoint = gridAdjustedPoint;
412
                        adjustedPoint = (Point2D) point.clone();
413
                } else {
414

    
415
                        minDistance = getGrid().adjustToGrid(gridAdjustedPoint);
416
                        if (minDistance < Double.MAX_VALUE) {
417
                                adjustedPoint = getMapControl().getViewPort().fromMapPoint(
418
                                                gridAdjustedPoint);
419
                                mapAdjustedPoint = gridAdjustedPoint;
420
                        } else {
421
                                mapAdjustedPoint = null;
422
                        }
423
                }
424
                Point2D handlerAdjustedPoint = null;
425

    
426
                // Se comprueba el ajuste a los handlers
427
                if (mapAdjustedPoint != null) {
428
                        handlerAdjustedPoint = (Point2D) mapAdjustedPoint.clone(); // getMapControl().getViewPort().toMapPoint(point);
429
                } else {
430
                        handlerAdjustedPoint = getMapControl().getViewPort().toMapPoint(
431
                                        point);
432
                }
433

    
434
                Point2D mapPoint = new Point2D.Double();
435
                double distance = adjustToHandler(handlerAdjustedPoint, mapPoint);
436

    
437
                if (distance < minDistance) {
438
                        bForceCoord = true;
439
                        adjustedPoint = getMapControl().getViewPort().fromMapPoint(mapPoint);
440
                        mapAdjustedPoint = mapPoint;
441
                        minDistance = distance;
442
                }
443

    
444
                // Si no hay ajuste
445
                if (minDistance == Double.MAX_VALUE) {
446
                        adjustedPoint = point;
447
                        mapAdjustedPoint = null;
448
                }
449

    
450
        }
451

    
452
        /**
453
         * @see java.awt.event.MouseWheelListener#mouseWheelMoved(java.awt.event.MouseWheelEvent)
454
         */
455
        public void mouseWheelMoved(MouseWheelEvent e) throws BehaviorException {
456
        }
457

    
458
        /**
459
         * M?todo que realiza las transiciones en las herramientas en funci?n de un
460
         * texto introducido en la consola
461
         *
462
         * @param text
463
         *            DOCUMENT ME!
464
         */
465
        public void textEntered(String text) {
466
                if (text == null) {
467
                        transition("cancel");
468
                } else {
469
                        /*
470
                         * if ("".equals(text)) { transition("aceptar"); } else {
471
                         */
472
                        text = text.trim();
473
                        int type = ABSOLUTE;
474
                        String[] numbers = new String[1];
475
                        numbers[0] = text;
476
                        if (text.indexOf(",") != -1) {
477

    
478
                                numbers = text.split(",");
479
                                if (numbers[0].substring(0, 1).equals("@")) {
480
                                        numbers[0] = numbers[0].substring(1, numbers[0].length());
481
                                        type = RELATIVE_SCU;
482
                                        if (numbers[0].substring(0, 1).equals("*")) {
483
                                                type = RELATIVE_SCP;
484
                                                numbers[0] = numbers[0].substring(1, numbers[0]
485
                                                                .length());
486
                                        }
487
                                }
488
                        } else if (text.indexOf("<") != -1) {
489
                                type = POLAR_SCP;
490
                                numbers = text.split("<");
491
                                if (numbers[0].substring(0, 1).equals("@")) {
492
                                        numbers[0] = numbers[0].substring(1, numbers[0].length());
493
                                        type = POLAR_SCU;
494
                                        if (numbers[0].substring(0, 1).equals("*")) {
495
                                                type = POLAR_SCP;
496
                                                numbers[0] = numbers[0].substring(1, numbers[0]
497
                                                                .length());
498
                                        }
499
                                }
500
                        }
501

    
502
                        double[] values = null;
503

    
504
                        try {
505
                                if (numbers.length == 2) {
506
                                        // punto
507
                                        values = new double[] { Double.parseDouble(numbers[0]),
508
                                                        Double.parseDouble(numbers[1]) };
509
                                        transition(values, null, type);
510
                                } else if (numbers.length == 1) {
511
                                        // valor
512
                                        values = new double[] { Double.parseDouble(numbers[0]) };
513
                                        transition(values[0]);
514
                                }
515
                        } catch (NumberFormatException e) {
516
                                transition(text);
517
                        } catch (NullPointerException e) {
518
                                transition(text);
519
                        }
520
                        // }
521
                }
522
                getMapControl().repaint();
523
        }
524

    
525
        /**
526
         * DOCUMENT ME!
527
         */
528
        public void configureMenu() {
529
                String[] desc = ((CADTool) cadToolStack.peek()).getDescriptions();
530
                // String[] labels = ((CADTool)
531
                // cadToolStack.peek()).getCurrentTransitions();
532
                CADExtension.clearMenu();
533

    
534
                for (int i = 0; i < desc.length; i++) {
535
                        if (desc[i] != null) {
536
                                CADExtension
537
                                                .addMenuEntry(PluginServices.getText(this, desc[i]));// ,
538
                                // labels[i]);
539
                        }
540
                }
541

    
542
        }
543

    
544
        /**
545
         * Recibe los valores de la transici?n (normalmente un punto) y el evento
546
         * con el que se gener? (si fue de rat?n ser? MouseEvent, el que viene en el
547
         * pressed) y si es de teclado, ser? un KeyEvent. Del evento se puede sacar
548
         * informaci?n acerca de si estaba pulsada la tecla CTRL, o Alt, etc.
549
         *
550
         * @param values
551
         * @param event
552
         */
553
        private void transition(double[] values, InputEvent event, int type) {
554
                questionAsked = true;
555
                if (!cadToolStack.isEmpty()) {
556
                        CADTool ct = (CADTool) cadToolStack.peek();
557

    
558
                        switch (type) {
559
                        case ABSOLUTE:
560
                                ct.transition(values[0], values[1], event);
561
                                previousPoint = values;
562
                                break;
563
                        case RELATIVE_SCU:
564
                                // Comprobar que tenemos almacenado el punto anterior
565
                                // y crear nuevo con coordenadas relativas a ?l.
566
                                double[] auxSCU = values;
567
                                if (previousPoint != null) {
568
                                        auxSCU[0] = previousPoint[0] + values[0];
569
                                        auxSCU[1] = previousPoint[1] + values[1];
570
                                }
571
                                ct.transition(auxSCU[0], auxSCU[1], event);
572

    
573
                                previousPoint = auxSCU;
574
                                break;
575
                        case RELATIVE_SCP:
576
                                // TODO de momento no implementado.
577
                                ct.transition(values[0], values[1], event);
578
                                previousPoint = values;
579
                                break;
580
                        case POLAR_SCU:
581
                                // Comprobar que tenemos almacenado el punto anterior
582
                                // y crear nuevo con coordenadas relativas a ?l.
583
                                double[] auxPolarSCU = values;
584
                                if (previousPoint != null) {
585
                                        Point2D point = UtilFunctions.getPoint(new Point2D.Double(
586
                                                        previousPoint[0], previousPoint[1]), Math
587
                                                        .toRadians(values[1]), values[0]);
588
                                        auxPolarSCU[0] = point.getX();
589
                                        auxPolarSCU[1] = point.getY();
590
                                        ct.transition(auxPolarSCU[0], auxPolarSCU[1], event);
591
                                } else {
592
                                        Point2D point = UtilFunctions.getPoint(new Point2D.Double(
593
                                                        0, 0), Math.toRadians(values[1]), values[0]);
594
                                        auxPolarSCU[0] = point.getX();
595
                                        auxPolarSCU[1] = point.getY();
596
                                        ct.transition(auxPolarSCU[0], auxPolarSCU[1], event);
597
                                }
598
                                previousPoint = auxPolarSCU;
599
                                break;
600
                        case POLAR_SCP:
601
                                double[] auxPolarSCP = values;
602
                                if (previousPoint != null) {
603
                                        Point2D point = UtilFunctions.getPoint(new Point2D.Double(
604
                                                        previousPoint[0], previousPoint[1]), values[1],
605
                                                        values[0]);
606
                                        auxPolarSCP[0] = point.getX();
607
                                        auxPolarSCP[1] = point.getY();
608
                                        ct.transition(auxPolarSCP[0], auxPolarSCP[1], event);
609
                                } else {
610
                                        Point2D point = UtilFunctions.getPoint(new Point2D.Double(
611
                                                        0, 0), values[1], values[0]);
612
                                        auxPolarSCP[0] = point.getX();
613
                                        auxPolarSCP[1] = point.getY();
614
                                        ct.transition(auxPolarSCP[0], auxPolarSCP[1], event);
615
                                }
616
                                previousPoint = auxPolarSCP;
617
                                break;
618
                        default:
619
                                break;
620
                        }
621
                        askQuestion();
622
                }
623
                configureMenu();
624
                PluginServices.getMainFrame().enableControls();
625
        }
626

    
627
        /**
628
         * DOCUMENT ME!
629
         *
630
         * @param text
631
         *            DOCUMENT ME!
632
         * @param source
633
         *            DOCUMENT ME!
634
         * @param sel
635
         *            DOCUMENT ME!
636
         * @param values
637
         *            DOCUMENT ME!
638
         */
639
        private void transition(double value) {
640
                questionAsked = true;
641
                if (!cadToolStack.isEmpty()) {
642
                        CADTool ct = (CADTool) cadToolStack.peek();
643
                        ct.transition(value);
644
                        askQuestion();
645
                }
646
                configureMenu();
647
                PluginServices.getMainFrame().enableControls();
648
        }
649

    
650
        public void transition(String option) {
651
                questionAsked = true;
652
                if (!cadToolStack.isEmpty()) {
653
                        CADTool ct = (CADTool) cadToolStack.peek();
654
                        try {
655
                                ct.transition(option);
656
                        } catch (Exception e) {
657
                                View vista = (View) PluginServices.getMDIManager()
658
                                                .getActiveView();
659
                                vista.getConsolePanel().addText(
660
                                                "\n" + PluginServices.getText(this, "incorrect_option")
661
                                                                + " : " + option, JConsole.ERROR);
662
                        }
663
                        askQuestion();
664
                }
665
                configureMenu();
666
                PluginServices.getMainFrame().enableControls();
667
        }
668

    
669
        /**
670
         * DOCUMENT ME!
671
         *
672
         * @param value
673
         *            DOCUMENT ME!
674
         */
675
        public void setGridVisibility(boolean value) {
676
                getGrid().setUseGrid(value);
677
                getGrid().setViewPort(getMapControl().getViewPort());
678
                getMapControl().repaint();
679
        }
680

    
681
        public void setRefentEnabled(boolean activated) {
682
                bRefent = activated;
683
        }
684

    
685
        public boolean isRefentEnabled()
686
        {
687
                return bRefent;
688
        }
689

    
690
        /**
691
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#getListener()
692
         */
693
        public ToolListener getListener() {
694
                return new ToolListener() {
695
                        /**
696
                         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
697
                         */
698
                        public Cursor getCursor() {
699
                                return null;
700
                        }
701

    
702
                        /**
703
                         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
704
                         */
705
                        public boolean cancelDrawing() {
706
                                return false;
707
                        }
708
                };
709
        }
710

    
711
        /**
712
         * DOCUMENT ME!
713
         *
714
         * @return DOCUMENT ME!
715
         */
716
        public CADTool getCadTool() {
717
                return (CADTool) cadToolStack.peek();
718
        }
719

    
720
        /**
721
         * DOCUMENT ME!
722
         *
723
         * @param cadTool
724
         *            DOCUMENT ME!
725
         */
726
        public void pushCadTool(CADTool cadTool) {
727
                cadToolStack.push(cadTool);
728
                cadTool.setCadToolAdapter(this);
729
                // cadTool.initializeStatus();
730
                // cadTool.setVectorialAdapter(vea);
731
                /*
732
                 * int ret = cadTool.transition(null, editableFeatureSource, selection,
733
                 * new double[0]);
734
                 *
735
                 * if ((ret & Automaton.AUTOMATON_FINISHED) ==
736
                 * Automaton.AUTOMATON_FINISHED) { popCadTool();
737
                 *
738
                 * if (cadToolStack.isEmpty()) { pushCadTool(new
739
                 * com.iver.cit.gvsig.gui.cad.smc.gen.CADTool());//new
740
                 * SelectionCadTool());
741
                 * PluginServices.getMainFrame().setSelectedTool("selection"); }
742
                 *
743
                 * askQuestion();
744
                 *
745
                 * getMapControl().drawMap(false); }
746
                 */
747
        }
748

    
749
        /**
750
         * DOCUMENT ME!
751
         */
752
        public void popCadTool() {
753
                cadToolStack.pop();
754
        }
755

    
756
        /**
757
         * DOCUMENT ME!
758
         */
759
        public void askQuestion() {
760
                CADTool cadtool = (CADTool) cadToolStack.peek();
761
                /*
762
                 * if (cadtool..getStatus()==0){
763
                 * PluginServices.getMainFrame().addTextToConsole("\n"
764
                 * +cadtool.getName()); }
765
                 */
766
                if (PluginServices.getMDIManager().getActiveView() instanceof View)
767
                {
768
                        View vista = (View) PluginServices.getMDIManager().getActiveView();
769
                        vista.getConsolePanel().addText(
770
                                        "\n" + "#" + cadtool.getQuestion() + " > ", JConsole.MESSAGE);
771
                        // ***PluginServices.getMainFrame().addTextToConsole("\n" +
772
                        // cadtool.getQuestion());
773
                        questionAsked = true;
774
                }
775

    
776
        }
777

    
778
        /**
779
         * DOCUMENT ME!
780
         *
781
         * @param cadTool
782
         *            DOCUMENT ME!
783
         */
784
        public void setCadTool(CADTool cadTool) {
785
                cadToolStack.clear();
786
                pushCadTool(cadTool);
787
                // askQuestion();
788
        }
789

    
790

    
791
        /**
792
         * Elimina las geometr?as seleccionadas actualmente
793
         */
794
        private void delete() {
795
                ILayerEdited aux = CADExtension.getEditionManager().getActiveLayerEdited();
796
                if (!(aux instanceof VectorialLayerEdited))
797
                        return;
798
                VectorialLayerEdited vle = (VectorialLayerEdited) aux;
799
                VectorialEditableAdapter vea = vle.getVEA();
800

    
801
                vea.startComplexRow();
802
                FBitSet selection = vea.getSelection();
803
                try {
804
                        int[] indexesToDel = new int[selection.cardinality()];
805
                        int j = 0;
806
                        for (int i = selection.nextSetBit(0); i >= 0; i = selection
807
                                        .nextSetBit(i + 1)) {
808
                                indexesToDel[j++] = i;
809
                                // /vea.removeRow(i);
810
                        }
811

    
812
//                          ArrayList selectedRow = vle.getSelectedRow();
813
//
814
//                          int[] indexesToDel = new int[selectedRow.size()];
815
//                          for (int i = 0;i < selectedRow.size(); i++) {
816
//                                  IRowEdited edRow = (IRowEdited) selectedRow.get(i);
817
//                                  indexesToDel[i] = vea.getInversedIndex(edRow.getIndex());
818
//                                  }
819
//
820
                        for (int i = indexesToDel.length - 1; i >= 0; i--) {
821
                                vea.removeRow(indexesToDel[i], PluginServices.getText(this,
822
                                                "deleted_feature"),EditionEvent.GRAPHIC);
823
                        }
824
                } catch (DriverIOException e) {
825
                        e.printStackTrace();
826
                } catch (IOException e) {
827
                        e.printStackTrace();
828
                } finally {
829
                        try {
830
                                String description=PluginServices.getText(this,"remove_geometry");
831
                                vea.endComplexRow(description);
832
                        } catch (IOException e1) {
833
                                e1.printStackTrace();
834
                        } catch (DriverIOException e1) {
835
                                e1.printStackTrace();
836
                        }
837
                }
838
                System.out.println("clear Selection");
839
                selection.clear();
840
                vle.clearSelection();
841
                /*
842
                 * if (getCadTool() instanceof SelectionCADTool) { SelectionCADTool
843
                 * selTool = (SelectionCADTool) getCadTool(); selTool.clearSelection(); }
844
                 */
845
                refreshEditedLayer();
846
        }
847

    
848
        /**
849
         * DOCUMENT ME!
850
         *
851
         * @param b
852
         */
853
        public void setAdjustGrid(boolean b) {
854
                getGrid().setAdjustGrid(b);
855
        }
856

    
857
        /**
858
         * DOCUMENT ME!
859
         *
860
         * @param actionCommand
861
         */
862
        public void keyPressed(String actionCommand) {
863
                if (actionCommand.equals("eliminar")) {
864
                        delete();
865
                } else if (actionCommand.equals("escape")) {
866
                        if (getMapControl().getTool().equals("cadtooladapter")) {
867
                                CADTool ct = (CADTool) cadToolStack.peek();
868
                                ct.end();
869
                                cadToolStack.clear();
870
                                SelectionCADTool selCad = new SelectionCADTool();
871
                                selCad.init();
872
                                VectorialLayerEdited vle = (VectorialLayerEdited) CADExtension
873
                                                .getEditionManager().getActiveLayerEdited();
874
                                vle.clearSelection();
875

    
876
                                pushCadTool(selCad);
877
                                // getVectorialAdapter().getSelection().clear();
878

    
879
                                refreshEditedLayer();
880

    
881

    
882
                                PluginServices.getMainFrame().setSelectedTool("_selection");
883
                                // askQuestion();
884
                        } else {
885
                                getMapControl().setPrevTool();
886
                        }
887
                }
888

    
889
                PluginServices.getMainFrame().enableControls();
890

    
891
        }
892

    
893
        /**
894
         * Provoca un repintado "soft" de la capa activa en edici?n.
895
         * Las capas por debajo de ella no se dibujan de verdad, solo
896
         * se dibuja la que est? en edici?n y las que est?n por encima
897
         * de ella en el TOC.
898
         */
899
        public void refreshEditedLayer()
900
        {
901
                ILayerEdited edLayer = CADExtension.getEditionManager().getActiveLayerEdited();
902
                if (edLayer != null)
903
                {
904
                        edLayer.getLayer().setDirty(true);
905
                        getMapControl().rePaintDirtyLayers();
906
                }
907

    
908
        }
909

    
910
        public CADGrid getGrid() {
911
                return cadgrid;
912
        }
913

    
914
        public boolean isOrtoMode() {
915
                return bOrtoMode;
916
        }
917

    
918
        public void setOrtoMode(boolean b) {
919
                bOrtoMode = b;
920
        }
921

    
922

    
923
}