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 @ 43510
History | View | Annotate | Download (13.1 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
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 | 40435 | jjdelcerro | 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 | 41964 | jjdelcerro | import javax.swing.SwingUtilities; |
32 | 40435 | jjdelcerro | |
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.Point; |
||
41 | import org.gvsig.fmap.mapcontrol.MapControl; |
||
42 | import org.gvsig.fmap.mapcontrol.MapControlDrawer; |
||
43 | import org.gvsig.fmap.mapcontrol.tools.BehaviorException; |
||
44 | import org.gvsig.fmap.mapcontrol.tools.Listeners.ToolListener; |
||
45 | import org.slf4j.Logger; |
||
46 | import org.slf4j.LoggerFactory; |
||
47 | |||
48 | /**
|
||
49 | 42036 | fdiaz | * <p>
|
50 | * When user is working with a tool on a {@link MapControl MapControl} instance,
|
||
51 | * <code>Behavior</code> defines the basic ways of interacting: selecting a
|
||
52 | * point, a circle, a rectangle, or ...
|
||
53 | * </p>
|
||
54 | 40435 | jjdelcerro | *
|
55 | 42036 | fdiaz | * <p>
|
56 | * All events generated will be <code>MouseEvent</code>, and will depend on the
|
||
57 | * nature of the <i>behavior</i>, like the kind of tool for applying the
|
||
58 | * changes.
|
||
59 | * </p>
|
||
60 | 40435 | jjdelcerro | *
|
61 | 42036 | fdiaz | * <p>
|
62 | * <code>Behavior</code> defines the common and basic functionality for all
|
||
63 | * kinds of interacting ways with the <code>MapControl</code> object.
|
||
64 | * </p>
|
||
65 | 40435 | jjdelcerro | *
|
66 | * @see IBehavior
|
||
67 | *
|
||
68 | * @author Luis W. Sevilla
|
||
69 | */
|
||
70 | public abstract class Behavior implements IBehavior { |
||
71 | 42036 | fdiaz | |
72 | public static final int BUTTON_LEFT = 1; |
||
73 | public static final int BUTTON_MIDDLE = 2; |
||
74 | public static final int BUTTON_RIGHT = 4; |
||
75 | |||
76 | /**
|
||
77 | * Reference to the <code>MapControl</code> object that uses.
|
||
78 | *
|
||
79 | * @see #getMapControl()
|
||
80 | * @see #setMapControl(MapControl)
|
||
81 | */
|
||
82 | private MapControl mapControl;
|
||
83 | |||
84 | 43510 | jjdelcerro | protected static final Logger LOG = LoggerFactory.getLogger(Behavior.class); |
85 | 40435 | jjdelcerro | |
86 | 43510 | jjdelcerro | protected GeometryManager geomManager = GeometryLocator.getGeometryManager();
|
87 | |||
88 | 42036 | fdiaz | |
89 | private int mouseButton = BUTTON_LEFT; |
||
90 | private boolean isMyButton = false; |
||
91 | |||
92 | public Behavior() {
|
||
93 | |||
94 | } |
||
95 | |||
96 | public Behavior(int mouseButton) { |
||
97 | this.mouseButton = mouseButton;
|
||
98 | } |
||
99 | |||
100 | /*
|
||
101 | * (non-Javadoc)
|
||
102 | *
|
||
103 | * @see com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#getListener()
|
||
104 | */
|
||
105 | public abstract ToolListener getListener(); |
||
106 | |||
107 | /*
|
||
108 | * <p>Draws as much of the associated <code>MapControl</code> image as is
|
||
109 | * currently available. The image
|
||
110 | * is drawn with its top-left corner at (0, 0) in this graphics context's
|
||
111 | * coordinate space. Transparent
|
||
112 | * pixels in the image do not affect whatever pixels are already there.</p>
|
||
113 | *
|
||
114 | * @see
|
||
115 | * com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#paintComponent(java.
|
||
116 | * awt.Graphics)
|
||
117 | */
|
||
118 | public void paintComponent(MapControlDrawer mapControlDrawer) { |
||
119 | } |
||
120 | |||
121 | public void paintComponent(MapControlDrawer mapControlDrawer, boolean clean) { |
||
122 | if (clean){
|
||
123 | clean(mapControlDrawer); |
||
124 | 41964 | jjdelcerro | } |
125 | 42036 | fdiaz | paintComponent(mapControlDrawer); |
126 | } |
||
127 | |||
128 | public void clean(MapControlDrawer mapControlDrawer){ |
||
129 | BufferedImage img = getMapControl().getImage();
|
||
130 | |||
131 | if (img != null) { |
||
132 | mapControlDrawer.drawImage(img, 0, 0); |
||
133 | 41964 | jjdelcerro | } |
134 | 42036 | fdiaz | } |
135 | 40435 | jjdelcerro | |
136 | 42036 | fdiaz | /*
|
137 | * (non-Javadoc)
|
||
138 | *
|
||
139 | * @see
|
||
140 | * com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#setMapControl(com.iver
|
||
141 | * .cit.gvsig.fmap.MapControl)
|
||
142 | */
|
||
143 | public void setMapControl(MapControl mc) { |
||
144 | mapControl = mc; |
||
145 | } |
||
146 | 40435 | jjdelcerro | |
147 | 42036 | fdiaz | /*
|
148 | * (non-Javadoc)
|
||
149 | *
|
||
150 | * @see com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#getImageCursor()
|
||
151 | */
|
||
152 | public Image getImageCursor() { |
||
153 | return getListener().getImageCursor();
|
||
154 | } |
||
155 | 40435 | jjdelcerro | |
156 | 42036 | fdiaz | /*
|
157 | * (non-Javadoc)
|
||
158 | *
|
||
159 | * @see com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#getMapControl()
|
||
160 | */
|
||
161 | public MapControl getMapControl() {
|
||
162 | return mapControl;
|
||
163 | } |
||
164 | 40435 | jjdelcerro | |
165 | 42036 | fdiaz | /*
|
166 | * (non-Javadoc)
|
||
167 | *
|
||
168 | * @see
|
||
169 | * com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#mouseClicked(java.awt
|
||
170 | * .event.MouseEvent)
|
||
171 | */
|
||
172 | public void mouseClicked(MouseEvent e) throws BehaviorException { |
||
173 | } |
||
174 | 40435 | jjdelcerro | |
175 | 42036 | fdiaz | /*
|
176 | * (non-Javadoc)
|
||
177 | *
|
||
178 | * @see
|
||
179 | * com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#mouseEntered(java.awt
|
||
180 | * .event.MouseEvent)
|
||
181 | */
|
||
182 | public void mouseEntered(MouseEvent e) throws BehaviorException { |
||
183 | } |
||
184 | 40435 | jjdelcerro | |
185 | 42036 | fdiaz | /*
|
186 | * (non-Javadoc)
|
||
187 | *
|
||
188 | * @see
|
||
189 | * com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#mouseExited(java.awt
|
||
190 | * .event.MouseEvent)
|
||
191 | */
|
||
192 | public void mouseExited(MouseEvent e) throws BehaviorException { |
||
193 | } |
||
194 | 40435 | jjdelcerro | |
195 | 42036 | fdiaz | /*
|
196 | * (non-Javadoc)
|
||
197 | *
|
||
198 | * @see
|
||
199 | * com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#mousePressed(java.awt
|
||
200 | * .event.MouseEvent)
|
||
201 | */
|
||
202 | public void mousePressed(MouseEvent e) throws BehaviorException { |
||
203 | } |
||
204 | 40435 | jjdelcerro | |
205 | 42036 | fdiaz | /*
|
206 | * (non-Javadoc)
|
||
207 | *
|
||
208 | * @see
|
||
209 | * com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#mouseReleased(java.awt
|
||
210 | * .event.MouseEvent)
|
||
211 | */
|
||
212 | public void mouseReleased(MouseEvent e) throws BehaviorException { |
||
213 | } |
||
214 | 40435 | jjdelcerro | |
215 | 42036 | fdiaz | /*
|
216 | * (non-Javadoc)
|
||
217 | *
|
||
218 | * @see
|
||
219 | * com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#mouseDragged(java.awt
|
||
220 | * .event.MouseEvent)
|
||
221 | */
|
||
222 | public void mouseDragged(MouseEvent e) throws BehaviorException { |
||
223 | } |
||
224 | 40435 | jjdelcerro | |
225 | 42036 | fdiaz | /*
|
226 | * (non-Javadoc)
|
||
227 | *
|
||
228 | * @see
|
||
229 | * com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#mouseMoved(java.awt.
|
||
230 | * event.MouseEvent)
|
||
231 | */
|
||
232 | public void mouseMoved(MouseEvent e) throws BehaviorException { |
||
233 | } |
||
234 | 40435 | jjdelcerro | |
235 | 42036 | fdiaz | /*
|
236 | * (non-Javadoc)
|
||
237 | *
|
||
238 | * @see
|
||
239 | * com.iver.cit.gvsig.fmap.tools.Behavior.IBehavior#mouseWheelMoved(java
|
||
240 | * .awt.event.MouseWheelEvent)
|
||
241 | */
|
||
242 | public void mouseWheelMoved(MouseWheelEvent e) throws BehaviorException { |
||
243 | } |
||
244 | 40435 | jjdelcerro | |
245 | 42036 | fdiaz | /**
|
246 | * Create point. If there is an
|
||
247 | * error return <code>null</code> and add the error
|
||
248 | * to the log
|
||
249 | *
|
||
250 | * @param x
|
||
251 | * The X coordinate
|
||
252 | * @param y
|
||
253 | * The y coordinate
|
||
254 | * @return
|
||
255 | * The Point
|
||
256 | */
|
||
257 | protected Point createPoint(double x, double y) { |
||
258 | Point point = null; |
||
259 | try {
|
||
260 | point = (Point) geomManager.create(TYPES.POINT, SUBTYPES.GEOM2D);
|
||
261 | point.setX(x); |
||
262 | point.setY(y); |
||
263 | } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
|
||
264 | 40435 | jjdelcerro | LOG.error("Error creating a point with x=" + x + ", y=" + y, |
265 | 42036 | fdiaz | new CreateGeometryException(TYPES.POINT, SUBTYPES.GEOM2D, e));
|
266 | } |
||
267 | return point;
|
||
268 | } |
||
269 | 40435 | jjdelcerro | |
270 | 42036 | fdiaz | /**
|
271 | * Create an Arc. If there is an
|
||
272 | * error return <code>null</code> and add the error
|
||
273 | * to the log
|
||
274 | *
|
||
275 | * @param p1
|
||
276 | * @param p2
|
||
277 | * @param p3
|
||
278 | * @return
|
||
279 | * The arc
|
||
280 | */
|
||
281 | protected Arc createArc(Point2D p1, Point2D p2, Point2D p3) { |
||
282 | return createArc(createPoint(p1), createPoint(p2), createPoint(p3));
|
||
283 | } |
||
284 | |||
285 | /**
|
||
286 | * Create an arc. If there is an
|
||
287 | * error return <code>null</code> and add the error
|
||
288 | * to the log
|
||
289 | *
|
||
290 | * @param p1
|
||
291 | * @param p2
|
||
292 | * @param p3
|
||
293 | * @return
|
||
294 | * The arc
|
||
295 | */
|
||
296 | protected Arc createArc(Point p1, Point p2, Point p3) { |
||
297 | Arc arc = null;
|
||
298 | try {
|
||
299 | arc = (Arc) geomManager.create(TYPES.ARC, SUBTYPES.GEOM2D); |
||
300 | arc.setPoints(p1, p2, p3); |
||
301 | } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
|
||
302 | 40435 | jjdelcerro | LOG.error("Error creating and arc with p1=" + p1 + ", p2=" + p2 |
303 | + ",p3=" + p3, new CreateGeometryException(TYPES.ARC, |
||
304 | SUBTYPES.GEOM2D, e)); |
||
305 | } catch (IllegalArgumentException ex) { |
||
306 | 42036 | fdiaz | LOG.info("Warning: unable to create arc from points: " + p1.getX()
|
307 | + " " + p1.getY() + " :: " + p2.getX() + " " + p2.getY() |
||
308 | + " :: " + p3.getX() + " " + p3.getY()); |
||
309 | 40435 | jjdelcerro | arc = null;
|
310 | } |
||
311 | 42036 | fdiaz | return arc;
|
312 | } |
||
313 | 40435 | jjdelcerro | |
314 | 42036 | fdiaz | /**
|
315 | * Create a curve point. If there is an
|
||
316 | * error return <code>null</code> and add the error
|
||
317 | * to the log
|
||
318 | *
|
||
319 | * @param p1
|
||
320 | * The AWT point
|
||
321 | * @return
|
||
322 | * The gvSIG point
|
||
323 | */
|
||
324 | protected Point createPoint(Point2D p1) { |
||
325 | return createPoint(p1.getX(), p1.getY());
|
||
326 | } |
||
327 | |||
328 | /**
|
||
329 | * Create an arc. If there is an
|
||
330 | * error return <code>null</code> and add the error
|
||
331 | * to the log
|
||
332 | *
|
||
333 | * @param centerX
|
||
334 | * @param centerY
|
||
335 | * @param radious
|
||
336 | * @param angleStart
|
||
337 | * @param angleExtent
|
||
338 | * @return The arc
|
||
339 | */
|
||
340 | protected Arc createArc(double centerX, double centerY, double radious, |
||
341 | double angleStart, double angleExtent) { |
||
342 | Arc arc = null;
|
||
343 | try {
|
||
344 | arc = (Arc) geomManager.create(TYPES.ARC, SUBTYPES.GEOM2D); |
||
345 | arc.setPoints(createPoint(centerX, centerY), radious, angleStart, |
||
346 | angleExtent); |
||
347 | } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
|
||
348 | 40435 | jjdelcerro | LOG.error( |
349 | "Error creating an arc with centerX=" + centerX + ", centerY=" |
||
350 | + centerY + ", radious=" + radious + ", angleStart=" |
||
351 | + angleStart + ", angleExtent=" + angleExtent,
|
||
352 | new CreateGeometryException(TYPES.ARC, SUBTYPES.GEOM2D, e));
|
||
353 | } catch (IllegalArgumentException ex) { |
||
354 | LOG.info("Warning: unable to create arc from points.");
|
||
355 | arc = null;
|
||
356 | } |
||
357 | 42036 | fdiaz | return arc;
|
358 | } |
||
359 | 40435 | jjdelcerro | |
360 | 42036 | fdiaz | /**
|
361 | * Create an circle. If there is an
|
||
362 | * error return <code>null</code> and add the error
|
||
363 | * to the log
|
||
364 | *
|
||
365 | * @param centerX
|
||
366 | * @param centerY
|
||
367 | 41974 | jjdelcerro | * @param radious
|
368 | 42036 | fdiaz | * @return
|
369 | * The arc
|
||
370 | */
|
||
371 | protected Circle createCircle(double centerX, double centerY, double radious) { |
||
372 | Circle circle = null;
|
||
373 | try {
|
||
374 | circle = (Circle) geomManager.create(TYPES.CIRCLE, SUBTYPES.GEOM2D); |
||
375 | circle.setPoints(createPoint(centerX, centerY), radious); |
||
376 | } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
|
||
377 | 40435 | jjdelcerro | LOG.error("Error creating circle with centerX=" + centerX
|
378 | + ", centerY=" + centerY + ", radious=" + radious, |
||
379 | 42036 | fdiaz | new CreateGeometryException(TYPES.CIRCLE, SUBTYPES.GEOM2D, e));
|
380 | } |
||
381 | 40435 | jjdelcerro | |
382 | 42036 | fdiaz | return circle;
|
383 | } |
||
384 | |||
385 | 43510 | jjdelcerro | // /**
|
386 | // * Create a lineString from a GeneralPath. If there is an
|
||
387 | // * error return <code>null</code> and add the error
|
||
388 | // * to the log
|
||
389 | // *
|
||
390 | // * @param gpx
|
||
391 | // * The GeneralPath
|
||
392 | // * @return
|
||
393 | // * The LineString
|
||
394 | // */
|
||
395 | // protected Curve createLineString(GeneralPathX gpx) {
|
||
396 | // Curve curve = null;
|
||
397 | // try {
|
||
398 | // curve = (Curve) geomManager.create(TYPES.CURVE, SUBTYPES.GEOM2D);
|
||
399 | // curve.setGeneralPath(gpx);
|
||
400 | // } catch (org.gvsig.fmap.geom.exception.CreateGeometryException e) {
|
||
401 | // LOG.error("Error creating lineString with GeneralPathX=" + gpx,
|
||
402 | // new CreateGeometryException(TYPES.CURVE, SUBTYPES.GEOM2D, e));
|
||
403 | // }
|
||
404 | // return curve;
|
||
405 | // }
|
||
406 | 42036 | fdiaz | |
407 | public void resetMyButton() { |
||
408 | this.isMyButton = false; |
||
409 | } |
||
410 | |||
411 | public boolean isMyButton(MouseEvent e) { |
||
412 | if (e == null) { |
||
413 | 41974 | jjdelcerro | return this.isMyButton; |
414 | } |
||
415 | 42036 | fdiaz | switch (this.mouseButton) { |
416 | case BUTTON_LEFT:
|
||
417 | default:
|
||
418 | this.isMyButton = SwingUtilities.isLeftMouseButton(e); |
||
419 | return this.isMyButton; |
||
420 | case BUTTON_MIDDLE:
|
||
421 | this.isMyButton = SwingUtilities.isMiddleMouseButton(e); |
||
422 | return this.isMyButton; |
||
423 | case BUTTON_RIGHT:
|
||
424 | this.isMyButton = SwingUtilities.isRightMouseButton(e); |
||
425 | return this.isMyButton; |
||
426 | } |
||
427 | } |
||
428 | |||
429 | public boolean isMyButton() { |
||
430 | return this.isMyButton; |
||
431 | } |
||
432 | 40435 | jjdelcerro | } |