1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package coras.client.ui;
22
23 import java.awt.BorderLayout;
24 import java.awt.Component;
25 import java.awt.Cursor;
26 import java.awt.Frame;
27 import java.awt.GridBagConstraints;
28 import java.awt.GridBagLayout;
29 import java.util.Observable;
30 import java.util.Observer;
31
32 import javax.swing.JButton;
33 import javax.swing.JDialog;
34 import javax.swing.JLabel;
35 import javax.swing.JPanel;
36 import javax.swing.JProgressBar;
37 import javax.swing.JScrollPane;
38 import javax.swing.JTextArea;
39 import javax.swing.SwingUtilities;
40
41 import coras.client.SwingWorker;
42 /***
43 * @author fvr
44 *
45 * TODO To change the template for this generated type comment go to
46 * Window - Preferences - Java - Code Style - Code Templates
47 */
48 public class ProgressDialog extends JDialog implements Observer {
49
50 private javax.swing.JPanel jContentPane = null;
51
52 private JPanel buttonPanel = null;
53 private JButton cancelButton = null;
54 private JPanel statusPanel = null;
55 private JLabel progressLabel = null;
56 private JProgressBar jProgressBar = null;
57
58 private SwingWorker worker = null;
59
60 private JTextArea statusArea = null;
61
62 private String lastTask = null;
63
64 private JScrollPane jScrollPane = null;
65 /***
66 * This is the default constructor
67 */
68 public ProgressDialog() {
69 this(null);
70 }
71
72 public ProgressDialog(Component owner) {
73 super(owner instanceof Frame ? (Frame)owner : null, true);
74 initialize();
75 if (owner != null) {
76 setLocationRelativeTo(owner);
77 }
78 }
79
80 public void setWorker(SwingWorker newWorker) {
81 if (worker == newWorker) {
82 return;
83 }
84 if (worker != null) {
85 worker.deleteObserver(this);
86 }
87 worker = newWorker;
88 if (worker != null) {
89 worker.addObserver(this);
90 update(worker, null);
91 }
92 }
93
94 public void setCancellable(boolean cancellable) {
95 getCancelButton().setEnabled(cancellable);
96 getButtonPanel().setVisible(cancellable);
97 }
98
99 /***
100 * This method initializes this
101 *
102 * @return void
103 */
104 private void initialize() {
105 this.setSize(400, 200);
106 this.setContentPane(getJContentPane());
107
108 this.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
109 this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
110 getStatusArea().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
111 this.addWindowListener(new java.awt.event.WindowAdapter() {
112 public void windowClosing(java.awt.event.WindowEvent e) {
113 if (getCancelButton().isEnabled() && worker != null) {
114 worker.interrupt();
115 }
116 setVisible(false);
117 dispose();
118 }
119 });
120 }
121
122 /***
123 * This method initializes jContentPane
124 *
125 * @return javax.swing.JPanel
126 */
127 private javax.swing.JPanel getJContentPane() {
128 if(jContentPane == null) {
129 jContentPane = new javax.swing.JPanel();
130 jContentPane.setLayout(new BorderLayout());
131 jContentPane.add(getButtonPanel(), java.awt.BorderLayout.SOUTH);
132 jContentPane.add(getStatusPanel(), java.awt.BorderLayout.CENTER);
133 }
134 return jContentPane;
135 }
136 /***
137 * This method initializes jPanel
138 *
139 * @return javax.swing.JPanel
140 */
141 private JPanel getButtonPanel() {
142 if (buttonPanel == null) {
143 buttonPanel = new JPanel();
144 buttonPanel.add(getCancelButton(), null);
145 }
146 return buttonPanel;
147 }
148 /***
149 * This method initializes jButton
150 *
151 * @return javax.swing.JButton
152 */
153 private JButton getCancelButton() {
154 if (cancelButton == null) {
155 cancelButton = new JButton();
156 cancelButton.setText("Cancel");
157
158 cancelButton.addActionListener(new java.awt.event.ActionListener() {
159 public void actionPerformed(java.awt.event.ActionEvent e) {
160 if (worker != null) {
161 worker.interrupt();
162 }
163 }
164 });
165 }
166 return cancelButton;
167 }
168 /***
169 * This method initializes jPanel
170 *
171 * @return javax.swing.JPanel
172 */
173 private JPanel getStatusPanel() {
174 if (statusPanel == null) {
175 GridBagConstraints gridBagConstraints14 = new GridBagConstraints();
176 GridBagConstraints gridBagConstraints13 = new GridBagConstraints();
177 progressLabel = new JLabel();
178 GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
179 GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
180 statusPanel = new JPanel();
181 statusPanel.setLayout(new GridBagLayout());
182 gridBagConstraints11.gridx = 0;
183 gridBagConstraints11.gridy = 2;
184 progressLabel.setText("Progress:");
185 gridBagConstraints12.gridx = 1;
186 gridBagConstraints12.gridy = 2;
187 gridBagConstraints12.fill = java.awt.GridBagConstraints.HORIZONTAL;
188 gridBagConstraints12.weightx = 1.0D;
189 statusPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(5,5,5,5));
190 gridBagConstraints13.gridx = 0;
191 gridBagConstraints13.gridy = 2;
192 gridBagConstraints13.weightx = 1.0;
193 gridBagConstraints13.weighty = 1.0;
194 gridBagConstraints13.fill = java.awt.GridBagConstraints.BOTH;
195 gridBagConstraints13.gridwidth = 2;
196 gridBagConstraints14.gridx = 0;
197 gridBagConstraints14.insets = new java.awt.Insets(0,0,5,0);
198 gridBagConstraints14.gridy = 1;
199 gridBagConstraints14.weightx = 1.0;
200 gridBagConstraints14.weighty = 1.0;
201 gridBagConstraints14.fill = java.awt.GridBagConstraints.BOTH;
202 gridBagConstraints14.gridwidth = 2;
203 statusPanel.add(getJScrollPane(), gridBagConstraints14);
204 statusPanel.add(progressLabel, gridBagConstraints11);
205 statusPanel.add(getJProgressBar(), gridBagConstraints12);
206 }
207 return statusPanel;
208 }
209 /***
210 * This method initializes jProgressBar
211 *
212 * @return javax.swing.JProgressBar
213 */
214 private JProgressBar getJProgressBar() {
215 if (jProgressBar == null) {
216 jProgressBar = new JProgressBar();
217 }
218 return jProgressBar;
219 }
220
221
222
223
224 public void update(Observable o, Object arg) {
225 if (o instanceof SwingWorker) {
226 final SwingWorker worker = (SwingWorker) o;
227 Runnable runner = new Runnable() {
228 public void run() {
229 if (worker.isFinished() || worker.isInterrupted()) {
230 dispose();
231 } else {
232 int taskLength = worker.getTaskLength();
233 int progress = worker.getProgress();
234 String jobName = worker.getJobName();
235 String currentTask = worker.getCurrentTask();
236 if (taskLength != getJProgressBar().getMaximum()) {
237 getJProgressBar().setMaximum(taskLength);
238 }
239 if (progress != getJProgressBar().getValue()) {
240 getJProgressBar().setValue(progress);
241 }
242 if (jobName != null && !jobName.equals(getTitle())) {
243 setTitle(jobName);
244 }
245 if (currentTask != null && !currentTask.equals(lastTask)) {
246 getStatusArea().setText(getStatusArea().getText() + currentTask + "\n");
247 lastTask = currentTask;
248 }
249 }
250 }
251 };
252 if (SwingUtilities.isEventDispatchThread()) {
253 runner.run();
254 } else {
255 try {
256 SwingUtilities.invokeAndWait(runner);
257 } catch (Exception e) {
258 }
259 }
260 }
261 }
262
263 /***
264 * This method initializes jTextArea
265 *
266 * @return javax.swing.JTextArea
267 */
268 private JTextArea getStatusArea() {
269 if (statusArea == null) {
270 statusArea = new JTextArea();
271 statusArea.setEditable(false);
272 }
273 return statusArea;
274 }
275 /***
276 * This method initializes jScrollPane
277 *
278 * @return javax.swing.JScrollPane
279 */
280 private JScrollPane getJScrollPane() {
281 if (jScrollPane == null) {
282 jScrollPane = new JScrollPane();
283 jScrollPane.setViewportView(getStatusArea());
284 }
285 return jScrollPane;
286 }
287 }