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

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

new: Changeset Cache Manager for querying, downloading, browsing, and managing changesets within JOSM. See also Changeset Manager and Changeset Query Dialog

  • Property svn:eol-style set to native
File size: 5.4 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.CheckParameterUtil;
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 private final static Logger logger = Logger.getLogger(PleaseWaitRunnable.class.getName());
26
27 private boolean cancelled = false;
28 private boolean ignoreException;
29 private final String title;
30
31 protected final ProgressMonitor progressMonitor;
32
33 /**
34 * Create the runnable object with a given message for the user.
35 */
36 public PleaseWaitRunnable(String title) {
37 this(title, false);
38 }
39
40 /**
41 * Create the runnable object with a given message for the user.
42 *
43 * @param title message for the user
44 * @param ignoreException If true, exception will be propagated to calling code. If false then
45 * exception will be thrown directly in EDT. When this runnable is executed using executor framework
46 * then use false unless you read result of task (because exception will get lost if you don't)
47 */
48 public PleaseWaitRunnable(String title, boolean ignoreException) {
49 this(title, new PleaseWaitProgressMonitor(title), ignoreException);
50 }
51
52 /**
53 * Create the runnable object with a given message for the user
54 *
55 * @param parent the parent component for the please wait dialog. Must not be null.
56 * @param title message for the user
57 * @param ignoreException If true, exception will be propagated to calling code. If false then
58 * exception will be thrown directly in EDT. When this runnable is executed using executor framework
59 * then use false unless you read result of task (because exception will get lost if you don't)
60 * @throws IllegalArgumentException thrown if parent is null
61 */
62 public PleaseWaitRunnable(Component parent, String title, boolean ignoreException) throws IllegalArgumentException{
63 CheckParameterUtil.ensureParameterNotNull(parent, "parent");
64 this.title = title;
65 this.progressMonitor = new PleaseWaitProgressMonitor(parent, title);
66 this.ignoreException = ignoreException;
67 }
68
69 public PleaseWaitRunnable(String title, ProgressMonitor progressMonitor, boolean ignoreException) {
70 this.title = title;
71 this.progressMonitor = progressMonitor == null?new PleaseWaitProgressMonitor(title):progressMonitor;
72 this.ignoreException = ignoreException;
73 }
74
75 private void doRealRun() {
76 try {
77 try {
78 progressMonitor.addCancelListener(this);
79 progressMonitor.beginTask(title);
80 try {
81 realRun();
82 } finally {
83 if (EventQueue.isDispatchThread()) {
84 finish();
85 } else {
86 EventQueue.invokeAndWait(new Runnable() {
87 public void run() {
88 finish();
89 }
90 });
91 }
92 }
93 } finally {
94 progressMonitor.finishTask();
95 progressMonitor.removeCancelListener(this);
96 if (progressMonitor instanceof PleaseWaitProgressMonitor) {
97 ((PleaseWaitProgressMonitor)progressMonitor).close();
98 }
99 }
100 } catch (final Exception e) {
101 if (!ignoreException) {
102 // Exception has to thrown in EDT to be shown to user
103 SwingUtilities.invokeLater(new Runnable() {
104 public void run() {
105 ExceptionDialogUtil.explainException(e);
106 }
107 });
108 }
109 }
110 }
111
112 public final void run() {
113 if (cancelled)
114 return; // since realRun isn't executed, do not call to finish
115
116 if (EventQueue.isDispatchThread()) {
117 new Thread(new Runnable() {
118 public void run() {
119 doRealRun();
120 }
121 }).start();
122 } else {
123 doRealRun();
124 }
125 }
126
127 public void operationCanceled() {
128 cancel();
129 }
130
131 /**
132 * User pressed cancel button.
133 */
134 protected abstract void cancel();
135
136 /**
137 * Called in the worker thread to do the actual work. When any of the
138 * exception is thrown, a message box will be displayed and closeDialog
139 * is called. finish() is called in any case.
140 */
141 protected abstract void realRun() throws SAXException, IOException, OsmTransferException;
142
143 /**
144 * Finish up the data work. Is guaranteed to be called if realRun is called.
145 * Finish is called in the gui thread just after the dialog disappeared.
146 */
147 protected abstract void finish();
148
149 public ProgressMonitor getProgressMonitor() {
150 return progressMonitor;
151 }
152}
Note: See TracBrowser for help on using the repository browser.