Statistics
| Revision:

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

History | View | Annotate | Download (6.41 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.event.InputEvent;
48
import java.awt.event.MouseEvent;
49
import java.awt.geom.Point2D;
50
import java.awt.geom.Rectangle2D;
51

    
52
import javax.swing.ImageIcon;
53

    
54
import com.iver.cit.gvsig.fmap.MapControl;
55
import com.iver.cit.gvsig.fmap.ViewPort;
56
import com.iver.cit.gvsig.fmap.tools.BehaviorException;
57
import com.iver.cit.gvsig.fmap.tools.Events.MoveEvent;
58
import com.iver.cit.gvsig.fmap.tools.Listeners.PanListener;
59
import com.iver.cit.gvsig.project.documents.view.MapOverview;
60

    
61

    
62
/**
63
 * <p>Listener for moving the extent of the associated {@link MapOverview MapOverview} object
64
 *  according the movement between the initial and final points of line determined by the movement
65
 *  dragging with the third button of the mouse.</p>
66
 * 
67
 * <p>Updates the extent of its <code>ViewPort</code> with the new position.</p>
68
 *
69
 * @author Vicente Caballero Navarro
70
 */
71
public class MapOverviewPanListener implements PanListener {
72
        /**
73
         * The image to display when the cursor is active.
74
         */
75
        private final Image icursor = new ImageIcon(MapControl.class.getResource(
76
                                "images/CruxCursor.png")).getImage();
77

    
78
        /**
79
         * The cursor used to work with this tool listener.
80
         * 
81
         * @see #getCursor()
82
         */
83
        private Cursor cur = Toolkit.getDefaultToolkit().createCustomCursor(icursor,
84
                        new Point(16, 16), "");
85

    
86
        /**
87
         * Reference to the <code>MapControl</code> object that uses.
88
         */
89
        protected MapControl mapControl;
90

    
91
        /**
92
          * <p>Creates a new listener for changing the position of the extent of the associated {@link MapOverview MapOverview} object.</p>
93
         *
94
         * @param mapControl the <code>MapControl</code> object which represents the <code>MapOverview</code>  
95
         */
96
        public MapOverviewPanListener(MapControl mapControl) {
97
                this.mapControl = mapControl;
98
        }
99

    
100
        /*
101
         * (non-Javadoc)
102
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getCursor()
103
         */
104
        public Cursor getCursor() {
105
                return cur;
106
        }
107

    
108
        /*
109
         * (non-Javadoc)
110
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
111
         */
112
        public boolean cancelDrawing() {
113
                return true;
114
        }
115

    
116
        /*
117
        public void move(MoveEvent event) throws BehaviorException {
118
                if (!checkModifiers(event.getEvent().getModifiers())){
119
                        return;
120
                }
121
                
122
                ViewPort vp = mapControl.getMapContext().getViewPort();
123
                MapOverview mov=(MapOverview) this.mapControl;
124
                ViewPort vpView=mov.getAssociatedMapContext().getViewPort();
125
                if (vp.getExtent() != null) {
126
                        Point2D p = vp.toMapPoint(event.getTo());
127
                        Rectangle2D r = (Rectangle2D) vpView.getExtent().clone();
128
                        r.setRect(p.getX() - (r.getWidth() / 2),
129
                                p.getY() - (r.getHeight() / 2), r.getWidth(), r.getHeight());
130
                        //vpView.setExtent(r);
131
                        double scale;
132
                        double escalaX;
133
                        double escalaY;
134
                        double newHeight;
135
                        double newWidth;
136
                        double xCenter = r.getCenterX();
137
                        double yCenter = r.getCenterY();
138

139
                        escalaX = mapControl.getWidth() / r.getWidth();
140
                        escalaY = mapControl.getHeight() / r.getHeight();
141
                        Rectangle2D adjustedExtent = new Rectangle2D.Double();
142

143
                        if (escalaX < escalaY) {
144
                                scale = escalaX;
145
                                newHeight = mapControl.getHeight() / scale;
146
                                adjustedExtent.setRect(xCenter - (r.getWidth() / 2.0),
147
                                        yCenter - (newHeight / 2.0), r.getWidth(), newHeight);
148
                        } else {
149
                                scale = escalaY;
150
                                newWidth = mapControl.getWidth() / scale;
151
                                adjustedExtent.setRect(xCenter - (newWidth / 2.0),
152
                                        yCenter - (r.getHeight() / 2.0), newWidth,
153
                                        r.getHeight());
154
                        }
155
                        mov.refreshOverView(adjustedExtent);
156
                }
157

158
        }
159
        */
160

    
161
        /*
162
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.PanListener#move(MoveEvent)
163
         */
164
        public void move(MoveEvent event) throws BehaviorException {
165
                
166
                
167
                if (!checkModifiers(event.getEvent())){
168
                        return;
169
                }
170
                System.out.println("mapOvervierPan");
171
                MapOverview mov=(MapOverview) this.mapControl;
172
                ViewPort vp = mov.getViewPort();
173
                ViewPort vpView=mov.getAssociatedMapContext().getViewPort();
174

    
175
                if (vp.getExtent() != null && vpView.getExtent() != null) {
176
                        
177
                        // recogemos la forma de la vista actual
178
                        Rectangle2D curRectangle = (Rectangle2D)vpView.getAdjustedExtent();
179
                        // traducimos las coordenadas en px de la ultima posicion del raton 
180
                        // a coordenadas de la vista 
181
                        Point2D thePoint= vp.toMapPoint( event.getTo());
182
                        
183
                        Rectangle2D realRectangle = new Rectangle2D.Double();                
184
                        double width =curRectangle.getWidth();
185
                        double height =curRectangle.getHeight();
186
                        // Creamos un rectangulo del mismo tama?o
187
                        // con la coordenadas del punto del raton,
188
                        // teniendo en cuenta que estas sera el 
189
                        // centro del recuadro
190
                        realRectangle.setRect(
191
                                        thePoint.getX() - (width/2),
192
                                        thePoint.getY() - (height/2),
193
                                        width,
194
                                        height                                
195
                        );
196
                        // cambiamos la posicion
197
                        mov.refreshOverView(realRectangle);
198
                }
199
        }
200
        
201
        /** 
202
         * Determines if has pressed the button 3 of the mouse.
203
         */
204
        private boolean checkModifiers(MouseEvent event) {
205
                
206
                int modifiers = event.getModifiers();
207
                /*
208
                int keyPressedMask = InputEvent.BUTTON2_MASK;
209
                
210
                *** No se porque el boton derecho del raton devuelve
211
                *** un modificador 'Meta + BUTTON3'. Pensaba que deberia
212
                *** devolver 'BUTTON2' ???? 
213
                */                
214
                int keyPressedMask = InputEvent.BUTTON3_MASK;
215
                return ((modifiers & keyPressedMask) == keyPressedMask);
216
        }
217
}