Statistics
| Revision:

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

History | View | Annotate | Download (16.3 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.fmap.core.v02.FConstant;
16
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
17
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
18
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
19
import com.iver.cit.gvsig.fmap.layers.FBitSet;
20
import com.iver.cit.gvsig.fmap.tools.BehaviorException;
21
import com.iver.cit.gvsig.fmap.tools.Behavior.Behavior;
22
import com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener;
23
import com.iver.cit.gvsig.gui.View;
24
import com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool;
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

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

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

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

    
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(vea,
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( vea,
320
                                                        values);
321
                                        } else if (numbers.length == 1) {
322
                                                //valor
323
                                                values = new double[] { Double.parseDouble(numbers[0]) };
324
                                                transition( vea,
325
                                                        values[0]);
326
                                        }
327
                                } catch (NumberFormatException e) {
328
                                        transition( vea,
329
                                                text);
330
                                }
331
                        //}
332
                }
333
        }
334

    
335
        /**
336
         * DOCUMENT ME!
337
         *
338
         * @param text DOCUMENT ME!
339
         */
340
        public void transition(String text) {
341
                transition( vea, text);
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( VectorialEditableAdapter source,
372
                double[] values) {
373
                questionAsked = true;
374
                if (!cadToolStack.isEmpty()){
375
                    CADTool ct = (CADTool) cadToolStack.peek();
376
                    ///String[] trs = ct.getAutomaton().getCurrentTransitions();
377
                    boolean esta = true;
378
                  /*  for (int i = 0; i < trs.length; i++) {
379
                if (trs[i].toUpperCase().equals(text.toUpperCase()))
380
                    esta = true;
381
            }
382
                    */
383
                    if (!esta){
384
                        askQuestion();
385
                    }else{
386
                                ct.transition(values[0],values[1]);
387
                                //Si es la transici?n que finaliza una geometria hay que redibujar la vista.
388

    
389
                                askQuestion();
390
                        /*        if ((ret & Automaton.AUTOMATON_FINISHED) == Automaton.AUTOMATON_FINISHED) {
391
                                        popCadTool();
392

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

398
                                        askQuestion();
399

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

406
                                        if (!questionAsked) {
407
                                                askQuestion();
408
                                        }
409
                                }
410

411
                                configureMenu();*/
412
                    }
413
                }
414
                PluginServices.getMainFrame().enableControls();
415
        }
416
        /**
417
         * DOCUMENT ME!
418
         *
419
         * @param text DOCUMENT ME!
420
         * @param source DOCUMENT ME!
421
         * @param sel DOCUMENT ME!
422
         * @param values DOCUMENT ME!
423
         */
424
        private void transition( VectorialEditableAdapter source,
425
                double value) {
426
                questionAsked = true;
427
                if (!cadToolStack.isEmpty()){
428
                    CADTool ct = (CADTool) cadToolStack.peek();
429
                    ct.transition(value);
430
                        askQuestion();
431
                    }
432

    
433
                PluginServices.getMainFrame().enableControls();
434
        }
435
        private void transition(VectorialEditableAdapter source,
436
                        String option) {
437
                        questionAsked = true;
438
                        if (!cadToolStack.isEmpty()){
439
                            CADTool ct = (CADTool) cadToolStack.peek();
440
                            ct.transition(option);
441
                                askQuestion();
442
                            }
443
                        PluginServices.getMainFrame().enableControls();
444
        }
445
        /**
446
         * DOCUMENT ME!
447
         *
448
         * @param value DOCUMENT ME!
449
         */
450
        public void setGrid(boolean value) {
451
                getGrid().setUseGrid(value);
452
                getGrid().setViewPort(getMapControl().getViewPort());
453
                getMapControl().drawMap(false);
454
        }
455

    
456
        /**
457
         * DOCUMENT ME!
458
         *
459
         * @param activated DOCUMENT ME!
460
         */
461
        public void setSnapping(boolean activated) {
462
                snapping = activated;
463
        }
464

    
465
        /**
466
         * DOCUMENT ME!
467
         *
468
         * @param x DOCUMENT ME!
469
         * @param y DOCUMENT ME!
470
         * @param dist DOCUMENT ME!
471
         */
472
        public void getSnapPoint(double x, double y, double dist) {
473
        }
474

    
475
        /**
476
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#getListener()
477
         */
478
        public ToolListener getListener() {
479
                return new ToolListener() {
480
                                /**
481
                                 * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
482
                                 */
483
                                public Cursor getCursor() {
484
                                        return null;
485
                                }
486

    
487
                                /**
488
                                 * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
489
                                 */
490
                                public boolean cancelDrawing() {
491
                                        return false;
492
                                }
493
                        };
494
        }
495

    
496
        /**
497
         * DOCUMENT ME!
498
         *
499
         * @return DOCUMENT ME!
500
         */
501
        public CADTool getCadTool() {
502
                return (CADTool) cadToolStack.peek();
503
        }
504

    
505
        /**
506
         * DOCUMENT ME!
507
         *
508
         * @param cadTool DOCUMENT ME!
509
         */
510
        public void pushCadTool(CADTool cadTool) {
511
                cadToolStack.push(cadTool);
512
                cadTool.setCadToolAdapter(this);
513
                //cadTool.initializeStatus();
514
                cadTool.setVectorialAdapter(vea);
515
                /*int ret = cadTool.transition(null, editableFeatureSource, selection,
516
                                new double[0]);
517

518
                if ((ret & Automaton.AUTOMATON_FINISHED) == Automaton.AUTOMATON_FINISHED) {
519
                        popCadTool();
520

521
                        if (cadToolStack.isEmpty()) {
522
                                pushCadTool(new com.iver.cit.gvsig.gui.cad.smc.gen.CADTool());//new SelectionCadTool());
523
                                PluginServices.getMainFrame().setSelectedTool("selection");
524
                        }
525

526
                        askQuestion();
527

528
                        getMapControl().drawMap(false);
529
                }
530
                */
531
        }
532

    
533
        /**
534
         * DOCUMENT ME!
535
         */
536
        public void popCadTool() {
537
                cadToolStack.pop();
538
        }
539

    
540
        /**
541
         * DOCUMENT ME!
542
         */
543
        public void askQuestion() {
544
                CADTool cadtool=(CADTool) cadToolStack.peek();
545
                /*if (cadtool..getStatus()==0){
546
                        PluginServices.getMainFrame().addTextToConsole("\n" +cadtool.getName());
547
                }
548
*/
549
                View vista = (View) PluginServices.getMDIManager().getActiveView();
550
                vista.getConsolePanel().addText("\n" + cadtool.getQuestion()+">");
551
                //***PluginServices.getMainFrame().addTextToConsole("\n" + cadtool.getQuestion());
552
                questionAsked = true;
553

    
554
        }
555

    
556
        /**
557
         * DOCUMENT ME!
558
         *
559
         * @param cadTool DOCUMENT ME!
560
         */
561
        public void setCadTool(CADTool cadTool) {
562
                cadToolStack.clear();
563
                pushCadTool(cadTool);
564
                askQuestion();
565
        }
566

    
567
        /**
568
         * DOCUMENT ME!
569
         *
570
         * @return DOCUMENT ME!
571
         */
572
        public VectorialEditableAdapter getVectorialAdapter() {
573
                return vea;
574
        }
575

    
576
        /**
577
         * DOCUMENT ME!
578
         *
579
         * @param editableFeatureSource DOCUMENT ME!
580
         * @param selection DOCUMENT ME!
581
         */
582
        public void setVectorialAdapter(
583
                VectorialEditableAdapter vea) {
584
                this.vea = vea;
585
        }
586

    
587
        /**
588
         * DOCUMENT ME!
589
         *
590
         * @return DOCUMENT ME!
591
         */
592
        /*public CadMapControl getCadMapControl() {
593
                return cadMapControl;
594
        }
595
*/
596
        /**
597
         * DOCUMENT ME!
598
         *
599
         * @param cadMapControl DOCUMENT ME!
600
         */
601
        /*public void setCadMapControl(CadMapControl cadMapControl) {
602
                this.cadMapControl = cadMapControl;
603
        }
604
*/
605

    
606
        /**
607
         * Elimina las geometr?as seleccionadas actualmente
608
         */
609
        private void delete() {
610
                vea.startComplexRow();
611
                FBitSet selection=getVectorialAdapter().getSelection();
612
                try {
613
                        for (int i = selection.nextSetBit(0); i >= 0;
614
                                        i = selection.nextSetBit(i + 1)) {
615
                                vea.removeRow(i);
616
                        }
617
                } catch (DriverIOException e) {
618
                        e.printStackTrace();
619
                } catch (IOException e) {
620
                        e.printStackTrace();
621
                } finally {
622
                        try {
623
                                vea.endComplexRow();
624
                        } catch (IOException e1) {
625
                                e1.printStackTrace();
626
                        } catch (DriverIOException e1) {
627
                                e1.printStackTrace();
628
                        }
629
                }
630

    
631
                selection.clear();
632
                getMapControl().drawMap(false);
633
        }
634

    
635
        /**
636
         * DOCUMENT ME!
637
         *
638
         * @param b
639
         */
640
        public void setAdjustGrid(boolean b) {
641
                getGrid().setAdjustGrid(b);
642
        }
643

    
644
        /**
645
         * DOCUMENT ME!
646
         *
647
         * @param actionCommand
648
         */
649
        public void keyPressed(String actionCommand) {
650
                if (actionCommand.equals("eliminar")) {
651
                        delete();
652
                } else if (actionCommand.equals("escape")) {
653
                        if (getMapControl().getTool().equals("cadtooladapter")){
654
                                CADTool ct = (CADTool) cadToolStack.peek();
655
                                ct.end();
656
                                cadToolStack.clear();
657
                                pushCadTool(new SelectionCADTool());
658
                                getVectorialAdapter().getSelection().clear();
659
                                getMapControl().drawMap(false);
660
                                /***PluginServices.getMainFrame().setSelectedTool("selection");***/
661
                                askQuestion();
662
                        }
663
                }
664

    
665
                PluginServices.getMainFrame().enableControls();
666

    
667
        }
668
        public CADGrid getGrid(){
669
                return cadgrid;
670
        }
671
}