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

Last change on this file since 2990 was 2990, checked in by jttt, 14 years ago

Fix some eclipse warnings

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