/*
** 2012 June 7
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
** May you do good and not evil.
** May you find forgiveness for yourself and forgive others.
** May you share freely, never taking more than you give.
*/
package info.ata4.util.gui.components;
import java.awt.Component;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
/**
*
* @author Nico Bergemann <barracuda415 at yahoo.de>
*/
public class ByteSizeCellRenderer extends DefaultTableCellRenderer {
private boolean si;
public ByteSizeCellRenderer(boolean si) {
this.si = si;
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if (value instanceof Long) {
value = humanReadableByteCount((Long) value);
} else if (value instanceof Integer) {
value = humanReadableByteCount((Integer) value);
}
JLabel c = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
c.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
return c;
}
private String humanReadableByteCount(long bytes) {
int unit = si ? 1000 : 1024;
if (bytes < unit) {
return bytes + " B";
}
int exp = (int) (Math.log(bytes) / Math.log(unit));
String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp-1) + (si ? "" : "i");
return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
}
}