Statistics
| Revision:

root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / documents / view / MapOverview.java @ 38562

History | View | Annotate | Download (10.9 KB)

1
/*
2
 * Created on 21-jun-2004
3
 *
4
 * TODO To change the template for this generated file go to
5
 * Window - Preferences - Java - Code Generation - Code and Comments
6
 */
7
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
8
 *
9
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
10
 *
11
 * This program is free software; you can redistribute it and/or
12
 * modify it under the terms of the GNU General Public License
13
 * as published by the Free Software Foundation; either version 2
14
 * of the License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
23
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
24
 *
25
 * For more information, contact:
26
 *
27
 *  Generalitat Valenciana
28
 *   Conselleria d'Infraestructures i Transport
29
 *   Av. Blasco Ib??ez, 50
30
 *   46010 VALENCIA
31
 *   SPAIN
32
 *
33
 *      +34 963862235
34
 *   gvsig@gva.es
35
 *      www.gvsig.gva.es
36
 *
37
 *    or
38
 *
39
 *   IVER T.I. S.A
40
 *   Salamanca 50
41
 *   46005 Valencia
42
 *   Spain
43
 *
44
 *   +34 963163400
45
 *   dac@iver.es
46
 */
47
package org.gvsig.app.project.documents.view;
48

    
49
import java.awt.BasicStroke;
50
import java.awt.Color;
51
import java.awt.Graphics;
52
import java.awt.Graphics2D;
53
import java.awt.geom.AffineTransform;
54
import java.awt.geom.Line2D;
55
import java.awt.geom.Point2D;
56
import java.awt.geom.Rectangle2D;
57
import java.awt.image.BufferedImage;
58

    
59
import org.cresques.cts.IProjection;
60
import org.gvsig.app.project.documents.view.toolListeners.MapOverviewChangeZoomListener;
61
import org.gvsig.app.project.documents.view.toolListeners.MapOverviewListener;
62
import org.gvsig.app.project.documents.view.toolListeners.MapOverviewPanListener;
63
import org.gvsig.fmap.dal.exception.ReadException;
64
import org.gvsig.fmap.geom.primitive.Envelope;
65
import org.gvsig.fmap.mapcontext.MapContext;
66
import org.gvsig.fmap.mapcontext.ViewPort;
67
import org.gvsig.fmap.mapcontext.events.ColorEvent;
68
import org.gvsig.fmap.mapcontext.events.ExtentEvent;
69
import org.gvsig.fmap.mapcontext.events.ProjectionEvent;
70
import org.gvsig.fmap.mapcontext.events.listeners.ViewPortListener;
71
import org.gvsig.fmap.mapcontrol.MapControl;
72
import org.gvsig.fmap.mapcontrol.tools.Behavior.Behavior;
73
import org.gvsig.fmap.mapcontrol.tools.Behavior.DraggerBehavior;
74
import org.gvsig.fmap.mapcontrol.tools.Behavior.PointBehavior;
75
import org.gvsig.fmap.mapcontrol.tools.Behavior.RectangleBehavior;
76

    
77

    
78

    
79
/**
80
 * <p>Lightweight <code>MapControl</code> that uses another <code>MapControl</code>'s <code>MapContext</code>, and updates
81
 *  any rectangular extent selected, to the associated <code>MapControl</code>.</p>
82
 *
83
 * <p>Both <code>MapControl</code> instances work in the same projection. And, always, the not undefined <i>adjusted extent</i>
84
 *  of the associated one, will be enhanced as a red-bordered grey-filled rectangle in this one. Furthermore, draws a horizontal and vertical
85
 *  this component's width or height, black lines centered in that rectangle.</p>
86
 *
87
 * @author FJP
88
 */
