source: osm/applications/editors/josm/plugins/wms-turbo-challenge2/src/wmsturbochallenge/FakeMapView.java

Last change on this file was 36483, checked in by stoecker, 8 months ago

set eol-style, fix checkstyle issues, add ignores

  • Property svn:eol-style set to native
File size: 4.5 KB
RevLine 
[36483]1// License: GPL. For details, see LICENSE file.
[33342]2package wmsturbochallenge;
3
4import java.awt.Graphics;
5import java.awt.Graphics2D;
6import java.awt.Point;
7import java.awt.image.BufferedImage;
8
9import org.openstreetmap.josm.data.ProjectionBounds;
10import org.openstreetmap.josm.data.coor.EastNorth;
11import org.openstreetmap.josm.gui.MapView;
[35045]12import org.openstreetmap.josm.gui.layer.MainLayerManager;
[33342]13
14/**
[19990]15 * Implements a fake MapView that we can pass to WMSLayer's .paint,
16 * this will give us two things:
17 * # We'll be able to tell WMSLayer.paint() what area we want it
18 * to download (it ignores the "bounds" parameter) and override
19 * isVisible and friends as needed, and
20 * # We'll receive notifications from Grabber when we need to
21 * repaint (and call WMSLayer's .paint again) because the
22 * Grabber downloaded some or all of the tiles that we asked
23 * WMSLayer for and WMSLayer created the Grabber passing it
24 * our MapView. Otherwise we wouldn't be able to tell when
25 * this happened and could only guess.
[36483]26 * @author Andrzej Zaborowski
[19990]27 */
[29854]28class FakeMapView extends MapView {
[23190]29 public ProjectionBounds view_bounds;
30 public MapView parent;
[19990]31
[23190]32 public Graphics2D graphics;
33 public BufferedImage ground_image;
34 public int ground_width = -1;
35 public int ground_height = -1;
36 public double scale;
37 public double max_east_west;
[19990]38
[33342]39 FakeMapView(MapView parent, double scale) {
40 // TODO: MapView constructor contains registering listeners and other code, that probably shouldn't be called in fake map view
[35045]41 super(new MainLayerManager(), null);
[23190]42 this.parent = parent;
43 this.scale = scale;
[19990]44
[23190]45 ProjectionBounds parent_bounds = parent.getProjectionBounds();
46 max_east_west =
[32914]47 parent_bounds.maxEast - parent_bounds.minEast;
[23190]48 }
[19990]49
[23190]50 public void setProjectionBounds(ProjectionBounds bounds) {
51 view_bounds = bounds;
[19990]52
[25935]53 if (bounds.maxEast - bounds.minEast > max_east_west) {
54 max_east_west = bounds.maxEast - bounds.minEast;
[19990]55
[23190]56 /* We need to set the parent MapView's bounds (i.e.
57 * zoom level) to the same as ours max possible
58 * bounds to avoid WMSLayer thinking we're zoomed
59 * out more than we are or it'll pop up an annoying
60 * "requested area is too large" popup.
61 */
62 EastNorth parent_center = parent.getCenter();
63 parent.zoomTo(new ProjectionBounds(
64 new EastNorth(
[32914]65 parent_center.east() -
66 max_east_west / 2,
67 parent_center.north()),
[23190]68 new EastNorth(
[32914]69 parent_center.east() +
70 max_east_west / 2,
71 parent_center.north())));
[19990]72
[23190]73 /* Request again because NavigatableContent adds
74 * a border just to be sure.
75 */
76 ProjectionBounds new_bounds =
[32914]77 parent.getProjectionBounds();
[23190]78 max_east_west =
[32914]79 new_bounds.maxEast - new_bounds.minEast;
[23190]80 }
[19990]81
[25935]82 Point vmin = getPoint(bounds.getMin());
83 Point vmax = getPoint(bounds.getMax());
[23190]84 int w = vmax.x + 1;
85 int h = vmin.y + 1;
[19990]86
[23190]87 if (w <= ground_width && h <= ground_height) {
88 graphics.setClip(0, 0, w, h);
89 return;
90 }
[19990]91
[23190]92 if (w > ground_width)
93 ground_width = w;
94 if (h > ground_height)
95 ground_height = h;
[19990]96
[23190]97 ground_image = new BufferedImage(ground_width,
98 ground_height,
99 BufferedImage.TYPE_INT_RGB);
100 graphics = ground_image.createGraphics();
101 graphics.setClip(0, 0, w, h);
102 }
[19990]103
[32914]104 @Override
[23190]105 public ProjectionBounds getProjectionBounds() {
106 return view_bounds;
107 }
[19990]108
[32914]109 @Override
[23190]110 public Point getPoint(EastNorth p) {
[25935]111 double x = p.east() - view_bounds.minEast;
112 double y = view_bounds.maxNorth - p.north();
[23190]113 x /= this.scale;
114 y /= this.scale;
[19990]115
[23190]116 return new Point((int) x, (int) y);
117 }
[19990]118
[32914]119 @Override
[23190]120 public EastNorth getEastNorth(int x, int y) {
121 return new EastNorth(
[32914]122 view_bounds.minEast + x * this.scale,
123 view_bounds.minNorth - y * this.scale);
[23190]124 }
[19990]125
[23190]126 public boolean isVisible(int x, int y) {
127 return true;
128 }
[19990]129
[32914]130 @Override
[23190]131 public Graphics getGraphics() {
132 return graphics;
133 }
[19990]134
[32914]135 @Override
[23190]136 public void repaint() {
137 }
[19990]138}
Note: See TracBrowser for help on using the repository browser.