1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.gui;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.EventQueue;
|
---|
7 | import java.io.FileNotFoundException;
|
---|
8 | import java.io.IOException;
|
---|
9 |
|
---|
10 | import javax.swing.SwingUtilities;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor;
|
---|
13 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
14 | import org.openstreetmap.josm.gui.progress.ProgressMonitor.CancelListener;
|
---|
15 | import org.openstreetmap.josm.io.OsmTransferException;
|
---|
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 |
|
---|
26 | private boolean cancelled = false;
|
---|
27 | private boolean ignoreException;
|
---|
28 | private final String title;
|
---|
29 |
|
---|
30 | protected final ProgressMonitor progressMonitor;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Create the runnable object with a given message for the user.
|
---|
34 | */
|
---|
35 | public PleaseWaitRunnable(String title) {
|
---|
36 | this(title, false);
|
---|
37 | }
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Create the runnable object with a given message for the user.
|
---|
41 | * @param title Message for user
|
---|
42 | * @param ignoreException If true, exception will be propaged 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(), ignoreException);
|
---|
48 | }
|
---|
49 |
|
---|
50 | public PleaseWaitRunnable(String title, ProgressMonitor progressMonitor, boolean ignoreException) {
|
---|
51 | this.title = title;
|
---|
52 | this.progressMonitor = progressMonitor == null?new PleaseWaitProgressMonitor():progressMonitor;
|
---|
53 | this.ignoreException = ignoreException;
|
---|
54 | this.progressMonitor.addCancelListener(this);
|
---|
55 | }
|
---|
56 |
|
---|
57 | private void doRealRun() {
|
---|
58 | try {
|
---|
59 | try {
|
---|
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 | } catch (SAXException x) {
|
---|
75 | x.printStackTrace();
|
---|
76 | progressMonitor.setErrorMessage(tr("Error while parsing")+": "+x.getMessage());
|
---|
77 | } catch (FileNotFoundException x) {
|
---|
78 | x.printStackTrace();
|
---|
79 | progressMonitor.setErrorMessage(tr("File not found")+": "+x.getMessage());
|
---|
80 | } catch (IOException x) {
|
---|
81 | x.printStackTrace();
|
---|
82 | progressMonitor.setErrorMessage(x.getMessage());
|
---|
83 | } catch(OsmTransferException x) {
|
---|
84 | x.printStackTrace();
|
---|
85 | if (x.getCause() != null) {
|
---|
86 | progressMonitor.setErrorMessage(x.getCause().getMessage());
|
---|
87 | } else {
|
---|
88 | progressMonitor.setErrorMessage(x.getMessage());
|
---|
89 | }
|
---|
90 | } finally {
|
---|
91 | progressMonitor.finishTask();
|
---|
92 | }
|
---|
93 | } catch (final Throwable e) {
|
---|
94 | if (!ignoreException) {
|
---|
95 | // Exception has to thrown in EDT to be shown to user
|
---|
96 | SwingUtilities.invokeLater(new Runnable() {
|
---|
97 | public void run() {
|
---|
98 | throw new RuntimeException(e);
|
---|
99 | }
|
---|
100 | });
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | public final void run() {
|
---|
106 | if (cancelled)
|
---|
107 | return; // since realRun isn't executed, do not call to finish
|
---|
108 |
|
---|
109 | if (EventQueue.isDispatchThread()) {
|
---|
110 | new Thread(new Runnable() {
|
---|
111 | public void run() {
|
---|
112 | doRealRun();
|
---|
113 | }
|
---|
114 | }).start();
|
---|
115 | } else {
|
---|
116 | doRealRun();
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | public void operationCanceled() {
|
---|
121 | cancel();
|
---|
122 | }
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * User pressed cancel button.
|
---|
126 | */
|
---|
127 | protected abstract void cancel();
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Called in the worker thread to do the actual work. When any of the
|
---|
131 | * exception is thrown, a message box will be displayed and closeDialog
|
---|
132 | * is called. finish() is called in any case.
|
---|
133 | */
|
---|
134 | protected abstract void realRun() throws SAXException, IOException, OsmTransferException;
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * Finish up the data work. Is guaranteed to be called if realRun is called.
|
---|
138 | * Finish is called in the gui thread just after the dialog disappeared.
|
---|
139 | */
|
---|
140 | protected abstract void finish();
|
---|
141 |
|
---|
142 | public ProgressMonitor getProgressMonitor() {
|
---|
143 | return progressMonitor;
|
---|
144 | }
|
---|
145 | }
|
---|