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

Last change on this file since 35045 was 35045, checked in by donvip, 5 years ago

fix #josm17859 - NPE

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