View Javadoc
1   //
2   // $Id: JUploadApplet.java 88 2007-05-02 00:04:52Z
3   // /C=DE/ST=Baden-Wuerttemberg/O=ISDN4Linux/OU=Fritz
4   // Elfert/CN=svn-felfert@isdn4linux.de/emailAddress=fritz@fritz-elfert.de $
5   //
6   // jupload - A file upload applet.
7   // Copyright 2007 The JUpload Team
8   //
9   // Created: 2007-04-28
10  // Creator: felfert
11  // Last modified: $Date$
12  //
13  // This program is free software; you can redistribute it and/or modify it under
14  // the terms of the GNU General Public License as published by the Free Software
15  // Foundation; either version 2 of the License, or (at your option) any later
16  // version. This program is distributed in the hope that it will be useful, but
17  // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
19  // details. You should have received a copy of the GNU General Public License
20  // along with this program; if not, write to the Free Software Foundation, Inc.,
21  // 675 Mass Ave, Cambridge, MA 02139, USA.
22  
23  package wjhk.jupload2.gui.filepanel;
24  
25  import java.awt.Component;
26  
27  import javax.swing.JTable;
28  import javax.swing.table.DefaultTableCellRenderer;
29  
30  import wjhk.jupload2.policies.UploadPolicy;
31  
32  /**
33   * Technical class, used to display file sizes. Used in
34   * {@link wjhk.jupload2.gui.filepanel.FilePanelJFlatTable}.
35   * 
36   * @author felfert
37   * @version $Revision$
38   */
39  public class SizeRenderer extends DefaultTableCellRenderer {
40  
41  	/** A generated serialVersionUID, to avoid warning during compilation */
42  	private static final long serialVersionUID = -2029129064667754146L;
43  
44  	/**
45  	 * The current upload policy
46  	 */
47  	private UploadPolicy uploadPolicy = null;
48  
49  	/** Size of one gigabyte, for file size display */
50  	private static final double gB = 1024L * 1024L * 1024L;
51  
52  	/** Size of one megabyte, for file size display */
53  	private static final double mB = 1024L * 1024L;
54  
55  	/** Size of one kilobyte, for file size display */
56  	private static final double kB = 1024L;
57  
58  	/**
59  	 * Creates a new instance.
60  	 * 
61  	 * @param uploadPolicy
62  	 *            The policy to be used for providing the translated unit
63  	 *            strings.
64  	 */
65  	public SizeRenderer(UploadPolicy uploadPolicy) {
66  		super();
67  		this.uploadPolicy = uploadPolicy;
68  	}
69  
70  	/**
71  	 * @see javax.swing.table.DefaultTableCellRenderer#getTableCellRendererComponent(javax.swing.JTable,
72  	 *      java.lang.Object, boolean, boolean, int, int)
73  	 */
74  	@Override
75  	public Component getTableCellRendererComponent(JTable table, Object value,
76  			boolean isSelected, boolean hasFocus, int row, int column) {
77  		Component cell = super.getTableCellRendererComponent(table, value,
78  				isSelected, hasFocus, row, column);
79  
80  		if (value instanceof Long) {
81  			setValue(formatFileSize(((Long) value).longValue(),
82  					this.uploadPolicy));
83  			super.setHorizontalAlignment(RIGHT);
84  		} else if (value != null) {
85  			// We have a value, but it's not a Long.
86  			this.uploadPolicy
87  					.displayWarn("value is not an instance of Long, in SizeRenderer.getTableCellRendererComponent(");
88  		}
89  		return cell;
90  	}
91  
92  	// ////////////////////////////////////////////////////////////////////////////
93  	// Various utilities for file size calculation
94  	// ////////////////////////////////////////////////////////////////////////////
95  
96  	/**
97  	 * Format a number of bytes into a well formatted string, like 122mB.
98  	 * 
99  	 * @param fileUploadSpeedParam
100 	 * @param uploadPolicy
101 	 * @return The formatted file upload speed, to be displayed to the user
102 	 */
103 	public static String formatFileUploadSpeed(double fileUploadSpeedParam,
104 			UploadPolicy uploadPolicy) {
105 		String unit;
106 		double fileUploadSpeed = fileUploadSpeedParam;
107 		if (fileUploadSpeed >= gB) {
108 			fileUploadSpeed /= gB;
109 			unit = uploadPolicy.getLocalizedString("speedunit_gb_per_second");
110 		} else if (fileUploadSpeed >= mB) {
111 			fileUploadSpeed /= mB;
112 			unit = uploadPolicy.getLocalizedString("speedunit_mb_per_second");
113 		} else if (fileUploadSpeed >= kB) {
114 			fileUploadSpeed /= kB;
115 			unit = uploadPolicy.getLocalizedString("speedunit_kb_per_second");
116 		} else {
117 			unit = uploadPolicy.getLocalizedString("speedunit_b_per_second");
118 		}
119 
120 		// TODO Use local as the first argument.
121 		String value = String.format("%1$,3.2f %2$s", fileUploadSpeed, unit);
122 		// TODO remove this temp log
123 		if (uploadPolicy.getDebugLevel() >= 80 && value.contains("Infinity")) {
124 			uploadPolicy.displayDebug("'" + value
125 					+ "' for values: fileUploadSpeed=" + fileUploadSpeed
126 					+ " and unit=" + unit, 80);
127 		}
128 		return value;
129 
130 	}
131 
132 	/**
133 	 * Format a number of bytes of a file size (or a number of uploaded bytes,
134 	 * or whatever), into a well formatted string, like 122mB.
135 	 * 
136 	 * @param fileSize
137 	 * @param uploadPolicy
138 	 * @return The formatted file size, to display to the user.
139 	 */
140 	public static String formatFileSize(double fileSize,
141 			UploadPolicy uploadPolicy) {
142 		final String sizeunit_gigabytes = uploadPolicy
143 				.getLocalizedString("unitGigabytes");
144 		final String sizeunit_megabytes = uploadPolicy
145 				.getLocalizedString("unitMegabytes");
146 		final String sizeunit_kilobytes = uploadPolicy
147 				.getLocalizedString("unitKilobytes");
148 		final String sizeunit_bytes = uploadPolicy
149 				.getLocalizedString("unitBytes");
150 		String unit;
151 
152 		double fileSizeToDisplay = fileSize;
153 		if (fileSizeToDisplay >= gB) {
154 			fileSizeToDisplay /= gB;
155 			unit = sizeunit_gigabytes;
156 		} else if (fileSizeToDisplay >= mB) {
157 			fileSizeToDisplay /= mB;
158 			unit = sizeunit_megabytes;
159 		} else if (fileSizeToDisplay >= kB) {
160 			fileSizeToDisplay /= kB;
161 			unit = sizeunit_kilobytes;
162 		} else {
163 			unit = sizeunit_bytes;
164 		}
165 
166 		return String.format("%1$,3.2f %2$s", fileSizeToDisplay, unit);
167 	}
168 
169 }