Revision 41699
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.fmap.control/src/main/java/org/gvsig/fmap/mapcontrol/dal/feature/swing/FeatureTable.java | ||
---|---|---|
30 | 30 |
import java.awt.Color; |
31 | 31 |
import java.awt.Component; |
32 | 32 |
import java.sql.Timestamp; |
33 |
import java.util.Date; |
|
33 | 34 |
|
34 | 35 |
import javax.swing.JTable; |
35 | 36 |
import javax.swing.event.ChangeEvent; |
... | ... | |
44 | 45 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
45 | 46 |
import org.gvsig.fmap.dal.feature.FeatureStoreNotification; |
46 | 47 |
import org.gvsig.fmap.geom.Geometry; |
48 |
import org.gvsig.fmap.mapcontrol.dal.feature.swing.table.ConfigurableFeatureTableModel; |
|
47 | 49 |
import org.gvsig.fmap.mapcontrol.dal.feature.swing.table.FeatureCellRenderer; |
48 | 50 |
import org.gvsig.fmap.mapcontrol.dal.feature.swing.table.FeatureTableModel; |
51 |
import org.gvsig.fmap.mapcontrol.dal.feature.swing.table.FormattedCellRenderer; |
|
49 | 52 |
import org.gvsig.fmap.mapcontrol.dal.feature.swing.table.GeometryWKTCellEditor; |
50 | 53 |
import org.gvsig.fmap.mapcontrol.dal.feature.swing.table.GeometryWKTCellRenderer; |
51 | 54 |
import org.gvsig.fmap.mapcontrol.dal.feature.swing.table.JToggleButtonHeaderCellRenderer; |
... | ... | |
191 | 194 |
setDefaultEditor(Geometry.class, new GeometryWKTCellEditor()); |
192 | 195 |
setDefaultRenderer(Feature.class, new FeatureCellRenderer()); |
193 | 196 |
|
197 |
if( this.getModel() instanceof ConfigurableFeatureTableModel ) { |
|
198 |
ConfigurableFeatureTableModel model = (ConfigurableFeatureTableModel)this.getModel(); |
|
199 |
setDefaultRenderer(Double.class, new FormattedCellRenderer(model)); |
|
200 |
setDefaultRenderer(Float.class, new FormattedCellRenderer(model)); |
|
201 |
setDefaultRenderer(Integer.class, new FormattedCellRenderer(model)); |
|
202 |
setDefaultRenderer(Long.class, new FormattedCellRenderer(model)); |
|
203 |
setDefaultRenderer(Date.class, new FormattedCellRenderer(model)); |
|
204 |
} |
|
205 |
|
|
194 | 206 |
// Set the selected row colors |
195 | 207 |
setSelectionForeground(Color.blue); |
196 | 208 |
setSelectionBackground(Color.yellow); |
trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.fmap.control/src/main/java/org/gvsig/fmap/mapcontrol/dal/feature/swing/table/ConfigurableFeatureTableModel.java | ||
---|---|---|
28 | 28 |
package org.gvsig.fmap.mapcontrol.dal.feature.swing.table; |
29 | 29 |
|
30 | 30 |
import java.security.InvalidParameterException; |
31 |
import java.text.SimpleDateFormat; |
|
31 | 32 |
import java.util.ArrayList; |
32 | 33 |
import java.util.HashMap; |
33 | 34 |
import java.util.Iterator; |
34 | 35 |
import java.util.List; |
36 |
import java.util.Locale; |
|
35 | 37 |
import java.util.Map; |
36 |
import java.util.logging.Level; |
|
37 | 38 |
|
38 | 39 |
import javax.swing.event.TableModelEvent; |
39 | 40 |
|
... | ... | |
74 | 75 |
|
75 | 76 |
private Map<String, String> name2AliasOriginal; |
76 | 77 |
|
78 |
private Map<String,String> patterns = null; |
|
79 |
|
|
80 |
private Locale localeOfData; |
|
77 | 81 |
/** |
78 | 82 |
* @see FeatureTableModel#FeatureTableModel(FeatureStore, FeatureQuery) |
79 | 83 |
*/ |
80 | 84 |
public ConfigurableFeatureTableModel(FeatureStore featureStore, |
81 | 85 |
FeatureQuery featureQuery) throws BaseException { |
82 | 86 |
super(featureStore, featureQuery); |
87 |
this.localeOfData = Locale.getDefault(); |
|
83 | 88 |
} |
84 | 89 |
|
85 | 90 |
/** |
... | ... | |
460 | 465 |
name2Alias = new HashMap<String, String>(name2AliasOriginal); |
461 | 466 |
fireTableStructureChanged(); |
462 | 467 |
} |
468 |
|
|
469 |
protected void initializeFormattingPatterns() { |
|
470 |
this.patterns = new HashMap<String, String>(); |
|
471 |
|
|
472 |
int columns = super.getColumnCount(); |
|
473 |
|
|
474 |
String defaultDatePattern = new SimpleDateFormat().toPattern(); |
|
475 |
String defaultDecimalPattern = "#,###.000"; |
|
476 |
String defaultIntegerPattern = "#,###"; |
|
477 |
|
|
478 |
for (int i = 0; i < columns; i++) { |
|
479 |
String columnName = super.getColumnName(i); |
|
480 |
columnNames.add(columnName); |
|
481 |
|
|
482 |
FeatureAttributeDescriptor descriptor = |
|
483 |
super.getDescriptorForColumn(i); |
|
484 |
|
|
485 |
switch(descriptor.getDataType().getType()) { |
|
486 |
case DataTypes.BYTE: |
|
487 |
case DataTypes.INT: |
|
488 |
case DataTypes.LONG: |
|
489 |
this.patterns.put(columnName,defaultIntegerPattern); |
|
490 |
break; |
|
491 |
case DataTypes.DOUBLE: |
|
492 |
case DataTypes.FLOAT: |
|
493 |
this.patterns.put(columnName,defaultDecimalPattern); |
|
494 |
break; |
|
495 |
case DataTypes.DATE: |
|
496 |
this.patterns.put(columnName,defaultDatePattern); |
|
497 |
break; |
|
498 |
default: |
|
499 |
this.patterns.put(columnName,null); |
|
500 |
} |
|
501 |
} |
|
502 |
} |
|
503 |
|
|
504 |
public String getFormattingPattern(int column) { |
|
505 |
String columnName = this.visibleColumnNames.get(column); |
|
506 |
return this.getFormattingPattern(columnName); |
|
507 |
} |
|
508 |
|
|
509 |
public String getFormattingPattern(String columnName) { |
|
510 |
if( this.patterns==null ) { |
|
511 |
initializeFormattingPatterns(); |
|
512 |
} |
|
513 |
return this.patterns.get(columnName); |
|
514 |
} |
|
515 |
|
|
516 |
public void setFormattingPattern(String columnName, String pattern) { |
|
517 |
if( this.patterns==null ) { |
|
518 |
initializeFormattingPatterns(); |
|
519 |
} |
|
520 |
this.patterns.put(columnName,pattern); |
|
521 |
} |
|
522 |
|
|
523 |
public Locale getLocaleOfData() { |
|
524 |
return this.localeOfData; |
|
525 |
} |
|
526 |
|
|
527 |
public void setLocaleOfData(Locale locale) { |
|
528 |
this.localeOfData = locale; |
|
529 |
} |
|
530 |
|
|
531 |
|
|
463 | 532 |
} |
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 | ||
---|---|---|
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.JTable; |
|
10 |
import javax.swing.SwingConstants; |
|
11 |
import javax.swing.table.DefaultTableCellRenderer; |
|
12 |
import org.apache.commons.lang3.StringUtils; |
|
13 |
|
|
14 |
public class FormattedCellRenderer extends DefaultTableCellRenderer { |
|
15 |
|
|
16 |
private static final long serialVersionUID = -162038647556726890L; |
|
17 |
private ConfigurableFeatureTableModel tableModel; |
|
18 |
private DecimalFormat decimalFormat; |
|
19 |
private SimpleDateFormat dateFormat; |
|
20 |
|
|
21 |
public FormattedCellRenderer(ConfigurableFeatureTableModel tableModel) { |
|
22 |
this.tableModel = tableModel; |
|
23 |
this.decimalFormat = (DecimalFormat) NumberFormat.getInstance(this.tableModel.getLocaleOfData()); |
|
24 |
this.dateFormat = (SimpleDateFormat) DateFormat.getDateInstance(DateFormat.MEDIUM, this.tableModel.getLocaleOfData()); |
|
25 |
} |
|
26 |
|
|
27 |
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { |
|
28 |
super.getTableCellRendererComponent(table,value, isSelected, hasFocus,row, column); |
|
29 |
try { |
|
30 |
if ( value instanceof Number ) { |
|
31 |
String pattern = this.tableModel.getFormattingPattern(column); |
|
32 |
if ( !StringUtils.isBlank(pattern) ) { |
|
33 |
this.decimalFormat.applyPattern(pattern); |
|
34 |
String formated = this.decimalFormat.format(value).toString(); |
|
35 |
this.setHorizontalAlignment(SwingConstants.RIGHT); |
|
36 |
this.setText(formated); |
|
37 |
return this; |
|
38 |
} |
|
39 |
} |
|
40 |
if ( value instanceof Date ) { |
|
41 |
String pattern = this.tableModel.getFormattingPattern(column); |
|
42 |
if ( !StringUtils.isBlank(pattern) ) { |
|
43 |
this.dateFormat.applyPattern(pattern); |
|
44 |
String formated = this.dateFormat.format(value).toString(); |
|
45 |
this.setHorizontalAlignment(SwingConstants.RIGHT); |
|
46 |
this.setText(formated); |
|
47 |
return this; |
|
48 |
} |
|
49 |
} |
|
50 |
} catch (Exception ex) { |
|
51 |
// Do nothing, use values from default renderer |
|
52 |
} |
|
53 |
// this.setHorizontalAlignment(SwingConstants.LEFT); |
|
54 |
// this.setText(value==null?"":value.toString()); |
|
55 |
return this; |
|
56 |
} |
|
57 |
|
|
58 |
} |
Also available in: Unified diff