svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / project / documents / view / MapOverview.java @ 21743
History | View | Annotate | Download (10.7 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 com.iver.cit.gvsig.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.data.ReadException; |
61 |
import org.gvsig.fmap.geom.primitive.Envelope; |
62 |
import org.gvsig.fmap.mapcontext.MapContext; |
63 |
import org.gvsig.fmap.mapcontext.ViewPort; |
64 |
import org.gvsig.fmap.mapcontext.events.ColorEvent; |
65 |
import org.gvsig.fmap.mapcontext.events.ExtentEvent; |
66 |
import org.gvsig.fmap.mapcontext.events.ProjectionEvent; |
67 |
import org.gvsig.fmap.mapcontext.events.listeners.ViewPortListener; |
68 |
import org.gvsig.fmap.mapcontrol.MapControl; |
69 |
import org.gvsig.fmap.mapcontrol.tools.Behavior.Behavior; |
70 |
import org.gvsig.fmap.mapcontrol.tools.Behavior.DraggerBehavior; |
71 |
import org.gvsig.fmap.mapcontrol.tools.Behavior.PointBehavior; |
72 |
import org.gvsig.fmap.mapcontrol.tools.Behavior.RectangleBehavior; |
73 |
|
74 |
import com.iver.cit.gvsig.project.documents.view.toolListeners.MapOverviewChangeZoomListener; |
75 |
import com.iver.cit.gvsig.project.documents.view.toolListeners.MapOverviewListener; |
76 |
import com.iver.cit.gvsig.project.documents.view.toolListeners.MapOverviewPanListener; |
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 |
} |
155 |
|
156 |
/**
|
157 |
* @see MapControl#getMapContext()
|
158 |
*/
|
159 |
public MapContext getAssociatedMapContext() {
|
160 |
return m_MapAssoc.getMapContext();
|
161 |
} |
162 |
|
163 |
/**
|
164 |
* <p>Gets the <code>MapControl</code> instance that wrappers.</p>
|
165 |
*
|
166 |
* @return the <code>MapControl</code> instance that wrappers
|
167 |
*/
|
168 |
public MapControl getAssociatedMapControl(){
|
169 |
return m_MapAssoc;
|
170 |
} |
171 |
|
172 |
/*
|
173 |
* (non-Javadoc)
|
174 |
* @see com.iver.cit.gvsig.fmap.ViewPortListener#extentChanged(com.iver.cit.gvsig.fmap.ExtentEvent)
|
175 |
*/
|
176 |
public void extentChanged(ExtentEvent evExtent) { |
177 |
// Nos llega el nuevo extent del FMap asociado, as? que dibujamos nuestro
|
178 |
// rect?ngulo para mostrar la zona de dibujo del otro mapa.
|
179 |
repaint(); |
180 |
} |
181 |
|
182 |
/**
|
183 |
* <p>If this had no extent, calls {@link #delModel() #delModel()}, otherwise recalculates this
|
184 |
* component's view port extent as the union of all extents of all layers of this map.</p>
|
185 |
*/
|
186 |
public void refreshExtent() { |
187 |
try {
|
188 |
if (this.getMapContext().getFullEnvelope()!=null){ |
189 |
this.getMapContext().getViewPort().setEnvelope(this.getMapContext().getFullEnvelope()); |
190 |
}else{
|
191 |
delModel(); |
192 |
} |
193 |
} catch (ReadException e1) {
|
194 |
e1.printStackTrace(); |
195 |
} |
196 |
} |
197 |
|
198 |
/**
|
199 |
* <p>Repaints this component updating its <i>extent</i>.</p>
|
200 |
*
|
201 |
* @param r the new extent
|
202 |
*/
|
203 |
public void refreshOverView(Envelope r){ |
204 |
extent=r; |
205 |
repaint(); |
206 |
} |
207 |
|
208 |
/**
|
209 |
* <p>Paints this component's graphical information using a 8-bit RGBA color double buffer, drawing a red-bordered
|
210 |
* grey-filled rectangle enhancing the extent selected, and a horizontal and vertical this component's width or
|
211 |
* height, black lines centered in that rectangle.</p>
|
212 |
*/
|
213 |
protected void paintComponent(Graphics g) { |
214 |
super.paintComponent(g);
|
215 |
if ((m_MapAssoc.getMapContext().getViewPort().getExtent() != null) && |
216 |
(getMapContext().getViewPort().getExtent() != null)) {
|
217 |
if (first) {
|
218 |
first = false;
|
219 |
repaint(); |
220 |
return;
|
221 |
} |
222 |
image = new BufferedImage(this.getWidth(), this.getHeight(), |
223 |
BufferedImage.TYPE_INT_ARGB);
|
224 |
ViewPort vp = getMapContext().getViewPort(); |
225 |
Envelope extentView=vp.getAdjustedExtent(); |
226 |
ViewPort vpOrig = m_MapAssoc.getMapContext().getViewPort(); |
227 |
if (extent==null) |
228 |
extent=vpOrig.getAdjustedExtent(); |
229 |
// Dibujamos el extent del mapa asociado.
|
230 |
Graphics2D g2 = (Graphics2D) image.getGraphics(); |
231 |
g2.setTransform(vp.getAffineTransform()); |
232 |
|
233 |
g2.setStroke(new BasicStroke((float) vp.getDist1pixel())); |
234 |
|
235 |
g2.setColor(Color.red);
|
236 |
Rectangle2D extentToDraw=new Rectangle2D.Double(extent.getMinimum(0),extent.getMinimum(1),extent.getLength(0),extent.getLength(1)); |
237 |
g2.draw(extentToDraw); |
238 |
g2.setColor(new Color(100,100,100,100)); |
239 |
g2.fill(extentToDraw); |
240 |
// dibujamos las l?neas vertical y horizontal
|
241 |
Point2D pRightUp = vp.toMapPoint(getWidth(), 0); |
242 |
|
243 |
Line2D.Double linVert = new Line2D.Double(extentToDraw.getCenterX(), |
244 |
extentView.getMinimum(1), extentToDraw.getCenterX(),
|
245 |
pRightUp.getY()); |
246 |
Line2D.Double linHoriz = new Line2D.Double(extentView.getMinimum(0), |
247 |
extentToDraw.getCenterY(), pRightUp.getX(), |
248 |
extentToDraw.getCenterY()); |
249 |
|
250 |
g2.setColor(Color.darkGray);
|
251 |
g2.draw(linVert); |
252 |
g2.draw(linHoriz); |
253 |
g.drawImage(image,0,0,this); |
254 |
g2.setTransform(new AffineTransform()); |
255 |
extent=null;
|
256 |
} |
257 |
|
258 |
} |
259 |
|
260 |
/**
|
261 |
* <p>Sets a <code>MapContext</code> to this component, configuring it to manage view port events produced
|
262 |
* in the associated <code>MapControl</code> and this one's view port.</p>
|
263 |
*
|
264 |
* <p>This <code>MapContext</code>'s projection will be the same as the associated <code>MapControl</code>'s one.</p>
|
265 |
*
|
266 |
* <p>Setting the model includes the following steps:
|
267 |
* <ul>
|
268 |
* <li><b>1.-</b> set <code>model</code> as the <code>MapContext</code> of <code>this</code>.</li>
|
269 |
* <li><b>2.-</b> set as <code>model</code> projection, the associated <code>MapControl<code>'s projection.</li>
|
270 |
* <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>
|
271 |
* <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>
|
272 |
* </ul>
|
273 |
* </p>
|
274 |
*
|
275 |
* @param model data to set
|
276 |
*/
|
277 |
public void setModel(MapContext model) { |
278 |
this.setMapContext(model);
|
279 |
model.setProjection(m_MapAssoc.getMapContext().getProjection()); |
280 |
m_MapAssoc.getMapContext().getViewPort().addViewPortListener(this);
|
281 |
getMapContext().getViewPort().addViewPortListener(this);
|
282 |
} |
283 |
|
284 |
/**
|
285 |
* <p>Removes this component as listener of the the associated <code>MapControl</code> and this one's view port. Besides,
|
286 |
* removes the extent.</p>
|
287 |
*/
|
288 |
private void delModel(){ |
289 |
this.getMapContext().getViewPort().setEnvelope(null); |
290 |
m_MapAssoc.getMapContext().getViewPort().removeViewPortListener(this);
|
291 |
getMapContext().getViewPort().removeViewPortListener(this);
|
292 |
} |
293 |
|
294 |
/*
|
295 |
* (non-Javadoc)
|
296 |
* @see com.iver.cit.gvsig.fmap.ViewPortListener#backColorChanged(com.iver.cit.gvsig.fmap.ColorEvent)
|
297 |
*/
|
298 |
public void backColorChanged(ColorEvent e) { |
299 |
} |
300 |
|
301 |
/**
|
302 |
* @see ViewPortListener#projectionChanged(ProjectionEvent)
|
303 |
*
|
304 |
* @see MapControl#setProjection(IProjection)
|
305 |
*/
|
306 |
public void projectionChanged(ProjectionEvent e) { |
307 |
super.setProjection(e.getNewProjection());
|
308 |
|
309 |
} |
310 |
|
311 |
/**
|
312 |
* <p>Unimplemented.</p>
|
313 |
*
|
314 |
* <p>Can't change the projection, because must be the same as the one of the
|
315 |
* associated <code>MapControl</code> instance.</p>
|
316 |
*/
|
317 |
public void setProjection(IProjection proj) { |
318 |
//No permitimos cambiar la proyeccion
|
319 |
//ya que debe ser la misma que la del
|
320 |
//MapControl asociado
|
321 |
return;
|
322 |
} |
323 |
} |