Statistics
| Revision:

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

History | View | Annotate | Download (10.6 KB)

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

    
3
import com.iver.andami.PluginServices;
4
import com.iver.cit.gvsig.fmap.MapControl;
5
import com.iver.cit.gvsig.fmap.ViewPort;
6
import com.iver.cit.gvsig.fmap.core.Handler;
7
import com.iver.cit.gvsig.fmap.core.IGeometry;
8
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
9
import com.iver.cit.gvsig.fmap.core.v02.FGraphicUtilities;
10
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
11
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
12
import com.iver.cit.gvsig.fmap.edition.EditableFeatureSource;
13
import com.iver.cit.gvsig.fmap.layers.FBitSet;
14
import com.iver.cit.gvsig.fmap.tools.Behavior.Behavior;
15
import com.iver.cit.gvsig.fmap.tools.BehaviorException;
16
import com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener;
17
import com.iver.cit.gvsig.gui.cad.automaton.Seleccion;
18
import com.iver.cit.gvsig.gui.cad.tools.SelectionCadTool;
19
import com.iver.fsac.Automaton;
20

    
21
import com.iver.utiles.console.InvalidResponseException;
22
import com.iver.utiles.console.JConsole;
23
import com.iver.utiles.console.ResponseListener;
24
import com.vividsolutions.jts.geom.Envelope;
25
import com.vividsolutions.jts.index.quadtree.Quadtree;
26

    
27
import java.awt.AWTException;
28
import java.awt.Color;
29
import java.awt.Cursor;
30
import java.awt.Graphics;
31
import java.awt.Graphics2D;
32
import java.awt.Point;
33
import java.awt.Robot;
34
import java.awt.event.ActionEvent;
35
import java.awt.event.MouseEvent;
36
import java.awt.event.MouseWheelEvent;
37
import java.awt.geom.Point2D;
38
import java.awt.geom.Rectangle2D;
39

    
40
import java.io.IOException;
41
import java.util.Stack;
42

    
43
import javax.swing.JMenuItem;
44
import javax.swing.JPopupMenu;
45
import javax.swing.KeyStroke;
46
import javax.swing.SwingUtilities;
47

    
48
import org.apache.log4j.Logger;
49

    
50

    
51
/**
52
 * DOCUMENT ME!
53
 *
54
 * @author Fernando Gonz?lez Cort?s
55
 */
