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