Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / toolListeners / MapOverviewPanListener.java @ 24759

History | View | Annotate | Download (6.52 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.Image;
44
import java.awt.event.InputEvent;
45
import java.awt.event.MouseEvent;
46
import java.awt.geom.Point2D;
47

    
48
import org.gvsig.fmap.geom.primitive.DefaultEnvelope;
49
import org.gvsig.fmap.geom.primitive.Envelope;
50
import org.gvsig.fmap.mapcontext.ViewPort;
51
import org.gvsig.fmap.mapcontrol.MapControl;
52
import org.gvsig.fmap.mapcontrol.tools.BehaviorException;
53
import org.gvsig.fmap.mapcontrol.tools.Events.MoveEvent;
54
import org.gvsig.fmap.mapcontrol.tools.Listeners.PanListener;
55

    
56
import com.iver.andami.PluginServices;
57
import com.iver.cit.gvsig.project.documents.view.MapOverview;
58

    
59

    
60

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

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

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

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

    
99
        /*
100
         * (non-Javadoc)
101
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#getImageCursor()
102
         */
103
        public Image getImageCursor() {
104
                return icursor;
105
        }
106

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

    
115
        /*
116
        public void move(MoveEvent event) throws BehaviorException {
117
                if (!checkModifiers(event.getEvent().getModifiers())){
118
                        return;
119
                }
120

121
                ViewPort vp = mapControl.getMapContext().getViewPort();
122
                MapOverview mov=(MapOverview) this.mapControl;
123
                ViewPort vpView=mov.getAssociatedMapContext().getViewPort();
124
                if (vp.getExtent() != null) {
125
                        Point2D p = vp.toMapPoint(event.getTo());
126
                        Rectangle2D r = (Rectangle2D) vpView.getExtent().clone();
127
                        r.setRect(p.getX() - (r.getWidth() / 2),
128
                                p.getY() - (r.getHeight() / 2), r.getWidth(), r.getHeight());
129
                        //vpView.setExtent(r);
130
                        double scale;
131
                        double escalaX;
132
                        double escalaY;
133
                        double newHeight;
134
                        double newWidth;
135
                        double xCenter = r.getCenterX();
136
                        double yCenter = r.getCenterY();
137

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

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

157
        }
158
        */
159

    
160
        /*
161
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.PanListener#move(MoveEvent)
162
         */
163
        public void move(MoveEvent event) throws BehaviorException {
164

    
165

    
166
                if (!checkModifiers(event.getEvent())){
167
                        return;
168
                }
169
                System.out.println("mapOvervierPan");
170
                MapOverview mov=(MapOverview) this.mapControl;
171
                ViewPort vp = mov.getViewPort();
172
                ViewPort vpView=mov.getAssociatedMapContext().getViewPort();
173

    
174
                if (vp.getExtent() != null && vpView.getExtent() != null) {
175

    
176
                        // recogemos la forma de la vista actual
177
                        Envelope curEnvelope = vpView.getAdjustedExtent();
178
                        // traducimos las coordenadas en px de la ultima posicion del raton
179
                        // a coordenadas de la vista
180
                        Point2D thePoint= vp.toMapPoint( event.getTo());
181

    
182
                        double diffx =
183
                                (curEnvelope.getMaximum(0) - curEnvelope.getMinimum(0)) / 2;
184
                        double diffy =
185
                                (curEnvelope.getMaximum(1) - curEnvelope.getMinimum(1)) / 2;
186
                        // Creamos un envelope 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

    
191
                        Envelope envelope = new DefaultEnvelope(
192
                                        thePoint.getX() - diffx,
193
                                        thePoint.getY() - diffy,
194
                                        thePoint.getX() + diffx,
195
                                        thePoint.getY()        + diffy
196
                        );
197
                        // cambiamos la posicion
198
                        mov.refreshOverView(envelope);
199
                        vpView.setEnvelope(envelope);
200
                        mov.getAssociatedMapContext().invalidate();
201
                }
202
        }
203

    
204
        /**
205
         * Determines if has pressed the button 3 of the mouse.
206
         */
207
        private boolean checkModifiers(MouseEvent event) {
208

    
209
                int modifiers = event.getModifiers();
210
                /*
211
                int keyPressedMask = InputEvent.BUTTON2_MASK;
212

213
                *** No se porque el boton derecho del raton devuelve
214
                *** un modificador 'Meta + BUTTON3'. Pensaba que deberia
215
                *** devolver 'BUTTON2' ????
216
                */
217
                int keyPressedMask = InputEvent.BUTTON3_MASK;
218
                return ((modifiers & keyPressedMask) == keyPressedMask);
219
        }
220
}