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 |
|
---|
14 | import javax.swing.JOptionPane;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.Main;
|
---|
17 | import org.openstreetmap.josm.gui.PleaseWaitDialog;
|
---|
18 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
19 |
|
---|
20 |
|
---|
21 | public class PleaseWaitProgressMonitor extends AbstractProgressMonitor {
|
---|
22 |
|
---|
23 | private static final int PROGRESS_BAR_MAX = 100;
|
---|
24 | private final Window dialogParent;
|
---|
25 | private int currentProgressValue = 0;
|
---|
26 |
|
---|
27 | private PleaseWaitDialog dialog;
|
---|
28 | private String windowTitle;
|
---|
29 |
|
---|
30 | public PleaseWaitProgressMonitor() {
|
---|
31 | this("");
|
---|
32 | }
|
---|
33 |
|
---|
34 | public PleaseWaitProgressMonitor(String windowTitle) {
|
---|
35 | this(JOptionPane.getFrameForComponent(Main.parent));
|
---|
36 | this.windowTitle = windowTitle;
|
---|
37 | }
|
---|
38 |
|
---|
39 | public PleaseWaitProgressMonitor(Window dialogParent) {
|
---|
40 | super(new CancelHandler());
|
---|
41 | this.dialogParent = dialogParent;
|
---|
42 | }
|
---|
43 |
|
---|
44 | private ActionListener cancelListener = new ActionListener(){
|
---|
45 | public void actionPerformed(ActionEvent e) {
|
---|
46 | cancel();
|
---|
47 | }
|
---|
48 | };
|
---|
49 |
|
---|
50 | private WindowListener windowListener = new WindowAdapter(){
|
---|
51 | @Override public void windowClosing(WindowEvent e) {
|
---|
52 | cancel();
|
---|
53 | }
|
---|
54 | };
|
---|
55 |
|
---|
56 | private void doInEDT(Runnable runnable) {
|
---|
57 | EventQueue.invokeLater(runnable);
|
---|
58 | }
|
---|
59 |
|
---|
60 | @Override
|
---|
61 | public void doBeginTask() {
|
---|
62 | doInEDT(new Runnable() {
|
---|
63 | public void run() {
|
---|
64 | if (dialogParent instanceof Frame && dialog == null) {
|
---|
65 | dialog = new PleaseWaitDialog(dialogParent);
|
---|
66 | } else if (dialogParent instanceof Dialog && dialog == null) {
|
---|
67 | dialog = new PleaseWaitDialog(dialogParent);
|
---|
68 | } else
|
---|
69 | throw new ProgressException("PleaseWaitDialog parent must be either Frame or Dialog");
|
---|
70 |
|
---|
71 | if (windowTitle != null) {
|
---|
72 | dialog.setTitle(windowTitle);
|
---|
73 | }
|
---|
74 | dialog.setCancelEnabled(true);
|
---|
75 | dialog.setCancelCallback(cancelListener);
|
---|
76 | dialog.setCustomText("");
|
---|
77 | dialog.addWindowListener(windowListener);
|
---|
78 | dialog.progress.setMaximum(PROGRESS_BAR_MAX);
|
---|
79 | dialog.setVisible(true);
|
---|
80 | }
|
---|
81 | });
|
---|
82 | }
|
---|
83 |
|
---|
84 | @Override
|
---|
85 | public void doFinishTask() {
|
---|
86 | // do nothing
|
---|
87 | }
|
---|
88 |
|
---|
89 | @Override
|
---|
90 | protected void updateProgress(double progressValue) {
|
---|
91 | final int newValue = (int)(progressValue * PROGRESS_BAR_MAX);
|
---|
92 | if (newValue != currentProgressValue) {
|
---|
93 | currentProgressValue = newValue;
|
---|
94 | doInEDT(new Runnable() {
|
---|
95 | public void run() {
|
---|
96 | dialog.progress.setValue(currentProgressValue);
|
---|
97 | }
|
---|
98 | });
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | @Override
|
---|
103 | protected void doSetCustomText(final String title) {
|
---|
104 | checkState(State.IN_TASK, State.IN_SUBTASK);
|
---|
105 | doInEDT(new Runnable() {
|
---|
106 | public void run() {
|
---|
107 | dialog.setCustomText(title);
|
---|
108 | }
|
---|
109 | });
|
---|
110 | }
|
---|
111 |
|
---|
112 | @Override
|
---|
113 | protected void doSetTitle(final String title) {
|
---|
114 | checkState(State.IN_TASK, State.IN_SUBTASK);
|
---|
115 | doInEDT(new Runnable() {
|
---|
116 | public void run() {
|
---|
117 | dialog.currentAction.setText(title);
|
---|
118 | }
|
---|
119 | });
|
---|
120 | }
|
---|
121 |
|
---|
122 | @Override
|
---|
123 | protected void doSetIntermediate(final boolean value) {
|
---|
124 | doInEDT(new Runnable() {
|
---|
125 | public void run() {
|
---|
126 | if (value && dialog.progress.getValue() == 0) {
|
---|
127 | // Enable only if progress is at the beginning. Doing intermediate progress in the middle
|
---|
128 | // will hide already reached progress
|
---|
129 | dialog.setIndeterminate(true);
|
---|
130 | } else {
|
---|
131 | dialog.setIndeterminate(false);
|
---|
132 | }
|
---|
133 | }
|
---|
134 | });
|
---|
135 | }
|
---|
136 |
|
---|
137 | @Override
|
---|
138 | protected void doSetErrorMessage(String message) {
|
---|
139 | // Do nothing
|
---|
140 | }
|
---|
141 |
|
---|
142 | @Override
|
---|
143 | public void appendLogMessage(final String message) {
|
---|
144 | doInEDT(new Runnable() {
|
---|
145 | public void run() {
|
---|
146 | dialog.appendLogMessage(message);
|
---|
147 | }
|
---|
148 | });
|
---|
149 | }
|
---|
150 |
|
---|
151 | public void close() {
|
---|
152 | dialog.setVisible(false);
|
---|
153 | dialog.setCancelCallback(null);
|
---|
154 | dialog.removeWindowListener(windowListener);
|
---|
155 | dialog.dispose();
|
---|
156 | dialog = null;
|
---|
157 | }
|
---|
158 | }
|
---|