Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / documents / view / toolListeners / MapOverviewPanListener.java @ 40397

History | View | Annotate | Download (7.09 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.app.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.andami.PluginServices;
49
import org.gvsig.app.gui.styling.SymbolSelector;
50
import org.gvsig.app.project.documents.view.MapOverview;
51
import org.gvsig.fmap.geom.GeometryLocator;
52
import org.gvsig.fmap.geom.GeometryManager;
53
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
54
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
55
import org.gvsig.fmap.geom.primitive.Envelope;
56
import org.gvsig.fmap.mapcontext.ViewPort;
57
import org.gvsig.fmap.mapcontrol.MapControl;
58
import org.gvsig.fmap.mapcontrol.tools.BehaviorException;
59
import org.gvsig.fmap.mapcontrol.tools.Events.MoveEvent;
60
import org.gvsig.fmap.mapcontrol.tools.Listeners.PanListener;
61
import org.slf4j.Logger;
62
import org.slf4j.LoggerFactory;
63

    
64

    
65

    
66

    
67
/**
68
 * <p>Listener for moving the extent of the associated {@link MapOverview MapOverview} object
69
 *  according the movement between the initial and final points of line determined by the movement
70
 *  dragging with the third button of the mouse.</p>
71
 *
72
 * <p>Updates the extent of its <code>ViewPort</code> with the new position.</p>
73
 *
74
 * @author Vicente Caballero Navarro
75
 */
76
public class MapOverviewPanListener implements PanListener {
77
        private static final GeometryManager geomManager = GeometryLocator.getGeometryManager();
78
        private static final Logger logger = LoggerFactory.getLogger(MapOverviewPanListener.class);
79
        
80
        /**
81
         * The image to display when the cursor is active.
82
         */
83
        private final Image icursor = PluginServices.getIconTheme().get("cursor-crux").getImage();
84

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

    
93
        /**
94
         * Reference to the <code>MapControl</code> object that uses.
95
         */
96
        protected MapControl mapControl;
97

    
98
        /**
99
          * <p>Creates a new listener for changing the position of the extent of the associated {@link MapOverview MapOverview} object.</p>
100
         *
101
         * @param mapControl the <code>MapControl</code> object which represents the <code>MapOverview</code>
102
         */
103
        public MapOverviewPanListener(MapControl mapControl) {
104
                this.mapControl = mapControl;
105
        }
106

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

    
115
        /*
116
         * (non-Javadoc)
117
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.ToolListener#cancelDrawing()
118
         */
119
        public boolean cancelDrawing() {
120
                return true;
121
        }
122

    
123
        /*
124
        public void move(MoveEvent event) throws BehaviorException {
125
                if (!checkModifiers(event.getEvent().getModifiers())){
126
                        return;
127
                }
128

129
                ViewPort vp = mapControl.getMapContext().getViewPort();
130
                MapOverview mov=(MapOverview) this.mapControl;
131
                ViewPort vpView=mov.getAssociatedMapContext().getViewPort();
132
                if (vp.getExtent() != null) {
133
                        Point2D p = vp.toMapPoint(event.getTo());
134
                        Rectangle2D r = (Rectangle2D) vpView.getExtent().clone();
135
                        r.setRect(p.getX() - (r.getWidth() / 2),
136
                                p.getY() - (r.getHeight() / 2), r.getWidth(), r.getHeight());
137
                        //vpView.setExtent(r);
138
                        double scale;
139
                        double escalaX;
140
                        double escalaY;
141
                        double newHeight;
142
                        double newWidth;
143
                        double xCenter = r.getCenterX();
144
                        double yCenter = r.getCenterY();
145

146
                        escalaX = mapControl.getWidth() / r.getWidth();
147
                        escalaY = mapControl.getHeight() / r.getHeight();
148
                        Rectangle2D adjustedExtent = new Rectangle2D.Double();
149

150
                        if (escalaX < escalaY) {
151
                                scale = escalaX;
152
                                newHeight = mapControl.getHeight() / scale;
153
                                adjustedExtent.setRect(xCenter - (r.getWidth() / 2.0),
154
                                        yCenter - (newHeight / 2.0), r.getWidth(), newHeight);
155
                        } else {
156
                                scale = escalaY;
157
                                newWidth = mapControl.getWidth() / scale;
158
                                adjustedExtent.setRect(xCenter - (newWidth / 2.0),
159
                                        yCenter - (r.getHeight() / 2.0), newWidth,
160
                                        r.getHeight());
161
                        }
162
                        mov.refreshOverView(adjustedExtent);
163
                }
164

165
        }
166
        */
167

    
168
        /*
169
         * @see com.iver.cit.gvsig.fmap.tools.Listeners.PanListener#move(MoveEvent)
170
         */
171
        public void move(MoveEvent event) throws BehaviorException {
172

    
173

    
174
                if (!checkModifiers(event.getEvent())){
175
                        return;
176
                }
177
                System.out.println("mapOvervierPan");
178
                MapOverview mov=(MapOverview) this.mapControl;
179
                ViewPort vp = mov.getViewPort();
180
                ViewPort vpView=mov.getAssociatedMapContext().getViewPort();
181

    
182
                if (vp.getExtent() != null && vpView.getExtent() != null) {
183

    
184
                        // recogemos la forma de la vista actual
185
                        Envelope curEnvelope = vpView.getAdjustedExtent();
186
                        // traducimos las coordenadas en px de la ultima posicion del raton
187
                        // a coordenadas de la vista
188
                        Point2D thePoint= vp.toMapPoint( event.getTo());
189

    
190
                        double diffx =
191
                                (curEnvelope.getMaximum(0) - curEnvelope.getMinimum(0)) / 2;
192
                        double diffy =
193
                                (curEnvelope.getMaximum(1) - curEnvelope.getMinimum(1)) / 2;
194
                        // Creamos un envelope del mismo tama�o
195
                        // con la coordenadas del punto del raton,
196
                        // teniendo en cuenta que estas sera el
197
                        // centro del recuadro
198

    
199
                        Envelope envelope;
200
                        try {
201
                                envelope = geomManager.createEnvelope(
202
                                                thePoint.getX() - diffx,
203
                                                thePoint.getY() - diffy,
204
                                                thePoint.getX() + diffx,
205
                                                thePoint.getY()        + diffy,
206
                                                SUBTYPES.GEOM2D
207
                                );
208
                                // cambiamos la posicion
209
                                mov.refreshOverView(envelope);
210
                                vpView.setEnvelope(envelope);
211
                        } catch (CreateEnvelopeException e) {
212
                                logger.error("Error creating the envelope", e);
213
                        }                        
214
                        mov.getAssociatedMapContext().invalidate();
215
                }
216
        }
217

    
218
        /**
219
         * Determines if has pressed the button 3 of the mouse.
220
         */
221
        private boolean checkModifiers(MouseEvent event) {
222

    
223
                int modifiers = event.getModifiers();
224
                /*
225
                int keyPressedMask = InputEvent.BUTTON2_MASK;
226

227
                *** No se porque el boton derecho del raton devuelve
228
                *** un modificador 'Meta + BUTTON3'. Pensaba que deberia
229
                *** devolver 'BUTTON2' ????
230
                */
231
                int keyPressedMask = InputEvent.BUTTON3_MASK;
232
                return ((modifiers & keyPressedMask) == keyPressedMask);
233
        }
234
}