r3479 - in trunk/lima-swing/src/main/java/org/chorem/lima/ui: celleditor financialtransaction fiscalperiod
Author: sletellier Date: 2012-06-27 12:16:23 +0200 (Wed, 27 Jun 2012) New Revision: 3479 Url: http://chorem.org/repositories/revision/lima/3479 Log: - Fix number edition using NumberEditor - Partial fix selection of first row on stopEditing cell in last row on FinancialTransactionTable Modified: trunk/lima-swing/src/main/java/org/chorem/lima/ui/celleditor/BigDecimalTableCellEditor.java 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 trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransaction/FinancialTransactionViewHandler.java trunk/lima-swing/src/main/java/org/chorem/lima/ui/fiscalperiod/FiscalPeriodView.jaxx 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-06-26 16:10:22 UTC (rev 3478) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/celleditor/BigDecimalTableCellEditor.java 2012-06-27 10:16:23 UTC (rev 3479) @@ -24,105 +24,118 @@ */ package org.chorem.lima.ui.celleditor; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.chorem.lima.LimaContext; - +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.JTextField; import javax.swing.SwingConstants; import javax.swing.SwingUtilities; import javax.swing.event.AncestorEvent; import javax.swing.event.AncestorListener; import javax.swing.table.TableCellEditor; -import java.awt.Component; -import java.awt.event.FocusEvent; -import java.awt.event.FocusListener; -import java.awt.event.KeyEvent; -import java.awt.event.KeyListener; -import java.awt.event.MouseEvent; -import java.math.BigDecimal; -import java.util.EventObject; +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, KeyListener { + implements TableCellEditor, FocusListener, AncestorListener { private static final Log log = LogFactory.getLog(BigDecimalTableCellEditor.class); private static final long serialVersionUID = 1L; - protected final JTextField textField; + protected int row; - private boolean keyPressed; + protected final NumberEditor numberEditor; /** constructor */ public BigDecimalTableCellEditor() { - textField = new JTextField(); - textField.setEditable(true); - textField.setHorizontalAlignment(SwingConstants.RIGHT); - textField.addFocusListener(this); - textField.addAncestorListener(this); - textField.addKeyListener(this); - keyPressed = false; + 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) { - textField.setText(value.toString()); - return textField; + + 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() { - String textValue = textField.getText().trim(); + BigDecimal bdValue = (BigDecimal)numberEditor.getModel(); + //replace all comma by full stop - textValue = textValue.replaceAll(",", "."); - if (textValue.equals("")) { - return BigDecimal.ZERO; - } else { +// textValue = textValue.replaceAll(",", "."); +// if (textValue.equals("")) { +// return BigDecimal.ZERO; +// } 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); - } + 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 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() { - textField.selectAll(); + numberEditor.getTextField().requestFocus(); + numberEditor.getTextField().selectAll(); } }); } @Override public void focusLost(FocusEvent e) { - keyPressed = false; } @Override public void ancestorAdded(AncestorEvent e) { SwingUtilities.invokeLater(new Runnable() { public void run() { - textField.requestFocus(); + numberEditor.getTextField().requestFocus(); + numberEditor.getTextField().selectAll(); } }); } @@ -135,31 +148,4 @@ public void ancestorMoved(AncestorEvent e) { } - @Override - public void keyTyped(KeyEvent e) { - - } - - @Override - public void keyPressed(KeyEvent e) { - } - - @Override - public void keyReleased(KeyEvent e) { - // replace all the cell content only if all the text has been selected - if (!keyPressed - && 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; - } - } - } - } 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-26 16:10:22 UTC (rev 3478) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransaction/FinancialTransactionTable.java 2012-06-27 10:16:23 UTC (rev 3479) @@ -25,6 +25,18 @@ package org.chorem.lima.ui.financialtransaction; +import java.awt.Color; +import java.awt.Component; +import java.awt.event.KeyAdapter; +import java.awt.event.KeyEvent; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.math.BigDecimal; +import java.util.Date; +import javax.swing.SwingUtilities; +import javax.swing.SwingWorker; +import javax.swing.event.CellEditorListener; +import javax.swing.event.ChangeEvent; import org.apache.commons.collections.CollectionUtils; import org.chorem.lima.entity.Account; import org.chorem.lima.entity.Entry; @@ -42,16 +54,6 @@ import org.jdesktop.swingx.decorator.HighlightPredicate; import org.jdesktop.swingx.decorator.Highlighter; -import javax.swing.SwingWorker; -import java.awt.Color; -import java.awt.Component; -import java.awt.event.KeyAdapter; -import java.awt.event.KeyEvent; -import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; -import java.math.BigDecimal; -import java.util.Date; - /** * Table des transaction qui ajoute des comportement (keys). * @@ -92,8 +94,29 @@ setDefaultEditor(Account.class, new AccountTableCellEditor()); //Get new amount editor - setDefaultEditor(BigDecimal.class, new BigDecimalTableCellEditor()); + BigDecimalTableCellEditor editor = new BigDecimalTableCellEditor(); + editor.addCellEditorListener(new CellEditorListener() { + @Override + public void editingStopped(ChangeEvent e) { + BigDecimalTableCellEditor editor = (BigDecimalTableCellEditor)e.getSource(); + final int row = editor.getRow(); + SwingUtilities.invokeLater(new Runnable() { + + @Override + public void run() { + getSelectionModel().setSelectionInterval(row, row); + } + }); + } + + @Override + public void editingCanceled(ChangeEvent e) { + // do nothing + } + }); + setDefaultEditor(BigDecimal.class, editor); + //Get new BigDecimal renderer setDefaultRenderer(BigDecimal.class, new BigDecimalTableCellRenderer()); @@ -139,12 +162,19 @@ colorTransaction = new ColorHighlighter(predicate, new Color(255, 198, 209), null); addHighlighter(colorTransaction); + + setTerminateEditOnFocusLost(true); } public FinancialTransactionViewHandler getHandler() { return handler; } + @Override + public void changeSelection(int rowIndex, int columnIndex, boolean toggle, boolean extend) { + super.changeSelection(rowIndex, columnIndex, true, true); + } + private class MyKeyAdapter extends KeyAdapter { @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-26 16:10:22 UTC (rev 3478) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransaction/FinancialTransactionTableModel.java 2012-06-27 10:16:23 UTC (rev 3479) @@ -25,14 +25,10 @@ package org.chorem.lima.ui.financialtransaction; -import static org.nuiton.i18n.I18n._; - import java.math.BigDecimal; import java.util.Date; import java.util.List; - import javax.swing.table.AbstractTableModel; - import org.chorem.lima.business.api.FinancialTransactionService; import org.chorem.lima.entity.Account; import org.chorem.lima.entity.Entry; @@ -40,6 +36,8 @@ import org.chorem.lima.entity.FinancialTransaction; import org.chorem.lima.service.LimaServiceFactory; +import static org.nuiton.i18n.I18n._; + /** * Basic transaction table model. * @@ -96,10 +94,10 @@ result = String.class; break; case 6: - result = BigDecimal.class; + result = String.class; break; case 7: - result = BigDecimal.class; + result = String.class; break; case 8: result = BigDecimal.class; Modified: trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransaction/FinancialTransactionViewHandler.java =================================================================== --- trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransaction/FinancialTransactionViewHandler.java 2012-06-26 16:10:22 UTC (rev 3478) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/financialtransaction/FinancialTransactionViewHandler.java 2012-06-27 10:16:23 UTC (rev 3479) @@ -25,8 +25,6 @@ package org.chorem.lima.ui.financialtransaction; -import static org.nuiton.i18n.I18n._; - import java.awt.event.ItemEvent; import java.util.ArrayList; import java.util.Calendar; @@ -34,11 +32,9 @@ import java.util.Collections; import java.util.Date; import java.util.List; - import javax.swing.JComboBox; import javax.swing.JOptionPane; import javax.swing.ListSelectionModel; - import org.apache.commons.lang3.time.DateUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -58,6 +54,8 @@ import org.chorem.lima.entity.FiscalPeriod; import org.chorem.lima.service.LimaServiceFactory; +import static org.nuiton.i18n.I18n._; + /** * Handler associated with financial transaction view. * Modified: trunk/lima-swing/src/main/java/org/chorem/lima/ui/fiscalperiod/FiscalPeriodView.jaxx =================================================================== --- trunk/lima-swing/src/main/java/org/chorem/lima/ui/fiscalperiod/FiscalPeriodView.jaxx 2012-06-26 16:10:22 UTC (rev 3478) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/fiscalperiod/FiscalPeriodView.jaxx 2012-06-27 10:16:23 UTC (rev 3479) @@ -48,8 +48,7 @@ id="fiscalPeriodTable" sortable="false" rowHeight="24" constructorParams="getFiscalPeriodTableModel()" columnControlVisible="true" - selectionModel='{model}' - /> + selectionModel='{model}'/> </JScrollPane> </cell> </row>
participants (1)
-
sletellier@users.chorem.org