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

Last change on this file since 4718 was 4718, checked in by jttt, 12 years ago

Add posibility to run please wait runnable tasks in background

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