PageRenderTime 34ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mpv5/utils/renderer/TableCellRendererForDezimal.java

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