Statistics
| Revision:

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

History | View | Annotate | Download (14.6 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.awt.geom.Rectangle2D;
11
import java.io.IOException;
12
import java.util.Stack;
13

    
14
import org.apache.log4j.Logger;
15

    
16
import com.iver.andami.PluginServices;
17
import com.iver.cit.gvsig.fmap.ViewPort;
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.core.v02.FConstant;
22
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
23
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
24
import com.iver.cit.gvsig.fmap.edition.EditableFeatureSource;
25
import com.iver.cit.gvsig.fmap.layers.FBitSet;
26
import com.iver.cit.gvsig.fmap.tools.BehaviorException;
27
import com.iver.cit.gvsig.fmap.tools.Behavior.Behavior;
28
import com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener;
29
import com.iver.cit.gvsig.gui.cad.tools.SelectionCadTool;
30
import com.iver.fsac.Automaton;
31
import com.iver.utiles.console.InvalidResponseException;
32
import com.iver.utiles.console.JConsole;
33
import com.iver.utiles.console.ResponseListener;
34

    
35

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

    
47
        //Para pasarle las coordenadas cuando se produce un evento textEntered
48
        private int lastX;
49
        private int lastY;
50
        private FSymbol symbol = new FSymbol(FConstant.SYMBOL_TYPE_POINT, Color.RED);
51
        private JConsole console;
52
        private Point2D mapAdjustedPoint;
53
        private CadMapControl cadMapControl;
54
        private boolean questionAsked = false;
55
        private CadGrid grid = new CadGrid();
56
        private Point adjustedPoint;
57
        
58
        /**
59
         * Pinta de alguna manera especial las geometrias seleccionadas para la
60
         * edici?n. En caso de que el snapping est? activado, pintar? el efecto
61
         * del mismo.
62
         *
63
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#paintComponent(java.awt.Graphics)
64
         */
65
        public void paintComponent(Graphics g) {
66
                super.paintComponent(g);
67

    
68
                /*
69
                   for (int i = selection.nextSetBit(0); i >= 0;
70
                                   i = selection.nextSetBit(i + 1)) {
71
                           try {
72
                                   IGeometry geom = editableFeatureSource.getGeometry(i);
73
                                   Handler[] handlers=geom.getHandlers();
74
                                   geom.draw((Graphics2D) g, getMapControl().getViewPort(), symbol);
75
                                   FGraphicUtilities.DrawHandlers((Graphics2D)g,getMapControl().getViewPort().getAffineTransform(),handlers);
76
                           } catch (IOException e) {
77
                                   e.printStackTrace();
78
                           } catch (DriverIOException e) {
79
                                   e.printStackTrace();
80
                           }
81
                   }
82
                 */
83
                grid.drawGrid(g);
84
                drawCursor(g);
85
                if (adjustedPoint!=null){
86
                Point2D p = getMapControl().getViewPort().toMapPoint((int) adjustedPoint.getX(),
87
                                (int) adjustedPoint.getY());
88
                ((CadTool) cadToolStack.peek()).drawOperation(g, editableFeatureSource,
89
                        selection, p.getX(), p.getY());
90
                }
91
        }
92

    
93
        /**
94
         * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent)
95
         */
96
        public void mouseClicked(MouseEvent e) throws BehaviorException {
97
        }
98

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

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

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

    
119
                        if (mapAdjustedPoint != null) {
120
                                p = mapAdjustedPoint;
121
                        } else {
122
                                p = vp.toMapPoint(adjustedPoint);
123
                        }
124

    
125
                        transition("punto", editableFeatureSource, selection,
126
                                new double[] { p.getX(), p.getY() });
127
                }
128
        }
129

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

    
144
                                Rectangle2D r = new Rectangle2D.Double(mapPoint.getX() - 1,
145
                                                mapPoint.getY() - 1, 2, 2);
146

    
147
                                int[] indexes = editableFeatureSource.getGeometriesIndexes(r);
148
                                
