Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.fmap.control / src / main / java / org / gvsig / fmap / mapcontrol / tools / Behavior / Behavior.java @ 41964

History | View | Annotate | Download (11.3 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.fmap.mapcontrol.tools.Behavior;
25

    
26
import java.awt.Image;
27
import java.awt.event.MouseEvent;
28
import java.awt.event.MouseWheelEvent;
29
import java.awt.geom.Point2D;
30
import java.awt.image.BufferedImage;
31
import javax.swing.SwingUtilities;
32

    
33
import org.gvsig.fmap.dal.feature.exception.CreateGeometryException;
34
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
35
import org.gvsig.fmap.geom.Geometry.TYPES;
36
import org.gvsig.fmap.geom.GeometryLocator;
37
import org.gvsig.fmap.geom.GeometryManager;
38
import org.gvsig.fmap.geom.primitive.Arc;
39
import org.gvsig.fmap.geom.primitive.Circle;
40
import org.gvsig.fmap.geom.primitive.Curve;
41
import org.gvsig.fmap.geom.primitive.GeneralPathX;
42
import org.gvsig.fmap.geom.primitive.Point;
43
import org.gvsig.fmap.mapcontrol.MapControl;
44
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
45
import org.gvsig.fmap.mapcontrol.tools.BehaviorException;
46
import org.gvsig.fmap.mapcontrol.tools.Listeners.ToolListener;
47
import org.slf4j.Logger;
48
import org.slf4j.LoggerFactory;
49

    
50

    
51

    
52
/**
53
 * <p>When user is working with a tool on a {@link MapControl MapControl} instance, <code>Behavior</code> defines
54
 *  the basic ways of interacting: selecting a point, a circle, a rectangle, or ...</p>
55
 *
56
 * <p>All events generated will be <code>MouseEvent</code>, and will depend on the nature of the
57
 *  <i>behavior</i>, like the kind of tool for applying the changes.</p>
58
 *
59
 * <p><code>Behavior</code> defines the common and basic functionality for all kinds of interacting ways
60
 *  with the <code>MapControl</code> object.</p>
61
 *
62
 * @see IBehavior
63
 *
64
 * @author Luis W. Sevilla
65
 */
66
public abstract class Behavior implements IBehavior {
67
        public static final int LEFT = 0;
68
        public static final int MIDDLE = 1;
69
        public static final int RIGHT = 2;
70
        
71
        /**
72
         * Reference to the <code>MapControl</code> object that uses.
73
         *
74
         * @see #getMapControl()
75
         * @see #setMapControl(MapControl)
76
         */
77
        private MapControl mapControl;
78
        
79
    private static final Logger LOG = LoggerFactory.getLogger(Behavior.class);
80

    
81
        protected GeometryManager geomManager = GeometryLocator.getGeometryManager();
82
        
83
        private int mouseButton = LEFT;
84
        
85
        public Behavior() {
86
            
87
        }
88
        
89
        public Behavior(int mouseButton) {
90
            this.mouseButton = mouseButton;
91
        }
92
        
93
        /*
94
         * (non-Javadoc)
95
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#getListener()
96
         */
97
        public abstract ToolListener getListener();
98

    
99
        /*
100
         * <p>Draws as much of the associated <code>MapControl</code> image as is currently available. The image
101
         *  is drawn with its top-left corner at (0, 0) in this graphics context's coordinate space. Transparent
102
         *  pixels in the image do not affect whatever pixels are already there.</p>
103
         *
104
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#paintComponent(java.awt.Graphics)
105
         */
106
        public void paintComponent(MapControlDrawer mapControlDrawer) {
107
                BufferedImage img = getMapControl().getImage();
108

    
109
                if (img != null) {
110
                        mapControlDrawer.drawImage(img, 0, 0);
111
                }
112
        }
113

    
114
        /* (non-Javadoc)
115
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#setMapControl(com.iver.cit.gvsig.fmap.MapControl)
116
         */
117
        public void setMapControl(MapControl mc) {
118
                mapControl = mc;
119
        }
120

    
121
        /* (non-Javadoc)
122
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#getImageCursor()
123
         */
124
        public Image getImageCursor(){
125
                return getListener().getImageCursor();
126
        }
127

    
128
        /* (non-Javadoc)
129
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#getMapControl()
130
         */
131
        public MapControl getMapControl() {
132
                return mapControl;
133
        }
134

    
135
        /* (non-Javadoc)
136
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#mouseClicked(java.awt.event.MouseEvent)
137
         */
138
        public void mouseClicked(MouseEvent e) throws BehaviorException {
139
        }
140

    
141
        /* (non-Javadoc)
142
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#mouseEntered(java.awt.event.MouseEvent)
143
         */
144
        public void mouseEntered(MouseEvent e) throws BehaviorException {
145
        }
146

    
147
        /* (non-Javadoc)
148
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#mouseExited(java.awt.event.MouseEvent)
149
         */
150
        public void mouseExited(MouseEvent e) throws BehaviorException {
151
        }
152

    
153
        /* (non-Javadoc)
154
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#mousePressed(java.awt.event.MouseEvent)
155
         */
156
        public void mousePressed(MouseEvent e) throws BehaviorException {
157
        }
158

    
159
        /* (non-Javadoc)
160
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#mouseReleased(java.awt.event.MouseEvent)
161
         */
162
        public void mouseReleased(MouseEvent e) throws BehaviorException {
163
        }
164

    
165
        /* (non-Javadoc)
166
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#mouseDragged(java.awt.event.MouseEvent)
167
         */
168
        public void mouseDragged(MouseEvent e) throws BehaviorException {
169
        }
170

    
171
        /* (non-Javadoc)
172
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#mouseMoved(java.awt.event.MouseEvent)
173
         */
174
        public void mouseMoved(MouseEvent e) throws BehaviorException {
175
        }
176

    
177
        /* (non-Javadoc)
178
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#mouseWheelMoved(java.awt.event.MouseWheelEvent)
179
         */
180
        public void mouseWheelMoved(MouseWheelEvent e) throws BehaviorException {
181
        }
182
        
183
        /**
184
         * Create point. If there is an
185
         * error return <code>null</code> and add the error
186
         * to the log
187
         * @param x
188
         * The X coordinate
189
         * @param y
190
         * The y coordinate
191
         * @return
192
         * The Point
193
         */
194
        protected Point createPoint(double x, double y){
195
                Point point = null;
196
                try {
197
                        point = (Point)geomManager.create(TYPES.POINT, SUBTYPES.GEOM2D);
198
                        point.setX(x);
199
                        point.setY(y);
200
                } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
201
            LOG.error("Error creating a point with x=" + x + ", y=" + y,
202
                new CreateGeometryException(TYPES.POINT, SUBTYPES.GEOM2D,
203
                e));
204
                }
205
                return point;
206
        }
207
        
208
        /**
209
         * Create an Arc. If there is an
210
         * error return <code>null</code> and add the error
211
         * to the log
212
         * @param p1
213
         * @param p2
214
         * @param p3
215
         * @return
216
         * The arc
217
         */
218
        protected Arc createArc(Point2D p1, Point2D p2, Point2D p3){
219
                return createArc(createPoint(p1), createPoint(p2), createPoint(p3));
220
        }
221

    
222
        /**
223
         * Create an arc. If there is an
224
         * error return <code>null</code> and add the error
225
         * to the log
226
         * @param p1
227
         * @param p2
228
         * @param p3
229
         * @return
230
         * The arc
231
         */
232
        protected Arc createArc(Point p1, Point p2, Point p3){
233
                Arc arc = null;
234
                try {
235
                        arc = (Arc)geomManager.create(TYPES.ARC, SUBTYPES.GEOM2D);
236
                        arc.setPoints(p1, p2, p3);
237
            } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
238
            LOG.error("Error creating and arc with p1=" + p1 + ", p2=" + p2
239
                + ",p3=" + p3, new CreateGeometryException(TYPES.ARC,
240
                SUBTYPES.GEOM2D, e));
241
        } catch (IllegalArgumentException ex) {
242
            LOG.info("Warning: unable to create arc from points: "
243
                + p1.getX() + " " + p1.getY() + " :: "
244
                + p2.getX() + " " + p2.getY() + " :: "
245
                + p3.getX() + " " + p3.getY());
246
            arc = null;
247
        }
248
            return arc;
249
        }
