KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > agent > client > gui > awt > ProgressBar


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.agent.client.gui.awt;
21
22 import java.awt.Button JavaDoc;
23 import java.awt.Canvas JavaDoc;
24 import java.awt.Color JavaDoc;
25 import java.awt.Component JavaDoc;
26 import java.awt.Dialog JavaDoc;
27 import java.awt.Dimension JavaDoc;
28 import java.awt.FlowLayout JavaDoc;
29 import java.awt.FontMetrics JavaDoc;
30 import java.awt.Frame JavaDoc;
31 import java.awt.Graphics JavaDoc;
32 import java.awt.Label JavaDoc;
33 import java.awt.Panel JavaDoc;
34 import java.awt.Window JavaDoc;
35 import java.awt.event.ActionEvent JavaDoc;
36 import java.awt.event.ActionListener JavaDoc;
37 import java.awt.event.WindowAdapter JavaDoc;
38 import java.awt.event.WindowEvent JavaDoc;
39 import java.text.MessageFormat JavaDoc;
40
41 import com.sshtools.ui.awt.UIUtil;
42
43 /**
44  * The ProgressBar class allows you to create non-modal progress bars using only
45  * the java.awt classes. All you have to do is create an instance of a
46  * ProgressBar, update the progress using the updateValue method as you're doing
47  * work, and use the dispose method to close the ProgressBar dialog when you're
48  * done. The ProgressBar also includes an optional Cancel button, so the user
49  * can cancel the action while your process is running (if you use this button,
50  * make sure you check the isCancelClicked method occassionally to see if the
51  * user clicked Cancel).
52  * <p>
53  * This class has been implemented here as an inner class, but there's no reason
54  * why it couldn't be a class all by itself. The progress bar component itself
55  * is actually an inner class to this inner class. Read the comments there to
56  * see how it works.
57  * <p> *
58  * Make sure you import java.awt.* and java.awt.event.*
59  * <p>
60  *
61  * Julian Robichaux -- http://www.nsftools.com
62  */

