Statistics
| Revision:

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

History | View | Annotate | Download (6.14 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
import java.net.URL;
46

    
47
import javax.swing.ImageIcon;
48

    
49
import org.slf4j.Logger;
50
import org.slf4j.LoggerFactory;
51

    
52
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
53
import org.gvsig.fmap.geom.GeometryLocator;
54
import org.gvsig.fmap.geom.GeometryManager;
55
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
56
import org.gvsig.fmap.geom.primitive.Envelope;
57
import org.gvsig.fmap.mapcontext.MapContext;
58
import org.gvsig.fmap.mapcontext.ViewPort;
59
import org.gvsig.fmap.mapcontrol.MapControl;
60
import org.gvsig.fmap.mapcontrol.tools.Events.EnvelopeEvent;
61
import org.gvsig.fmap.mapcontrol.tools.Listeners.RectangleListener;
62

    
63

    
64

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

    
106
        /**
107
         * The cursor used to work with this tool listener.
108
         *
109
         * @see #getCursor()
110
         */
111
//        private Cursor cur = Toolkit.getDefaultToolkit().createCustomCursor(izoomin,
112
//                        new Point(16, 16), "");
113

    
114
        /**
115
         * Reference to the <code>MapControl</code> object that uses.
116
         */
117
        private MapControl mapCtrl;
118

    
119

    
120
    /**
121
     * 
122
     */
123
    public ZoomInListenerImpl() {
124
        URL res = MapControl.class.getResource("images/ZoomInCursor.gif");
125
        if( res == null ) {
126
            this.izoomin = new ImageIcon().getImage();
127
            logger.error("Can't load icon 'images/ZoomInCursor.gif'");
128
        } else {
129
            this.izoomin = new ImageIcon(res).getImage();
130
        }
131
    }
132

    
133
    /**
134
          * <p>Creates a new <code>ZoomInListenerImpl</code> object.</p>
135
         *
136
         * @param mapCtrl the <code>MapControl</code> where is defined the rectangle
137
         */
138
        public ZoomInListenerImpl(MapControl mapCtrl) {
139
            this();
140
                this.mapCtrl = mapCtrl;
141
        }
142

    
143
        /*
144
         * (non-Javadoc)
145
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.RectangleListener#rectangle(com.iver.cit.gvsig.fmap.tools.Events.RectangleEvent)
146
         */
147
        public void rectangle(EnvelopeEvent event) {
148
                Envelope rect = event.getWorldCoordRect();
149
                Rectangle2D pixelRect = event.getPixelCoordRect();
150

    
151

    
152
                ViewPort vp = mapCtrl.getMapContext().getViewPort();
153

    
154
                if ((pixelRect.getWidth() < 3) && (pixelRect.getHeight() < 3))
155
                {
156
                        if (vp.getExtent()!=null){
157
                                double factor = 1/MapContext.ZOOMINFACTOR;
158
                                double x = rect.getMaximum(0) -
159
                                        ((vp.getExtent().getWidth() * factor) / 2.0);
160
                                double y = rect.getMaximum(1) -
161
                                        ((vp.getExtent().getHeight() * factor) / 2.0);
162
                                double width = vp.getExtent().getWidth() * factor;
163
                                double height = vp.getExtent().getHeight() * factor;
164
                                Envelope r;
165
                                try {
166
                                        r = geomManager.createEnvelope(x, y, x + width, y + height, SUBTYPES.GEOM2D);
167
                                        vp.setEnvelope(r);
168
                                } catch (CreateEnvelopeException e) {
169
                                        logger.error("Error creating the envelope", e);
170
                                }
171
                                
172
                        }
173
                }
174
                else
175
                {
176
                    vp.setEnvelope(event.getWorldCoordRect());
177
                }
178
        }
179

    
180
        /*
181
         * (non-Javadoc)
182
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getImageCursor()
183
         */
184
        public Image getImageCursor() {
185
                return izoomin;
186
        }
187

    
188
        /*
189
         * (non-Javadoc)
190
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
191
         */
192
        public boolean cancelDrawing() {
193
            logger.debug("cancelDrawing true");
194
                return true;
195
        }
196
}