- Timestamp:
- 2005-10-13T08:27:12+02:00 (19 years ago)
- Location:
- src/org/openstreetmap/josm
- Files:
-
- 1 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
src/org/openstreetmap/josm/actions/OpenOsmServerAction.java
r18 r20 2 2 3 3 import java.awt.GridBagLayout; 4 import java.awt.GridLayout; 4 5 import java.awt.event.ActionEvent; 6 import java.awt.event.ActionListener; 5 7 import java.awt.event.KeyEvent; 6 8 7 9 import javax.swing.AbstractAction; 10 import javax.swing.DefaultListModel; 11 import javax.swing.JButton; 12 import javax.swing.JCheckBox; 8 13 import javax.swing.JLabel; 9 14 import javax.swing.JOptionPane; 10 15 import javax.swing.JPanel; 16 import javax.swing.JScrollPane; 11 17 import javax.swing.JTextField; 18 import javax.swing.event.ListSelectionEvent; 19 import javax.swing.event.ListSelectionListener; 12 20 13 21 import org.openstreetmap.josm.data.GeoPoint; 14 22 import org.openstreetmap.josm.data.osm.DataSet; 23 import org.openstreetmap.josm.gui.BookmarkList; 15 24 import org.openstreetmap.josm.gui.GBC; 16 25 import org.openstreetmap.josm.gui.ImageProvider; … … 18 27 import org.openstreetmap.josm.gui.MapFrame; 19 28 import org.openstreetmap.josm.gui.MapView; 29 import org.openstreetmap.josm.gui.BookmarkList.Bookmark; 20 30 import org.openstreetmap.josm.gui.layer.Layer; 21 31 import org.openstreetmap.josm.gui.layer.LayerFactory; … … 34 44 public class OpenOsmServerAction extends AbstractAction { 35 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 36 53 public OpenOsmServerAction() { 37 54 super("Connect to OSM", ImageProvider.get("connectosm")); … … 44 61 dlg.add(new JLabel("Bounding box"), GBC.eol()); 45 62 46 JTextField minLat = new JTextField(9);47 JTextField minLon = new JTextField(9);48 JTextField maxLat = new JTextField(9);49 JTextField maxLon = new JTextField(9);50 51 63 dlg.add(new JLabel("min lat"), GBC.std().insets(10,0,5,0)); 52 dlg.add( minLat, GBC.std());64 dlg.add(latlon[0], GBC.std()); 53 65 dlg.add(new JLabel("max lat"), GBC.std().insets(10,0,5,0)); 54 dlg.add( maxLat, GBC.eol());66 dlg.add(latlon[1], GBC.eol()); 55 67 dlg.add(new JLabel("min lon"), GBC.std().insets(10,0,5,0)); 56 dlg.add( minLon, GBC.std());68 dlg.add(latlon[2], GBC.std()); 57 69 dlg.add(new JLabel("max lon"), GBC.std().insets(10,0,5,0)); 58 dlg.add( maxLon, GBC.eol());70 dlg.add(latlon[3], GBC.eop()); 59 71 60 72 if (Main.main.getMapFrame() != null) { … … 64 76 GeoPoint bottomLeft = mv.getPoint(0, h, true); 65 77 GeoPoint topRight = mv.getPoint(w, 0, true); 66 minLat.setText(""+bottomLeft.lat); 67 minLon.setText(""+bottomLeft.lon); 68 maxLat.setText(""+topRight.lat); 69 maxLon.setText(""+topRight.lon); 70 71 minLat.setCaretPosition(0); 72 minLon.setCaretPosition(0); 73 maxLat.setCaretPosition(0); 74 maxLon.setCaretPosition(0); 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()); 75 85 } 76 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 77 136 int r = JOptionPane.showConfirmDialog(Main.main, dlg, "Choose an area", 78 137 JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); … … 80 139 return; 81 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 } 82 146 OsmReader osmReader = new OsmReader(Main.pref.osmDataServer, 83 Main.pref.osmDataUsername, Main.pref.osmDataPassword, 84 Double.parseDouble(minLat.getText()), 85 Double.parseDouble(minLon.getText()), 86 Double.parseDouble(maxLat.getText()), 87 Double.parseDouble(maxLon.getText())); 147 rawGps.isSelected(), b.latlon[0], b.latlon[1], b.latlon[2], b.latlon[3]); 88 148 try { 89 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."); 90 154 91 String name = minLat.getText()+" "+minLon.getText()+" x "+92 maxLat.getText()+" "+maxLon.getText();155 String name = latlon[0].getText()+" "+latlon[1].getText()+" x "+ 156 latlon[2].getText()+" "+latlon[3].getText(); 93 157 94 Layer layer = LayerFactory.create(dataSet, name, false);158 Layer layer = LayerFactory.create(dataSet, name, rawGps.isSelected()); 95 159 96 160 if (Main.main.getMapFrame() == null) … … 106 170 } 107 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 } 108 194 } -
src/org/openstreetmap/josm/data/Preferences.java
r18 r20 6 6 import java.io.FileReader; 7 7 import java.io.FileWriter; 8 import java.io.IOException; 8 9 import java.util.Collection; 9 10 import java.util.LinkedList; … … 79 80 * Return the location of the preferences file 80 81 */ 81 public static String getPreferences File() {82 return System.getProperty("user.home")+"/.josm -preferences";82 public static String getPreferencesDir() { 83 return System.getProperty("user.home")+"/.josm/"; 83 84 } 84 85 … … 91 92 super(message, cause); 92 93 } 94 public PreferencesException(String message) { 95 super(message); 96 } 93 97 } 94 98 /** … … 97 101 */ 98 102 public void load() throws PreferencesException { 99 File file = new File( System.getProperty("user.home")+"/.josm-preferences");103 File file = new File(getPreferencesDir()+"/preferences"); 100 104 Element root; 101 105 try { … … 159 163 160 164 try { 161 final FileWriter file = new FileWriter(getPreferencesFile()); 165 File prefDir = new File(getPreferencesDir()); 166 if (prefDir.exists() && !prefDir.isDirectory()) 167 throw new PreferencesException("Preferences directory "+getPreferencesDir()+" is not a directory."); 168 if (!prefDir.exists()) 169 prefDir.mkdirs(); 170 171 FileWriter file = new FileWriter(getPreferencesDir()+"/preferences"); 162 172 new XMLOutputter(Format.getPrettyFormat()).output(root, file); 163 173 file.close(); 164 } catch ( Exception e) {174 } catch (IOException e) { 165 175 throw new PreferencesException("Could not write preferences", e); 166 176 } -
src/org/openstreetmap/josm/gui/Main.java
r19 r20 126 126 } catch (PreferencesException e1) { 127 127 e1.printStackTrace(); 128 errMsg = "Preferences could not be loaded. Write default preference file to '"+Preferences.getPreferences File()+"'.";128 errMsg = "Preferences could not be loaded. Write default preference file to '"+Preferences.getPreferencesDir()+"preferences'."; 129 129 try { 130 130 pref.save(); -
src/org/openstreetmap/josm/gui/PreferenceDialog.java
r18 r20 189 189 190 190 191 // tooltips192 191 osmDataServer.setToolTipText("The base URL to the OSM server (REST API)"); 193 192 osmDataUsername.setToolTipText("Login name (email) to the OSM account."); … … 197 196 forceRawGpsLines.setToolTipText("Force drawing of lines if the imported data contain no line information."); 198 197 forceRawGpsLines.setSelected(Main.pref.isForceRawGpsLines()); 198 forceRawGpsLines.setEnabled(drawRawGpsLines.isSelected()); 199 199 mergeNodes.setToolTipText("When importing GPX data, all nodes with exact the same lat/lon are merged."); 200 200 mergeNodes.setSelected(Main.pref.mergeNodes); -
src/org/openstreetmap/josm/io/OsmReader.java
r18 r20 1 1 package org.openstreetmap.josm.io; 2 2 3 import java.awt.Font; 4 import java.awt.GridBagLayout; 3 5 import java.io.IOException; 4 6 import java.io.InputStreamReader; … … 9 11 import java.net.URL; 10 12 13 import javax.swing.JLabel; 14 import javax.swing.JOptionPane; 15 import javax.swing.JPanel; 16 import javax.swing.JPasswordField; 17 import javax.swing.JTextField; 18 11 19 import org.openstreetmap.josm.data.osm.DataSet; 20 import org.openstreetmap.josm.gui.GBC; 21 import org.openstreetmap.josm.gui.Main; 12 22 13 23 /** … … 22 32 */ 23 33 private String urlStr; 34 /** 35 * Whether importing the raw trackpoints or the regular osm map information 36 */ 37 private boolean rawGps; 38 /** 39 * Whether the user cancelled the password dialog 40 */ 41 private boolean cancelled = false; 42 /** 43 * Set to true, when the autenticator tried the password once. 44 */ 45 private boolean passwordtried = false; 24 46 25 47 /** 26 48 * Construct the reader and store the information for attaching 27 49 */ 28 public OsmReader(String server, final String username, final String password,50 public OsmReader(String server, boolean rawGps, 29 51 double lat1, double lon1, double lat2, double lon2) { 52 this.rawGps = rawGps; 30 53 urlStr = server.endsWith("/") ? server : server+"/"; 31 urlStr += "map?bbox="+lon1+","+lat1+","+lon2+","+lat2; 54 urlStr += rawGps?"trackpoints" : "map"; 55 urlStr += "?bbox="+lon1+","+lat1+","+lon2+","+lat2; 56 if (rawGps) 57 urlStr += "&page="; 32 58 33 59 HttpURLConnection.setFollowRedirects(true); … … 35 61 @Override 36 62 protected PasswordAuthentication getPasswordAuthentication() { 63 String username = Main.pref.osmDataUsername; 64 String password = Main.pref.osmDataPassword; 65 if (passwordtried || "".equals(username) || password == null || "".equals(password)) { 66 JPanel p = new JPanel(new GridBagLayout()); 67 p.add(new JLabel("Username"), GBC.std().insets(0,0,10,0)); 68 JTextField usernameField = new JTextField("".equals(username) ? "" : username, 20); 69 p.add(usernameField, GBC.eol()); 70 p.add(new JLabel("Password"), GBC.std().insets(0,0,10,0)); 71 JPasswordField passwordField = new JPasswordField(password == null ? "" : password, 20); 72 p.add(passwordField, GBC.eol()); 73 JLabel warning = new JLabel("Warning: The password is transferred unencrypted."); 74 warning.setFont(warning.getFont().deriveFont(Font.ITALIC)); 75 p.add(warning, GBC.eol()); 76 int choice = JOptionPane.showConfirmDialog(Main.main, p, "Enter Password", JOptionPane.OK_CANCEL_OPTION); 77 if (choice == JOptionPane.CANCEL_OPTION) { 78 cancelled = true; 79 return null; 80 } 81 username = usernameField.getText(); 82 password = String.valueOf(passwordField.getPassword()); 83 if ("".equals(username)) 84 return null; 85 } 86 passwordtried = true; 37 87 return new PasswordAuthentication(username, password.toCharArray()); 38 88 } … … 44 94 Reader in; 45 95 try { 96 if (rawGps) { 97 DataSet ds = new DataSet(); 98 for (int i = 0;;++i) { 99 URL url = new URL(urlStr+i); 100 HttpURLConnection con = (HttpURLConnection)url.openConnection(); 101 con.setConnectTimeout(20000); 102 if (con.getResponseCode() == 401 && cancelled) 103 return null; 104 in = new InputStreamReader(con.getInputStream()); 105 DataSet currentData = new GpxReader(in, true).parse(); 106 if (currentData.nodes.isEmpty()) 107 return ds; 108 ds.mergeFrom(currentData, true); 109 } 110 } 46 111 URL url = new URL(urlStr); 47 112 HttpURLConnection con = (HttpURLConnection)url.openConnection(); 48 con.setDoInput(true);49 113 con.setConnectTimeout(20000); 50 con.setRequestMethod("GET");51 con.connect();114 if (con.getResponseCode() == 401 && cancelled) 115 return null; 52 116 in = new InputStreamReader(con.getInputStream()); 117 return new GpxReader(in, false).parse(); 53 118 } catch (IOException e) { 54 119 throw new ConnectionException("Failed to open server connection\n"+e.getMessage(), e); 55 120 } 56 GpxReader reader = new GpxReader(in, false);57 return reader.parse();58 121 } 59 122 }
Note:
See TracChangeset
for help on using the changeset viewer.