source: josm/trunk/src/org/openstreetmap/josm/tools/bugreport/BugReportDialog.java@ 11191

Last change on this file since 11191 was 11102, checked in by Don-vip, 8 years ago

add more unit tests

  • Property svn:eol-style set to native
File size: 10.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools.bugreport;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.awt.Frame;
8import java.awt.GridBagConstraints;
9import java.awt.GridBagLayout;
10import java.awt.event.ActionEvent;
11
12import javax.swing.AbstractAction;
13import javax.swing.BorderFactory;
14import javax.swing.Icon;
15import javax.swing.JButton;
16import javax.swing.JCheckBox;
17import javax.swing.JDialog;
18import javax.swing.JLabel;
19import javax.swing.JOptionPane;
20import javax.swing.JPanel;
21import javax.swing.UIManager;
22
23import org.openstreetmap.josm.Main;
24import org.openstreetmap.josm.actions.ExpertToggleAction;
25import org.openstreetmap.josm.gui.preferences.plugin.PluginPreference;
26import org.openstreetmap.josm.gui.util.GuiHelper;
27import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
28import org.openstreetmap.josm.gui.widgets.UrlLabel;
29import org.openstreetmap.josm.plugins.PluginDownloadTask;
30import org.openstreetmap.josm.plugins.PluginHandler;
31import org.openstreetmap.josm.tools.GBC;
32import org.openstreetmap.josm.tools.ImageProvider;
33import org.openstreetmap.josm.tools.InputMapUtils;
34import org.openstreetmap.josm.tools.bugreport.BugReportQueue.SuppressionMode;
35
36/**
37 * This is a dialog that can be used to display a bug report.
38 * <p>
39 * It displays the bug to the user and asks the user to submit a bug report.
40 * @author Michael Zangl
41 * @since 10649
42 */
43public class BugReportDialog extends JDialog {
44 private static final int MAX_MESSAGE_SIZE = 500;
45 // This is explicitly not an ExtendedDialog - we still want to be able to display bug reports if there are problems with preferences/..
46 private final JPanel content = new JPanel(new GridBagLayout());
47 private final BugReport report;
48 private final DebugTextDisplay textPanel;
49 private JCheckBox cbSuppressSingle;
50 private JCheckBox cbSuppressAll;
51
52 /**
53 * Create a new dialog.
54 * @param report The report to display the dialog for.
55 */
56 public BugReportDialog(BugReport report) {
57 super(findParent(), tr("You have encountered a bug in JOSM"));
58 this.report = report;
59 textPanel = new DebugTextDisplay(report);
60 setContentPane(content);
61
62 addMessageSection();
63
64 addUpToDateSection();
65 // TODO: Notify user about plugin updates, then remove that notification that is displayed before this dialog is displayed.
66
67 addCreateTicketSection();
68
69 if (ExpertToggleAction.isExpert()) {
70 addDebugTextSection();
71 }
72
73 addIgnoreButton();
74
75 pack();
76 setModal(true);
77 setDefaultCloseOperation(DISPOSE_ON_CLOSE);
78
79 InputMapUtils.addEscapeAction(getRootPane(), new AbstractAction() {
80 @Override
81 public void actionPerformed(ActionEvent e) {
82 closeDialog();
83 }
84 });
85 }
86
87 /**
88 * The message informing the user what happened.
89 */
90 private void addMessageSection() {
91 String message = tr(
92 "An unexpected exception occurred.\n" + "This is always a coding error. If you are running the latest "
93 + "version of JOSM, please consider being kind and file a bug report.");
94 Icon icon = UIManager.getIcon("OptionPane.errorIcon");
95
96 JPanel panel = new JPanel(new GridBagLayout());
97
98 panel.add(new JLabel(icon), GBC.std().insets(0, 0, 10, 0));
99 JMultilineLabel messageLabel = new JMultilineLabel(message);
100 messageLabel.setMaxWidth(MAX_MESSAGE_SIZE);
101 panel.add(messageLabel, GBC.eol().fill());
102 content.add(panel, GBC.eop().fill(GBC.HORIZONTAL).insets(20));
103 }
104
105 private void addDebugTextSection() {
106 JPanel panel = new JPanel(new GridBagLayout());
107 addBorder(panel, tr("Debug information"));
108 panel.add(textPanel, GBC.eop().fill());
109
110 panel.add(new JLabel(tr("Manually report at:")), GBC.std());
111 panel.add(new UrlLabel(Main.getJOSMWebsite() + "/newticket"), GBC.std().fill(GBC.HORIZONTAL));
112 JButton copy = new JButton("Copy to clipboard");
113 copy.addActionListener(e -> textPanel.copyToClipboard());
114 panel.add(copy, GBC.eol().anchor(GBC.EAST));
115 content.add(panel, GBC.eop().fill());
116 }
117
118 private void addUpToDateSection() {
119 JPanel panel = new JosmUpdatePanel();
120 addBorder(panel, tr("Is JOSM up to date?"));
121 content.add(panel, GBC.eop().fill(GBC.HORIZONTAL));
122 }
123
124 private void addCreateTicketSection() {
125 JPanel panel = new JPanel(new GridBagLayout());
126 addBorder(panel, tr("Send bug report"));
127
128 JMultilineLabel helpText = new JMultilineLabel(
129 tr("If you are running the latest version of JOSM and the plugins, "
130 + "please file a bug report in our bugtracker.\n"
131 + "There the error information should already be "
132 + "filled in for you. Please include information on how to reproduce "
133 + "the error and try to supply as much detail as possible."));
134 helpText.setMaxWidth(MAX_MESSAGE_SIZE);
135 panel.add(helpText, GBC.eop().fill(GridBagConstraints.HORIZONTAL));
136
137 if (ExpertToggleAction.isExpert()) {
138 // The default settings should be fine in most situations.
139 panel.add(new BugReportSettingsPanel(report), GBC.eop().fill(GBC.HORIZONTAL));
140 }
141
142 JButton sendBugReportButton = new JButton(tr("Report Bug"), ImageProvider.get("bug"));
143 sendBugReportButton.addActionListener(e -> sendBug());
144 panel.add(sendBugReportButton, GBC.eop().anchor(GBC.EAST));
145 content.add(panel, GBC.eop().fill(GBC.HORIZONTAL));
146 }
147
148 private static void addBorder(JPanel panel, String title) {
149 panel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder(title), BorderFactory
150 .createEmptyBorder(10, 10, 10, 10)));
151 }
152
153 private void addIgnoreButton() {
154 JPanel panel = new JPanel(new GridBagLayout());
155 cbSuppressSingle = new JCheckBox(tr("Suppress this error for this session."));
156 cbSuppressSingle.setVisible(false);
157 panel.add(cbSuppressSingle, GBC.std(0, 0).fill(GBC.HORIZONTAL));
158 cbSuppressAll = new JCheckBox(tr("Suppress further error dialogs for this session."));
159 cbSuppressAll.setVisible(false);
160 panel.add(cbSuppressAll, GBC.std(0, 1).fill(GBC.HORIZONTAL));
161 JButton ignore = new JButton(tr("Ignore this error."));
162 ignore.addActionListener(e -> closeDialog());
163 panel.add(ignore, GBC.std(1, 0).span(1, 2).anchor(GBC.CENTER));
164 content.add(panel, GBC.eol().fill(GBC.HORIZONTAL).insets(20));
165 }
166
167 /**
168 * Shows or hides the suppress errors button
169 * @param showSuppress <code>true</code> to show the suppress errors checkbox.
170 */
171 public void setShowSuppress(boolean showSuppress) {
172 cbSuppressSingle.setVisible(showSuppress);
173 pack();
174 }
175
176 /**
177 * Shows or hides the suppress all errors button
178 * @param showSuppress <code>true</code> to show the suppress errors checkbox.
179 * @since 10819
180 */
181 public void setShowSuppressAll(boolean showSuppress) {
182 cbSuppressAll.setVisible(showSuppress);
183 pack();
184 }
185
186 /**
187 * Check if the checkbox to suppress further errors was selected
188 * @return <code>true</code> if the user wishes to suppress errors.
189 */
190 public SuppressionMode shouldSuppressFurtherErrors() {
191 if (cbSuppressAll.isSelected()) {
192 return SuppressionMode.ALL;
193 } else if (cbSuppressSingle.isSelected()) {
194 return SuppressionMode.SAME;
195 } else {
196 return SuppressionMode.NONE;
197 }
198 }
199
200 private void closeDialog() {
201 setVisible(false);
202 }
203
204 private void sendBug() {
205 BugReportSender.reportBug(textPanel.getCodeText());
206 }
207
208 /**
209 * A safe way to find a matching parent frame.
210 * @return The parent frame.
211 */
212 private static Frame findParent() {
213 Component current = Main.parent;
214 try {
215 // avoid cycles/invalid hirarchies
216 for (int i = 0; i < 20 && current != null; i++) {
217 if (current instanceof Frame) {
218 return (Frame) current;
219 }
220 current = current.getParent();
221 }
222 } catch (RuntimeException e) {
223 BugReport.intercept(e).put("current", current).warn();
224 }
225 return null;
226 }
227
228 /**
229 * Show the bug report for a given exception
230 * @param e The exception to display
231 * @param exceptionCounter A counter of how many exceptions have already been worked on
232 * @return The new suppression status
233 * @since 10819
234 */
235 public static SuppressionMode showFor(ReportedException e, int exceptionCounter) {
236 if (e.isOutOfMemory()) {
237 // do not translate the string, as translation may raise an exception
238 JOptionPane.showMessageDialog(Main.parent, "JOSM is out of memory. " +
239 "Strange things may happen.\nPlease restart JOSM with the -Xmx###M option,\n" +
240 "where ### is the number of MB assigned to JOSM (e.g. 256).\n" +
241 "Currently, " + Runtime.getRuntime().maxMemory()/1024/1024 + " MB are available to JOSM.",
242 "Error",
243 JOptionPane.ERROR_MESSAGE
244 );
245 return SuppressionMode.NONE;
246 } else {
247 return GuiHelper.runInEDTAndWaitAndReturn(() -> {
248 PluginDownloadTask downloadTask = PluginHandler.updateOrdisablePluginAfterException(e);
249 if (downloadTask != null) {
250 // Ask for restart to install new plugin
251 PluginPreference.notifyDownloadResults(
252 Main.parent, downloadTask, !downloadTask.getDownloadedPlugins().isEmpty());
253 return SuppressionMode.NONE;
254 }
255
256 BugReport report = new BugReport(e);
257 BugReportDialog dialog = new BugReportDialog(report);
258 dialog.setShowSuppress(exceptionCounter > 0);
259 dialog.setShowSuppressAll(exceptionCounter > 1);
260 dialog.setVisible(true);
261 return dialog.shouldSuppressFurtherErrors();
262 });
263 }
264 }
265}
Note: See TracBrowser for help on using the repository browser.