250
        
251
        /**
252
         * Create a curve point. If there is an
253
         * error return <code>null</code> and add the error
254
         * to the log
255
         * @param p1
256
         * The AWT point
257
         * @return
258
         * The gvSIG point
259
         */
260
        protected Point createPoint(Point2D p1){
261
                return createPoint(p1.getX(), p1.getY());
262
        }
263
        
264

    
265
        /**
266
         * Create an arc. If there is an
267
         * error return <code>null</code> and add the error
268
         * to the log
269
         * @param centerX
270
         * @param centerY
271
         * @param angleStart
272
         * @param angleExtent
273
         * @return
274
         * The arc
275
         */
276
        protected Arc createArc(double centerX, double centerY, double radious,
277
                        double angleStart, double angleExtent){
278
                Arc arc = null;
279
                try {
280
                        arc = (Arc)geomManager.create(TYPES.ARC, SUBTYPES.GEOM2D);
281
                        arc.setPoints(createPoint(centerX, centerY), radious, angleStart, angleExtent);
282
            } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
283
            LOG.error(
284
                "Error creating an arc with centerX=" + centerX + ", centerY="
285
                    + centerY + ", radious=" + radious + ", angleStart="
286
                    + angleStart + ", angleExtent=" + angleExtent,
287
                new CreateGeometryException(TYPES.ARC, SUBTYPES.GEOM2D, e));
288
        } catch (IllegalArgumentException ex) {
289
            LOG.info("Warning: unable to create arc from points.");
290
            arc = null;
291
        }
