Statistics
| Revision:

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

History | View | Annotate | Download (21.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.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.List;
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.Handler;
23
import com.iver.cit.gvsig.fmap.core.IGeometry;
24
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
25
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
26
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
27
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
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.tools.BehaviorException;
31
import com.iver.cit.gvsig.fmap.tools.Behavior.Behavior;
32
import com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener;
33
import com.iver.cit.gvsig.gui.View;
34
import com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool;
35
import com.iver.cit.gvsig.layers.VectorialLayerEdited;
36
import com.iver.utiles.console.JConsole;
37
import com.vividsolutions.jts.geom.Envelope;
38
import com.vividsolutions.jts.index.SpatialIndex;
39

    
40
public class CADToolAdapter extends Behavior {
41
        public static int MAX_ENTITIES_IN_SPATIAL_CACHE = 5000;
42

    
43
        private Stack cadToolStack = new Stack();
44

    
45
        // Para pasarle las coordenadas cuando se produce un evento textEntered
46
        private int lastX;
47

    
48
        private int lastY;
49

    
50
        private FSymbol symbol = new FSymbol(FConstant.SYMBOL_TYPE_POINT, Color.RED);
51

    
52
        private Point2D mapAdjustedPoint;
53

    
54
        private boolean questionAsked = false;
55

    
56
        private Point2D adjustedPoint;
57

    
58
        private boolean snapping = false;
59

    
60
        private boolean adjustSnapping = false;
61

    
62
        private VectorialEditableAdapter vea;
63

    
64
        private CADGrid cadgrid = new CADGrid();
65

    
66
        private SpatialIndex spatialCache;
67

    
68
        /**
69
         * Pinta de alguna manera especial las geometrias seleccionadas para la
70
         * edici?n. En caso de que el snapping est? activado, pintar? el efecto del
71
         * mismo.
72
         *
73
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#paintComponent(java.awt.Graphics)
74
         */
75
        public void paintComponent(Graphics g) {
76
                super.paintComponent(g);
77
                drawCursor(g);
78
                getGrid().drawGrid(g);
79
                if (adjustedPoint != null) {
80
                        Point2D p = null;
81
                        if (mapAdjustedPoint != null) {
82
                                p = mapAdjustedPoint;
83
                        } else {
84
                                p = getMapControl().getViewPort().toMapPoint(adjustedPoint);
85
                        }
86
                        ((CADTool) cadToolStack.peek())
87
                                        .drawOperation(g, p.getX(), p.getY());
88
                }
89
        }
90

    
91
        /**
92
         * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
93
         */
94
        public void mouseClicked(MouseEvent e) throws BehaviorException {
95
                if (e.getButton() == MouseEvent.BUTTON3) {
96
                        CADExtension.showPopup(e);
97
                }
98
        }
99

    
100
        /**
101
         * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
102
         */
103
        public void mouseEntered(MouseEvent e) throws BehaviorException {
104
                clearMouseImage();
105
        }
106

    
107
        /**
108
         * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
109
         */
110
        public void mouseExited(MouseEvent e) throws BehaviorException {
111
        }
112

    
113
        /**
114
         * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
115
         */
116
        public void mousePressed(MouseEvent e) throws BehaviorException {
117
                if (e.getButton() == MouseEvent.BUTTON1) {
118
                        ViewPort vp = getMapControl().getMapContext().getViewPort();
119
                        Point2D p;
120

    
121
                        if (mapAdjustedPoint != null) {
122
                                p = mapAdjustedPoint;
123
                        } else {
124
                                p = vp.toMapPoint(adjustedPoint);
125
                        }
126
                        transition(new double[] { p.getX(), p.getY() }, e);
127
                }
128
        }
129

    
130
        /**
131
         * Ajusta un punto de la imagen que se pasa como par?metro al grid si ?ste
132
         * est? activo y devuelve la distancia de un punto al punto ajustado
133
         *
134
         * @param point
135
         * @param mapHandlerAdjustedPoint
136
         *            DOCUMENT ME!
137
         *
138
         * @return Distancia del punto que se pasa como par?metro al punto ajustado
139
         */
140
        private double adjustToHandler(Point2D point,
141
                        Point2D mapHandlerAdjustedPoint) {
142
                // if (selection.cardinality() > 0) {
143
                if (getSpatialCache() == null)
144
                        return Double.MAX_VALUE;
145

    
146
                double rw = getMapControl().getViewPort().toMapDistance(5);
147
                Point2D mapPoint = point;
148
                Rectangle2D r = new Rectangle2D.Double(mapPoint.getX() - rw / 2,
149
                                mapPoint.getY() - rw / 2, rw, rw);
150

    
151
                // int[] indexes = vea.getRowsIndexes_OLD(r);
152
                Envelope e = FConverter.convertRectangle2DtoEnvelope(r);
153
                List l = getSpatialCache().query(e);
154
                double min = Double.MAX_VALUE;
155
                Point2D argmin = null;
156
                Point2D mapArgmin = null;
157

    
158
                for (int i = 0; i < l.size(); i++) {
159
                // for (int i = 0; i < indexes.length; i++) {
160
                        IGeometry geometry = null;
161
                        /* try {
162
                                geometry = vea.getShape(indexes[i]);
163
                        } catch (DriverIOException e1) {
164
                                // TODO Auto-generated catch block
165
                                e1.printStackTrace();
166
                        } */
167
                        geometry = (IGeometry) l.get(i);// getFeature(indexes[i]);
168
                        Handler[] handlers = geometry.getHandlers(IGeometry.SELECTHANDLER);
169

    
170
                        for (int j = 0; j < handlers.length; j++) {
171
                                Point2D handlerPoint = handlers[j].getPoint();
172
                                // System.err.println("handlerPoint= "+ handlerPoint);
173
                                Point2D handlerImagePoint = handlerPoint;
174
                                double dist = handlerImagePoint.distance(point);
175
                                if ((dist < getMapControl().getViewPort().toMapDistance(
176
                                                SelectionCADTool.tolerance))
177
                                                && (dist < min)) {
178
                                        min = dist;
179
                                        argmin = handlerImagePoint;
180
                                        mapArgmin = handlerPoint;
181
                                }
182
                        }
183
                }
184

    
185
                if (argmin != null) {
186
                        point.setLocation(argmin);
187

    
188
                        // Se hace el casting porque no se quiere redondeo
189
                        point.setLocation(argmin.getX(), argmin.getY());
190

    
191
                        mapHandlerAdjustedPoint.setLocation(mapArgmin);
192

    
193
                        return min;
194
                }
195

    
196
                return Double.MAX_VALUE;
197

    
198
        }
199

    
200
        /**
201
         * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
202
         */
203
        public void mouseReleased(MouseEvent e) throws BehaviorException {
204
                getMapControl().repaint();
205
        }
206

    
207
        /**
208
         * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
209
         */
210
        public void mouseDragged(MouseEvent e) throws BehaviorException {
211
                lastX = e.getX();
212
                lastY = e.getY();
213

    
214
                calculateSnapPoint(e.getPoint());
215
        }
216

    
217
        /**
218
         * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
219
         */
220
        public void mouseMoved(MouseEvent e) throws BehaviorException {
221

    
222
                lastX = e.getX();
223
                lastY = e.getY();
224

    
225
                calculateSnapPoint(e.getPoint());
226

    
227
                getMapControl().repaint();
228
        }
229

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

    
237
                getMapControl().setCursor(transparentCursor);
238
        }
239

    
240
        /**
241
         * DOCUMENT ME!
242
         *
243
         * @param g
244
         *            DOCUMENT ME!
245
         */
246
        private void drawCursor(Graphics g) {
247

    
248
                Point2D p = adjustedPoint;
249

    
250
                if (p == null) {
251
                        getGrid().setViewPort(getMapControl().getViewPort());
252

    
253
                        return;
254
                }
255

    
256
                int size1 = 15;
257
                int size2 = 3;
258
                g.drawLine((int) (p.getX() - size1), (int) (p.getY()),
259
                                (int) (p.getX() + size1), (int) (p.getY()));
260
                g.drawLine((int) (p.getX()), (int) (p.getY() - size1),
261
                                (int) (p.getX()), (int) (p.getY() + size1));
262

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

    
274
                                adjustSnapping = false;
275
                        } else {
276
                                g.drawRect((int) (p.getX() - size2), (int) (p.getY() - size2),
277
                                                (int) (size2 * 2), (int) (size2 * 2));
278
                        }
279
                }