63 public class ProgressBar {
64     /**
65      * Display nothing in the progress bar
66      */

67     public final int BOXMSG_NONE = 0;
68     /**
69      * Display the percent complete (like "20%")
70      */

71     public final int BOXMSG_PERCENT = 1;
72     /**
73      * Display the number complete (like "4 of 10")
74      */

75     public final int BOXMSG_NUMBERS = 2;
76     
77
78     private Frame JavaDoc parent;
79     private Window JavaDoc pbar;
80     private Label JavaDoc mess;
81     private ProgressBox pbox;
82     private Button JavaDoc cancel;
83     private boolean shouldCancel;
84
85     /**
86      * This version of the constructor creates a ProgressBar that includes a
87      * Cancel button
88      *
89      * @param parentComponent
90      * @param message
91      * @param title
92      * @param maxValue
93      */

94     public ProgressBar(Component JavaDoc parentComponent, String JavaDoc message, String JavaDoc title, long maxValue) {
95         this(parentComponent, message, title, maxValue, true);
96     }
97
98     /**
99      * When you create a new instance of a ProgressBar, it sets up the progress
100      * bar and immediately displays it. The message parameter is a label that
101      * displays just above the progress bar, the maxValue parameter indicates
102      * what value should be considered 100% on the progress bar, and the
103      * allowCancel parameter indicates whether or not a Cancel button should be
104      * displayed at the bottom of the dialog.
105      *
106      * @param parentComponent
107      * @param message
108      * @param title
109      * @param maxValue
110      * @param allowCancel
111      */

112     public ProgressBar(Component JavaDoc parentComponent, String JavaDoc message, String JavaDoc title, long maxValue, boolean allowCancel) {
113         shouldCancel = false;
114         // this is the invisible Frame we'll be using to call all the Dialog
115

116         parent = UIUtil.getFrameAncestor(parentComponent);
117         if (parent == null) {
118             pbar = new Frame JavaDoc(title);
119         } else {
120             if (!parent.isVisible()) {
121                 parent.setVisible(true);
122             }
123             parent.toFront();
124             pbar = new Dialog JavaDoc(parent, title);
125         }
126
127         if (parent != null) {
128             parent.addWindowListener(new WindowAdapter JavaDoc() {
129                 public void windowClosing(WindowEvent JavaDoc e) {
130                     parent.setVisible(false);
131                     dispose();
132                 }
133             });
134         }
135
136         // add the message to the top of the dialog
137
Panel JavaDoc top = new Panel JavaDoc(new FlowLayout JavaDoc(FlowLayout.CENTER, 1, 1));
138         mess = new Label JavaDoc(message);
139         top.add(mess);
140         pbar.add("North", top); //$NON-NLS-1$
141

142         // add the progress bar to the middle of the dialog
143
Panel JavaDoc middle = new Panel JavaDoc(new FlowLayout JavaDoc(FlowLayout.CENTER, 1, 1));
144         pbox = new ProgressBox(maxValue);
145         middle.add(pbox);
146         pbar.add("Center", middle); //$NON-NLS-1$
147

148         // add the Cancel button to the bottom of the dialog (if allowCancel is
149
// true)
150
if (allowCancel) {
151             Panel JavaDoc bottom = new Panel JavaDoc(new FlowLayout JavaDoc(FlowLayout.CENTER, 1, 1));
152             cancel = new Button JavaDoc(Messages.getString("ProgressBar.cancel")); //$NON-NLS-1$
153
cancel.addActionListener(new ActionListener JavaDoc() {
154                 public void actionPerformed(ActionEvent JavaDoc e) {
155                     // pbar.dispose();
156
shouldCancel = true;
157                 }
158             });
159             bottom.add(cancel);
160             pbar.add("South", bottom); //$NON-NLS-1$
161
}
162
163         // display the ProgressBar dialog
164
Dimension JavaDoc d = pbar.getToolkit().getScreenSize();
165         pbar.setLocation(d.width / 3, d.height / 3); // center the
166
// ProgressBar (sort of)
167
pbar.pack(); // organize all its components
168
if (pbar instanceof Dialog JavaDoc) {
169             ((Dialog JavaDoc) pbar).setResizable(false); // make sure the user can't
170
// resize it
171
} else {
172             ((Frame JavaDoc) pbar).setResizable(false); // make sure the user can't
173
// resize it
174
}
175         pbar.setVisible(true); // and display it
176
pbar.toFront(); // give the ProgressBar focus
177

178     }
179
180     /**
181      * The updateValue method allows you to update the value used by the
182      * progress bar in order to calculate the percent complete on the bar.
183      * Percent complete is the value parameter passed to this method divided by
184      * the maxValue you passed in when you initially instantiated the
185      * ProgressBar.
186      *
187      * @param value
188      */

189     public void updateValue(long value) {
190         pbox.updateValue(value);
191     }
192
193     /**
194      * The updateMaxValue method allows you to update the maximum value used by
195      * the progress bar in order to calculate the percent complete on the bar.
196      * @param value
197      */

198     public void updateMaxValue(long value) {
199         pbox.updateMaxValue(value);
200     }
201
202     /**
203      * The getCurrentValue method returns the current value used by the progress
204      * bar to determine the percent complete.
205      *
206      * @return current value
207      */

208     public long getCurrentValue() {
209         return pbox.getCurrentValue();
210     }
211
212     /**
213      * The getMaxValue method returns the maximum value used by the progress bar
214      * to determine the percent complete (once the current value equals the
215      * maximum value, we're at 100%)
216      *
217      * @return max value
218      */

219     public long getMaxValue() {
220         return pbox.getMaxValue();
221     }
222
223     /**
224      * The setBarText method sets the value of the dispText field in the
225      * progress bar, which indicates what kind of message should be displayed in
226      * the bar. You'll normally use a value of BOXMSG_NONE, BOXMSG_PERCENT, or
227      * BOXMSG_NUMBERS for this value.
228      *
229      * @param boxMsgValue text
230      */

231     public void setBarText(int boxMsgValue) {
232         pbox.setBarMsg(boxMsgValue);
233     }
234
235     /**
236      * Set the message
237      *
238      * @param message message
239      */

240     public void setMessage(String JavaDoc message) {
241         mess.setText(message);
242         // parent.pack();
243
}
244
245     /**
246      * The dispose method removes the ProgressBar from the screen display.
247      */

248     public void dispose() {
249         // use this when you're ready to clean up
250
try {
251             pbar.dispose();
252         } catch (Exception JavaDoc e) {
253         }
254
255         // TODO brett - This closes the vpn window? any reason for this?
256
// try {
257
// parent.dispose();
258
// }
259
// catch (Exception e) {}
260
}
261
262     /**
263      * The isCancelClicked method indicates whether or not the user clicked the
264      * Cancel button. Normally, when you realize that the user has clicked
265      * Cancel, you'll want to call the dispose method to stop displaying the
266      * ProgressBar.
267      *
268      * @return cancel click
269      */

270     public boolean isCancelClicked() {
271         return shouldCancel;
272     }
273
274     /**
275      * The ProgressBox is the actual awt component that displays a progress bar.
276      * It's implemented here as an inner class of the ProgressBar class, but it
277      * can be a separate class too. Just make sure you import java.awt.*
278      *
279      * Julian Robichaux -- http://www.nsftools.com
280      */

281     class ProgressBox extends Canvas JavaDoc {
282         /**
283          * Display nothing in the progress bar
284          */

285         public final int MSG_NONE = 0;
286         
287         /**
288          * Display the percent complete like "20%")
289          */

290         public final int MSG_PERCENT = 1;
291         
292         /**
293          * Display the number complete (like "4 of 10")
294          */

295         public final int MSG_NUMBERS = 2;
296
297         private long maxVal, currentVal;
298         private int cols, width, height, dispText;
299         private Color JavaDoc barClr, borderClr, textClr;
300
301         /**
302          * Constructor.
303          *
304          * @param maxValue max value
305          */

306         public ProgressBox(long maxValue) {
307             this(maxValue, 40);
308         }
309
310         /**
311          * Constructor.
312          *
313          * @param maxValue max value
314          * @param width width
315          */

316         public ProgressBox(long maxValue, int width) {
317             // one unit of width for this component is the width of
318
// the letter 'X' in the current font (so a width of 10 is
319
// a width of 'XXXXXXXXXX' using the current font)
320
maxVal = maxValue;
321             currentVal = 0;
322             cols = width;
323             dispText = MSG_PERCENT;
324             barClr = Color.decode("#D8DFEE"); // make the progress bar light //$NON-NLS-1$
325
// blue
326
borderClr = Color.gray; // make the bar border gray
327
textClr = Color.darkGray; // make the text dark gray
328
}
329
330         protected void measure() {
331             // get the global values we use in relation to our current font
332
FontMetrics JavaDoc fm = this.getFontMetrics(this.getFont());
333             if (fm == null) {
334                 return;
335             }
336             width = fm.stringWidth("X") * cols; //$NON-NLS-1$
337
height = fm.getHeight() + 4;
338         }
339
340         public void addNotify() {
341             // automatically invoked after our Canvas is created but
342
// before it's displayed (FontMetrics aren't available until
343
// super.addNotify() has been called)
344
super.addNotify();
345             measure();
346         }
347
348         public Dimension JavaDoc getPreferredSize() {
349             // called by the LayoutManager to find out how big we want to be
350
return new Dimension JavaDoc(width + 4, height + 4);
351         }
352
353         public Dimension JavaDoc getMinimumSize() {
354             // called by the LayoutManager to find out what our bare minimum
355
// size requirements are
356
return getPreferredSize();
357         }
358
359         /**
360          * Update value
361          *
362          * @param value value
363          */

364         public void updateValue(long value) {
365             // change the currentVal, which is used to determine what our
366
// percent
367
// complete is, and update the progress bar
368
currentVal = value;
369             this.repaint();
370         }
371
372         /**
373          * Update max value
374          *
375          * @param value max value
376          */

377         public void updateMaxValue(long value) {
378             // change the maxVal, which is used to determine what our percent
379
// complete is ((currentVal / maxVal) * 100 = percent complete),
380
// and update the progress bar
381
maxVal = value;
382             this.repaint();
383         }
384
385         /**
386          * Get current value
387          *
388          * @return current value
389          */

390         public long getCurrentValue() {
391             // return the currentVal
392
return currentVal;
393         }
394
395         /**
396          * Get max value
397          *
398          * @return max value
399          */

400         public long getMaxValue() {
401             // return the maxVal
402
return maxVal;
403         }
404
405         /**
406          * Set text
407          *
408          * @param msgValue text
409          */

410         public void setBarMsg(int msgValue) {
411             // change the dispText value, which is used to determine what text,
412
// if any, is displayed in the progress bar (use either MSG_NONE,
413
// MSG_PERCENT, or MSG_NUMBERS)
414
dispText = msgValue;
415         }
416
417         /**
418          * Set the colors
419          *
420          * @param barColor bar color
421          * @param borderColor border color
422          * @param textColor text color
423          */

424         public void setColors(Color JavaDoc barColor, Color JavaDoc borderColor, Color JavaDoc textColor) {
425             // set the colors used by the progress bar components
426
if (barColor != null) {
427                 barClr = barColor;
428             }
429             if (borderColor != null) {
430                 borderClr = borderColor;
431             }
432             if (textColor != null) {
433                 textClr = textColor;
434             }
435         }
436
437         public void paint(Graphics JavaDoc g) {
438             // draw the actual progress bar to the screen
439
// this is the bar itself
440
g.setColor(barClr);
441             if (currentVal > 0 && maxVal > 0)
442                 g.fillRect(0, 0, (int) ((currentVal * width) / maxVal), height);
443
444             // this is the border around the bar
445
g.setColor(borderClr);
446             g.drawRect(0, 0, width, height);
447
448             // this is the text to display (if any)
449
g.setColor(textClr);
450             if (dispText == MSG_PERCENT) {
451                 if (currentVal > 0 && maxVal > 0) {
452                     centerText(String.valueOf((int) ((currentVal * 100) / maxVal)) + "%", g, width, height); //$NON-NLS-1$
453
} else if (currentVal == 0) {
454                     centerText("0%", g, width, height); //$NON-NLS-1$
455

456                 }
457             } else if (dispText == MSG_NUMBERS) {
458                 centerText(MessageFormat.format(Messages.getString("ProgressBar.numbers"), new Object JavaDoc[] { new Long JavaDoc(currentVal), new Long JavaDoc(maxVal) } ), g, width, height); //$NON-NLS-1$
459
}
460         }
461
462         private void centerText(String JavaDoc s, Graphics JavaDoc g, int w, int h) {
463             // from the centerText method in "Java Examples in a Nutshell"
464
FontMetrics JavaDoc fm = this.getFontMetrics(this.getFont());
465             if (fm == null) {
466                 return;
467             }
468             int sWidth = fm.stringWidth(s);
469             int sx = (w - sWidth) / 2;
470             int sy = (h - fm.getHeight()) / 2 + fm.getAscent();
471             g.drawString(s, sx, sy);
472         }
473
474     } // end of the ProgressBox class
475

476 } // end of the ProgressBar class
477
Popular Tags