Source code: com/voytechs/jnetanalyzer/message/swing/JTimedProgressBar.java
1 /*
2 * File: JTimedProgressBar.java
3 * Auth: Mark Bednarczyk
4 * Date: DATE
5 * Id: $Id: JTimedProgressBar.java,v 1.1.1.1 2003/09/22 16:32:06 voytechs Exp $
6 ********************************************
7 Copyright (C) 2003 Mark Bednarczyk
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 ********************************************
23 * $Log: JTimedProgressBar.java,v $
24 * Revision 1.1.1.1 2003/09/22 16:32:06 voytechs
25 * Initial import.
26 *
27 */
28 package com.voytechs.jnetanalyzer.message.swing;
29
30 import java.lang.*;
31 import java.util.*;
32
33 import java.awt.*;
34 import java.awt.event.*;
35 import javax.swing.*;
36 import javax.swing.border.*;
37 import java.sql.*;
38 import java.text.*;
39
40 /**
41 *
42 */
43 public class JTimedProgressBar
44 extends JProgressBar
45 implements ActionListener {
46
47 /* Internal attributes */
48 private static final boolean debug = true;
49
50 private javax.swing.Timer timer;
51 private float timeOut;
52 private float timeLeft;
53 private float timeDecrement = 500; // 100ms (0.10sec)
54
55 private int timeOutDisappearDelay = 15 * 1000;
56
57 private Action timeOutAction;
58
59 /**
60 * Sets up a timed progress bar. Progress bar is updated every 100ms (0.10sec)
61 * until zero, then the timer is stopped.
62 * @param timeOutMillis Number of milliseconds to timeout.
63 * @exception
64 */
65 public JTimedProgressBar(int timeOutMillis) {
66
67 this.timeOut = (float)timeOutMillis;
68 this.timeLeft = (float)timeOut;
69
70 setStringPainted(true);
71
72
73 /**
74 * Start up the timer.
75 */
76 this.timer = new javax.swing.Timer((int)timeDecrement, this);
77 this.timer.setRepeats(true);
78 this.timer.start();
79 setValue(100);
80 }
81
82
83 /**
84 * Handle timer events until timeout occures. Then turn off timer.
85 */
86 public void actionPerformed(ActionEvent event) {
87
88
89 if(timeLeft <= 0) {
90 setVisible(false);
91 timer.stop();
92
93 if(debug)
94 System.out.println("JTimedProgressBar(): timer removed");
95
96 return;
97 }
98
99 timeLeft -= timeDecrement;
100
101 if(timeLeft <= 0) {
102 timer.stop();
103 timer.setInitialDelay(timeOutDisappearDelay);
104 timer.restart();
105
106 setValue(0);
107 setString("timeout!");
108
109 if(debug)
110 System.out.println("JTimedProgressBar(): timer stop");
111 return;
112 }
113
114 setValue((int)(timeLeft/timeOut * 100));
115
116 setString("timeout in " + (int)(timeLeft / 1000) + " seconds");
117 }
118
119 /**
120 * Fire action perform upon timeout.
121 */
122 protected void fireActionPerform() {
123 }
124
125 /**
126 * Cancels the timeout timer.
127 */
128 public void cancelTimeoutTimer() {
129 timer.stop();
130 }
131
132 /**
133 * Test function for JTimedProgressBar
134 * @param args command line arguments
135 */
136 public static void main(String [] args) {
137 }
138
139 } /* END OF: JTimedProgressBar */