Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / CADToolAdapter.java @ 5223

History | View | Annotate | Download (21.7 KB)

1
package com.iver.cit.gvsig.gui.cad;
2

    
3
import java.awt.Color;
4
import java.awt.Cursor;
5
import java.awt.Graphics;
6
import java.awt.Image;
7
import java.awt.Point;
8
import java.awt.Toolkit;
9
import java.awt.event.InputEvent;
10
import java.awt.event.MouseEvent;
11
import java.awt.event.MouseWheelEvent;
12
import java.awt.geom.Point2D;
13
import java.awt.geom.Rectangle2D;
14
import java.awt.image.MemoryImageSource;
15
import java.io.IOException;
16
import java.util.Stack;
17

    
18
import com.iver.andami.PluginServices;
19
import com.iver.cit.gvsig.CADExtension;
20
import com.iver.cit.gvsig.EditionManager;
21
import com.iver.cit.gvsig.fmap.DriverException;
22
import com.iver.cit.gvsig.fmap.ViewPort;
23
import com.iver.cit.gvsig.fmap.core.Handler;
24
import com.iver.cit.gvsig.fmap.core.IFeature;
25
import com.iver.cit.gvsig.fmap.core.IGeometry;
26
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
27
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
28
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
29
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
30
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
31
import com.iver.cit.gvsig.fmap.edition.IRowEdited;
32
import com.iver.cit.gvsig.fmap.edition.UtilFunctions;
33
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
34
import com.iver.cit.gvsig.fmap.layers.FBitSet;
35
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
36
import com.iver.cit.gvsig.fmap.layers.SpatialCache;
37
import com.iver.cit.gvsig.fmap.tools.BehaviorException;
38
import com.iver.cit.gvsig.fmap.tools.Behavior.Behavior;
39
import com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener;
40
import com.iver.cit.gvsig.gui.View;
41
import com.iver.cit.gvsig.gui.cad.snapping.NearestPointSnapper;
42
import com.iver.cit.gvsig.gui.cad.snapping.SnappingVisitor;
43
import com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool;
44
import com.iver.cit.gvsig.layers.ILayerEdited;
45
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
46
import com.iver.utiles.console.JConsole;
47
import com.vividsolutions.jts.geom.Envelope;
48

    
49
public class CADToolAdapter extends Behavior {
50

    
51
        public static final int ABSOLUTE = 0;
52

    
53
        public static final int RELATIVE_SCP = 1;
54

    
55
        public static final int RELATIVE_SCU = 2;
56

    
57
        public static final int POLAR_SCP = 3;
58

    
59
        public static final int POLAR_SCU = 4;
60

    
61
        private double[] previousPoint = null;
62

    
63
        private Stack cadToolStack = new Stack();
64

    
65
        // Para pasarle las coordenadas cuando se produce un evento textEntered
66
        private int lastX;
67

    
68
        private int lastY;
69

    
70
        private FSymbol symbol = new FSymbol(FConstant.SYMBOL_TYPE_POINT, Color.RED);
71

    
72
        private Point2D mapAdjustedPoint;
73

    
74
        private boolean questionAsked = false;
75

    
76
        private Point2D adjustedPoint;
77

    
78
        private boolean snapping = false;
79

    
80
        private boolean adjustSnapping = false;
81

    
82
        private CADGrid cadgrid = new CADGrid();
83

    
84
        /**
85
         * Pinta de alguna manera especial las geometrias seleccionadas para la
86
         * edici?n. En caso de que el snapping est? activado, pintar? el efecto del
87
         * mismo.
88
         * 
89
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#paintComponent(java.awt.Graphics)
90
         */
91
        public void paintComponent(Graphics g) {
92
                super.paintComponent(g);
93
                drawCursor(g);
94
                getGrid().drawGrid(g);
95
                if (adjustedPoint != null) {
96
                        Point2D p = null;
97
                        if (mapAdjustedPoint != null) {
98
                                p = mapAdjustedPoint;
99
                        } else {
100
                                p = getMapControl().getViewPort().toMapPoint(adjustedPoint);
101
                        }
102
                        ((CADTool) cadToolStack.peek())
103
                                        .drawOperation(g, p.getX(), p.getY());
104
                }
105
        }
106

    
107
        /**
108
         * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
109
         */
110
        public void mouseClicked(MouseEvent e) throws BehaviorException {
111
                if (e.getButton() == MouseEvent.BUTTON3) {
112
                        CADExtension.showPopup(e);
113
                }
114
        }
115

    
116
        /**
117
         * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
118
         */
119
        public void mouseEntered(MouseEvent e) throws BehaviorException {
120
                clearMouseImage();
121
        }
122

    
123
        /**
124
         * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
125
         */
126
        public void mouseExited(MouseEvent e) throws BehaviorException {
127
        }
128

    
129
        /**
130
         * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
131
         */
132
        public void mousePressed(MouseEvent e) throws BehaviorException {
133
                if (e.getButton() == MouseEvent.BUTTON1) {
134
                        ViewPort vp = getMapControl().getMapContext().getViewPort();
135
                        Point2D p;
136

    
137
                        if (mapAdjustedPoint != null) {
138
                                p = mapAdjustedPoint;
139
                        } else {
140
                                p = vp.toMapPoint(adjustedPoint);
141
                        }
142
                        transition(new double[] { p.getX(), p.getY() }, e, ABSOLUTE);
143
                }
144
        }
145

    
146
        /**
147
         * Ajusta un punto de la imagen que se pasa como par?metro al grid si ?ste
148
         * est? activo y devuelve la distancia de un punto al punto ajustado
149
         * 
150
         * @param point
151
         * @param mapHandlerAdjustedPoint
152
         *            DOCUMENT ME!
153
         * 
154
         * @return Distancia del punto que se pasa como
155
         *  par?metro al punto ajustado. Si no hay ajuste, 
156
         *  devuelve Double.MAX_VALUE
157
         */
158
        private double adjustToHandler(Point2D point,
159
                        Point2D mapHandlerAdjustedPoint) {
160

    
161
                ILayerEdited aux = CADExtension.getEditionManager().getActiveLayerEdited();
162
                if (!(aux instanceof VectorialLayerEdited)) 
163
                        return Double.MAX_VALUE;
164
                VectorialLayerEdited vle = (VectorialLayerEdited) aux;                
165
                VectorialEditableAdapter vea = vle.getVEA();
166
                FLyrVect lyrVect = (FLyrVect) vle.getLayer();
167
                SpatialCache cache = lyrVect.getSpatialCache();
168
                ViewPort vp = getMapControl().getViewPort();
169
                if (cache == null)
170
                        return Double.MAX_VALUE;
171
                
172
                double rw = getMapControl().getViewPort().toMapDistance(5);
173
                Point2D mapPoint = point;
174
                Rectangle2D r = new Rectangle2D.Double(mapPoint.getX() - rw / 2,
175
                                mapPoint.getY() - rw / 2, rw, rw);
176

    
177
                Envelope e = FConverter.convertRectangle2DtoEnvelope(r);
178
                
179
                // TODO: Poner en VectorialLayerEdited los ISnappers que queremos
180
                // usar e iterar por ellos para obtener el mejor punto.
181
                NearestPointSnapper defaultSnap = new NearestPointSnapper();
182
                double mapTolerance = vp.toMapDistance(SelectionCADTool.tolerance);
183
                SnappingVisitor snapVisitor = new SnappingVisitor(defaultSnap, point, mapTolerance);                
184
                cache.query(e, snapVisitor);
185
                
186

    
187
                if (snapVisitor.getSnapPoint() != null) {
188
                        mapHandlerAdjustedPoint.setLocation(snapVisitor.getSnapPoint());
189
                        return snapVisitor.getMinDist();
190
                }
191

    
192
                return Double.MAX_VALUE;
193

    
194
        }
195

    
196
        /**
197
         * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
198
         */
199
        public void mouseReleased(MouseEvent e) throws BehaviorException {
200
                getMapControl().repaint();
201
        }
202

    
203
        /**
204
         * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
205
         */
206
        public void mouseDragged(MouseEvent e) throws BehaviorException {
207
                lastX = e.getX();
208
                lastY = e.getY();
209

    
210
                calculateSnapPoint(e.getPoint());
211
        }
212

    
213
        /**
214
         * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
215
         */
216
        public void mouseMoved(MouseEvent e) throws BehaviorException {
217

    
218
                lastX = e.getX();
219
                lastY = e.getY();
220

    
221
                calculateSnapPoint(e.getPoint());
222

    
223
                getMapControl().repaint();
224
        }
225

    
226
        private void clearMouseImage() {
227
                int[] pixels = new int[16 * 16];
228
                Image image = Toolkit.getDefaultToolkit().createImage(
229
                                new MemoryImageSource(16, 16, pixels, 0, 16));
230
                Cursor transparentCursor = Toolkit.getDefaultToolkit()
231
                                .createCustomCursor(image, new Point(0, 0), "invisiblecursor");
232

    
233
                getMapControl().setCursor(transparentCursor);
234
        }
235

    
236
        /**
237
         * DOCUMENT ME!
238
         * 
239
         * @param g
240
         *            DOCUMENT ME!
241
         */
242
        private void drawCursor(Graphics g) {
243

    
244
                Point2D p = adjustedPoint;
245

    
246
                if (p == null) {
247
                        getGrid().setViewPort(getMapControl().getViewPort());
248

    
249
                        return;
250
                }
251

    
252
                int size1 = 15;
253
                int size2 = 3;
254
                g.drawLine((int) (p.getX() - size1), (int) (p.getY()),
255
                                (int) (p.getX() + size1), (int) (p.getY()));
256
                g.drawLine((int) (p.getX()), (int) (p.getY() - size1),
257
                                (int) (p.getX()), (int) (p.getY() + size1));
258

    
259
                if (adjustedPoint != null) {
260
                        if (adjustSnapping) {
261
                                g.setColor(Color.ORANGE);
262
                                g.drawRect((int) (adjustedPoint.getX() - 6),
263
                                                (int) (adjustedPoint.getY() - 6), 12, 12);
264
                                g.drawRect((int) (adjustedPoint.getX() - 3),
265
                                                (int) (adjustedPoint.getY() - 3), 6, 6);
266
                                g.setColor(Color.MAGENTA);
267
                                g.drawRect((int) (adjustedPoint.getX() - 4),
268
                                                (int) (adjustedPoint.getY() - 4), 8, 8);
269

    
270
                                adjustSnapping = false;
271
                        } else {
272
                                g.drawRect((int) (p.getX() - size2), (int) (p.getY() - size2),
273
                                                (int) (size2 * 2), (int) (size2 * 2));
274
                        }
275
                }
276
        }
277

    
278
        /**
279
         * DOCUMENT ME!
280
         * 
281
         * @param point
282
         */
283
        private void calculateSnapPoint(Point point) {
284
                // Se comprueba el ajuste a rejilla
285

    
286
                Point2D gridAdjustedPoint = getMapControl().getViewPort().toMapPoint(
287
                                point);
288
                double minDistance = Double.MAX_VALUE;
289
                CADTool ct = (CADTool) cadToolStack.peek();
290
                if (ct instanceof SelectionCADTool
291
                                && ((SelectionCADTool) ct).getStatus().equals(
292
                                                "Selection.FirstPoint")) {
293
                        mapAdjustedPoint = gridAdjustedPoint;
294
                        adjustedPoint = (Point2D) point.clone();
295
                } else {
296

    
297
                        minDistance = getGrid().adjustToGrid(gridAdjustedPoint);
298
                        if (minDistance < Double.MAX_VALUE) {
299
                                adjustedPoint = getMapControl().getViewPort().fromMapPoint(
300
                                                gridAdjustedPoint);
301
                                mapAdjustedPoint = gridAdjustedPoint;
302
                        } else {
303
                                mapAdjustedPoint = null;
304
                        }
305
                }
306
                Point2D handlerAdjustedPoint = null;
307

    
308
                // Se comprueba el ajuste a los handlers
309
                if (mapAdjustedPoint != null) {
310
                        handlerAdjustedPoint = (Point2D) mapAdjustedPoint.clone(); // getMapControl().getViewPort().toMapPoint(point);
311
                } else {
312
                        handlerAdjustedPoint = getMapControl().getViewPort().toMapPoint(
313
                                        point);
314
                }
315

    
316
                Point2D mapPoint = new Point2D.Double();
317
                double distance = adjustToHandler(handlerAdjustedPoint, mapPoint);
318

    
319
                if (distance < minDistance) {
320
                        adjustSnapping = true;
321
                        adjustedPoint = getMapControl().getViewPort().fromMapPoint(mapPoint);
322
                        mapAdjustedPoint = mapPoint;
323
                        minDistance = distance;
324
                }
325

    
326
                // Si no hay ajuste
327
                if (minDistance == Double.MAX_VALUE) {
328
                        adjustedPoint = point;
329
                        mapAdjustedPoint = null;
330
                }
331

    
332
        }
333

    
334
        /**
335
         * @see java.awt.event.MouseWheelListener#mouseWheelMoved(java.awt.event.MouseWheelEvent)
336
         */
337
        public void mouseWheelMoved(MouseWheelEvent e) throws BehaviorException {
338
                getMapControl().cancelDrawing();
339
                ViewPort vp = getMapControl().getViewPort();
340
                // Point2D pReal = vp.toMapPoint(e.getPoint());
341

    
342
                Point2D pReal = new Point2D.Double(vp.getAdjustedExtent().getCenterX(),
343
                                vp.getAdjustedExtent().getCenterY());
344
                int amount = e.getWheelRotation();
345
                double nuevoX;
346
                double nuevoY;
347
                double factor;
348

    
349
                if (amount > 0) // nos acercamos
350
                {
351
                        factor = 0.9;
352
                } else // nos alejamos
353
                {
354
                        factor = 1.2;
355
                }
356
                Rectangle2D.Double r = new Rectangle2D.Double();
357
                if (vp.getExtent() != null) {
358
                        nuevoX = pReal.getX()
359
                                        - ((vp.getExtent().getWidth() * factor) / 2.0);
360
                        nuevoY = pReal.getY()
361
                                        - ((vp.getExtent().getHeight() * factor) / 2.0);
362
                        r.x = nuevoX;
363
                        r.y = nuevoY;
364
                        r.width = vp.getExtent().getWidth() * factor;
365
                        r.height = vp.getExtent().getHeight() * factor;
366

    
367
                        vp.setExtent(r);
368
                }
369
        }
370

    
371
        /**
372
         * M?todo que realiza las transiciones en las herramientas en funci?n de un
373
         * texto introducido en la consola
374
         * 
375
         * @param text
376
         *            DOCUMENT ME!
377
         */
378
        public void textEntered(String text) {
379
                if (text == null) {
380
                        transition("cancel");
381
                } else {
382
                        /*
383
                         * if ("".equals(text)) { transition("aceptar"); } else {
384
                         */
385
                        text = text.trim();
386
                        int type = ABSOLUTE;
387
                        String[] numbers = new String[1];
388
                        numbers[0] = text;
389
                        if (text.indexOf(",") != -1) {
390

    
391
                                numbers = text.split(",");
392
                                if (numbers[0].substring(0, 1).equals("@")) {
393
                                        numbers[0] = numbers[0].substring(1, numbers[0].length());
394
                                        type = RELATIVE_SCU;
395
                                        if (numbers[0].substring(0, 1).equals("*")) {
396
                                                type = RELATIVE_SCP;
397
                                                numbers[0] = numbers[0].substring(1, numbers[0]
398
                                                                .length());
399
                                        }
400
                                }
401
                        } else if (text.indexOf("<") != -1) {
402
                                type = POLAR_SCP;
403
                                numbers = text.split("<");
404
                                if (numbers[0].substring(0, 1).equals("@")) {
405
                                        numbers[0] = numbers[0].substring(1, numbers[0].length());
406
                                        type = POLAR_SCU;
407
                                        if (numbers[0].substring(0, 1).equals("*")) {
408
                                                type = POLAR_SCP;
409
                                                numbers[0] = numbers[0].substring(1, numbers[0]
410
                                                                .length());
411
                                        }
412
                                }
413
                        }
414

    
415
                        double[] values = null;
416

    
417
                        try {
418
                                if (numbers.length == 2) {
419
                                        // punto
420
                                        values = new double[] { Double.parseDouble(numbers[0]),
421
                                                        Double.parseDouble(numbers[1]) };
422
                                        transition(values, null, type);
423
                                } else if (numbers.length == 1) {
424
                                        // valor
425
                                        values = new double[] { Double.parseDouble(numbers[0]) };
426
                                        transition(values[0]);
427
                                }
428
                        } catch (NumberFormatException e) {
429
                                transition(text);
430
                        } catch (NullPointerException e) {
431
                                transition(text);
432
                        }
433
                        // }
434
                }
435
                getMapControl().repaint();
436
        }
437

    
438
        /**
439
         * DOCUMENT ME!
440
         */
441
        public void configureMenu() {
442
                String[] desc = ((CADTool) cadToolStack.peek()).getDescriptions();
443
                // String[] labels = ((CADTool)
444
                // cadToolStack.peek()).getCurrentTransitions();
445
                CADExtension.clearMenu();
446

    
447
                for (int i = 0; i < desc.length; i++) {
448
                        if (desc[i] != null) {
449
                                CADExtension
450
                                                .addMenuEntry(PluginServices.getText(this, desc[i]));// ,
451
                                // labels[i]);
452
                        }
453
                }
454

    
455
        }
456

    
457
        /**
458
         * Recibe los valores de la transici?n (normalmente un punto) y el evento
459
         * con el que se gener? (si fue de rat?n ser? MouseEvent, el que viene en el
460
         * pressed) y si es de teclado, ser? un KeyEvent. Del evento se puede sacar
461
         * informaci?n acerca de si estaba pulsada la tecla CTRL, o Alt, etc.
462
         * 
463
         * @param values
464
         * @param event
465
         */
466
        private void transition(double[] values, InputEvent event, int type) {
467
                questionAsked = true;
468
                if (!cadToolStack.isEmpty()) {
469
                        CADTool ct = (CADTool) cadToolStack.peek();
470

    
471
                        switch (type) {
472
                        case ABSOLUTE:
473
                                ct.transition(values[0], values[1], event);
474
                                previousPoint = values;
475
                                break;
476
                        case RELATIVE_SCU:
477
                                // Comprobar que tenemos almacenado el punto anterior
478
                                // y crear nuevo con coordenadas relativas a ?l.
479
                                double[] auxSCU = values;
480
                                if (previousPoint != null) {
481
                                        auxSCU[0] = previousPoint[0] + values[0];
482
                                        auxSCU[1] = previousPoint[1] + values[1];
483
                                }
484
                                ct.transition(auxSCU[0], auxSCU[1], event);
485

    
486
                                previousPoint = auxSCU;
487
                                break;
488
                        case RELATIVE_SCP:
489
                                // TODO de momento no implementado.
490
                                ct.transition(values[0], values[1], event);
491
                                previousPoint = values;
492
                                break;
493
                        case POLAR_SCU:
494
                                // Comprobar que tenemos almacenado el punto anterior
495
                                // y crear nuevo con coordenadas relativas a ?l.
496
                                double[] auxPolarSCU = values;
497
                                if (previousPoint != null) {
498
                                        Point2D point = UtilFunctions.getPoint(new Point2D.Double(
499
                                                        previousPoint[0], previousPoint[1]), Math
500
                                                        .toRadians(values[1]), values[0]);
501
                                        auxPolarSCU[0] = point.getX();
502
                                        auxPolarSCU[1] = point.getY();
503
                                        ct.transition(auxPolarSCU[0], auxPolarSCU[1], event);
504
                                } else {
505
                                        Point2D point = UtilFunctions.getPoint(new Point2D.Double(
506
                                                        0, 0), Math.toRadians(values[1]), values[0]);
507
                                        auxPolarSCU[0] = point.getX();
508
                                        auxPolarSCU[1] = point.getY();
509
                                        ct.transition(auxPolarSCU[0], auxPolarSCU[1], event);
510
                                }
511
                                previousPoint = auxPolarSCU;
512
                                break;
513
                        case POLAR_SCP:
514
                                double[] auxPolarSCP = values;
515
                                if (previousPoint != null) {
516
                                        Point2D point = UtilFunctions.getPoint(new Point2D.Double(
517
                                                        previousPoint[0], previousPoint[1]), values[1],
518
                                                        values[0]);
519
                                        auxPolarSCP[0] = point.getX();
520
                                        auxPolarSCP[1] = point.getY();
521
                                        ct.transition(auxPolarSCP[0], auxPolarSCP[1], event);
522
                                } else {
523
                                        Point2D point = UtilFunctions.getPoint(new Point2D.Double(
524
                                                        0, 0), values[1], values[0]);
525
                                        auxPolarSCP[0] = point.getX();
526
                                        auxPolarSCP[1] = point.getY();
527
                                        ct.transition(auxPolarSCP[0], auxPolarSCP[1], event);
528
                                }
529
                                previousPoint = auxPolarSCP;
530
                                break;
531
                        default:
532
                                break;
533
                        }
534
                        askQuestion();
535
                }
536
                configureMenu();
537
                PluginServices.getMainFrame().enableControls();
538
        }
539

    
540
        /**
541
         * DOCUMENT ME!
542
         * 
543
         * @param text
544
         *            DOCUMENT ME!
545
         * @param source
546
         *            DOCUMENT ME!
547
         * @param sel
548
         *            DOCUMENT ME!
549
         * @param values
550
         *            DOCUMENT ME!
551
         */
552
        private void transition(double value) {
553
                questionAsked = true;
554
                if (!cadToolStack.isEmpty()) {
555
                        CADTool ct = (CADTool) cadToolStack.peek();
556
                        ct.transition(value);
557
                        askQuestion();
558
                }
559
                configureMenu();
560
                PluginServices.getMainFrame().enableControls();
561
        }
562

    
563
        public void transition(String option) {
564
                questionAsked = true;
565
                if (!cadToolStack.isEmpty()) {
566
                        CADTool ct = (CADTool) cadToolStack.peek();
567
                        try {
568
                                ct.transition(option);
569
                        } catch (Exception e) {
570
                                View vista = (View) PluginServices.getMDIManager()
571
                                                .getActiveView();
572
                                vista.getConsolePanel().addText(
573
                                                "\n" + PluginServices.getText(this, "incorrect_option")
574
                                                                + " : " + option, JConsole.ERROR);
575
                        }
576
                        askQuestion();
577
                }
578
                configureMenu();
579
                PluginServices.getMainFrame().enableControls();
580
        }
581

    
582
        /**
583
         * DOCUMENT ME!
584
         * 
585
         * @param value
586
         *            DOCUMENT ME!
587
         */
588
        public void setGrid(boolean value) {
589
                getGrid().setUseGrid(value);
590
                getGrid().setViewPort(getMapControl().getViewPort());
591
                getMapControl().drawMap(false);
592
        }
593

    
594
        /**
595
         * DOCUMENT ME!
596
         * 
597
         * @param activated
598
         *            DOCUMENT ME!
599
         */
600
        public void setSnapping(boolean activated) {
601
                snapping = activated;
602
        }
603

    
604
        /**
605
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#getListener()
606
         */
607
        public ToolListener getListener() {
608
                return new ToolListener() {
609
                        /**
610
                         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
611
                         */
612
                        public Cursor getCursor() {
613
                                return null;
614
                        }
615

    
616
                        /**
617
                         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
618
                         */
619
                        public boolean cancelDrawing() {
620
                                return false;
621
                        }
622
                };
623
        }
624

    
625
        /**
626
         * DOCUMENT ME!
627
         * 
628
         * @return DOCUMENT ME!
629
         */
630
        public CADTool getCadTool() {
631
                return (CADTool) cadToolStack.peek();
632
        }
633

    
634
        /**
635
         * DOCUMENT ME!
636
         * 
637
         * @param cadTool
638
         *            DOCUMENT ME!
639
         */
640
        public void pushCadTool(CADTool cadTool) {
641
                cadToolStack.push(cadTool);
642
                cadTool.setCadToolAdapter(this);
643
                // cadTool.initializeStatus();
644
                // cadTool.setVectorialAdapter(vea);
645
                /*
646
                 * int ret = cadTool.transition(null, editableFeatureSource, selection,
647
                 * new double[0]);
648
                 * 
649
                 * if ((ret & Automaton.AUTOMATON_FINISHED) ==
650
                 * Automaton.AUTOMATON_FINISHED) { popCadTool();
651
                 * 
652
                 * if (cadToolStack.isEmpty()) { pushCadTool(new
653
                 * com.iver.cit.gvsig.gui.cad.smc.gen.CADTool());//new
654
                 * SelectionCadTool());
655
                 * PluginServices.getMainFrame().setSelectedTool("selection"); }
656
                 * 
657
                 * askQuestion();
658
                 * 
659
                 * getMapControl().drawMap(false); }
660
                 */
661
        }
662

    
663
        /**
664
         * DOCUMENT ME!
665
         */
666
        public void popCadTool() {
667
                cadToolStack.pop();
668
        }
669

    
670
        /**
671
         * DOCUMENT ME!
672
         */
673
        public void askQuestion() {
674
                CADTool cadtool = (CADTool) cadToolStack.peek();
675
                /*
676
                 * if (cadtool..getStatus()==0){
677
                 * PluginServices.getMainFrame().addTextToConsole("\n"
678
                 * +cadtool.getName()); }
679
                 */
680
                View vista = (View) PluginServices.getMDIManager().getActiveView();
681
                vista.getConsolePanel().addText(
682
                                "\n" + "#" + cadtool.getQuestion() + " > ", JConsole.MESSAGE);
683
                // ***PluginServices.getMainFrame().addTextToConsole("\n" +
684
                // cadtool.getQuestion());
685
                questionAsked = true;
686

    
687
        }
688

    
689
        /**
690
         * DOCUMENT ME!
691
         * 
692
         * @param cadTool
693
         *            DOCUMENT ME!
694
         */
695
        public void setCadTool(CADTool cadTool) {
696
                cadToolStack.clear();
697
                pushCadTool(cadTool);
698
                // askQuestion();
699
        }
700

    
701

    
702
        /**
703
         * Elimina las geometr?as seleccionadas actualmente
704
         */
705
        private void delete() {
706
                ILayerEdited aux = CADExtension.getEditionManager().getActiveLayerEdited();
707
                if (!(aux instanceof VectorialLayerEdited)) 
708
                        return;
709
                VectorialLayerEdited vle = (VectorialLayerEdited) aux;                
710
                VectorialEditableAdapter vea = vle.getVEA();
711
                FLyrVect lyrVect = (FLyrVect) vle.getLayer();
712

    
713
                vea.startComplexRow();
714
                FBitSet selection = vea.getSelection();
715
                try {
716
                        int[] indexesToDel = new int[selection.cardinality()];
717
                        int j = 0;
718
                        for (int i = selection.nextSetBit(0); i >= 0; i = selection
719
                                        .nextSetBit(i + 1)) {
720
                                indexesToDel[j++] = i;
721
                                // /vea.removeRow(i);
722
                        }
723
                        /*
724
                         * VectorialLayerEdited vle = (VectorialLayerEdited) CADExtension
725
                         * .getEditionManager().getActiveLayerEdited(); ArrayList
726
                         * selectedRow = vle.getSelectedRow();
727
                         * 
728
                         * int[] indexesToDel = new int[selectedRow.size()]; for (int i = 0;
729
                         * i < selectedRow.size(); i++) { IRowEdited edRow = (IRowEdited)
730
                         * selectedRow.get(i); indexesToDel[i] = edRow.getIndex(); }
731
                         */
732
                        for (int i = indexesToDel.length - 1; i >= 0; i--) {
733
                                vea.removeRow(indexesToDel[i], PluginServices.getText(this,
734
                                                "deleted_feature"),EditionEvent.GRAPHIC);
735
                        }
736
                } catch (DriverIOException e) {
737
                        e.printStackTrace();
738
                } catch (IOException e) {
739
                        e.printStackTrace();
740
                } finally {
741
                        try {
742
                                vea.endComplexRow();
743
                        } catch (IOException e1) {
744
                                e1.printStackTrace();
745
                        } catch (DriverIOException e1) {
746
                                e1.printStackTrace();
747
                        }
748
                }
749
                System.out.println("clear Selection");
750
                selection.clear();
751
                vle.clearSelection();
752
                /*
753
                 * if (getCadTool() instanceof SelectionCADTool) { SelectionCADTool
754
                 * selTool = (SelectionCADTool) getCadTool(); selTool.clearSelection(); }
755
                 */
756
                getMapControl().drawMap(false);
757
        }
758

    
759
        /**
760
         * DOCUMENT ME!
761
         * 
762
         * @param b
763
         */
764
        public void setAdjustGrid(boolean b) {
765
                getGrid().setAdjustGrid(b);
766
        }
767

    
768
        /**
769
         * DOCUMENT ME!
770
         * 
771
         * @param actionCommand
772
         */
773
        public void keyPressed(String actionCommand) {
774
                if (actionCommand.equals("eliminar")) {
775
                        delete();
776
                } else if (actionCommand.equals("escape")) {
777
                        if (getMapControl().getTool().equals("cadtooladapter")) {
778
                                CADTool ct = (CADTool) cadToolStack.peek();
779
                                ct.end();
780
                                cadToolStack.clear();
781
                                SelectionCADTool selCad = new SelectionCADTool();
782
                                selCad.init();
783
                                VectorialLayerEdited vle = (VectorialLayerEdited) CADExtension
784
                                                .getEditionManager().getActiveLayerEdited();
785
                                vle.clearSelection();
786

    
787
                                pushCadTool(selCad);
788
                                // getVectorialAdapter().getSelection().clear();
789
                                getMapControl().drawMap(false);
790
                                PluginServices.getMainFrame().setSelectedTool("_selection");
791
                                // askQuestion();
792
                        } else {
793
                                getMapControl().setPrevTool();
794
                        }
795
                }
796

    
797
                PluginServices.getMainFrame().enableControls();
798

    
799
        }
800

    
801
        public CADGrid getGrid() {
802
                return cadgrid;
803
        }
804

    
805

    
806
}