Statistics
| Revision:

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

History | View | Annotate | Download (15.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.Point;
7
import java.awt.event.MouseEvent;
8
import java.awt.event.MouseWheelEvent;
9
import java.awt.geom.Point2D;
10
import java.io.IOException;
11
import java.util.Stack;
12

    
13
import com.iver.andami.PluginServices;
14
import com.iver.cit.gvsig.fmap.ViewPort;
15
import com.iver.cit.gvsig.gui.View;
16
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
17
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
18
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
19
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
20
import com.iver.cit.gvsig.fmap.layers.FBitSet;
21
import com.iver.cit.gvsig.fmap.tools.BehaviorException;
22
import com.iver.cit.gvsig.fmap.tools.Behavior.Behavior;
23
import com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener;
24
import com.iver.utiles.console.JConsole;
25

    
26
/**
27
 * DOCUMENT ME!
28
 *
29
 * @author Fernando Gonz?lez Cort?s
30
 */
31
public class CADToolAdapter extends Behavior {
32
        private Stack cadToolStack = new Stack();
33
        private FBitSet selection;
34

    
35
        //Para pasarle las coordenadas cuando se produce un evento textEntered
36
        private int lastX;
37
        private int lastY;
38
        private FSymbol symbol = new FSymbol(FConstant.SYMBOL_TYPE_POINT, Color.RED);
39
        private Point2D mapAdjustedPoint;
40
        private boolean questionAsked = false;
41
        private Point2D adjustedPoint;
42
        private boolean snapping = false;
43
        private boolean adjustSnapping = false;
44
        private VectorialEditableAdapter vea;
45
        private CADGrid cadgrid=new CADGrid();
46
        /**
47
         * Pinta de alguna manera especial las geometrias seleccionadas para la
48
         * edici?n. En caso de que el snapping est? activado, pintar? el efecto
49
         * del mismo.
50
         *
51
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#paintComponent(java.awt.Graphics)
52
         */
53
        public void paintComponent(Graphics g) {
54
                super.paintComponent(g);
55
                drawCursor(g);
56

    
57
                if (adjustedPoint != null) {
58
                        Point2D p=null;
59
                        if (mapAdjustedPoint != null) {
60
                                p = mapAdjustedPoint;
61
                        } else {
62
                                p = getMapControl().getViewPort().toMapPoint(adjustedPoint);
63
                        }
64
                        ((CADTool) cadToolStack.peek()).drawOperation(g,
65
                                selection, p.getX(), p.getY());
66
                }
67
        }
68

    
69
        /**
70
         * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
71
         */
72
        public void mouseClicked(MouseEvent e) throws BehaviorException {
73
        }
74

    
75
        /**
76
         * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
77
         */
78
        public void mouseEntered(MouseEvent e) throws BehaviorException {
79
        }
80

    
81
        /**
82
         * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
83
         */
84
        public void mouseExited(MouseEvent e) throws BehaviorException {
85
        }
86

    
87
        /**
88
         * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
89
         */
90
        public void mousePressed(MouseEvent e) throws BehaviorException {
91
                if (e.getButton() == MouseEvent.BUTTON1) {
92
                        ViewPort vp = getMapControl().getMapContext().getViewPort();
93
                        Point2D p;
94

    
95
                                if (mapAdjustedPoint != null) {
96
                                p = mapAdjustedPoint;
97
                        } else {
98
                                p = vp.toMapPoint(adjustedPoint);
99
                        }
100
                        transition("punto", vea, selection,
101
                                new double[] { p.getX(), p.getY() });
102
                }
103
        }
104

    
105
        /**
106
         * Ajusta un punto de la imagen que se pasa como  par?metro al grid si ?ste
107
         * est? activo y devuelve la distancia de un punto al punto ajustado
108
         *
109
         * @param point
110
         * @param mapHandlerAdjustedPoint DOCUMENT ME!
111
         *
112
         * @return Distancia del punto que se pasa como par?metro al punto ajustado
113
         */
114
        private double adjustToHandler(Point2D point,
115
                Point2D mapHandlerAdjustedPoint) {
116
        /***        //if (selection.cardinality() > 0) {
117
                double rw=getMapControl().getViewPort().toMapDistance(5);
118
                try {
119
                        Point2D mapPoint = point;
120
                        Rectangle2D r = new Rectangle2D.Double(mapPoint.getX() - rw/2,
121
                                        mapPoint.getY() - rw/2, rw, rw);
122

123
                        int[] indexes = vea.getRowsIndexes(r);
124

125
                        double min = Double.MAX_VALUE;
126
                        Point2D argmin = null;
127
                        Point2D mapArgmin = null;
128

129
                        for (int i = 0; i < indexes.length; i++) {
130
                                IFeature fea;
131
                                fea = vea.getFeature(indexes[i]);
132

133
                                Handler[] handlers = fea.getGeometry().getHandlers(FGeometry.SELECTHANDLER);
134

135
                                for (int j = 0; j < handlers.length; j++) {
136
                                        Point2D handlerPoint = handlers[j].getPoint();
137
                                        //System.err.println("handlerPoint= "+ handlerPoint);
138
                                        Point2D handlerImagePoint = handlerPoint;
139
                                        double dist = handlerImagePoint.distance(point);
140
                                        if ((dist < getMapControl().getViewPort().toMapDistance(SelectionCadTool.tolerance)) &&
141
                                                        (dist < min)) {
142
                                                min = dist;
143
                                                argmin = handlerImagePoint;
144
                                                mapArgmin = handlerPoint;
145
                                        }
146
                                }
147
                        }
148

149
                        if (argmin != null) {
150
                                point.setLocation(argmin);
151

152
                                //Se hace el casting porque no se quiere redondeo
153
                                point.setLocation(argmin.getX(), argmin.getY());
154

155
                                mapHandlerAdjustedPoint.setLocation(mapArgmin);
156

157
                                return min;
158
                        }
159
                } catch (IOException e) {
160
                        e.printStackTrace();
161
                } catch (DriverIOException e) {
162
                        e.printStackTrace();
163
                }
164
        ***/
165
                return Double.MAX_VALUE;
166

    
167
        }
168

    
169
        /**
170
         * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
171
         */
172
        public void mouseReleased(MouseEvent e) throws BehaviorException {
173
        }
174

    
175
        /**
176
         * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
177
         */
178
        public void mouseDragged(MouseEvent e) throws BehaviorException {
179
                lastX = e.getX();
180
                lastY = e.getY();
181

    
182
                calculateSnapPoint(e.getPoint());
183
        }
184

    
185
        /**
186
         * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
187
         */
188
        public void mouseMoved(MouseEvent e) throws BehaviorException {
189
                lastX = e.getX();
190
                lastY = e.getY();
191

    
192
                calculateSnapPoint(e.getPoint());
193

    
194
                getMapControl().repaint();
195
        }
196

    
197
        /**
198
         * DOCUMENT ME!
199
         *
200
         * @param g DOCUMENT ME!
201
         */
202
        private void drawCursor(Graphics g) {
203
                Point2D p = adjustedPoint;
204

    
205
                if (p == null) {
206
                        getGrid().setViewPort(getMapControl().getViewPort());
207

    
208
                        return;
209
                }
210

    
211
                int size1 = 15;
212
                int size2 = 3;
213
                g.drawLine((int) (p.getX() - size1), (int) (p.getY()),
214
                        (int) (p.getX() + size1), (int) (p.getY()));
215
                g.drawLine((int) (p.getX()), (int) (p.getY() - size1),
216
                        (int) (p.getX()), (int) (p.getY() + size1));
217

    
218
                if (adjustedPoint != null) {
219
                        if (adjustSnapping) {
220
                                g.setColor(Color.ORANGE);
221
                                g.drawRect((int) (adjustedPoint.getX() - 5),
222
                                        (int) (adjustedPoint.getY() - 5), 10, 10);
223
                                g.drawRect((int) (adjustedPoint.getX() - 3),
224
                                        (int) (adjustedPoint.getY() - 3), 6, 6);
225
                                g.setColor(Color.MAGENTA);
226
                                g.drawRect((int) (adjustedPoint.getX() - 4),
227
                                        (int) (adjustedPoint.getY() - 4), 8, 8);
228

    
229
                                adjustSnapping = false;
230
                        } else {
231
                                g.drawRect((int) (p.getX() - size2), (int) (p.getY() - size2),
232
                                        (int) (size2 * 2), (int) (size2 * 2));
233
                        }
234
                }
235
        }
236

    
237
        /**
238
         * DOCUMENT ME!
239
         *
240
         * @param point
241
         */
242
        private void calculateSnapPoint(Point point) {
243
                //Se comprueba el ajuste a rejilla
244

    
245
                Point2D gridAdjustedPoint = getMapControl().getViewPort().toMapPoint(point);
246
                double minDistance = Double.MAX_VALUE;
247

    
248
        /***        if (cadToolStack.peek() instanceof SelectionCadTool && ((SelectionCadTool)cadToolStack.peek()).getAutomaton().getStatus()==0){
249
                        mapAdjustedPoint=gridAdjustedPoint;
250
                        adjustedPoint=(Point2D)point.clone();
251
                }else{
252
***/
253
                        minDistance= getGrid().adjustToGrid(gridAdjustedPoint);
254
                        if (minDistance < Double.MAX_VALUE) {
255
                                adjustedPoint = getMapControl().getViewPort().fromMapPoint(gridAdjustedPoint);
256
                                mapAdjustedPoint = gridAdjustedPoint;
257
                        } else {
258
                                mapAdjustedPoint = null;
259
                        }
260
        /***        }***/
261
                Point2D handlerAdjustedPoint = null;
262

    
263
                //Se comprueba el ajuste a los handlers
264
                if (mapAdjustedPoint != null) {
265
                        handlerAdjustedPoint = (Point2D) mapAdjustedPoint.clone(); //getMapControl().getViewPort().toMapPoint(point);
266
                } else {
267
                        handlerAdjustedPoint = getMapControl().getViewPort().toMapPoint(point);
268
                }
269

    
270
                Point2D mapPoint = new Point2D.Double();
271
                double distance = adjustToHandler(handlerAdjustedPoint, mapPoint);
272

    
273
                if (distance < minDistance) {
274
                        adjustSnapping = true;
275
                        adjustedPoint = getMapControl().getViewPort().fromMapPoint(handlerAdjustedPoint);
276
                        mapAdjustedPoint = mapPoint;
277
                        minDistance = distance;
278
                }
279

    
280
                //Si no hay ajuste
281
                if (minDistance == Double.MAX_VALUE) {
282
                        adjustedPoint = point;
283
                        mapAdjustedPoint = null;
284
                }
285

    
286
        }
287

    
288
        /**
289
         * @see java.awt.event.MouseWheelListener#mouseWheelMoved(java.awt.event.MouseWheelEvent)
290
         */
291
        public void mouseWheelMoved(MouseWheelEvent e) throws BehaviorException {
292
        }
293

    
294
        /**
295
         * M?todo que realiza las transiciones en las herramientas en funci?n de un
296
         * texto introducido en la consola
297
         *
298
         * @param text DOCUMENT ME!
299
         */
300
        public void textEntered(String text) {
301
                if (text == null) {
302
                        transition("cancel");
303
                } else {
304
                /*        if ("".equals(text)) {
305
                                transition("aceptar");
306
                        } else {*/
307
                                text = text.trim();
308

    
309
                                String[] numbers = text.split(",");
310
                                double[] values = null;
311

    
312
                                try {
313
                                        if (numbers.length == 2) {
314
                                                //punto
315
                                                values = new double[] {
316
                                                                Double.parseDouble(numbers[0]),
317
                                                                Double.parseDouble(numbers[1])
318
                                                        };
319
                                                transition("punto", vea, selection,
320
                                                        values);
321
                                        } else if (numbers.length == 1) {
322
                                                //valor
323
                                                values = new double[] { Double.parseDouble(numbers[0]) };
324
                                                transition("numero", vea, selection,
325
                                                        values);
326
                                        }
327
                                } catch (NumberFormatException e) {
328
                                        transition(text, vea, selection,
329
                                                new double[0]);
330
                                }
331
                        //}
332
                }
333
        }
334

    
335
        /**
336
         * DOCUMENT ME!
337
         *
338
         * @param text DOCUMENT ME!
339
         */
340
        public void transition(String text) {
341
                transition(text, vea, selection, new double[0]);
342
                configureMenu();
343
        }
344

    
345
        /**
346
         * DOCUMENT ME!
347
         */
348
        private void configureMenu() {
349
        /***        String[] desc = ((CADTool) cadToolStack.peek()).getAutomaton()
350
                                                 .getCurrentTransitionDescriptions();
351
                String[] labels = ((CADTool) cadToolStack.peek()).getAutomaton()
352
                                                   .getCurrentTransitions();
353
                getMapControl().clearMenu();
354

355
                for (int i = 0; i < desc.length; i++) {
356
                        if (desc[i] != null) {
357
                                getMapControl().addMenuEntry(desc[i], labels[i]);
358
                        }
359
                }
360
                ***/
361
        }
362

    
363
        /**
364
         * DOCUMENT ME!
365
         *
366
         * @param text DOCUMENT ME!
367
         * @param source DOCUMENT ME!
368
         * @param sel DOCUMENT ME!
369
         * @param values DOCUMENT ME!
370
         */
371
        private void transition(String text, VectorialEditableAdapter source,
372
                FBitSet sel, double[] values) {
373
                questionAsked = true;
374
                //logger.debug(text);
375
                if (!cadToolStack.isEmpty()){
376
                    CADTool ct = (CADTool) cadToolStack.peek();
377
                    ///String[] trs = ct.getAutomaton().getCurrentTransitions();
378
                    boolean esta = true;
379
                  /*  for (int i = 0; i < trs.length; i++) {
380
                if (trs[i].toUpperCase().equals(text.toUpperCase()))
381
                    esta = true;
382
            }
383
                    */
384
                    if (!esta){
385
                        askQuestion();
386
                    }else{
387
                                ct.transition(sel,
388
                                                values[0],values[1]);
389
                                //Si es la transici?n que finaliza una geometria hay que redibujar la vista.
390
                                getMapControl().drawMap(false);
391
                                askQuestion();
392
                        /*        if ((ret & Automaton.AUTOMATON_FINISHED) == Automaton.AUTOMATON_FINISHED) {
393
                                        popCadTool();
394

395
                                        if (cadToolStack.isEmpty()) {
396
                                                pushCadTool(new com.iver.cit.gvsig.gui.cad.smc.gen.CADTool());//new SelectionCadTool());
397
                                                PluginServices.getMainFrame().setSelectedTool("selection");
398
                                        }
399

400
                                        askQuestion();
401

402
                                        getMapControl().drawMap(false);
403
                                } else {
404
                                        if (((CadTool) cadToolStack.peek()).getAutomaton().checkState('c')) {
405
                                                getMapControl().drawMap(false);
406
                                        }
407

408
                                        if (!questionAsked) {
409
                                                askQuestion();
410
                                        }
411
                                }
412

413
                                configureMenu();*/
414
                    }
415
                }
416
                PluginServices.getMainFrame().enableControls();
417
        }
418

    
419
        /**
420
         * DOCUMENT ME!
421
         *
422
         * @param value DOCUMENT ME!
423
         */
424
        public void setGrid(boolean value) {
425
                getGrid().setUseGrid(value);
426
                getGrid().setViewPort(getMapControl().getViewPort());
427
                getMapControl().drawMap(false);
428
        }
429

    
430
        /**
431
         * DOCUMENT ME!
432
         *
433
         * @param activated DOCUMENT ME!
434
         */
435
        public void setSnapping(boolean activated) {
436
                snapping = activated;
437
        }
438

    
439
        /**
440
         * DOCUMENT ME!
441
         *
442
         * @param x DOCUMENT ME!
443
         * @param y DOCUMENT ME!
444
         * @param dist DOCUMENT ME!
445
         */
446
        public void getSnapPoint(double x, double y, double dist) {
447
        }
448

    
449
        /**
450
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#getListener()
451
         */
452
        public ToolListener getListener() {
453
                return new ToolListener() {
454
                                /**
455
                                 * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
456
                                 */
457
                                public Cursor getCursor() {
458
                                        return null;
459
                                }
460

    
461
                                /**
462
                                 * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
463
                                 */
464
                                public boolean cancelDrawing() {
465
                                        return false;
466
                                }
467
                        };
468
        }
469

    
470
        /**
471
         * DOCUMENT ME!
472
         *
473
         * @return DOCUMENT ME!
474
         */
475
        public CADTool getCadTool() {
476
                return (CADTool) cadToolStack.peek();
477
        }
478

    
479
        /**
480
         * DOCUMENT ME!
481
         *
482
         * @param cadTool DOCUMENT ME!
483
         */
484
        public void pushCadTool(CADTool cadTool) {
485
                cadToolStack.push(cadTool);
486
                cadTool.setCadToolAdapter(this);
487
                //cadTool.initializeStatus();
488
                cadTool.setVectorialAdapter(vea);
489
                /*int ret = cadTool.transition(null, editableFeatureSource, selection,
490
                                new double[0]);
491

492
                if ((ret & Automaton.AUTOMATON_FINISHED) == Automaton.AUTOMATON_FINISHED) {
493
                        popCadTool();
494

495
                        if (cadToolStack.isEmpty()) {
496
                                pushCadTool(new com.iver.cit.gvsig.gui.cad.smc.gen.CADTool());//new SelectionCadTool());
497
                                PluginServices.getMainFrame().setSelectedTool("selection");
498
                        }
499

500
                        askQuestion();
501

502
                        getMapControl().drawMap(false);
503
                }
504
                */
505
        }
506

    
507
        /**
508
         * DOCUMENT ME!
509
         */
510
        public void popCadTool() {
511
                cadToolStack.pop();
512
        }
513

    
514
        /**
515
         * DOCUMENT ME!
516
         */
517
        public void askQuestion() {
518
                CADTool cadtool=(CADTool) cadToolStack.peek();
519
                /*if (cadtool..getStatus()==0){
520
                        PluginServices.getMainFrame().addTextToConsole("\n" +cadtool.getName());
521
                }
522
*/
523
                View vista = (View) PluginServices.getMDIManager().getActiveView();
524
                vista.getConsolePanel().addText("\n" + cadtool.getQuestion());
525
                //***PluginServices.getMainFrame().addTextToConsole("\n" + cadtool.getQuestion());
526
                questionAsked = true;
527

    
528
        }
529

    
530
        /**
531
         * DOCUMENT ME!
532
         *
533
         * @param cadTool DOCUMENT ME!
534
         */
535
        public void setCadTool(CADTool cadTool) {
536
                cadToolStack.clear();
537
                pushCadTool(cadTool);
538
                askQuestion();
539
        }
540

    
541
        /**
542
         * DOCUMENT ME!
543
         *
544
         * @return DOCUMENT ME!
545
         */
546
        public VectorialEditableAdapter getVectorialAdapter() {
547
                return vea;
548
        }
549

    
550
        /**
551
         * DOCUMENT ME!
552
         *
553
         * @param editableFeatureSource DOCUMENT ME!
554
         * @param selection DOCUMENT ME!
555
         */
556
        public void setVectorialAdapter(
557
                VectorialEditableAdapter vea, FBitSet selection) {
558
                this.vea = vea;
559
                this.selection = selection;
560
        }
561

    
562
        /**
563
         * DOCUMENT ME!
564
         *
565
         * @return DOCUMENT ME!
566
         */
567
        /*public CadMapControl getCadMapControl() {
568
                return cadMapControl;
569
        }
570
*/
571
        /**
572
         * DOCUMENT ME!
573
         *
574
         * @param cadMapControl DOCUMENT ME!
575
         */
576
        /*public void setCadMapControl(CadMapControl cadMapControl) {
577
                this.cadMapControl = cadMapControl;
578
        }
579
*/
580

    
581
        /**
582
         * Elimina las geometr?as seleccionadas actualmente
583
         */
584
        private void delete() {
585
                vea.startComplexRow();
586

    
587
                try {
588
                        for (int i = selection.nextSetBit(0); i >= 0;
589
                                        i = selection.nextSetBit(i + 1)) {
590
                                vea.removeRow(i);
591
                        }
592
                } catch (DriverIOException e) {
593
                        e.printStackTrace();
594
                } catch (IOException e) {
595
                        e.printStackTrace();
596
                } finally {
597
                        try {
598
                                vea.endComplexRow();
599
                        } catch (IOException e1) {
600
                                e1.printStackTrace();
601
                        } catch (DriverIOException e1) {
602
                                e1.printStackTrace();
603
                        }
604
                }
605

    
606
                selection.clear();
607
                getMapControl().drawMap(false);
608
        }
609

    
610
        /**
611
         * DOCUMENT ME!
612
         *
613
         * @param b
614
         */
615
        public void setAdjustGrid(boolean b) {
616
                getGrid().setAdjustGrid(b);
617
        }
618

    
619
        /**
620
         * DOCUMENT ME!
621
         *
622
         * @param actionCommand
623
         */
624
        public void keyPressed(String actionCommand) {
625
                if (actionCommand.equals("eliminar")) {
626
                        delete();
627
                } else if (actionCommand.equals("escape")) {
628
                        if (getMapControl().getCurrentMapTool() instanceof CADToolAdapter){
629
                                cadToolStack.clear();
630
                                /***pushCadTool(new SelectionCadTool());***/
631
                                selection.clear();
632
                                getMapControl().drawMap(false);
633
                                /***PluginServices.getMainFrame().setSelectedTool("selection");***/
634
                                askQuestion();
635
                        }
636
                }
637

    
638
                PluginServices.getMainFrame().enableControls();
639

    
640
        }
641
        public CADGrid getGrid(){
642
                return cadgrid;
643
        }
644
}