1 | package org.openstreetmap.josm.gui;
|
---|
2 |
|
---|
3 | import java.awt.Color;
|
---|
4 | import java.awt.Dimension;
|
---|
5 | import java.awt.Graphics;
|
---|
6 | import java.awt.Point;
|
---|
7 | import java.awt.Rectangle;
|
---|
8 | import java.awt.event.KeyAdapter;
|
---|
9 | import java.awt.event.KeyEvent;
|
---|
10 | import java.awt.event.KeyListener;
|
---|
11 | import java.beans.PropertyChangeListener;
|
---|
12 | import java.net.URL;
|
---|
13 |
|
---|
14 | import javax.swing.ImageIcon;
|
---|
15 | import javax.swing.JTextField;
|
---|
16 | import javax.swing.SwingUtilities;
|
---|
17 | import javax.swing.event.ListSelectionEvent;
|
---|
18 | import javax.swing.event.ListSelectionListener;
|
---|
19 |
|
---|
20 | import org.openstreetmap.josm.Main;
|
---|
21 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
22 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
23 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
24 | import org.openstreetmap.josm.gui.BookmarkList.Bookmark;
|
---|
25 | import org.openstreetmap.josm.gui.SelectionManager.SelectionEnded;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * A component that let the user select a lat/lon bounding box from zooming
|
---|
29 | * into the world as a picture and selecting a region.
|
---|
30 | *
|
---|
31 | * The component has to be of the aspect ration 2:1 to look good.
|
---|
32 | *
|
---|
33 | * @author imi
|
---|
34 | */
|
---|
35 | public class WorldChooser extends NavigatableComponent {
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * The world as picture.
|
---|
39 | */
|
---|
40 | private ImageIcon world;
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Maximum scale level
|
---|
44 | */
|
---|
45 | private double scaleMax;
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Mark this rectangle (lat/lon values) when painting.
|
---|
49 | */
|
---|
50 | private EastNorth markerMin, markerMax;
|
---|
51 |
|
---|
52 | private Projection projection;
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Create the chooser component.
|
---|
56 | */
|
---|
57 | public WorldChooser() {
|
---|
58 | URL path = Main.class.getResource("/images/world.jpg");
|
---|
59 | world = new ImageIcon(path);
|
---|
60 | center = new EastNorth(world.getIconWidth()/2, world.getIconHeight()/2);
|
---|
61 | setPreferredSize(new Dimension(200, 100));
|
---|
62 | new MapMover(this);
|
---|
63 | projection = new Projection() {
|
---|
64 | public EastNorth latlon2eastNorth(LatLon p) {
|
---|
65 | return new EastNorth(
|
---|
66 | (p.lon()+180) / 360 * world.getIconWidth(),
|
---|
67 | (p.lat()+90) / 180 * world.getIconHeight());
|
---|
68 | }
|
---|
69 | public LatLon eastNorth2latlon(EastNorth p) {
|
---|
70 | return new LatLon(
|
---|
71 | p.east()*360/world.getIconWidth() - 180,
|
---|
72 | p.north()*180/world.getIconHeight() - 90);
|
---|
73 | }
|
---|
74 | @Override
|
---|
75 | public String toString() {
|
---|
76 | return "WorldChooser";
|
---|
77 | }
|
---|
78 | };
|
---|
79 | }
|
---|
80 |
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Set the scale as well as the preferred size.
|
---|
84 | */
|
---|
85 | @Override
|
---|
86 | public void setPreferredSize(Dimension preferredSize) {
|
---|
87 | super.setPreferredSize(preferredSize);
|
---|
88 | scale = world.getIconWidth()/preferredSize.getWidth();
|
---|
89 | scaleMax = scale;
|
---|
90 | }
|
---|
91 |
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Draw the current selected region.
|
---|
95 | */
|
---|
96 | @Override
|
---|
97 | public void paint(Graphics g) {
|
---|
98 | EastNorth tl = getEastNorth(0,0);
|
---|
99 | EastNorth br = getEastNorth(getWidth(),getHeight());
|
---|
100 | g.drawImage(world.getImage(),0,0,getWidth(),getHeight(),(int)tl.east(),(int)tl.north(),(int)br.east(),(int)br.north(), null);
|
---|
101 |
|
---|
102 | // draw marker rect
|
---|
103 | if (markerMin != null && markerMax != null) {
|
---|
104 | Point p1 = getPoint(markerMin);
|
---|
105 | Point p2 = getPoint(markerMax);
|
---|
106 | double x = Math.min(p1.x, p2.x);
|
---|
107 | double y = Math.min(p1.y, p2.y);
|
---|
108 | double w = Math.max(p1.x, p2.x) - x;
|
---|
109 | double h = Math.max(p1.y, p2.y) - y;
|
---|
110 | if (w < 1)
|
---|
111 | w = 1;
|
---|
112 | if (h < 1)
|
---|
113 | h = 1;
|
---|
114 | g.setColor(Color.YELLOW);
|
---|
115 | g.drawRect((int)x, (int)y, (int)w, (int)h);
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 |
|
---|
120 | @Override
|
---|
121 | public void zoomTo(EastNorth newCenter, double scale) {
|
---|
122 | if (getWidth() != 0 && scale > scaleMax)
|
---|
123 | scale = scaleMax;
|
---|
124 | super.zoomTo(newCenter, scale);
|
---|
125 | }
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * Show the selection bookmark in the world.
|
---|
129 | */
|
---|
130 | public void addListMarker(final BookmarkList list) {
|
---|
131 | list.addListSelectionListener(new ListSelectionListener(){
|
---|
132 | public void valueChanged(ListSelectionEvent e) {
|
---|
133 | Bookmark b = (Bookmark)list.getSelectedValue();
|
---|
134 | if (b != null) {
|
---|
135 | markerMin = getProjection().latlon2eastNorth(new LatLon(b.latlon[0],b.latlon[1]));
|
---|
136 | markerMax = getProjection().latlon2eastNorth(new LatLon(b.latlon[2],b.latlon[3]));
|
---|
137 | } else {
|
---|
138 | markerMin = null;
|
---|
139 | markerMax = null;
|
---|
140 | }
|
---|
141 | repaint();
|
---|
142 | }
|
---|
143 | });
|
---|
144 | }
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * Update edit fields and react upon changes.
|
---|
148 | * @param field Must have exactly 4 entries (min lat to max lon)
|
---|
149 | */
|
---|
150 | public void addInputFields(final JTextField[] field, JTextField osmUrl, final KeyListener osmUrlRefresher) {
|
---|
151 | // listener that invokes updateMarkerFromTextField after all
|
---|
152 | // messages are dispatched and so text fields are updated.
|
---|
153 | KeyListener listener = new KeyAdapter(){
|
---|
154 | @Override
|
---|
155 | public void keyTyped(KeyEvent e) {
|
---|
156 | SwingUtilities.invokeLater(new Runnable(){
|
---|
157 | public void run() {
|
---|
158 | updateMarkerFromTextFields(field);
|
---|
159 | }
|
---|
160 | });
|
---|
161 | }
|
---|
162 | };
|
---|
163 |
|
---|
164 | for (JTextField f : field)
|
---|
165 | f.addKeyListener(listener);
|
---|
166 | osmUrl.addKeyListener(listener);
|
---|
167 |
|
---|
168 | SelectionEnded selListener = new SelectionEnded(){
|
---|
169 | public void selectionEnded(Rectangle r, boolean alt, boolean shift, boolean ctrl) {
|
---|
170 | markerMin = getEastNorth(r.x, r.y+r.height);
|
---|
171 | markerMax = getEastNorth(r.x+r.width, r.y);
|
---|
172 | LatLon min = getProjection().eastNorth2latlon(markerMin);
|
---|
173 | LatLon max = getProjection().eastNorth2latlon(markerMax);
|
---|
174 | field[0].setText(""+min.lat());
|
---|
175 | field[1].setText(""+min.lon());
|
---|
176 | field[2].setText(""+max.lat());
|
---|
177 | field[3].setText(""+max.lon());
|
---|
178 | for (JTextField f : field)
|
---|
179 | f.setCaretPosition(0);
|
---|
180 | osmUrlRefresher.keyTyped(null);
|
---|
181 | repaint();
|
---|
182 | }
|
---|
183 | public void addPropertyChangeListener(PropertyChangeListener listener) {}
|
---|
184 | public void removePropertyChangeListener(PropertyChangeListener listener) {}
|
---|
185 | };
|
---|
186 | SelectionManager sm = new SelectionManager(selListener, false, this);
|
---|
187 | sm.register(this);
|
---|
188 | updateMarkerFromTextFields(field);
|
---|
189 | }
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Update the marker field from the values of the given textfields
|
---|
193 | */
|
---|
194 | private void updateMarkerFromTextFields(JTextField[] field) {
|
---|
195 | // try to read all values
|
---|
196 | double v[] = new double[field.length];
|
---|
197 | for (int i = 0; i < field.length; ++i) {
|
---|
198 | try {
|
---|
199 | v[i] = Double.parseDouble(field[i].getText());
|
---|
200 | } catch (NumberFormatException nfe) {
|
---|
201 | return;
|
---|
202 | }
|
---|
203 | }
|
---|
204 | markerMin = new EastNorth(v[0], v[1]);
|
---|
205 | markerMax = new EastNorth(v[2], v[3]);
|
---|
206 | repaint();
|
---|
207 | }
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * Always use our image projection mode.
|
---|
211 | */
|
---|
212 | @Override
|
---|
213 | protected Projection getProjection() {
|
---|
214 | return projection;
|
---|
215 | }
|
---|
216 | }
|
---|