1 | package org.openstreetmap.josm.gui.download;
|
---|
2 |
|
---|
3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
4 |
|
---|
5 | import java.awt.BorderLayout;
|
---|
6 | import java.awt.Color;
|
---|
7 | import java.awt.Dimension;
|
---|
8 | import java.awt.Graphics;
|
---|
9 | import java.awt.Point;
|
---|
10 | import java.awt.Rectangle;
|
---|
11 | import java.beans.PropertyChangeListener;
|
---|
12 | import java.net.URL;
|
---|
13 |
|
---|
14 | import javax.swing.ImageIcon;
|
---|
15 | import javax.swing.JLabel;
|
---|
16 | import javax.swing.JPanel;
|
---|
17 |
|
---|
18 | import org.openstreetmap.josm.Main;
|
---|
19 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
20 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
21 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
22 | import org.openstreetmap.josm.gui.MapMover;
|
---|
23 | import org.openstreetmap.josm.gui.MapScaler;
|
---|
24 | import org.openstreetmap.josm.gui.NavigatableComponent;
|
---|
25 | import org.openstreetmap.josm.gui.SelectionManager;
|
---|
26 | import org.openstreetmap.josm.gui.SelectionManager.SelectionEnded;
|
---|
27 |
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * A component that let the user select a lat/lon bounding box from zooming
|
---|
31 | * into the world as a picture and selecting a region.
|
---|
32 | *
|
---|
33 | * The component has to be of the aspect ration 2:1 to look good.
|
---|
34 | *
|
---|
35 | * @author imi
|
---|
36 | */
|
---|
37 | public class WorldChooser extends NavigatableComponent implements DownloadSelection {
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * The world as picture.
|
---|
41 | */
|
---|
42 | private ImageIcon world;
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Maximum scale level
|
---|
46 | */
|
---|
47 | private double scaleMax;
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * Mark this rectangle (lat/lon values) when painting.
|
---|
51 | */
|
---|
52 | private EastNorth markerMin, markerMax;
|
---|
53 |
|
---|
54 | private Projection projection;
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Create the chooser component.
|
---|
58 | */
|
---|
59 | public WorldChooser() {
|
---|
60 | URL path = Main.class.getResource("/images/world.jpg");
|
---|
61 | world = new ImageIcon(path);
|
---|
62 | center = new EastNorth(world.getIconWidth()/2, world.getIconHeight()/2);
|
---|
63 | setPreferredSize(new Dimension(400, 200));
|
---|
64 |
|
---|
65 | projection = new Projection() {
|
---|
66 | public EastNorth latlon2eastNorth(LatLon p) {
|
---|
67 | return new EastNorth(
|
---|
68 | (p.lon()+180) / 360 * world.getIconWidth(),
|
---|
69 | (p.lat()+90) / 180 * world.getIconHeight());
|
---|
70 | }
|
---|
71 | public LatLon eastNorth2latlon(EastNorth p) {
|
---|
72 | return new LatLon(
|
---|
73 | p.north()*180/world.getIconHeight() - 90,
|
---|
74 | p.east()*360/world.getIconWidth() - 180);
|
---|
75 | }
|
---|
76 | @Override public String toString() {
|
---|
77 | return "WorldChooser";
|
---|
78 | }
|
---|
79 | public String getCacheDirectoryName() {
|
---|
80 | throw new UnsupportedOperationException();
|
---|
81 | }
|
---|
82 | public double scaleFactor() {
|
---|
83 | return 1.0 / world.getIconWidth();
|
---|
84 | }
|
---|
85 |
|
---|
86 | };
|
---|
87 |
|
---|
88 | MapScaler scaler = new MapScaler(this, projection);
|
---|
89 | add(scaler);
|
---|
90 | scaler.setLocation(10,10);
|
---|
91 |
|
---|
92 | setMinimumSize(new Dimension(350, 350/2));
|
---|
93 | }
|
---|
94 |
|
---|
95 | public void addGui(final DownloadDialog gui) {
|
---|
96 | JPanel temp = new JPanel();
|
---|
97 | temp.setLayout(new BorderLayout());
|
---|
98 | temp.add(this, BorderLayout.CENTER);
|
---|
99 | temp.add(new JLabel(tr("You can use the mouse or Ctrl+Arrow keys/./, to zoom and pan.")), BorderLayout.SOUTH);
|
---|
100 | gui.tabpane.add(temp, "Map");
|
---|
101 | new MapMover(this, temp);
|
---|
102 | SelectionEnded selListener = new SelectionEnded(){
|
---|
103 | public void selectionEnded(Rectangle r, boolean alt, boolean shift, boolean ctrl) {
|
---|
104 | markerMin = getEastNorth(r.x, r.y+r.height);
|
---|
105 | markerMax = getEastNorth(r.x+r.width, r.y);
|
---|
106 | LatLon min = getProjection().eastNorth2latlon(markerMin);
|
---|
107 | LatLon max = getProjection().eastNorth2latlon(markerMax);
|
---|
108 | gui.minlat = min.lat();
|
---|
109 | gui.minlon = min.lon();
|
---|
110 | gui.maxlat = max.lat();
|
---|
111 | gui.maxlon = max.lon();
|
---|
112 | gui.boundingBoxChanged(WorldChooser.this);
|
---|
113 | repaint();
|
---|
114 | }
|
---|
115 | public void addPropertyChangeListener(PropertyChangeListener listener) {}
|
---|
116 | public void removePropertyChangeListener(PropertyChangeListener listener) {}
|
---|
117 | };
|
---|
118 | SelectionManager sm = new SelectionManager(selListener, false, this);
|
---|
119 | sm.register(this);
|
---|
120 | }
|
---|
121 |
|
---|
122 | public void boundingBoxChanged(DownloadDialog gui) {
|
---|
123 | markerMin = getProjection().latlon2eastNorth(new LatLon(gui.minlat, gui.minlon));
|
---|
124 | markerMax = getProjection().latlon2eastNorth(new LatLon(gui.maxlat, gui.maxlon));
|
---|
125 | repaint();
|
---|
126 | }
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Set the scale as well as the preferred size.
|
---|
130 | */
|
---|
131 | @Override public void setPreferredSize(Dimension preferredSize) {
|
---|
132 | super.setPreferredSize(preferredSize);
|
---|
133 | scale = world.getIconWidth()/preferredSize.getWidth();
|
---|
134 | scaleMax = scale;
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Draw the current selected region.
|
---|
139 | */
|
---|
140 | @Override public void paint(Graphics g) {
|
---|
141 | EastNorth tl = getEastNorth(0,0);
|
---|
142 | EastNorth br = getEastNorth(getWidth(),getHeight());
|
---|
143 | g.drawImage(world.getImage(),0,0,getWidth(),getHeight(),(int)tl.east(),(int)tl.north(),(int)br.east(),(int)br.north(), null);
|
---|
144 |
|
---|
145 | // draw marker rect
|
---|
146 | if (markerMin != null && markerMax != null) {
|
---|
147 | Point p1 = getPoint(markerMin);
|
---|
148 | Point p2 = getPoint(markerMax);
|
---|
149 | double x = Math.min(p1.x, p2.x);
|
---|
150 | double y = Math.min(p1.y, p2.y);
|
---|
151 | double w = Math.max(p1.x, p2.x) - x;
|
---|
152 | double h = Math.max(p1.y, p2.y) - y;
|
---|
153 | if (w < 1)
|
---|
154 | w = 1;
|
---|
155 | if (h < 1)
|
---|
156 | h = 1;
|
---|
157 | g.setColor(Color.YELLOW);
|
---|
158 | g.drawRect((int)x, (int)y, (int)w, (int)h);
|
---|
159 | }
|
---|
160 | super.paint(g);
|
---|
161 | }
|
---|
162 |
|
---|
163 | @Override public void zoomTo(EastNorth newCenter, double scale) {
|
---|
164 | if (getWidth() != 0 && scale > scaleMax) {
|
---|
165 | scale = scaleMax;
|
---|
166 | newCenter = center;
|
---|
167 | }
|
---|
168 | super.zoomTo(newCenter, scale);
|
---|
169 | }
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * Always use our image projection mode.
|
---|
173 | */
|
---|
174 | @Override protected Projection getProjection() {
|
---|
175 | return projection;
|
---|
176 | }
|
---|
177 | }
|
---|