Changeset 6299 in josm for trunk


Ignore:
Timestamp:
2013-10-06T17:26:37+02:00 (11 years ago)
Author:
Don-vip
Message:

Improve feedback when clicking a web marker pointing to a local file fails

Location:
trunk/src/org/openstreetmap/josm
Files:
6 edited

Legend:

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

    r6296 r6299  
    293293     */
    294294    public static void error(Throwable t) {
    295         error(t.getClass().getName()+": "+t.getMessage());
     295        error(t.getClass().getName()+": "+t.getMessage().trim());
    296296    }
    297297   
     
    302302     */
    303303    public static void warn(Throwable t) {
    304         warn(t.getClass().getName()+": "+t.getMessage());
     304        warn(t.getClass().getName()+": "+t.getMessage().trim());
    305305    }
    306306
  • trunk/src/org/openstreetmap/josm/gui/layer/markerlayer/ButtonMarker.java

    r4284 r6299  
    4949        Point screen = mv.getPoint(getEastNorth());
    5050        buttonRectangle.setLocation(screen.x+4, screen.y+2);
    51         symbol.paintIcon(mv, g, screen.x+4, screen.y+2);
     51        paintIcon(mv, g, screen.x+4, screen.y+2);
    5252        Border b;
    5353        Point mousePosition = mv.getMousePosition();
  • trunk/src/org/openstreetmap/josm/gui/layer/markerlayer/Marker.java

    r6162 r6299  
    22package org.openstreetmap.josm.gui.layer.markerlayer;
    33
     4import java.awt.AlphaComposite;
     5import java.awt.Color;
    46import java.awt.Graphics;
     7import java.awt.Graphics2D;
    58import java.awt.Point;
    69import java.awt.event.ActionEvent;
     10import java.awt.image.BufferedImage;
    711import java.io.File;
    812import java.net.MalformedURLException;
     
    1923import java.util.TimeZone;
    2024
    21 import javax.swing.Icon;
     25import javax.swing.ImageIcon;
    2226
    2327import org.openstreetmap.josm.Main;
     
    279283    private final String text;
    280284
    281     public final Icon symbol;
     285    protected final ImageIcon symbol;
     286    private BufferedImage redSymbol = null;
    282287    public final MarkerLayer parentLayer;
    283     public double time; /* absolute time of marker in seconds since epoch */
    284     public double offset; /* time offset in seconds from the gpx point from which it was derived,
    285                              may be adjusted later to sync with other data, so not final */
     288    /** Absolute time of marker in seconds since epoch */
     289    public double time;
     290    /** Time offset in seconds from the gpx point from which it was derived, may be adjusted later to sync with other data, so not final */
     291    public double offset;
    286292
    287293    private String cachedText;
    288294    private int textVersion = -1;
    289295    private CachedLatLon coor;
     296   
     297    private boolean erroneous = false;
    290298
    291299    public Marker(LatLon ll, TemplateEngineDataProvider dataProvider, String iconName, MarkerLayer parentLayer, double time, double offset) {
     
    294302        this.offset = offset;
    295303        this.time = time;
    296         // /* ICON(markers/) */"Bridge"
    297         // /* ICON(markers/) */"Crossing"
    298304        this.symbol = iconName != null ? ImageProvider.getIfAvailable("markers",iconName) : null;
    299305        this.parentLayer = parentLayer;
     
    308314        this.offset = offset;
    309315        this.time = time;
    310         // /* ICON(markers/) */"Bridge"
    311         // /* ICON(markers/) */"Crossing"
    312316        this.symbol = iconName != null ? ImageProvider.getIfAvailable("markers",iconName) : null;
    313317        this.parentLayer = parentLayer;
     
    340344    }
    341345
     346    /**
     347     * Sets the marker's coordinates.
     348     * @param coor The marker's coordinates (lat/lon)
     349     */
    342350    public final void setCoor(LatLon coor) {
    343351        this.coor = new CachedLatLon(coor);
    344352    }
    345353
     354    /**
     355     * Returns the marker's coordinates.
     356     * @return The marker's coordinates (lat/lon)
     357     */
    346358    public final LatLon getCoor() {
    347359        return coor;
    348360    }
    349361
     362    /**
     363     * Sets the marker's projected coordinates.
     364     * @param eastNorth The marker's projected coordinates (easting/northing)
     365     */
    350366    public final void setEastNorth(EastNorth eastNorth) {
    351367        this.coor = new CachedLatLon(eastNorth);
    352368    }
    353369
     370    /**
     371     * Returns the marker's projected coordinates.
     372     * @return The marker's projected coordinates (easting/northing)
     373     */
    354374    public final EastNorth getEastNorth() {
    355375        return coor.getEastNorth();
    356376    }
    357 
    358377
    359378    /**
     
    377396    }
    378397
    379 
    380398    /**
    381399     * Paints the marker.
     
    388406        Point screen = mv.getPoint(getEastNorth());
    389407        if (symbol != null && showTextOrIcon) {
    390             symbol.paintIcon(mv, g, screen.x-symbol.getIconWidth()/2, screen.y-symbol.getIconHeight()/2);
     408            paintIcon(mv, g, screen.x-symbol.getIconWidth()/2, screen.y-symbol.getIconHeight()/2);
    391409        } else {
    392410            g.drawLine(screen.x-2, screen.y-2, screen.x+2, screen.y+2);
     
    399417        }
    400418    }
    401 
     419   
     420    protected void paintIcon(MapView mv, Graphics g, int x, int y) {
     421        if (!erroneous) {
     422            symbol.paintIcon(mv, g, x, y);
     423        } else {
     424            if (redSymbol == null) {
     425                int width = symbol.getIconWidth();
     426                int height = symbol.getIconHeight();
     427                               
     428                redSymbol = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
     429                Graphics2D gbi = redSymbol.createGraphics();
     430                gbi.drawImage(symbol.getImage(), 0, 0, null);
     431                gbi.setColor(Color.RED);
     432                gbi.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.666f));
     433                gbi.fillRect(0, 0, width, height);
     434                gbi.dispose();
     435            }
     436            g.drawImage(redSymbol, x, y, mv);
     437        }
     438    }
    402439
    403440    protected TemplateEntryProperty getTextTemplate() {
     
    465502        throw new UnsupportedOperationException();
    466503    }
     504   
     505    /**
     506     * Determines if this marker is erroneous.
     507     * @return {@code true} if this markers has any kind of error, {@code false} otherwise
     508     * @since 6299
     509     */
     510    public final boolean isErroneous() {
     511        return erroneous;
     512    }
     513   
     514    /**
     515     * Sets this marker erroneous or not.
     516     * @param erroneous {@code true} if this markers has any kind of error, {@code false} otherwise
     517     * @since 6299
     518     */
     519    public final void setErroneous(boolean erroneous) {
     520        this.erroneous = erroneous;
     521        if (!erroneous) {
     522            redSymbol = null;
     523        }
     524    }
    467525}
  • trunk/src/org/openstreetmap/josm/gui/layer/markerlayer/PlayHeadMarker.java

    r6296 r6299  
    274274        if (time < 0.0) return;
    275275        Point screen = mv.getPoint(getEastNorth());
    276         symbol.paintIcon(mv, g, screen.x, screen.y);
     276        paintIcon(mv, g, screen.x, screen.y);
    277277    }
    278278
  • trunk/src/org/openstreetmap/josm/gui/layer/markerlayer/WebMarker.java

    r5684 r6299  
    55
    66import java.awt.event.ActionEvent;
     7import java.io.File;
    78import java.net.URL;
    89import java.util.Collections;
     
    1516import org.openstreetmap.josm.data.gpx.GpxLink;
    1617import org.openstreetmap.josm.data.gpx.WayPoint;
     18import org.openstreetmap.josm.gui.Notification;
     19import org.openstreetmap.josm.tools.CheckParameterUtil;
    1720import org.openstreetmap.josm.tools.OpenBrowser;
    1821
     
    2528public class WebMarker extends ButtonMarker {
    2629
    27     public final URL webUrl;
     30    private final URL webUrl;
    2831
    2932    public WebMarker(LatLon ll, URL webUrl, MarkerLayer parentLayer, double time, double offset) {
    3033        super(ll, "web.png", parentLayer, time, offset);
     34        CheckParameterUtil.ensureParameterNotNull(webUrl, "webUrl");
    3135        this.webUrl = webUrl;
    3236    }
     
    3539        String error = OpenBrowser.displayUrl(webUrl.toString());
    3640        if (error != null) {
    37             JOptionPane.showMessageDialog(Main.parent,
    38                     "<html><b>" +
    39                             tr("There was an error while trying to display the URL for this marker") +
    40                             "</b><br>" + tr("(URL was: ") + webUrl.toString() + ")" + "<br>" + error,
    41                             tr("Error displaying URL"), JOptionPane.ERROR_MESSAGE);
     41            setErroneous(true);
     42            new Notification(
     43                    "<b>" + tr("There was an error while trying to display the URL for this marker") + "</b><br>" +
     44                                  tr("(URL was: ") + webUrl.toString() + ")" + "<br>" + error)
     45                    .setIcon(JOptionPane.ERROR_MESSAGE)
     46                    .setDuration(Notification.TIME_LONG)
     47                    .show();
     48        } else {
     49            updateErroneous();
    4250        }
    4351    }
     
    5159        return wpt;
    5260    }
     61   
     62    private final void updateErroneous() {
     63        if ("file".equals(webUrl.getProtocol())) {
     64            String path = webUrl.getPath();
     65            try {
     66                setErroneous(path.isEmpty() || !new File(path).exists());
     67            } catch (Exception e) {
     68                Main.warn(e);
     69                setErroneous(true);
     70            }
     71        } else {
     72            setErroneous(false);
     73        }
     74    }
    5375}
  • trunk/src/org/openstreetmap/josm/tools/OpenBrowser.java

    r6248 r6299  
    2929
    3030    /**
     31     * Displays an external URI using platform associated software.
     32     * A web resource will launch platform's browser, an audio file URI will launch audio player, etc.
     33     * @param uri The URI to display
    3134     * @return <code>null</code> for success or a string in case of an error.
    3235     * @throws IllegalStateException thrown if no platform is set to which opening the URL can be dispatched,
     
    3437     */
    3538    public static String displayUrl(URI uri) {
     39        CheckParameterUtil.ensureParameterNotNull(uri, "uri");
    3640        if (Main.applet) {
    3741            try {
     
    4347            }
    4448        }
     49       
     50        Main.info(tr("Opening URL: {0}", uri));
    4551
    4652        if (Desktop.isDesktopSupported()) {
    4753            try {
    48                 try {
     54                if (Main.platform instanceof PlatformHookWindows) {
     55                    // Desktop API works fine under Windows, so we don't try any fallback in case of I/O exceptions because it's not API's fault
    4956                    Desktop.getDesktop().browse(uri);
    50                 } catch (IOException e) {
    51                     // Workaround for KDE (Desktop API is severely flawed)
    52                     // see http://bugs.sun.com/view_bug.do?bug_id=6486393
    53                     Main.warn("Desktop class failed. Platform dependent fall back for open url in browser.");
    54                     displayUrlFallback(uri);
     57                } else {
     58                    // This is not the case with some Linux environments (see below), and not sure about Mac OS X, so we need to handle API failure
     59                    try {
     60                        Desktop.getDesktop().browse(uri);
     61                    } catch (IOException e) {
     62                        // Workaround for KDE (Desktop API is severely flawed)
     63                        // see https://bugs.openjdk.java.net/browse/JDK-6486393
     64                        Main.warn("Desktop class failed. Platform dependent fall back for open url in browser.");
     65                        displayUrlFallback(uri);
     66                    }
    5567                }
    5668            } catch (Exception e) {
    57                 e.printStackTrace();
     69                Main.warn(e);
    5870                return e.getMessage();
    5971            }
     
    6981    }
    7082
     83    /**
     84     * Displays an external URL using platform associated software.
     85     * A web resource will launch platform's browser, an audio file URL will launch audio player, etc.
     86     * @param url The URL to display
     87     * @return <code>null</code> for success or a string in case of an error.
     88     * @throws IllegalStateException thrown if no platform is set to which opening the URL can be dispatched,
     89     * {@link Main#platform}
     90     */
    7191    public static String displayUrl(String url) {
    7292        try {
Note: See TracChangeset for help on using the changeset viewer.