001 package org.shiftone.jrat.ui.util;
002
003
004 import org.shiftone.jrat.util.Percent;
005 import org.shiftone.jrat.util.log.Logger;
006
007 import javax.swing.*;
008 import javax.swing.table.DefaultTableCellRenderer;
009 import java.awt.*;
010 import java.text.DecimalFormat;
011
012
013 /**
014 * @author jeff@shiftone.org (Jeff Drost)
015 */
016 public class PercentTableCellRenderer extends DefaultTableCellRenderer {
017
018 private static final Logger LOG = Logger.getLogger(PercentTableCellRenderer.class);
019 private Object value = null;
020 private DecimalFormat floatDecimalFormat = new DecimalFormat("#,##0.00");
021 private DecimalFormat doubleDecimalFormat = new DecimalFormat("#,##0.00");
022 private DecimalFormat longDecimalFormat = new DecimalFormat("###,###,###");
023 private static final Color COLOR_XOR = Color.LIGHT_GRAY;
024
025 public PercentTableCellRenderer() {
026 super();
027 }
028
029
030 public static void setDefaultRenderer(JTable table) {
031
032 table.setDefaultRenderer(Object.class, new PercentTableCellRenderer());
033 table.setDefaultRenderer(Percent.class, new PercentTableCellRenderer());
034 table.setDefaultRenderer(Number.class, new PercentTableCellRenderer());
035 table.setDefaultRenderer(Integer.class, new PercentTableCellRenderer());
036 table.setDefaultRenderer(Long.class, new PercentTableCellRenderer());
037 table.setDefaultRenderer(Double.class, new PercentTableCellRenderer());
038 table.setDefaultRenderer(Float.class, new PercentTableCellRenderer());
039 }
040
041
042 /**
043 * Method getTableCellRendererComponent
044 */
045 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
046 int row, int column) {
047
048 this.value = value;
049
050 if (value instanceof Number) {
051 setHorizontalAlignment(JLabel.RIGHT);
052 } else {
053 setHorizontalAlignment(JLabel.LEFT);
054 }
055
056 return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
057
058 // return this;
059 }
060
061
062 /**
063 * Method paint
064 */
065 public void paint(Graphics g) {
066
067 super.paint(g);
068
069 if (value instanceof Percent) {
070 double pct = ((Percent) value).doubleValue();
071
072 if (pct > 100.0) {
073 pct = 100.0;
074 }
075
076 int w = (int) ((getWidth() * pct) / 100.0);
077
078 g.setXORMode(COLOR_XOR);
079 g.fillRect(0, 0, w, getHeight());
080 }
081 }
082
083
084 /**
085 * Method setValue. synchronized because DecimalFormat is not thread safe
086 */
087 protected synchronized void setValue(Object value) {
088
089 if (value == null) {
090 value = "";
091 } else if (value instanceof Number) {
092 Number num = (Number) value;
093
094 if ((value instanceof Integer) || (value instanceof Long)) {
095 value = longDecimalFormat.format(num);
096 } else if (value instanceof Float) {
097 Float f = (Float) value;
098
099 if (f.isNaN() || f.isInfinite()) {
100 value = String.valueOf(f);
101 } else {
102 value = floatDecimalFormat.format(num);
103 }
104 } else if (value instanceof Double) {
105 Double d = (Double) value;
106
107 if (d.isNaN() || d.isInfinite()) {
108 value = String.valueOf(d);
109 } else {
110 value = doubleDecimalFormat.format(num);
111 }
112 }
113 }
114
115 super.setValue(value);
116 }
117 }