root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / documents / view / MapOverview.java @ 29598
History | View | Annotate | Download (10.8 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 |
* <p>Associated <code>MapControl</code> instance that this component represents its overview.</p>
|
92 |
*/
|
93 |
private MapControl m_MapAssoc;
|
94 |
|
95 |
/**
|
96 |
* <p>Determines that's the first time this component is going to be painted.</p>
|
97 |
*/
|
98 |
boolean first = true; |
99 |
|
100 |
/**
|
101 |
* <p>Tool listener used to apply a <i>zoom out</i> operation in this component graphical information.</p>
|
102 |
*/
|
103 |
private MapOverviewListener movl;
|
104 |
|
105 |
/**
|
106 |
* <p>Tool listener used to allow user work with this component applying <i>pan</i> operations.</p>
|
107 |
*/
|
108 |
private MapOverviewPanListener movpl;
|
109 |
|
110 |
/**
|
111 |
* <p>Tool listener used to allow user work with this component applying <i>pan</i> operations.</p>
|
112 |
*/
|
113 |
private MapOverviewChangeZoomListener movczl;
|
114 |
|
115 |
/**
|
116 |
* <p>Rectangular area selected in this component, that will be the extent of the associated <code>MapControl</code> instance.</p>
|
117 |
*/
|
118 |
private Envelope extent;
|
119 |
|
120 |
/**
|
121 |
* <p>Double buffer used to paint this component graphical information.</p>
|
122 |
*/
|
123 |
private BufferedImage image; |
124 |
|
125 |
/**
|
126 |
* <p>Creates a <code>MapOverview</code> instance associated to <code>mapAssoc</code>.</p>
|
127 |
*
|
128 |
* @param mapAssoc <code>MapControl</code> this component will be the overview
|
129 |
*/
|
130 |
public MapOverview(MapControl mapAssoc) {
|
131 |
super();
|
132 |
this.setName("MapOverview"); |
133 |
// super.vp.setBackColor(new Color(230,230,230));
|
134 |
m_MapAssoc = mapAssoc; |
135 |
|
136 |
// setModel((FMap) m_MapAssoc.getMapContext().clone()); // Lo inicializamos con
|
137 |
// los mismos temas que tenga el grande.
|
138 |
movl = new MapOverviewListener(this); |
139 |
movpl = new MapOverviewPanListener(this); |
140 |
movczl = new MapOverviewChangeZoomListener(this); |
141 |
addMapTool( |
142 |
"zoomtopoint",
|
143 |
new Behavior[]{ |
144 |
new PointBehavior(movl),
|
145 |
new RectangleBehavior(movczl),
|
146 |
new DraggerBehavior(movczl),
|
147 |
new DraggerBehavior(movpl)
|
148 |
} |
149 |
); |
150 |
|
151 |
// setCursor(movl.getCursor());
|
152 |
|
153 |
setTool("zoomtopoint");
|
154 |
getGrid().setShowGrid(false);
|
155 |
getGrid().setAdjustGrid(false);
|
156 |
} |
157 |
|
158 |
/**
|
159 |
* @see MapControl#getMapContext()
|
160 |
*/
|
161 |
public MapContext getAssociatedMapContext() {
|
162 |
return m_MapAssoc.getMapContext();
|
163 |
} |
164 |
|
165 |
/**
|
166 |
* <p>Gets the <code>MapControl</code> instance that wrappers.</p>
|
167 |
*
|
168 |
* @return the <code>MapControl</code> instance that wrappers
|
169 |
*/
|
170 |
public MapControl getAssociatedMapControl(){
|
171 |
return m_MapAssoc;
|
172 |
} |
173 |
|
174 |
/*
|
175 |
* (non-Javadoc)
|
176 |
* @see com.iver.cit.gvsig.fmap.ViewPortListener#extentChanged(com.iver.cit.gvsig.fmap.ExtentEvent)
|
177 |
*/
|
178 |
public void extentChanged(ExtentEvent evExtent) { |
179 |
// Nos llega el nuevo extent del FMap asociado, as? que dibujamos nuestro
|
180 |
// rect?ngulo para mostrar la zona de dibujo del otro mapa.
|
181 |
repaint(); |
182 |
} |
183 |
|
184 |
/**
|
185 |
* <p>If this had no extent, calls {@link #delModel() #delModel()}, otherwise recalculates this
|
186 |
* component's view port extent as the union of all extents of all layers of this map.</p>
|
187 |
*/
|
188 |
public void refreshExtent() { |
189 |
try {
|
190 |
if (this.getMapContext().getFullEnvelope()!=null){ |
191 |
this.getMapContext().getViewPort().setEnvelope(this.getMapContext().getFullEnvelope()); |
192 |
}else{
|
193 |
delModel(); |
194 |
} |
195 |
} catch (ReadException e1) {
|
196 |
e1.printStackTrace(); |
197 |
} |
198 |
} |
199 |
|
200 |
/**
|
201 |
* <p>Repaints this component updating its <i>extent</i>.</p>
|
202 |
*
|
203 |
* @param r the new extent
|
204 |
*/
|
205 |
public void refreshOverView(Envelope r){ |
206 |
extent=r; |
207 |
repaint(); |
208 |
} |
209 |
|
210 |
/**
|
211 |
* <p>Paints this component's graphical information using a 8-bit RGBA color double buffer, drawing a red-bordered
|
212 |
* grey-filled rectangle enhancing the extent selected, and a horizontal and vertical this component's width or
|
213 |
* height, black lines centered in that rectangle.</p>
|
214 |
*/
|
215 |
protected void paintComponent(Graphics g) { |
216 |
getGrid().setShowGrid(false);
|
217 |
getGrid().setAdjustGrid(false);
|
218 |
super.paintComponent(g);
|
219 |
if ((m_MapAssoc.getMapContext().getViewPort().getExtent() != null) && |
220 |
(getMapContext().getViewPort().getExtent() != null)) {
|
221 |
if (first) {
|
222 |
first = false;
|
223 |
repaint(); |
224 |
return;
|
225 |
} |
226 |
image = new BufferedImage(this.getWidth(), this.getHeight(), |
227 |
BufferedImage.TYPE_INT_ARGB);
|
228 |
ViewPort vp = getMapContext().getViewPort(); |
229 |
Envelope extentView=vp.getAdjustedExtent(); |
230 |
ViewPort vpOrig = m_MapAssoc.getMapContext().getViewPort(); |
231 |
if (extent==null) { |
232 |
extent=vpOrig.getAdjustedExtent(); |
233 |
} |
234 |
// Dibujamos el extent del mapa asociado.
|
235 |
Graphics2D g2 = (Graphics2D) image.getGraphics(); |
236 |
g2.setTransform(vp.getAffineTransform()); |
237 |
|
238 |
g2.setStroke(new BasicStroke((float) vp.getDist1pixel())); |
239 |
|
240 |
g2.setColor(Color.red);
|
241 |
Rectangle2D extentToDraw=new Rectangle2D.Double(extent.getMinimum(0),extent.getMinimum(1),extent.getLength(0),extent.getLength(1)); |
242 |
g2.draw(extentToDraw); |
243 |
g2.setColor(new Color(100,100,100,100)); |
244 |
g2.fill(extentToDraw); |
245 |
// dibujamos las l?neas vertical y horizontal
|
246 |
Point2D pRightUp = vp.toMapPoint(getWidth(), 0); |
247 |
|
248 |
Line2D.Double linVert = new Line2D.Double(extentToDraw.getCenterX(), |
249 |
extentView.getMinimum(1), extentToDraw.getCenterX(),
|
250 |
pRightUp.getY()); |
251 |
Line2D.Double linHoriz = new Line2D.Double(extentView.getMinimum(0), |
252 |
extentToDraw.getCenterY(), pRightUp.getX(), |
253 |
extentToDraw.getCenterY()); |
254 |
|
255 |
g2.setColor(Color.darkGray);
|
256 |
g2.draw(linVert); |
257 |
g2.draw(linHoriz); |
258 |
g.drawImage(image,0,0,this); |
259 |
g2.setTransform(new AffineTransform()); |
260 |
extent=null;
|
261 |
} |
262 |
|
263 |
} |
264 |
|
265 |
/**
|
266 |
* <p>Sets a <code>MapContext</code> to this component, configuring it to manage view port events produced
|
267 |
* in the associated <code>MapControl</code> and this one's view port.</p>
|
268 |
*
|
269 |
* <p>This <code>MapContext</code>'s projection will be the same as the associated <code>MapControl</code>'s one.</p>
|
270 |
*
|
271 |
* <p>Setting the model includes the following steps:
|
272 |
* <ul>
|
273 |
* <li><b>1.-</b> set <code>model</code> as the <code>MapContext</code> of <code>this</code>.</li>
|
274 |
* <li><b>2.-</b> set as <code>model</code> projection, the associated <code>MapControl<code>'s projection.</li>
|
275 |
* <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>
|
276 |
* <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>
|
277 |
* </ul>
|
278 |
* </p>
|
279 |
*
|
280 |
* @param model data to set
|
281 |
*/
|
282 |
public void setModel(MapContext model) { |
283 |
this.setMapContext(model);
|
284 |
model.setProjection(m_MapAssoc.getMapContext().getProjection()); |
285 |
m_MapAssoc.getMapContext().getViewPort().addViewPortListener(this);
|
286 |
getMapContext().getViewPort().addViewPortListener(this);
|
287 |
} |
288 |
|
289 |
/**
|
290 |
* <p>Removes this component as listener of the the associated <code>MapControl</code> and this one's view port. Besides,
|
291 |
* removes the extent.</p>
|
292 |
*/
|
293 |
private void delModel(){ |
294 |
this.getMapContext().getViewPort().setEnvelope(null); |
295 |
m_MapAssoc.getMapContext().getViewPort().removeViewPortListener(this);
|
296 |
getMapContext().getViewPort().removeViewPortListener(this);
|
297 |
} |
298 |
|
299 |
/*
|
300 |
* (non-Javadoc)
|
301 |
* @see com.iver.cit.gvsig.fmap.ViewPortListener#backColorChanged(com.iver.cit.gvsig.fmap.ColorEvent)
|
302 |
*/
|
303 |
public void backColorChanged(ColorEvent e) { |
304 |
} |
305 |
|
306 |
/**
|
307 |
* @see ViewPortListener#projectionChanged(ProjectionEvent)
|
308 |
*
|
309 |
* @see MapControl#setProjection(IProjection)
|
310 |
*/
|
311 |
public void projectionChanged(ProjectionEvent e) { |
312 |
super.setProjection(e.getNewProjection());
|
313 |
|
314 |
} |
315 |
|
316 |
/**
|
317 |
* <p>Unimplemented.</p>
|
318 |
*
|
319 |
* <p>Can't change the projection, because must be the same as the one of the
|
320 |
* associated <code>MapControl</code> instance.</p>
|
321 |
*/
|
322 |
public void setProjection(IProjection proj) { |
323 |
//No permitimos cambiar la proyeccion
|
324 |
//ya que debe ser la misma que la del
|
325 |
//MapControl asociado
|
326 |
return;
|
327 |
} |
328 |
} |