Statistics
| Revision:

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

History | View | Annotate | Download (13.8 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
        }
374

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

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

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

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

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

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

    
432
        /**
433
         * DOCUMENT ME!
434
         *
435
         * @param cadTool DOCUMENT ME!
436
         */
437
        public void pushCadTool(CadTool cadTool) {
438
                cadToolStack.push(cadTool);
439
                cadTool.setCadToolAdapter(this);
440
                cadTool.initializeStatus();
441
                cadTool.transition(null, editableFeatureSource, selection, new double[0]);
442
        }
443

    
444
        /**
445
         * DOCUMENT ME!
446
         */
447
        public void popCadTool() {
448
                console.cancelQuestion();
449
                cadToolStack.pop();
450
        }
451

    
452
        /**
453
         * DOCUMENT ME!
454
         */
455
        private void askQuestion() {
456
                console.askQuestion(((CadTool) cadToolStack.peek()).getQuestion(),
457
                        new ResponseListener() {
458
                                public void acceptResponse(String response,
459
                                        boolean userCancelled) throws InvalidResponseException {
460
                                        if (response != null) {
461
                                                if ("".equals(response)) {
462
                                                        transition("aceptar");
463
                                                }
464

    
465
                                                textEntered(response);
466
                                        } else if (userCancelled) {
467
                                                transition("cancel");
468
                                        }
469
                                }
470
                        });
471
                questionAsked = true;
472
        }
473

    
474
        /**
475
         * DOCUMENT ME!
476
         *
477
         * @param cadTool DOCUMENT ME!
478
         */
479
        public void setCadTool(CadTool cadTool) {
480
                cadToolStack.clear();
481
                pushCadTool(cadTool);
482
                askQuestion();
483
        }
484

    
485
        /**
486
         * DOCUMENT ME!
487
         *
488
         * @return DOCUMENT ME!
489
         */
490
        public EditableFeatureSource getEditableFeatureSource() {
491
                return editableFeatureSource;
492
        }
493

    
494
        /**
495
         * DOCUMENT ME!
496
         *
497
         * @param editableFeatureSource DOCUMENT ME!
498
         * @param selection DOCUMENT ME!
499
         */
500
        public void setEditableFeatureSource(
501
                EditableFeatureSource editableFeatureSource, FBitSet selection) {
502
                this.editableFeatureSource = editableFeatureSource;
503
                this.selection = selection;
504
        }
505

    
506
        /**
507
         * DOCUMENT ME!
508
         *
509
         * @param console
510
         */
511
        public void setConsole(JConsole console) {
512
                this.console = console;
513
        }
514

    
515
        /**
516
         * DOCUMENT ME!
517
         *
518
         * @return DOCUMENT ME!
519
         */
520
        public CadMapControl getCadMapControl() {
521
                return cadMapControl;
522
        }
523

    
524
        /**
525
         * DOCUMENT ME!
526
         *
527
         * @param cadMapControl DOCUMENT ME!
528
         */
529
        public void setCadMapControl(CadMapControl cadMapControl) {
530
                this.cadMapControl = cadMapControl;
531
        }
532

    
533
        /**
534
         * DOCUMENT ME!
535
         */
536
        public void delete() {
537
                for (int i = selection.nextSetBit(0); i >= 0;
538
                                i = selection.nextSetBit(i + 1)) {
539
                        try {
540
                                editableFeatureSource.removeGeometry(i);
541
                        } catch (DriverIOException e) {
542
                                e.printStackTrace();
543
                        } catch (IOException e) {
544
                                e.printStackTrace();
545
                        }
546
                }
547

    
548
                selection.clear();
549
                getCadMapControl().getMapControl().drawMap(false);
550
        }
551

    
552
        /**
553
         * @param b
554
         */
555
        public void setAdjustGrid(boolean b) {
556
                grid.setAdjustGrid(b);
557
        }
558
}