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  
21  package base.jdbs.ui.panel;
22  
23  import java.awt.GridLayout;
24  import java.util.Observable;
25  import java.util.Observer;
26  
27  import javax.swing.JLabel;
28  import javax.swing.JPanel;
29  import javax.swing.border.TitledBorder;
30  
31  import org.apache.log4j.Logger;
32  
33  import base.jdbs.Backup;
34  
35  /***
36   * @author skunk
37   * 
38   */
39  @SuppressWarnings("serial")
40  public class FileStatisticPanel extends JPanel implements Observer {
41  
42  	private static final transient Logger logger = Logger
43  			.getLogger(FileStatisticPanel.class.getName());
44  
45  	private final String TOTAL_FILE_COUNT = "Total Files: ";
46  
47  	private final String TOTAL_FILE_SIZE = "Total Size: ";
48  
49  	private final String AVG_FILE_SIZE = "Average File Size: ";
50  
51  	private JLabel totalFileLabel;
52  
53  	private JLabel totalFileSizeLabel;
54  
55  	private JLabel avgFileSizeLabel;
56  
57  	private final Backup backup;
58  
59  	public FileStatisticPanel(Backup backup) {
60  		this.backup = backup;
61  		this.backup.addObserver(this);
62  		initialize();
63  	}
64  
65  	protected void initialize() {
66  		this.setBorder(new TitledBorder("File Statistics"));
67  		this.setLayout(new GridLayout(1, 3));
68  		this.add(this.getTotalFileLabel());
69  		this.add(this.getTotalFileSizeLabel());
70  		this.add(this.getAvgFileSizeLabel());
71  	}
72  
73  	public void update(Observable arg0, Object arg1) {
74  		if (backup.getFileDescriptor().length > 0) {
75  			long totalSize = 0;
76  			for (int i = 0; i < backup.getFileDescriptor().length; i++)
77  				totalSize += backup.getFileDescriptor()[i].getSize();
78  			this.getTotalFileLabel().setText(
79  					TOTAL_FILE_COUNT + backup.getFileDescriptor().length);
80  			this.getTotalFileSizeLabel().setText(
81  					TOTAL_FILE_SIZE + totalSize + " Kb");
82  			this.getAvgFileSizeLabel().setText(
83  					AVG_FILE_SIZE
84  							+ (totalSize / backup.getFileDescriptor().length)
85  							+ " Kb");
86  		}
87  	}
88  
89  	/***
90  	 * @return Returns the avgFileSizeLabel.
91  	 */
92  	protected JLabel getAvgFileSizeLabel() {
93  		if (this.avgFileSizeLabel == null) {
94  			long totalSize = 0;
95  			for (int i = 0; i < backup.getFileDescriptor().length; i++)
96  				totalSize += backup.getFileDescriptor()[i].getSize();
97  			String labelText = AVG_FILE_SIZE;
98  			if (backup.getFileDescriptor().length > 0)
99  				labelText += (totalSize / backup.getFileDescriptor().length)
100 						+ " Kb";
101 
102 			this.avgFileSizeLabel = new JLabel(labelText);
103 
104 		}
105 		return avgFileSizeLabel;
106 	}
107 
108 	/***
109 	 * @return Returns the totalFileLabel.
110 	 */
111 	protected JLabel getTotalFileLabel() {
112 		if (this.totalFileLabel == null) {
113 			this.totalFileLabel = new JLabel(TOTAL_FILE_COUNT
114 					+ backup.getFileDescriptor().length);
115 		}
116 		return totalFileLabel;
117 	}
118 
119 	/***
120 	 * @return Returns the totalFileSizeLabel.
121 	 */
122 	protected JLabel getTotalFileSizeLabel() {
123 		if (this.totalFileSizeLabel == null) {
124 			long totalSize = 0;
125 			for (int i = 0; i < backup.getFileDescriptor().length; i++)
126 				totalSize += backup.getFileDescriptor()[i].getSize();
127 			this.totalFileSizeLabel = new JLabel(TOTAL_FILE_SIZE + totalSize
128 					+ " Kb");
129 		}
130 		return totalFileSizeLabel;
131 	}
132 
133 }