Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_controls / src / org / gvsig / fmap / mapcontrol / tools / Behavior / Behavior.java @ 38564

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.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
                }
247
            return arc;
248
        }
249
        
250
        /**
251
         * Create a curve point. If there is an
252
         * error return <code>null</code> and add the error
253
         * to the log
254
         * @param p1
255
         * The AWT point
256
         * @return
257
         * The gvSIG point
258
         */
259
        protected Point createPoint(Point2D p1){
260
                return createPoint(p1.getX(), p1.getY());
261
        }
262
        
263

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

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

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