Statistics
| Revision:

svn-gvsig-desktop / branches / gvSIG_CAD_Layout_version / applications / appgvSIG / src / com / iver / cit / gvsig / gui / cad / CADToolAdapter.java @ 1763

History | View | Annotate | Download (15.2 KB)

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

    
3
import java.awt.Color;
4
import java.awt.Cursor;
5
import java.awt.Graphics;
6
import java.awt.Point;
7
import java.awt.event.MouseEvent;
8
import java.awt.event.MouseWheelEvent;
9
import java.awt.geom.AffineTransform;
10
import java.awt.geom.Point2D;
11
import java.awt.geom.Rectangle2D;
12
import java.io.IOException;
13
import java.util.Stack;
14

    
15
import org.apache.log4j.Logger;
16

    
17
import com.iver.andami.PluginServices;
18
import com.iver.cit.gvsig.fmap.core.FGeometry;
19
import com.iver.cit.gvsig.fmap.core.Handler;
20
import com.iver.cit.gvsig.fmap.core.IGeometry;
21
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
22
import com.iver.cit.gvsig.fmap.edition.EditableFeatureSource;
23
import com.iver.cit.gvsig.fmap.layers.FBitSet;
24
import com.iver.cit.gvsig.fmap.tools.BehaviorException;
25
import com.iver.cit.gvsig.fmap.tools.Behavior.Behavior;
26
import com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener;
27
import com.iver.cit.gvsig.gui.cad.tools.SelectionCadTool;
28
import com.iver.fsac.Automaton;
29

    
30

    
31
/**
32
 * DOCUMENT ME!
33
 *
34
 * @author Fernando Gonz?lez Cort?s
35
 */
