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.awt.event.ActionEvent;
|
---|
8 | import java.awt.event.ActionListener;
|
---|
9 | import java.awt.event.WindowAdapter;
|
---|
10 | import java.awt.event.WindowEvent;
|
---|
11 | import java.io.FileNotFoundException;
|
---|
12 | import java.io.IOException;
|
---|
13 | import java.lang.reflect.InvocationTargetException;
|
---|
14 |
|
---|
15 | import javax.swing.JOptionPane;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.Main;
|
---|
18 | import org.xml.sax.SAXException;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Instanced of this thread will display a "Please Wait" message in middle of JOSM
|
---|
22 | * to indicate a progress beeing executed.
|
---|
23 | *
|
---|
24 | * @author Imi
|
---|
25 | */
|
---|
26 | public abstract class PleaseWaitRunnable implements Runnable {
|
---|
27 |
|
---|
28 | public String errorMessage;
|
---|
29 |
|
---|
30 | private boolean closeDialogCalled = false;
|
---|
31 | private boolean cancelled = false;
|
---|
32 |
|
---|
33 | private final String title;
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Create the runnable object with a given message for the user.
|
---|
37 | */
|
---|
38 | public PleaseWaitRunnable(String title) {
|
---|
39 | this.title = title;
|
---|
40 | Main.pleaseWaitDlg.cancel.addActionListener(new ActionListener(){
|
---|
41 | public void actionPerformed(ActionEvent e) {
|
---|
42 | if (!cancelled) {
|
---|
43 | cancelled = true;
|
---|
44 | cancel();
|
---|
45 | }
|
---|
46 | }
|
---|
47 | });
|
---|
48 | Main.pleaseWaitDlg.addWindowListener(new WindowAdapter(){
|
---|
49 | @Override public void windowClosing(WindowEvent e) {
|
---|
50 | if (!closeDialogCalled) {
|
---|
51 | if (!cancelled) {
|
---|
52 | cancelled = true;
|
---|
53 | cancel();
|
---|
54 | }
|
---|
55 | closeDialog();
|
---|
56 | }
|
---|
57 | }
|
---|
58 | });
|
---|
59 | }
|
---|
60 |
|
---|
61 | public final void run() {
|
---|
62 | try {
|
---|
63 | if (cancelled)
|
---|
64 | return; // since realRun isn't executed, do not call to finish
|
---|
65 |
|
---|
66 | // reset dialog state
|
---|
67 | Main.pleaseWaitDlg.setTitle(title);
|
---|
68 | errorMessage = null;
|
---|
69 | closeDialogCalled = false;
|
---|
70 |
|
---|
71 | // show the dialog
|
---|
72 | synchronized (this) {
|
---|
73 | EventQueue.invokeLater(new Runnable() {
|
---|
74 | public void run() {
|
---|
75 | synchronized (PleaseWaitRunnable.this) {
|
---|
76 | PleaseWaitRunnable.this.notifyAll();
|
---|
77 | }
|
---|
78 | Main.pleaseWaitDlg.setVisible(true);
|
---|
79 | }
|
---|
80 | });
|
---|
81 | try {wait();} catch (InterruptedException e) {}
|
---|
82 | }
|
---|
83 |
|
---|
84 | realRun();
|
---|
85 | } catch (SAXException x) {
|
---|
86 | x.printStackTrace();
|
---|
87 | errorMessage = tr("Error while parsing")+": "+x.getMessage();
|
---|
88 | } catch (FileNotFoundException x) {
|
---|
89 | x.printStackTrace();
|
---|
90 | errorMessage = tr("File not found")+": "+x.getMessage();
|
---|
91 | } catch (IOException x) {
|
---|
92 | x.printStackTrace();
|
---|
93 | errorMessage = x.getMessage();
|
---|
94 | } finally {
|
---|
95 | closeDialog();
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * User pressed cancel button.
|
---|
101 | */
|
---|
102 | protected abstract void cancel();
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Called in the worker thread to do the actual work. When any of the
|
---|
106 | * exception is thrown, a message box will be displayed and closeDialog
|
---|
107 | * is called. finish() is called in any case.
|
---|
108 | */
|
---|
109 | protected abstract void realRun() throws SAXException, IOException;
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Finish up the data work. Is guaranteed to be called if realRun is called.
|
---|
113 | * Finish is called in the gui thread just after the dialog disappeared.
|
---|
114 | */
|
---|
115 | protected abstract void finish();
|
---|
116 |
|
---|
117 | /**
|
---|
118 | * Close the dialog. Usually called from worker thread.
|
---|
119 | */
|
---|
120 | public void closeDialog() {
|
---|
121 | if (closeDialogCalled)
|
---|
122 | return;
|
---|
123 | closeDialogCalled = true;
|
---|
124 | try {
|
---|
125 | Runnable runnable = new Runnable(){
|
---|
126 | public void run() {
|
---|
127 | try {
|
---|
128 | finish();
|
---|
129 | } finally {
|
---|
130 | Main.pleaseWaitDlg.setVisible(false);
|
---|
131 | Main.pleaseWaitDlg.dispose();
|
---|
132 | }
|
---|
133 | if (errorMessage != null)
|
---|
134 | JOptionPane.showMessageDialog(Main.parent, errorMessage);
|
---|
135 | }
|
---|
136 | };
|
---|
137 |
|
---|
138 | // make sure, this is called in the dispatcher thread ASAP
|
---|
139 | if (EventQueue.isDispatchThread())
|
---|
140 | runnable.run();
|
---|
141 | else
|
---|
142 | EventQueue.invokeAndWait(runnable);
|
---|
143 |
|
---|
144 | } catch (InterruptedException e) {
|
---|
145 | } catch (InvocationTargetException e) {
|
---|
146 | throw new RuntimeException(e);
|
---|
147 | }
|
---|
148 | }
|
---|
149 | }
|
---|