| 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
|---|
| 2 | package org.openstreetmap.josm.gui;
|
|---|
| 3 |
|
|---|
| 4 | import java.awt.EventQueue;
|
|---|
| 5 | import java.io.IOException;
|
|---|
| 6 | import java.util.logging.Logger;
|
|---|
| 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.xml.sax.SAXException;
|
|---|
| 15 |
|
|---|
| 16 | /**
|
|---|
| 17 | * Instanced of this thread will display a "Please Wait" message in middle of JOSM
|
|---|
| 18 | * to indicate a progress being executed.
|
|---|
| 19 | *
|
|---|
| 20 | * @author Imi
|
|---|
| 21 | */
|
|---|
| 22 | public abstract class PleaseWaitRunnable implements Runnable, CancelListener {
|
|---|
| 23 | private final static Logger logger = Logger.getLogger(PleaseWaitRunnable.class.getName());
|
|---|
| 24 |
|
|---|
| 25 | private boolean cancelled = 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 | public PleaseWaitRunnable(String title, ProgressMonitor progressMonitor, boolean ignoreException) {
|
|---|
| 51 | this.title = title;
|
|---|
| 52 | this.progressMonitor = progressMonitor == null?new PleaseWaitProgressMonitor(title):progressMonitor;
|
|---|
| 53 | this.ignoreException = ignoreException;
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | private void doRealRun() {
|
|---|
| 57 | try {
|
|---|
| 58 | try {
|
|---|
| 59 | progressMonitor.addCancelListener(this);
|
|---|
| 60 | progressMonitor.beginTask(title);
|
|---|
| 61 | try {
|
|---|
| 62 | realRun();
|
|---|
| 63 | } finally {
|
|---|
| 64 | if (EventQueue.isDispatchThread()) {
|
|---|
| 65 | finish();
|
|---|
| 66 | } else {
|
|---|
| 67 | EventQueue.invokeAndWait(new Runnable() {
|
|---|
| 68 | public void run() {
|
|---|
| 69 | finish();
|
|---|
| 70 | }
|
|---|
| 71 | });
|
|---|
| 72 | }
|
|---|
| 73 | }
|
|---|
| 74 | } finally {
|
|---|
| 75 | progressMonitor.finishTask();
|
|---|
| 76 | progressMonitor.removeCancelListener(this);
|
|---|
| 77 | if (progressMonitor instanceof PleaseWaitProgressMonitor) {
|
|---|
| 78 | ((PleaseWaitProgressMonitor)progressMonitor).close();
|
|---|
| 79 | }
|
|---|
| 80 | }
|
|---|
| 81 | } catch (final Exception e) {
|
|---|
| 82 | if (!ignoreException) {
|
|---|
| 83 | // Exception has to thrown in EDT to be shown to user
|
|---|
| 84 | SwingUtilities.invokeLater(new Runnable() {
|
|---|
| 85 | public void run() {
|
|---|
| 86 | ExceptionDialogUtil.explainException(e);
|
|---|
| 87 | }
|
|---|
| 88 | });
|
|---|
| 89 | }
|
|---|
| 90 | }
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | public final void run() {
|
|---|
| 94 | if (cancelled)
|
|---|
| 95 | return; // since realRun isn't executed, do not call to finish
|
|---|
| 96 |
|
|---|
| 97 | if (EventQueue.isDispatchThread()) {
|
|---|
| 98 | new Thread(new Runnable() {
|
|---|
| 99 | public void run() {
|
|---|
| 100 | doRealRun();
|
|---|
| 101 | }
|
|---|
| 102 | }).start();
|
|---|
| 103 | } else {
|
|---|
| 104 | doRealRun();
|
|---|
| 105 | }
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | public void operationCanceled() {
|
|---|
| 109 | cancel();
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | /**
|
|---|
| 113 | * User pressed cancel button.
|
|---|
| 114 | */
|
|---|
| 115 | protected abstract void cancel();
|
|---|
| 116 |
|
|---|
| 117 | /**
|
|---|
| 118 | * Called in the worker thread to do the actual work. When any of the
|
|---|
| 119 | * exception is thrown, a message box will be displayed and closeDialog
|
|---|
| 120 | * is called. finish() is called in any case.
|
|---|
| 121 | */
|
|---|
| 122 | protected abstract void realRun() throws SAXException, IOException, OsmTransferException;
|
|---|
| 123 |
|
|---|
| 124 | /**
|
|---|
| 125 | * Finish up the data work. Is guaranteed to be called if realRun is called.
|
|---|
| 126 | * Finish is called in the gui thread just after the dialog disappeared.
|
|---|
| 127 | */
|
|---|
| 128 | protected abstract void finish();
|
|---|
| 129 |
|
|---|
| 130 | public ProgressMonitor getProgressMonitor() {
|
|---|
| 131 | return progressMonitor;
|
|---|
| 132 | }
|
|---|
| 133 | }
|
|---|