Author: mallon Date: 2012-08-08 12:12:31 +0200 (Wed, 08 Aug 2012) New Revision: 3573 Url: http://chorem.org/repositories/revision/lima/3573 Log: refs #705 Correction sur l editeur, concernant la gestion de la virgule. 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 2012-08-07 16:58:51 UTC (rev 3572) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/celleditor/BigDecimalTableCellEditor.java 2012-08-08 10:12:31 UTC (rev 3573) @@ -30,13 +30,18 @@ import javax.swing.DefaultCellEditor; import javax.swing.JTextField; +import javax.swing.SwingConstants; import javax.swing.SwingUtilities; import javax.swing.event.AncestorEvent; import javax.swing.event.AncestorListener; import java.awt.event.FocusEvent; import java.awt.event.FocusListener; +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; import java.math.BigDecimal; import java.math.RoundingMode; +import java.text.DecimalFormat; +import java.text.ParsePosition; /** * @author sletellier <letellier@codelutin.com> @@ -45,12 +50,30 @@ public class BigDecimalTableCellEditor extends DefaultCellEditor { protected int row; + protected String comma = ""; private static final Log log = LogFactory.getLog(BigDecimalTableCellEditor.class); public BigDecimalTableCellEditor() { super(new JTextField()); setClickCountToStart(1); + getComponent().setHorizontalAlignment(SwingConstants.RIGHT); + getComponent().addKeyListener(new KeyListener() { + @Override + public void keyTyped(KeyEvent e) { + // nothing to do + } + + @Override + public void keyPressed(KeyEvent e) { + // nothing to do + } + + @Override + public void keyReleased(KeyEvent e) { + limitComma(e); + } + }); getComponent().addFocusListener(new FocusListener() { @Override @@ -87,6 +110,9 @@ return (JTextField) super.getComponent(); } + /** + * Two kinds of edition, with LimaConfig value + * */ public void runEdition() { String comportmentEditingCell = LimaConfig.getInstance().getComportmentEditingCell(); if (comportmentEditingCell.equals("ALL")) { @@ -97,17 +123,36 @@ } } + /** + * Round the value to higher, and the number of decimal to 2; + * Parse string value to BigDecimal when it contains comma + * @return bigDecimal value + * */ @Override public BigDecimal getCellEditorValue() { String editorStringValue = super.getCellEditorValue().toString(); if (StringUtils.isBlank(editorStringValue)) { editorStringValue = "0"; } - BigDecimal cellEditorValue = (BigDecimal.valueOf(Double.valueOf(editorStringValue))); + BigDecimal cellEditorValue; + DecimalFormat decimalFormat = new DecimalFormat(); + cellEditorValue = new BigDecimal(decimalFormat.parse(editorStringValue,new ParsePosition(0)).doubleValue()); if (super.getCellEditorValue() != BigDecimal.ZERO) { return cellEditorValue.setScale(2, RoundingMode.HALF_UP); } return cellEditorValue; } + /** + * Limit number of comma to one + * @param e keyEvent starting control of comma + * */ + protected void limitComma(KeyEvent e) { + if ( (String.valueOf(e.getKeyChar()).equals(",") && comma.equals(","))) { + getComponent().setText(getComponent().getText().substring(0, getComponent().getText().length()-1)); + } else if (String.valueOf(e.getKeyChar()).equals(",") && !comma.equals(",")) { + comma = ","; + } + } + }