- Timestamp:
- 2016-03-28T14:23:40+02:00 (8 years ago)
- Location:
- trunk
- Files:
-
- 1 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/tools/bugreport/BugReportExceptionHandler.java
r10064 r10067 5 5 6 6 import java.awt.Component; 7 import java.awt.GraphicsEnvironment; 7 8 import java.awt.GridBagConstraints; 8 9 import java.awt.GridBagLayout; … … 44 45 private static boolean suppressExceptionDialogs; 45 46 46 private staticclass BugReporterThread extends Thread {47 static final class BugReporterThread extends Thread { 47 48 48 49 private final class BugReporterWorker implements Runnable { … … 57 58 // Then ask for submitting a bug report, for exceptions thrown from a plugin too, unless updated to a new version 58 59 if (pluginDownloadTask == null) { 59 String[] buttonTexts = new String[] {tr("Do nothing"), tr("Report Bug")}; 60 String[] buttonIcons = new String[] {"cancel", "bug"}; 61 int defaultButtonIdx = 1; 62 String message = tr("An unexpected exception occurred.<br>" + 63 "This is always a coding error. If you are running the latest<br>" + 64 "version of JOSM, please consider being kind and file a bug report." 65 ); 66 // Check user is running current tested version, the error may already be fixed 67 int josmVersion = Version.getInstance().getVersion(); 68 if (josmVersion != Version.JOSM_UNKNOWN_VERSION) { 69 try { 70 int latestVersion = Integer.parseInt(new WikiReader(). 71 read(Main.getJOSMWebsite()+"/wiki/TestedVersion?format=txt").trim()); 72 if (latestVersion > josmVersion) { 73 buttonTexts = new String[] {tr("Do nothing"), tr("Update JOSM"), tr("Report Bug")}; 74 buttonIcons = new String[] {"cancel", "download", "bug"}; 75 defaultButtonIdx = 2; 76 message = tr("An unexpected exception occurred. This is always a coding error.<br><br>" + 77 "However, you are running an old version of JOSM ({0}),<br>" + 78 "instead of using the current tested version (<b>{1}</b>).<br><br>"+ 79 "<b>Please update JOSM</b> before considering to file a bug report.", 80 String.valueOf(josmVersion), String.valueOf(latestVersion)); 81 } 82 } catch (IOException | NumberFormatException e) { 83 Main.warn("Unable to detect latest version of JOSM: "+e.getMessage()); 84 } 85 } 86 // Show dialog 87 ExtendedDialog ed = new ExtendedDialog(Main.parent, tr("Unexpected Exception"), buttonTexts); 88 ed.setButtonIcons(buttonIcons); 89 ed.setIcon(JOptionPane.ERROR_MESSAGE); 90 ed.setCancelButton(1); 91 ed.setDefaultButton(defaultButtonIdx); 92 JPanel pnl = new JPanel(new GridBagLayout()); 93 pnl.add(new JLabel("<html>" + message + "</html>"), GBC.eol()); 94 JCheckBox cbSuppress = null; 95 if (exceptionCounter > 1) { 96 cbSuppress = new JCheckBox(tr("Suppress further error dialogs for this session.")); 97 pnl.add(cbSuppress, GBC.eol()); 98 } 99 ed.setContent(pnl); 100 ed.setFocusOnDefaultButton(true); 101 ed.showDialog(); 102 if (cbSuppress != null && cbSuppress.isSelected()) { 103 suppressExceptionDialogs = true; 104 } 105 if (ed.getValue() <= 1) { 106 // "Do nothing" 107 return; 108 } else if (ed.getValue() < buttonTexts.length) { 109 // "Update JOSM" 110 try { 111 Main.platform.openUrl(Main.getJOSMWebsite()); 112 } catch (IOException e) { 113 Main.warn("Unable to access JOSM website: "+e.getMessage()); 114 } 115 } else { 116 // "Report bug" 117 askForBugReport(e); 118 } 60 askForBugReport(e); 119 61 } else { 120 62 // Ask for restart to install new plugin … … 131 73 * @param t the exception 132 74 */ 133 BugReporterThread(Throwable t) {75 private BugReporterThread(Throwable t) { 134 76 super("Bug Reporter"); 135 77 this.e = t; 78 } 79 80 static void askForBugReport(final Throwable e) { 81 String[] buttonTexts = new String[] {tr("Do nothing"), tr("Report Bug")}; 82 String[] buttonIcons = new String[] {"cancel", "bug"}; 83 int defaultButtonIdx = 1; 84 String message = tr("An unexpected exception occurred.<br>" + 85 "This is always a coding error. If you are running the latest<br>" + 86 "version of JOSM, please consider being kind and file a bug report." 87 ); 88 // Check user is running current tested version, the error may already be fixed 89 int josmVersion = Version.getInstance().getVersion(); 90 if (josmVersion != Version.JOSM_UNKNOWN_VERSION) { 91 try { 92 int latestVersion = Integer.parseInt(new WikiReader(). 93 read(Main.getJOSMWebsite()+"/wiki/TestedVersion?format=txt").trim()); 94 if (latestVersion > josmVersion) { 95 buttonTexts = new String[] {tr("Do nothing"), tr("Update JOSM"), tr("Report Bug")}; 96 buttonIcons = new String[] {"cancel", "download", "bug"}; 97 defaultButtonIdx = 2; 98 message = tr("An unexpected exception occurred. This is always a coding error.<br><br>" + 99 "However, you are running an old version of JOSM ({0}),<br>" + 100 "instead of using the current tested version (<b>{1}</b>).<br><br>"+ 101 "<b>Please update JOSM</b> before considering to file a bug report.", 102 String.valueOf(josmVersion), String.valueOf(latestVersion)); 103 } 104 } catch (IOException | NumberFormatException ex) { 105 Main.warn("Unable to detect latest version of JOSM: "+ex.getMessage()); 106 } 107 } 108 // Build panel 109 JPanel pnl = new JPanel(new GridBagLayout()); 110 pnl.add(new JLabel("<html>" + message + "</html>"), GBC.eol()); 111 JCheckBox cbSuppress = null; 112 if (exceptionCounter > 1) { 113 cbSuppress = new JCheckBox(tr("Suppress further error dialogs for this session.")); 114 pnl.add(cbSuppress, GBC.eol()); 115 } 116 if (GraphicsEnvironment.isHeadless()) { 117 return; 118 } 119 // Show dialog 120 ExtendedDialog ed = new ExtendedDialog(Main.parent, tr("Unexpected Exception"), buttonTexts); 121 ed.setButtonIcons(buttonIcons); 122 ed.setIcon(JOptionPane.ERROR_MESSAGE); 123 ed.setCancelButton(1); 124 ed.setDefaultButton(defaultButtonIdx); 125 ed.setContent(pnl); 126 ed.setFocusOnDefaultButton(true); 127 ed.showDialog(); 128 if (cbSuppress != null && cbSuppress.isSelected()) { 129 suppressExceptionDialogs = true; 130 } 131 if (ed.getValue() <= 1) { 132 // "Do nothing" 133 return; 134 } else if (ed.getValue() < buttonTexts.length) { 135 // "Update JOSM" 136 try { 137 Main.platform.openUrl(Main.getJOSMWebsite()); 138 } catch (IOException ex) { 139 Main.warn("Unable to access JOSM website: "+ex.getMessage()); 140 } 141 } else { 142 // "Report bug" 143 try { 144 JPanel p = buildPanel(e); 145 JOptionPane.showMessageDialog(Main.parent, p, tr("You have encountered a bug in JOSM"), JOptionPane.ERROR_MESSAGE); 146 } catch (Exception ex) { 147 Main.error(ex); 148 } 149 } 136 150 } 137 151 … … 182 196 } 183 197 184 private static void askForBugReport(final Throwable e) {185 try {186 JPanel p = buildPanel(e);187 JOptionPane.showMessageDialog(Main.parent, p, tr("You have encountered a bug in JOSM"), JOptionPane.ERROR_MESSAGE);188 } catch (Exception e1) {189 Main.error(e1);190 }191 }192 193 198 static JPanel buildPanel(final Throwable e) { 194 199 StringWriter stack = new StringWriter(); -
trunk/src/org/openstreetmap/josm/tools/bugreport/BugReportSender.java
r10055 r10067 49 49 50 50 private final String statusText; 51 private String errorMessage; 51 52 52 53 /** … … 54 55 * @param statusText The status text to send. 55 56 */ 56 p ublicBugReportSender(String statusText) {57 protected BugReportSender(String statusText) { 57 58 super("Bug report sender"); 58 59 this.statusText = statusText; … … 101 102 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 102 103 Document document = builder.parse(in); 103 return retri veDebugToken(document);104 return retrieveDebugToken(document); 104 105 } 105 106 } catch (IOException | SAXException | ParserConfigurationException | XPathExpressionException t) { … … 108 109 } 109 110 110 private String getJOSMTicketURL() {111 private static String getJOSMTicketURL() { 111 112 return Main.getJOSMWebsite() + "/josmticket"; 112 113 } 113 114 114 private String retriveDebugToken(Document document) throws XPathExpressionException, BugReportSenderException {115 private static String retrieveDebugToken(Document document) throws XPathExpressionException, BugReportSenderException { 115 116 XPathFactory factory = XPathFactory.newInstance(); 116 117 XPath xpath = factory.newXPath(); … … 134 135 135 136 private void failed(String string) { 137 errorMessage = string; 136 138 SwingUtilities.invokeLater(new Runnable() { 137 139 @Override 138 140 public void run() { 139 JPanel errorPanel = new JPanel(); 140 errorPanel.setLayout(new GridBagLayout()); 141 JPanel errorPanel = new JPanel(new GridBagLayout()); 141 142 errorPanel.add(new JMultilineLabel( 142 143 tr("Opening the bug report failed. Please report manually using this website:")), … … 149 150 } 150 151 }); 152 } 153 154 /** 155 * Returns the error message that could have occured during bug sending. 156 * @return the error message, or {@code null} if successful 157 */ 158 public final String getErrorMessage() { 159 return errorMessage; 151 160 } 152 161 … … 164 173 * Opens the bug report window on the JOSM server. 165 174 * @param statusText The status text to send along to the server. 175 * @return bug report sender started thread 166 176 */ 167 public static void reportBug(String statusText) { 168 new BugReportSender(statusText).start(); 177 public static BugReportSender reportBug(String statusText) { 178 BugReportSender sender = new BugReportSender(statusText); 179 sender.start(); 180 return sender; 169 181 } 170 182 } -
trunk/test/unit/org/openstreetmap/josm/tools/bugreport/BugReportExceptionHandlerTest.java
r10064 r10067 2 2 package org.openstreetmap.josm.tools.bugreport; 3 3 4 import static org.junit.Assert.assertFalse; 4 5 import static org.junit.Assert.assertNotNull; 5 6 … … 18 19 @Before 19 20 public void setUp() { 20 JOSMFixture.createUnitTestFixture().init( );21 JOSMFixture.createUnitTestFixture().init(true); 21 22 } 22 23 … … 26 27 @Test 27 28 public void testBuildPanel() { 28 assertNotNull(BugReportExceptionHandler.buildPanel(new Exception())); 29 assertNotNull(BugReportExceptionHandler.buildPanel(new Exception("testBuildPanel"))); 30 } 31 32 /** 33 * Unit test for {@link BugReportExceptionHandler.BugReporterThread#askForBugReport} method. 34 */ 35 @Test 36 public void testAskForBugReport() { 37 BugReportExceptionHandler.BugReporterThread.askForBugReport(new Exception("testAskForBugReport")); 38 } 39 40 /** 41 * Unit test for {@link BugReportExceptionHandler#handleException} method. 42 */ 43 @Test 44 public void testHandleException() { 45 BugReportExceptionHandler.handleException(new Exception("testHandleException")); 46 assertFalse(BugReportExceptionHandler.exceptionHandlingInProgress()); 29 47 } 30 48 }
Note:
See TracChangeset
for help on using the changeset viewer.