Statistics
| Revision:

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

History | View | Annotate | Download (22.2 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.ArrayList;
17
import java.util.Stack;
18

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

    
45
public class CADToolAdapter extends Behavior {
46

    
47
        public static final int ABSOLUTE = 0;
48

    
49
        public static final int RELATIVE_SCP = 1;
50

    
51
        public static final int RELATIVE_SCU = 2;
52

    
53
        public static final int POLAR_SCP = 3;
54

    
55
        public static final int POLAR_SCU = 4;
56

    
57
        private double[] previousPoint = null;
58

    
59
        private Stack cadToolStack = new Stack();
60

    
61
        // Para pasarle las coordenadas cuando se produce un evento textEntered
62
        private int lastX;
63

    
64
        private int lastY;
65

    
66
        private FSymbol symbol = new FSymbol(FConstant.SYMBOL_TYPE_POINT, Color.RED);
67

    
68
        private Point2D mapAdjustedPoint;
69

    
70
        private ISnapper usedSnap = null;
71

    
72
        private boolean questionAsked = false;
73

    
74
        private Point2D adjustedPoint;
75

    
76
        private boolean snapping = false;
77

    
78
        private boolean adjustSnapping = false;
79

    
80
        private CADGrid cadgrid = new CADGrid();
81

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

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

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

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

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

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

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

    
159
                ILayerEdited aux = CADExtension.getEditionManager().getActiveLayerEdited();
160
                if (!(aux instanceof VectorialLayerEdited))
161
                        return Double.MAX_VALUE;
162
                VectorialLayerEdited vle = (VectorialLayerEdited) aux;
163
                FLyrVect lyrVect = (FLyrVect) vle.getLayer();
164
                ArrayList snappers = vle.getSnappers();
165

    
166
                SpatialCache cache = lyrVect.getSpatialCache();
167
                ViewPort vp = getMapControl().getViewPort();
168
                if (cache == null)
169
                        return Double.MAX_VALUE;
170

    
171
                double rw = getMapControl().getViewPort().toMapDistance(5);
172
                Point2D mapPoint = point;
173
                Rectangle2D r = new Rectangle2D.Double(mapPoint.getX() - rw / 2,
174
                                mapPoint.getY() - rw / 2, rw, rw);
175

    
176
                Envelope e = FConverter.convertRectangle2DtoEnvelope(r);
177

    
178
                // TODO: PROVISIONAL. PONER ALGO COMO ESTO EN UN CUADRO DE DIALOGO
179
                // DE CONFIGURACI?N DEL SNAPPING
180
                NearestPointSnapper defaultSnap = new NearestPointSnapper();
181
                snappers.clear();
182
                snappers.add(defaultSnap);
183

    
184
                double mapTolerance = vp.toMapDistance(SelectionCADTool.tolerance);
185
                double minDist = mapTolerance;
186
                usedSnap = null;
187
                Point2D lastPoint = null;
188
                if (previousPoint != null)
189
                {
190
                        lastPoint = new Point2D.Double(previousPoint[0], previousPoint[1]);
191
                }
192
                for (int i = 0; i < snappers.size(); i++)
193
                {
194
                        ISnapper theSnapper = (ISnapper) snappers.get(i);
195

    
196
                        SnappingVisitor snapVisitor = new SnappingVisitor(theSnapper, point, mapTolerance, lastPoint);
197
                        // System.out.println("Cache size = " + cache.size());
198
                        cache.query(e, snapVisitor);
199

    
200
                        if (snapVisitor.getSnapPoint() != null) {
201
                                if (minDist > snapVisitor.getMinDist())
202
                                {
203
                                        minDist = snapVisitor.getMinDist();
204
                                        usedSnap = theSnapper;
205
                                        mapHandlerAdjustedPoint.setLocation(snapVisitor.getSnapPoint());
206
                                }
207
                        }
208
                }
209
                if (usedSnap != null)
210
                        return minDist;
211
                return Double.MAX_VALUE;
212

    
213
        }
214

    
215
        /**
216
         * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
217
         */
218
        public void mouseReleased(MouseEvent e) throws BehaviorException {
219
                getMapControl().repaint();
220
        }
221

    
222
        /**
223
         * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
224
         */
225
        public void mouseDragged(MouseEvent e) throws BehaviorException {
226
                lastX = e.getX();
227
                lastY = e.getY();
228

    
229
                calculateSnapPoint(e.getPoint());
230
        }
231

    
232
        /**
233
         * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
234
         */
235
        public void mouseMoved(MouseEvent e) throws BehaviorException {
236

    
237
                lastX = e.getX();
238
                lastY = e.getY();
239

    
240
                calculateSnapPoint(e.getPoint());
241

    
242
                getMapControl().repaint();
243
        }
244

    
245
        private void clearMouseImage() {
246
                int[] pixels = new int[16 * 16];
247
                Image image = Toolkit.getDefaultToolkit().createImage(
248
                                new MemoryImageSource(16, 16, pixels, 0, 16));
249
                Cursor transparentCursor = Toolkit.getDefaultToolkit()
250
                                .createCustomCursor(image, new Point(0, 0), "invisiblecursor");
251

    
252
                getMapControl().setCursor(transparentCursor);
253
        }
254

    
255
        /**
256
         * DOCUMENT ME!
257
         *
258
         * @param g
259
         *            DOCUMENT ME!
260
         */
261
        private void drawCursor(Graphics g) {
262

    
263
                Point2D p = adjustedPoint;
264

    
265
                if (p == null) {
266
                        getGrid().setViewPort(getMapControl().getViewPort());
267

    
268
                        return;
269
                }
270

    
271
                int size1 = 15;
272
                int size2 = 3;
273
                g.drawLine((int) (p.getX() - size1), (int) (p.getY()),
274
                                (int) (p.getX() + size1), (int) (p.getY()));
275
                g.drawLine((int) (p.getX()), (int) (p.getY() - size1),
276
                                (int) (p.getX()), (int) (p.getY() + size1));
277

    
278
                getMapControl().setToolTipText(null);
279
                if (adjustedPoint != null) {
280
                        if (adjustSnapping) {
281
                                /* g.setColor(Color.ORANGE);
282
                                g.drawRect((int) (adjustedPoint.getX() - 6),
283
                                                (int) (adjustedPoint.getY() - 6), 12, 12);
284
                                g.drawRect((int) (adjustedPoint.getX() - 3),
285
                                                (int) (adjustedPoint.getY() - 3), 6, 6);
286
                                g.setColor(Color.MAGENTA);
287
                                g.drawRect((int) (adjustedPoint.getX() - 4),
288
                                                (int) (adjustedPoint.getY() - 4), 8, 8); */
289
                                if (usedSnap != null)
290
                                {
291
                                        usedSnap.draw(g, adjustedPoint);
292
                                        getMapControl().setToolTipText(usedSnap.getToolTipText());
293
                                }
294

    
295
                                adjustSnapping = false;
296
                        } else {
297
                                g.drawRect((int) (p.getX() - size2), (int) (p.getY() - size2),
298
                                                (int) (size2 * 2), (int) (size2 * 2));
299
                        }
300
                }
301
        }
302

    
303
        /**
304
         * DOCUMENT ME!
305
         *
306
         * @param point
307
         */
308
        private void calculateSnapPoint(Point point) {
309
                // Se comprueba el ajuste a rejilla
310

    
311
                Point2D gridAdjustedPoint = getMapControl().getViewPort().toMapPoint(
312
                                point);
313
                double minDistance = Double.MAX_VALUE;
314
                CADTool ct = (CADTool) cadToolStack.peek();
315
                if (ct instanceof SelectionCADTool
316
                                && ((SelectionCADTool) ct).getStatus().equals(
317
                                                "Selection.FirstPoint")) {
318
                        mapAdjustedPoint = gridAdjustedPoint;
319
                        adjustedPoint = (Point2D) point.clone();
320
                } else {
321

    
322
                        minDistance = getGrid().adjustToGrid(gridAdjustedPoint);
323
                        if (minDistance < Double.MAX_VALUE) {
324
                                adjustedPoint = getMapControl().getViewPort().fromMapPoint(
325
                                                gridAdjustedPoint);
326
                                mapAdjustedPoint = gridAdjustedPoint;
327
                        } else {
328
                                mapAdjustedPoint = null;
329
                        }
330
                }
331
                Point2D handlerAdjustedPoint = null;
332

    
333
                // Se comprueba el ajuste a los handlers
334
                if (mapAdjustedPoint != null) {
335
                        handlerAdjustedPoint = (Point2D) mapAdjustedPoint.clone(); // getMapControl().getViewPort().toMapPoint(point);
336
                } else {
337
                        handlerAdjustedPoint = getMapControl().getViewPort().toMapPoint(
338
                                        point);
339
                }
340

    
341
                Point2D mapPoint = new Point2D.Double();
342
                double distance = adjustToHandler(handlerAdjustedPoint, mapPoint);
343

    
344
                if (distance < minDistance) {
345
                        adjustSnapping = true;
346
                        adjustedPoint = getMapControl().getViewPort().fromMapPoint(mapPoint);
347
                        mapAdjustedPoint = mapPoint;
348
                        minDistance = distance;
349
                }
350

    
351
                // Si no hay ajuste
352
                if (minDistance == Double.MAX_VALUE) {
353
                        adjustedPoint = point;
354
                        mapAdjustedPoint = null;
355
                }
356

    
357
        }
358

    
359
        /**
360
         * @see java.awt.event.MouseWheelListener#mouseWheelMoved(java.awt.event.MouseWheelEvent)
361
         */
362
        public void mouseWheelMoved(MouseWheelEvent e) throws BehaviorException {
363
                getMapControl().cancelDrawing();
364
                ViewPort vp = getMapControl().getViewPort();
365
                // Point2D pReal = vp.toMapPoint(e.getPoint());
366

    
367
                Point2D pReal = new Point2D.Double(vp.getAdjustedExtent().getCenterX(),
368
                                vp.getAdjustedExtent().getCenterY());
369
                int amount = e.getWheelRotation();
370
                double nuevoX;
371
                double nuevoY;
372
                double factor;
373

    
374
                if (amount > 0) // nos acercamos
375
                {
376
                        factor = 0.9;
377
                } else // nos alejamos
378
                {
379
                        factor = 1.2;
380
                }
381
                Rectangle2D.Double r = new Rectangle2D.Double();
382
                if (vp.getExtent() != null) {
383
                        nuevoX = pReal.getX()
384
                                        - ((vp.getExtent().getWidth() * factor) / 2.0);
385
                        nuevoY = pReal.getY()
386
                                        - ((vp.getExtent().getHeight() * factor) / 2.0);
387
                        r.x = nuevoX;
388
                        r.y = nuevoY;
389
                        r.width = vp.getExtent().getWidth() * factor;
390
                        r.height = vp.getExtent().getHeight() * factor;
391

    
392
                        vp.setExtent(r);
393
                }
394
        }
395

    
396
        /**
397
         * M?todo que realiza las transiciones en las herramientas en funci?n de un
398
         * texto introducido en la consola
399
         *
400
         * @param text
401
         *            DOCUMENT ME!
402
         */
403
        public void textEntered(String text) {
404
                if (text == null) {
405
                        transition("cancel");
406
                } else {
407
                        /*
408
                         * if ("".equals(text)) { transition("aceptar"); } else {
409
                         */
410
                        text = text.trim();
411
                        int type = ABSOLUTE;
412
                        String[] numbers = new String[1];
413
                        numbers[0] = text;
414
                        if (text.indexOf(",") != -1) {
415

    
416
                                numbers = text.split(",");
417
                                if (numbers[0].substring(0, 1).equals("@")) {
418
                                        numbers[0] = numbers[0].substring(1, numbers[0].length());
419
                                        type = RELATIVE_SCU;
420
                                        if (numbers[0].substring(0, 1).equals("*")) {
421
                                                type = RELATIVE_SCP;
422
                                                numbers[0] = numbers[0].substring(1, numbers[0]
423
                                                                .length());
424
                                        }
425
                                }
426
                        } else if (text.indexOf("<") != -1) {
427
                                type = POLAR_SCP;
428
                                numbers = text.split("<");
429
                                if (numbers[0].substring(0, 1).equals("@")) {
430
                                        numbers[0] = numbers[0].substring(1, numbers[0].length());
431
                                        type = POLAR_SCU;
432
                                        if (numbers[0].substring(0, 1).equals("*")) {
433
                                                type = POLAR_SCP;
434
                                                numbers[0] = numbers[0].substring(1, numbers[0]
435
                                                                .length());
436
                                        }
437
                                }
438
                        }
439

    
440
                        double[] values = null;
441

    
442
                        try {
443
                                if (numbers.length == 2) {
444
                                        // punto
445
                                        values = new double[] { Double.parseDouble(numbers[0]),
446
                                                        Double.parseDouble(numbers[1]) };
447
                                        transition(values, null, type);
448
                                } else if (numbers.length == 1) {
449
                                        // valor
450
                                        values = new double[] { Double.parseDouble(numbers[0]) };
451
                                        transition(values[0]);
452
                                }
453
                        } catch (NumberFormatException e) {
454
                                transition(text);
455
                        } catch (NullPointerException e) {
456
                                transition(text);
457
                        }
458
                        // }
459
                }
460
                getMapControl().repaint();
461
        }
462

    
463
        /**
464
         * DOCUMENT ME!
465
         */
466
        public void configureMenu() {
467
                String[] desc = ((CADTool) cadToolStack.peek()).getDescriptions();
468
                // String[] labels = ((CADTool)
469
                // cadToolStack.peek()).getCurrentTransitions();
470
                CADExtension.clearMenu();
471

    
472
                for (int i = 0; i < desc.length; i++) {
473
                        if (desc[i] != null) {
474
                                CADExtension
475
                                                .addMenuEntry(PluginServices.getText(this, desc[i]));// ,
476
                                // labels[i]);
477
                        }
478
                }
479

    
480
        }
481

    
482
        /**
483
         * Recibe los valores de la transici?n (normalmente un punto) y el evento
484
         * con el que se gener? (si fue de rat?n ser? MouseEvent, el que viene en el
485
         * pressed) y si es de teclado, ser? un KeyEvent. Del evento se puede sacar
486
         * informaci?n acerca de si estaba pulsada la tecla CTRL, o Alt, etc.
487
         *
488
         * @param values
489
         * @param event
490
         */
491
        private void transition(double[] values, InputEvent event, int type) {
492
                questionAsked = true;
493
                if (!cadToolStack.isEmpty()) {
494
                        CADTool ct = (CADTool) cadToolStack.peek();
495

    
496
                        switch (type) {
497
                        case ABSOLUTE:
498
                                ct.transition(values[0], values[1], event);
499
                                previousPoint = values;
500
                                break;
501
                        case RELATIVE_SCU:
502
                                // Comprobar que tenemos almacenado el punto anterior
503
                                // y crear nuevo con coordenadas relativas a ?l.
504
                                double[] auxSCU = values;
505
                                if (previousPoint != null) {
506
                                        auxSCU[0] = previousPoint[0] + values[0];
507
                                        auxSCU[1] = previousPoint[1] + values[1];
508
                                }
509
                                ct.transition(auxSCU[0], auxSCU[1], event);
510

    
511
                                previousPoint = auxSCU;
512
                                break;
513
                        case RELATIVE_SCP:
514
                                // TODO de momento no implementado.
515
                                ct.transition(values[0], values[1], event);
516
                                previousPoint = values;
517
                                break;
518
                        case POLAR_SCU:
519
                                // Comprobar que tenemos almacenado el punto anterior
520
                                // y crear nuevo con coordenadas relativas a ?l.
521
                                double[] auxPolarSCU = values;
522
                                if (previousPoint != null) {
523
                                        Point2D point = UtilFunctions.getPoint(new Point2D.Double(
524
                                                        previousPoint[0], previousPoint[1]), Math
525
                                                        .toRadians(values[1]), values[0]);
526
                                        auxPolarSCU[0] = point.getX();
527
                                        auxPolarSCU[1] = point.getY();
528
                                        ct.transition(auxPolarSCU[0], auxPolarSCU[1], event);
529
                                } else {
530
                                        Point2D point = UtilFunctions.getPoint(new Point2D.Double(
531
                                                        0, 0), Math.toRadians(values[1]), values[0]);
532
                                        auxPolarSCU[0] = point.getX();
533
                                        auxPolarSCU[1] = point.getY();
534
                                        ct.transition(auxPolarSCU[0], auxPolarSCU[1], event);
535
                                }
536
                                previousPoint = auxPolarSCU;
537
                                break;
538
                        case POLAR_SCP:
539
                                double[] auxPolarSCP = values;
540
                                if (previousPoint != null) {
541
                                        Point2D point = UtilFunctions.getPoint(new Point2D.Double(
542
                                                        previousPoint[0], previousPoint[1]), values[1],
543
                                                        values[0]);
544
                                        auxPolarSCP[0] = point.getX();
545
                                        auxPolarSCP[1] = point.getY();
546
                                        ct.transition(auxPolarSCP[0], auxPolarSCP[1], event);
547
                                } else {
548
                                        Point2D point = UtilFunctions.getPoint(new Point2D.Double(
549
                                                        0, 0), values[1], values[0]);
550
                                        auxPolarSCP[0] = point.getX();
551
                                        auxPolarSCP[1] = point.getY();
552
                                        ct.transition(auxPolarSCP[0], auxPolarSCP[1], event);
553
                                }
554
                                previousPoint = auxPolarSCP;
555
                                break;
556
                        default:
557
                                break;
558
                        }
559
                        askQuestion();
560
                }
561
                configureMenu();
562
                PluginServices.getMainFrame().enableControls();
563
        }
564

    
565
        /**
566
         * DOCUMENT ME!
567
         *
568
         * @param text
569
         *            DOCUMENT ME!
570
         * @param source
571
         *            DOCUMENT ME!
572
         * @param sel
573
         *            DOCUMENT ME!
574
         * @param values
575
         *            DOCUMENT ME!
576
         */
577
        private void transition(double value) {
578
                questionAsked = true;
579
                if (!cadToolStack.isEmpty()) {
580
                        CADTool ct = (CADTool) cadToolStack.peek();
581
                        ct.transition(value);
582
                        askQuestion();
583
                }
584
                configureMenu();
585
                PluginServices.getMainFrame().enableControls();
586
        }
587

    
588
        public void transition(String option) {
589
                questionAsked = true;
590
                if (!cadToolStack.isEmpty()) {
591
                        CADTool ct = (CADTool) cadToolStack.peek();
592
                        try {
593
                                ct.transition(option);
594
                        } catch (Exception e) {
595
                                View vista = (View) PluginServices.getMDIManager()
596
                                                .getActiveView();
597
                                vista.getConsolePanel().addText(
598
                                                "\n" + PluginServices.getText(this, "incorrect_option")
599
                                                                + " : " + option, JConsole.ERROR);
600
                        }
601
                        askQuestion();
602
                }
603
                configureMenu();
604
                PluginServices.getMainFrame().enableControls();
605
        }
606

    
607
        /**
608
         * DOCUMENT ME!
609
         *
610
         * @param value
611
         *            DOCUMENT ME!
612
         */
613
        public void setGrid(boolean value) {
614
                getGrid().setUseGrid(value);
615
                getGrid().setViewPort(getMapControl().getViewPort());
616
                getMapControl().drawMap(false);
617
        }
618

    
619
        /**
620
         * DOCUMENT ME!
621
         *
622
         * @param activated
623
         *            DOCUMENT ME!
624
         */
625
        public void setSnapping(boolean activated) {
626
                snapping = activated;
627
        }
628

    
629
        /**
630
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#getListener()
631
         */
632
        public ToolListener getListener() {
633
                return new ToolListener() {
634
                        /**
635
                         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
636
                         */
637
                        public Cursor getCursor() {
638
                                return null;
639
                        }
640

    
641
                        /**
642
                         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
643
                         */
644
                        public boolean cancelDrawing() {
645
                                return false;
646
                        }
647
                };
648
        }
649

    
650
        /**
651
         * DOCUMENT ME!
652
         *
653
         * @return DOCUMENT ME!
654
         */
655
        public CADTool getCadTool() {
656
                return (CADTool) cadToolStack.peek();
657
        }
658

    
659
        /**
660
         * DOCUMENT ME!
661
         *
662
         * @param cadTool
663
         *            DOCUMENT ME!
664
         */
665
        public void pushCadTool(CADTool cadTool) {
666
                cadToolStack.push(cadTool);
667
                cadTool.setCadToolAdapter(this);
668
                // cadTool.initializeStatus();
669
                // cadTool.setVectorialAdapter(vea);
670
                /*
671
                 * int ret = cadTool.transition(null, editableFeatureSource, selection,
672
                 * new double[0]);
673
                 *
674
                 * if ((ret & Automaton.AUTOMATON_FINISHED) ==
675
                 * Automaton.AUTOMATON_FINISHED) { popCadTool();
676
                 *
677
                 * if (cadToolStack.isEmpty()) { pushCadTool(new
678
                 * com.iver.cit.gvsig.gui.cad.smc.gen.CADTool());//new
679
                 * SelectionCadTool());
680
                 * PluginServices.getMainFrame().setSelectedTool("selection"); }
681
                 *
682
                 * askQuestion();
683
                 *
684
                 * getMapControl().drawMap(false); }
685
                 */
686
        }
687

    
688
        /**
689
         * DOCUMENT ME!
690
         */
691
        public void popCadTool() {
692
                cadToolStack.pop();
693
        }
694

    
695
        /**
696
         * DOCUMENT ME!
697
         */
698
        public void askQuestion() {
699
                CADTool cadtool = (CADTool) cadToolStack.peek();
700
                /*
701
                 * if (cadtool..getStatus()==0){
702
                 * PluginServices.getMainFrame().addTextToConsole("\n"
703
                 * +cadtool.getName()); }
704
                 */
705
                View vista = (View) PluginServices.getMDIManager().getActiveView();
706
                vista.getConsolePanel().addText(
707
                                "\n" + "#" + cadtool.getQuestion() + " > ", JConsole.MESSAGE);
708
                // ***PluginServices.getMainFrame().addTextToConsole("\n" +
709
                // cadtool.getQuestion());
710
                questionAsked = true;
711

    
712
        }
713

    
714
        /**
715
         * DOCUMENT ME!
716
         *
717
         * @param cadTool
718
         *            DOCUMENT ME!
719
         */
720
        public void setCadTool(CADTool cadTool) {
721
                cadToolStack.clear();
722
                pushCadTool(cadTool);
723
                // askQuestion();
724
        }
725

    
726

    
727
        /**
728
         * Elimina las geometr?as seleccionadas actualmente
729
         */
730
        private void delete() {
731
                ILayerEdited aux = CADExtension.getEditionManager().getActiveLayerEdited();
732
                if (!(aux instanceof VectorialLayerEdited))
733
                        return;
734
                VectorialLayerEdited vle = (VectorialLayerEdited) aux;
735
                VectorialEditableAdapter vea = vle.getVEA();
736

    
737
                vea.startComplexRow();
738
                FBitSet selection = vea.getSelection();
739
                try {
740
                        int[] indexesToDel = new int[selection.cardinality()];
741
                        int j = 0;
742
                        for (int i = selection.nextSetBit(0); i >= 0; i = selection
743
                                        .nextSetBit(i + 1)) {
744
                                indexesToDel[j++] = i;
745
                                // /vea.removeRow(i);
746
                        }
747
                        /*
748
                         * VectorialLayerEdited vle = (VectorialLayerEdited) CADExtension
749
                         * .getEditionManager().getActiveLayerEdited(); ArrayList
750
                         * selectedRow = vle.getSelectedRow();
751
                         *
752
                         * int[] indexesToDel = new int[selectedRow.size()]; for (int i = 0;
753
                         * i < selectedRow.size(); i++) { IRowEdited edRow = (IRowEdited)
754
                         * selectedRow.get(i); indexesToDel[i] = edRow.getIndex(); }
755
                         */
756
                        for (int i = indexesToDel.length - 1; i >= 0; i--) {
757
                                vea.removeRow(indexesToDel[i], PluginServices.getText(this,
758
                                                "deleted_feature"),EditionEvent.GRAPHIC);
759
                        }
760
                } catch (DriverIOException e) {
761
                        e.printStackTrace();
762
                } catch (IOException e) {
763
                        e.printStackTrace();
764
                } finally {
765
                        try {
766
                                vea.endComplexRow();
767
                        } catch (IOException e1) {
768
                                e1.printStackTrace();
769
                        } catch (DriverIOException e1) {
770
                                e1.printStackTrace();
771
                        }
772
                }
773
                System.out.println("clear Selection");
774
                selection.clear();
775
                vle.clearSelection();
776
                /*
777
                 * if (getCadTool() instanceof SelectionCADTool) { SelectionCADTool
778
                 * selTool = (SelectionCADTool) getCadTool(); selTool.clearSelection(); }
779
                 */
780
                getMapControl().drawMap(false);
781
        }
782

    
783
        /**
784
         * DOCUMENT ME!
785
         *
786
         * @param b
787
         */
788
        public void setAdjustGrid(boolean b) {
789
                getGrid().setAdjustGrid(b);
790
        }
791

    
792
        /**
793
         * DOCUMENT ME!
794
         *
795
         * @param actionCommand
796
         */
797
        public void keyPressed(String actionCommand) {
798
                if (actionCommand.equals("eliminar")) {
799
                        delete();
800
                } else if (actionCommand.equals("escape")) {
801
                        if (getMapControl().getTool().equals("cadtooladapter")) {
802
                                CADTool ct = (CADTool) cadToolStack.peek();
803
                                ct.end();
804
                                cadToolStack.clear();
805
                                SelectionCADTool selCad = new SelectionCADTool();
806
                                selCad.init();
807
                                VectorialLayerEdited vle = (VectorialLayerEdited) CADExtension
808
                                                .getEditionManager().getActiveLayerEdited();
809
                                vle.clearSelection();
810

    
811
                                pushCadTool(selCad);
812
                                // getVectorialAdapter().getSelection().clear();
813
                                getMapControl().drawMap(false);
814
                                PluginServices.getMainFrame().setSelectedTool("_selection");
815
                                // askQuestion();
816
                        } else {
817
                                getMapControl().setPrevTool();
818
                        }
819
                }
820

    
821
                PluginServices.getMainFrame().enableControls();
822

    
823
        }
824

    
825
        public CADGrid getGrid() {
826
                return cadgrid;
827
        }
828

    
829

    
830
}