Statistics
| Revision:

root / branches / gvSIG_CAD / applications / appgvSIG / src / com / iver / cit / gvsig / gui / cad / CADToolAdapter.java @ 3513

History | View | Annotate | Download (17.1 KB)

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

    
3
import com.iver.andami.PluginServices;
4

    
5
import com.iver.cit.gvsig.ViewControls;
6
import com.iver.cit.gvsig.fmap.ViewPort;
7
import com.iver.cit.gvsig.fmap.core.FGeometry;
8
import com.iver.cit.gvsig.fmap.core.Handler;
9
import com.iver.cit.gvsig.fmap.core.IGeometry;
10
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
11
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
12
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
13
import com.iver.cit.gvsig.fmap.edition.EditableFeatureSource;
14
import com.iver.cit.gvsig.fmap.layers.FBitSet;
15
import com.iver.cit.gvsig.fmap.tools.Behavior.Behavior;
16
import com.iver.cit.gvsig.fmap.tools.BehaviorException;
17
import com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener;
18
import com.iver.cit.gvsig.gui.cad.tools.SelectionCadTool;
19

    
20
import com.iver.fsac.Automaton;
21

    
22
import com.iver.utiles.console.JConsole;
23

    
24
import org.apache.log4j.Logger;
25
import org.exolab.castor.xml.CastorException;
26

    
27
import java.awt.Color;
28
import java.awt.Cursor;
29
import java.awt.Graphics;
30
import java.awt.Point;
31
import java.awt.event.MouseEvent;
32
import java.awt.event.MouseWheelEvent;
33
import java.awt.geom.Point2D;
34
import java.awt.geom.Rectangle2D;
35

    
36
import java.io.IOException;
37

    
38
import java.util.Stack;
39

    
40

    
41
/**
42
 * DOCUMENT ME!
43
 *
44
 * @author Fernando Gonz?lez Cort?s
45
 */
