1 | package org.openstreetmap.josm.actions;
|
---|
2 |
|
---|
3 | import java.awt.GridBagLayout;
|
---|
4 | import java.awt.GridLayout;
|
---|
5 | import java.awt.event.ActionEvent;
|
---|
6 | import java.awt.event.ActionListener;
|
---|
7 | import java.awt.event.InputEvent;
|
---|
8 | import java.awt.event.KeyEvent;
|
---|
9 | import java.io.IOException;
|
---|
10 |
|
---|
11 | import javax.swing.DefaultListModel;
|
---|
12 | import javax.swing.JButton;
|
---|
13 | import javax.swing.JCheckBox;
|
---|
14 | import javax.swing.JLabel;
|
---|
15 | import javax.swing.JOptionPane;
|
---|
16 | import javax.swing.JPanel;
|
---|
17 | import javax.swing.JScrollPane;
|
---|
18 | import javax.swing.JTextField;
|
---|
19 | import javax.swing.KeyStroke;
|
---|
20 | import javax.swing.event.ListSelectionEvent;
|
---|
21 | import javax.swing.event.ListSelectionListener;
|
---|
22 |
|
---|
23 | import org.jdom.JDOMException;
|
---|
24 | import org.openstreetmap.josm.Main;
|
---|
25 | import org.openstreetmap.josm.data.GeoPoint;
|
---|
26 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
27 | import org.openstreetmap.josm.gui.BookmarkList;
|
---|
28 | import org.openstreetmap.josm.gui.GBC;
|
---|
29 | import org.openstreetmap.josm.gui.MapFrame;
|
---|
30 | import org.openstreetmap.josm.gui.MapView;
|
---|
31 | import org.openstreetmap.josm.gui.BookmarkList.Bookmark;
|
---|
32 | import org.openstreetmap.josm.gui.layer.Layer;
|
---|
33 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
34 | import org.openstreetmap.josm.gui.layer.RawGpsDataLayer;
|
---|
35 | import org.openstreetmap.josm.io.OsmServerReader;
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Action that opens a connection to the osm server.
|
---|
39 | *
|
---|
40 | * An dialog is displayed asking the user to specify a rectangle to grab.
|
---|
41 | * The url and account settings from the preferences are used.
|
---|
42 | *
|
---|
43 | * @author imi
|
---|
44 | */
|
---|
45 | public class OpenOsmServerAction extends JosmAction {
|
---|
46 |
|
---|
47 | JTextField[] latlon = new JTextField[]{
|
---|
48 | new JTextField(9),
|
---|
49 | new JTextField(9),
|
---|
50 | new JTextField(9),
|
---|
51 | new JTextField(9)};
|
---|
52 | JCheckBox rawGps = new JCheckBox("Open as raw gps data", false);
|
---|
53 |
|
---|
54 | public OpenOsmServerAction() {
|
---|
55 | super("Connect to OSM", "connectosm", "Open a connection to the OSM server.", KeyEvent.VK_C,
|
---|
56 | KeyStroke.getAWTKeyStroke(KeyEvent.VK_C, InputEvent.CTRL_DOWN_MASK | InputEvent.SHIFT_DOWN_MASK));
|
---|
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() instanceof RawGpsDataLayer);
|
---|
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 | OsmServerReader osmReader = new OsmServerReader(Main.pref.osmDataServer,
|
---|
147 | b.latlon[0], b.latlon[1], b.latlon[2], b.latlon[3]);
|
---|
148 | try {
|
---|
149 | String name = latlon[0].getText()+" "+latlon[1].getText()+" x "+
|
---|
150 | latlon[2].getText()+" "+latlon[3].getText();
|
---|
151 |
|
---|
152 | Layer layer;
|
---|
153 | if (rawGps.isSelected()) {
|
---|
154 | layer = new RawGpsDataLayer(osmReader.parseRawGps(), name);
|
---|
155 | } else {
|
---|
156 | DataSet dataSet = osmReader.parseOsm();
|
---|
157 | if (dataSet == null)
|
---|
158 | return; // user cancelled download
|
---|
159 | if (dataSet.nodes.isEmpty())
|
---|
160 | JOptionPane.showMessageDialog(Main.main, "No data imported.");
|
---|
161 |
|
---|
162 | layer = new OsmDataLayer(dataSet, name);
|
---|
163 | }
|
---|
164 |
|
---|
165 | if (Main.main.getMapFrame() == null)
|
---|
166 | Main.main.setMapFrame(name, new MapFrame(layer));
|
---|
167 | else
|
---|
168 | Main.main.getMapFrame().mapView.addLayer(layer);
|
---|
169 | } catch (JDOMException x) {
|
---|
170 | x.printStackTrace();
|
---|
171 | JOptionPane.showMessageDialog(Main.main, x.getMessage());
|
---|
172 | } catch (IOException x) {
|
---|
173 | x.printStackTrace();
|
---|
174 | JOptionPane.showMessageDialog(Main.main, x.getMessage());
|
---|
175 | }
|
---|
176 | }
|
---|
177 |
|
---|
178 | /**
|
---|
179 | * Read a bookmark from the current set edit fields. If one of the fields is
|
---|
180 | * empty or contain illegal chars, <code>null</code> is returned.
|
---|
181 | * The name of the bookmark is <code>null</code>.
|
---|
182 | * @return A bookmark containing information from the edit fields and rawgps
|
---|
183 | * checkbox.
|
---|
184 | */
|
---|
185 | Bookmark readBookmark() {
|
---|
186 | try {
|
---|
187 | Bookmark b = new Bookmark();
|
---|
188 | for (int i = 0; i < 4; ++i) {
|
---|
189 | if (latlon[i].getText().equals(""))
|
---|
190 | return null;
|
---|
191 | b.latlon[i] = Double.parseDouble(latlon[i].getText());
|
---|
192 | }
|
---|
193 | b.rawgps = rawGps.isSelected();
|
---|
194 | return b;
|
---|
195 | } catch (NumberFormatException x) {
|
---|
196 | return null;
|
---|
197 | }
|
---|
198 | }
|
---|
199 | }
|
---|