Changeset 12790 in josm


Ignore:
Timestamp:
2017-09-08T21:10:14+02:00 (20 months ago)
Author:
Don-vip
Message:

see #15229 - see #15182 - remove GUI references from BugReportSender

Location:
trunk
Files:
1 added
4 edited
2 moved

Legend:

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

    r12777 r12790  
    149149import org.openstreetmap.josm.tools.bugreport.BugReportExceptionHandler;
    150150import org.openstreetmap.josm.tools.bugreport.BugReportQueue;
     151import org.openstreetmap.josm.tools.bugreport.BugReportSender;
    151152import org.xml.sax.SAXException;
    152153
     
    778779        if (!GraphicsEnvironment.isHeadless()) {
    779780            BugReportQueue.getInstance().setBugReportHandler(BugReportDialog::showFor);
     781            BugReportSender.setBugReportSendingHandler(BugReportDialog.bugReportSendingHandler);
    780782        }
    781783
  • trunk/src/org/openstreetmap/josm/gui/bugreport/BugReportDialog.java

    r12649 r12790  
    1919import javax.swing.JOptionPane;
    2020import javax.swing.JPanel;
     21import javax.swing.SwingUtilities;
    2122import javax.swing.UIManager;
    2223
     
    3233import org.openstreetmap.josm.tools.ImageProvider;
    3334import org.openstreetmap.josm.tools.InputMapUtils;
     35import org.openstreetmap.josm.tools.OpenBrowser;
    3436import org.openstreetmap.josm.tools.bugreport.BugReport;
    3537import org.openstreetmap.josm.tools.bugreport.BugReportQueue.SuppressionMode;
    3638import org.openstreetmap.josm.tools.bugreport.BugReportSender;
     39import org.openstreetmap.josm.tools.bugreport.BugReportSender.BugReportSendingHandler;
    3740import org.openstreetmap.josm.tools.bugreport.ReportedException;
    3841
     
    5255    private JCheckBox cbSuppressSingle;
    5356    private JCheckBox cbSuppressAll;
     57
     58    /**
     59     * Default bug report callback that opens the bug report form in user browser
     60     * and displays a dialog in case of error.
     61     * @since 12790
     62     */
     63    public static final BugReportSendingHandler bugReportSendingHandler = new BugReportSendingHandler() {
     64        @Override
     65        public String sendingBugReport(String bugUrl, String statusText) {
     66            return OpenBrowser.displayUrl(bugUrl);
     67        }
     68
     69        @Override
     70        public void failed(String errorMessage, String statusText) {
     71            SwingUtilities.invokeLater(() -> {
     72                JPanel errorPanel = new JPanel(new GridBagLayout());
     73                errorPanel.add(new JMultilineLabel(
     74                        tr("Opening the bug report failed. Please report manually using this website:")),
     75                        GBC.eol().fill(GridBagConstraints.HORIZONTAL));
     76                errorPanel.add(new UrlLabel(Main.getJOSMWebsite() + "/newticket", 2), GBC.eop().insets(8, 0, 0, 0));
     77                errorPanel.add(new DebugTextDisplay(statusText));
     78
     79                JOptionPane.showMessageDialog(Main.parent, errorPanel, tr("You have encountered a bug in JOSM"),
     80                        JOptionPane.ERROR_MESSAGE);
     81            });
     82        }
     83    };
    5484
    5585    /**
  • trunk/src/org/openstreetmap/josm/tools/bugreport/BugReportQueue.java

    r12770 r12790  
    44import java.util.ArrayList;
    55import java.util.LinkedList;
     6import java.util.Objects;
    67import java.util.concurrent.CopyOnWriteArrayList;
    78import java.util.function.Predicate;
     
    1819    private static final BugReportQueue INSTANCE = new BugReportQueue();
    1920
     21    /**
     22     * The fallback bug report handler if none is set. Prints the stacktrace on standard output.
     23     * @since 12770
     24     */
    2025    public static final BugReportHandler FALLBACK_BUGREPORT_HANDLER = (e, index) -> {
    2126        e.printStackTrace();
     
    142147    }
    143148
     149    /**
     150     * Sets the {@link BugReportHandler} for this queue.
     151     * @param bugReportHandler the handler in charge of displaying the bug report. Must not be null
     152     * @since 12770
     153     */
    144154    public void setBugReportHandler(BugReportHandler bugReportHandler) {
    145         this.bugReportHandler = bugReportHandler;
     155        this.bugReportHandler = Objects.requireNonNull(bugReportHandler, "bugReportHandler");
    146156    }
    147157
  • trunk/src/org/openstreetmap/josm/tools/bugreport/BugReportSender.java

    r12649 r12790  
    22package org.openstreetmap.josm.tools.bugreport;
    33
    4 import static org.openstreetmap.josm.tools.I18n.tr;
    5 
    6 import java.awt.GridBagConstraints;
    7 import java.awt.GridBagLayout;
    84import java.io.IOException;
    95import java.io.InputStream;
     
    128import java.nio.charset.StandardCharsets;
    139import java.util.Base64;
     10import java.util.Objects;
    1411
    15 import javax.swing.JOptionPane;
    16 import javax.swing.JPanel;
    17 import javax.swing.SwingUtilities;
    1812import javax.xml.parsers.ParserConfigurationException;
    1913import javax.xml.xpath.XPath;
     
    2317
    2418import org.openstreetmap.josm.Main;
    25 import org.openstreetmap.josm.gui.bugreport.DebugTextDisplay;
    26 import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
    27 import org.openstreetmap.josm.gui.widgets.UrlLabel;
    28 import org.openstreetmap.josm.tools.GBC;
    2919import org.openstreetmap.josm.tools.HttpClient;
    3020import org.openstreetmap.josm.tools.HttpClient.Response;
     
    4535public class BugReportSender extends Thread {
    4636
     37    /**
     38     * Called during bug submission to JOSM bugtracker. Completes the bug report submission and handles errors.
     39     * @since 12790
     40     */
     41    public interface BugReportSendingHandler {
     42        /**
     43         * Called when a bug is sent to JOSM bugtracker.
     44         * @param bugUrl URL to visit to effectively submit the bug report to JOSM website
     45         * @param statusText the status text being sent
     46         * @return <code>null</code> for success or a string in case of an error
     47         */
     48        String sendingBugReport(String bugUrl, String statusText);
     49
     50        /**
     51         * Called when a bug failed to be sent to JOSM bugtracker.
     52         * @param errorMessage the error message
     53         * @param statusText the status text being sent
     54         */
     55        void failed(String errorMessage, String statusText);
     56    }
     57
     58    /**
     59     * The fallback bug report sending handler if none is set.
     60     * @since xxx
     61     */
     62    public static final BugReportSendingHandler FALLBACK_BUGREPORT_SENDING_HANDLER = new BugReportSendingHandler() {
     63        @Override
     64        public String sendingBugReport(String bugUrl, String statusText) {
     65            return OpenBrowser.displayUrl(bugUrl);
     66        }
     67
     68        @Override
     69        public void failed(String errorMessage, String statusText) {
     70            Logging.error("Unable to send bug report: {0}\n{1}", errorMessage, statusText);
     71        }
     72    };
     73
     74    private static BugReportSendingHandler handler = FALLBACK_BUGREPORT_SENDING_HANDLER;
     75
    4776    private final String statusText;
    4877    private String errorMessage;
     
    6291            // first, send the debug text using post.
    6392            String debugTextPasteId = pasteDebugText();
     93            String bugUrl = getJOSMTicketURL() + "?pdata_stored=" + debugTextPasteId;
    6494
    65             // then open a browser to display the pasted text.
    66             String openBrowserError = OpenBrowser.displayUrl(getJOSMTicketURL() + "?pdata_stored=" + debugTextPasteId);
    67             if (openBrowserError != null) {
    68                 Logging.warn(openBrowserError);
    69                 failed(openBrowserError);
     95            // then notify handler
     96            errorMessage = handler.sendingBugReport(bugUrl, statusText);
     97            if (errorMessage != null) {
     98                Logging.warn(errorMessage);
     99                handler.failed(errorMessage, statusText);
    70100            }
    71101        } catch (BugReportSenderException e) {
    72102            Logging.warn(e);
    73             failed(e.getMessage());
     103            errorMessage = e.getMessage();
     104            handler.failed(errorMessage, statusText);
    74105        }
    75106    }
     
    128159    }
    129160
    130     private void failed(String string) {
    131         errorMessage = string;
    132         SwingUtilities.invokeLater(() -> {
    133             JPanel errorPanel = new JPanel(new GridBagLayout());
    134             errorPanel.add(new JMultilineLabel(
    135                     tr("Opening the bug report failed. Please report manually using this website:")),
    136                     GBC.eol().fill(GridBagConstraints.HORIZONTAL));
    137             errorPanel.add(new UrlLabel(Main.getJOSMWebsite() + "/newticket", 2), GBC.eop().insets(8, 0, 0, 0));
    138             errorPanel.add(new DebugTextDisplay(statusText));
    139 
    140             JOptionPane.showMessageDialog(Main.parent, errorPanel, tr("You have encountered a bug in JOSM"),
    141                     JOptionPane.ERROR_MESSAGE);
    142         });
    143     }
    144 
    145161    /**
    146162     * Returns the error message that could have occured during bug sending.
     
    171187        return sender;
    172188    }
     189
     190    /**
     191     * Sets the {@link BugReportSendingHandler} for bug report sender.
     192     * @param bugReportSendingHandler the handler in charge of completing the bug report submission and handle errors. Must not be null
     193     * @since 12790
     194     */
     195    public static void setBugReportSendingHandler(BugReportSendingHandler bugReportSendingHandler) {
     196        handler = Objects.requireNonNull(bugReportSendingHandler, "bugReportSendingHandler");
     197    }
    173198}
  • trunk/test/unit/org/openstreetmap/josm/gui/bugreport/BugReportSettingsPanelTest.java

    r12789 r12790  
    11// License: GPL. For details, see LICENSE file.
    2 package org.openstreetmap.josm.tools.bugreport;
     2package org.openstreetmap.josm.gui.bugreport;
    33
    44import static org.junit.Assert.assertNotNull;
     
    66import org.junit.Rule;
    77import org.junit.Test;
    8 import org.openstreetmap.josm.gui.bugreport.BugReportSettingsPanel;
    98import org.openstreetmap.josm.testutils.JOSMTestRules;
     9import org.openstreetmap.josm.tools.bugreport.BugReport;
    1010
    1111import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
  • trunk/test/unit/org/openstreetmap/josm/gui/bugreport/DebugTextDisplayTest.java

    r12789 r12790  
    11// License: GPL. For details, see LICENSE file.
    2 package org.openstreetmap.josm.tools.bugreport;
     2package org.openstreetmap.josm.gui.bugreport;
    33
    44import static org.junit.Assert.assertEquals;
     
    66import org.junit.Rule;
    77import org.junit.Test;
    8 import org.openstreetmap.josm.gui.bugreport.DebugTextDisplay;
    98import org.openstreetmap.josm.gui.datatransfer.ClipboardUtils;
    109import org.openstreetmap.josm.testutils.JOSMTestRules;
Note: See TracChangeset for help on using the changeset viewer.