1 | package org.openstreetmap.josm.gui;
|
---|
2 |
|
---|
3 | import java.awt.EventQueue;
|
---|
4 | import java.awt.GridBagLayout;
|
---|
5 | import java.awt.event.ActionEvent;
|
---|
6 | import java.awt.event.ActionListener;
|
---|
7 | import java.awt.event.WindowAdapter;
|
---|
8 | import java.awt.event.WindowEvent;
|
---|
9 | import java.io.FileNotFoundException;
|
---|
10 | import java.io.IOException;
|
---|
11 |
|
---|
12 | import javax.swing.BorderFactory;
|
---|
13 | import javax.swing.BoundedRangeModel;
|
---|
14 | import javax.swing.JButton;
|
---|
15 | import javax.swing.JDialog;
|
---|
16 | import javax.swing.JLabel;
|
---|
17 | import javax.swing.JOptionPane;
|
---|
18 | import javax.swing.JPanel;
|
---|
19 | import javax.swing.JProgressBar;
|
---|
20 |
|
---|
21 | import org.jdom.JDOMException;
|
---|
22 | import org.openstreetmap.josm.Main;
|
---|
23 | import org.openstreetmap.josm.tools.GBC;
|
---|
24 | import org.xml.sax.SAXException;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Instanced of this thread will display a "Please Wait" message in middle of JOSM
|
---|
28 | * to indicate a progress beeing executed.
|
---|
29 | *
|
---|
30 | * @author Imi
|
---|
31 | */
|
---|
32 | public abstract class PleaseWaitRunnable implements Runnable {
|
---|
33 | public final JDialog pleaseWaitDlg;
|
---|
34 | public String errorMessage;
|
---|
35 |
|
---|
36 | private final JProgressBar progressBar = new JProgressBar();
|
---|
37 | private boolean closeDialogCalled = false;
|
---|
38 |
|
---|
39 | protected final JLabel currentAction = new JLabel("Contact OSM server...");
|
---|
40 | protected final BoundedRangeModel progress = progressBar.getModel();
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Create the runnable object with a given message for the user.
|
---|
44 | */
|
---|
45 | public PleaseWaitRunnable(String msg) {
|
---|
46 | pleaseWaitDlg = new JDialog(JOptionPane.getFrameForComponent(Main.parent), msg, true);
|
---|
47 | pleaseWaitDlg.setLayout(new GridBagLayout());
|
---|
48 | JPanel pane = new JPanel(new GridBagLayout());
|
---|
49 | pane.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
|
---|
50 | pane.add(currentAction, GBC.eol().fill(GBC.HORIZONTAL));
|
---|
51 | pane.add(progressBar, GBC.eop().fill(GBC.HORIZONTAL));
|
---|
52 | JButton cancel = new JButton("Cancel");
|
---|
53 | pane.add(cancel, GBC.eol().anchor(GBC.CENTER));
|
---|
54 | pleaseWaitDlg.setContentPane(pane);
|
---|
55 | pleaseWaitDlg.setSize(350,100);
|
---|
56 | pleaseWaitDlg.setLocationRelativeTo(Main.parent);
|
---|
57 |
|
---|
58 | cancel.addActionListener(new ActionListener(){
|
---|
59 | public void actionPerformed(ActionEvent e) {
|
---|
60 | cancel();
|
---|
61 | }
|
---|
62 | });
|
---|
63 | pleaseWaitDlg.addWindowListener(new WindowAdapter(){
|
---|
64 | @Override public void windowClosing(WindowEvent e) {
|
---|
65 | if (!closeDialogCalled) {
|
---|
66 | cancel();
|
---|
67 | closeDialog();
|
---|
68 | }
|
---|
69 | }
|
---|
70 | });
|
---|
71 | }
|
---|
72 |
|
---|
73 | public final void run() {
|
---|
74 | try {
|
---|
75 | realRun();
|
---|
76 | } catch (SAXException x) {
|
---|
77 | x.printStackTrace();
|
---|
78 | errorMessage = "Error while parsing: "+x.getMessage();
|
---|
79 | } catch (JDOMException x) {
|
---|
80 | x.printStackTrace();
|
---|
81 | errorMessage = "Error while parsing: "+x.getMessage();
|
---|
82 | } catch (FileNotFoundException x) {
|
---|
83 | x.printStackTrace();
|
---|
84 | errorMessage = "URL not found: " + x.getMessage();
|
---|
85 | } catch (IOException x) {
|
---|
86 | x.printStackTrace();
|
---|
87 | errorMessage = x.getMessage();
|
---|
88 | } finally {
|
---|
89 | closeDialog();
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * User pressed cancel button.
|
---|
95 | */
|
---|
96 | protected abstract void cancel();
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Called in the worker thread to do the actual work. When any of the
|
---|
100 | * exception is thrown, a message box will be displayed and closeDialog
|
---|
101 | * is called. finish() is called in any case.
|
---|
102 | */
|
---|
103 | protected abstract void realRun() throws SAXException, JDOMException, IOException;
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Finish up the data work. Is guaranteed to be called if realRun is called.
|
---|
107 | * Finish is called in the gui thread just after the dialog disappeared.
|
---|
108 | */
|
---|
109 | protected abstract void finish();
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Close the dialog. Usually called from worker thread.
|
---|
113 | */
|
---|
114 | public void closeDialog() {
|
---|
115 | if (closeDialogCalled)
|
---|
116 | return;
|
---|
117 | closeDialogCalled = true;
|
---|
118 | EventQueue.invokeLater(new Runnable(){
|
---|
119 | public void run() {
|
---|
120 | finish();
|
---|
121 | pleaseWaitDlg.setVisible(false);
|
---|
122 | pleaseWaitDlg.dispose();
|
---|
123 | if (errorMessage != null)
|
---|
124 | JOptionPane.showMessageDialog(Main.parent, errorMessage);
|
---|
125 | }
|
---|
126 | });
|
---|
127 | }
|
---|
128 | }
|
---|