source: josm/src/org/openstreetmap/josm/actions/OpenOsmServerAction.java@ 21

Last change on this file since 21 was 21, checked in by imi, 19 years ago
  • started command implementation
  • cleaned up Layer
  • gpsbabel style for importing qpegps tracks
File size: 6.5 KB
Line 
1package org.openstreetmap.josm.actions;
2
3import java.awt.GridBagLayout;
4import java.awt.GridLayout;
5import java.awt.event.ActionEvent;
6import java.awt.event.ActionListener;
7import java.awt.event.KeyEvent;
8
9import javax.swing.AbstractAction;
10import javax.swing.DefaultListModel;
11import javax.swing.JButton;
12import javax.swing.JCheckBox;
13import javax.swing.JLabel;
14import javax.swing.JOptionPane;
15import javax.swing.JPanel;
16import javax.swing.JScrollPane;
17import javax.swing.JTextField;
18import javax.swing.event.ListSelectionEvent;
19import javax.swing.event.ListSelectionListener;
20
21import org.openstreetmap.josm.Main;
22import org.openstreetmap.josm.command.DataSet;
23import org.openstreetmap.josm.data.GeoPoint;
24import org.openstreetmap.josm.gui.BookmarkList;
25import org.openstreetmap.josm.gui.GBC;
26import org.openstreetmap.josm.gui.ImageProvider;
27import org.openstreetmap.josm.gui.MapFrame;
28import org.openstreetmap.josm.gui.MapView;
29import org.openstreetmap.josm.gui.BookmarkList.Bookmark;
30import org.openstreetmap.josm.gui.layer.Layer;
31import org.openstreetmap.josm.gui.layer.LayerFactory;
32import org.openstreetmap.josm.io.OsmReader;
33import org.openstreetmap.josm.io.DataReader.ConnectionException;
34import org.openstreetmap.josm.io.DataReader.ParseException;
35
36/**
37 * Action that opens a connection to the osm server.
38 *
39 * An dialog is displayed asking the user to specify a rectangle to grab.
40 * The url and account settings from the preferences are used.
41 *
42 * @author imi
43 */
44public class OpenOsmServerAction extends AbstractAction {
45
46 private JTextField[] latlon = new JTextField[]{
47 new JTextField(9),
48 new JTextField(9),
49 new JTextField(9),
50 new JTextField(9)};
51 private JCheckBox rawGps = new JCheckBox("Open as raw gps data", false);
52
53 public OpenOsmServerAction() {
54 super("Connect to OSM", ImageProvider.get("connectosm"));
55 putValue(MNEMONIC_KEY, KeyEvent.VK_C);
56 putValue(SHORT_DESCRIPTION, "Open a connection to the OSM server.");
57 }
58
59 public void actionPerformed(ActionEvent e) {
60 JPanel dlg = new JPanel(new GridBagLayout());
61 dlg.add(new JLabel("Bounding box"), GBC.eol());
62
63 dlg.add(new JLabel("min lat"), GBC.std().insets(10,0,5,0));
64 dlg.add(latlon[0], GBC.std());
65 dlg.add(new JLabel("max lat"), GBC.std().insets(10,0,5,0));
66 dlg.add(latlon[1], GBC.eol());
67 dlg.add(new JLabel("min lon"), GBC.std().insets(10,0,5,0));
68 dlg.add(latlon[2], GBC.std());
69 dlg.add(new JLabel("max lon"), GBC.std().insets(10,0,5,0));
70 dlg.add(latlon[3], GBC.eop());
71
72 if (Main.main.getMapFrame() != null) {
73 MapView mv = Main.main.getMapFrame().mapView;
74 int w = mv.getWidth();
75 int h = mv.getHeight();
76 GeoPoint bottomLeft = mv.getPoint(0, h, true);
77 GeoPoint topRight = mv.getPoint(w, 0, true);
78 latlon[0].setText(""+bottomLeft.lat);
79 latlon[1].setText(""+bottomLeft.lon);
80 latlon[2].setText(""+topRight.lat);
81 latlon[3].setText(""+topRight.lon);
82 for (JTextField f : latlon)
83 f.setCaretPosition(0);
84 rawGps.setSelected(!mv.getActiveLayer().isEditable());
85 }
86
87 dlg.add(rawGps, GBC.eop());
88
89 // load bookmarks
90 dlg.add(new JLabel("Bookmarks"), GBC.eol());
91 final BookmarkList bookmarks = new BookmarkList();
92 bookmarks.getSelectionModel().addListSelectionListener(new ListSelectionListener(){
93 public void valueChanged(ListSelectionEvent e) {
94 Bookmark b = (Bookmark)bookmarks.getSelectedValue();
95 for (int i = 0; i < 4; ++i) {
96 latlon[i].setText(b == null ? "" : ""+b.latlon[i]);
97 latlon[i].setCaretPosition(0);
98 }
99 rawGps.setSelected(b == null ? false : b.rawgps);
100 }
101 });
102 dlg.add(new JScrollPane(bookmarks), GBC.eol().fill());
103
104 JPanel buttons = new JPanel(new GridLayout(1,2));
105 JButton add = new JButton("Add");
106 add.addActionListener(new ActionListener(){
107 public void actionPerformed(ActionEvent e) {
108 Bookmark b = readBookmark();
109 if (b == null) {
110 JOptionPane.showMessageDialog(Main.main, "Please enter the desired coordinates first.");
111 return;
112 }
113 b.name = JOptionPane.showInputDialog(Main.main, "Please enter a name for the location.");
114 if (!b.name.equals("")) {
115 ((DefaultListModel)bookmarks.getModel()).addElement(b);
116 bookmarks.save();
117 }
118 }
119 });
120 buttons.add(add);
121 JButton remove = new JButton("Remove");
122 remove.addActionListener(new ActionListener(){
123 public void actionPerformed(ActionEvent e) {
124 Object sel = bookmarks.getSelectedValue();
125 if (sel == null) {
126 JOptionPane.showMessageDialog(Main.main, "Select a bookmark first.");
127 return;
128 }
129 ((DefaultListModel)bookmarks.getModel()).removeElement(sel);
130 bookmarks.save();
131 }
132 });
133 buttons.add(remove);
134 dlg.add(buttons, GBC.eop().fill(GBC.HORIZONTAL));
135
136 int r = JOptionPane.showConfirmDialog(Main.main, dlg, "Choose an area",
137 JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
138 if (r != JOptionPane.OK_OPTION)
139 return;
140
141 Bookmark b = readBookmark();
142 if (b == null) {
143 JOptionPane.showMessageDialog(Main.main, "Please enter the desired coordinates or click on a bookmark.");
144 return;
145 }
146 OsmReader osmReader = new OsmReader(Main.pref.osmDataServer,
147 rawGps.isSelected(), b.latlon[0], b.latlon[1], b.latlon[2], b.latlon[3]);
148 try {
149 DataSet dataSet = osmReader.parse();
150 if (dataSet == null)
151 return; // user cancelled download
152 if (dataSet.nodes.isEmpty())
153 JOptionPane.showMessageDialog(Main.main, "No data imported.");
154
155 String name = latlon[0].getText()+" "+latlon[1].getText()+" x "+
156 latlon[2].getText()+" "+latlon[3].getText();
157
158 Layer layer = LayerFactory.create(dataSet, name, rawGps.isSelected());
159
160 if (Main.main.getMapFrame() == null)
161 Main.main.setMapFrame(name, new MapFrame(layer));
162 else
163 Main.main.getMapFrame().mapView.addLayer(layer);
164 } catch (ParseException x) {
165 x.printStackTrace();
166 JOptionPane.showMessageDialog(Main.main, x.getMessage());
167 } catch (ConnectionException x) {
168 x.printStackTrace();
169 JOptionPane.showMessageDialog(Main.main, x.getMessage());
170 }
171 }
172
173 /**
174 * Read a bookmark from the current set edit fields. If one of the fields is
175 * empty or contain illegal chars, <code>null</code> is returned.
176 * The name of the bookmark is <code>null</code>.
177 * @return A bookmark containing information from the edit fields and rawgps
178 * checkbox.
179 */
180 private Bookmark readBookmark() {
181 try {
182 Bookmark b = new Bookmark();
183 for (int i = 0; i < 4; ++i) {
184 if (latlon[i].getText().equals(""))
185 return null;
186 b.latlon[i] = Double.parseDouble(latlon[i].getText());
187 }
188 b.rawgps = rawGps.isSelected();
189 return b;
190 } catch (NumberFormatException x) {
191 return null;
192 }
193 }
194}
Note: See TracBrowser for help on using the repository browser.