Ignore:
Timestamp:
2016-11-23T23:39:50+01:00 (7 years ago)
Author:
simon04
Message:

Jump to Position: automatically paste URL from clipboard

Also, use OsmUrlToBounds for parsing bounds from URL

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/JumpToAction.java

    r11300 r11301  
    99import java.awt.event.ActionEvent;
    1010import java.awt.event.KeyEvent;
     11import java.util.Optional;
    1112
    1213import javax.swing.JLabel;
     
    2122import org.openstreetmap.josm.gui.ExtendedDialog;
    2223import org.openstreetmap.josm.gui.MapView;
     24import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
    2325import org.openstreetmap.josm.gui.widgets.JosmTextField;
    2426import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator;
     
    9193        }
    9294        MapView mv = Main.map.mapView;
    93         LatLon curPos = mv.getProjection().eastNorth2latlon(mv.getCenter());
    94         lat.setText(Double.toString(curPos.lat()));
    95         lon.setText(Double.toString(curPos.lon()));
    96 
    97         double dist = mv.getDist100Pixel();
    98         zm.setText(Long.toString(Math.round(dist*100)/100));
     95
     96        final Optional<Bounds> boundsFromClipboard = Optional
     97                .ofNullable(ClipboardUtils.getClipboardStringContent())
     98                .map(OsmUrlToBounds::parse);
     99        if (boundsFromClipboard.isPresent()) {
     100            setBounds(boundsFromClipboard.get());
     101        } else {
     102            setBounds(mv.getState().getViewArea().getCornerBounds());
     103        }
    99104        updateUrl(true);
    100105
     
    158163        }
    159164
    160         double zoomFactor = 1/dist;
     165        double zoomFactor = 1/ mv.getDist100Pixel();
    161166        mv.zoomToFactor(mv.getProjection().latlon2eastNorth(ll), zoomFactor * zoomLvl);
    162167    }
     
    166171        String urlText = url.getText();
    167172        Bounds b = OsmUrlToBounds.parse(urlText);
     173        setBounds(b);
     174    }
     175
     176    private void setBounds(Bounds b) {
    168177        if (b != null) {
    169             lat.setText(Double.toString((b.getMinLat() + b.getMaxLat())/2));
    170             lon.setText(Double.toString((b.getMinLon() + b.getMaxLon())/2));
    171 
    172             int zoomLvl = 16;
    173             int hashIndex = urlText.indexOf("#map");
    174             if (hashIndex >= 0) {
    175                 zoomLvl = Integer.parseInt(urlText.substring(hashIndex+5, urlText.indexOf('/', hashIndex)));
    176             } else {
    177                 String[] args = urlText.substring(urlText.indexOf('?')+1).split("&");
    178                 for (String arg : args) {
    179                     int eq = arg.indexOf('=');
    180                     if (eq == -1 || !"zoom".equalsIgnoreCase(arg.substring(0, eq))) continue;
    181 
    182                     zoomLvl = Integer.parseInt(arg.substring(eq + 1));
    183                     break;
    184                 }
    185             }
    186 
    187             // 10 000 000 = 10 000 * 1000 = World * (km -> m)
    188             zm.setText(Double.toString(Math.round(10000000d * Math.pow(2d, (-1d) * zoomLvl))));
     178            final LatLon center = b.getCenter();
     179            lat.setText(Double.toString(center.lat()));
     180            lon.setText(Double.toString(center.lon()));
     181            zm.setText(Double.toString(OsmUrlToBounds.getZoom(b)));
    189182        }
    190183    }
     
    195188            double dlat = Double.parseDouble(lat.getText());
    196189            double dlon = Double.parseDouble(lon.getText());
    197             double m = Double.parseDouble(zm.getText());
    198             // Inverse function to the one above. 18 is the current maximum zoom
    199             // available on standard renderers, so choose this is in case m should be zero
    200             int zoomLvl = 18;
    201             if (m > 0)
    202                 zoomLvl = (int) Math.round((-1) * Math.log(m/10_000_000)/Math.log(2));
    203 
    204             url.setText(OsmUrlToBounds.getURL(dlat, dlon, zoomLvl));
     190            double zoomLvl = Double.parseDouble(zm.getText());
     191            url.setText(OsmUrlToBounds.getURL(dlat, dlon, (int) zoomLvl));
    205192        } catch (NumberFormatException x) {
    206193            Main.debug(x.getMessage());
Note: See TracChangeset for help on using the changeset viewer.