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

Last change on this file since 1865 was 1865, checked in by Gubaer, 15 years ago

replaced JOptionPane.show* by OptionPaneUtil.show*
ExtendeDialog now always on top, too

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