source: josm/src/org/openstreetmap/josm/gui/WorldChooser.java@ 155

Last change on this file since 155 was 155, checked in by imi, 18 years ago
  • added online help system
File size: 6.3 KB
Line 
1package org.openstreetmap.josm.gui;
2
3import java.awt.Color;
4import java.awt.Dimension;
5import java.awt.Graphics;
6import java.awt.Point;
7import java.awt.Rectangle;
8import java.awt.event.KeyAdapter;
9import java.awt.event.KeyEvent;
10import java.awt.event.KeyListener;
11import java.beans.PropertyChangeListener;
12import java.net.URL;
13
14import javax.swing.ImageIcon;
15import javax.swing.JTextField;
16import javax.swing.SwingUtilities;
17import javax.swing.event.ListSelectionEvent;
18import javax.swing.event.ListSelectionListener;
19
20import org.openstreetmap.josm.Main;
21import org.openstreetmap.josm.data.coor.LatLon;
22import org.openstreetmap.josm.data.coor.EastNorth;
23import org.openstreetmap.josm.data.projection.Projection;
24import org.openstreetmap.josm.gui.BookmarkList.Bookmark;
25import 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 */
35public 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.north()*180/world.getIconHeight() - 90,
72 p.east()*360/world.getIconWidth() - 180);
73 }
74 @Override public String toString() {
75 return "WorldChooser";
76 }
77 public String getCacheDirectoryName() {
78 throw new UnsupportedOperationException();
79 }
80 public double scaleFactor() {
81 return 1;
82 }
83 };
84 setMinimumSize(new Dimension(350, 350/2));
85 }
86
87
88 /**
89 * Set the scale as well as the preferred size.
90 */
91 @Override public void setPreferredSize(Dimension preferredSize) {
92 super.setPreferredSize(preferredSize);
93 scale = world.getIconWidth()/preferredSize.getWidth();
94 scaleMax = scale;
95 }
96
97
98 /**
99 * Draw the current selected region.
100 */
101 @Override public void paint(Graphics g) {
102 EastNorth tl = getEastNorth(0,0);
103 EastNorth br = getEastNorth(getWidth(),getHeight());
104 g.drawImage(world.getImage(),0,0,getWidth(),getHeight(),(int)tl.east(),(int)tl.north(),(int)br.east(),(int)br.north(), null);
105
106 // draw marker rect
107 if (markerMin != null && markerMax != null) {
108 Point p1 = getPoint(markerMin);
109 Point p2 = getPoint(markerMax);
110 double x = Math.min(p1.x, p2.x);
111 double y = Math.min(p1.y, p2.y);
112 double w = Math.max(p1.x, p2.x) - x;
113 double h = Math.max(p1.y, p2.y) - y;
114 if (w < 1)
115 w = 1;
116 if (h < 1)
117 h = 1;
118 g.setColor(Color.YELLOW);
119 g.drawRect((int)x, (int)y, (int)w, (int)h);
120 }
121 }
122
123
124 @Override public void zoomTo(EastNorth newCenter, double scale) {
125 if (getWidth() != 0 && scale > scaleMax) {
126 scale = scaleMax;
127 newCenter = center;
128 }
129 super.zoomTo(newCenter, scale);
130 }
131
132 /**
133 * Show the selection bookmark in the world.
134 */
135 public void addListMarker(final BookmarkList list) {
136 list.addListSelectionListener(new ListSelectionListener(){
137 public void valueChanged(ListSelectionEvent e) {
138 Bookmark b = (Bookmark)list.getSelectedValue();
139 if (b != null) {
140 markerMin = getProjection().latlon2eastNorth(new LatLon(b.latlon[0],b.latlon[1]));
141 markerMax = getProjection().latlon2eastNorth(new LatLon(b.latlon[2],b.latlon[3]));
142 } else {
143 markerMin = null;
144 markerMax = null;
145 }
146 repaint();
147 }
148 });
149 }
150
151 /**
152 * Update edit fields and react upon changes.
153 * @param field Must have exactly 4 entries (min lat to max lon)
154 */
155 public void addInputFields(final JTextField[] field, JTextField osmUrl, final KeyListener osmUrlRefresher) {
156 // listener that invokes updateMarkerFromTextField after all
157 // messages are dispatched and so text fields are updated.
158 KeyListener listener = new KeyAdapter(){
159 @Override public void keyTyped(KeyEvent e) {
160 SwingUtilities.invokeLater(new Runnable(){
161 public void run() {
162 updateMarkerFromTextFields(field);
163 }
164 });
165 }
166 };
167
168 for (JTextField f : field)
169 f.addKeyListener(listener);
170 osmUrl.addKeyListener(listener);
171
172 SelectionEnded selListener = new SelectionEnded(){
173 public void selectionEnded(Rectangle r, boolean alt, boolean shift, boolean ctrl) {
174 markerMin = getEastNorth(r.x, r.y+r.height);
175 markerMax = getEastNorth(r.x+r.width, r.y);
176 LatLon min = getProjection().eastNorth2latlon(markerMin);
177 LatLon max = getProjection().eastNorth2latlon(markerMax);
178 field[0].setText(""+min.lat());
179 field[1].setText(""+min.lon());
180 field[2].setText(""+max.lat());
181 field[3].setText(""+max.lon());
182 for (JTextField f : field)
183 f.setCaretPosition(0);
184 osmUrlRefresher.keyTyped(null);
185 repaint();
186 }
187 public void addPropertyChangeListener(PropertyChangeListener listener) {}
188 public void removePropertyChangeListener(PropertyChangeListener listener) {}
189 };
190 SelectionManager sm = new SelectionManager(selListener, false, this);
191 sm.register(this);
192 updateMarkerFromTextFields(field);
193 }
194
195 /**
196 * Update the marker field from the values of the given textfields
197 */
198 private void updateMarkerFromTextFields(JTextField[] field) {
199 // try to read all values
200 double v[] = new double[field.length];
201 for (int i = 0; i < field.length; ++i) {
202 try {
203 v[i] = Double.parseDouble(field[i].getText());
204 } catch (NumberFormatException nfe) {
205 return;
206 }
207 }
208 markerMin = getProjection().latlon2eastNorth(new LatLon(v[0], v[1]));
209 markerMax = getProjection().latlon2eastNorth(new LatLon(v[2], v[3]));
210 repaint();
211 }
212
213 /**
214 * Always use our image projection mode.
215 */
216 @Override protected Projection getProjection() {
217 return projection;
218 }
219}
Note: See TracBrowser for help on using the repository browser.