- Timestamp:
- 2006-04-22T18:50:21+02:00 (19 years ago)
- Location:
- src/org/openstreetmap/josm
- Files:
-
- 2 added
- 12 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
src/org/openstreetmap/josm/Main.java
r90 r92 1 // 1 //Licence: GPL 2 2 package org.openstreetmap.josm; 3 3 … … 11 11 import java.io.IOException; 12 12 import java.util.Arrays; 13 import java.util.Iterator;14 13 import java.util.LinkedList; 15 14 import java.util.StringTokenizer; … … 39 38 import org.openstreetmap.josm.actions.UndoAction; 40 39 import org.openstreetmap.josm.actions.UploadAction; 40 import org.openstreetmap.josm.data.Bounds; 41 41 import org.openstreetmap.josm.data.Preferences; 42 42 import org.openstreetmap.josm.data.osm.DataSet; … … 45 45 import org.openstreetmap.josm.gui.MapFrame; 46 46 import org.openstreetmap.josm.gui.ShowModifiers; 47 import org.openstreetmap.josm.gui.dialogs.SelectionListDialog; 47 48 import org.openstreetmap.josm.gui.layer.Layer; 48 49 import org.openstreetmap.josm.gui.layer.OsmDataLayer; … … 68 69 */ 69 70 public static Executor worker = Executors.newSingleThreadExecutor(); 70 71 71 72 72 73 public static Projection proj; 73 74 … … 81 82 */ 82 83 public static DataSet ds = new DataSet(); 83 84 84 85 /** 85 86 * The main panel. … … 96 97 private OpenAction openAction; 97 98 private DownloadAction downloadAction; 98 99 99 //private Action wmsServerAction; 100 100 101 /** 101 102 * Construct an main frame, ready sized and operating. Does not … … 110 111 setSize(1000,740); // some strange default size 111 112 setVisible(true); 112 113 113 114 downloadAction = new DownloadAction(); 114 115 Action uploadAction = new UploadAction(); 115 116 //wmsServerAction = new WmsServerAction(); 116 117 openAction = new OpenAction(); 117 118 Action saveAction = new SaveAction(); 118 119 Action gpxExportAction = new GpxExportAction(null); … … 136 137 mainMenu.add(fileMenu); 137 138 138 139 139 140 JMenu layerMenu = new JMenu("Layer"); 140 141 layerMenu.setMnemonic('L'); 141 142 layerMenu.add(downloadAction); 142 143 layerMenu.add(uploadAction); 143 144 144 layerMenu.addSeparator(); 145 //layerMenu.add(new JCheckBoxMenuItem(wmsServerAction)); 145 146 mainMenu.add(layerMenu); 146 147 147 148 JMenu editMenu = new JMenu("Edit"); 148 149 editMenu.setMnemonic('E'); … … 152 153 editMenu.add(preferencesAction); 153 154 mainMenu.add(editMenu); 154 155 155 156 mainMenu.add(new JSeparator()); 156 157 JMenu helpMenu = new JMenu("Help"); … … 174 175 toolBar.addSeparator(); 175 176 toolBar.add(preferencesAction); 176 177 177 178 getContentPane().add(toolBar, BorderLayout.NORTH); 178 179 179 180 addWindowListener(new WindowAdapter(){ 180 181 @Override public void windowClosing(WindowEvent arg0) { … … 214 215 LinkedList<String> arguments = new LinkedList<String>(Arrays.asList(args)); 215 216 216 if (arguments.contains("--help") ) {217 if (arguments.contains("--help") || arguments.contains("-?") || arguments.contains("-h")) { 217 218 System.out.println("Java OpenStreetMap Editor"); 218 219 System.out.println(); 219 220 System.out.println("usage:"); 220 System.out.println("\tjava -jar josm.jar <option s> file file file...");221 System.out.println("\tjava -jar josm.jar <option> <option> <option>..."); 221 222 System.out.println(); 222 223 System.out.println("options:"); 223 System.out.println("\t--help Show this help"); 224 System.out.println("\t--geometry=widthxheight(+|-)x(+|-)y Standard unix geometry argument"); 225 System.out.println("\t--download=minlat,minlon,maxlat,maxlon Download the bounding box"); 226 System.out.println("\t--no-fullscreen Don't launch in fullscreen mode"); 227 System.out.println("\t--reset-preferences Reset the preferences to default"); 228 System.out.println("file(.osm|.xml|.gpx|.txt|.csv) Open the specific file"); 224 System.out.println("\t--help|-?|-h Show this help"); 225 System.out.println("\t--geometry=widthxheight(+|-)x(+|-)y Standard unix geometry argument"); 226 System.out.println("\t--download=minlat,minlon,maxlat,maxlon Download the bounding box"); 227 System.out.println("\t--downloadgps=minlat,minlon,maxlat,maxlon Download the bounding box"); 228 System.out.println("\t--selection=<searchstring> Select with the given search"); 229 System.out.println("\t--no-fullscreen Don't launch in fullscreen mode"); 230 System.out.println("\t--reset-preferences Reset the preferences to default"); 231 System.out.println("\tURL|filename(.osm|.xml|.gpx|.txt|.csv) Open file / Download url"); 232 System.out.println(); 233 System.out.println("examples:"); 234 System.out.println("\tjava -jar josm.jar track1.gpx track2.gpx london.osm"); 235 System.out.println("\tjava -jar josm.jar http://www.openstreetmap.org/index.html?lat=43.2&lon=11.1&zoom=13"); 236 System.out.println("\tjava -jar josm.jar london.osm --selection=http://www.ostertag.name/osm/OSM_errors_node-duplicate.xml"); 237 System.out.println("\tjava -jar josm.jar osm://43.2,11.1,43.4,11.4"); 238 System.out.println(); 239 System.out.println("Parameters are read in the order they are specified, so make sure you load"); 240 System.out.println("some data before --selection"); 241 System.out.println(); 242 System.out.println("Instead of --download=<bbox> you may specify osm://<bbox>"); 229 243 System.exit(0); 230 244 } … … 260 274 proj = new Epsg4326(); 261 275 } 262 276 263 277 try { 264 278 UIManager.setLookAndFeel(pref.get("laf")); … … 266 280 e.printStackTrace(); 267 281 } 268 282 269 283 new Main(); 270 284 main.setVisible(true); … … 279 293 } 280 294 281 for (Iterator<String> it = arguments.iterator(); it.hasNext();) {282 String s = it.next(); 283 if (s.startsWith("--download=")) {284 downloadFromParamString(false, s.substring(11));285 it.remove();295 boolean showModifiers = false; 296 297 for (String s : arguments) { 298 if (s.startsWith("--download=") || s.startsWith("osm:")) { 299 downloadFromParamString(false, s); 286 300 } else if (s.startsWith("--downloadgps=")) { 287 downloadFromParamString(true, s.substring(14)); 288 it.remove(); 301 downloadFromParamString(true, s); 289 302 } else if (s.startsWith("--geometry=")) { 290 303 Matcher m = Pattern.compile("(\\d+)x(\\d+)(([+-])(\\d+)([+-])(\\d+))?").matcher(s.substring(11)); … … 305 318 } else 306 319 System.out.println("Ignoring malformed geometry: "+s.substring(11)); 307 it.remove(); 320 } else if (s.equals("--show-modifiers")) { 321 showModifiers = true; 322 } else if (s.startsWith("--selection=")) { 323 SelectionListDialog.search(s.substring(12), SelectionListDialog.SearchMode.add); 324 } else if (s.startsWith("http:") || s.startsWith("ftp:") || s.startsWith("https:")) { 325 Bounds b = DownloadAction.osmurl2bounds(s); 326 if (b == null) 327 System.out.println("Ignoring malformed url: "+s); 328 else 329 Main.main.downloadAction.download(false, b.min.lat(), b.min.lon(), b.max.lat(), b.max.lon()); 330 } else { 331 main.openAction.openFile(new File(s)); 308 332 } 309 333 } 310 334 311 if ( arguments.remove("--show-modifiers")) {335 if (showModifiers) { 312 336 Point p = main.getLocationOnScreen(); 313 337 Dimension s = main.getSize(); … … 315 339 main.setVisible(true); 316 340 } 317 318 for (String s : arguments)319 main.openAction.openFile(new File(s));320 341 } 321 342 322 343 323 344 private static void downloadFromParamString(boolean rawGps, String s) { 345 s = s.replaceAll("^(osm:/?/?)|(--download(gps)?=)", ""); 346 System.out.println(s); 347 System.exit(1); 324 348 StringTokenizer st = new StringTokenizer(s, ","); 325 349 if (st.countTokens() != 4) { 326 JOptionPane.showMessageDialog(main, " Download option does not take "+st.countTokens()+" bounding parameter.");350 JOptionPane.showMessageDialog(main, "Malformed bounding box: "+s); 327 351 return; 328 352 } … … 355 379 } 356 380 357 381 358 382 /** 359 383 * Sets some icons to the ui. -
src/org/openstreetmap/josm/actions/DownloadAction.java
r86 r92 13 13 import java.util.Collection; 14 14 import java.util.HashMap; 15 import java.util.Map;16 15 17 16 import javax.swing.DefaultListModel; … … 36 35 import org.openstreetmap.josm.gui.MapFrame; 37 36 import org.openstreetmap.josm.gui.MapView; 37 import org.openstreetmap.josm.gui.PleaseWaitRunnable; 38 38 import org.openstreetmap.josm.gui.WorldChooser; 39 39 import org.openstreetmap.josm.gui.BookmarkList.Bookmark; … … 60 60 * Run in the worker thread. 61 61 */ 62 63 62 private final class DownloadOsmTask extends PleaseWaitRunnable { 63 private final OsmServerReader reader; 64 64 private DataSet dataSet; 65 65 66 private DownloadOsmTask(OsmServerReader reader) { 67 super("Downloading data"); 68 this.reader = reader; 69 } 70 71 @Override public void realRun() throws IOException, SAXException { 72 dataSet = reader.parseOsm(); 73 if (dataSet == null) 74 return; 75 if (dataSet.nodes.isEmpty()) 76 JOptionPane.showMessageDialog(Main.main, "No data imported."); 77 } 66 private DownloadOsmTask(OsmServerReader reader) { 67 super("Downloading data"); 68 this.reader = reader; 69 reader.setProgressInformation(currentAction, progress); 70 } 71 72 @Override public void realRun() throws IOException, SAXException { 73 dataSet = reader.parseOsm(); 74 if (dataSet == null) 75 return; 76 if (dataSet.nodes.isEmpty()) 77 JOptionPane.showMessageDialog(Main.main, "No data imported."); 78 } 78 79 79 80 @Override protected void finish() { … … 81 82 return; // user cancelled download or error occoured 82 83 Layer layer = new OsmDataLayer(dataSet, "Data Layer", false); 83 if (Main.main.getMapFrame() == null) 84 Main.main.setMapFrame(new MapFrame(layer)); 85 else 86 Main.main.getMapFrame().mapView.addLayer(layer); 87 } 88 89 } 90 91 92 private final class DownloadGpsTask extends PleaseWaitRunnable { 93 private final OsmServerReader reader; 84 if (Main.main.getMapFrame() == null) 85 Main.main.setMapFrame(new MapFrame(layer)); 86 else 87 Main.main.getMapFrame().mapView.addLayer(layer); 88 } 89 90 @Override protected void cancel() { 91 reader.cancel(); 92 } 93 } 94 95 96 private final class DownloadGpsTask extends PleaseWaitRunnable { 97 private final OsmServerReader reader; 94 98 private Collection<Collection<GpsPoint>> rawData; 95 99 96 97 98 99 100 101 102 103 100 private DownloadGpsTask(OsmServerReader reader) { 101 super("Downloading GPS data"); 102 this.reader = reader; 103 } 104 105 @Override public void realRun() throws IOException, JDOMException { 106 rawData = reader.parseRawGps(); 107 } 104 108 105 109 @Override protected void finish() { … … 108 112 String name = latlon[0].getText() + " " + latlon[1].getText() + " x " + latlon[2].getText() + " " + latlon[3].getText(); 109 113 Layer layer = new RawGpsDataLayer(rawData, name); 110 if (Main.main.getMapFrame() == null) 111 Main.main.setMapFrame(new MapFrame(layer)); 112 else 113 Main.main.getMapFrame().mapView.addLayer(layer); 114 } 115 116 } 117 118 119 /** 120 * minlat, minlon, maxlat, maxlon 121 */ 114 if (Main.main.getMapFrame() == null) 115 Main.main.setMapFrame(new MapFrame(layer)); 116 else 117 Main.main.getMapFrame().mapView.addLayer(layer); 118 } 119 120 @Override protected void cancel() { 121 } 122 } 123 124 125 /** 126 * minlat, minlon, maxlat, maxlon 127 */ 122 128 JTextField[] latlon = new JTextField[]{ 123 129 new JTextField(9), … … 136 142 137 143 public void actionPerformed(ActionEvent e) { 138 139 144 String osmDataServer = Main.pref.get("osm-server.url"); 140 145 //TODO: Remove this in later versions (temporary only) … … 178 183 } 179 184 dlg.add(rawGps, GBC.eop()); 180 185 181 186 // OSM url edit 182 187 dlg.add(new JLabel("URL from www.openstreetmap.org"), GBC.eol()); … … 221 226 SwingUtilities.invokeLater(new Runnable() { 222 227 public void run() { 223 Map<String, Double> map = readArgs(osmUrl.getText()); 224 try { 225 double size = 180.0 / Math.pow(2, map.get("zoom")); 226 Bounds b = new Bounds( 227 new LatLon(map.get("lat") - size/2, map.get("lon") - size), 228 new LatLon(map.get("lat") + size/2, map.get("lon") + size)); 228 Bounds b = osmurl2bounds(osmUrl.getText()); 229 if (b != null) 229 230 setEditBounds(b); 230 } catch (Exception x) { // NPE or IAE231 else 231 232 for (JTextField f : latlon) 232 233 f.setText(""); 233 }234 234 } 235 235 }); 236 236 } 237 237 }); 238 238 239 239 // Bookmarks 240 240 dlg.add(new JLabel("Bookmarks"), GBC.eol()); … … 289 289 wc.setPreferredSize(new Dimension(d.width, d.width/2)); 290 290 wc.addInputFields(latlon, osmUrl, osmUrlRefresher); 291 291 292 292 // Finally: the dialog 293 293 Bookmark b; … … 330 330 } 331 331 } 332 333 334 /** 335 * Extrakt URL arguments. 336 */ 337 private Map<String, Double> readArgs(String s) { 338 int i = s.indexOf('?'); 332 333 334 public static Bounds osmurl2bounds(String url) { 335 int i = url.indexOf('?'); 339 336 if (i == -1) 340 return n ew HashMap<String, Double>();341 String[] args = s.substring(i+1).split("&");337 return null; 338 String[] args = url.substring(i+1).split("&"); 342 339 HashMap<String, Double> map = new HashMap<String, Double>(); 343 340 for (String arg : args) { … … 350 347 } 351 348 } 352 return map; 353 } 354 349 try { 350 double size = 180.0 / Math.pow(2, map.get("zoom")); 351 return new Bounds( 352 new LatLon(map.get("lat") - size/2, map.get("lon") - size), 353 new LatLon(map.get("lat") + size/2, map.get("lon") + size)); 354 } catch (Exception x) { // NPE or IAE 355 return null; 356 } 357 } 358 355 359 /** 356 360 * Set the four edit fields to the given bounds coordinates. -
src/org/openstreetmap/josm/actions/JosmAction.java
r81 r92 1 1 package org.openstreetmap.josm.actions; 2 2 3 import java.awt.EventQueue;4 import java.io.FileNotFoundException;5 import java.io.IOException;6 3 7 4 import javax.swing.AbstractAction; 8 import javax.swing.BorderFactory;9 5 import javax.swing.JComponent; 10 import javax.swing.JDialog;11 import javax.swing.JLabel;12 import javax.swing.JOptionPane;13 6 import javax.swing.KeyStroke; 14 7 15 import org.jdom.JDOMException;16 8 import org.openstreetmap.josm.Main; 17 9 import org.openstreetmap.josm.tools.ImageProvider; 18 import org.xml.sax.SAXException;19 10 20 11 /** … … 24 15 abstract public class JosmAction extends AbstractAction { 25 16 26 /**27 * Instanced of this thread will display a "Please Wait" message in middle of JOSM28 * to indicate a progress beeing executed.29 *30 * @author Imi31 */32 protected abstract class PleaseWaitRunnable implements Runnable {33 public final JDialog pleaseWaitDlg;34 private String errorMessage;35 /**36 * Create the runnable object with a given message for the user.37 */38 public PleaseWaitRunnable(String msg) {39 pleaseWaitDlg = new JDialog(Main.main, true);40 pleaseWaitDlg.setUndecorated(true);41 JLabel l = new JLabel(msg+". Please Wait.");42 l.setBorder(BorderFactory.createCompoundBorder(43 BorderFactory.createEtchedBorder(),44 BorderFactory.createEmptyBorder(20,20,20,20)));45 pleaseWaitDlg.getContentPane().add(l);46 pleaseWaitDlg.pack();47 pleaseWaitDlg.setLocationRelativeTo(Main.main);48 pleaseWaitDlg.setResizable(false);49 }50 public final void run() {51 try {52 realRun();53 } catch (SAXException x) {54 x.printStackTrace();55 errorMessage = "Error while parsing: "+x.getMessage();56 } catch (JDOMException x) {57 x.printStackTrace();58 errorMessage = "Error while parsing: "+x.getMessage();59 } catch (FileNotFoundException x) {60 x.printStackTrace();61 errorMessage = "URL not found: " + x.getMessage();62 } catch (IOException x) {63 x.printStackTrace();64 errorMessage = x.getMessage();65 } finally {66 closeDialog();67 }68 }69 /**70 * Called in the worker thread to do the actual work. When any of the71 * exception is thrown, a message box will be displayed and closeDialog72 * is called. finish() is called in any case.73 */74 protected abstract void realRun() throws SAXException, JDOMException, IOException;75 /**76 * Finish up the data work. Is guaranteed to be called if realRun is called.77 * Finish is called in the gui thread just after the dialog disappeared.78 */79 protected void finish() {}80 /**81 * Close the dialog. Usually called from worker thread.82 */83 public void closeDialog() {84 EventQueue.invokeLater(new Runnable(){85 public void run() {86 finish();87 pleaseWaitDlg.setVisible(false);88 pleaseWaitDlg.dispose();89 if (errorMessage != null)90 JOptionPane.showMessageDialog(Main.main, errorMessage);91 }92 });93 }94 }95 96 17 /** 97 18 * Construct the action as menu action entry. -
src/org/openstreetmap/josm/actions/UploadAction.java
r90 r92 19 19 import org.openstreetmap.josm.data.osm.OsmPrimitive; 20 20 import org.openstreetmap.josm.gui.OsmPrimitivRenderer; 21 import org.openstreetmap.josm.gui.PleaseWaitRunnable; 21 22 import org.openstreetmap.josm.io.OsmServerWriter; 22 23 import org.openstreetmap.josm.tools.GBC; … … 38 39 39 40 public void actionPerformed(ActionEvent e) { 40 41 41 42 String osmDataServer = Main.pref.get("osm-server.url"); 42 43 //TODO: Remove this in later versions (temporary only) … … 54 55 Main.pref.put("osm-server.url", osmDataServer.substring(0, osmDataServer.length()-cutPos)); 55 56 } 56 57 57 58 if (!Main.main.getMapFrame().conflictDialog.conflicts.isEmpty()) { 58 59 JOptionPane.showMessageDialog(Main.main, "There are unresolved conflicts. You have to resolve these first."); … … 82 83 all.addAll(update); 83 84 all.addAll(delete); 84 85 85 86 PleaseWaitRunnable uploadTask = new PleaseWaitRunnable("Uploading data"){ 86 @Override protected void realRun() throws JDOMException { 87 server.uploadOsm(all); 88 } 89 @Override protected void finish() { 90 Main.main.getMapFrame().mapView.editLayer().cleanData(server.processed, !add.isEmpty()); 91 } 92 93 }; 87 @Override protected void realRun() throws JDOMException { 88 server.setProgressInformation(currentAction, progress); 89 server.uploadOsm(all); 90 } 91 @Override protected void finish() { 92 Main.main.getMapFrame().mapView.editLayer().cleanData(server.processed, !add.isEmpty()); 93 } 94 @Override protected void cancel() { 95 server.cancel(); 96 } 97 }; 94 98 Main.worker.execute(uploadTask); 95 99 uploadTask.pleaseWaitDlg.setVisible(true); -
src/org/openstreetmap/josm/data/osm/visitor/NameVisitor.java
r86 r92 17 17 * @author imi 18 18 */ 19 public class SelectionComponentVisitor implements Visitor {19 public class NameVisitor implements Visitor { 20 20 21 /** 22 * The name of the item class 23 */ 24 public String className; 21 25 /** 22 26 * The name of this item. … … 43 47 } 44 48 icon = ImageProvider.get("data", "segment"); 49 className = "segment"; 45 50 } 46 51 … … 54 59 name = (n.id==0?"":""+n.id)+" ("+n.coor.lat()+","+n.coor.lon()+")"; 55 60 icon = ImageProvider.get("data", "node"); 61 className = "node"; 56 62 } 57 63 … … 74 80 } 75 81 icon = ImageProvider.get("data", "way"); 82 className = "way"; 76 83 } 77 84 } -
src/org/openstreetmap/josm/gui/MapStatus.java
r86 r92 30 30 import org.openstreetmap.josm.data.coor.LatLon; 31 31 import org.openstreetmap.josm.data.osm.OsmPrimitive; 32 import org.openstreetmap.josm.data.osm.visitor. SelectionComponentVisitor;32 import org.openstreetmap.josm.data.osm.visitor.NameVisitor; 33 33 import org.openstreetmap.josm.tools.GBC; 34 34 … … 120 120 osmNearest = mv.getNearest(ms.mousePos, (ms.modifiers & MouseEvent.ALT_DOWN_MASK) != 0); 121 121 if (osmNearest != null) { 122 SelectionComponentVisitor visitor = new SelectionComponentVisitor();122 NameVisitor visitor = new NameVisitor(); 123 123 osmNearest.visit(visitor); 124 124 nameText.setText(visitor.name); … … 133 133 JPanel c = new JPanel(new GridBagLayout()); 134 134 for (final OsmPrimitive osm : osms) { 135 SelectionComponentVisitor visitor = new SelectionComponentVisitor();135 NameVisitor visitor = new NameVisitor(); 136 136 osm.visit(visitor); 137 137 final StringBuilder text = new StringBuilder(); -
src/org/openstreetmap/josm/gui/OsmPrimitivRenderer.java
r86 r92 8 8 9 9 import org.openstreetmap.josm.data.osm.OsmPrimitive; 10 import org.openstreetmap.josm.data.osm.visitor. SelectionComponentVisitor;10 import org.openstreetmap.josm.data.osm.visitor.NameVisitor; 11 11 12 12 /** … … 16 16 public class OsmPrimitivRenderer extends DefaultListCellRenderer { 17 17 18 private SelectionComponentVisitor visitor = new SelectionComponentVisitor();18 private NameVisitor visitor = new NameVisitor(); 19 19 20 20 @Override public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { -
src/org/openstreetmap/josm/gui/dialogs/PropertiesDialog.java
r86 r92 234 234 } 235 235 }); 236 propertyTable.addMouseListener(new DblClickWatch());236 //propertyTable.addMouseListener(new DblClickWatch()); 237 237 238 238 JScrollPane scrollPane = new JScrollPane(propertyTable); -
src/org/openstreetmap/josm/gui/dialogs/SelectionListDialog.java
r91 r92 11 11 import java.awt.event.MouseEvent; 12 12 import java.io.IOException; 13 import java.io.InputStreamReader;14 13 import java.io.Reader; 14 import java.net.HttpURLConnection; 15 15 import java.net.MalformedURLException; 16 16 import java.net.URL; … … 31 31 import javax.swing.ListSelectionModel; 32 32 33 import org.jdom.JDOMException; 33 34 import org.openstreetmap.josm.Main; 34 35 import org.openstreetmap.josm.data.SelectionChangedListener; … … 36 37 import org.openstreetmap.josm.gui.MapFrame; 37 38 import org.openstreetmap.josm.gui.OsmPrimitivRenderer; 39 import org.openstreetmap.josm.gui.PleaseWaitRunnable; 38 40 import org.openstreetmap.josm.io.OsmIdReader; 41 import org.openstreetmap.josm.io.ProgressReader; 39 42 import org.openstreetmap.josm.tools.GBC; 40 43 import org.openstreetmap.josm.tools.ImageProvider; … … 51 54 public class SelectionListDialog extends ToggleDialog implements SelectionChangedListener { 52 55 56 public static enum SearchMode {replace, add, remove} 57 58 private static class SelectionWebsiteLoader extends PleaseWaitRunnable { 59 public final URL url; 60 public Collection<OsmPrimitive> sel; 61 private final SearchMode mode; 62 private OsmIdReader idReader = new OsmIdReader(); 63 public SelectionWebsiteLoader(String urlStr, SearchMode mode) { 64 super("Load Selection"); 65 this.mode = mode; 66 URL u = null; 67 try {u = new URL(urlStr);} catch (MalformedURLException e) {} 68 this.url = u; 69 } 70 @Override protected void realRun() throws SAXException, JDOMException, IOException { 71 currentAction.setText("Contact "+url.getHost()+"..."); 72 sel = mode != SearchMode.remove ? new LinkedList<OsmPrimitive>() : Main.ds.allNonDeletedPrimitives(); 73 try { 74 HttpURLConnection con = (HttpURLConnection)url.openConnection(); 75 Reader in = new ProgressReader(con, progress); 76 currentAction.setText("Downloading..."); 77 Map<Long, String> ids = idReader.parseIds(in); 78 for (OsmPrimitive osm : Main.ds.allNonDeletedPrimitives()) { 79 if (ids.containsKey(osm.id) && osm.getClass().getName().toLowerCase().endsWith(ids.get(osm.id))) { 80 if (mode == SearchMode.remove) 81 sel.remove(osm); 82 else 83 sel.add(osm); 84 } 85 } 86 } catch (IOException e) { 87 e.printStackTrace(); 88 JOptionPane.showMessageDialog(Main.main, "Could not read from url: '"+url+"'"); 89 } catch (SAXException e) { 90 e.printStackTrace(); 91 JOptionPane.showMessageDialog(Main.main, "Parsing error in url: '"+url+"'"); 92 } 93 } 94 @Override protected void cancel() { 95 sel = null; 96 idReader.cancel(); 97 } 98 @Override protected void finish() { 99 if (sel != null) 100 Main.ds.setSelected(sel); 101 } 102 } 103 53 104 /** 54 105 * The selection's list data. … … 128 179 return; 129 180 lastSearch = input.getText(); 130 Collection<OsmPrimitive> sel = null; 131 if (lastSearch.startsWith("http://")) 132 sel = selectFromWebsite(lastSearch, replace.isSelected(), add.isSelected(), remove.isSelected()); 133 if (sel == null) { 134 sel = Main.ds.getSelected(); 135 SearchCompiler.Match matcher = SearchCompiler.compile(lastSearch); 136 for (OsmPrimitive osm : Main.ds.allNonDeletedPrimitives()) { 137 if (replace.isSelected()) { 138 if (matcher.match(osm)) 139 sel.add(osm); 140 else 141 sel.remove(osm); 142 } else if (add.isSelected() && !osm.selected && matcher.match(osm)) 143 sel.add(osm); 144 else if (remove.isSelected() && osm.selected && matcher.match(osm)) 145 sel.remove(osm); 146 } 147 } 148 Main.ds.setSelected(sel); 181 SearchMode mode = replace.isSelected() ? SearchMode.replace : (add.isSelected() ? SearchMode.add : SearchMode.remove); 182 search(lastSearch, mode); 149 183 } 150 184 }); … … 154 188 selectionChanged(Main.ds.getSelected()); 155 189 } 156 157 private Collection<OsmPrimitive> selectFromWebsite(String url, boolean replace, boolean add, boolean remove) {158 Collection<OsmPrimitive> sel = replace ? new LinkedList<OsmPrimitive>() : Main.ds.allNonDeletedPrimitives();159 try {160 Reader in = new InputStreamReader(new URL(url).openStream());161 Map<Long, String> ids = OsmIdReader.parseIds(in);162 for (OsmPrimitive osm : Main.ds.allNonDeletedPrimitives()) {163 if (ids.containsKey(osm.id) && osm.getClass().getName().toLowerCase().endsWith(ids.get(osm.id))) {164 if (remove)165 sel.remove(osm);166 else167 sel.add(osm);168 }169 }170 } catch (MalformedURLException e) {171 return null;172 } catch (IOException e) {173 e.printStackTrace();174 JOptionPane.showMessageDialog(Main.main, "Could not read from url: '"+url+"'");175 } catch (SAXException e) {176 e.printStackTrace();177 JOptionPane.showMessageDialog(Main.main, "Parsing error in url: '"+url+"'");178 }179 return sel;180 }181 190 182 191 @Override public void setVisible(boolean b) { … … 216 225 Main.ds.setSelected(sel); 217 226 } 227 228 public static void search(String search, SearchMode mode) { 229 if (search.startsWith("http://")) { 230 SelectionWebsiteLoader loader = new SelectionWebsiteLoader(search, mode); 231 if (loader.url != null) { 232 Main.worker.execute(loader); 233 loader.pleaseWaitDlg.setVisible(true); 234 return; 235 } 236 } 237 Collection<OsmPrimitive> sel = Main.ds.getSelected(); 238 SearchCompiler.Match matcher = SearchCompiler.compile(search); 239 for (OsmPrimitive osm : Main.ds.allNonDeletedPrimitives()) { 240 if (mode == SearchMode.replace) { 241 if (matcher.match(osm)) 242 sel.add(osm); 243 else 244 sel.remove(osm); 245 } else if (mode == SearchMode.add && !osm.selected && matcher.match(osm)) 246 sel.add(osm); 247 else if (mode == SearchMode.remove && osm.selected && matcher.match(osm)) 248 sel.remove(osm); 249 } 250 Main.ds.setSelected(sel); 251 } 218 252 } -
src/org/openstreetmap/josm/io/OsmConnection.java
r86 r92 7 7 import java.net.PasswordAuthentication; 8 8 9 import javax.swing.BoundedRangeModel; 9 10 import javax.swing.JLabel; 10 11 import javax.swing.JOptionPane; … … 24 25 public class OsmConnection { 25 26 26 /** 27 * The authentication class handling the login requests. 28 */ 29 private static class OsmAuth extends Authenticator { 30 /** 31 * Set to true, when the autenticator tried the password once. 32 */ 33 boolean passwordtried = false; 34 /** 35 * Whether the user cancelled the password dialog 36 */ 37 boolean cancelled = false; 38 39 @Override protected PasswordAuthentication getPasswordAuthentication() { 40 String username = Main.pref.get("osm-server.username"); 41 String password = Main.pref.get("osm-server.password"); 42 if (passwordtried || username.equals("") || password.equals("")) { 43 JPanel p = new JPanel(new GridBagLayout()); 44 p.add(new JLabel("Username"), GBC.std().insets(0,0,10,0)); 45 JTextField usernameField = new JTextField(username, 20); 46 p.add(usernameField, GBC.eol()); 47 p.add(new JLabel("Password"), GBC.std().insets(0,0,10,0)); 48 JPasswordField passwordField = new JPasswordField(password, 20); 49 p.add(passwordField, GBC.eol()); 50 JLabel warning = new JLabel("Warning: The password is transferred unencrypted."); 51 warning.setFont(warning.getFont().deriveFont(Font.ITALIC)); 52 p.add(warning, GBC.eol()); 53 int choice = JOptionPane.showConfirmDialog(Main.main, p, "Enter Password", JOptionPane.OK_CANCEL_OPTION); 54 if (choice == JOptionPane.CANCEL_OPTION) { 55 cancelled = true; 56 return null; 57 } 58 username = usernameField.getText(); 59 password = String.valueOf(passwordField.getPassword()); 60 if (username.equals("")) 61 return null; 62 } 63 passwordtried = true; 64 return new PasswordAuthentication(username, password.toCharArray()); 65 } 66 } 67 68 /** 69 * The authenticator. 70 */ 27 protected boolean cancel = false; 28 protected HttpURLConnection activeConnection; 29 protected JLabel currentAction; 30 protected BoundedRangeModel progress; 31 71 32 private static OsmAuth authentication; 72 73 33 /** 74 34 * Initialize the http defaults and the authenticator. … … 80 40 81 41 /** 42 * The authentication class handling the login requests. 43 */ 44 private static class OsmAuth extends Authenticator { 45 /** 46 * Set to true, when the autenticator tried the password once. 47 */ 48 boolean passwordtried = false; 49 /** 50 * Whether the user cancelled the password dialog 51 */ 52 boolean authCancelled = false; 53 54 @Override protected PasswordAuthentication getPasswordAuthentication() { 55 String username = Main.pref.get("osm-server.username"); 56 String password = Main.pref.get("osm-server.password"); 57 if (passwordtried || username.equals("") || password.equals("")) { 58 JPanel p = new JPanel(new GridBagLayout()); 59 p.add(new JLabel("Username"), GBC.std().insets(0,0,10,0)); 60 JTextField usernameField = new JTextField(username, 20); 61 p.add(usernameField, GBC.eol()); 62 p.add(new JLabel("Password"), GBC.std().insets(0,0,10,0)); 63 JPasswordField passwordField = new JPasswordField(password, 20); 64 p.add(passwordField, GBC.eol()); 65 JLabel warning = new JLabel("Warning: The password is transferred unencrypted."); 66 warning.setFont(warning.getFont().deriveFont(Font.ITALIC)); 67 p.add(warning, GBC.eol()); 68 int choice = JOptionPane.showConfirmDialog(Main.main, p, "Enter Password", JOptionPane.OK_CANCEL_OPTION); 69 if (choice == JOptionPane.CANCEL_OPTION) { 70 authCancelled = true; 71 return null; 72 } 73 username = usernameField.getText(); 74 password = String.valueOf(passwordField.getPassword()); 75 if (username.equals("")) 76 return null; 77 } 78 passwordtried = true; 79 return new PasswordAuthentication(username, password.toCharArray()); 80 } 81 } 82 83 /** 82 84 * Must be called before each connection attemp to initialize the authentication. 83 85 */ 84 86 protected final void initAuthentication() { 85 authentication. cancelled = false;87 authentication.authCancelled = false; 86 88 authentication.passwordtried = false; 87 89 } … … 90 92 * @return Whether the connection was cancelled. 91 93 */ 92 protected final boolean is Cancelled() {93 return authentication. cancelled;94 protected final boolean isAuthCancelled() { 95 return authentication.authCancelled; 94 96 } 97 98 public void setProgressInformation(JLabel currentAction, BoundedRangeModel progress) { 99 this.currentAction = currentAction; 100 this.progress = progress; 101 } 102 103 public void cancel() { 104 currentAction.setText("Aborting..."); 105 cancel = true; 106 if (activeConnection != null) { 107 activeConnection.setConnectTimeout(1); 108 activeConnection.setReadTimeout(1); 109 activeConnection.disconnect(); 110 } 111 } 95 112 } -
src/org/openstreetmap/josm/io/OsmIdReader.java
r91 r92 18 18 public class OsmIdReader extends MinML2 { 19 19 20 private boolean cancel; 20 21 Map<Long, String> entries = new HashMap<Long, String>(); 22 private Reader in; 21 23 22 24 @Override public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { … … 31 33 } 32 34 33 public static Map<Long, String> parseIds(Reader in) throws IOException, SAXException { 34 OsmIdReader r = new OsmIdReader(); 35 r.parse(in); 36 return r.entries; 35 public Map<Long, String> parseIds(Reader in) throws IOException, SAXException { 36 this.in = in; 37 try { 38 parse(in); 39 } catch (SAXException e) { 40 if (!cancel) 41 throw e; 42 } 43 return entries; 44 } 45 46 public void cancel() { 47 cancel = true; 48 if (in != null) 49 try {in.close();} catch (IOException e) {} 37 50 } 38 51 } -
src/org/openstreetmap/josm/io/OsmServerReader.java
r78 r92 2 2 3 3 import java.io.IOException; 4 import java.io.InputStreamReader;5 4 import java.io.Reader; 6 5 import java.net.HttpURLConnection; … … 68 67 break; 69 68 r.close(); 69 activeConnection = null; 70 70 } 71 71 … … 83 83 if (r == null) 84 84 return null; 85 currentAction.setText("Downloading OSM data..."); 85 86 DataSet data = OsmReader.parseDataSet(r); 86 87 r.close(); 88 activeConnection = null; 87 89 return data; 88 90 } … … 99 101 initAuthentication(); 100 102 URL url = new URL(urlStr); 101 HttpURLConnection con = (HttpURLConnection)url.openConnection();102 con.setConnectTimeout(20000);103 if ( con.getResponseCode() == 401 && isCancelled())103 activeConnection = (HttpURLConnection)url.openConnection(); 104 activeConnection.setConnectTimeout(15000); 105 if (isAuthCancelled() && activeConnection.getResponseCode() == 401) 104 106 return null; 105 return new InputStreamReader(con.getInputStream());107 return new ProgressReader(activeConnection, progress); 106 108 } 107 109 } -
src/org/openstreetmap/josm/io/OsmServerWriter.java
r86 r92 16 16 import org.jdom.JDOMException; 17 17 import org.openstreetmap.josm.Main; 18 import org.openstreetmap.josm.data.osm.Segment;19 18 import org.openstreetmap.josm.data.osm.Node; 20 19 import org.openstreetmap.josm.data.osm.OsmPrimitive; 20 import org.openstreetmap.josm.data.osm.Segment; 21 21 import org.openstreetmap.josm.data.osm.Way; 22 import org.openstreetmap.josm.data.osm.visitor.NameVisitor; 22 23 import org.openstreetmap.josm.data.osm.visitor.Visitor; 23 24 … … 44 45 */ 45 46 public Collection<OsmPrimitive> processed; 46 47 48 /** 49 * Whether the operation should be aborted as soon as possible. 50 */ 51 private boolean cancel = false; 52 47 53 /** 48 54 * Send the dataset to the server. Ask the user first and does nothing if he … … 53 59 initAuthentication(); 54 60 61 progress.setMaximum(list.size()); 62 progress.setValue(0); 63 64 NameVisitor v = new NameVisitor(); 55 65 try { 56 for (OsmPrimitive osm : list) 66 for (OsmPrimitive osm : list) { 67 if (cancel) 68 return; 69 osm.visit(v); 70 currentAction.setText("Upload "+v.className+" "+osm.id+"..."); 57 71 osm.visit(this); 72 progress.setValue(progress.getValue()+1); 73 } 58 74 } catch (RuntimeException e) { 59 75 throw new JDOMException("An error occoured: ", e); … … 138 154 URL url = new URL(Main.pref.get("osm-server.url") + "/0.3/" + urlSuffix + "/" + osm.id); 139 155 System.out.println("upload to: "+url); 140 HttpURLConnection con = (HttpURLConnection) url.openConnection();141 con.setConnectTimeout(20000);142 con.setRequestMethod(requestMethod);156 activeConnection = (HttpURLConnection) url.openConnection(); 157 activeConnection.setConnectTimeout(15000); 158 activeConnection.setRequestMethod(requestMethod); 143 159 if (addBody) 144 con.setDoOutput(true);145 con.connect();160 activeConnection.setDoOutput(true); 161 activeConnection.connect(); 146 162 147 163 if (addBody) { 148 Writer out = new OutputStreamWriter( con.getOutputStream());164 Writer out = new OutputStreamWriter(activeConnection.getOutputStream()); 149 165 OsmWriter.outputSingle(out, osm, true); 150 166 out.close(); 151 167 } 152 168 153 int retCode = con.getResponseCode();169 int retCode = activeConnection.getResponseCode(); 154 170 if (retCode == 200 && osm.id == 0) 155 osm.id = readId( con.getInputStream());171 osm.id = readId(activeConnection.getInputStream()); 156 172 System.out.println("got return: "+retCode+" with id "+osm.id); 157 String retMsg = con.getResponseMessage();158 con.disconnect();173 String retMsg = activeConnection.getResponseMessage(); 174 activeConnection.disconnect(); 159 175 if (retCode == 410 && requestMethod.equals("DELETE")) 160 176 return; // everything fine.. was already deleted. … … 170 186 if (e instanceof RuntimeException) 171 187 throw (RuntimeException)e; 172 throw new RuntimeException(e.getMessage(), e); 188 if (!cancel) 189 throw new RuntimeException(e.getMessage(), e); 173 190 } 174 191 }
Note:
See TracChangeset
for help on using the changeset viewer.