source: josm/trunk/src/org/openstreetmap/josm/gui/PleaseWaitRunnable.java@ 2980

Last change on this file since 2980 was 2980, checked in by Gubaer, 14 years ago

fixed #4269: Exception does not lead to bug report window

  • Property svn:eol-style set to native
File size: 5.7 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.gui;
3
4import java.awt.Component;
5import java.awt.EventQueue;
6import java.io.IOException;
7import java.util.logging.Logger;
8
9import javax.swing.SwingUtilities;
10
11import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor;
12import org.openstreetmap.josm.gui.progress.ProgressMonitor;
13import org.openstreetmap.josm.gui.progress.ProgressMonitor.CancelListener;
14import org.openstreetmap.josm.io.OsmTransferException;
15import org.openstreetmap.josm.tools.BugReportExceptionHandler;
16import org.openstreetmap.josm.tools.CheckParameterUtil;
17import org.xml.sax.SAXException;
18
19/**
20 * Instanced of this thread will display a "Please Wait" message in middle of JOSM
21 * to indicate a progress being executed.
22 *
23 * @author Imi
24 */
25public abstract class PleaseWaitRunnable implements Runnable, CancelListener {
26 private final static Logger logger = Logger.getLogger(PleaseWaitRunnable.class.getName());
27
28 private boolean cancelled = false;
29 private boolean ignoreException;
30 private final String title;
31
32 protected final ProgressMonitor progressMonitor;
33
34 /**
35 * Create the runnable object with a given message for the user.
36 */
37 public PleaseWaitRunnable(String title) {
38 this(title, false);
39 }
40
41 /**
42 * Create the runnable object with a given message for the user.
43 *
44 * @param title message for the user
45 * @param ignoreException If true, exception will be propagated to calling code. If false then
46 * exception will be thrown directly in EDT. When this runnable is executed using executor framework
47 * then use false unless you read result of task (because exception will get lost if you don't)
48 */
49 public PleaseWaitRunnable(String title, boolean ignoreException) {
50 this(title, new PleaseWaitProgressMonitor(title), ignoreException);
51 }
52
53 /**
54 * Create the runnable object with a given message for the user
55 *
56 * @param parent the parent component for the please wait dialog. Must not be null.
57 * @param title message for the user
58 * @param ignoreException If true, exception will be propagated to calling code. If false then
59 * exception will be thrown directly in EDT. When this runnable is executed using executor framework
60 * then use false unless you read result of task (because exception will get lost if you don't)
61 * @throws IllegalArgumentException thrown if parent is null
62 */
63 public PleaseWaitRunnable(Component parent, String title, boolean ignoreException) throws IllegalArgumentException{
64 CheckParameterUtil.ensureParameterNotNull(parent, "parent");
65 this.title = title;
66 this.progressMonitor = new PleaseWaitProgressMonitor(parent, title);
67 this.ignoreException = ignoreException;
68 }
69
70 public PleaseWaitRunnable(String title, ProgressMonitor progressMonitor, boolean ignoreException) {
71 this.title = title;
72 this.progressMonitor = progressMonitor == null?new PleaseWaitProgressMonitor(title):progressMonitor;
73 this.ignoreException = ignoreException;
74 }
75
76 private void doRealRun() {
77 try {
78 try {
79 progressMonitor.addCancelListener(this);
80 progressMonitor.beginTask(title);
81 try {
82 realRun();
83 } finally {
84 if (EventQueue.isDispatchThread()) {
85 finish();
86 } else {
87 EventQueue.invokeAndWait(new Runnable() {
88 public void run() {
89 finish();
90 }
91 });
92 }
93 }
94 } finally {
95 progressMonitor.finishTask();
96 progressMonitor.removeCancelListener(this);
97 if (progressMonitor instanceof PleaseWaitProgressMonitor) {
98 ((PleaseWaitProgressMonitor)progressMonitor).close();
99 }
100 }
101 } catch (final Exception e) {
102 if (!ignoreException) {
103 // Exception has to thrown in EDT to be shown to user
104 SwingUtilities.invokeLater(new Runnable() {
105 public void run() {
106 if (e instanceof RuntimeException) {
107 BugReportExceptionHandler.handleException(e);
108 } else {
109 ExceptionDialogUtil.explainException(e);
110 }
111 }
112 });
113 }
114 }
115 }
116
117 public final void run() {
118 if (cancelled)
119 return; // since realRun isn't executed, do not call to finish
120
121 if (EventQueue.isDispatchThread()) {
122 new Thread(new Runnable() {
123 public void run() {
124 doRealRun();
125 }
126 }).start();
127 } else {
128 doRealRun();
129 }
130 }
131
132 public void operationCanceled() {
133 cancel();
134 }
135
136 /**
137 * User pressed cancel button.
138 */
139 protected abstract void cancel();
140
141 /**
142 * Called in the worker thread to do the actual work. When any of the
143 * exception is thrown, a message box will be displayed and closeDialog
144 * is called. finish() is called in any case.
145 */
146 protected abstract void realRun() throws SAXException, IOException, OsmTransferException;
147
148 /**
149 * Finish up the data work. Is guaranteed to be called if realRun is called.
150 * Finish is called in the gui thread just after the dialog disappeared.
151 */
152 protected abstract void finish();
153
154 public ProgressMonitor getProgressMonitor() {
155 return progressMonitor;
156 }
157}
Note: See TracBrowser for help on using the repository browser.