280
        }
281

    
282
        /**
283
         * DOCUMENT ME!
284
         *
285
         * @param point
286
         */
287
        private void calculateSnapPoint(Point point) {
288
                // Se comprueba el ajuste a rejilla
289

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

    
301
                        minDistance = getGrid().adjustToGrid(gridAdjustedPoint);
302
                        if (minDistance < Double.MAX_VALUE) {
303
                                adjustedPoint = getMapControl().getViewPort().fromMapPoint(
304
                                                gridAdjustedPoint);
305
                                mapAdjustedPoint = gridAdjustedPoint;
306
                        } else {
307
                                mapAdjustedPoint = null;
308
                        }
309
                }
310
                Point2D handlerAdjustedPoint = null;
311

    
312
                // Se comprueba el ajuste a los handlers
313
                if (mapAdjustedPoint != null) {
314
                        handlerAdjustedPoint = (Point2D) mapAdjustedPoint.clone(); // getMapControl().getViewPort().toMapPoint(point);
315
                } else {
316
                        handlerAdjustedPoint = getMapControl().getViewPort().toMapPoint(
317
                                        point);
318
                }
319

    
320
                Point2D mapPoint = new Point2D.Double();
321
                double distance = adjustToHandler(handlerAdjustedPoint, mapPoint);
