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 base.jdbs.ConfigurationManager; 32 import base.util.FileUtil; 33 34 /*** 35 * @author skunk 36 * 37 */ 38 @SuppressWarnings("serial") 39 public class RepositoryStatisticPanel extends JPanel implements Observer { 40 41 private final String USED_SPACE = "Used Space: "; 42 43 private final String DECLARED_AVAILABLE_SPACE = "Declared Available Space: "; 44 45 private final String HOSTED_ARTIFACT_COUNT = "Hosted Artifacts: "; 46 47 private final String AVERAGE_ARTIFACT_SIZE_COUNT = "Average Artifact Size: "; 48 49 private JLabel usedSpaceLabel; 50 51 private JLabel declaredAvailableSpaceLabel; 52 53 private JLabel hostedArtifactCountLabel; 54 55 private JLabel averageArtifactSizeLabel; 56 57 public RepositoryStatisticPanel() { 58 initialize(); 59 ConfigurationManager.getInstance().getRepository().addObserver(this); 60 } 61 62 protected void initialize() { 63 this.setBorder(new TitledBorder("Utilizzation Statistics")); 64 this.setLayout(new GridLayout(2, 2)); 65 this.add(this.getUsedSpaceLabel()); 66 this.add(this.getDeclaredAvailableSpaceLabel()); 67 this.add(this.getHostedArtifactCountLabel()); 68 this.add(this.getAverageArtifactSizeLabel()); 69 } 70 71 public void update(Observable arg0, Object arg1) { 72 this.getUsedSpaceLabel().setText( 73 USED_SPACE 74 + FileUtil.fileSizeInMB(ConfigurationManager 75 .getInstance().getRepository().getLocation()) 76 + " Mb"); 77 this.getDeclaredAvailableSpaceLabel().setText( 78 DECLARED_AVAILABLE_SPACE 79 + (ConfigurationManager.getInstance().getRepository() 80 .getDeclaredAvailableSpace() - FileUtil 81 .fileSizeInMB(ConfigurationManager 82 .getInstance().getRepository() 83 .getLocation())) + " Mb"); 84 this.getHostedArtifactCountLabel().setText( 85 HOSTED_ARTIFACT_COUNT 86 + ConfigurationManager.getInstance().getRepository() 87 .getBackupDescriptor().length); 88 String labelText = AVERAGE_ARTIFACT_SIZE_COUNT; 89 if (ConfigurationManager.getInstance().getRepository() 90 .getBackupDescriptor().length > 0) { 91 long size = 0; 92 for (int i = 0; i < ConfigurationManager.getInstance() 93 .getRepository().getBackupDescriptor().length; i++) 94 size += FileUtil.fileSizeInKB(ConfigurationManager 95 .getInstance().getRepository().getBackupDescriptor()[i] 96 .getBackupFile()); 97 labelText += (size / ConfigurationManager.getInstance() 98 .getRepository().getBackupDescriptor().length) 99 + " Kb"; 100 } 101 this.getAverageArtifactSizeLabel().setText(labelText); 102 } 103 104 /*** 105 * @return Returns the averageArtifactSizeLabel. 106 */ 107 protected JLabel getAverageArtifactSizeLabel() { 108 if (this.averageArtifactSizeLabel == null) { 109 String labelText = AVERAGE_ARTIFACT_SIZE_COUNT; 110 if (ConfigurationManager.getInstance().getRepository() 111 .getBackupDescriptor().length > 0) { 112 long size = 0; 113 for (int i = 0; i < ConfigurationManager.getInstance() 114 .getRepository().getBackupDescriptor().length; i++) 115 size += FileUtil.fileSizeInKB(ConfigurationManager 116 .getInstance().getRepository() 117 .getBackupDescriptor()[i].getBackupFile()); 118 labelText += (size / ConfigurationManager.getInstance() 119 .getRepository().getBackupDescriptor().length) 120 + " Kb"; 121 } 122 this.averageArtifactSizeLabel = new JLabel(labelText); 123 } 124 return averageArtifactSizeLabel; 125 } 126 127 /*** 128 * @return Returns the declaredAvailableSpaceLabel. 129 */ 130 protected JLabel getDeclaredAvailableSpaceLabel() { 131 if (this.declaredAvailableSpaceLabel == null) { 132 this.declaredAvailableSpaceLabel = new JLabel( 133 DECLARED_AVAILABLE_SPACE 134 + (ConfigurationManager.getInstance() 135 .getRepository() 136 .getDeclaredAvailableSpace() - FileUtil 137 .fileSizeInMB(ConfigurationManager 138 .getInstance().getRepository() 139 .getLocation())) + " Mb"); 140 } 141 return declaredAvailableSpaceLabel; 142 } 143 144 /*** 145 * @return Returns the hostedArtifactCountLabel. 146 */ 147 protected JLabel getHostedArtifactCountLabel() { 148 if (this.hostedArtifactCountLabel == null) { 149 this.hostedArtifactCountLabel = new JLabel(HOSTED_ARTIFACT_COUNT 150 + ConfigurationManager.getInstance().getRepository() 151 .getBackupDescriptor().length); 152 } 153 return hostedArtifactCountLabel; 154 } 155 156 /*** 157 * @return Returns the usedSpaceLabel. 158 */ 159 protected JLabel getUsedSpaceLabel() { 160 if (this.usedSpaceLabel == null) { 161 this.usedSpaceLabel = new JLabel(USED_SPACE 162 + FileUtil.fileSizeInMB(ConfigurationManager.getInstance() 163 .getRepository().getLocation()) + " Mb"); 164 } 165 return usedSpaceLabel; 166 } 167 }