-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgressPopup.java
More file actions
109 lines (88 loc) · 2.79 KB
/
ProgressPopup.java
File metadata and controls
109 lines (88 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import java.awt.Color;
import java.awt.Rectangle;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.Popup;
import javax.swing.PopupFactory;
import javax.swing.border.CompoundBorder;
/**
* Shows a popup containing a progress bar and a message while
* long running actions are in process.
*
* In order to make sure the progress popup always goes away, it's important
* to always call done() in a finally clause.
*
* @author Omar Farooq
*/
public class ProgressPopup {
JFrame parentFrame;
private int accumulator;
private Popup popup;
private JProgressBar progressBar;
private JLabel messageLabel;
/**
* @param parent The JFrame to use as the parent to the popup
*/
public ProgressPopup(JFrame parent) {
this.parentFrame = parent;
}
private void createProgressBar(String message) {
// create contents of popup
Box box = new Box(BoxLayout.Y_AXIS);
box.setBorder(new CompoundBorder(
BorderFactory.createLineBorder(Color.DARK_GRAY, 1),
BorderFactory.createEmptyBorder(10,10,10,10)));
messageLabel = new JLabel(message);
box.add(messageLabel);
box.add(Box.createVerticalStrut(4));
progressBar = new JProgressBar(JProgressBar.HORIZONTAL, 0, 100);
box.add(progressBar);
// calculate where to put popup
Rectangle r = parentFrame.getBounds();
int x = r.x + r.width / 2 - box.getWidth();
int y = r.y + r.height / 2 - box.getHeight();
// This uses swing's PopupFactory, which decides at runtime whether
// to use a light-weight popup or heavy weight popup. This behavior
// isn't particularly desirable, if you ask me. But, it does behave
// correctly, which is more than can be said for a plain JPopup as
// far as I could tell.
popup = PopupFactory.getSharedInstance().getPopup(parentFrame, box, x, y);
}
public void init(String message) {
init(100, message);
}
public void init(final int totalExpectedProgress) {
init(totalExpectedProgress, "Please wait...");
}
public void init(final int totalExpectedProgress, final String message) {
createProgressBar(message);
progressBar.setMaximum(totalExpectedProgress);
popup.show();
}
public void done() {
popup.hide();
}
public void setProgress(final int progress) {
accumulator = progress;
progressBar.setValue(progress);
}
public void progress(int progress, int expected) {
accumulator = progress;
progressBar.setValue(progress);
progressBar.setMaximum(expected);
}
public void incrementProgress(final int amount) {
accumulator += amount;
progressBar.setValue(accumulator);
}
public void setExpectedProgress(int expected) {
progressBar.setMaximum(expected);
}
public void setMessage(String message) {
this.messageLabel.setText(message);
}
}