322

    
323
                if (distance < minDistance) {
324
                        adjustSnapping = true;
325
                        adjustedPoint = getMapControl().getViewPort().fromMapPoint(
326
                                        handlerAdjustedPoint);
327
                        mapAdjustedPoint = mapPoint;
328
                        minDistance = distance;
329
                }
330

    
331
                // Si no hay ajuste
332
                if (minDistance == Double.MAX_VALUE) {
333
                        adjustedPoint = point;
334
                        mapAdjustedPoint = null;
335
                }
336

    
337
        }
338

    
339
        /**
340
         * @see java.awt.event.MouseWheelListener#mouseWheelMoved(java.awt.event.MouseWheelEvent)
341
         */
342
        public void mouseWheelMoved(MouseWheelEvent e) throws BehaviorException {
343
                getMapControl().cancelDrawing();
344
                ViewPort vp = getMapControl().getViewPort();
345
                // Point2D pReal = vp.toMapPoint(e.getPoint());
346

    
347
                Point2D pReal = new Point2D.Double(vp.getAdjustedExtent().getCenterX(),
348
                                vp.getAdjustedExtent().getCenterY());
349
                int amount = e.getWheelRotation();
350
                double nuevoX;
351
                double nuevoY;
352
                double factor;
353

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

    
372
                        vp.setExtent(r);
373
                }
374
        }
375

    
376
        /**
377
         * M?todo que realiza las transiciones en las herramientas en funci?n de un
378
         * texto introducido en la consola
379
         *
380
         * @param text
381
         *            DOCUMENT ME!
382
         */
383
        public void textEntered(String text) {
384
                if (text == null) {
385
                        transition("cancel");
386
                } else {
387
                        /*
388
                         * if ("".equals(text)) { transition("aceptar"); } else {
389
                         */
390
                        text = text.trim();
391

    
392
                        String[] numbers = text.split(",");
393
                        double[] values = null;
394

    
395
                        try {
396
                                if (numbers.length == 2) {
397
                                        // punto
398
                                        values = new double[] { Double.parseDouble(numbers[0]),
399
                                                        Double.parseDouble(numbers[1]) };
400
                                        transition(values, null);
401
                                } else if (numbers.length == 1) {
402
                                        // valor
403
                                        values = new double[] { Double.parseDouble(numbers[0]) };
404
                                        transition(values[0]);
405
                                }
406
                        } catch (NumberFormatException e) {
407
                                transition(text);
408
                        }
409
                        // }
410
                }
411
                getMapControl().repaint();
412
        }
413

    
414

    
415
        /**
416
         * DOCUMENT ME!
417
         */
418
        public void configureMenu() {
419
                String[] desc = ((CADTool) cadToolStack.peek()).getDescriptions();
420
                // String[] labels = ((CADTool)
421
                // cadToolStack.peek()).getCurrentTransitions();
422
                CADExtension.clearMenu();
423

    
424
                for (int i = 0; i < desc.length; i++) {
425
                        if (desc[i] != null) {
426
                                CADExtension.addMenuEntry(PluginServices.getText(this,desc[i]));// , labels[i]);
427
                        }
428
                }
429

    
430
        }
431

    
432
        /**
433
         * Recibe los valores de la transici?n (normalmente un punto) y el evento
434
         * con el que se gener? (si fue de rat?n ser? MouseEvent, el que viene
435
         * en el pressed) y si es de teclado, ser? un KeyEvent.
436
         * Del evento se puede sacar informaci?n acerca de si estaba pulsada la tecla
437
         * CTRL, o Alt, etc.
438
         * @param values
439
         * @param event
440
         */
