/src/mpv5/utils/renderer/TableCellRendererForDezimal.java
Java | 121 lines | 73 code | 11 blank | 37 comment | 8 complexity | dd5f784c00017d48ec30478d41bcefd9 MD5 | raw file
1/* 2 * 3This file is part of YaBS. 4 5YaBS is free software: you can redistribute it and/or modify 6it under the terms of the GNU General Public License as published by 7the Free Software Foundation, either version 3 of the License, or 8(at your option) any later version. 9 10YaBS is distributed in the hope that it will be useful, 11but WITHOUT ANY WARRANTY; without even the implied warranty of 12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13GNU General Public License for more details. 14 15You should have received a copy of the GNU General Public License 16along with YaBS. If not, see <http://www.gnu.org/licenses/>. 17 18 * 19 */ 20package mpv5.utils.renderer; 21 22import java.awt.Color; 23import java.awt.Component; 24import java.text.DecimalFormat; 25import java.text.NumberFormat; 26import javax.swing.JFormattedTextField; 27import javax.swing.JTable; 28import javax.swing.table.DefaultTableCellRenderer; 29import javax.swing.table.TableColumn; 30import mpv5.globals.GlobalSettings; 31import mpv5.logging.Log; 32import mpv5.utils.numberformat.FormatNumber; 33 34public class TableCellRendererForDezimal extends DefaultTableCellRenderer { 35 36 private static final long serialVersionUID = 1L; 37 private final DefaultTableCellRenderer adaptee = new DefaultTableCellRenderer(); 38 private final JTable t; 39 private Color color; 40 private NumberFormat format = FormatNumber.getShortDecimalFormat(); 41 42 /** 43 * 44 * @param t 45 * @param color Not used yet 46 */ 47 public TableCellRendererForDezimal(JTable t, Color color) { 48 this(t); 49 this.color = color; 50 } 51 52 /** 53 * 54 * @param t 55 * @param pattern Overriding FormatNumber.getShortDecimalFormat() and GlobalSettings.getProperty("table.decimal.format") 56 */ 57 public TableCellRendererForDezimal(JTable t, String pattern) { 58 this(t); 59 if (pattern != null && pattern.length() != 0) { 60 try { 61 this.format = new DecimalFormat(pattern); 62 } catch (Exception e) { 63 Log.Debug(e); 64 } 65 } 66 } 67 68 /** 69 * Set this renderer to the given column + editor 70 * @param column 71 */ 72 public void setRendererTo(int column) { 73 74 TableColumn col = t.getColumnModel().getColumn(t.getColumnModel().getColumnIndex(t.getModel().getColumnName(column))); 75 col.setCellEditor(new TableCellEditorForDezimal(new JFormattedTextField(), format)); 76 col.setCellRenderer(this); 77 } 78 79 /** 80 * 81 * @param t 82 */ 83 public TableCellRendererForDezimal(JTable t) { 84 super(); 85 this.t = t; 86 87 if (GlobalSettings.hasProperty("table.decimal.format")) { 88 try { 89 this.format = new DecimalFormat(GlobalSettings.getProperty("table.decimal.format")); 90 } catch (Exception e) { 91 Log.Debug(e); 92 } 93 } 94 } 95 96 @Override 97 protected void setValue(Object value) { 98 if (value instanceof Number) { 99 value = format.format(value); 100 } 101 super.setValue(value); 102 } 103 104 @Override 105 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) { 106 if (value instanceof Number) { 107 value = format.format(value); 108 } 109 adaptee.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col); 110 setForeground(adaptee.getForeground()); 111 setBackground(adaptee.getBackground()); 112 setBorder(adaptee.getBorder()); 113 setFont(adaptee.getFont()); 114 setText(adaptee.getText()); 115 if (hasFocus) { 116 setBackground(Color.BLUE); 117 setForeground(Color.WHITE); 118 } 119 return this; 120 } 121}