Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.plugin / org.gvsig.app / org.gvsig.app.mainplugin / src / main / java / org / gvsig / app / project / documents / view / MapOverview.java @ 41098

History | View | Annotate | Download (10.8 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24

    
25
package org.gvsig.app.project.documents.view;
26

    
27
import java.awt.BasicStroke;
28
import java.awt.Color;
29
import java.awt.Graphics;
30
import java.awt.Graphics2D;
31
import java.awt.Point;
32
import java.awt.geom.AffineTransform;
33
import java.awt.geom.Line2D;
34
import java.awt.geom.Point2D;
35
import java.awt.geom.Rectangle2D;
36
import java.awt.image.BufferedImage;
37

    
38
import org.cresques.cts.IProjection;
39
import org.gvsig.app.project.documents.view.toolListeners.MapOverviewChangeZoomListener;
40
import org.gvsig.app.project.documents.view.toolListeners.MapOverviewListener;
41
import org.gvsig.app.project.documents.view.toolListeners.MapOverviewPanListener;
42
import org.gvsig.fmap.dal.exception.ReadException;
43
import org.gvsig.fmap.geom.primitive.Envelope;
44
import org.gvsig.fmap.mapcontext.MapContext;
45
import org.gvsig.fmap.mapcontext.ViewPort;
46
import org.gvsig.fmap.mapcontext.events.ColorEvent;
47
import org.gvsig.fmap.mapcontext.events.ExtentEvent;
48
import org.gvsig.fmap.mapcontext.events.ProjectionEvent;
49
import org.gvsig.fmap.mapcontext.events.listeners.ViewPortListener;
50
import org.gvsig.fmap.mapcontrol.MapControl;
51
import org.gvsig.fmap.mapcontrol.tools.Behavior.Behavior;
52
import org.gvsig.fmap.mapcontrol.tools.Behavior.DraggerBehavior;
53
import org.gvsig.fmap.mapcontrol.tools.Behavior.PointBehavior;
54
import org.gvsig.fmap.mapcontrol.tools.Behavior.RectangleBehavior;
55

    
56

    
57

    
58
/**
59
 * <p>Lightweight <code>MapControl</code> that uses another <code>MapControl</code>'s <code>MapContext</code>, and updates
60
 *  any rectangular extent selected, to the associated <code>MapControl</code>.</p>
61
 *
62
 * <p>Both <code>MapControl</code> instances work in the same projection. And, always, the not undefined <i>adjusted extent</i>
63
 *  of the associated one, will be enhanced as a red-bordered grey-filled rectangle in this one. Furthermore, draws a horizontal and vertical
64
 *  this component's width or height, black lines centered in that rectangle.</p>
65
 *
66
 * @author FJP
67
 */
68
public class MapOverview extends MapControl implements ViewPortListener {
69
        /**
70
         * 
71
         */
72
        private static final long serialVersionUID = -2849739771493279542L;
73

    
74
        /**
75
         * <p>Associated <code>MapControl</code> instance that this component represents its overview.</p>
76
         */
77
        private MapControl m_MapAssoc;
78

    
79
        /**
80
         * <p>Determines that's the first time this component is going to be painted.</p>
81
         */
82
        boolean first = true;
83

    
84
        /**
85
         * <p>Tool listener used to apply a <i>zoom out</i> operation in this component graphical information.</p>
86
         */
87
        private MapOverviewListener movl;
88

    
89
        /**
90
         * <p>Tool listener used to allow user work with this component applying <i>pan</i> operations.</p>
91
         */
92
        private MapOverviewPanListener movpl;
93

    
94
        /**
95
         * <p>Tool listener used to allow user work with this component applying <i>pan</i> operations.</p>
96
         */
97
        private MapOverviewChangeZoomListener movczl;
98

    
99
        /**
100
         * <p>Rectangular area selected in this component, that will be the extent of the associated <code>MapControl</code> instance.</p>
101
         */
102
        private Envelope extent;
103

    
104
        /**
105
         * <p>Double buffer used to paint this component graphical information.</p>
106
         */
107
        private BufferedImage image;
108

    
109
        /**
110
         * <p>Creates a <code>MapOverview</code> instance associated to <code>mapAssoc</code>.</p>
111
         *
112
         * @param mapAssoc <code>MapControl</code> this component will be the overview
113
         */
114
        public MapOverview(MapControl mapAssoc) {
115
                super();
116
                this.setName("MapOverview");
117
                // super.vp.setBackColor(new Color(230,230,230));
118
                m_MapAssoc = mapAssoc;
119

    
120
                // setModel((FMap) m_MapAssoc.getMapContext().clone()); // Lo inicializamos con
121
                // los mismos temas que tenga el grande.
122
                movl = new MapOverviewListener(this);
123
                movpl = new MapOverviewPanListener(this);
124
                movczl = new MapOverviewChangeZoomListener(this);
125
                addBehavior(
126
                                "zoomtopoint",
127
                                new Behavior[]{
128
                                                new PointBehavior(movl),
129
                                                new RectangleBehavior(movczl),
130
                                                new DraggerBehavior(movczl),
131
                                                new DraggerBehavior(movpl)
132
                                }
133
                );
134

    
135
//                setCursor(movl.getCursor());
136

    
137
                setTool("zoomtopoint");
138
                getGrid().setShowGrid(false);
139
                getGrid().setAdjustGrid(false);
140
        }
141

    
142
        /**
143
         * @see MapControl#getMapContext()
144
         */
145
        public MapContext getAssociatedMapContext() {
146
                return m_MapAssoc.getMapContext();
147
        }
148

    
149
        /**
150
         * <p>Gets the <code>MapControl</code> instance that wrappers.</p>
151
         *
152
         * @return the <code>MapControl</code> instance that wrappers
153
         */
154
        public MapControl getAssociatedMapControl(){
155
                return m_MapAssoc;
156
        }
157

    
158
        /*
159
         * (non-Javadoc)
160
         * @see com.iver.cit.gvsig.fmap.ViewPortListener#extentChanged(com.iver.cit.gvsig.fmap.ExtentEvent)
161
         */
162
        public void extentChanged(ExtentEvent evExtent) {
163
                // Nos llega el nuevo extent del FMap asociado, as? que dibujamos nuestro
164
                // rect?ngulo para mostrar la zona de dibujo del otro mapa.
165
                repaint();
166
        }
167

    
168
        /**
169
         * <p>If this had no extent, calls {@link #delModel() #delModel()}, otherwise recalculates this
170
         *  component's view port extent as the union of all extents of all layers of this map.</p>
171
         */
172
        public void refreshExtent() {
173
                try {
174
                if (this.getMapContext().getFullEnvelope()!=null){
175
                                this.getMapContext().getViewPort().setEnvelope(this.getMapContext().getFullEnvelope());
176
                } else {
177
                        // delModel();
178
                }
179
                } catch (ReadException e1) {
180
                        e1.printStackTrace();
181
                }
182
        }
183

    
184
        /**
185
         * <p>Repaints this component updating its <i>extent</i>.</p>
186
         *
187
         * @param r the new extent
188
         */
189
        public void refreshOverView(Envelope r){
190
                extent=r;
191
                repaint();
192
        }
193

    
194
        /**
195
         * <p>Paints this component's graphical information using a 8-bit RGBA color double buffer, drawing a red-bordered
196
         *  grey-filled rectangle enhancing the extent selected, and a horizontal and vertical this component's width or
197
         *  height, black lines centered in that rectangle.</p>
198
         */
199
        protected void paintComponent(Graphics g) {
200
                getGrid().setShowGrid(false);
201
                getGrid().setAdjustGrid(false);
202
                super.paintComponent(g);
203
                
204
                if (m_MapAssoc.getMapContext().getViewPort().getEnvelope() != null &&
205
                                        getMapContext().getViewPort().getEnvelope() != null &&
206
                                        getMapContext().getLayers().getLayersCount() > 0) {
207
                    
208
                                if (first) {
209
                                        first = false;
210
                                        repaint();
211
                                        return;
212
                                }
213
                                image = new BufferedImage(this.getWidth(), this.getHeight(),
214
                                    BufferedImage.TYPE_INT_ARGB);
215
                                ViewPort vp = getMapContext().getViewPort();
216
                                Envelope extentView=vp.getAdjustedEnvelope();
217
                                ViewPort vpOrig = m_MapAssoc.getMapContext().getViewPort();
218
                                if (extent==null) {
219
                                        extent=vpOrig.getAdjustedEnvelope();
220
                                }
221
                                // Dibujamos el extent del mapa asociado.
222
                                Graphics2D g2 = (Graphics2D) image.getGraphics();
223
                                g2.setTransform(vp.getAffineTransform());
224

    
225
                                g2.setStroke(new BasicStroke((float) vp.getDist1pixel()));
226

    
227
                                g2.setColor(Color.red);
228
                                Rectangle2D extentToDraw=new Rectangle2D.Double(extent.getMinimum(0),extent.getMinimum(1),extent.getLength(0),extent.getLength(1));
229
                                g2.draw(extentToDraw);
230
                                g2.setColor(new Color(100,100,100,100));
231
                                g2.fill(extentToDraw);
232
                                // dibujamos las l?neas vertical y horizontal
233
                                Point2D pRightUp = vp.toMapPoint(getWidth(), 0);
234

    
235
                                Line2D.Double linVert = new Line2D.Double(extentToDraw.getCenterX(),
236
                                                extentView.getMinimum(1), extentToDraw.getCenterX(),
237
                                                pRightUp.getY());
238
                                Line2D.Double linHoriz = new Line2D.Double(extentView.getMinimum(0),
239
                                                extentToDraw.getCenterY(), pRightUp.getX(),
240
                                                extentToDraw.getCenterY());
241

    
242
                                g2.setColor(Color.darkGray);
243
                                g2.draw(linVert);
244
                                g2.draw(linHoriz);
245
                                g.drawImage(image,0,0,this);
246
                                g2.setTransform(new AffineTransform());
247
                                extent=null;
248
                        }
249

    
250
        }
251

    
252
        /**
253
         * <p>Sets a <code>MapContext</code> to this component, configuring it to manage view port events produced
254
         *  in the associated <code>MapControl</code> and this one's view port.</p>
255
         *
256
         * <p>This <code>MapContext</code>'s projection will be the same as the associated <code>MapControl</code>'s one.</p>
257
         *
258
         * <p>Setting the model includes the following steps:
259
         *  <ul>
260
         *   <li><b>1.-</b> set <code>model</code> as the <code>MapContext</code> of <code>this</code>.</li>
261
         *   <li><b>2.-</b> set as <code>model</code> projection, the associated <code>MapControl<code>'s projection.</li>
262
         *   <li><b>3.-</b> set <code>this</code> as <i>view port</i> listener of the associated <code>MapControl</code>'s <i>view port</i>.</li>
263
         *   <li><b>4.-</b> set <code>this</code> as <i>view port</i> listener of this <code>MapContext</code>'s <i>view port</i></li>
264
         *  </ul>
265
         * </p>
266
         *
267
         * @param model data to set
268
         */
269
        public void setModel(MapContext model) {
270
                this.setMapContext(model);
271
                model.setProjection(m_MapAssoc.getMapContext().getProjection());
272
                m_MapAssoc.getMapContext().getViewPort().addViewPortListener(this);
273
                getMapContext().getViewPort().addViewPortListener(this);
274
        }
275

    
276
        /**
277
         * <p>Removes this component as listener of the the associated <code>MapControl</code> and this one's view port. Besides,
278
     *  removes the extent.</p>
279
         */
280
        /*
281
        private void delModel(){
282
                this.getMapContext().getViewPort().setEnvelope(null);
283
                m_MapAssoc.getMapContext().getViewPort().removeViewPortListener(this);
284
                getMapContext().getViewPort().removeViewPortListener(this);
285
        }
286
        */
287

    
288
        /*
289
         * (non-Javadoc)
290
         * @see com.iver.cit.gvsig.fmap.ViewPortListener#backColorChanged(com.iver.cit.gvsig.fmap.ColorEvent)
291
         */
292
        public void backColorChanged(ColorEvent e) {
293
                // do nothing
294
        }
295

    
296
        /**
297
         * @see ViewPortListener#projectionChanged(ProjectionEvent)
298
         *
299
         * @see MapControl#setProjection(IProjection)
300
         */
301
        public void projectionChanged(ProjectionEvent e) {
302
                super.setProjection(e.getNewProjection());
303

    
304
        }
305

    
306
        /**
307
         * <p>Unimplemented.</p>
308
         *
309
         * <p>Can't change the projection, because must be the same as the one of the
310
         *  associated <code>MapControl</code> instance.</p>
311
         */
312
        public void setProjection(IProjection proj) {
313
                //No permitimos cambiar la proyeccion
314
                //ya que debe ser la misma que la del
315
                //MapControl asociado
316
                return;
317
        }
318
        
319
        public void calculateSnapPoint(Point point) {
320
            /*
321
             * This is the behavior of a MapControl when there is no snapping.
322
             * We do the same and save a lot of computations
323
             */
324
        adjustedPoint = point;
325
        mapAdjustedPoint = null;
326
        }
327
        
328
        
329
        
330
}