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

Last change on this file since 2322 was 2322, checked in by Gubaer, 14 years ago

Added canceling of DownloadOsmTaskLists
Removed error remembering in the progress dialog

  • Property svn:mime-type set to text/plain
File size: 4.6 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;
13
14import javax.swing.JOptionPane;
15
16import org.openstreetmap.josm.Main;
17import org.openstreetmap.josm.gui.PleaseWaitDialog;
18import static org.openstreetmap.josm.tools.I18n.tr;
19
20
21public 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}
Note: See TracBrowser for help on using the repository browser.