441
        private void transition(double[] values, InputEvent event) {
442
                questionAsked = true;
443
                if (!cadToolStack.isEmpty()) {
444
                        CADTool ct = (CADTool) cadToolStack.peek();
445
                        // /String[] trs = ct.getAutomaton().getCurrentTransitions();
446
                        boolean esta = true;
447
                        /*
448
                         * for (int i = 0; i < trs.length; i++) { if
449
                         * (trs[i].toUpperCase().equals(text.toUpperCase())) esta = true; }
450
                         */
451
                        if (!esta) {
452
                                askQuestion();
453
                        } else {
454
                                ct.transition(values[0], values[1], event);
455
                                // Si es la transici?n que finaliza una geometria hay que
456
                                // redibujar la vista.
457

    
458
                                askQuestion();
459
                                /*
460
                                 * if ((ret & Automaton.AUTOMATON_FINISHED) ==
461
                                 * Automaton.AUTOMATON_FINISHED) { popCadTool();
462
                                 *
463
                                 * if (cadToolStack.isEmpty()) { pushCadTool(new
464
                                 * com.iver.cit.gvsig.gui.cad.smc.gen.CADTool());//new
465
                                 * SelectionCadTool());
466
                                 * PluginServices.getMainFrame().setSelectedTool("selection"); }
467
                                 *
468
                                 * askQuestion();
469
                                 *
470
                                 * getMapControl().drawMap(false); } else { if (((CadTool)
471
                                 * cadToolStack.peek()).getAutomaton().checkState('c')) {
472
                                 * getMapControl().drawMap(false); }
473
                                 *
474
                                 * if (!questionAsked) { askQuestion(); } }
475
                                 *
476
                                 * configureMenu();
477
                                 */
478
                        }
479
                }
480
                configureMenu();
481
                PluginServices.getMainFrame().enableControls();
482
        }
483

    
484
        /**
485
         * DOCUMENT ME!
486
         *
487
         * @param text
488
         *            DOCUMENT ME!
489
         * @param source
490
         *            DOCUMENT ME!
491
         * @param sel
492
         *            DOCUMENT ME!
493
         * @param values
494
         *            DOCUMENT ME!
495
         */
496
        private void transition(double value) {
497
                questionAsked = true;
498
                if (!cadToolStack.isEmpty()) {
499
                        CADTool ct = (CADTool) cadToolStack.peek();
500
                        ct.transition(value);
501
                        askQuestion();
502
                }
503
                configureMenu();
504
                PluginServices.getMainFrame().enableControls();
505
        }
506

    
507
        public void transition(String option) {
508
                questionAsked = true;
509
                if (!cadToolStack.isEmpty()) {
510
                        CADTool ct = (CADTool) cadToolStack.peek();
511
                        try{
512
                        ct.transition(option);
513
                        }catch (Exception e) {
514
                                View vista = (View) PluginServices.getMDIManager().getActiveView();
515
                                vista.getConsolePanel().addText("\n" + PluginServices.getText(this,"incorrect_option")+ " : "+ option,JConsole.ERROR);
516
                        }
517
                        askQuestion();
518
                }
519
                configureMenu();
520
                PluginServices.getMainFrame().enableControls();
521
        }
522

    
523
        /**
524
         * DOCUMENT ME!
525
         *
526
         * @param value
527
         *            DOCUMENT ME!
528
         */
529
        public void setGrid(boolean value) {
530
                getGrid().setUseGrid(value);
531
                getGrid().setViewPort(getMapControl().getViewPort());
532
                getMapControl().drawMap(false);
533
        }
534

    
535
        /**
536
         * DOCUMENT ME!
537
         *
538
         * @param activated
539
         *            DOCUMENT ME!
540
         */
541
        public void setSnapping(boolean activated) {
542
                snapping = activated;
543
        }
544

    
545
        /**
546
         * DOCUMENT ME!
547
         *
548
         * @param x
549
         *            DOCUMENT ME!
550
         * @param y
551
         *            DOCUMENT ME!
552
         * @param dist
553
         *            DOCUMENT ME!
554
         */
555
        public void getSnapPoint(double x, double y, double dist) {
556
        }
557

    
558
        /**
559
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#getListener()
560
         */
