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