1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
34
35
36
37
38
39 public class SizeRenderer extends DefaultTableCellRenderer {
40
41
42 private static final long serialVersionUID = -2029129064667754146L;
43
44
45
46
47 private UploadPolicy uploadPolicy = null;
48
49
50 private static final double gB = 1024L * 1024L * 1024L;
51
52
53 private static final double mB = 1024L * 1024L;
54
55
56 private static final double kB = 1024L;
57
58
59
60
61
62
63
64
65 public SizeRenderer(UploadPolicy uploadPolicy) {
66 super();
67 this.uploadPolicy = uploadPolicy;
68 }
69
70
71
72
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
86 this.uploadPolicy
87 .displayWarn("value is not an instance of Long, in SizeRenderer.getTableCellRendererComponent(");
88 }
89 return cell;
90 }
91
92
93
94
95
96
97
98
99
100
101
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
121 String value = String.format("%1$,3.2f %2$s", fileUploadSpeed, unit);
122
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
134
135
136
137
138
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 }