561
        public ToolListener getListener() {
562
                return new ToolListener() {
563
                        /**
564
                         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
565
                         */
566
                        public Cursor getCursor() {
567
                                return null;
568
                        }
569

    
570
                        /**
571
                         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
572
                         */
573
                        public boolean cancelDrawing() {
574
                                return false;
575
                        }
576
                };
577
        }
578

    
579
        /**
580
         * DOCUMENT ME!
581
         *
582
         * @return DOCUMENT ME!
583
         */
584
        public CADTool getCadTool() {
585
                return (CADTool) cadToolStack.peek();
586
        }
587

    
588
        /**
589
         * DOCUMENT ME!
590
         *
591
         * @param cadTool
592
         *            DOCUMENT ME!
593
         */
594
        public void pushCadTool(CADTool cadTool) {
595
                cadToolStack.push(cadTool);
596
                cadTool.setCadToolAdapter(this);
597
                // cadTool.initializeStatus();
598
                // cadTool.setVectorialAdapter(vea);
599
                /*
600
                 * int ret = cadTool.transition(null, editableFeatureSource, selection,
601
                 * new double[0]);
602
                 *
603
                 * if ((ret & Automaton.AUTOMATON_FINISHED) ==
604
                 * Automaton.AUTOMATON_FINISHED) { popCadTool();
605
                 *
606
                 * if (cadToolStack.isEmpty()) { pushCadTool(new
607
                 * com.iver.cit.gvsig.gui.cad.smc.gen.CADTool());//new
608
                 * SelectionCadTool());
609
                 * PluginServices.getMainFrame().setSelectedTool("selection"); }
610
                 *
611
                 * askQuestion();
612
                 *
613
                 * getMapControl().drawMap(false); }
614
                 */
615
        }
616

    
617
        /**
618
         * DOCUMENT ME!
619
         */
620
        public void popCadTool() {
621
                cadToolStack.pop();
622
        }
623

    
624
        /**
625
         * DOCUMENT ME!
626
         */
627
        public void askQuestion() {
628
                CADTool cadtool = (CADTool) cadToolStack.peek();
629
                /*
630
                 * if (cadtool..getStatus()==0){
631
                 * PluginServices.getMainFrame().addTextToConsole("\n"
632
                 * +cadtool.getName()); }
633
                 */
634
                View vista = (View) PluginServices.getMDIManager().getActiveView();
635
                vista.getConsolePanel().addText("\n" +"#" +cadtool.getQuestion() + " > ",JConsole.MESSAGE);
636
                // ***PluginServices.getMainFrame().addTextToConsole("\n" +
637
                // cadtool.getQuestion());
638
                questionAsked = true;
639

    
640
        }
641

    
642
        /**
643
         * DOCUMENT ME!
644
         *
645
         * @param cadTool
646
         *            DOCUMENT ME!
647
         */
648
        public void setCadTool(CADTool cadTool) {
649
                cadToolStack.clear();
650
                pushCadTool(cadTool);
651
                //askQuestion();
652
        }
653

    
654
        /**
655
         * DOCUMENT ME!
656
         *
657
         * @return DOCUMENT ME!
658
         */
659
        public VectorialEditableAdapter getVectorialAdapter() {
660
                return vea;
661
        }
662

    
663
        /**
664
         * DOCUMENT ME!
665
         *
666
         * @param editableFeatureSource
667
         *            DOCUMENT ME!
668
         * @param selection
669
         *            DOCUMENT ME!
670
         */
671
        public void setVectorialAdapter(VectorialEditableAdapter vea) {
672
                this.vea = vea;
673
        }
674

    
675
        /**
676
         * DOCUMENT ME!
677
         *
678
         * @return DOCUMENT ME!
679
         */
680
        /*
681
         * public CadMapControl getCadMapControl() { return cadMapControl; }
682
         */
683
        /**
684
         * DOCUMENT ME!
685
         *
686
         * @param cadMapControl
687
         *            DOCUMENT ME!
688
         */
689
        /*
690
         * public void setCadMapControl(CadMapControl cadMapControl) {
691
         * this.cadMapControl = cadMapControl; }
692
         */
693

    
694
        /**
695
         * Elimina las geometr?as seleccionadas actualmente
696
         */
