Statistics
| Revision:

root / tags / v2_0_0_Build_2038 / libraries / libFMap_controls / src / org / gvsig / fmap / mapcontrol / tools / Behavior / Behavior.java @ 37103

History | View | Annotate | Download (10.3 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.slf4j.Logger;
53
import org.slf4j.LoggerFactory;
54

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

    
70

    
71

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
228
        /**
229
         * Create an arc. If there is an
230
         * error return <code>null</code> and add the error
231
         * to the log
232
         * @param p1
233
         * @param p2
234
         * @param p3
235
         * @return
236
         * The arc
237
         */
238
        protected Arc createArc(Point p1, Point p2, Point p3){
239
                Arc arc = null;
240
                try {
241
                        arc = (Arc)geomManager.create(TYPES.ARC, SUBTYPES.GEOM2D);
242
                        arc.setPoints(p1, p2, p3);
243
            } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
244
            LOG.error("Error creating and arc with p1=" + p1 + ", p2=" + p2
245
                + ",p3=" + p3, new CreateGeometryException(TYPES.ARC,
246
                SUBTYPES.GEOM2D, e));
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
                }
289
            return arc;
290
        }
291

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

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