OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 94 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
73 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.erp.core.common.ui;
15
 
16
import org.openconcerto.ui.table.TableCellRendererUtils;
17
 
18
import java.awt.Component;
19
import java.math.BigDecimal;
20
import java.text.DecimalFormat;
21
import java.text.DecimalFormatSymbols;
22
 
23
import javax.swing.JTable;
24
import javax.swing.SwingConstants;
25
import javax.swing.table.DefaultTableCellRenderer;
26
 
27
public class DeviseTableCellRenderer extends DefaultTableCellRenderer {
94 ilm 28
    private final DecimalFormat decimalFormat;
29
    private final DecimalFormat decimalFormat2;
73 ilm 30
    private BigDecimal oneCents = new BigDecimal(0.01f);
174 ilm 31
    private boolean hideZeroValue = false;
73 ilm 32
 
33
    public DeviseTableCellRenderer() {
94 ilm 34
        this(new DecimalFormat("##,##0.00#"), new DecimalFormat("##,##0.00#######"));
35
    }
36
 
37
    public DeviseTableCellRenderer(DecimalFormat decimalFormat, DecimalFormat decimalFormatLessOne) {
38
        this.decimalFormat = decimalFormat;
39
        this.decimalFormat2 = decimalFormatLessOne;
73 ilm 40
        final DecimalFormatSymbols symbol = DecimalFormatSymbols.getInstance();
41
        symbol.setDecimalSeparator('.');
42
        decimalFormat.setDecimalFormatSymbols(symbol);
43
        decimalFormat2.setDecimalFormatSymbols(symbol);
44
    }
45
 
174 ilm 46
    public void setHideZeroValue(boolean hideZeroValue) {
47
        this.hideZeroValue = hideZeroValue;
48
    }
49
 
73 ilm 50
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
51
        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
52
        TableCellRendererUtils.setColors(this, table, isSelected);
53
        this.setHorizontalAlignment(SwingConstants.RIGHT);
54
        if (table.getColumnClass(column) != BigDecimal.class) {
55
            throw new IllegalStateException("Value is not a BigDecimal :" + table.getColumnClass(column));
56
        }
174 ilm 57
 
80 ilm 58
        if (value != null) {
174 ilm 59
            BigDecimal amount = (BigDecimal) value;
60
            if (this.hideZeroValue && amount.signum() == 0) {
61
                this.setText("");
62
            } else {
63
                if (amount.compareTo(oneCents) < 0)
64
                    this.setText(decimalFormat2.format(value));
65
                else
66
                    this.setText(decimalFormat.format(value));
67
            }
80 ilm 68
        } else {
69
            this.setText("");
70
        }
71
 
73 ilm 72
        return this;
73
    }
74
 
75
}