svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_controls / src / org / gvsig / fmap / mapcontrol / tools / Behavior / Behavior.java @ 33411
History | View | Annotate | Download (9.6 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.apache.log4j.Logger; |
53 |
import org.gvsig.fmap.dal.feature.exception.CreateGeometryException; |
54 |
import org.gvsig.fmap.geom.GeometryLocator; |
55 |
import org.gvsig.fmap.geom.GeometryManager; |
56 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
57 |
import org.gvsig.fmap.geom.Geometry.TYPES; |
58 |
import org.gvsig.fmap.geom.primitive.Arc; |
59 |
import org.gvsig.fmap.geom.primitive.Circle; |
60 |
import org.gvsig.fmap.geom.primitive.Curve; |
61 |
import org.gvsig.fmap.geom.primitive.GeneralPathX; |
62 |
import org.gvsig.fmap.geom.primitive.Point; |
63 |
import org.gvsig.fmap.mapcontrol.MapControl; |
64 |
import org.gvsig.fmap.mapcontrol.MapControlDrawer; |
65 |
import org.gvsig.fmap.mapcontrol.tools.BehaviorException; |
66 |
import org.gvsig.fmap.mapcontrol.tools.Listeners.ToolListener; |
67 |
|
68 |
|
69 |
|
70 |
/**
|
71 |
* <p>When user is working with a tool on a {@link MapControl MapControl} instance, <code>Behavior</code> defines
|
72 |
* the basic ways of interacting: selecting a point, a circle, a rectangle, or ...</p>
|
73 |
*
|
74 |
* <p>All events generated will be <code>MouseEvent</code>, and will depend on the nature of the
|
75 |
* <i>behavior</i>, like the kind of tool for applying the changes.</p>
|
76 |
*
|
77 |
* <p><code>Behavior</code> defines the common and basic functionality for all kinds of interacting ways
|
78 |
* with the <code>MapControl</code> object.</p>
|
79 |
*
|
80 |
* @see IBehavior
|
81 |
*
|
82 |
* @author Luis W. Sevilla
|
83 |
*/
|
84 |
public abstract class Behavior implements IBehavior { |
85 |
/**
|
86 |
* Reference to the <code>MapControl</code> object that uses.
|
87 |
*
|
88 |
* @see #getMapControl()
|
89 |
* @see #setMapControl(MapControl)
|
90 |
*/
|
91 |
private MapControl mapControl;
|
92 |
|
93 |
protected static Logger logger = Logger.getLogger(Behavior.class |
94 |
.getName()); |
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 |
logger.error(new CreateGeometryException(TYPES.POINT, SUBTYPES.GEOM2D, e));
|
207 |
} |
208 |
return point;
|
209 |
} |
210 |
|
211 |
/**
|
212 |
* Create an Arc. If there is an
|
213 |
* error return <code>null</code> and add the error
|
214 |
* to the log
|
215 |
* @param p1
|
216 |
* @param p2
|
217 |
* @param p3
|
218 |
* @return
|
219 |
* The arc
|
220 |
*/
|
221 |
protected Arc createArc(Point2D p1, Point2D p2, Point2D p3){ |
222 |
return createArc(createPoint(p1), createPoint(p2), createPoint(p3));
|
223 |
} |
224 |
|
225 |
/**
|
226 |
* Create an arc. If there is an
|
227 |
* error return <code>null</code> and add the error
|
228 |
* to the log
|
229 |
* @param p1
|
230 |
* @param p2
|
231 |
* @param p3
|
232 |
* @return
|
233 |
* The arc
|
234 |
*/
|
235 |
protected Arc createArc(Point p1, Point p2, Point p3){ |
236 |
Arc arc = null;
|
237 |
try {
|
238 |
arc = (Arc)geomManager.create(TYPES.ARC, SUBTYPES.GEOM2D); |
239 |
arc.setPoints(p1, p2, p3); |
240 |
} catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
|
241 |
logger.error(new CreateGeometryException(TYPES.ARC, SUBTYPES.GEOM2D, e));
|
242 |
} |
243 |
return arc;
|
244 |
} |
245 |
|
246 |
/**
|
247 |
* Create a curve point. If there is an
|
248 |
* error return <code>null</code> and add the error
|
249 |
* to the log
|
250 |
* @param p1
|
251 |
* The AWT point
|
252 |
* @return
|
253 |
* The gvSIG point
|
254 |
*/
|
255 |
protected Point createPoint(Point2D p1){ |
256 |
return createPoint(p1.getX(), p1.getY());
|
257 |
} |
258 |
|
259 |
|
260 |
/**
|
261 |
* Create an arc. If there is an
|
262 |
* error return <code>null</code> and add the error
|
263 |
* to the log
|
264 |
* @param centerX
|
265 |
* @param centerY
|
266 |
* @param angleStart
|
267 |
* @param angleExtent
|
268 |
* @return
|
269 |
* The arc
|
270 |
*/
|
271 |
protected Arc createArc(double centerX, double centerY, double radious, |
272 |
double angleStart, double angleExtent){ |
273 |
Arc arc = null;
|
274 |
try {
|
275 |
arc = (Arc)geomManager.create(TYPES.ARC, SUBTYPES.GEOM2D); |
276 |
arc.setPoints(createPoint(centerX, centerY), radious, angleStart, angleExtent); |
277 |
} catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
|
278 |
logger.error(new CreateGeometryException(TYPES.ARC, SUBTYPES.GEOM2D, e));
|
279 |
} |
280 |
return arc;
|
281 |
} |
282 |
|
283 |
/**
|
284 |
* Create an circle. If there is an
|
285 |
* error return <code>null</code> and add the error
|
286 |
* to the log
|
287 |
* @param centerX
|
288 |
* @param centerY
|
289 |
* @return
|
290 |
* The arc
|
291 |
*/
|
292 |
protected Circle createCircle(double centerX, double centerY, double radious){ |
293 |
Circle circle = null;
|
294 |
try {
|
295 |
circle = (Circle)geomManager.create(TYPES.CIRCLE, SUBTYPES.GEOM2D); |
296 |
circle.setPoints(createPoint(centerX, centerY), radious); |
297 |
} catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
|
298 |
logger.error(new CreateGeometryException(TYPES.CIRCLE, SUBTYPES.GEOM2D, e));
|
299 |
} |
300 |
|
301 |
return circle;
|
302 |
} |
303 |
|
304 |
/**
|
305 |
* Create a lineString from a GeneralPath. If there is an
|
306 |
* error return <code>null</code> and add the error
|
307 |
* to the log
|
308 |
* @param gpx
|
309 |
* The GeneralPath
|
310 |
* @return
|
311 |
* The LineString
|
312 |
*/
|
313 |
protected Curve createLineString(GeneralPathX gpx){
|
314 |
Curve curve = null;
|
315 |
try {
|
316 |
curve = (Curve)geomManager.create(TYPES.CURVE, SUBTYPES.GEOM2D); |
317 |
curve.setGeneralPath(gpx); |
318 |
} catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
|
319 |
logger.error(new CreateGeometryException(TYPES.CURVE, SUBTYPES.GEOM2D, e));
|
320 |
} |
321 |
return curve;
|
322 |
} |
323 |
} |