Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / toolListeners / ZoomPixelCursorListener.java @ 20100

History | View | Annotate | Download (5.81 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 com.iver.cit.gvsig.project.documents.view.toolListeners;
42

    
43
import java.awt.Cursor;
44
import java.awt.Image;
45
import java.awt.Point;
46
import java.awt.Toolkit;
47
import java.awt.geom.Point2D;
48
import java.awt.geom.Rectangle2D;
49
import java.util.ArrayList;
50

    
51
import javax.swing.ImageIcon;
52

    
53
import org.apache.log4j.Logger;
54

    
55
import com.iver.cit.gvsig.fmap.MapControl;
56
import com.iver.cit.gvsig.fmap.ViewPort;
57
import com.iver.cit.gvsig.fmap.layers.FLayer;
58
import com.iver.cit.gvsig.fmap.layers.FLyrRaster;
59
import com.iver.cit.gvsig.fmap.tools.BehaviorException;
60
import com.iver.cit.gvsig.fmap.tools.Events.PointEvent;
61
import com.iver.cit.gvsig.fmap.tools.Listeners.PointListener;
62

    
63

    
64
/**
65
 * <p>Listener for doing a <i>zoom in</i> operation, centering the extent of the <code>ViewPort</code>
66
 *  of the associated {@link MapControl MapControl} around the pixel selected by the <code>PointEvent</code>'s
67
 *  point coordinates.</p>
68
 *
69
 * <p>Listens a single click of any mouse's button.</p>
70
 *
71
 * @author Nacho Brodin (brodin_ign@gva.es)
72
 */
73
public class ZoomPixelCursorListener implements PointListener {
74
        /**
75
         * Object used to log messages for this listener.
76
         */
77
        private static Logger         logger = Logger.getLogger(ZoomPixelCursorListener.class.getName());
78
        /**
79
         * Object used to log messages for this listener.
80
         */
81
        private final Image         img = new ImageIcon(MapControl.class.getResource(
82
                                                                            "images/ZoomPixelCursor.gif")).getImage();
83

    
84
        /**
85
         * The cursor used to work with this tool listener.
86
         * 
87
         * @see #getCursor()
88
         */
89
        private Cursor                         cur = Toolkit.getDefaultToolkit().createCustomCursor(        img,
90
                                                                                                                                                                        new Point(16, 16), "");
91

    
92
        /**
93
         * Reference to the <code>MapControl</code> object that uses.
94
         */
95
        private MapControl                 mapCtrl;
96

    
97
        /**
98
          * <p>Creates a new <code>ZoomPixelCursorListener</code> object.</p>
99
         *
100
         * @param mc the <code>MapControl</code> where will be applied the changes
101
         */
102
        public ZoomPixelCursorListener(MapControl mc) {
103
                this.mapCtrl = mc;
104
        }
105

    
106
        /*
107
         * (non-Javadoc)
108
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.PointListener#point(com.iver.cit.gvsig.fmap.tools.Events.PointEvent)
109
         */
110
        public void point(PointEvent event) throws BehaviorException {
111
                Point2D pReal = mapCtrl.getMapContext().getViewPort().toMapPoint(event.getPoint());
112
                Point imagePoint = new Point((int) event.getPoint().getX(), (int) event.getPoint().getY());
113
                ViewPort v = mapCtrl.getMapContext().getViewPort();
114

    
115
                FLayer[] actives = mapCtrl.getMapContext().getLayers().getActives();
116
                Rectangle2D ext = null;
117
                try{
118
                        ext = actives[0].getFullExtent();
119
                } catch (com.iver.cit.gvsig.fmap.DriverException e1) {
120
                        e1.printStackTrace();
121
                }
122

    
123
            ArrayList attr = ((FLyrRaster)actives[0]).getSource().getAttributes();
124
            int width = 0, height = 0;
125
                   for (int i=0; i<attr.size(); i++) {
126
                        Object[] a = (Object []) attr.get(i);
127
                        if (a[0].toString().equals("Width"))
128
                                width = ((Integer)a[1]).intValue();
129
                        if (a[0].toString().equals("Height"))
130
                                height = ((Integer)a[1]).intValue();
131
                }
132

    
133

    
134
                   if(        ext != null &&
135
                           width != 0 &&
136
                        height != 0){
137

    
138
                double wcOriginCenterX = pReal.getX();
139
                double wcOriginCenterY = pReal.getY();
140

    
141
                //Hallamos la relaci?n entre el pixel y las WC a partir de la imagen de la capa
142
                        double relacionPixelWcWidth =  (ext.getMaxX() - ext.getMinX())/width;
143
                        double relacionPixelWcHeight = (ext.getMaxY() - ext.getMinY())/height;
144
                        double desplazamientoX = ext.getMinX();
145
                        double desplazamientoY = ext.getMinY();
146

    
147
                        double wcOriginX = wcOriginCenterX - ((v.getImageWidth()*relacionPixelWcWidth)/2);
148
                        double wcOriginY = wcOriginCenterY - ((v.getImageHeight()*relacionPixelWcHeight)/2);
149

    
150
                        double wcDstMinX = wcOriginX;
151
                        double wcDstMinY = wcOriginY;
152
                        double wcDstMaxX = wcDstMinX + (v.getImageWidth()*relacionPixelWcWidth);
153
                        double wcDstMaxY = wcDstMinY + (v.getImageHeight()*relacionPixelWcHeight);
154

    
155
                        double wcDstWidth = wcDstMaxX - wcDstMinX;
156
                        double wcDstHeight = wcDstMaxY - wcDstMinY;
157

    
158
                        ext = new Rectangle2D.Double(wcDstMinX, wcDstMinY, wcDstWidth, wcDstHeight);
159
                mapCtrl.getMapContext().getViewPort().setExtent(ext);
160
                   }
161
        }
162

    
163
        /*
164
         * (non-Javadoc)
165
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
166
         */
167
        public Cursor getCursor() {
168
                return cur;
169
        }
170

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

    
179
        /*
180
         * (non-Javadoc)
181
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.PointListener#pointDoubleClick(com.iver.cit.gvsig.fmap.tools.Events.PointEvent)
182
         */
183
        public void pointDoubleClick(PointEvent event) throws BehaviorException {
184
                // TODO Auto-generated method stub
185

    
186
        }
187
}