View Javadoc

1   /********************************************************************************
2    * InternetCafe is a software solution that helps the management of Cybercafes 
3    * according with the ITALIAN DECREE LAW ON ANTI-TERROR MEASURES, 27 JULY 2005.
4    * Copyright (C) 2006  Guido Angelo Ingenito
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
9    * of the License, or (at your option) any later version.
10  
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   * 
16   * You should have received a copy of the GNU General Public License
17   * along with this program; if not, write to the Free Software
18   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19   *******************************************************************************/
20  package ui.panel;
21  
22  import java.awt.BorderLayout;
23  import java.awt.Dimension;
24  import java.awt.GridLayout;
25  import java.awt.Toolkit;
26  import java.awt.event.ActionEvent;
27  import java.awt.event.ActionListener;
28  
29  import javax.swing.BorderFactory;
30  import javax.swing.JLabel;
31  import javax.swing.JPanel;
32  import javax.swing.JProgressBar;
33  import javax.swing.Timer;
34  import javax.swing.border.EtchedBorder;
35  
36  @SuppressWarnings("serial") //$NON-NLS-1$
37  public class StatusBarPanel extends JPanel {
38  
39  	private JProgressBar progressBar;
40  
41  	private JPanel messagePanel;
42  
43  	private JLabel messageLabel;
44  
45  	private Timer timer;
46  
47  	private boolean taskDone = false;
48  
49  	public StatusBarPanel() {
50  		// MessageLogger.println("StatusBar",MessageLogger.DEBUG_MESSAGE);
51  
52  		initialize();
53  	}
54  
55  	protected void initialize() {
56  		this.setOpaque(true);
57  		this.setPreferredSize(new Dimension(1024, 20));
58  		this.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
59  		this.setLayout(new GridLayout(1, 2));
60  		this.add(getMessagePanel(), null);
61  
62  		JPanel progressPanel = new JPanel();
63  		progressPanel.setLayout(new GridLayout(1, 3));
64  		progressPanel.add(new JLabel(), null);
65  		progressPanel.add(getProgressBar(), null);
66  		progressPanel.add(new JLabel(), null);
67  		this.add(progressPanel, null);
68  	}
69  
70  	/***
71  	 * @return Returns the progressBar.
72  	 */
73  	public JProgressBar getProgressBar() {
74  		if (progressBar == null) {
75  			progressBar = new JProgressBar();
76  			progressBar.setMinimum(0);
77  			progressBar.setMaximum(110);
78  		}
79  		return progressBar;
80  	}
81  
82  	/***
83  	 * @return Returns the messagePanel.
84  	 */
85  	protected JPanel getMessagePanel() {
86  		if (messagePanel == null) {
87  			messagePanel = new JPanel();
88  			messagePanel.setLayout(new BorderLayout());
89  			messagePanel.add(getMessageLabel(), BorderLayout.CENTER);
90  		}
91  		return messagePanel;
92  	}
93  
94  	/***
95  	 * @return Returns the messageLabel.
96  	 */
97  	protected JLabel getMessageLabel() {
98  		if (messageLabel == null) {
99  			messageLabel = new JLabel();
100 		}
101 		return messageLabel;
102 	}
103 
104 	public void setMessage(String message) {
105 		this.getMessageLabel().setText(message);
106 	}
107 
108 	/***
109 	 * @return Returns the timer.
110 	 */
111 	public Timer getTimer() {
112 		if (timer == null || !timer.isRunning()) {
113 			// Create a timer.
114 			timer = new Timer(100, new ActionListener() {
115 				public void actionPerformed(ActionEvent evt) {
116 					getProgressBar().setValue(getProgressBar().getValue() + 1);
117 					if (isTaskDone()) {
118 						Toolkit.getDefaultToolkit().beep();
119 						timer.stop();
120 						getProgressBar()
121 								.setValue(getProgressBar().getMinimum());
122 					}
123 				}
124 			});
125 		}
126 		return timer;
127 	}
128 
129 	/***
130 	 * @return Returns the taskDone.
131 	 */
132 	public synchronized boolean isTaskDone() {
133 		return taskDone;
134 	}
135 
136 	/***
137 	 * @param taskDone
138 	 *            The taskDone to set.
139 	 */
140 	public synchronized void setTaskDone(boolean taskDone) {
141 		this.taskDone = taskDone;
142 		getProgressBar().setValue(getProgressBar().getMinimum());
143 		getTimer().stop();
144 	}
145 }