1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package p3j.gui.dialogs.execstatus;
17
18 import java.awt.BorderLayout;
19 import java.awt.Dimension;
20 import java.awt.Frame;
21 import java.awt.event.ActionEvent;
22 import java.awt.event.ActionListener;
23
24 import javax.swing.JButton;
25 import javax.swing.JDialog;
26 import javax.swing.JLabel;
27 import javax.swing.JPanel;
28 import javax.swing.JProgressBar;
29 import javax.swing.SwingConstants;
30
31 import org.jamesii.gui.utils.BasicUtilities;
32
33 import p3j.misc.IProgressObserver;
34 import p3j.misc.gui.GUI;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public class SimpleProgressDialog extends JDialog implements IProgressObserver {
51
52
53 private static final long serialVersionUID = 6832108832051608722L;
54
55
56 private static final int DIALOG_WIDTH = 400;
57
58
59 private static final int DIALOG_HEIGHT = 130;
60
61
62 final JProgressBar progressBar;
63
64
65 final boolean cancellationAllowed;
66
67
68 private boolean cancelled = false;
69
70
71 private JButton okButton = new JButton("OK");
72 {
73 okButton.addActionListener(new ActionListener() {
74 @Override
75 public void actionPerformed(ActionEvent e) {
76 setVisible(false);
77 }
78 });
79 }
80
81
82 private JButton cancelButton = new JButton("Cancel");
83 {
84 cancelButton.addActionListener(new ActionListener() {
85 @Override
86 public void actionPerformed(ActionEvent e) {
87 cancelled = true;
88 }
89 });
90 }
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106 public SimpleProgressDialog(Frame owner, String processName,
107 String detailedDescription, int numOfWaypoints,
108 boolean cancellationAllowed) {
109 super(owner, processName, false);
110 this.cancellationAllowed = cancellationAllowed;
111 setSize(DIALOG_WIDTH, DIALOG_HEIGHT
112 - (detailedDescription.isEmpty() ? 20 : 0));
113 GUI.centerOnScreen(this);
114
115 progressBar = new JProgressBar(0, numOfWaypoints);
116 okButton.setEnabled(false);
117
118 JPanel buttonPanel = new JPanel();
119 buttonPanel.add(okButton);
120 if (cancellationAllowed)
121 buttonPanel.add(cancelButton);
122
123 JPanel content = new JPanel(new BorderLayout(GUI.STD_LAYOUT_GAP,
124 GUI.STD_LAYOUT_GAP));
125 JLabel executionSummary = new JLabel(detailedDescription,
126 SwingConstants.CENTER);
127 content.add(executionSummary, BorderLayout.NORTH);
128 content.add(progressBar, BorderLayout.CENTER);
129 content.add(buttonPanel, BorderLayout.SOUTH);
130
131 progressBar.setStringPainted(true);
132 progressBar.setPreferredSize(new Dimension(0,
133 ExecutionProgressDialog.PREFERRED_HEIGHT_PROGBAR));
134 getContentPane().add(content);
135 }
136
137 @Override
138 public void taskFinished() {
139 updateProgress(progressBar.getMaximum(), "Done");
140 okButton.setEnabled(true);
141 cancelButton.setEnabled(false);
142 }
143
144 @Override
145 public void taskCanceled() {
146 setVisible(false);
147 }
148
149 @Override
150 public void updateProgress(final int waypoint, final String status) {
151 BasicUtilities.invokeLaterOnEDT(new Runnable() {
152 @Override
153 public void run() {
154 progressBar.setValue(waypoint);
155 progressBar.setString(status);
156 }
157 });
158 }
159
160 @Override
161 public int getCurrentWaypoint() {
162 return progressBar.getValue();
163 }
164
165 @Override
166 public void addWaypoints(int additionalWayPoints) {
167 progressBar.setMaximum(progressBar.getMaximum() + additionalWayPoints);
168 }
169
170 @Override
171 public void incrementProgress(String status) {
172 updateProgress(progressBar.getValue() + 1, status);
173 }
174
175 @Override
176 public boolean isCancelled() {
177 return cancelled;
178 }
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195 public static IProgressObserver showDialog(Frame owner, String processName,
196 String detailedDescription, int numOfWaypoints,
197 boolean cancellationAllowed) {
198 final SimpleProgressDialog theDialog = new SimpleProgressDialog(owner,
199 processName, detailedDescription, numOfWaypoints, cancellationAllowed);
200 BasicUtilities.invokeLaterOnEDT(new Runnable() {
201 @Override
202 public void run() {
203 theDialog.setVisible(true);
204 }
205 });
206 return theDialog;
207 }
208
209 }