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

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

Added canceling of DownloadOsmTaskLists
Removed error remembering in the progress dialog

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