[6380] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[283] | 2 | package org.openstreetmap.josm.gui;
|
---|
| 3 |
|
---|
[2689] | 4 | import java.awt.Component;
|
---|
[283] | 5 | import java.awt.EventQueue;
|
---|
| 6 | import java.io.IOException;
|
---|
| 7 |
|
---|
[1493] | 8 | import javax.swing.SwingUtilities;
|
---|
[283] | 9 |
|
---|
[1811] | 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;
|
---|
[4718] | 13 | import org.openstreetmap.josm.gui.progress.ProgressTaskId;
|
---|
[1670] | 14 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
[2980] | 15 | import org.openstreetmap.josm.tools.BugReportExceptionHandler;
|
---|
[2689] | 16 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
[283] | 17 | import org.xml.sax.SAXException;
|
---|
| 18 |
|
---|
| 19 | /**
|
---|
| 20 | * Instanced of this thread will display a "Please Wait" message in middle of JOSM
|
---|
[1195] | 21 | * to indicate a progress being executed.
|
---|
[283] | 22 | *
|
---|
| 23 | * @author Imi
|
---|
| 24 | */
|
---|
[1811] | 25 | public abstract class PleaseWaitRunnable implements Runnable, CancelListener {
|
---|
[4310] | 26 | private boolean canceled = false;
|
---|
[1493] | 27 | private boolean ignoreException;
|
---|
[1169] | 28 | private final String title;
|
---|
[1647] | 29 |
|
---|
[1811] | 30 | protected final ProgressMonitor progressMonitor;
|
---|
[1733] | 31 |
|
---|
[1493] | 32 | /**
|
---|
| 33 | * Create the runnable object with a given message for the user.
|
---|
[1647] | 34 | */
|
---|
[1493] | 35 | public PleaseWaitRunnable(String title) {
|
---|
| 36 | this(title, false);
|
---|
| 37 | }
|
---|
[1169] | 38 | /**
|
---|
| 39 | * Create the runnable object with a given message for the user.
|
---|
[2512] | 40 | *
|
---|
[2319] | 41 | * @param title message for the user
|
---|
| 42 | * @param ignoreException If true, exception will be propagated to calling code. If false then
|
---|
[1493] | 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)
|
---|
[1169] | 45 | */
|
---|
[1493] | 46 | public PleaseWaitRunnable(String title, boolean ignoreException) {
|
---|
[2025] | 47 | this(title, new PleaseWaitProgressMonitor(title), ignoreException);
|
---|
[1811] | 48 | }
|
---|
| 49 |
|
---|
[2689] | 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 |
|
---|
[1811] | 67 | public PleaseWaitRunnable(String title, ProgressMonitor progressMonitor, boolean ignoreException) {
|
---|
[1169] | 68 | this.title = title;
|
---|
[2025] | 69 | this.progressMonitor = progressMonitor == null?new PleaseWaitProgressMonitor(title):progressMonitor;
|
---|
[1493] | 70 | this.ignoreException = ignoreException;
|
---|
[1169] | 71 | }
|
---|
[283] | 72 |
|
---|
[1733] | 73 | private void doRealRun() {
|
---|
[1169] | 74 | try {
|
---|
[4718] | 75 | ProgressTaskId oldTaskId = null;
|
---|
[1493] | 76 | try {
|
---|
[2319] | 77 | progressMonitor.addCancelListener(this);
|
---|
[1811] | 78 | progressMonitor.beginTask(title);
|
---|
[4718] | 79 | oldTaskId = progressMonitor.getProgressTaskId();
|
---|
| 80 | progressMonitor.setProgressTaskId(canRunInBackground());
|
---|
[1811] | 81 | try {
|
---|
| 82 | realRun();
|
---|
| 83 | } finally {
|
---|
| 84 | if (EventQueue.isDispatchThread()) {
|
---|
| 85 | finish();
|
---|
| 86 | } else {
|
---|
| 87 | EventQueue.invokeAndWait(new Runnable() {
|
---|
[6084] | 88 | @Override
|
---|
[1811] | 89 | public void run() {
|
---|
| 90 | finish();
|
---|
| 91 | }
|
---|
| 92 | });
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
[1493] | 95 | } finally {
|
---|
[1811] | 96 | progressMonitor.finishTask();
|
---|
[2319] | 97 | progressMonitor.removeCancelListener(this);
|
---|
[4718] | 98 | progressMonitor.setProgressTaskId(oldTaskId);
|
---|
[2319] | 99 | if (progressMonitor instanceof PleaseWaitProgressMonitor) {
|
---|
| 100 | ((PleaseWaitProgressMonitor)progressMonitor).close();
|
---|
| 101 | }
|
---|
[4907] | 102 | if (EventQueue.isDispatchThread()) {
|
---|
| 103 | afterFinish();
|
---|
| 104 | } else {
|
---|
| 105 | EventQueue.invokeAndWait(new Runnable() {
|
---|
[6084] | 106 | @Override
|
---|
[4907] | 107 | public void run() {
|
---|
| 108 | afterFinish();
|
---|
| 109 | }
|
---|
| 110 | });
|
---|
| 111 | }
|
---|
[1493] | 112 | }
|
---|
[2322] | 113 | } catch (final Exception e) {
|
---|
[1493] | 114 | if (!ignoreException) {
|
---|
| 115 | // Exception has to thrown in EDT to be shown to user
|
---|
| 116 | SwingUtilities.invokeLater(new Runnable() {
|
---|
[6084] | 117 | @Override
|
---|
[1169] | 118 | public void run() {
|
---|
[2980] | 119 | if (e instanceof RuntimeException) {
|
---|
| 120 | BugReportExceptionHandler.handleException(e);
|
---|
| 121 | } else {
|
---|
| 122 | ExceptionDialogUtil.explainException(e);
|
---|
| 123 | }
|
---|
[1647] | 124 | }
|
---|
[1169] | 125 | });
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
[283] | 129 |
|
---|
[4718] | 130 | /**
|
---|
| 131 | * Can be overriden if something needs to run after progress monitor is closed.
|
---|
| 132 | */
|
---|
| 133 | protected void afterFinish() {
|
---|
| 134 |
|
---|
| 135 | }
|
---|
| 136 |
|
---|
[6084] | 137 | @Override
|
---|
[1733] | 138 | public final void run() {
|
---|
[4310] | 139 | if (canceled)
|
---|
[1733] | 140 | return; // since realRun isn't executed, do not call to finish
|
---|
| 141 |
|
---|
| 142 | if (EventQueue.isDispatchThread()) {
|
---|
| 143 | new Thread(new Runnable() {
|
---|
[6084] | 144 | @Override
|
---|
[1733] | 145 | public void run() {
|
---|
| 146 | doRealRun();
|
---|
| 147 | }
|
---|
| 148 | }).start();
|
---|
| 149 | } else {
|
---|
| 150 | doRealRun();
|
---|
| 151 | }
|
---|
| 152 | }
|
---|
| 153 |
|
---|
[6084] | 154 | @Override
|
---|
[1811] | 155 | public void operationCanceled() {
|
---|
| 156 | cancel();
|
---|
| 157 | }
|
---|
| 158 |
|
---|
[1169] | 159 | /**
|
---|
| 160 | * User pressed cancel button.
|
---|
| 161 | */
|
---|
| 162 | protected abstract void cancel();
|
---|
[283] | 163 |
|
---|
[1169] | 164 | /**
|
---|
| 165 | * Called in the worker thread to do the actual work. When any of the
|
---|
| 166 | * exception is thrown, a message box will be displayed and closeDialog
|
---|
| 167 | * is called. finish() is called in any case.
|
---|
| 168 | */
|
---|
[1670] | 169 | protected abstract void realRun() throws SAXException, IOException, OsmTransferException;
|
---|
[283] | 170 |
|
---|
[1169] | 171 | /**
|
---|
| 172 | * Finish up the data work. Is guaranteed to be called if realRun is called.
|
---|
| 173 | * Finish is called in the gui thread just after the dialog disappeared.
|
---|
| 174 | */
|
---|
| 175 | protected abstract void finish();
|
---|
[283] | 176 |
|
---|
[1811] | 177 | public ProgressMonitor getProgressMonitor() {
|
---|
| 178 | return progressMonitor;
|
---|
[1169] | 179 | }
|
---|
[4718] | 180 |
|
---|
| 181 | /**
|
---|
[6830] | 182 | * Task can run in background if returned value != null. Note that it's tasks responsibility
|
---|
[4718] | 183 | * to ensure proper synchronization, PleaseWaitRunnable doesn't with it.
|
---|
[6830] | 184 | * @return If returned value is != null then task can run in background. TaskId could be used in future for "Always run in background" checkbox
|
---|
[4718] | 185 | */
|
---|
| 186 | public ProgressTaskId canRunInBackground() {
|
---|
| 187 | return null;
|
---|
| 188 | }
|
---|
[283] | 189 | }
|
---|