r3259 - trunk/lima-swing/src/main/java/org/chorem/lima/ui/celleditor
Author: vsalaun Date: 2011-08-11 11:23:09 +0200 (Thu, 11 Aug 2011) New Revision: 3259 Url: http://chorem.org/repositories/revision/lima/3259 Log: update BigDecimal editor Modified: trunk/lima-swing/src/main/java/org/chorem/lima/ui/celleditor/BigDecimalTableCellEditor.java Modified: trunk/lima-swing/src/main/java/org/chorem/lima/ui/celleditor/BigDecimalTableCellEditor.java =================================================================== --- trunk/lima-swing/src/main/java/org/chorem/lima/ui/celleditor/BigDecimalTableCellEditor.java 2011-08-10 13:27:34 UTC (rev 3258) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/celleditor/BigDecimalTableCellEditor.java 2011-08-11 09:23:09 UTC (rev 3259) @@ -45,6 +45,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.chorem.lima.LimaContext; +import org.chorem.lima.business.LimaException; public class BigDecimalTableCellEditor extends AbstractCellEditor implements TableCellEditor, FocusListener, AncestorListener, KeyListener { @@ -81,8 +82,17 @@ textValue = textValue.replaceAll(",", "."); if (textValue.equals("")) { return BigDecimal.ZERO; - } else { - BigDecimal bdValue = new BigDecimal(textValue); + } else { + //set 0 in case the user type in a non number format + BigDecimal bdValue = BigDecimal.ZERO; + try { + bdValue = new BigDecimal(textValue); + } catch (NumberFormatException eee) { + if (log.isErrorEnabled()) { + log.error("Can't set " + textValue + " to a BigDecimal", eee); + } + } + //round half up the number using the scale given by the configuration bdValue = bdValue.setScale(LimaContext.getContext().getConfig().getScale(), BigDecimal.ROUND_HALF_UP); return bdValue; @@ -138,21 +148,20 @@ } @Override - public void keyReleased(KeyEvent e) { - if (keyPressed == false) { - if ((e.getKeyCode() >= KeyEvent.VK_0 - && e.getKeyCode() <= KeyEvent.VK_9) - || e.getKeyCode() == KeyEvent.VK_COMMA - || e.getKeyCode() == KeyEvent.VK_PERIOD) { - //check if the TextField is selectAll - if (textField.getSelectedText().equals(textField.getText())) { - textField.setText(String.valueOf(e.getKeyChar())); - } - keyPressed = true; - } else if (e.getKeyChar() == KeyEvent.VK_BACK_SPACE){ - textField.setText(""); - keyPressed = true; - } + public void keyReleased(KeyEvent e) { + // replace all the cell content only if all the text has been selected + if (keyPressed == false + && textField.getSelectionStart() == 0 + && textField.getSelectionEnd() == textField.getText().length()) { + // delete the the cell content + if (e.getKeyChar() == KeyEvent.VK_BACK_SPACE) { + textField.setText(""); + keyPressed = true; + // replace the content by the char typed in + } else if (String.valueOf(e.getKeyChar()).matches("[a-zA-z0-9]")) { + textField.setText(String.valueOf(e.getKeyChar())); + keyPressed = true; + } } }
participants (1)
-
vsalaun@users.chorem.org