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 @ 41041

History | View | Annotate | Download (10.6 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.geom.AffineTransform;
32
import java.awt.geom.Line2D;
33
import java.awt.geom.Point2D;
34
import java.awt.geom.Rectangle2D;
35
import java.awt.image.BufferedImage;
36

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

    
55

    
56

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
249
        }
250

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

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

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

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

    
303
        }
304

    
305
        /**
306
         * <p>Unimplemented.</p>
307
         *
308
         * <p>Can't change the projection, because must be the same as the one of the
309
         *  associated <code>MapControl</code> instance.</p>
310
         */
311
        public void setProjection(IProjection proj) {
312
                //No permitimos cambiar la proyeccion
313
                //ya que debe ser la misma que la del
314
                //MapControl asociado
315
                return;
316
        }
317
}