Statistics
| Revision:

svn-gvsig-desktop / tags / v2_0_0_Build_2059 / libraries / libFMap_controls / src / org / gvsig / fmap / mapcontrol / tools / Behavior / Behavior.java @ 39308

History | View | Annotate | Download (10.8 KB)

1
/*
2
 * Created on 28-oct-2004
3
 */
4
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
5
 *
6
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
21
 *
22
 * For more information, contact:
23
 *
24
 *  Generalitat Valenciana
25
 *   Conselleria d'Infraestructures i Transport
26
 *   Av. Blasco Ib??ez, 50
27
 *   46010 VALENCIA
28
 *   SPAIN
29
 *
30
 *      +34 963862235
31
 *   gvsig@gva.es
32
 *      www.gvsig.gva.es
33
 *
34
 *    or
35
 *
36
 *   IVER T.I. S.A
37
 *   Salamanca 50
38
 *   46005 Valencia
39
 *   Spain
40
 *
41
 *   +34 963163400
42
 *   dac@iver.es
43
 */
44
package org.gvsig.fmap.mapcontrol.tools.Behavior;
45

    
46
import java.awt.Image;
47
import java.awt.event.MouseEvent;
48
import java.awt.event.MouseWheelEvent;
49
import java.awt.geom.Point2D;
50
import java.awt.image.BufferedImage;
51

    
52
import org.gvsig.fmap.dal.feature.exception.CreateGeometryException;
53
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
54
import org.gvsig.fmap.geom.Geometry.TYPES;
55
import org.gvsig.fmap.geom.GeometryLocator;
56
import org.gvsig.fmap.geom.GeometryManager;
57
import org.gvsig.fmap.geom.primitive.Arc;
58
import org.gvsig.fmap.geom.primitive.Circle;
59
import org.gvsig.fmap.geom.primitive.Curve;
60
import org.gvsig.fmap.geom.primitive.GeneralPathX;
61
import org.gvsig.fmap.geom.primitive.Point;
62
import org.gvsig.fmap.mapcontrol.MapControl;
63
import org.gvsig.fmap.mapcontrol.MapControlDrawer;
64
import org.gvsig.fmap.mapcontrol.tools.BehaviorException;
65
import org.gvsig.fmap.mapcontrol.tools.Listeners.ToolListener;
66
import org.slf4j.Logger;
67
import org.slf4j.LoggerFactory;
68

    
69

    
70

    
71
/**
72
 * <p>When user is working with a tool on a {@link MapControl MapControl} instance, <code>Behavior</code> defines
73
 *  the basic ways of interacting: selecting a point, a circle, a rectangle, or ...</p>
74
 *
75
 * <p>All events generated will be <code>MouseEvent</code>, and will depend on the nature of the
76
 *  <i>behavior</i>, like the kind of tool for applying the changes.</p>
77
 *
78
 * <p><code>Behavior</code> defines the common and basic functionality for all kinds of interacting ways
79
 *  with the <code>MapControl</code> object.</p>
80
 *
81
 * @see IBehavior
82
 *
83
 * @author Luis W. Sevilla
84
 */
85
public abstract class Behavior implements IBehavior {
86
        /**
87
         * Reference to the <code>MapControl</code> object that uses.
88
         *
89
         * @see #getMapControl()
90
         * @see #setMapControl(MapControl)
91
         */
92
        private MapControl mapControl;
93
        
94
    private static final Logger LOG = LoggerFactory.getLogger(Behavior.class);
95

    
96
        protected GeometryManager geomManager = GeometryLocator.getGeometryManager();
97
        
98
        /*
99
         * (non-Javadoc)
100
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#getListener()
101
         */
102
        public abstract ToolListener getListener();
103

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

    
114
                if (img != null) {
115
                        mapControlDrawer.drawImage(img, 0, 0);
116
                }
117
        }
118

    
119
        /* (non-Javadoc)
120
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#setMapControl(com.iver.cit.gvsig.fmap.MapControl)
121
         */
122
        public void setMapControl(MapControl mc) {
123
                mapControl = mc;
124
        }
125

    
126
        /* (non-Javadoc)
127
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#getImageCursor()
128
         */
129
        public Image getImageCursor(){
130
                return getListener().getImageCursor();
131
        }
132

    
133
        /* (non-Javadoc)
134
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#getMapControl()
135
         */
136
        public MapControl getMapControl() {
137
                return mapControl;
138
        }
139

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

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

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

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

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

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

    
176
        /* (non-Javadoc)
177
         * @see com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#mouseMoved(java.awt.event.MouseEvent)
178
         */
179
        public void mouseMoved(MouseEvent e) throws BehaviorException {
180
        }
181

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

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

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

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

    
324
        /**
325
         * Create a lineString from a GeneralPath. If there is an
326
         * error return <code>null</code> and add the error
327
         * to the log
328
         * @param gpx
329
         * The GeneralPath
330
         * @return
331
         * The LineString
332
         */
333
        protected Curve createLineString(GeneralPathX gpx){
334
                Curve curve = null;
335
                try {
336
                        curve = (Curve)geomManager.create(TYPES.CURVE, SUBTYPES.GEOM2D);
337
                        curve.setGeneralPath(gpx);
338
                } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
339
            LOG.error("Error creating lineString with GeneralPathX=" + gpx,
340
                new CreateGeometryException(TYPES.CURVE, SUBTYPES.GEOM2D,
341
                e));
342
                }
343
                return curve;
344
        }
345
}