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