36
public abstract class CADToolAdapter extends Behavior {
37
        private static Logger logger = Logger.getLogger(CADToolAdapter.class.getName());
38
        protected Stack cadToolStack = new Stack();
39
        protected FBitSet selection;
40
        protected EditableFeatureSource editableFeatureSource;
41

    
42
        //Para pasarle las coordenadas cuando se produce un evento textEntered
43
        private int lastX;
44
        private int lastY;
45
        //private FSymbol symbol = new FSymbol(FConstant.SYMBOL_TYPE_POINT, Color.RED);
46
        private Point2D mapAdjustedPoint;
47
        private CadMapControl cadMapControl;
48
        private boolean questionAsked = false;
49
        protected CadGrid grid;// = new CadGrid();
50
        protected Point2D adjustedPoint;
51
        private boolean snapping=true;
52
        private boolean adjustSnapping=false;
53
        
54
        public CADToolAdapter(){
55
                 grid= new CadGrid(this);
56
        }
57
        public void paintComponent(Graphics g){
58
                super.paintComponent(g);
59
        }
60
        /**
61
         * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
62
         */
63
        public void mouseClicked(MouseEvent e) throws BehaviorException {
64
        }
65

    
66
        /**
67
         * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
68
         */
69
        public void mouseEntered(MouseEvent e) throws BehaviorException {
70
        }
71

    
72
        /**
73
         * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
74
         */
75
        public void mouseExited(MouseEvent e) throws BehaviorException {
76
        }
77

    
78
        /**
79
         * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
80
         */
81
        public void mousePressed(MouseEvent e) throws BehaviorException {
82
                if (e.getButton() == MouseEvent.BUTTON1) {
83
                        //ViewPort vp = getMapControl().getMapContext().getViewPort();
84
                Point2D p;
85

    
86
                        if (mapAdjustedPoint != null) {
87
                                p = mapAdjustedPoint;
88
                        } else {
89
                                p = toPoint(adjustedPoint);
90
                        }
91

    
92
                        transition("punto", editableFeatureSource, selection,
93
                                new double[] { p.getX(), p.getY() });
94
                }
95
        }
96

    
97
        
98
        /**
99
         * Ajusta un punto de la imagen que se pasa como  par?metro al grid si ?ste
100
         * est? activo y devuelve la distancia de un punto al punto ajustado
101
         *
102
         * @param point
103
         *
104
         * @return Distancia del punto que se pasa como par?metro al punto ajustado
105
         */
106
        private double adjustToHandler(Point2D point, Point2D mapHandlerAdjustedPoint) {
107
                //if (selection.cardinality() > 0) {
108
                        double rw=toDistance(5);
109
                        try {
110
                                Point2D mapPoint = point;
111

    
112
                                Rectangle2D r = new Rectangle2D.Double(mapPoint.getX() - rw/2,
113
                                        mapPoint.getY() - rw/2, rw, rw);
114

    
115
                                int[] indexes = editableFeatureSource.getGeometriesIndexes(r);
116
                                
117
                                double min = Double.MAX_VALUE;
118
                                Point2D argmin = null;
119
                                Point2D mapArgmin = null;
120

    
121
                                for (int i = 0; i < indexes.length; i++) {
122
                                        //if (!selection.get(indexes[i])) continue;
123
                                        IGeometry geom;
124
                                        geom = editableFeatureSource.getGeometry(indexes[i]);
125

    
126
                                        Handler[] handlers = geom.getHandlers(FGeometry.SELECTHANDLER);
127

    
128
                                        for (int j = 0; j < handlers.length; j++) {
129
                                                Point2D handlerPoint = handlers[j].getPoint();
130
                                                Point2D handlerImagePoint = handlerPoint;
131
                                                double dist = handlerImagePoint.distance(point);
132

    
133
                                                if ((dist < toDistance(SelectionCadTool.tolerance)) &&
134
                                                                (dist < min)) {
135
                                                        min = dist;
136
                                                        argmin = handlerImagePoint;
137
                                                        mapArgmin = handlerPoint;
138
                                                }
139
                                        }
140
                                }
141

    
142
                                if (argmin != null) {
143
                                        point.setLocation(argmin);
144

    
145
                                        //Se hace el casting porque no se quiere redondeo
146
                                        //point.setLocation((int)argmin.getX(),
147
                                        //                (int) argmin.getY());
148
                                        
149
                                        mapHandlerAdjustedPoint.setLocation(mapArgmin);
150
                                        return min;
151
                                }
152
                        } catch (IOException e) {
153
                                e.printStackTrace();
154
                        } catch (DriverIOException e) {
155
                                e.printStackTrace();
156
                        }
157
                //}
158

    
159
                return Double.MAX_VALUE;
160
        }
161

    
162
        /**
163
         * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
164
         */
165
        public void mouseReleased(MouseEvent e) throws BehaviorException {
166
        }
167

    
168
        /**
169
         * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
170
         */
171
        public void mouseDragged(MouseEvent e) throws BehaviorException {
172
                lastX = e.getX();
173
                lastY = e.getY();
174

    
175
                calculateSnapPoint(e.getPoint());
176
        }
177

    
178
        /**
179
         * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
180
         */
181
        public abstract void mouseMoved(MouseEvent e) throws BehaviorException;
182

    
183
        /**
184
         * DOCUMENT ME!
185
         *
186
         * @param g DOCUMENT ME!
187
         */
188
        public void drawCursor(Graphics g) {
189
                g.setColor(Color.black);
190
                Point2D p = adjustedPoint;
191
                if (p==null) grid.setAT(getAT());
192
                //else{
193
                int size1 = 15;
194
                int size2 = 3;
195
                g.drawLine((int) (p.getX() - size1), (int) (p.getY()),
196
                        (int) (p.getX() + size1), (int) (p.getY()));
197
                g.drawLine((int) (p.getX()), (int) (p.getY() - size1),
198
                        (int) (p.getX()), (int) (p.getY() + size1));
199
                if (adjustedPoint!=null){
200
                        if (adjustSnapping){
201
                                g.setColor(Color.ORANGE);
202
                                g.drawRect((int)(adjustedPoint.getX()-5),(int)(adjustedPoint.getY()-5),10,10);
203
                                g.drawRect((int)(adjustedPoint.getX()-3),(int)(adjustedPoint.getY()-3),6,6);
204
                                g.setColor(Color.MAGENTA);
205
                                g.drawRect((int)(adjustedPoint.getX()-4),(int)(adjustedPoint.getY()-4),8,8);
206
                                
207
                                adjustSnapping=false;
208
                        }else{
209
                                g.drawRect((int) (p.getX() - size2), (int) (p.getY() - size2),
210
                                                (int) (size2 * 2), (int) (size2 * 2));
211
                        }
212
                }
213
                //}
214
        }
215
        /**
216
         * DOCUMENT ME!
217
         *
218
         * @param point
219
         */
220
         protected void calculateSnapPoint(Point point) {
221
                //Se comprueba el ajuste a rejilla
222
                
223
                Point2D gridAdjustedPoint = toPoint(point);
224
                double minDistance = Double.MAX_VALUE; 
225
                        
226
                if (!cadToolStack.isEmpty() && cadToolStack.peek() instanceof SelectionCadTool && ((SelectionCadTool)cadToolStack.peek()).getAutomaton().getStatus()==0){
227
                        mapAdjustedPoint=gridAdjustedPoint;
228
                        adjustedPoint=(Point2D)point.clone();
229
                }else{
230
                        minDistance= grid.adjustToGrid(gridAdjustedPoint);
231
                        if (minDistance < Double.MAX_VALUE) {
232
                                adjustedPoint = fromPoint(gridAdjustedPoint);
233
                                mapAdjustedPoint = gridAdjustedPoint;
234
                        } else {
235
                                mapAdjustedPoint = null;
236
                        }
237
                }
238
                Point2D handlerAdjustedPoint = null;
239

    
240
                //Se comprueba el ajuste a los handlers
241
                if (mapAdjustedPoint != null) {
242
                        handlerAdjustedPoint = (Point2D) mapAdjustedPoint.clone(); //getMapControl().getViewPort().toMapPoint(point);
243
                } else {
244
                        handlerAdjustedPoint = toPoint(point);
245
                }
246

    
247
                Point2D mapPoint = new Point2D.Double();
248
                double distance = adjustToHandler(handlerAdjustedPoint, mapPoint);
249

    
250
                if (distance < minDistance) {
251
                        adjustSnapping = true;
252
                        adjustedPoint = fromPoint(handlerAdjustedPoint);
253
                        mapAdjustedPoint = mapPoint;
254
                        minDistance = distance;
255
                }
256

    
257
                //Si no hay ajuste
258
                if (minDistance == Double.MAX_VALUE) {
259
                        adjustedPoint = point;
260
                        mapAdjustedPoint = null;
261
                }
262
        }
263
        /*protected void calculateSnapPoint(Point point) {
264
                //Se comprueba el ajuste a rejilla
265
                Point gridAdjustedPoint = (Point) point.clone();
266
                double minDistance = grid.adjustToGrid(gridAdjustedPoint);
267
                if (minDistance < Double.MAX_VALUE) {
268
                        adjustedPoint = gridAdjustedPoint;
269
                        mapAdjustedPoint = toPoint(adjustedPoint);
270
                }
271
                
272
                //Se comprueba el ajuste a los handlers
273
                Point handlerAdjustedPoint = (Point) point.clone();
274
                Point2D mapPoint = new Point2D.Double();
275
                double distance = adjustToHandler(handlerAdjustedPoint, mapPoint);
276
                if (distance < minDistance) {
277
                        adjustSnapping=true;
278
                        adjustedPoint = handlerAdjustedPoint;
279
                        mapAdjustedPoint = mapPoint;
280
                        minDistance = distance;
281
                }
282

283
                //Si no hay ajuste
284
                if (minDistance == Double.MAX_VALUE) {
285
                        adjustedPoint = point;
286
                        mapAdjustedPoint = null;
287
                }
288
        }
289

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

    
297
        /**
298
         * DOCUMENT ME!
299
         *
300
         * @param text DOCUMENT ME!
301
         */
302
        public void textEntered(String text) {
303
                text = text.trim();
304

    
305
                String[] numbers = text.split(",");
306
                double[] values = null;
307

    
308
                try {
309
                        if (numbers.length == 2) {
310
                                //punto
311
                                values = new double[] {
312
                                                Double.parseDouble(numbers[0]),
313
                                                Double.parseDouble(numbers[1])
314
                                        };
315
                                transition("punto", editableFeatureSource, selection, values);
316
                        } else if (numbers.length == 1) {
317
                                //valor
318
                                values = new double[] { Double.parseDouble(numbers[0]) };
319
                                transition("numero", editableFeatureSource, selection, values);
320
                        }
321
                } catch (NumberFormatException e) {
322
                        transition(text, editableFeatureSource, selection, new double[0]);
323
                }
324
        }
325

    
326
        /**
327
         * DOCUMENT ME!
328
         *
329
         * @param text DOCUMENT ME!
330
         */
331
        public void transition(String text) {
332
                transition(text, editableFeatureSource, selection, new double[0]);
333
                configureMenu();
334
        }
335

    
336
        public abstract void configureMenu();
337

    
338
        /**
339
         * DOCUMENT ME!
340
         *
341
         * @param text DOCUMENT ME!
342
         * @param source DOCUMENT ME!
343
         * @param sel DOCUMENT ME!
344
         * @param values DOCUMENT ME!
345
         */
346
        protected void transition(String text, EditableFeatureSource source,
347
                FBitSet sel, double[] values) {
348
                questionAsked = false;
349
                logger.debug(text);
350

    
351
                int ret = ((CadTool) cadToolStack.peek()).transition(text, source, sel,
352
                                values);
353

    
354
                if ((ret & Automaton.AUTOMATON_FINISHED) == Automaton.AUTOMATON_FINISHED) {
355
                        popCadTool();
356

    
357
                        if (cadToolStack.isEmpty()) {
358
                                pushCadTool(new SelectionCadTool());
359
                                PluginServices.getMainFrame().selectTool("selection");
360
                        }
361

    
362
                        askQuestion();
363
                        if (getMapControl()!=null)
364
                        getMapControl().drawMap(false);
365
                } else {
366
                        if (((CadTool) cadToolStack.peek()).getAutomaton().checkState('c')) {
367
                                refresh();
368
                        }
369

    
370
                        if (!questionAsked) {
371
                                askQuestion();
372
                        }
373
                }
374
                //if (console!=null){
375
                        configureMenu();
376
                //}
377
                PluginServices.getMainFrame().enableControls();
378
        }
379

    
380
        /**
381
         * DOCUMENT ME!
382
         *
383
         * @param value DOCUMENT ME!
384
         */
385
        public abstract void setGrid(boolean value);
386
        
387

    
388
        /**
389
         * DOCUMENT ME!
390
         *
391
         * @param activated DOCUMENT ME!
392
         */
393
        public void setSnapping(boolean activated) {
394
                snapping=activated;
395
        }
396

    
397
        /**
398
         * DOCUMENT ME!
399
         *
400
         * @param x DOCUMENT ME!
401
         * @param y DOCUMENT ME!
402
         */
403
        public void getSnapPoint(double x, double y,double dist) {
404
        }
405

    
406
        /**
407
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#getListener()
408
         */
409
        public ToolListener getListener() {
410
                return new ToolListener() {
411
                                /**
412
                                 * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
413
                                 */
414
                                public Cursor getCursor() {
415
                                        return null;
416
                                }
417

    
418
                                /**
419
                                 * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
420
                                 */
421
                                public boolean cancelDrawing() {
422
                                        return false;
423
                                }
424
                        };
425
        }
426

    
427
        /**
428
         * DOCUMENT ME!
429
         *
430
         * @return DOCUMENT ME!
431
         */
432
        public CadTool getCadTool() {
433
                return (CadTool) cadToolStack.peek();
434
        }
435

    
436
        /**
437
         * DOCUMENT ME!
438
         *
439
         * @param cadTool DOCUMENT ME!
440
         */
441
        public void pushCadTool(CadTool cadTool) {
442
                cadToolStack.push(cadTool);
443
                cadTool.setCadToolAdapter(this);
444
                cadTool.initializeStatus();
445
                int ret = cadTool.transition(null, editableFeatureSource, selection, new double[0]);
446
                if ((ret & Automaton.AUTOMATON_FINISHED) == Automaton.AUTOMATON_FINISHED) {
447
                        popCadTool();
448

    
449
                        if (cadToolStack.isEmpty()) {
450
                                pushCadTool(new SelectionCadTool());
451
                                PluginServices.getMainFrame().selectTool("selection");
452
                        }
453

    
454
                        askQuestion();
455

    
456
                        getMapControl().drawMap(false);
457
                }
458
        }
459

    
460
        /**
461
         * DOCUMENT ME!
462
         */
463
        public void popCadTool() {
464
                cadToolStack.pop();
465
        }
466

    
467
        /**
468
         * DOCUMENT ME!
469
         */
470
        private void askQuestion() {
471
                CadTool cadtool=(CadTool) cadToolStack.peek();
472
                if (cadtool.getAutomaton().getStatus()==0){
473
                        PluginServices.getMainFrame().addTextToConsole("\n" +cadtool.getName());
474
                }
475
                PluginServices.getMainFrame().addTextToConsole("\n" + cadtool.getQuestion());
476
                questionAsked = true;
477
                
478
        }
479

    
480
        /**
481
         * DOCUMENT ME!
482
         *
483
         * @param cadTool DOCUMENT ME!
484
         */
485
        public void setCadTool(CadTool cadTool) {
486
                cadToolStack.clear();
487
                pushCadTool(cadTool);
488
                //if (console!=null)
489
                askQuestion();
490
        }
491

    
492
        /**
493
         * DOCUMENT ME!
494
         *
495
         * @return DOCUMENT ME!
496
         */
497
        public EditableFeatureSource getEditableFeatureSource() {
498
                return editableFeatureSource;
499
        }
500

    
501
        /**
502
         * DOCUMENT ME!
503
         *
504
         * @param editableFeatureSource DOCUMENT ME!
505
         * @param selection DOCUMENT ME!
506
         */
507
        public void setEditableFeatureSource(
508
                EditableFeatureSource editableFeatureSource, FBitSet selection) {
509
                this.editableFeatureSource = editableFeatureSource;
510
                this.selection = selection;
511
        }
512

    
513
        /**
514
         * DOCUMENT ME!
515
         *
516
         * @param console
517
         */
518
        /*public void setConsole(JConsole console) {
519
                this.console = console;
520
        }
521
*/
522
        /**
523
         * DOCUMENT ME!
524
         *
525
         * @return DOCUMENT ME!
526
         */
527
        public CadMapControl getCadMapControl() {
528
                return cadMapControl;
529
        }
530

    
531
        /**
532
         * DOCUMENT ME!
533
         *
534
         * @param cadMapControl DOCUMENT ME!
535
         */
536
        public void setCadMapControl(CadMapControl cadMapControl) {
537
                this.cadMapControl = cadMapControl;
538
        }
539

    
540
        /**
541
         * Elimina las geometr?as seleccionadas actualmente
542
         */
543
        private void delete() {
544
                editableFeatureSource.startComplexGeometry();
545
                try {
546
                for (int i = selection.nextSetBit(0); i >= 0;
547
                                i = selection.nextSetBit(i + 1)) {
548
                                editableFeatureSource.removeGeometry(i);
549
                }
550
                } catch (DriverIOException e) {
551
                        e.printStackTrace();
552
                } catch (IOException e) {
553
                        e.printStackTrace();
554
                } finally {
555
                try {
556
                        editableFeatureSource.endComplexGeometry();
557
                } catch (IOException e1) {
558
                        e1.printStackTrace();
559
                } catch (DriverIOException e1) {
560
                        e1.printStackTrace();
561
                }
562
                }
563
                selection.clear();
564
                refresh();
565
                //getCadMapControl().getMapControl().drawMap(false);
566
        }
567

    
568
        /**
569
         * @param b
570
         */
571
        public void setAdjustGrid(boolean b) {
572
                grid.setAdjustGrid(b);
573
        }
574

    
575
        /**
576
         * @param actionCommand
577
         */
578
        public void keyPressed(String actionCommand) {
579
                if (actionCommand.equals("eliminar")){
580
                        delete();
581
                }else if (actionCommand.equals("escape")) {
582
                                cadToolStack.clear();
583
                                pushCadTool(new SelectionCadTool());
584
                                selection.clear();
585
                                getMapControl().drawMap(false);
586
                                PluginServices.getMainFrame().selectTool("selection");
587
                                askQuestion();
588
                } 
589
                PluginServices.getMainFrame().enableControls();
590
        }
591
        public abstract AffineTransform getAT();
592
        public abstract double toDistance(int i);
593
        public FBitSet getSelection(){
594
                return selection;
595
        }
596
        public abstract void refresh();
597
        public abstract Point2D toPoint(Point2D p);
598
        public abstract Rectangle2D getExtent();
599
        public abstract double fromDistance(double d);
600
        public abstract Point2D fromPoint(Point2D p);
601
        public void setGridSizeX(double i1){
602
                grid.setGridSizeX(i1);
603
        }
604
        public void setGridSizeY(double i1){
605
                grid.setGridSizeY(i1);
606
        }
607
        public double getGridSizeX(){
608
                return grid.getGridSizeX();
609
        }
610
        public double getGridSizeY(){
611
                return grid.getGridSizeY();
612
        }
613
        public Point2D getAdjustedPoint(){
614
                return adjustedPoint;
615
        }
616
}