source: josm/trunk/src/org/openstreetmap/josm/gui/progress/PleaseWaitProgressMonitor.java@ 8510

Last change on this file since 8510 was 8510, checked in by Don-vip, 9 years ago

checkstyle: enable relevant whitespace checks and fix them

  • Property svn:eol-style set to native
File size: 10.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.progress;
3
4import java.awt.Component;
5import java.awt.event.ActionEvent;
6import java.awt.event.ActionListener;
7import java.awt.event.WindowAdapter;
8import java.awt.event.WindowEvent;
9import java.awt.event.WindowListener;
10
11import javax.swing.JOptionPane;
12import javax.swing.SwingUtilities;
13
14import org.openstreetmap.josm.Main;
15import org.openstreetmap.josm.gui.MapFrame;
16import org.openstreetmap.josm.gui.MapStatus.BackgroundProgressMonitor;
17import org.openstreetmap.josm.gui.PleaseWaitDialog;
18
19public class PleaseWaitProgressMonitor extends AbstractProgressMonitor {
20
21 /**
22 * Implemented by both foreground dialog and background progress dialog (in status bar)
23 */
24 public interface ProgressMonitorDialog {
25 void setVisible(boolean visible);
26
27 void updateProgress(int progress);
28
29 void setCustomText(String text);
30
31 void setCurrentAction(String text);
32
33 void setIndeterminate(boolean newValue);
34
35 // TODO Not implemented properly in background monitor, log message will get lost if progress runs in background
36 void appendLogMessage(String message);
37 }
38
39 public static final int PROGRESS_BAR_MAX = 10000;
40 private final Component dialogParent;
41
42 private int currentProgressValue = 0;
43 private String customText;
44 private String title;
45 private boolean indeterminate;
46
47 private boolean isInBackground;
48 private PleaseWaitDialog dialog;
49 private String windowTitle;
50 protected ProgressTaskId taskId;
51
52 private boolean cancelable;
53
54 private void doInEDT(Runnable runnable) {
55 // This must be invoke later even if current thread is EDT because inside there is dialog.setVisible
56 // which freeze current code flow until modal dialog is closed
57 SwingUtilities.invokeLater(runnable);
58 }
59
60 private void setDialogVisible(boolean visible) {
61 if (dialog.isVisible() != visible) {
62 dialog.setVisible(visible);
63 }
64 }
65
66 private ProgressMonitorDialog getDialog() {
67
68 BackgroundProgressMonitor backgroundMonitor = null;
69 MapFrame map = Main.map;
70 if (map != null) {
71 backgroundMonitor = map.statusLine.progressMonitor;
72 }
73
74 if (backgroundMonitor != null) {
75 backgroundMonitor.setVisible(isInBackground);
76 }
77 if (dialog != null) {
78 setDialogVisible(!isInBackground || backgroundMonitor == null);
79 }
80
81 if (isInBackground && backgroundMonitor != null) {
82 backgroundMonitor.setVisible(true);
83 if (dialog != null) {
84 setDialogVisible(false);
85 }
86 return backgroundMonitor;
87 } else if (backgroundMonitor != null) {
88 backgroundMonitor.setVisible(false);
89 if (dialog != null) {
90 setDialogVisible(true);
91 }
92 return dialog;
93 } else if (dialog != null) {
94 setDialogVisible(true);
95 return dialog;
96 } else
97 return null;
98 }
99
100 public PleaseWaitProgressMonitor() {
101 this("");
102 }
103
104 public PleaseWaitProgressMonitor(String windowTitle) {
105 this(Main.parent);
106 this.windowTitle = windowTitle;
107 }
108
109 public PleaseWaitProgressMonitor(Component dialogParent) {
110 super(new CancelHandler());
111 this.dialogParent = JOptionPane.getFrameForComponent(dialogParent);
112 this.cancelable = true;
113 }
114
115 public PleaseWaitProgressMonitor(Component dialogParent, String windowTitle) {
116 this(JOptionPane.getFrameForComponent(dialogParent));
117 this.windowTitle = windowTitle;
118 }
119
120 private ActionListener cancelListener = new ActionListener() {
121 @Override
122 public void actionPerformed(ActionEvent e) {
123 cancel();
124 }
125 };
126
127 private ActionListener inBackgroundListener = new ActionListener() {
128 @Override
129 public void actionPerformed(ActionEvent e) {
130 isInBackground = true;
131 ProgressMonitorDialog dialog = getDialog();
132 if (dialog != null) {
133 reset();
134 dialog.setVisible(true);
135 }
136 }
137 };
138
139 private WindowListener windowListener = new WindowAdapter() {
140 @Override public void windowClosing(WindowEvent e) {
141 cancel();
142 }
143 };
144
145 public final boolean isCancelable() {
146 return cancelable;
147 }
148
149 public final void setCancelable(boolean cancelable) {
150 this.cancelable = cancelable;
151 }
152
153 @Override
154 public void doBeginTask() {
155 doInEDT(new Runnable() {
156 @Override
157 public void run() {
158 Main.currentProgressMonitor = PleaseWaitProgressMonitor.this;
159 if (dialogParent != null && dialog == null) {
160 dialog = new PleaseWaitDialog(dialogParent);
161 } else
162 throw new ProgressException("PleaseWaitDialog parent must be set");
163
164 if (windowTitle != null) {
165 dialog.setTitle(windowTitle);
166 }
167 dialog.setCancelEnabled(cancelable);
168 dialog.setCancelCallback(cancelListener);
169 dialog.setInBackgroundCallback(inBackgroundListener);
170 dialog.setCustomText("");
171 dialog.addWindowListener(windowListener);
172 dialog.progress.setMaximum(PROGRESS_BAR_MAX);
173 dialog.setVisible(true);
174 }
175 });
176 }
177
178 @Override
179 public void doFinishTask() {
180 // do nothing
181 }
182
183 @Override
184 protected void updateProgress(double progressValue) {
185 final int newValue = (int) (progressValue * PROGRESS_BAR_MAX);
186 if (newValue != currentProgressValue) {
187 currentProgressValue = newValue;
188 doInEDT(new Runnable() {
189 @Override
190 public void run() {
191 ProgressMonitorDialog dialog = getDialog();
192 if (dialog != null) {
193 dialog.updateProgress(currentProgressValue);
194 }
195 }
196 });
197 }
198 }
199
200 @Override
201 protected void doSetCustomText(final String title) {
202 checkState(State.IN_TASK, State.IN_SUBTASK);
203 this.customText = title;
204 doInEDT(new Runnable() {
205 @Override
206 public void run() {
207 ProgressMonitorDialog dialog = getDialog();
208 if (dialog != null) {
209 dialog.setCustomText(title);
210 }
211 }
212 });
213 }
214
215 @Override
216 protected void doSetTitle(final String title) {
217 checkState(State.IN_TASK, State.IN_SUBTASK);
218 this.title = title;
219 doInEDT(new Runnable() {
220 @Override
221 public void run() {
222 ProgressMonitorDialog dialog = getDialog();
223 if (dialog != null) {
224 dialog.setCurrentAction(title);
225 }
226 }
227 });
228 }
229
230 @Override
231 protected void doSetIntermediate(final boolean value) {
232 this.indeterminate = value;
233 doInEDT(new Runnable() {
234 @Override
235 public void run() {
236 // Enable only if progress is at the beginning. Doing intermediate progress in the middle
237 // will hide already reached progress
238 ProgressMonitorDialog dialog = getDialog();
239 if (dialog != null) {
240 dialog.setIndeterminate(value && currentProgressValue == 0);
241 }
242 }
243 });
244 }
245
246 @Override
247 public void appendLogMessage(final String message) {
248 doInEDT(new Runnable() {
249 @Override
250 public void run() {
251 ProgressMonitorDialog dialog = getDialog();
252 if (dialog != null) {
253 dialog.appendLogMessage(message);
254 }
255 }
256 });
257 }
258
259 public void reset() {
260 if (dialog != null) {
261 dialog.setTitle(title);
262 dialog.setCustomText(customText);
263 dialog.updateProgress(currentProgressValue);
264 dialog.setIndeterminate(indeterminate && currentProgressValue == 0);
265 }
266 BackgroundProgressMonitor backgroundMonitor = null;
267 MapFrame map = Main.map;
268 if (map != null) {
269 backgroundMonitor = map.statusLine.progressMonitor;
270 }
271 if (backgroundMonitor != null) {
272 backgroundMonitor.setCurrentAction(title);
273 backgroundMonitor.setCustomText(customText);
274 backgroundMonitor.updateProgress(currentProgressValue);
275 backgroundMonitor.setIndeterminate(indeterminate && currentProgressValue == 0);
276 }
277
278 }
279
280 public void close() {
281 doInEDT(new Runnable() {
282 @Override
283 public void run() {
284 if (dialog != null) {
285 dialog.setVisible(false);
286 dialog.setCancelCallback(null);
287 dialog.setInBackgroundCallback(null);
288 dialog.removeWindowListener(windowListener);
289 dialog.dispose();
290 dialog = null;
291 Main.currentProgressMonitor = null;
292 MapFrame map = Main.map;
293 if (map != null) {
294 map.statusLine.progressMonitor.setVisible(false);
295 }
296 }
297 }
298 });
299 }
300
301 public void showForegroundDialog() {
302 isInBackground = false;
303 doInEDT(new Runnable() {
304 @Override
305 public void run() {
306 if (dialog != null) {
307 dialog.setInBackgroundPossible(PleaseWaitProgressMonitor.this.taskId != null && Main.isDisplayingMapView());
308 reset();
309 getDialog();
310 }
311 }
312 });
313
314 }
315
316 @Override
317 public void setProgressTaskId(ProgressTaskId taskId) {
318 this.taskId = taskId;
319 doInEDT(new Runnable() {
320 @Override
321 public void run() {
322 if (dialog != null) {
323 dialog.setInBackgroundPossible(PleaseWaitProgressMonitor.this.taskId != null && Main.isDisplayingMapView());
324 }
325 }
326 });
327 }
328
329 @Override
330 public ProgressTaskId getProgressTaskId() {
331 return taskId;
332 }
333
334 @Override
335 public Component getWindowParent() {
336 Component parent = dialog;
337 if (isInBackground || parent == null)
338 return Main.parent;
339 else
340 return parent;
341 }
342}
Note: See TracBrowser for help on using the repository browser.