697
        private void delete() {
698
                vea.startComplexRow();
699
                FBitSet selection = getVectorialAdapter().getSelection();
700
                try {
701
                        int[] indexesToDel = new int[selection.cardinality()];
702
                        int j = 0;
703
                        for (int i = selection.nextSetBit(0); i >= 0; i = selection
704
                                        .nextSetBit(i + 1)) {
705
                                indexesToDel[j++] = i;
706
                                // /vea.removeRow(i);
707
                        }
708
                        /* VectorialLayerEdited vle = (VectorialLayerEdited) CADExtension
709
                                .getEditionManager().getActiveLayerEdited();
710
                        ArrayList selectedRow = vle.getSelectedRow();
711

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

    
747
        /**
748
         * DOCUMENT ME!
749
         *
750
         * @param b
751
         */
752
        public void setAdjustGrid(boolean b) {
753
                getGrid().setAdjustGrid(b);
754
        }
755

    
756
        /**
757
         * DOCUMENT ME!
758
         *
759
         * @param actionCommand
760
         */
761
        public void keyPressed(String actionCommand) {
762
                if (actionCommand.equals("eliminar")) {
763
                        delete();
764
                } else if (actionCommand.equals("escape")) {
765
                        if (getMapControl().getTool().equals("cadtooladapter")) {
766
                                CADTool ct = (CADTool) cadToolStack.peek();
767
                                ct.end();
768
                                cadToolStack.clear();
769
                                SelectionCADTool selCad = new SelectionCADTool();
770
                                selCad.init();
771
                                VectorialLayerEdited vle=(VectorialLayerEdited)CADExtension.getEditionManager().getActiveLayerEdited();
772
                                vle.clearSelection();
773

    
774
                                pushCadTool(selCad);
775
                                // getVectorialAdapter().getSelection().clear();
776
                                getMapControl().drawMap(false);
777
                                PluginServices.getMainFrame().setSelectedTool("SELCAD");
778
                                //askQuestion();
779
                        }else{
780
                                getMapControl().setPrevTool();
781
                        }
782
                }
783

    
784
                PluginServices.getMainFrame().enableControls();
785

    
786
        }
787

    
788
        public CADGrid getGrid() {
789
                return cadgrid;
790
        }
791

    
792
        /**
793
         * @return Returns the spatialCache.
794
         */
795
        public SpatialIndex getSpatialCache() {
796
                return spatialCache;
797
        }
798

    
799
        /**
800
         * Se usa para rellenar la cache de entidades
801
         * con la que queremos trabajar (para hacer snapping,
802
         * por ejemplo. Lo normal ser?
803
         * rellenarla cada vez que cambie el extent, y
804
         * bas?ndonos en el futuro EditionManager para saber
805
         * de cu?ntos temas hay que leer. Si el numero de entidades
806
         * supera MAX_ENTITIES_IN_SPATIAL_CACHE, lo pondremos
807
         * a nulo.
808
         * @throws DriverException
809
         */
810
/*        public void createSpatialCache() throws DriverException {
811
                ViewPort vp = getMapControl().getViewPort();
812
                Rectangle2D extent = vp.getAdjustedExtent();
813
                // TODO: Por ahora cogemos el VectorialAdapter
814
                // de aqu?, pero deber?amos tener un m?todo que
815
                // le pregunte al EditionManager el tema sobre
816
                // el que estamos pintando.
817
                String strEPSG = vp.getProjection().getAbrev().substring(5);
818
                IRowEdited[] feats = getVectorialAdapter().getFeatures(extent, strEPSG);
819
                if (feats.length > MAX_ENTITIES_IN_SPATIAL_CACHE)
820
                        this.spatialCache = null;
821
                else
822
                        this.spatialCache = new Quadtree();
823
                for (int i=0; i < feats.length; i++)
824
                {
825
                        IFeature feat =  (IFeature)feats[i].getLinkedRow();
826
                        IGeometry geom = feat.getGeometry();
827
                        // TODO: EL getBounds2D del IGeometry ralentiza innecesariamente
828
                        // Podr?amos hacer que GeneralPathX lo tenga guardado,
829
                        // y tenga un constructor en el que se lo fijes.
830
                        // De esta forma, el driver, a la vez que recupera
831
                        // las geometr?as podr?a calcular el boundingbox
832
                        // y asignarlo. Luego habr?a que poner un m?todo
833
                        // que recalcule el bounding box bajo demanda.
834

835
                        Envelope e = FConverter.convertRectangle2DtoEnvelope(geom.getBounds2D());
836
                        spatialCache.insert(e, geom);
837
                }
838
        }*/
839
}