source: josm/src/org/openstreetmap/josm/actions/DownloadAction.java@ 43

Last change on this file since 43 was 43, checked in by imi, 18 years ago
  • removed UTM (too complex)
  • added please wait dialog for down-/uploading
  • added created_by=JOSM to every new element
File size: 7.8 KB
Line 
1package org.openstreetmap.josm.actions;
2
3import java.awt.Dimension;
4import java.awt.GridBagLayout;
5import java.awt.GridLayout;
6import java.awt.event.ActionEvent;
7import java.awt.event.ActionListener;
8import java.awt.event.InputEvent;
9import java.awt.event.KeyEvent;
10import java.io.FileNotFoundException;
11import java.io.IOException;
12
13import javax.swing.DefaultListModel;
14import javax.swing.JButton;
15import javax.swing.JCheckBox;
16import javax.swing.JDialog;
17import javax.swing.JLabel;
18import javax.swing.JOptionPane;
19import javax.swing.JPanel;
20import javax.swing.JScrollPane;
21import javax.swing.JTextField;
22import javax.swing.KeyStroke;
23import javax.swing.event.ListSelectionEvent;
24import javax.swing.event.ListSelectionListener;
25
26import org.jdom.JDOMException;
27import org.openstreetmap.josm.Main;
28import org.openstreetmap.josm.data.GeoPoint;
29import org.openstreetmap.josm.data.osm.DataSet;
30import org.openstreetmap.josm.gui.BookmarkList;
31import org.openstreetmap.josm.gui.GBC;
32import org.openstreetmap.josm.gui.MapFrame;
33import org.openstreetmap.josm.gui.MapView;
34import org.openstreetmap.josm.gui.WorldChooser;
35import org.openstreetmap.josm.gui.BookmarkList.Bookmark;
36import org.openstreetmap.josm.gui.layer.Layer;
37import org.openstreetmap.josm.gui.layer.OsmDataLayer;
38import org.openstreetmap.josm.gui.layer.RawGpsDataLayer;
39import org.openstreetmap.josm.io.OsmServerReader;
40
41/**
42 * Action that opens a connection to the osm server and download map data.
43 *
44 * An dialog is displayed asking the user to specify a rectangle to grab.
45 * The url and account settings from the preferences are used.
46 *
47 * @author imi
48 */
49public class DownloadAction extends JosmAction {
50
51 JTextField[] latlon = new JTextField[]{
52 new JTextField(9),
53 new JTextField(9),
54 new JTextField(9),
55 new JTextField(9)};
56 JCheckBox rawGps = new JCheckBox("Open as raw gps data", false);
57
58 public DownloadAction() {
59 super("Download from OSM", "download", "Download map data from the OSM server.", KeyEvent.VK_D,
60 KeyStroke.getAWTKeyStroke(KeyEvent.VK_D, InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK));
61 }
62
63 public void actionPerformed(ActionEvent e) {
64 JPanel dlg = new JPanel(new GridBagLayout());
65
66 WorldChooser wc = new WorldChooser();
67 dlg.add(wc, GBC.eop());
68
69 dlg.add(new JLabel("Bounding box"), GBC.eol());
70 dlg.add(new JLabel("min lat"), GBC.std().insets(10,0,5,0));
71 dlg.add(latlon[0], GBC.std());
72 dlg.add(new JLabel("min lon"), GBC.std().insets(10,0,5,0));
73 dlg.add(latlon[1], GBC.eol());
74 dlg.add(new JLabel("max lat"), GBC.std().insets(10,0,5,0));
75 dlg.add(latlon[2], GBC.std());
76 dlg.add(new JLabel("max lon"), GBC.std().insets(10,0,5,0));
77 dlg.add(latlon[3], GBC.eol());
78
79 if (Main.main.getMapFrame() != null) {
80 MapView mv = Main.main.getMapFrame().mapView;
81 int w = mv.getWidth();
82 int h = mv.getHeight();
83 GeoPoint bottomLeft = mv.getPoint(0, h, true);
84 GeoPoint topRight = mv.getPoint(w, 0, true);
85 if (bottomLeft.isOutSideWorld())
86 bottomLeft = new GeoPoint(-89.999, -179.999); // do not use the Projection constants, since this look better.
87 if (topRight.isOutSideWorld())
88 topRight = new GeoPoint(89.999, 179.999);
89 latlon[0].setText(""+bottomLeft.lat);
90 latlon[1].setText(""+bottomLeft.lon);
91 latlon[2].setText(""+topRight.lat);
92 latlon[3].setText(""+topRight.lon);
93 for (JTextField f : latlon)
94 f.setCaretPosition(0);
95 rawGps.setSelected(mv.getActiveLayer() instanceof RawGpsDataLayer);
96 }
97
98 dlg.add(rawGps, GBC.eop());
99
100 // load bookmarks
101 dlg.add(new JLabel("Bookmarks"), GBC.eol());
102 final BookmarkList bookmarks = new BookmarkList();
103 bookmarks.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
104 public void valueChanged(ListSelectionEvent e) {
105 Bookmark b = (Bookmark)bookmarks.getSelectedValue();
106 for (int i = 0; i < 4; ++i) {
107 latlon[i].setText(b == null ? "" : ""+b.latlon[i]);
108 latlon[i].setCaretPosition(0);
109 }
110 rawGps.setSelected(b == null ? false : b.rawgps);
111 }
112 });
113 wc.addListMarker(bookmarks);
114 wc.addLatLonInputField(latlon);
115 dlg.add(new JScrollPane(bookmarks), GBC.eol().fill());
116
117 JPanel buttons = new JPanel(new GridLayout(1,2));
118 JButton add = new JButton("Add");
119 add.addActionListener(new ActionListener(){
120 public void actionPerformed(ActionEvent e) {
121 Bookmark b = readBookmark();
122 if (b == null) {
123 JOptionPane.showMessageDialog(Main.main, "Please enter the desired coordinates first.");
124 return;
125 }
126 b.name = JOptionPane.showInputDialog(Main.main, "Please enter a name for the location.");
127 if (!b.name.equals("")) {
128 ((DefaultListModel)bookmarks.getModel()).addElement(b);
129 bookmarks.save();
130 }
131 }
132 });
133 buttons.add(add);
134 JButton remove = new JButton("Remove");
135 remove.addActionListener(new ActionListener(){
136 public void actionPerformed(ActionEvent e) {
137 Object sel = bookmarks.getSelectedValue();
138 if (sel == null) {
139 JOptionPane.showMessageDialog(Main.main, "Select a bookmark first.");
140 return;
141 }
142 ((DefaultListModel)bookmarks.getModel()).removeElement(sel);
143 bookmarks.save();
144 }
145 });
146 buttons.add(remove);
147 dlg.add(buttons, GBC.eop().fill(GBC.HORIZONTAL));
148
149 Dimension d = dlg.getPreferredSize();
150 wc.setPreferredSize(new Dimension(d.width, d.width/2));
151
152 int r = JOptionPane.showConfirmDialog(Main.main, dlg, "Choose an area",
153 JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
154 if (r != JOptionPane.OK_OPTION)
155 return;
156
157 Bookmark b = readBookmark();
158 if (b == null) {
159 JOptionPane.showMessageDialog(Main.main, "Please enter the desired coordinates or click on a bookmark.");
160 return;
161 }
162
163 final OsmServerReader osmReader = new OsmServerReader(b.latlon[0], b.latlon[1], b.latlon[2], b.latlon[3]);
164
165 final JDialog pleaseWaitDlg = createPleaseWaitDialog("Downloading data");
166
167 new Thread(){
168 @Override
169 public void run() {
170 try {
171 String name = latlon[0].getText() + " "
172 + latlon[1].getText() + " x " + latlon[2].getText()
173 + " " + latlon[3].getText();
174
175 Layer layer = null;
176 if (rawGps.isSelected()) {
177 layer = new RawGpsDataLayer(osmReader.parseRawGps(),
178 name);
179 } else {
180 DataSet dataSet = osmReader.parseOsm();
181 if (dataSet == null)
182 return; // user cancelled download
183 if (dataSet.nodes.isEmpty())
184 JOptionPane.showMessageDialog(Main.main,
185 "No data imported.");
186
187 layer = new OsmDataLayer(dataSet, name);
188 }
189
190 if (Main.main.getMapFrame() == null)
191 Main.main.setMapFrame(name, new MapFrame(layer));
192 else
193 Main.main.getMapFrame().mapView.addLayer(layer);
194 pleaseWaitDlg.setVisible(false);
195 } catch (JDOMException x) {
196 pleaseWaitDlg.setVisible(false);
197 x.printStackTrace();
198 JOptionPane.showMessageDialog(Main.main, x.getMessage());
199 } catch (FileNotFoundException x) {
200 pleaseWaitDlg.setVisible(false);
201 x.printStackTrace();
202 JOptionPane.showMessageDialog(Main.main,
203 "URL nicht gefunden: " + x.getMessage());
204 } catch (IOException x) {
205 pleaseWaitDlg.setVisible(false);
206 x.printStackTrace();
207 JOptionPane.showMessageDialog(Main.main, x.getMessage());
208 } finally {
209 pleaseWaitDlg.setVisible(false);
210 }
211 }
212 }.start();
213
214 pleaseWaitDlg.setVisible(true);
215 }
216
217 /**
218 * Read a bookmark from the current set edit fields. If one of the fields is
219 * empty or contain illegal chars, <code>null</code> is returned.
220 * The name of the bookmark is <code>null</code>.
221 * @return A bookmark containing information from the edit fields and rawgps
222 * checkbox.
223 */
224 Bookmark readBookmark() {
225 try {
226 Bookmark b = new Bookmark();
227 for (int i = 0; i < 4; ++i) {
228 if (latlon[i].getText().equals(""))
229 return null;
230 b.latlon[i] = Double.parseDouble(latlon[i].getText());
231 }
232 b.rawgps = rawGps.isSelected();
233 return b;
234 } catch (NumberFormatException x) {
235 return null;
236 }
237 }
238}
Note: See TracBrowser for help on using the repository browser.