1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.progress;
|
---|
3 |
|
---|
4 | import java.awt.Dialog;
|
---|
5 | import java.awt.EventQueue;
|
---|
6 | import java.awt.Frame;
|
---|
7 | import java.awt.Window;
|
---|
8 | import java.awt.event.ActionEvent;
|
---|
9 | import java.awt.event.ActionListener;
|
---|
10 | import java.awt.event.WindowAdapter;
|
---|
11 | import java.awt.event.WindowEvent;
|
---|
12 | import java.awt.event.WindowListener;
|
---|
13 | import java.lang.reflect.InvocationTargetException;
|
---|
14 |
|
---|
15 | import javax.swing.JOptionPane;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.Main;
|
---|
18 | import org.openstreetmap.josm.gui.PleaseWaitDialog;
|
---|
19 |
|
---|
20 |
|
---|
21 | public class PleaseWaitProgressMonitor extends AbstractProgressMonitor {
|
---|
22 |
|
---|
23 | private static final int PROGRESS_BAR_MAX = 100;
|
---|
24 | private final Window dialogParent;
|
---|
25 |
|
---|
26 | private PleaseWaitDialog dialog;
|
---|
27 |
|
---|
28 | public PleaseWaitProgressMonitor() {
|
---|
29 | this(JOptionPane.getFrameForComponent(Main.map));
|
---|
30 | }
|
---|
31 |
|
---|
32 | public PleaseWaitProgressMonitor(Window dialogParent) {
|
---|
33 | super(new CancelHandler());
|
---|
34 | this.dialogParent = dialogParent;
|
---|
35 | }
|
---|
36 |
|
---|
37 | private ActionListener cancelListener = new ActionListener(){
|
---|
38 | public void actionPerformed(ActionEvent e) {
|
---|
39 | cancel();
|
---|
40 | }
|
---|
41 | };
|
---|
42 |
|
---|
43 | private WindowListener windowListener = new WindowAdapter(){
|
---|
44 | @Override public void windowClosing(WindowEvent e) {
|
---|
45 | cancel();
|
---|
46 | closeDialog();
|
---|
47 | }
|
---|
48 | };
|
---|
49 |
|
---|
50 | private void closeDialog() {
|
---|
51 | try {
|
---|
52 | Runnable runnable = new Runnable(){
|
---|
53 | public void run() {
|
---|
54 | }
|
---|
55 | };
|
---|
56 |
|
---|
57 | // make sure, this is called in the dispatcher thread ASAP
|
---|
58 | if (EventQueue.isDispatchThread()) {
|
---|
59 | runnable.run();
|
---|
60 | } else {
|
---|
61 | EventQueue.invokeAndWait(runnable);
|
---|
62 | }
|
---|
63 |
|
---|
64 | } catch (InterruptedException e) {
|
---|
65 | } catch (InvocationTargetException e) {
|
---|
66 | throw new RuntimeException(e);
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | private void doInEDT(Runnable runnable) {
|
---|
71 | EventQueue.invokeLater(runnable);
|
---|
72 | }
|
---|
73 |
|
---|
74 | public void doBeginTask() {
|
---|
75 | doInEDT(new Runnable() {
|
---|
76 | public void run() {
|
---|
77 | if (dialogParent instanceof Frame) {
|
---|
78 | dialog = new PleaseWaitDialog((Frame)dialogParent);
|
---|
79 | } else if (dialogParent instanceof Dialog) {
|
---|
80 | dialog = new PleaseWaitDialog((Dialog)dialogParent);
|
---|
81 | } else {
|
---|
82 | throw new ProgressException("PleaseWaitDialog parent must be either Frame or Dialog");
|
---|
83 | }
|
---|
84 |
|
---|
85 | dialog.cancel.setEnabled(true);
|
---|
86 | dialog.setCustomText("");
|
---|
87 | dialog.cancel.addActionListener(cancelListener);
|
---|
88 | dialog.addWindowListener(windowListener);
|
---|
89 | dialog.progress.setMaximum(PROGRESS_BAR_MAX);
|
---|
90 | dialog.setVisible(true);
|
---|
91 | }
|
---|
92 | });
|
---|
93 | }
|
---|
94 |
|
---|
95 | public void doFinishTask() {
|
---|
96 | doInEDT(new Runnable() {
|
---|
97 | public void run() {
|
---|
98 | if (dialog != null) {
|
---|
99 | dialog.setVisible(false);
|
---|
100 | dialog.dispose();
|
---|
101 | dialog.removeWindowListener(windowListener);
|
---|
102 | dialog.cancel.removeActionListener(cancelListener);
|
---|
103 | if (getErrorMessage() != null) {
|
---|
104 | JOptionPane.showMessageDialog(Main.parent, getErrorMessage());
|
---|
105 | }
|
---|
106 | dialog = null;
|
---|
107 | }
|
---|
108 | }
|
---|
109 | });
|
---|
110 | }
|
---|
111 |
|
---|
112 | public void worked(int ticks) {
|
---|
113 | this.ticks += ticks;
|
---|
114 | updateProgress(0);
|
---|
115 | }
|
---|
116 |
|
---|
117 | protected void updateProgress(final double progressValue) {
|
---|
118 | doInEDT(new Runnable() {
|
---|
119 | public void run() {
|
---|
120 | dialog.progress.setValue((int)(progressValue * PROGRESS_BAR_MAX));
|
---|
121 | }
|
---|
122 | });
|
---|
123 | }
|
---|
124 |
|
---|
125 | @Override
|
---|
126 | protected void doSetCustomText(final String title) {
|
---|
127 | checkState(State.IN_TASK, State.IN_SUBTASK);
|
---|
128 | doInEDT(new Runnable() {
|
---|
129 | public void run() {
|
---|
130 | dialog.setCustomText(title);
|
---|
131 | }
|
---|
132 | });
|
---|
133 | }
|
---|
134 |
|
---|
135 | @Override
|
---|
136 | protected void doSetTitle(final String title) {
|
---|
137 | checkState(State.IN_TASK, State.IN_SUBTASK);
|
---|
138 | doInEDT(new Runnable() {
|
---|
139 | public void run() {
|
---|
140 | dialog.currentAction.setText(title);
|
---|
141 | }
|
---|
142 | });
|
---|
143 | }
|
---|
144 |
|
---|
145 | @Override
|
---|
146 | protected void doSetIntermediate(final boolean value) {
|
---|
147 | doInEDT(new Runnable() {
|
---|
148 | public void run() {
|
---|
149 | if (value && dialog.progress.getValue() == 0) {
|
---|
150 | // Enable only if progress is at the begging. Doing intermediate progress in the middle
|
---|
151 | // will hide already reached progress
|
---|
152 | dialog.setIndeterminate(true);
|
---|
153 | } else {
|
---|
154 | dialog.setIndeterminate(false);
|
---|
155 | }
|
---|
156 | }
|
---|
157 | });
|
---|
158 | }
|
---|
159 |
|
---|
160 | @Override
|
---|
161 | protected void doSetErrorMessage(String message) {
|
---|
162 | // Do nothing
|
---|
163 | }
|
---|
164 |
|
---|
165 | }
|
---|