Author: vsalaun Date: 2011-05-12 10:51:57 +0200 (Thu, 12 May 2011) New Revision: 3114 Url: http://chorem.org/repositories/revision/lima/3114 Log: #267 ajout du renderer pour les objets de type BigDecimal Added: trunk/lima-swing/src/main/java/org/chorem/lima/ui/celleditor/BigDecimalTableCellEditor.java trunk/lima-swing/src/main/java/org/chorem/lima/ui/celleditor/BigDecimalTableCellRenderer.java Added: 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 (rev 0) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/celleditor/BigDecimalTableCellEditor.java 2011-05-12 08:51:57 UTC (rev 3114) @@ -0,0 +1,60 @@ +package org.chorem.lima.ui.celleditor; + +import java.awt.Component; +import java.awt.event.KeyEvent; +import java.awt.event.MouseEvent; +import java.math.BigDecimal; +import java.util.Date; +import java.util.EventObject; + +import javax.swing.AbstractCellEditor; +import javax.swing.DefaultCellEditor; +import javax.swing.JTable; +import javax.swing.JTextField; +import javax.swing.SwingConstants; +import javax.swing.SwingUtilities; +import javax.swing.table.TableCellEditor; +import javax.swing.text.JTextComponent; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.chorem.lima.LimaContext; + +public class BigDecimalTableCellEditor extends AbstractCellEditor implements TableCellEditor { + + private static final Log log = LogFactory.getLog(BigDecimalTableCellEditor.class); + private static final long serialVersionUID = 1L; + protected final JTextField textField; + + /** + * constructor + */ + public BigDecimalTableCellEditor() { + textField = new JTextField(); + textField.setEditable(true); + textField.setHorizontalAlignment(SwingConstants.RIGHT); + } + + @Override + public Component getTableCellEditorComponent(JTable table, Object value, + boolean isSelected, int row, int column) { + textField.setText(value.toString()); + return textField; + } + + // the editor should return a BigDecimal + public Object getCellEditorValue() { + String textValue = textField.getText().trim(); + //replace all comma by fullstop + textValue = textValue.replaceAll(",", "."); + if (textValue.equals("")) { + return BigDecimal.ZERO; + } else { + BigDecimal bdValue = new BigDecimal(textValue); + bdValue = bdValue.setScale(LimaContext.getContext().getConfig().getScale(), + BigDecimal.ROUND_HALF_UP); + return bdValue; + } + } + +} Added: trunk/lima-swing/src/main/java/org/chorem/lima/ui/celleditor/BigDecimalTableCellRenderer.java =================================================================== --- trunk/lima-swing/src/main/java/org/chorem/lima/ui/celleditor/BigDecimalTableCellRenderer.java (rev 0) +++ trunk/lima-swing/src/main/java/org/chorem/lima/ui/celleditor/BigDecimalTableCellRenderer.java 2011-05-12 08:51:57 UTC (rev 3114) @@ -0,0 +1,62 @@ +package org.chorem.lima.ui.celleditor; + +import java.awt.Component; +import java.awt.Font; +import java.math.BigDecimal; +import java.text.DecimalFormat; +import java.text.DecimalFormatSymbols; +import java.text.NumberFormat; +import java.util.Locale; + +import javax.swing.JTable; +import javax.swing.SwingConstants; +import javax.swing.table.TableCellRenderer; +import javax.swing.table.DefaultTableCellRenderer; + +import org.chorem.lima.LimaConfig; +import org.chorem.lima.LimaContext; +import org.chorem.lima.beans.FinancialStatementAmounts; +import org.chorem.lima.ui.financialstatementreport.FinancialStatementReportTableModel; + +public class BigDecimalTableCellRenderer extends DefaultTableCellRenderer { + + private static final long serialVersionUID = -2499433026151065390L; + + public BigDecimalTableCellRenderer() { + // + } + + public Component getTableCellRendererComponent(JTable table, Object value, + boolean isSelected, boolean hasFocus, int row, int column) + { + + Component cell = super.getTableCellRendererComponent( + table, value, isSelected, hasFocus, row, column); + setHorizontalAlignment(SwingConstants.RIGHT); + setValue(value); + return this; + } + + public void setValue(Object aValue) { + Object result = aValue; + if (( aValue != null) && (aValue instanceof BigDecimal)) { + LimaConfig config = LimaContext.getContext().getConfig(); + String scale = ""; + for (int i = 0; i < config.getScale(); i++) { + scale += "0"; + } + DecimalFormat formatter = new DecimalFormat("##0." + scale); + DecimalFormatSymbols symbol = new DecimalFormatSymbols(); + //set decimalSeparator and thousandSeparator preferences + symbol.setDecimalSeparator(config.getDecimalSeparator()); + symbol.setGroupingSeparator(config.getThousandSeparator()); + formatter.setDecimalFormatSymbols(symbol); + //always set grouping + formatter.setGroupingUsed(true); + formatter.setGroupingSize(3); + result = formatter.format(result); + } + super.setValue(result); + } + +}
participants (1)
-
vsalaun@users.chorem.org