问题描述
我正在从 XML 文件创建 nattable。在读取 XML 时,所有值都转换为字符串(因为其他应用程序功能)。尽管表中的所有值都是字符串,但我需要对值应用不同的样式,这些值在 XML 中一直是数字。所以我创建了 List<List<String>> dataTypes
,它存储原始数据类型。我阅读了这个列表并应用了样式:
sortedList = new SortedList<>(rowObjectsGlazedList,null);
// wrap the SortedList with the FilterList
filterList = new FilterList<>(sortedList);
bodyDataProvider = new ListDataProvider<TableLine>(filterList,getColumnAccessor(columnCount));
bodyDataLayer = new DataLayer(bodyDataProvider);
System.out.println("BodyLayerstack.java:71: bodyDataLayer = new DataLayer(bodyDataProvider)");
IConfigLabelAccumulator cellLabelAccumulator = new IConfigLabelAccumulator() {
@Override
public void accumulateConfigLabels(LabelStack configLabels,int columnPosition,int rowPosition) {
int columnIndex = bodyDataLayer.getColumnIndexByPosition(columnPosition);
int rowIndex = bodyDataLayer.getRowIndexByPosition(rowPosition);
if( isRowHeader(columnIndicesForRowHeaders,columnIndex) )
configLabels.addLabel(NatTableFactory.RowHeaderLabel);
else
configLabels.addLabel(values.get(rowIndex).getobjectTypeByColumn(columnIndex));
}
};
bodyDataLayer.setConfigLabelAccumulator(cellLabelAccumulator);
System.out.println("BodyLayerstack.java: 88: bodyDataLayer.setConfigLabelAccumulator(cellLabelAccumulator) ");
GlazedListsEventLayer<TableLine> glazedListsEventLayer = new GlazedListsEventLayer<>(bodyDataLayer,filterList);
selectionLayer = new SelectionLayer(glazedListsEventLayer,false);
selectionLayer.setSelectionModel(getSelectionModel());
selectionLayer.addConfiguration(getSelectionConfiguration());
setUnderlyingLayer(new ViewportLayer(selectionLayer));
经理:
public class AttributeManager implements EventHandler {
..
private static final String EmptyValue = " ";
private static final String ValueTypestring = "String";
private static final String ValueTypeNumber = "Number";
...
private void addToModel(XLSAttributeModel xlsModel) {
...
List<String> values = new ArrayList<String>();
List<String> valueTypes = new ArrayList<String>();
// set cells value
for( XLSCell cell : r.getCells() ) {
// if cell index exists,add empty value for cells before cell with index
if( cell.getIndex() != null ) {
for( int j = colIdx; j < cell.getIndex()-1; j++ ) {
values.add(EmptyValue);
valueTypes.add(ValueTypestring);
}
colIdx = cell.getIndex()-1;
}
if( cell.getData() != null ) {
values.add(cell.getData().getdisplayValue());
valueTypes.add(getValueType(cell.getData().getDataType()));
} else {
values.add(EmptyValue);
valueTypes.add(ValueTypestring);
}
colIdx++;
}
// add empty value for cells in remaining columns
for( ; colIdx < columnCount; colIdx++ ) {
values.add(EmptyValue);
valueTypes.add(ValueTypestring);
}
Attributes attrs = new Attributes();
String[] vals = new String[values.size()];
for( int i = 0; i < values.size(); i++ )
vals[i] = values.get(i);
attrs.setValues(vals);
data.addData(attrs);
//
data.addDataTypes(valueTypes);
和:
public String getobjectTypeByColumn(int columnIndex)
{
if (this.dataTypes == null || columnIndex > this.dataTypes.size() - 1)
return null;
return this.dataTypes.get(columnIndex);
}
然后我设置样式:
public static AbstractRegistryConfiguration getNatTableConfiguration()
{
return new AbstractRegistryConfiguration()
{
@Override
public void configureRegistry(IConfigRegistry configRegistry)
{
Style headerCellStyle = new Style();
headerCellStyle.setAttributeValue(CellStyleAttributes.BACKGROUND_COLOR,GUIHelper.COLOR_WIDGET_BACKGROUND);
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE,headerCellStyle,displayMode.norMAL,NatTableFactory.RowHeaderLabel);
Style cellAlignStyle = new Style();
cellAlignStyle.setAttributeValue(CellStyleAttributes.HORIZONTAL_ALIGNMENT,HorizontalAlignmentEnum.RIGHT);
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE,cellAlignStyle,NatTableFactory.DataTypeNumberLabel);
//
System.out.println("NatTableLayerConfigurations.java: 67: NatTableFactory.DataTypeNumberLabel ");
cellAlignStyle = new Style();
cellAlignStyle.setAttributeValue(CellStyleAttributes.HORIZONTAL_ALIGNMENT,HorizontalAlignmentEnum.LEFT);
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE,NatTableFactory.DataTypestringLabel);
Style cellStyle = new Style();
cellStyle.setAttributeValue(CellStyleAttributes.FONT,ApplicationSizeManager.getDefaultFont(display.getDefault()));
cellStyle.setAttributeValue(CellStyleAttributes.BACKGROUND_COLOR,new Color(display.getDefault(),255,255));
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE,cellStyle,displayMode.norMAL);
}
};
}
和视图:
...
List<List<String>> dataTypes = data.getDataTypeList();
params.setCellTypes(new String[dataTypes.size()][dataTypes.get(0).size()]);
String[][] cellTypes = params.getCellTypes();
System.out.println("AttributeViewFrame.java:270: String[][] cellTypes = params.getCellTypes(); ");
for( int i = 0; i < dataTypes.size(); i++ ) {
List<String> types = dataTypes.get(i);
for( int j = 0; j < types.size() && j < dataTypes.get(0).size(); j++ ) {
cellTypes[i][j] = types.get(j);
}
}
...
for( int i = 0; i < cells.length; i++ ) {
if( hiddenRowsList != null && hiddenRowsList.contains(i) )
continue;
List<Object> objectList = new ArrayList<Object>();
List<String> dataTypeList = new ArrayList<String>();
String partNo = "";
for( int j = 0; j < cells[i].length; j++ ) {
if( hiddenColsList == null || !hiddenColsList.contains(j) ) {
objectList.add(cells[i][j]);
dataTypeList.add(cellTypes[i][j]);
//
System.out.println("AttributeViewFrame.java:394: dataTypeList.add: cellTypes[i][j]: "+cellTypes[i][j]);
if (containsPartNumer)
{
if (columnHeaders != null && columnHeaders[0][j] instanceof String && ((String) columnHeaders[0][j]).equalsIgnoreCase(PartNo))
partNo = (String) cells[i][j];
else if (cells != null && cells[0][j] instanceof String && ((String) cells[0][j]).equalsIgnoreCase(PartNo))
partNo = (String) cells[i][j];
}
}
}
TableLine tLine = new TableLine(i,objectList,dataTypeList,params.getHighlighPaths(i),partNo);
tLines.add(tLine);
}
return tLines;
问题是,排序(或过滤)后没有更新样式。如果排序前某个位置是数字,则排序后的单元格也格式化为数字。
解决方法
我明白了,我的问题是:
configLabels.addLabel(values.get(rowIndex).getObjectTypeByColumn(columnIndex));
排序时那里的值没有更新。我用以下内容替换了该行:
IDataProvider addLabelsDataProvider = bodyDataLayer.getDataProvider();
@SuppressWarnings("unchecked")
ListDataProvider<TableLine> addLabelsListDataProvider = (ListDataProvider<TableLine>) addLabelsDataProvider;
List<TableLine> myList = addLabelsListDataProvider.getList();
configLabels.addLabel(myList.get(rowIndex).getObjectTypeByColumn(columnIndex).toString());