Statistics
| Revision:

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

History | View | Annotate | Download (5.73 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib??ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package org.gvsig.fmap.mapcontrol.tools;
42

    
43
import java.awt.Image;
44
import java.awt.geom.Rectangle2D;
45

    
46
import org.gvsig.fmap.IconThemeHelper;
47
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
48
import org.gvsig.fmap.geom.GeometryLocator;
49
import org.gvsig.fmap.geom.GeometryManager;
50
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
51
import org.gvsig.fmap.geom.primitive.Envelope;
52
import org.gvsig.fmap.mapcontext.MapContext;
53
import org.gvsig.fmap.mapcontext.ViewPort;
54
import org.gvsig.fmap.mapcontrol.MapControl;
55
import org.gvsig.fmap.mapcontrol.tools.Events.EnvelopeEvent;
56
import org.gvsig.fmap.mapcontrol.tools.Listeners.RectangleListener;
57
import org.slf4j.Logger;
58
import org.slf4j.LoggerFactory;
59

    
60

    
61

    
62
/**
63
 * <p>Listener for doing a <i>zoom in</i> operation of the extent of the <code>ViewPort</code> of the associated {@link MapControl MapControl}
64
 *  object, defining a rectangular area.</p>
65
 *
66
 * <p>If the area defined is smaller than 3 pixels x 3 pixels holds the zoom, otherwise, calculates the new extent <i>r</i>
67
 *  with this equations:
68
 *  <code><br>
69
 *   double factor = 1/MapContext.ZOOMINFACTOR;<br>
70
 *         Rectangle2D rect = event.getWorldCoordRect();<br>
71
 *   Rectangle2D.Double r = new Rectangle2D.Double();<br>
72
 *   ViewPort vp = mapCtrl.getMapContext().getViewPort();<br>
73
 *   double nuevoX = rect.getMaxX() - ((vp.getExtent().getWidth() * factor) / 2.0);<br>
74
 *   double nuevoY = rect.getMaxY() - ((vp.getExtent().getHeight() * factor) / 2.0);<br>
75
 *   Rectangle2D.Double r; // This will be the new extent<br>
76
 *   r.x = nuevoX;<br>
77
 *   r.y = nuevoY;<br>
78
 *   r.width = vp.getExtent().getWidth() * factor;<br>
79
 *   r.height = vp.getExtent().getHeight() * factor;<br>
80
 *   vp.setExtent(r);
81
 *  </code>
82
 * </p>
83
 *
84
 * <p>The ultimately extent will be an adaptation from that, calculated by the <code>ViewPort</code>
85
 *  bearing in mind the ratio of the available rectangle where display the graphical information.</p>
86
 *
87
 * @see MapContext#ZOOMINFACTOR
88
 * @see ViewPort#setEnvelope(Envelope)
89
 * @see ZoomOutListenerImpl
90
 * @see ZoomOutRightButtonListener
91
 *
92
 * @author Vicente Caballero Navarro
93
 */
94
public class ZoomInListenerImpl implements RectangleListener {
95
        private static final GeometryManager geomManager = GeometryLocator.getGeometryManager();
96
        private static final Logger logger = LoggerFactory.getLogger(ZoomInListenerImpl.class);
97
        
98
        /**
99
         * The image to display when the cursor is active.
100
         */
101
//        private final Image izoomin;
102

    
103
        /**
104
         * Reference to the <code>MapControl</code> object that uses.
105
         */
106
        private MapControl mapCtrl;
107

    
108

    
109
    /**
110
     * 
111
     */
112
    public ZoomInListenerImpl() {
113
//        this.izoomin = PluginServices.getIconTheme().get("cursor-zoom-in").getImage();
114
    }
115

    
116
    /**
117
          * <p>Creates a new <code>ZoomInListenerImpl</code> object.</p>
118
         *
119
         * @param mapCtrl the <code>MapControl</code> where is defined the rectangle
120
         */
121
        public ZoomInListenerImpl(MapControl mapCtrl) {
122
            this();
123
                this.mapCtrl = mapCtrl;
124
        }
125

    
126
        /*
127
         * (non-Javadoc)
128
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.RectangleListener#rectangle(com.iver.cit.gvsig.fmap.tools.Events.RectangleEvent)
129
         */
130
        public void rectangle(EnvelopeEvent event) {
131
                Envelope rect = event.getWorldCoordRect();
132
                Rectangle2D pixelRect = event.getPixelCoordRect();
133

    
134

    
135
                ViewPort vp = mapCtrl.getMapContext().getViewPort();
136

    
137
                if ((pixelRect.getWidth() < 3) && (pixelRect.getHeight() < 3))
138
                {
139
                        if (vp.getExtent()!=null){
140
                                double factor = 1/MapContext.ZOOMINFACTOR;
141
                                double x = rect.getMaximum(0) -
142
                                        ((vp.getExtent().getWidth() * factor) / 2.0);
143
                                double y = rect.getMaximum(1) -
144
                                        ((vp.getExtent().getHeight() * factor) / 2.0);
145
                                double width = vp.getExtent().getWidth() * factor;
146
                                double height = vp.getExtent().getHeight() * factor;
147
                                Envelope r;
148
                                try {
149
                                        r = geomManager.createEnvelope(x, y, x + width, y + height, SUBTYPES.GEOM2D);
150
                                        vp.setEnvelope(r);
151
                                } catch (CreateEnvelopeException e) {
152
                                        logger.error("Error creating the envelope", e);
153
                                }
154
                                
155
                        }
156
                }
157
                else
158
                {
159
                    vp.setEnvelope(event.getWorldCoordRect());
160
                }
161
        }
162

    
163
        /*
164
         * (non-Javadoc)
165
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getImageCursor()
166
         */
167
        public Image getImageCursor() {
168
                return IconThemeHelper.getImage("cursor-zoom-in");
169
        }
170

    
171
        /*
172
         * (non-Javadoc)
173
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
174
         */
175
        public boolean cancelDrawing() {
176
            logger.debug("cancelDrawing true");
177
                return true;
178
        }
179
}