PageRenderTime 38ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mpv5/utils/renderer/TableCellEditorForDezimal.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 122 lines | 75 code | 12 blank | 35 comment | 15 complexity | 108687b4d44544819800d2d509c9f2d1 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.event.MouseEvent;
  18. import java.math.BigDecimal;
  19. import java.text.NumberFormat;
  20. import java.util.EventObject;
  21. import javax.swing.JFormattedTextField;
  22. import javax.swing.SwingConstants;
  23. import javax.swing.SwingUtilities;
  24. import mpv5.logging.Log;
  25. import mpv5.utils.numberformat.FormatNumber;
  26. /**
  27. *
  28. *
  29. */
  30. public class TableCellEditorForDezimal extends LazyCellEditor {
  31. private static final long serialVersionUID = 1L;
  32. /**
  33. *
  34. * @param tf
  35. */
  36. public TableCellEditorForDezimal(final JFormattedTextField tf) {
  37. this(tf, null);
  38. }
  39. /**
  40. *
  41. * @param tf
  42. * @param format
  43. */
  44. public TableCellEditorForDezimal(final JFormattedTextField tf, final NumberFormat format) {
  45. super(tf);
  46. super.setClickCountToStart(1);
  47. tf.setFocusLostBehavior(JFormattedTextField.COMMIT);
  48. tf.setHorizontalAlignment(SwingConstants.LEFT);
  49. tf.setBorder(null);
  50. delegate = new EditorDelegate() {
  51. boolean isMousePressed = false;
  52. @Override
  53. public void setValue(Object param) {
  54. if (isMousePressed
  55. && param != null && (param.getClass() == Double.class || param.getClass() == BigDecimal.class)) {
  56. SwingUtilities.invokeLater(new Runnable() {
  57. public void run() {
  58. tf.selectAll();
  59. }
  60. });
  61. try {
  62. tf.setText(format == null ? FormatNumber.formatDezimal((Number) param) : format.format((Number) param));
  63. } catch (Exception e) {
  64. try {
  65. param = new BigDecimal(String.valueOf(param));
  66. tf.setText(format == null ? FormatNumber.formatDezimal((Number) param) : format.format((Number) param));
  67. } catch (Exception ex) {
  68. tf.setText(String.valueOf(param));
  69. }
  70. }
  71. } else {
  72. tf.setText("");
  73. }
  74. }
  75. @Override
  76. public Object getCellEditorValue() {
  77. try {
  78. String _field = tf.getText();
  79. Number _number = (format == null ? FormatNumber.parseDezimal(_field) : format.parse(_field));
  80. if (_number != null) {
  81. double _parsed = _number.doubleValue();
  82. Double d = new Double(_parsed);
  83. // tf.setBackground(Color.white);
  84. return d;
  85. } else {
  86. // tf.setBackground(Color.white);
  87. return new Double(0.0);
  88. }
  89. } catch (Exception e) {
  90. Log.Debug(this, e.getMessage());
  91. // tf.setBackground(Color.red);
  92. return new Double(0.0);
  93. }
  94. }
  95. @Override
  96. public boolean isCellEditable(EventObject anEvent) {
  97. if (anEvent instanceof MouseEvent) {
  98. isMousePressed = true;
  99. return ((MouseEvent) anEvent).getClickCount() >= clickCountToStart;
  100. }
  101. isMousePressed = false;
  102. return true;
  103. }
  104. };
  105. }
  106. }