292
            return arc;
293
        }
294

    
295
        /**
296
         * Create an circle. If there is an
297
         * error return <code>null</code> and add the error
298
         * to the log
299
         * @param centerX
300
         * @param centerY
301
         * @return
302
         * The arc
303
         */
304
        protected Circle createCircle(double centerX, double centerY, double radious){
305
                Circle circle = null;
306
                try {
307
                        circle = (Circle)geomManager.create(TYPES.CIRCLE, SUBTYPES.GEOM2D);
308
                        circle.setPoints(createPoint(centerX, centerY), radious);
309
            } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
310
            LOG.error("Error creating circle with centerX=" + centerX
311
                + ", centerY=" + centerY + ", radious=" + radious,
312
                new CreateGeometryException(TYPES.CIRCLE,
313
                SUBTYPES.GEOM2D, e));
314
                }
315
            
316
            return circle;
317
        }
318

    
319
        /**
320
         * Create a lineString from a GeneralPath. If there is an
321
         * error return <code>null</code> and add the error
322
         * to the log
323
         * @param gpx
324
         * The GeneralPath
325
         * @return
326
         * The LineString
327
         */
328
        protected Curve createLineString(GeneralPathX gpx){
329
                Curve curve = null;
330
                try {
331
                        curve = (Curve)geomManager.create(TYPES.CURVE, SUBTYPES.GEOM2D);
332
                        curve.setGeneralPath(gpx);
333
                } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
334
            LOG.error("Error creating lineString with GeneralPathX=" + gpx,
335
                new CreateGeometryException(TYPES.CURVE, SUBTYPES.GEOM2D,
336
                e));
337
                }
338
                return curve;
339
        }
340
        
341
        
342
        protected boolean isMyButton(MouseEvent e) {
343
            switch(this.mouseButton) {
344
                case LEFT:
345
                default:
346
                    return (SwingUtilities.isLeftMouseButton(e));
347
                case MIDDLE:
348
                    return (SwingUtilities.isMiddleMouseButton(e));
349
                case RIGHT:
350
                    return (SwingUtilities.isRightMouseButton(e));                   
351
            }
352
        }
353
}