46
public class CADToolAdapter extends Behavior {
47
        private static Logger logger = Logger.getLogger(CADToolAdapter.class.getName());
48
        private Stack cadToolStack = new Stack();
49
        private FBitSet selection;
50
        private EditableFeatureSource editableFeatureSource;
51

    
52
        //Para pasarle las coordenadas cuando se produce un evento textEntered
53
        private int lastX;
54
        private int lastY;
55
        private FSymbol symbol = new FSymbol(FConstant.SYMBOL_TYPE_POINT, Color.RED);
56
        private Point2D mapAdjustedPoint;
57
        private CadMapControl cadMapControl;
58
        private boolean questionAsked = false;
59
        private Point2D adjustedPoint;
60
        private boolean snapping = false;
61
        private boolean adjustSnapping = false;
62

    
63
        /**
64
         * Pinta de alguna manera especial las geometrias seleccionadas para la
65
         * edici?n. En caso de que el snapping est? activado, pintar? el efecto
66
         * del mismo.
67
         *
68
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#paintComponent(java.awt.Graphics)
69
         */
70
        public void paintComponent(Graphics g) {
71
                super.paintComponent(g);
72

    
73
                /*
74
                   for (int i = selection.nextSetBit(0); i >= 0;
75
                                   i = selection.nextSetBit(i + 1)) {
76
                           try {
77
                                   IGeometry geom = editableFeatureSource.getGeometry(i);
78
                                   Handler[] handlers=geom.getHandlers();
79
                                   geom.draw((Graphics2D) g, getMapControl().getViewPort(), symbol);
80
                                   FGraphicUtilities.DrawHandlers((Graphics2D)g,getMapControl().getViewPort().getAffineTransform(),handlers);
81
                           } catch (IOException e) {
82
                                   e.printStackTrace();
83
                           } catch (DriverIOException e) {
84
                                   e.printStackTrace();
85
                           }
86
                   }
87
                 */
88
                drawCursor(g);
89

    
90
                if (adjustedPoint != null) {
91
                        //Point2D p = getMapControl().getViewPort().toMapPoint((int) adjustedPoint.getX(),
92
                        //                (int) adjustedPoint.getY());
93
                        Point2D p=null;
94
                        if (mapAdjustedPoint != null) {
95
                                p = mapAdjustedPoint;
96
                        } else {
97
                                p = getMapControl().getViewPort().toMapPoint(adjustedPoint);
98
                        }
99
                        ((CadTool) cadToolStack.peek()).drawOperation(g,
100
                                editableFeatureSource, selection, p.getX(), p.getY());
101
                }
102
        }
103

    
104
        /**
105
         * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
106
         */
107
        public void mouseClicked(MouseEvent e) throws BehaviorException {
108
        }
109

    
110
        /**
111
         * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
112
         */
113
        public void mouseEntered(MouseEvent e) throws BehaviorException {
114
        }
115

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

    
122
        /**
123
         * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
124
         */
125
        public void mousePressed(MouseEvent e) throws BehaviorException {
126
                if (!(getMapControl().getCurrentMapTool() instanceof CADToolAdapter)) return;
127
                if (e.getButton() == MouseEvent.BUTTON1) {
128
                        ViewPort vp = getMapControl().getMapContext().getViewPort();
129
                        Point2D p;
130

    
131
                        //if (cadToolStack.peek() instanceof SelectionCadTool){
132
                        //p= vp.toMapPoint(e.getPoint());
133
                        //}else{
134
                                if (mapAdjustedPoint != null) {
135
                                p = mapAdjustedPoint;
136
                        } else {
137
                                p = vp.toMapPoint(adjustedPoint);
138
                        }
139
                        //}
140
                        transition("punto", editableFeatureSource, selection,
141
                                new double[] { p.getX(), p.getY() });
142
                }
143
        }
144

    
145
        /**
146
         * Ajusta un punto de la imagen que se pasa como  par?metro al grid si ?ste
147
         * est? activo y devuelve la distancia de un punto al punto ajustado
148
         *
149
         * @param point
150
         * @param mapHandlerAdjustedPoint DOCUMENT ME!
151
         *
152
         * @return Distancia del punto que se pasa como par?metro al punto ajustado
153
         */
154
        private double adjustToHandler(Point2D point,
155
                Point2D mapHandlerAdjustedPoint) {
156
                //if (selection.cardinality() > 0) {
157
                double rw=getMapControl().getViewPort().toMapDistance(5);
158
                try {
159
                        Point2D mapPoint = point;
160
                        Rectangle2D r = new Rectangle2D.Double(mapPoint.getX() - rw/2,
161
                                        mapPoint.getY() - rw/2, rw, rw);
162

    
163
                        int[] indexes = editableFeatureSource.getGeometriesIndexes(r);
164

    
165
                        double min = Double.MAX_VALUE;
166
                        Point2D argmin = null;
167
                        Point2D mapArgmin = null;
168

    
169
                        for (int i = 0; i < indexes.length; i++) {
170
                                //if (!selection.get(indexes[i])) continue;
171
                                IGeometry geom;
172
                                geom = editableFeatureSource.getGeometry(indexes[i]);
173

    
174
                                Handler[] handlers = geom.getHandlers(FGeometry.SELECTHANDLER);
175

    
176
                                for (int j = 0; j < handlers.length; j++) {
177
                                        Point2D handlerPoint = handlers[j].getPoint();
178
                                        //System.err.println("handlerPoint= "+ handlerPoint);
179
                                        Point2D handlerImagePoint = handlerPoint;
180
                                        double dist = handlerImagePoint.distance(point);
181
                                        if ((dist < getMapControl().getViewPort().toMapDistance(SelectionCadTool.tolerance)) &&
182
                                                        (dist < min)) {
183
                                                min = dist;
184
                                                argmin = handlerImagePoint;
185
                                                mapArgmin = handlerPoint;
186
                                        }
187
                                }
188
                        }
189

    
190
                        if (argmin != null) {
191
                                point.setLocation(argmin);
192

    
193
                                //Se hace el casting porque no se quiere redondeo
194
                                point.setLocation(argmin.getX(), argmin.getY());
195

    
196
                                mapHandlerAdjustedPoint.setLocation(mapArgmin);
197

    
198
                                return min;
199
                        }
200
                } catch (IOException e) {
201
                        e.printStackTrace();
202
                } catch (DriverIOException e) {
203
                        e.printStackTrace();
204
                }
205

    
206
                //}
207
                return Double.MAX_VALUE;
208
        }
209

    
210
        /**
211
         * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
212
         */
213
        public void mouseReleased(MouseEvent e) throws BehaviorException {
214
        }
215

    
216
        /**
217
         * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
218
         */
219
        public void mouseDragged(MouseEvent e) throws BehaviorException {
220
                lastX = e.getX();
221
                lastY = e.getY();
222

    
223
                calculateSnapPoint(e.getPoint());
224
        }
225

    
226
        /**
227
         * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
228
         */
229
        public void mouseMoved(MouseEvent e) throws BehaviorException {
230
                lastX = e.getX();
231
                lastY = e.getY();
232

    
233
                calculateSnapPoint(e.getPoint());
234

    
235
                getMapControl().repaint();
236
        }
237

    
238
        /**
239
         * DOCUMENT ME!
240
         *
241
         * @param g DOCUMENT ME!
242
         */
243
        private void drawCursor(Graphics g) {
244
                Point2D p = adjustedPoint;
245

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

    
250
                        return;
251
                }
252

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

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

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

    
279
        /**
280
         * DOCUMENT ME!
281
         *
282
         * @param point
283
         */
284
        private void calculateSnapPoint(Point point) {
285
                //Se comprueba el ajuste a rejilla
286
                
287
                Point2D gridAdjustedPoint = getMapControl().getViewPort().toMapPoint(point);
288
                double minDistance = Double.MAX_VALUE; 
289
                        
290
                if (cadToolStack.peek() instanceof SelectionCadTool && ((SelectionCadTool)cadToolStack.peek()).getAutomaton().getStatus()==0){
291
                        mapAdjustedPoint=gridAdjustedPoint;
292
                        adjustedPoint=(Point2D)point.clone();
293
                }else{
294
                
295
                        minDistance= getMapControl().getGrid().adjustToGrid(gridAdjustedPoint);
296
                        if (minDistance < Double.MAX_VALUE) {
297
                                adjustedPoint = getMapControl().getViewPort().fromMapPoint(gridAdjustedPoint);
298
                                mapAdjustedPoint = gridAdjustedPoint;
299
                        } else {
300
                                mapAdjustedPoint = null;
301
                        }
302
                }
303
                Point2D handlerAdjustedPoint = null;
304

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

    
312
                Point2D mapPoint = new Point2D.Double();
313
                double distance = adjustToHandler(handlerAdjustedPoint, mapPoint);
314

    
315
                if (distance < minDistance) {
316
                        adjustSnapping = true;
317
                        adjustedPoint = getMapControl().getViewPort().fromMapPoint(handlerAdjustedPoint);
318
                        mapAdjustedPoint = mapPoint;
319
                        minDistance = distance;
320
                }
321

    
322
                //Si no hay ajuste
323
                if (minDistance == Double.MAX_VALUE) {
324
                        adjustedPoint = point;
325
                        mapAdjustedPoint = null;
326
                }
327
        }
328

    
329
        /**
330
         * @see java.awt.event.MouseWheelListener#mouseWheelMoved(java.awt.event.MouseWheelEvent)
331
         */
332
        public void mouseWheelMoved(MouseWheelEvent e) throws BehaviorException {
333
        }
334

    
335
        /**
336
         * M?todo que realiza las transiciones en las herramientas en funci?n de un
337
         * texto introducido en la consola
338
         *
339
         * @param text DOCUMENT ME!
340
         */
341
        public void textEntered(String text) {
342
                if (text == null) {
343
                        transition("cancel");
344
                } else {
345
                /*        if ("".equals(text)) {
346
                                transition("aceptar");
347
                        } else {*/
348
                                text = text.trim();
349

    
350
                                String[] numbers = text.split(",");
351
                                double[] values = null;
352

    
353
                                try {
354
                                        if (numbers.length == 2) {
355
                                                //punto
356
                                                values = new double[] {
357
                                                                Double.parseDouble(numbers[0]),
358
                                                                Double.parseDouble(numbers[1])
359
                                                        };
360
                                                transition("punto", editableFeatureSource, selection,
361
                                                        values);
362
                                        } else if (numbers.length == 1) {
363
                                                //valor
364
                                                values = new double[] { Double.parseDouble(numbers[0]) };
365
                                                transition("numero", editableFeatureSource, selection,
366
                                                        values);
367
                                        }
368
                                } catch (NumberFormatException e) {
369
                                        transition(text, editableFeatureSource, selection,
370
                                                new double[0]);
371
                                }
372
                        //}
373
                }
374
        }
375

    
376
        /**
377
         * DOCUMENT ME!
378
         *
379
         * @param text DOCUMENT ME!
380
         */
381
        public void transition(String text) {
382
                transition(text, editableFeatureSource, selection, new double[0]);
383
                configureMenu();
384
        }
385

    
386
        /**
387
         * DOCUMENT ME!
388
         */
389
        private void configureMenu() {
390
                String[] desc = ((CadTool) cadToolStack.peek()).getAutomaton()
391
                                                 .getCurrentTransitionDescriptions();
392
                String[] labels = ((CadTool) cadToolStack.peek()).getAutomaton()
393
                                                   .getCurrentTransitions();
394
                getCadMapControl().clearMenu();
395

    
396
                for (int i = 0; i < desc.length; i++) {
397
                        if (desc[i] != null) {
398
                                getCadMapControl().addMenuEntry(desc[i], labels[i]);
399
                        }
400
                }
401
        }
402

    
403
        /**
404
         * DOCUMENT ME!
405
         *
406
         * @param text DOCUMENT ME!
407
         * @param source DOCUMENT ME!
408
         * @param sel DOCUMENT ME!
409
         * @param values DOCUMENT ME!
410
         */
411
        private void transition(String text, EditableFeatureSource source,
412
                FBitSet sel, double[] values) {
413
                questionAsked = false;
414
                logger.debug(text);
415
                if (!cadToolStack.isEmpty()){
416
                    CadTool ct = (CadTool) cadToolStack.peek();
417
                    String[] trs = ct.getAutomaton().getCurrentTransitions();
418
                    boolean esta = false;
419
                    for (int i = 0; i < trs.length; i++) {
420
                if (trs[i].toUpperCase().equals(text.toUpperCase()))
421
                    esta = true;
422
            }
423
                    if (!esta){
424
                        askQuestion();
425
                    }else{
426
                                int ret = ct.transition(text, source, sel,
427
                                                values);
428

    
429
                                if ((ret & Automaton.AUTOMATON_FINISHED) == Automaton.AUTOMATON_FINISHED) {
430
                                        popCadTool();
431

    
432
                                        if (cadToolStack.isEmpty()) {
433
                                                pushCadTool(new SelectionCadTool());
434
                                                PluginServices.getMainFrame().setSelectedTool("selection");
435
                                        }
436

    
437
                                        askQuestion();
438

    
439
                                        getMapControl().drawMap(false);
440
                                } else {
441
                                        if (((CadTool) cadToolStack.peek()).getAutomaton().checkState('c')) {
442
                                                getMapControl().drawMap(false);
443
                                        }
444

    
445
                                        if (!questionAsked) {
446
                                                askQuestion();
447
                                        }
448
                                }
449

    
450
                                configureMenu();
451
                    }
452
                }
453
                PluginServices.getMainFrame().enableControls();
454
        }
455

    
456
        /**
457
         * DOCUMENT ME!
458
         *
459
         * @param value DOCUMENT ME!
460
         */
461
        public void setGrid(boolean value) {
462
                getMapControl().getGrid().setUseGrid(value);
463
                getMapControl().getGrid().setViewPort(cadMapControl.getMapControl()
464
                                                                                                                   .getViewPort());
465
                getMapControl().drawMap(false);
466
        }
467

    
468
        /**
469
         * DOCUMENT ME!
470
         *
471
         * @param activated DOCUMENT ME!
472
         */
473
        public void setSnapping(boolean activated) {
474
                snapping = activated;
475
        }
476

    
477
        /**
478
         * DOCUMENT ME!
479
         *
480
         * @param x DOCUMENT ME!
481
         * @param y DOCUMENT ME!
482
         * @param dist DOCUMENT ME!
483
         */
484
        public void getSnapPoint(double x, double y, double dist) {
485
        }
486

    
487
        /**
488
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#getListener()
489
         */
490
        public ToolListener getListener() {
491
                return new ToolListener() {
492
                                /**
493
                                 * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
494
                                 */
495
                                public Cursor getCursor() {
496
                                        return null;
497
                                }
498

    
499
                                /**
500
                                 * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
501
                                 */
502
                                public boolean cancelDrawing() {
503
                                        return false;
504
                                }
505
                        };
506
        }
507

    
508
        /**
509
         * DOCUMENT ME!
510
         *
511
         * @return DOCUMENT ME!
512
         */
513
        public CadTool getCadTool() {
514
                return (CadTool) cadToolStack.peek();
515
        }
516

    
517
        /**
518
         * DOCUMENT ME!
519
         *
520
         * @param cadTool DOCUMENT ME!
521
         */
522
        public void pushCadTool(CadTool cadTool) {
523
                cadToolStack.push(cadTool);
524
                cadTool.setCadToolAdapter(this);
525
                cadTool.initializeStatus();
526

    
527
                int ret = cadTool.transition(null, editableFeatureSource, selection,
528
                                new double[0]);
529

    
530
                if ((ret & Automaton.AUTOMATON_FINISHED) == Automaton.AUTOMATON_FINISHED) {
531
                        popCadTool();
532

    
533
                        if (cadToolStack.isEmpty()) {
534
                                pushCadTool(new SelectionCadTool());
535
                                PluginServices.getMainFrame().setSelectedTool("selection");
536
                        }
537

    
538
                        askQuestion();
539

    
540
                        getMapControl().drawMap(false);
541
                }
542
        }
543

    
544
        /**
545
         * DOCUMENT ME!
546
         */
547
        public void popCadTool() {
548
                cadToolStack.pop();
549
        }
550

    
551
        /**
552
         * DOCUMENT ME!
553
         */
554
        public void askQuestion() {
555
                CadTool cadtool=(CadTool) cadToolStack.peek();
556
                if (cadtool.getAutomaton().getStatus()==0){
557
                        PluginServices.getMainFrame().addTextToConsole("\n" +cadtool.getName());
558
                }
559
                PluginServices.getMainFrame().addTextToConsole("\n" + cadtool.getQuestion());
560
                questionAsked = true;
561
        }
562

    
563
        /**
564
         * DOCUMENT ME!
565
         *
566
         * @param cadTool DOCUMENT ME!
567
         */
568
        public void setCadTool(CadTool cadTool) {
569
                cadToolStack.clear();
570
                pushCadTool(cadTool);
571
                askQuestion();
572
        }
573

    
574
        /**
575
         * DOCUMENT ME!
576
         *
577
         * @return DOCUMENT ME!
578
         */
579
        public EditableFeatureSource getEditableFeatureSource() {
580
                return editableFeatureSource;
581
        }
582

    
583
        /**
584
         * DOCUMENT ME!
585
         *
586
         * @param editableFeatureSource DOCUMENT ME!
587
         * @param selection DOCUMENT ME!
588
         */
589
        public void setEditableFeatureSource(
590
                EditableFeatureSource editableFeatureSource, FBitSet selection) {
591
                this.editableFeatureSource = editableFeatureSource;
592
                this.selection = selection;
593
        }
594

    
595
        /**
596
         * DOCUMENT ME!
597
         *
598
         * @return DOCUMENT ME!
599
         */
600
        public CadMapControl getCadMapControl() {
601
                return cadMapControl;
602
        }
603

    
604
        /**
605
         * DOCUMENT ME!
606
         *
607
         * @param cadMapControl DOCUMENT ME!
608
         */
609
        public void setCadMapControl(CadMapControl cadMapControl) {
610
                this.cadMapControl = cadMapControl;
611
        }
612

    
613
        /**
614
         * Elimina las geometr?as seleccionadas actualmente
615
         */
616
        private void delete() {
617
                editableFeatureSource.startComplexGeometry();
618

    
619
                try {
620
                        for (int i = selection.nextSetBit(0); i >= 0;
621
                                        i = selection.nextSetBit(i + 1)) {
622
                                editableFeatureSource.removeGeometry(i);
623
                        }
624
                } catch (DriverIOException e) {
625
                        e.printStackTrace();
626
                } catch (IOException e) {
627
                        e.printStackTrace();
628
                } finally {
629
                        try {
630
                                editableFeatureSource.endComplexGeometry();
631
                        } catch (IOException e1) {
632
                                e1.printStackTrace();
633
                        } catch (DriverIOException e1) {
634
                                e1.printStackTrace();
635
                        }
636
                }
637

    
638
                selection.clear();
639
                getCadMapControl().getMapControl().drawMap(false);
640
        }
641

    
642
        /**
643
         * DOCUMENT ME!
644
         *
645
         * @param b
646
         */
647
        public void setAdjustGrid(boolean b) {
648
                getMapControl().getGrid().setAdjustGrid(b);
649
        }
650

    
651
        /**
652
         * DOCUMENT ME!
653
         *
654
         * @param actionCommand
655
         */
656
        public void keyPressed(String actionCommand) {
657
                if (actionCommand.equals("eliminar")) {
658
                        delete();
659
                } else if (actionCommand.equals("escape")) {
660
                        if (getMapControl().getCurrentMapTool() instanceof CADToolAdapter){
661
                                cadToolStack.clear();
662
                                pushCadTool(new SelectionCadTool());
663
                                selection.clear();
664
                                getMapControl().drawMap(false);
665
                                PluginServices.getMainFrame().setSelectedTool("selection");
666
                                askQuestion();
667
                        }
668
                } 
669

    
670
                PluginServices.getMainFrame().enableControls();
671
        }
672
}