149
                                double min = Double.MAX_VALUE;
150
                                Point2D argmin = null;
151
                                Point2D mapArgmin = null;
152

    
153
                                for (int i = 0; i < indexes.length; i++) {
154
                                        if (!selection.get(indexes[i])) continue;
155
                                        IGeometry geom;
156
                                        geom = editableFeatureSource.getGeometry(indexes[i]);
157

    
158
                                        Handler[] handlers = geom.getHandlers(FGeometry.SELECTHANDLER);
159

    
160
                                        for (int j = 0; j < handlers.length; j++) {
161
                                                Point2D handlerPoint = handlers[j].getPoint();
162
                                                Point2D handlerImagePoint = getMapControl().getViewPort().fromMapPoint(handlerPoint);
163
                                                double dist = handlerImagePoint.distance(point);
164

    
165
                                                if ((dist < SelectionCadTool.tolerance) &&
166
                                                                (dist < min)) {
167
                                                        min = dist;
168
                                                        argmin = handlerImagePoint;
169
                                                        mapArgmin = handlerPoint;
170
                                                }
171
                                        }
172
                                }
173

    
174
                                if (argmin != null) {
175
                                        point.setLocation(argmin);
176

    
177
                                        //Se hace el casting porque no se quiere redondeo
178
                                        point.setLocation((int)argmin.getX(),
179
                                                        (int) argmin.getY());
180
                                        
181
                                        mapHandlerAdjustedPoint.setLocation(mapArgmin);
182
                                        return min;
183
                                }
184
                        } catch (IOException e) {
185
                                e.printStackTrace();
186
                        } catch (DriverIOException e) {
187
                                e.printStackTrace();
188
                        }
189
                }
190

    
191
                return Double.MAX_VALUE;
192
        }
193

    
194
        /**
195
         * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
196
         */
197
        public void mouseReleased(MouseEvent e) throws BehaviorException {
198
        }
199

    
200
        /**
201
         * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
202
         */
203
        public void mouseDragged(MouseEvent e) throws BehaviorException {
204
                lastX = e.getX();
205
                lastY = e.getY();
206

    
207
                calculateSnapPoint(e.getPoint());
208
        }
209

    
210
        /**
211
         * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
212
         */
213
        public void mouseMoved(MouseEvent e) throws BehaviorException {
214
                lastX = e.getX();
215
                lastY = e.getY();
216

    
217
                calculateSnapPoint(e.getPoint());
218

    
219
                getMapControl().repaint();
220
        }
221

    
222
        /**
223
         * DOCUMENT ME!
224
         *
225
         * @param g DOCUMENT ME!
226
         */
227
        private void drawCursor(Graphics g) {
228
                Point p = adjustedPoint;
229
                if (p==null) grid.setViewPort(cadMapControl.getMapControl().getViewPort());
230
                
231
                int size1 = 15;
232
                int size2 = 3;
233
                g.drawLine((int) (p.getX() - size1), (int) (p.getY()),
234
                        (int) (p.getX() + size1), (int) (p.getY()));
235
                g.drawLine((int) (p.getX()), (int) (p.getY() - size1),
236
                        (int) (p.getX()), (int) (p.getY() + size1));
237
                g.drawRect((int) (p.getX() - size2), (int) (p.getY() - size2),
238
                        (int) (size2 * 2), (int) (size2 * 2));
239
        }
240

    
241
        /**
242
         * DOCUMENT ME!
243
         *
244
         * @param point
245
         */
