Changeset 19141 in josm


Ignore:
Timestamp:
2024-07-15T20:55:18+02:00 (3 months ago)
Author:
taylor.smock
Message:

See #23804: Try to detect common failures with OpenBrowser.displayUrl

In #23804, the user indicated that the web browser did not open, so they were
unable to go through the automatic authentication flow. This attempts to work
around that problem by detecting caught exceptions and showing the user a dialog
that will allow them to copy the authorization URL.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/oauth/OAuth20Authorization.java

    r18786 r19141  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
     6import java.awt.Dimension;
    67import java.io.IOException;
    78import java.net.MalformedURLException;
     
    1718import java.util.function.Consumer;
    1819
     20import javax.swing.JOptionPane;
     21import javax.swing.JScrollPane;
     22
     23import org.openstreetmap.josm.gui.MainApplication;
     24import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
     25import org.openstreetmap.josm.gui.util.GuiHelper;
     26import org.openstreetmap.josm.gui.widgets.HtmlPanel;
    1927import org.openstreetmap.josm.io.remotecontrol.handler.AuthorizationHandler;
    2028import org.openstreetmap.josm.io.remotecontrol.handler.RequestHandler;
     
    5664                + "&code_challenge_method=S256&code_challenge=" + s256CodeChallenge;
    5765        AuthorizationHandler.addAuthorizationConsumer(state, new OAuth20AuthorizationHandler(state, codeVerifier, parameters, consumer));
    58         OpenBrowser.displayUrl(url);
     66        GuiHelper.runInEDT(() -> showUrlOpenFailure(url, OpenBrowser.displayUrl(url)));
     67    }
     68
     69    /**
     70     * Show a message if a URL fails to open
     71     * @param url The URL that failed to open
     72     * @param error The message indicating why the URL failed to open; if {@code null}, no message is shown.
     73     */
     74    private static void showUrlOpenFailure(String url, String error) {
     75        if (error != null) {
     76            final HtmlPanel textField = new HtmlPanel("<html><body>"
     77                    + tr("The web browser failed to open with the following error: \"{0}\".<br>\n"
     78                            + "Please open the following url:<br>\n"
     79                            + "<a href=\"{1}\">{1}</a><br>\n"
     80                            + "Should we copy the URL to the clipboard?", error, url)
     81                    + "</body></html>");
     82            textField.enableClickableHyperlinks();
     83            final JScrollPane scrollPane = new JScrollPane(textField);
     84            // Ensure that the scroll pane doesn't extend too much or too little.
     85            // For now, assume that the user hasn't made the main JOSM frame beyond monitors.
     86            scrollPane.setPreferredSize(new Dimension(Math.min(textField.getPreferredSize().width + 32,
     87                    MainApplication.getMainFrame().getWidth() - 240 /* warning image + buffer */),
     88                    textField.getPreferredSize().height + scrollPane.getHorizontalScrollBar().getPreferredSize().height * 2));
     89            GuiHelper.prepareResizeableOptionPane(scrollPane, scrollPane.getPreferredSize());
     90            int answer = JOptionPane.showConfirmDialog(MainApplication.getMainFrame(), scrollPane, tr("Failed to open browser"),
     91                    JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE);
     92            if (answer == JOptionPane.YES_OPTION) {
     93                ClipboardUtils.copyString(url);
     94            }
     95        }
    5996    }
    6097
Note: See TracChangeset for help on using the changeset viewer.