【发布时间】:2017-03-25 01:03:32
【问题描述】:
我已阅读文档:https://eclipse.org/nattable/documentation.php?page=styling
我很好奇是否有任何简单的方法可以使用单独的配置添加背景行颜色和图像。我不希望像 CellPainterWrapper 示例那样将它们组合成 1 个配置,因为我想将两者之间的逻辑分开。我当前的代码适用于图像或背景颜色,但我不能同时使用这两种方法(最顶层的配置会覆盖最底层的配置)。下面是我的sn-p:
void run(){
addBackgroundRowColors();
addImageToColumn();
}
void addImageToColumn() {
getNatTable().addConfiguration(new AbstractRegistryConfiguration() {
@Override
public void configureRegistry(IConfigRegistry configRegistry) {
final Style cellStyleOne = new Style();
cellStyleOne.setAttributeValue(CellStyleAttributes.IMAGE,
myIcon);
configRegistry.registerConfigAttribute(
CellConfigAttributes.CELL_STYLE, cellStyleOne,
DisplayMode.NORMAL, myIconLabel);
final Style cellStyleTwo = new Style();
cellStyleTwo.setAttributeValue(CellStyleAttributes.IMAGE,
myIcon2);
configRegistry.registerConfigAttribute(
CellConfigAttributes.CELL_STYLE, cellStyleTwo,
DisplayMode.NORMAL, myIconLabel2);
configRegistry.registerConfigAttribute(
CellConfigAttributes.CELL_PAINTER,
new CellPainterDecorator(new TextPainter(),
CellEdgeEnum.LEFT, 10, new ImagePainter()),
DisplayMode.NORMAL);
}
});
DataLayer dl = getGlazedListsGridLayer().getBodyDataLayer();
IConfigLabelAccumulator cellLabelAccumulator = (configLabels,
columnPosition, rowPosition) -> {
// Label code here...
};
dl.setConfigLabelAccumulator(cellLabelAccumulator);
}
void addBackgroundRowColors() {
getNatTable().addConfiguration(new AbstractRegistryConfiguration() {
@Override
public void configureRegistry(IConfigRegistry configRegistry) {
Style cellStyleOne = new Style();
cellStyleOne.setAttributeValue(
CellStyleAttributes.BACKGROUND_COLOR, myColorOne);
configRegistry.registerConfigAttribute(
CellConfigAttributes.CELL_STYLE, cellStyleOne,
DisplayMode.NORMAL, myColorLabel1);
Style cellStyleTwo = new Style();
cellStyleTwo.setAttributeValue(
CellStyleAttributes.BACKGROUND_COLOR, cellStyleTwo);
configRegistry.registerConfigAttribute(
CellConfigAttributes.CELL_STYLE, cellStyleTwo,
DisplayMode.NORMAL, myColorLabel2);
}
});
DataLayer dl = getGlazedListsGridLayer().getBodyDataLayer();
IConfigLabelAccumulator cellLabelAccumulator = (configLabels,
columnPosition, rowPosition) -> {
// Label code here...
};
dl.setConfigLabelAccumulator(cellLabelAccumulator);
}
更新
我最终做了类似于以下的事情来使它工作:
AggregateConfigLabelAccumulator aggregate =
new AggregateConfigLabelAccumulator();
aggregate.add(addImageToColumn());
aggregate.add(addBackgroundRowColors());
getGlazedListsGridLayer().getBodyDataLayer().
setConfigLabelAccumulator(aggregate);
【问题讨论】:
-
问题是什么?你想摆脱配置中的单个 CellPainterWrapper 吗?我认为这不会以简单的方式起作用。或者上面的sn -p是你想做的吗?乍一看应该可以。您是否将两个标签都添加到标签累加器中的标签堆栈中?
-
我的问题是如何使用上面的代码 sn-p 完成这项工作?我不想将我的图像和背景颜色代码混合到一个庞大的方法中。上面的 sn-p 是我想做的。每个 IConfigLabelAccumulator 都将自己的标签添加到堆栈中。换句话说,背景颜色标签累加器只处理背景颜色标签。图片标签累加器只处理图片标签。