246
        private void calculateSnapPoint(Point point) {
247
                //Se comprueba el ajuste a rejilla
248
                Point gridAdjustedPoint = (Point) point.clone();
249
                double minDistance = grid.adjustToGrid(gridAdjustedPoint);
250
                if (minDistance < Double.MAX_VALUE) {
251
                        adjustedPoint = gridAdjustedPoint;
252
                        mapAdjustedPoint = getMapControl().getViewPort().toMapPoint(adjustedPoint);
253
                }
254
                
255
                //Se comprueba el ajuste a los handlers
256
                Point handlerAdjustedPoint = (Point) point.clone();
257
                Point2D mapPoint = new Point2D.Double();
258
                double distance = adjustToHandler(handlerAdjustedPoint, mapPoint);
259
                if (distance < minDistance) {
260
                        adjustedPoint = handlerAdjustedPoint;
261
                        mapAdjustedPoint = mapPoint;
262
                        minDistance = distance;
263
                }
264

    
265
                //Si no hay ajuste
266
                if (minDistance == Double.MAX_VALUE) {
267
                        adjustedPoint = point;
268
                        mapAdjustedPoint = null;
269
                }
270
        }
271

    
272

    
273
        /**
274
         * @see java.awt.event.MouseWheelListener#mouseWheelMoved(java.awt.event.MouseWheelEvent)
275
         */
276
        public void mouseWheelMoved(MouseWheelEvent e) throws BehaviorException {
277
        }
278

    
279
        /**
280
         * DOCUMENT ME!
281
         *
282
         * @param text DOCUMENT ME!
283
         */
284
        public void textEntered(String text) {
285
                text = text.trim();
286

    
287
                String[] numbers = text.split(",");
288
                double[] values = null;
289

    
290
                try {
291
                        if (numbers.length == 2) {
292
                                //punto
293
                                values = new double[] {
294
                                                Double.parseDouble(numbers[0]),
295
                                                Double.parseDouble(numbers[1])
296
                                        };
297
                                transition("punto", editableFeatureSource, selection, values);
298
                        } else if (numbers.length == 1) {
299
                                //valor
300
                                values = new double[] { Double.parseDouble(numbers[0]) };
301
                                transition("numero", editableFeatureSource, selection, values);
302
                        }
303
                } catch (NumberFormatException e) {
304
                        transition(text, editableFeatureSource, selection, new double[0]);
305
                }
306
        }
307

    
308
        /**
309
         * DOCUMENT ME!
310
         *
311
         * @param text DOCUMENT ME!
312
         */
313
        public void transition(String text) {
314
                transition(text, editableFeatureSource, selection, new double[0]);
315
                configureMenu();
316
        }
317

    
318
        /**
319
         * DOCUMENT ME!
320
         */
321
        private void configureMenu() {
322
                String[] desc = ((CadTool) cadToolStack.peek()).getAutomaton()
323
                                                 .getCurrentTransitionDescriptions();
324
                String[] labels = ((CadTool) cadToolStack.peek()).getAutomaton()
325
                                                   .getCurrentTransitions();
326
                getCadMapControl().clearMenu();
327

    
328
                for (int i = 0; i < desc.length; i++) {
329
                        if (desc[i] != null) {
330
                                getCadMapControl().addMenuEntry(desc[i], labels[i]);
331
                        }
332
                }
333
        }
334

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

    
348
                int ret = ((CadTool) cadToolStack.peek()).transition(text, source, sel,
349
                                values);
350

    
351
                if ((ret & Automaton.AUTOMATON_FINISHED) == Automaton.AUTOMATON_FINISHED) {
352
                        popCadTool();
353

    
354
                        if (cadToolStack.isEmpty()) {
355
                                pushCadTool(new SelectionCadTool());
356
                                PluginServices.getMainFrame().selectTool("selection");
357
                        }
358

    
359
                        askQuestion();
360

    
361
                        getMapControl().drawMap(false);
362
                } else {
363
                        if (((CadTool) cadToolStack.peek()).getAutomaton().checkState('c')) {
364
                                getMapControl().drawMap(false);
365
                        }
366

    
367
                        if (!questionAsked) {
368
                                askQuestion();
369
                        }
370
                }
371

    
372
                configureMenu();
373
                PluginServices.getMainFrame().enableControls();
374
        }
375

    
376
        /**
377
         * DOCUMENT ME!
378
         *
379
         * @param value DOCUMENT ME!
380
         */