89
public class MapOverview extends MapControl implements ViewPortListener {
90
        /**
91
         * 
92
         */
93
        private static final long serialVersionUID = -2849739771493279542L;
94

    
95
        /**
96
         * <p>Associated <code>MapControl</code> instance that this component represents its overview.</p>
97
         */
98
        private MapControl m_MapAssoc;
99

    
100
        /**
101
         * <p>Determines that's the first time this component is going to be painted.</p>
102
         */
103
        boolean first = true;
104

    
105
        /**
106
         * <p>Tool listener used to apply a <i>zoom out</i> operation in this component graphical information.</p>
107
         */
108
        private MapOverviewListener movl;
109

    
110
        /**
111
         * <p>Tool listener used to allow user work with this component applying <i>pan</i> operations.</p>
112
         */
113
        private MapOverviewPanListener movpl;
114

    
115
        /**
116
         * <p>Tool listener used to allow user work with this component applying <i>pan</i> operations.</p>
117
         */
118
        private MapOverviewChangeZoomListener movczl;
119

    
120
        /**
121
         * <p>Rectangular area selected in this component, that will be the extent of the associated <code>MapControl</code> instance.</p>
122
         */
123
        private Envelope extent;
124

    
125
        /**
126
         * <p>Double buffer used to paint this component graphical information.</p>
127
         */
128
        private BufferedImage image;
129

    
130
        /**
131
         * <p>Creates a <code>MapOverview</code> instance associated to <code>mapAssoc</code>.</p>
132
         *
133
         * @param mapAssoc <code>MapControl</code> this component will be the overview
134
         */
135
        public MapOverview(MapControl mapAssoc) {
136
                super();
137
                this.setName("MapOverview");
138
                // super.vp.setBackColor(new Color(230,230,230));
139
                m_MapAssoc = mapAssoc;
140

    
141
                // setModel((FMap) m_MapAssoc.getMapContext().clone()); // Lo inicializamos con
142
                // los mismos temas que tenga el grande.
143
                movl = new MapOverviewListener(this);
144
                movpl = new MapOverviewPanListener(this);
145
                movczl = new MapOverviewChangeZoomListener(this);
146
                addBehavior(
147
                                "zoomtopoint",
148
                                new Behavior[]{
149
                                                new PointBehavior(movl),
150
                                                new RectangleBehavior(movczl),
151
                                                new DraggerBehavior(movczl),
152
                                                new DraggerBehavior(movpl)
153
                                }
154
                );
155

    
156
//                setCursor(movl.getCursor());
157

    
158
                setTool("zoomtopoint");
159
                getGrid().setShowGrid(false);
160
                getGrid().setAdjustGrid(false);
161
        }
162

    
163
        /**
164
         * @see MapControl#getMapContext()
165
         */
166
        public MapContext getAssociatedMapContext() {
167
                return m_MapAssoc.getMapContext();
168
        }
169

    
170
        /**
171
         * <p>Gets the <code>MapControl</code> instance that wrappers.</p>
172
         *
173
         * @return the <code>MapControl</code> instance that wrappers
174
         */
175
        public MapControl getAssociatedMapControl(){
176
                return m_MapAssoc;
177
        }
178

    
179
        /*
180
         * (non-Javadoc)
181
         * @see com.iver.cit.gvsig.fmap.ViewPortListener#extentChanged(com.iver.cit.gvsig.fmap.ExtentEvent)
182
         */
183
        public void extentChanged(ExtentEvent evExtent) {
184
                // Nos llega el nuevo extent del FMap asociado, as? que dibujamos nuestro
185
                // rect?ngulo para mostrar la zona de dibujo del otro mapa.
186
                repaint();
187
        }
188

    
189
        /**
190
         * <p>If this had no extent, calls {@link #delModel() #delModel()}, otherwise recalculates this
191
         *  component's view port extent as the union of all extents of all layers of this map.</p>
192
         */
193
        public void refreshExtent() {
194
                try {
195
                if (this.getMapContext().getFullEnvelope()!=null){
196
                                this.getMapContext().getViewPort().setEnvelope(this.getMapContext().getFullEnvelope());
197
                }else{
198
                        delModel();
199
                }
200
                } catch (ReadException e1) {
201
                        e1.printStackTrace();
202
                }
203
        }
204

    
205
        /**
206
         * <p>Repaints this component updating its <i>extent</i>.</p>
207
         *
208
         * @param r the new extent
209
         */
210
        public void refreshOverView(Envelope r){
211
                extent=r;
212
                repaint();
213
        }
214

    
215
        /**
216
         * <p>Paints this component's graphical information using a 8-bit RGBA color double buffer, drawing a red-bordered
217
         *  grey-filled rectangle enhancing the extent selected, and a horizontal and vertical this component's width or
218
         *  height, black lines centered in that rectangle.</p>
219
         */
220
        protected void paintComponent(Graphics g) {
221
                getGrid().setShowGrid(false);
222
                getGrid().setAdjustGrid(false);
223
                super.paintComponent(g);
224
                
225
                if ((m_MapAssoc.getMapContext().getViewPort().getEnvelope() != null) &&
226
                                        (getMapContext().getViewPort().getEnvelope() != null)) {
227
                                if (first) {
228
                                        first = false;
229
                                        repaint();
230
                                        return;
231
                                }
232
                                image = new BufferedImage(this.getWidth(), this.getHeight(),
233
                                    BufferedImage.TYPE_INT_ARGB);
234
                                ViewPort vp = getMapContext().getViewPort();
235
                                Envelope extentView=vp.getAdjustedEnvelope();
236
                                ViewPort vpOrig = m_MapAssoc.getMapContext().getViewPort();
237
                                if (extent==null) {
238
                                        extent=vpOrig.getAdjustedEnvelope();
239
                                }
240
                                // Dibujamos el extent del mapa asociado.
241
                                Graphics2D g2 = (Graphics2D) image.getGraphics();
242
                                g2.setTransform(vp.getAffineTransform());
243

    
244
                                g2.setStroke(new BasicStroke((float) vp.getDist1pixel()));
245

    
246
                                g2.setColor(Color.red);
247
                                Rectangle2D extentToDraw=new Rectangle2D.Double(extent.getMinimum(0),extent.getMinimum(1),extent.getLength(0),extent.getLength(1));
248
                                g2.draw(extentToDraw);
249
                                g2.setColor(new Color(100,100,100,100));
250
                                g2.fill(extentToDraw);
251
                                // dibujamos las l?neas vertical y horizontal
252
                                Point2D pRightUp = vp.toMapPoint(getWidth(), 0);
253

    
254
                                Line2D.Double linVert = new Line2D.Double(extentToDraw.getCenterX(),
255
                                                extentView.getMinimum(1), extentToDraw.getCenterX(),
256
                                                pRightUp.getY());
257
                                Line2D.Double linHoriz = new Line2D.Double(extentView.getMinimum(0),
258
                                                extentToDraw.getCenterY(), pRightUp.getX(),
259
                                                extentToDraw.getCenterY());
260

    
261
                                g2.setColor(Color.darkGray);
262
                                g2.draw(linVert);
263
                                g2.draw(linHoriz);
264
                                g.drawImage(image,0,0,this);
265
                                g2.setTransform(new AffineTransform());
266
                                extent=null;
267
                        }
268

    
269
        }
270

    
271
        /**
272
         * <p>Sets a <code>MapContext</code> to this component, configuring it to manage view port events produced
273
         *  in the associated <code>MapControl</code> and this one's view port.</p>
274
         *
275
         * <p>This <code>MapContext</code>'s projection will be the same as the associated <code>MapControl</code>'s one.</p>
276
         *
277
         * <p>Setting the model includes the following steps:
278
         *  <ul>
279
         *   <li><b>1.-</b> set <code>model</code> as the <code>MapContext</code> of <code>this</code>.</li>
280
         *   <li><b>2.-</b> set as <code>model</code> projection, the associated <code>MapControl<code>'s projection.</li>
281
         *   <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>
282
         *   <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>
283
         *  </ul>
284
         * </p>
285
         *
286
         * @param model data to set
287
         */
288
        public void setModel(MapContext model) {
289
                this.setMapContext(model);
290
                model.setProjection(m_MapAssoc.getMapContext().getProjection());
291
                m_MapAssoc.getMapContext().getViewPort().addViewPortListener(this);
292
                getMapContext().getViewPort().addViewPortListener(this);
293
        }
294

    
295
        /**
296
         * <p>Removes this component as listener of the the associated <code>MapControl</code> and this one's view port. Besides,
297
     *  removes the extent.</p>
298
         */
299
        private void delModel(){
300
                this.getMapContext().getViewPort().setEnvelope(null);
301
                m_MapAssoc.getMapContext().getViewPort().removeViewPortListener(this);
302
                getMapContext().getViewPort().removeViewPortListener(this);
303
        }
304

    
305
        /*
306
         * (non-Javadoc)
307
         * @see com.iver.cit.gvsig.fmap.ViewPortListener#backColorChanged(com.iver.cit.gvsig.fmap.ColorEvent)
308
         */
309
        public void backColorChanged(ColorEvent e) {
310
                // do nothing
311
        }
312

    
313
        /**
314
         * @see ViewPortListener#projectionChanged(ProjectionEvent)
315
         *
316
         * @see MapControl#setProjection(IProjection)
317
         */
318
        public void projectionChanged(ProjectionEvent e) {
319
                super.setProjection(e.getNewProjection());
320

    
321
        }
322

    
323
        /**
324
         * <p>Unimplemented.</p>
325
         *
326
         * <p>Can't change the projection, because must be the same as the one of the
327
         *  associated <code>MapControl</code> instance.</p>
328
         */
329
        public void setProjection(IProjection proj) {
330
                //No permitimos cambiar la proyeccion
331
                //ya que debe ser la misma que la del
332
                //MapControl asociado
333
                return;
334
        }
335
}