Changeset 6155 in josm


Ignore:
Timestamp:
2013-08-19T11:42:28+02:00 (11 years ago)
Author:
bastiK
Message:

fixed #8945 - another NPE + unit test (patch by AlfonZ)

Location:
trunk
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/OsmUrlToBounds.java

    r6119 r6155  
    22package org.openstreetmap.josm.tools;
    33
     4import java.awt.HeadlessException;
    45import java.awt.Toolkit;
    56import java.io.UnsupportedEncodingException;
     
    2728        if (b != null)
    2829            return b;
    29         int i = url.indexOf('?');
     30        int i = url.indexOf("#map");
     31        if (i >= 0) {
     32            // probably it's a URL following the new scheme?
     33            return parseHashURLs(url);
     34        }
     35        i = url.indexOf('?');
    3036        if (i == -1) {
    31             //probably it's a URL following the new scheme?
    32             if (url.indexOf('#') >= 0)
    33                 return parseHashURLs(url);
    34             else
    35                 return null;
     37            return null;
    3638        }
    3739        String[] args = url.substring(i+1).split("&");
     
    8082     * The following function, called by the old parse function if necessary, provides parsing new URLs
    8183     * the new URLs follow the scheme http://www.openstreetmap.org/#map=18/51.71873/8.76164&layers=CN
    82      * @param url
    83      * @return
     84     * @param url string for parsing
     85     * @return Bounds if hashurl, {@code null} otherwise
    8486     */
    8587    private static Bounds parseHashURLs(String url) {
    86         int startIndex = url.indexOf("=");
     88        int startIndex = url.indexOf("#map=");
    8789        if (startIndex == -1) return null;
    88         int endIndex = url.indexOf("&");
     90        int endIndex = url.indexOf('&', startIndex);
    8991        if (endIndex == -1) endIndex = url.length();
    9092        try
    9193        {
    92             String coordPart = url.substring(startIndex+1, endIndex);
     94            String coordPart = url.substring(startIndex+5, endIndex);
    9395            String[] parts = coordPart.split("/");
    9496            Bounds b = positionToBounds(Double.parseDouble(parts[1]),
     
    181183    public static Bounds positionToBounds(final double lat, final double lon, final int zoom) {
    182184        int tileSizeInPixels = 256;
    183         int height = Toolkit.getDefaultToolkit().getScreenSize().height;
    184         int width = Toolkit.getDefaultToolkit().getScreenSize().width;
    185         if (Main.isDisplayingMapView()) {
    186             height = Main.map.mapView.getHeight();
    187             width = Main.map.mapView.getWidth();
     185        int height;
     186        int width;
     187        try {
     188            height = Toolkit.getDefaultToolkit().getScreenSize().height;
     189            width = Toolkit.getDefaultToolkit().getScreenSize().width;
     190            if (Main.isDisplayingMapView()) {
     191                height = Main.map.mapView.getHeight();
     192                width = Main.map.mapView.getWidth();
     193            }
     194        } catch (HeadlessException he) {
     195            // in headless mode, when running tests
     196            height = 480;
     197            width = 640;
    188198        }
    189199        double scale = (1 << zoom) * tileSizeInPixels / (2 * Math.PI * R);
Note: See TracChangeset for help on using the changeset viewer.