JAL-2102 implemented improvement to round FTS's numberic data columns to a configurab...
[jalview.git] / src / jalview / fts / core / DecimalFormatTableCellRenderer.java
1 package jalview.fts.core;
2
3 import java.awt.Component;
4 import java.text.DecimalFormat;
5
6 import javax.swing.JLabel;
7 import javax.swing.JTable;
8 import javax.swing.table.DefaultTableCellRenderer;
9
10 /**
11  * The class to handle the formatting of the double values for JTable cells.
12  */
13 public class DecimalFormatTableCellRenderer extends
14         DefaultTableCellRenderer
15 {
16   private DecimalFormat formatter;
17
18   public DecimalFormatTableCellRenderer(boolean isFormated,
19           int significantFigures)
20   {
21     String integerFormater = isFormated ? "###,##0" : "0";
22     String fractionFormater = isFormated ? "###,##0." : "0.";
23     if (significantFigures > 0)
24     {
25       StringBuilder significantFigureBuilder = new StringBuilder();
26       for (int x = 1; x <= significantFigures; ++x)
27       {
28         significantFigureBuilder.append("0");
29       }
30       formatter = new DecimalFormat(fractionFormater
31               + significantFigureBuilder.toString());
32     }
33     else
34     {
35       formatter = new DecimalFormat(integerFormater);
36     }
37     super.setHorizontalAlignment(JLabel.RIGHT);
38   }
39
40   public DecimalFormatTableCellRenderer()
41   {
42     super.setHorizontalAlignment(JLabel.RIGHT);
43   }
44
45   @Override
46   public Component getTableCellRendererComponent(JTable table,
47           Object value, boolean isSelected, boolean hasFocus, int row,
48           int column)
49   {
50     if (value == null)
51     {
52       return null;
53     }
54
55     value = formatter.format(value);
56
57     return super.getTableCellRendererComponent(table, value, isSelected,
58             hasFocus, row, column);
59   }
60 }