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

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

fix #josm17859 - NPE

File size: 4.4 KB
RevLine 
[19990]1/*
2 * GPLv2 or 3, Copyright (c) 2010 Andrzej Zaborowski
[33342]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;
[35045]14import org.openstreetmap.josm.gui.layer.MainLayerManager;
[33342]15
16/**
[19990]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 */
[29854]29class FakeMapView extends MapView {
[23190]30 public ProjectionBounds view_bounds;
31 public MapView parent;
[19990]32
[23190]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;
[19990]39
[33342]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
[35045]42 super(new MainLayerManager(), null);
[23190]43 this.parent = parent;
44 this.scale = scale;
[19990]45
[23190]46 ProjectionBounds parent_bounds = parent.getProjectionBounds();
47 max_east_west =
[32914]48 parent_bounds.maxEast - parent_bounds.minEast;
[23190]49 }
[19990]50
[23190]51 public void setProjectionBounds(ProjectionBounds bounds) {
52 view_bounds = bounds;
[19990]53
[25935]54 if (bounds.maxEast - bounds.minEast > max_east_west) {
55 max_east_west = bounds.maxEast - bounds.minEast;
[19990]56
[23190]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(
[32914]66 parent_center.east() -
67 max_east_west / 2,
68 parent_center.north()),
[23190]69 new EastNorth(
[32914]70 parent_center.east() +
71 max_east_west / 2,
72 parent_center.north())));
[19990]73
[23190]74 /* Request again because NavigatableContent adds
75 * a border just to be sure.
76 */
77 ProjectionBounds new_bounds =
[32914]78 parent.getProjectionBounds();
[23190]79 max_east_west =
[32914]80 new_bounds.maxEast - new_bounds.minEast;
[23190]81 }
[19990]82
[25935]83 Point vmin = getPoint(bounds.getMin());
84 Point vmax = getPoint(bounds.getMax());
[23190]85 int w = vmax.x + 1;
86 int h = vmin.y + 1;
[19990]87
[23190]88 if (w <= ground_width && h <= ground_height) {
89 graphics.setClip(0, 0, w, h);
90 return;
91 }
[19990]92
[23190]93 if (w > ground_width)
94 ground_width = w;
95 if (h > ground_height)
96 ground_height = h;
[19990]97
[23190]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 }
[19990]104
[32914]105 @Override
[23190]106 public ProjectionBounds getProjectionBounds() {
107 return view_bounds;
108 }
[19990]109
[32914]110 @Override
[23190]111 public Point getPoint(EastNorth p) {
[25935]112 double x = p.east() - view_bounds.minEast;
113 double y = view_bounds.maxNorth - p.north();
[23190]114 x /= this.scale;
115 y /= this.scale;
[19990]116
[23190]117 return new Point((int) x, (int) y);
118 }
[19990]119
[32914]120 @Override
[23190]121 public EastNorth getEastNorth(int x, int y) {
122 return new EastNorth(
[32914]123 view_bounds.minEast + x * this.scale,
124 view_bounds.minNorth - y * this.scale);
[23190]125 }
[19990]126
[23190]127 public boolean isVisible(int x, int y) {
128 return true;
129 }
[19990]130
[32914]131 @Override
[23190]132 public Graphics getGraphics() {
133 return graphics;
134 }
[19990]135
[32914]136 @Override
[23190]137 public void repaint() {
138 }
[19990]139}
Note: See TracBrowser for help on using the repository browser.