381
        public void setGrid(boolean value) {
382
                grid.setUseGrid(value);
383
                grid.setViewPort(cadMapControl.getMapControl().getViewPort());
384
        }
385

    
386
        /**
387
         * DOCUMENT ME!
388
         *
389
         * @param activated DOCUMENT ME!
390
         */
391
        public void setSnapping(boolean activated) {
392
        }
393

    
394
        /**
395
         * DOCUMENT ME!
396
         *
397
         * @param x DOCUMENT ME!
398
         * @param y DOCUMENT ME!
399
         */
400
        public void getSnapPoint(double x, double y) {
401
        }
402

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

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

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

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

    
446
                        if (cadToolStack.isEmpty()) {
447
                                pushCadTool(new SelectionCadTool());
448
                                PluginServices.getMainFrame().selectTool("selection");
449
                        }
450

    
451
                        askQuestion();
452

    
453
                        getMapControl().drawMap(false);
454
                }
455
        }
456

    
457
        /**
458
         * DOCUMENT ME!
459
         */
460
        public void popCadTool() {
461
                console.cancelQuestion();
462
                cadToolStack.pop();
463
        }
464

    
465
        /**
466
         * DOCUMENT ME!
467
         */
468
        private void askQuestion() {
469
                console.askQuestion(((CadTool) cadToolStack.peek()).getQuestion(),
470
                        new ResponseListener() {
471
                                public void acceptResponse(String response,
472
                                        boolean userCancelled) throws InvalidResponseException {
473
                                        if (response != null) {
474
                                                if ("".equals(response)) {
475
                                                        transition("aceptar");
476
                                                }
477

    
478
                                                textEntered(response);
479
                                        } else if (userCancelled) {
480
                                                transition("cancel");
481
                                        }
482
                                }
483
                        });
484
                questionAsked = true;
485
        }
486

    
487
        /**
488
         * DOCUMENT ME!
489
         *
490
         * @param cadTool DOCUMENT ME!
491
         */
492
        public void setCadTool(CadTool cadTool) {
493
                cadToolStack.clear();
494
                pushCadTool(cadTool);
495
                askQuestion();
496
        }
497

    
498
        /**
499
         * DOCUMENT ME!
500
         *
501
         * @return DOCUMENT ME!
502
         */
503
        public EditableFeatureSource getEditableFeatureSource() {
504
                return editableFeatureSource;
505
        }
506

    
507
        /**
508
         * DOCUMENT ME!
509
         *
510
         * @param editableFeatureSource DOCUMENT ME!
511
         * @param selection DOCUMENT ME!
512
         */
513
        public void setEditableFeatureSource(
514
                EditableFeatureSource editableFeatureSource, FBitSet selection) {
515
                this.editableFeatureSource = editableFeatureSource;
516
                this.selection = selection;
517
        }
518

    
519
        /**
520
         * DOCUMENT ME!
521
         *
522
         * @param console
523
         */
524
        public void setConsole(JConsole console) {
525
                this.console = console;
526
        }
527

    
528
        /**
529
         * DOCUMENT ME!
530
         *
531
         * @return DOCUMENT ME!
532
         */
533
        public CadMapControl getCadMapControl() {
534
                return cadMapControl;
535
        }
536

    
537
        /**
538
         * DOCUMENT ME!
539
         *
540
         * @param cadMapControl DOCUMENT ME!
541
         */
542
        public void setCadMapControl(CadMapControl cadMapControl) {
543
                this.cadMapControl = cadMapControl;
544
        }
545

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

    
573
        /**
574
         * @param b
575
         */
576
        public void setAdjustGrid(boolean b) {
577
                grid.setAdjustGrid(b);
578
        }
579

    
580
        /**
581
         * @param actionCommand
582
         */
583
        public void keyPressed(String actionCommand) {
584
                if (actionCommand.equals("eliminar")){
585
                        delete();
586
                }else if (actionCommand.equals("escape")){
587
                        transition("cancel");
588
                }
589
                PluginServices.getMainFrame().enableControls();
590
        }
591
}