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

History | View | Annotate | Download (10.5 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
                                if (first) {
206
                                        first = false;
207
                                        repaint();
208
                                        return;
209
                                }
210
                                image = new BufferedImage(this.getWidth(), this.getHeight(),
211
                                    BufferedImage.TYPE_INT_ARGB);
212
                                ViewPort vp = getMapContext().getViewPort();
213
                                Envelope extentView=vp.getAdjustedEnvelope();
214
                                ViewPort vpOrig = m_MapAssoc.getMapContext().getViewPort();
215
                                if (extent==null) {
216
                                        extent=vpOrig.getAdjustedEnvelope();
217
                                }
218
                                // Dibujamos el extent del mapa asociado.
219
                                Graphics2D g2 = (Graphics2D) image.getGraphics();
220
                                g2.setTransform(vp.getAffineTransform());
221

    
222
                                g2.setStroke(new BasicStroke((float) vp.getDist1pixel()));
223

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

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

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

    
247
        }
248

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

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

    
283
        /*
284
         * (non-Javadoc)
285
         * @see com.iver.cit.gvsig.fmap.ViewPortListener#backColorChanged(com.iver.cit.gvsig.fmap.ColorEvent)
286
         */
287
        public void backColorChanged(ColorEvent e) {
288
                // do nothing
289
        }
290

    
291
        /**
292
         * @see ViewPortListener#projectionChanged(ProjectionEvent)
293
         *
294
         * @see MapControl#setProjection(IProjection)
295
         */
296
        public void projectionChanged(ProjectionEvent e) {
297
                super.setProjection(e.getNewProjection());
298

    
299
        }
300

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