svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.fmap.control / src / main / java / org / gvsig / fmap / mapcontrol / dal / feature / swing / table / FormattedCellRenderer.java @ 47436
History | View | Annotate | Download (2.33 KB)
1 |
package org.gvsig.fmap.mapcontrol.dal.feature.swing.table; |
---|---|
2 |
|
3 |
import java.awt.Component; |
4 |
import java.text.DateFormat; |
5 |
import java.text.DecimalFormat; |
6 |
import java.text.NumberFormat; |
7 |
import java.text.SimpleDateFormat; |
8 |
import java.util.Date; |
9 |
import javax.swing.JLabel; |
10 |
import javax.swing.JTable; |
11 |
import javax.swing.SwingConstants; |
12 |
import org.apache.commons.lang3.StringUtils; |
13 |
|
14 |
@SuppressWarnings("UseSpecificCatch") |
15 |
public class FormattedCellRenderer extends FeatureAttributeCellRenderer { |
16 |
|
17 |
private DecimalFormat decimalFormat; |
18 |
private SimpleDateFormat dateFormat; |
19 |
|
20 |
public FormattedCellRenderer() {
|
21 |
} |
22 |
|
23 |
@Override
|
24 |
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { |
25 |
JLabel c = (JLabel) super.getTableCellRendererComponent(table,value, isSelected, hasFocus,row, column); |
26 |
try {
|
27 |
ConfigurableFeatureTableModel tableModel = this.getTableModel(table);
|
28 |
if(this.decimalFormat == null){ |
29 |
this.decimalFormat = (DecimalFormat) NumberFormat.getInstance(tableModel.getLocaleOfData()); |
30 |
this.dateFormat = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.MEDIUM, tableModel.getLocaleOfData()); |
31 |
} |
32 |
if ( value instanceof Number ) { |
33 |
String pattern = this.getTableModel(table).getFormattingPattern(column); |
34 |
if ( !StringUtils.isBlank(pattern) ) {
|
35 |
this.decimalFormat.applyPattern(pattern);
|
36 |
String formated = this.decimalFormat.format(value); |
37 |
c.setHorizontalAlignment(SwingConstants.RIGHT);
|
38 |
c.setText(formated); |
39 |
return this; |
40 |
} |
41 |
} |
42 |
if ( value instanceof Date ) { |
43 |
String pattern = this.getTableModel(table).getFormattingPattern(column); |
44 |
if ( !StringUtils.isBlank(pattern) ) {
|
45 |
this.dateFormat.applyPattern(pattern);
|
46 |
String formated = this.dateFormat.format(value); |
47 |
c.setHorizontalAlignment(SwingConstants.RIGHT);
|
48 |
c.setText(formated); |
49 |
return this; |
50 |
} |
51 |
} |
52 |
} catch (Exception ex) { |
53 |
// Do nothing, use values from default renderer
|
54 |
} |
55 |
return c;
|
56 |
} |
57 |
|
58 |
} |