source: josm/trunk/src/org/openstreetmap/josm/gui/MapViewState.java@ 10619

Last change on this file since 10619 was 10596, checked in by Don-vip, 8 years ago

see #11390, see #13120, see #13190 - sonar - squid:S1612 - Lambdas should be replaced with method references

  • Property svn:eol-style set to native
File size: 14.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui;
3
4import java.awt.Container;
5import java.awt.Point;
6import java.awt.Rectangle;
7import java.awt.geom.AffineTransform;
8import java.awt.geom.Point2D;
9import java.awt.geom.Point2D.Double;
10
11import javax.swing.JComponent;
12
13import org.openstreetmap.josm.Main;
14import org.openstreetmap.josm.data.Bounds;
15import org.openstreetmap.josm.data.ProjectionBounds;
16import org.openstreetmap.josm.data.coor.EastNorth;
17import org.openstreetmap.josm.data.coor.LatLon;
18import org.openstreetmap.josm.data.projection.Projection;
19import org.openstreetmap.josm.gui.download.DownloadDialog;
20import org.openstreetmap.josm.tools.bugreport.BugReport;
21
22/**
23 * This class represents a state of the {@link MapView}.
24 * @author Michael Zangl
25 * @since 10343
26 */
27public final class MapViewState {
28
29 private final Projection projection;
30
31 private final int viewWidth;
32 private final int viewHeight;
33
34 private final double scale;
35
36 /**
37 * Top left {@link EastNorth} coordinate of the view.
38 */
39 private final EastNorth topLeft;
40
41 private final Point topLeftOnScreen;
42 private final Point topLeftInWindow;
43
44 /**
45 * Create a new {@link MapViewState}
46 * @param projection The projection to use.
47 * @param viewWidth The view width
48 * @param viewHeight The view height
49 * @param scale The scale to use
50 * @param topLeft The top left corner in east/north space.
51 */
52 private MapViewState(Projection projection, int viewWidth, int viewHeight, double scale, EastNorth topLeft) {
53 this.projection = projection;
54 this.scale = scale;
55 this.topLeft = topLeft;
56
57 this.viewWidth = viewWidth;
58 this.viewHeight = viewHeight;
59 topLeftInWindow = new Point(0, 0);
60 topLeftOnScreen = new Point(0, 0);
61 }
62
63 private MapViewState(EastNorth topLeft, MapViewState mapViewState) {
64 this.projection = mapViewState.projection;
65 this.scale = mapViewState.scale;
66 this.topLeft = topLeft;
67
68 viewWidth = mapViewState.viewWidth;
69 viewHeight = mapViewState.viewHeight;
70 topLeftInWindow = mapViewState.topLeftInWindow;
71 topLeftOnScreen = mapViewState.topLeftOnScreen;
72 }
73
74 private MapViewState(double scale, MapViewState mapViewState) {
75 this.projection = mapViewState.projection;
76 this.scale = scale;
77 this.topLeft = mapViewState.topLeft;
78
79 viewWidth = mapViewState.viewWidth;
80 viewHeight = mapViewState.viewHeight;
81 topLeftInWindow = mapViewState.topLeftInWindow;
82 topLeftOnScreen = mapViewState.topLeftOnScreen;
83 }
84
85 private MapViewState(JComponent position, MapViewState mapViewState) {
86 this.projection = mapViewState.projection;
87 this.scale = mapViewState.scale;
88 this.topLeft = mapViewState.topLeft;
89
90 viewWidth = position.getWidth();
91 viewHeight = position.getHeight();
92 topLeftInWindow = new Point();
93 // better than using swing utils, since this allows us to use the mehtod if no screen is present.
94 Container component = position;
95 while (component != null) {
96 topLeftInWindow.x += component.getX();
97 topLeftInWindow.y += component.getY();
98 component = component.getParent();
99 }
100 try {
101 topLeftOnScreen = position.getLocationOnScreen();
102 } catch (RuntimeException e) {
103 throw BugReport.intercept(e).put("position", position).put("parent", position::getParent);
104 }
105 }
106
107 private MapViewState(Projection projection, MapViewState mapViewState) {
108 this.projection = projection;
109 this.scale = mapViewState.scale;
110 this.topLeft = mapViewState.topLeft;
111
112 viewWidth = mapViewState.viewWidth;
113 viewHeight = mapViewState.viewHeight;
114 topLeftInWindow = mapViewState.topLeftInWindow;
115 topLeftOnScreen = mapViewState.topLeftOnScreen;
116 }
117
118 /**
119 * The scale in east/north units per pixel.
120 * @return The scale.
121 */
122 public double getScale() {
123 return scale;
124 }
125
126 /**
127 * Gets the MapViewPoint representation for a position in view coordinates.
128 * @param x The x coordinate inside the view.
129 * @param y The y coordinate inside the view.
130 * @return The MapViewPoint.
131 */
132 public MapViewPoint getForView(double x, double y) {
133 return new MapViewViewPoint(x, y);
134 }
135
136 /**
137 * Gets the {@link MapViewPoint} for the given {@link EastNorth} coordinate.
138 * @param eastNorth the position.
139 * @return The point for that position.
140 */
141 public MapViewPoint getPointFor(EastNorth eastNorth) {
142 return new MapViewEastNorthPoint(eastNorth);
143 }
144
145 /**
146 * Gets a rectangle representing the whole view area.
147 * @return The rectangle.
148 */
149 public MapViewRectangle getViewArea() {
150 return getForView(0, 0).rectTo(getForView(viewWidth, viewHeight));
151 }
152
153 /**
154 * Gets a rectangle of the view as map view area.
155 * @param rectangle The rectangle to get.
156 * @return The view area.
157 * @since 10458
158 */
159 public MapViewRectangle getViewArea(Rectangle rectangle) {
160 return getForView(rectangle.getMinX(), rectangle.getMinY()).rectTo(getForView(rectangle.getMaxX(), rectangle.getMaxY()));
161 }
162
163 /**
164 * Gets the center of the view.
165 * @return The center position.
166 */
167 public MapViewPoint getCenter() {
168 return getForView(viewWidth / 2.0, viewHeight / 2.0);
169 }
170
171 /**
172 * Gets the width of the view on the Screen;
173 * @return The width of the view component in screen pixel.
174 */
175 public double getViewWidth() {
176 return viewWidth;
177 }
178
179 /**
180 * Gets the height of the view on the Screen;
181 * @return The height of the view component in screen pixel.
182 */
183 public double getViewHeight() {
184 return viewHeight;
185 }
186
187 /**
188 * Gets the current projection used for the MapView.
189 * @return The projection.
190 */
191 public Projection getProjection() {
192 return projection;
193 }
194
195 /**
196 * Creates an affine transform that is used to convert the east/north coordinates to view coordinates.
197 * @return The affine transform. It should not be changed.
198 * @since 10375
199 */
200 public AffineTransform getAffineTransform() {
201 return new AffineTransform(1.0 / scale, 0.0, 0.0, -1.0 / scale, -topLeft.east() / scale,
202 topLeft.north() / scale);
203 }
204
205 /**
206 * Creates a new state that is the same as the current state except for that it is using a new center.
207 * @param newCenter The new center coordinate.
208 * @return The new state.
209 * @since 10375
210 */
211 public MapViewState usingCenter(EastNorth newCenter) {
212 return movedTo(getCenter(), newCenter);
213 }
214
215 /**
216 * @param mapViewPoint The reference point.
217 * @param newEastNorthThere The east/north coordinate that should be there.
218 * @return The new state.
219 * @since 10375
220 */
221 public MapViewState movedTo(MapViewPoint mapViewPoint, EastNorth newEastNorthThere) {
222 EastNorth delta = newEastNorthThere.subtract(mapViewPoint.getEastNorth());
223 if (delta.distanceSq(0, 0) < .1e-20) {
224 return this;
225 } else {
226 return new MapViewState(topLeft.add(delta), this);
227 }
228 }
229
230 /**
231 * Creates a new state that is the same as the current state except for that it is using a new scale.
232 * @param newScale The new scale to use.
233 * @return The new state.
234 * @since 10375
235 */
236 public MapViewState usingScale(double newScale) {
237 return new MapViewState(newScale, this);
238 }
239
240 /**
241 * Creates a new state that is the same as the current state except for that it is using the location of the given component.
242 * <p>
243 * The view is moved so that the center is the same as the old center.
244 * @param positon The new location to use.
245 * @return The new state.
246 * @since 10375
247 */
248 public MapViewState usingLocation(JComponent positon) {
249 EastNorth center = this.getCenter().getEastNorth();
250 return new MapViewState(positon, this).usingCenter(center);
251 }
252
253 /**
254 * Creates a state that uses the projection.
255 * @param projection The projection to use.
256 * @return The new state.
257 * @since 10486
258 */
259 public MapViewState usingProjection(Projection projection) {
260 if (projection.equals(this.projection)) {
261 return this;
262 } else {
263 return new MapViewState(projection, this);
264 }
265 }
266
267 /**
268 * Create the default {@link MapViewState} object for the given map view. The screen position won't be set so that this method can be used
269 * before the view was added to the hirarchy.
270 * @param width The view width
271 * @param height The view height
272 * @return The state
273 * @since 10375
274 */
275 public static MapViewState createDefaultState(int width, int height) {
276 Projection projection = Main.getProjection();
277 double scale = projection.getDefaultZoomInPPD();
278 MapViewState state = new MapViewState(projection, width, height, scale, new EastNorth(0, 0));
279 EastNorth center = calculateDefaultCenter();
280 return state.movedTo(state.getCenter(), center);
281 }
282
283 private static EastNorth calculateDefaultCenter() {
284 Bounds b = DownloadDialog.getSavedDownloadBounds();
285 if (b == null) {
286 b = Main.getProjection().getWorldBoundsLatLon();
287 }
288 return Main.getProjection().latlon2eastNorth(b.getCenter());
289 }
290
291 /**
292 * A class representing a point in the map view. It allows to convert between the different coordinate systems.
293 * @author Michael Zangl
294 */
295 public abstract class MapViewPoint {
296
297 /**
298 * Get this point in view coordinates.
299 * @return The point in view coordinates.
300 */
301 public Point2D getInView() {
302 return new Point2D.Double(getInViewX(), getInViewY());
303 }
304
305 protected abstract double getInViewX();
306
307 protected abstract double getInViewY();
308
309 /**
310 * Convert this point to window coordinates.
311 * @return The point in window coordinates.
312 */
313 public Point2D getInWindow() {
314 return getUsingCorner(topLeftInWindow);
315 }
316
317 /**
318 * Convert this point to screen coordinates.
319 * @return The point in screen coordinates.
320 */
321 public Point2D getOnScreen() {
322 return getUsingCorner(topLeftOnScreen);
323 }
324
325 private Double getUsingCorner(Point corner) {
326 return new Point2D.Double(corner.getX() + getInViewX(), corner.getY() + getInViewY());
327 }
328
329 /**
330 * Gets the {@link EastNorth} coordinate of this point.
331 * @return The east/north coordinate.
332 */
333 public EastNorth getEastNorth() {
334 return new EastNorth(topLeft.east() + getInViewX() * scale, topLeft.north() - getInViewY() * scale);
335 }
336
337 /**
338 * Create a rectangle from this to the other point.
339 * @param other The other point. Needs to be of the same {@link MapViewState}
340 * @return A rectangle.
341 */
342 public MapViewRectangle rectTo(MapViewPoint other) {
343 return new MapViewRectangle(this, other);
344 }
345
346 /**
347 * Gets the current position in LatLon coordinates according to the current projection.
348 * @return The positon as LatLon.
349 */
350 public LatLon getLatLon() {
351 return projection.eastNorth2latlon(getEastNorth());
352 }
353 }
354
355 private class MapViewViewPoint extends MapViewPoint {
356 private final double x;
357 private final double y;
358
359 MapViewViewPoint(double x, double y) {
360 this.x = x;
361 this.y = y;
362 }
363
364 @Override
365 protected double getInViewX() {
366 return x;
367 }
368
369 @Override
370 protected double getInViewY() {
371 return y;
372 }
373
374 @Override
375 public String toString() {
376 return "MapViewViewPoint [x=" + x + ", y=" + y + ']';
377 }
378 }
379
380 private class MapViewEastNorthPoint extends MapViewPoint {
381
382 private final EastNorth eastNorth;
383
384 MapViewEastNorthPoint(EastNorth eastNorth) {
385 this.eastNorth = eastNorth;
386 }
387
388 @Override
389 protected double getInViewX() {
390 return (eastNorth.east() - topLeft.east()) / scale;
391 }
392
393 @Override
394 protected double getInViewY() {
395 return (topLeft.north() - eastNorth.north()) / scale;
396 }
397
398 @Override
399 public EastNorth getEastNorth() {
400 return eastNorth;
401 }
402
403 @Override
404 public String toString() {
405 return "MapViewEastNorthPoint [eastNorth=" + eastNorth + ']';
406 }
407 }
408
409 /**
410 * A rectangle on the MapView. It is rectangular in screen / EastNorth space.
411 * @author Michael Zangl
412 */
413 public class MapViewRectangle {
414 private final MapViewPoint p1;
415 private final MapViewPoint p2;
416
417 /**
418 * Create a new MapViewRectangle
419 * @param p1 The first point to use
420 * @param p2 The second point to use.
421 */
422 MapViewRectangle(MapViewPoint p1, MapViewPoint p2) {
423 this.p1 = p1;
424 this.p2 = p2;
425 }
426
427 /**
428 * Gets the projection bounds for this rectangle.
429 * @return The projection bounds.
430 */
431 public ProjectionBounds getProjectionBounds() {
432 ProjectionBounds b = new ProjectionBounds(p1.getEastNorth());
433 b.extend(p2.getEastNorth());
434 return b;
435 }
436
437 /**
438 * Gets a rough estimate of the bounds by assuming lat/lon are parallel to x/y.
439 * @return The bounds computed by converting the corners of this rectangle.
440 * @see #getLatLonBoundsBox()
441 */
442 public Bounds getCornerBounds() {
443 Bounds b = new Bounds(p1.getLatLon());
444 b.extend(p2.getLatLon());
445 return b;
446 }
447
448 /**
449 * Gets the real bounds that enclose this rectangle.
450 * This is computed respecting that the borders of this rectangle may not be a straignt line in latlon coordinates.
451 * @return The bounds.
452 * @since 10458
453 */
454 public Bounds getLatLonBoundsBox() {
455 return projection.getLatLonBoundsBox(getProjectionBounds());
456 }
457 }
458
459}
Note: See TracBrowser for help on using the repository browser.