svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.fmap.control / src / main / java / org / gvsig / fmap / mapcontrol / tools / ZoomInListenerImpl.java @ 43510
History | View | Annotate | Download (5.46 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | 40435 | jjdelcerro | *
|
4 | 40559 | jjdelcerro | * Copyright (C) 2007-2013 gvSIG Association.
|
5 | 40435 | jjdelcerro | *
|
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 | 40559 | jjdelcerro | * as published by the Free Software Foundation; either version 3
|
9 | 40435 | jjdelcerro | * 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 | 40559 | jjdelcerro | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 | * MA 02110-1301, USA.
|
||
20 | 40435 | jjdelcerro | *
|
21 | 40559 | jjdelcerro | * For any additional information, do not hesitate to contact us
|
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | 40435 | jjdelcerro | */
|
24 | package org.gvsig.fmap.mapcontrol.tools; |
||
25 | |||
26 | import java.awt.Image; |
||
27 | import java.awt.geom.Rectangle2D; |
||
28 | |||
29 | import org.gvsig.fmap.IconThemeHelper; |
||
30 | import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
||
31 | import org.gvsig.fmap.geom.GeometryLocator; |
||
32 | import org.gvsig.fmap.geom.GeometryManager; |
||
33 | import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
||
34 | import org.gvsig.fmap.geom.primitive.Envelope; |
||
35 | import org.gvsig.fmap.mapcontext.MapContext; |
||
36 | import org.gvsig.fmap.mapcontext.ViewPort; |
||
37 | import org.gvsig.fmap.mapcontrol.MapControl; |
||
38 | import org.gvsig.fmap.mapcontrol.tools.Events.EnvelopeEvent; |
||
39 | import org.gvsig.fmap.mapcontrol.tools.Listeners.RectangleListener; |
||
40 | import org.slf4j.Logger; |
||
41 | import org.slf4j.LoggerFactory; |
||
42 | |||
43 | |||
44 | |||
45 | /**
|
||
46 | * <p>Listener for doing a <i>zoom in</i> operation of the extent of the <code>ViewPort</code> of the associated {@link MapControl MapControl}
|
||
47 | * object, defining a rectangular area.</p>
|
||
48 | *
|
||
49 | * <p>If the area defined is smaller than 3 pixels x 3 pixels holds the zoom, otherwise, calculates the new extent <i>r</i>
|
||
50 | * with this equations:
|
||
51 | * <code><br>
|
||
52 | * double factor = 1/MapContext.ZOOMINFACTOR;<br>
|
||
53 | * Rectangle2D rect = event.getWorldCoordRect();<br>
|
||
54 | * Rectangle2D.Double r = new Rectangle2D.Double();<br>
|
||
55 | * ViewPort vp = mapCtrl.getMapContext().getViewPort();<br>
|
||
56 | * double nuevoX = rect.getMaxX() - ((vp.getExtent().getWidth() * factor) / 2.0);<br>
|
||
57 | * double nuevoY = rect.getMaxY() - ((vp.getExtent().getHeight() * factor) / 2.0);<br>
|
||
58 | * Rectangle2D.Double r; // This will be the new extent<br>
|
||
59 | * r.x = nuevoX;<br>
|
||
60 | * r.y = nuevoY;<br>
|
||
61 | * r.width = vp.getExtent().getWidth() * factor;<br>
|
||
62 | * r.height = vp.getExtent().getHeight() * factor;<br>
|
||
63 | * vp.setExtent(r);
|
||
64 | * </code>
|
||
65 | * </p>
|
||
66 | *
|
||
67 | * <p>The ultimately extent will be an adaptation from that, calculated by the <code>ViewPort</code>
|
||
68 | * bearing in mind the ratio of the available rectangle where display the graphical information.</p>
|
||
69 | *
|
||
70 | * @see MapContext#ZOOMINFACTOR
|
||
71 | * @see ViewPort#setEnvelope(Envelope)
|
||
72 | * @see ZoomOutListenerImpl
|
||
73 | * @see ZoomOutRightButtonListener
|
||
74 | *
|
||
75 | * @author Vicente Caballero Navarro
|
||
76 | */
|
||
77 | public class ZoomInListenerImpl implements RectangleListener { |
||
78 | private static final GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
||
79 | private static final Logger logger = LoggerFactory.getLogger(ZoomInListenerImpl.class); |
||
80 | |||
81 | /**
|
||
82 | * The image to display when the cursor is active.
|
||
83 | */
|
||
84 | // private final Image izoomin;
|
||
85 | |||
86 | /**
|
||
87 | * Reference to the <code>MapControl</code> object that uses.
|
||
88 | */
|
||
89 | private MapControl mapCtrl;
|
||
90 | |||
91 | |||
92 | /**
|
||
93 | *
|
||
94 | */
|
||
95 | public ZoomInListenerImpl() {
|
||
96 | // this.izoomin = PluginServices.getIconTheme().get("cursor-zoom-in").getImage();
|
||
97 | } |
||
98 | |||
99 | /**
|
||
100 | * <p>Creates a new <code>ZoomInListenerImpl</code> object.</p>
|
||
101 | *
|
||
102 | * @param mapCtrl the <code>MapControl</code> where is defined the rectangle
|
||
103 | */
|
||
104 | public ZoomInListenerImpl(MapControl mapCtrl) {
|
||
105 | this();
|
||
106 | this.mapCtrl = mapCtrl;
|
||
107 | } |
||
108 | |||
109 | /*
|
||
110 | * (non-Javadoc)
|
||
111 | * @see com.iver.cit.gvsig.fmap.tools.Listeners.RectangleListener#rectangle(com.iver.cit.gvsig.fmap.tools.Events.RectangleEvent)
|
||
112 | */
|
||
113 | public void rectangle(EnvelopeEvent event) { |
||
114 | Envelope rect = event.getWorldCoordRect(); |
||
115 | Rectangle2D pixelRect = event.getPixelCoordRect();
|
||
116 | |||
117 | |||
118 | ViewPort vp = mapCtrl.getMapContext().getViewPort(); |
||
119 | |||
120 | if ((pixelRect.getWidth() < 3) && (pixelRect.getHeight() < 3)) |
||
121 | { |
||
122 | if (vp.getExtent()!=null){ |
||
123 | double factor = 1/MapContext.ZOOMINFACTOR; |
||
124 | double x = rect.getMaximum(0) - |
||
125 | ((vp.getExtent().getWidth() * factor) / 2.0);
|
||
126 | double y = rect.getMaximum(1) - |
||
127 | ((vp.getExtent().getHeight() * factor) / 2.0);
|
||
128 | double width = vp.getExtent().getWidth() * factor;
|
||
129 | double height = vp.getExtent().getHeight() * factor;
|
||
130 | Envelope r; |
||
131 | try {
|
||
132 | r = geomManager.createEnvelope(x, y, x + width, y + height, SUBTYPES.GEOM2D); |
||
133 | vp.setEnvelope(r); |
||
134 | } catch (CreateEnvelopeException e) {
|
||
135 | logger.error("Error creating the envelope", e);
|
||
136 | } |
||
137 | |||
138 | } |
||
139 | } |
||
140 | else
|
||
141 | { |
||
142 | vp.setEnvelope(event.getWorldCoordRect()); |
||
143 | } |
||
144 | } |
||
145 | |||
146 | /*
|
||
147 | * (non-Javadoc)
|
||
148 | * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getImageCursor()
|
||
149 | */
|
||
150 | public Image getImageCursor() { |
||
151 | return IconThemeHelper.getImage("cursor-zoom-in"); |
||
152 | } |
||
153 | |||
154 | /*
|
||
155 | * (non-Javadoc)
|
||
156 | * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
|
||
157 | */
|
||
158 | public boolean cancelDrawing() { |
||
159 | logger.debug("cancelDrawing true");
|
||
160 | return true; |
||
161 | } |
||
162 | } |