Author: sletellier Date: 2012-06-27 13:46:23 +0200 (Wed, 27 Jun 2012) New Revision: 3481 Url: http://chorem.org/repositories/revision/lima/3481 Log: Push NumberCellEditor to JaxxWidget Removed: trunk/lima-swing/src/main/java/org/chorem/lima/ui/celleditor/BigDecimalTableCellEditor.java Modified: trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransaction/FinancialTransactionTable.java trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransaction/FinancialTransactionTableModel.java Deleted: 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-06-27 10:19:17 UTC (rev 3480) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/celleditor/BigDecimalTableCellEditor.java 2012-06-27 11:46:23 UTC (rev 3481) @@ -1,151 +0,0 @@ -/* - * #%L - * Lima :: Swing - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2008 - 2011 CodeLutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/gpl-3.0.html>. - * #L% - */ -package org.chorem.lima.ui.celleditor; - -import java.awt.Component; -import java.awt.event.FocusEvent; -import java.awt.event.FocusListener; -import java.math.BigDecimal; -import javax.swing.AbstractCellEditor; -import javax.swing.JTable; -import javax.swing.SwingConstants; -import javax.swing.SwingUtilities; -import javax.swing.event.AncestorEvent; -import javax.swing.event.AncestorListener; -import javax.swing.table.TableCellEditor; -import jaxx.runtime.swing.editor.NumberEditor; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.chorem.lima.LimaContext; - -public class BigDecimalTableCellEditor extends AbstractCellEditor - implements TableCellEditor, FocusListener, AncestorListener { - - private static final Log log = LogFactory.getLog(BigDecimalTableCellEditor.class); - - private static final long serialVersionUID = 1L; - - protected int row; - - protected final NumberEditor numberEditor; - - /** constructor */ - public BigDecimalTableCellEditor() { - numberEditor = new NumberEditor(); - numberEditor.getTextField().setHorizontalAlignment(SwingConstants.RIGHT); - numberEditor.getTextField().setBorder(null); - numberEditor.getTextField().addAncestorListener(this); - numberEditor.getTextField().addFocusListener(this); - - numberEditor.setModelType(BigDecimal.class); - numberEditor.setModel(new BigDecimal(0)); -// numberEditor.setUseFloat(true); - numberEditor.setUseSign(false); - numberEditor.init(); - } - - public int getRow() { - return row; - } - - @Override - public Component getTableCellEditorComponent(JTable table, Object value, - boolean isSelected, int row, int column) { - - this.row = row; - - // FIXME sletellier 20120105 : we dont need to set modelText - BigDecimal bd = (BigDecimal) value; - numberEditor.setModel(bd); - numberEditor.setModelText(bd.toPlainString()); - return numberEditor; - } - - // the editor should return a BigDecimal - public Object getCellEditorValue() { - BigDecimal bdValue = (BigDecimal)numberEditor.getModel(); - - //replace all comma by full stop -// textValue = textValue.replaceAll(",", "."); -// if (textValue.equals("")) { -// return BigDecimal.ZERO; -// } else { - //set 0 in case the user type in a non number format - if (bdValue == null) { - bdValue = BigDecimal.ZERO; - } -// try { -// BigDecimal bdValue = new BigDecimal(f); -// } 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; -// } - } - -// @Override -// public boolean isCellEditable(EventObject evt) { -// return !(evt instanceof MouseEvent) || ((MouseEvent) evt).getClickCount() == 2; -// } -// -// /** Listeners */ - @Override - public void focusGained(FocusEvent e) { - SwingUtilities.invokeLater(new Runnable() { - public void run() { - numberEditor.getTextField().requestFocus(); - numberEditor.getTextField().selectAll(); - } - }); - } - - @Override - public void focusLost(FocusEvent e) { - } - - @Override - public void ancestorAdded(AncestorEvent e) { - SwingUtilities.invokeLater(new Runnable() { - public void run() { - numberEditor.getTextField().requestFocus(); - numberEditor.getTextField().selectAll(); - } - }); - } - - @Override - public void ancestorRemoved(AncestorEvent e) { - } - - @Override - public void ancestorMoved(AncestorEvent e) { - } - -} Modified: trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransaction/FinancialTransactionTable.java =================================================================== --- trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransaction/FinancialTransactionTable.java 2012-06-27 10:19:17 UTC (rev 3480) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransaction/FinancialTransactionTable.java 2012-06-27 11:46:23 UTC (rev 3481) @@ -95,6 +95,8 @@ //Get new amount editor BigDecimalTableCellEditor editor = new BigDecimalTableCellEditor(); + + // FIXME sletellier 27/06/12 : hack to fix selection of first row on stopEditing cell in last row editor.addCellEditorListener(new CellEditorListener() { @Override Modified: trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransaction/FinancialTransactionTableModel.java =================================================================== --- trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransaction/FinancialTransactionTableModel.java 2012-06-27 10:19:17 UTC (rev 3480) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransaction/FinancialTransactionTableModel.java 2012-06-27 11:46:23 UTC (rev 3481) @@ -29,6 +29,7 @@ import java.util.Date; import java.util.List; import javax.swing.table.AbstractTableModel; +import org.chorem.lima.LimaContext; import org.chorem.lima.business.api.FinancialTransactionService; import org.chorem.lima.entity.Account; import org.chorem.lima.entity.Entry; @@ -386,11 +387,13 @@ currentEntry.setLettering((String) value); break; case 6: - currentEntry.setAmount((BigDecimal) value); + BigDecimal bigDecimalValue = getBigDecimalValue(value); + currentEntry.setAmount(bigDecimalValue); currentEntry.setDebit(true); break; case 7: - currentEntry.setAmount((BigDecimal) value); + bigDecimalValue = getBigDecimalValue(value); + currentEntry.setAmount(bigDecimalValue); currentEntry.setDebit(false); break; } @@ -417,6 +420,22 @@ fireTableRowsUpdated(trIndex, endIndex); } + protected BigDecimal getBigDecimalValue(Object value) { + + BigDecimal result = (BigDecimal)value; + + //set 0 in case the user type in a non number format + if (result == null) { + result = BigDecimal.ZERO; + } + + //round half up the number using the scale given by the configuration + result = result.setScale(LimaContext.getContext().getConfig().getScale(), + BigDecimal.ROUND_HALF_UP); + + return result; + } + public Object getElementAt(int row) { Object result = transactionAndEntries.get(row); return result;
participants (1)
-
sletellier@users.chorem.org