56
public class CADToolAdapter extends Behavior {
57
        private static Logger logger = Logger.getLogger(CADToolAdapter.class.getName());
58
        
59
        private Stack cadToolStack = new Stack();
60
        private FBitSet selection;
61
        private EditableFeatureSource editableFeatureSource;
62

    
63
        //Para pasarle las coordenadas cuando se produce un evento textEntered
64
        private int lastX;
65
        private int lastY;
66
        private FSymbol symbol = new FSymbol(FConstant.SYMBOL_TYPE_POINT, Color.RED);
67
        private JConsole console;
68
        private boolean grid = false;
69
        private int gridSize = 30;
70
        private Point gridPoint;
71
        private CadMapControl cadMapControl;
72
        private boolean questionAsked = false;
73
        
74
        /**
75
         * Pinta de alguna manera especial las geometrias seleccionadas para la
76
         * edici?n. En caso de que el snapping est? activado, pintar? el efecto
77
         * del mismo.
78
         *
79
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#paintComponent(java.awt.Graphics)
80
         */
81
        public void paintComponent(Graphics g) {
82
                super.paintComponent(g);
83
/*
84
                for (int i = selection.nextSetBit(0); i >= 0;
85
                                i = selection.nextSetBit(i + 1)) {
86
                        try {
87
                                IGeometry geom = editableFeatureSource.getGeometry(i);
88
                                Handler[] handlers=geom.getHandlers();
89
                                geom.draw((Graphics2D) g, getMapControl().getViewPort(), symbol);
90
                                FGraphicUtilities.DrawHandlers((Graphics2D)g,getMapControl().getViewPort().getAffineTransform(),handlers);
91
                        } catch (IOException e) {
92
                                e.printStackTrace();
93
                        } catch (DriverIOException e) {
94
                                e.printStackTrace();
95
                        }
96
                }
97
*/
98
                if (gridPoint != null){
99
                        g.drawLine(gridPoint.x, gridPoint.y, lastX, lastY);
100
                }
101
                Point2D p = getMapControl().getViewPort().toMapPoint(lastX, lastY);
102
                ((CadTool)cadToolStack.peek()).drawOperation(g, editableFeatureSource,selection,p.getX(), p.getY());
103
        }
104

    
105
        /**
106
         * DOCUMENT ME!
107
         *
108
         * @return DOCUMENT ME!
109
         */
110
        public Point getLastMousePosition() {
111
                return new Point(lastX, lastY);
112
        }
113

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

    
120
        /**
121
         * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent)
122
         */
123
        public void mouseEntered(MouseEvent e) throws BehaviorException {
124
        }
125

    
126
        /**
127
         * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent)
128
         */
129
        public void mouseExited(MouseEvent e) throws BehaviorException {
130
        }
131

    
132
        /**
133
         * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent)
134
         */
135
        public void mousePressed(MouseEvent e) throws BehaviorException {
136
                if (e.getButton() == MouseEvent.BUTTON1){
137
                        ViewPort vp = getMapControl().getMapContext().getViewPort();
138
                        Point2D p2;
139
                        if (gridPoint != null){
140
                                p2 = vp.toMapPoint(gridPoint);
141
                        }else{
142
                                p2 = vp.toMapPoint(e.getPoint());
143
                        }
144
        
145
                        transition("punto", editableFeatureSource, selection,
146
                                new double[] { p2.getX(), p2.getY() });
147
                }
148
        }
149

    
150
        /**
151
         * Ajusta un punto de la imagen que se pasa como 
152
         * par?metro al grid si ?ste est? activo y devuelve la distancia
153
         * de un punto al punto ajustado
154
         * 
155
         * @param point
156
         * 
157
         * @return Distancia del punto que se pasa como par?metro al
158
         * punto ajustado
159
         */
160
        private double adjustToGrid(Point point) {
161
                if (grid){
162
                        Point p = new Point(point);
163
                        point.x = (((int)point.getX()+ (gridSize/2))/gridSize) * gridSize;
164
                        point.y = (((int)point.getY()+ (gridSize/2))/gridSize) * gridSize;
165
                        return p.distance(point);
166
                }
167
                return Double.MAX_VALUE;
168
        }
169

    
170
        /**
171
         * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent)
172
         */
173
        public void mouseReleased(MouseEvent e) throws BehaviorException {
174
        }
175

    
176
        /**
177
         * @see java.awt.event.MouseMotionListener#mouseDragged(java.awt.event.MouseEvent)
178
         */
179
        public void mouseDragged(MouseEvent e) throws BehaviorException {
180
                lastX = e.getX();
181
                lastY = e.getY();
182
                
183
                calculateSnapPoint(e.getPoint());
184
        }
185

    
186
        /**
187
         * @see java.awt.event.MouseMotionListener#mouseMoved(java.awt.event.MouseEvent)
188
         */
189
        public void mouseMoved(MouseEvent e) throws BehaviorException {
190
                lastX = e.getX();
191
                lastY = e.getY();
192

    
193
                calculateSnapPoint(e.getPoint());
194
                
195
                getMapControl().repaint();
196
        }
197

    
198
        /**
199
         * @param point
200
         */
201
        private void calculateSnapPoint(Point point) {
202
                double minDistance = adjustToGrid(point);
203
                
204
                if (minDistance < Double.MAX_VALUE){
205
                        gridPoint = point;
206
                }else{
207
                        gridPoint = null;
208
                }
209
        }
210

    
211
        /**
212
         * @see java.awt.event.MouseWheelListener#mouseWheelMoved(java.awt.event.MouseWheelEvent)
213
         */
214
        public void mouseWheelMoved(MouseWheelEvent e) throws BehaviorException {
215
        }
216

    
217
        /**
218
         * DOCUMENT ME!
219
         *
220
         * @param text DOCUMENT ME!
221
         */
222
        public void textEntered(String text) {
223
                text = text.trim();
224
                
225
                String[] numbers = text.split(",");
226
                double[] values = null;
227

    
228
                try {
229
                        if (numbers.length == 2) {
230
                                //punto
231
                                values = new double[] {
232
                                                Double.parseDouble(numbers[0]),
233
                                                Double.parseDouble(numbers[1])
234
                                        };
235
                                transition("punto", editableFeatureSource, selection, values);
236
                        } else if (numbers.length == 1) {
237
                                //valor
238
                                values = new double[] { Double.parseDouble(numbers[0]) };
239
                                transition("numero", editableFeatureSource, selection, values);
240
                        }
241
                } catch (NumberFormatException e) {
242
                        transition(text, editableFeatureSource, selection, new double[0]);
243
                }
244
        }
245

    
246
        public void transition(String text){
247
                transition(text,editableFeatureSource,selection,new double[0]);
248
                configureMenu();
249
        }
250
        
251
        private void configureMenu(){
252
                String[] desc = ((CadTool)cadToolStack.peek()).getAutomaton().getCurrentTransitionDescriptions();
253
                String[] labels = ((CadTool)cadToolStack.peek()).getAutomaton().getCurrentTransitions();
254
                getCadMapControl().clearMenu();
255
                for (int i = 0; i < desc.length; i++) {
256
                        if (desc[i] != null){
257
                                getCadMapControl().addMenuEntry(desc[i], labels[i]);
258
                        }
259
                }
260
        }
261
        
262
        /**
263
         * DOCUMENT ME!
264
         *
265
         * @param text DOCUMENT ME!
266
         * @param source DOCUMENT ME!
267
         * @param sel DOCUMENT ME!
268
         * @param values DOCUMENT ME!
269
         */
270
        private void transition(String text, EditableFeatureSource source,
271
                FBitSet sel, double[] values) {
272
                        questionAsked = false;
273
                        logger.debug(text);
274
                        int ret = ((CadTool)cadToolStack.peek()).transition(text, source, sel, values);
275
        
276
                        if ((ret & Automaton.AUTOMATON_FINISHED) == Automaton.AUTOMATON_FINISHED){
277
                                popCadTool();
278
                                if (cadToolStack.isEmpty()){
279
                                        pushCadTool(new SelectionCadTool());
280
                                        PluginServices.getMainFrame().selectTool("selection");
281
                                }
282
                                askQuestion();
283
                                
284
                                getMapControl().drawMap(false);
285
                        }else{
286
                                if (((CadTool)cadToolStack.peek()).getAutomaton().checkState('c')){
287
                                        getMapControl().drawMap(false);
288
                                }
289
                                if (!questionAsked) askQuestion();
290
                                
291
                        }
292
                configureMenu();
293
        }
294

    
295
        public void setGrid(boolean value){
296
                this.grid = value;
297
        }
298
        
299
        /**
300
         * DOCUMENT ME!
301
         *
302
         * @param activated DOCUMENT ME!
303
         */
304
        public void setSnapping(boolean activated) {
305
        }
306

    
307
        /**
308
         * DOCUMENT ME!
309
         *
310
         * @param x DOCUMENT ME!
311
         * @param y DOCUMENT ME!
312
         */
313
        public void getSnapPoint(double x, double y) {
314
        }
315

    
316
        /**
317
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.Behavior#getListener()
318
         */
319
        public ToolListener getListener() {
320
                return new ToolListener() {
321
                                /**
322
                                 * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
323
                                 */
324
                                public Cursor getCursor() {
325
                                        return null;
326
                                }
327

    
328
                                /**
329
                                 * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
330
                                 */
331
                                public boolean cancelDrawing() {
332
                                        return false;
333
                                }
334
                        };
335
        }
336

    
337
        /**
338
         * DOCUMENT ME!
339
         *
340
         * @return DOCUMENT ME!
341
         */
342
        public CadTool getCadTool() {
343
                return (CadTool)cadToolStack.peek();
344
        }
345
        
346
        public void pushCadTool(CadTool cadTool){
347
                cadToolStack.push(cadTool);
348
                cadTool.setCadToolAdapter(this);
349
                cadTool.initializeStatus();
350
                cadTool.transition(null, editableFeatureSource, selection, new double[0]);
351
        }
352
        
353
        public void popCadTool(){
354
                console.cancelQuestion();
355
                cadToolStack.pop();
356
        }
357
        
358
        private void askQuestion(){
359
                console.askQuestion(((CadTool)cadToolStack.peek()).getQuestion(),
360
                                new ResponseListener() {
361
                                        public void acceptResponse(String response)
362
                                                throws InvalidResponseException {
363
                                                if (response != null) {
364
                                                        textEntered(response);
365
                                                }
366
                                        }
367
                                });
368
                questionAsked = true;
369
        }
370
        
371
        /**
372
         * DOCUMENT ME!
373
         *
374
         * @param cadTool DOCUMENT ME!
375
         */
376
        public void setCadTool(CadTool cadTool) {
377
                cadToolStack.clear();
378
                pushCadTool(cadTool);
379
                askQuestion();
380
        }
381

    
382
        /**
383
         * DOCUMENT ME!
384
         *
385
         * @return DOCUMENT ME!
386
         */
387
        public EditableFeatureSource getEditableFeatureSource() {
388
                return editableFeatureSource;
389
        }
390

    
391
        /**
392
         * DOCUMENT ME!
393
         *
394
         * @param editableFeatureSource DOCUMENT ME!
395
         */
396
        public void setEditableFeatureSource(
397
                EditableFeatureSource editableFeatureSource, FBitSet selection) {
398
                this.editableFeatureSource = editableFeatureSource;
399
                this.selection = selection;
400
        }
401

    
402
        /**
403
         * DOCUMENT ME!
404
         *
405
         * @param console
406
         */
407
        public void setConsole(JConsole console) {
408
                this.console = console;
409
        }
410

    
411
        public CadMapControl getCadMapControl() {
412
                return cadMapControl;
413
        }
414
        public void setCadMapControl(CadMapControl cadMapControl) {
415
                this.cadMapControl = cadMapControl;
416
        }
417
}