[英]Styling in NatTable is not updated after sorting/filtering
我正在从 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, DisplayMode.NORMAL, 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, cellAlignStyle, DisplayMode.NORMAL, 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, 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;
问题是,排序(或过滤)后样式不会更新。 如果在某些 position 是排序前的数字,则单元格在排序后也被格式化为数字。
我明白了,我的问题是: 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());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.