Home » Eclipse Projects » NatTable » Custom cell painter example bugs
Custom cell painter example bugs [message #1706841] |
Mon, 31 August 2015 13:01 |
Ines El Euch Messages: 35 Registered: March 2015 |
Member |
|
|
Hello,
I have used the code of the "Using a custom cell painter" example from NatTable examples (under Classic Examples -> Styling) but the display of the color is incoherent and illogic. I have used the same code with the diference in the data provider, I used a Glazed list and I applied that style to two columns only. Further, I used a cusom PercentageDisplayConverter (doesn't differ from the one used in the example the only difference relies on the type of the values, I used double values). I tried to figure out the problem but I am really stuck, I did not found any plausible explantion to this behaviour. I am counting on your help to solve this problem. Thanks in advance.
Best regards,
Inès E.
PS : an attached photo of the example (how it is supposed to look like) and the second one is the output that I have got.
How it is supposed to look :
My output :
-
Attachment: myOutput.PNG
(Size: 42.90KB, Downloaded 1357 times) -
Attachment: example.PNG
(Size: 32.50KB, Downloaded 1343 times)
[Updated on: Mon, 31 August 2015 13:02] Report message to a moderator
|
|
| |
Re: Custom cell painter example bugs [message #1706857 is a reply to message #1706853] |
Mon, 31 August 2015 13:21 |
Ines El Euch Messages: 35 Registered: March 2015 |
Member |
|
|
CustomPaintingConfig.java
import org.eclipse.nebula.widgets.nattable.config.AbstractRegistryConfiguration;
import org.eclipse.nebula.widgets.nattable.config.CellConfigAttributes;
import org.eclipse.nebula.widgets.nattable.config.IConfigRegistry;
import org.eclipse.nebula.widgets.nattable.painter.cell.PercentageBarCellPainter;
import org.eclipse.nebula.widgets.nattable.painter.cell.decorator.PercentageBarDecorator;
import org.eclipse.nebula.widgets.nattable.style.CellStyleAttributes;
import org.eclipse.nebula.widgets.nattable.style.DisplayMode;
import org.eclipse.nebula.widgets.nattable.style.HorizontalAlignmentEnum;
import org.eclipse.nebula.widgets.nattable.style.Style;
import org.eclipse.nebula.widgets.nattable.style.VerticalAlignmentEnum;
import org.eclipse.nebula.widgets.nattable.util.GUIHelper;
public class CustomPaintingConfig extends AbstractRegistryConfiguration {
@Override
public void configureRegistry(IConfigRegistry configRegistry) {
Style cellStyle = new Style();
cellStyle.setAttributeValue(CellStyleAttributes.HORIZONTAL_ALIGNMENT,
HorizontalAlignmentEnum.CENTER);
cellStyle.setAttributeValue(CellStyleAttributes.VERTICAL_ALIGNMENT,
VerticalAlignmentEnum.MIDDLE);
cellStyle
.setAttributeValue(
PercentageBarDecorator.PERCENTAGE_BAR_COMPLETE_REGION_START_COLOR,
GUIHelper.getColor(251, 149, 123));
cellStyle
.setAttributeValue(
PercentageBarDecorator.PERCENTAGE_BAR_COMPLETE_REGION_END_COLOR,
GUIHelper.getColor(248, 253, 219));
cellStyle.setAttributeValue(
PercentageBarDecorator.PERCENTAGE_BAR_INCOMPLETE_REGION_COLOR,
GUIHelper.getColor(254, 254, 254));
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE,
cellStyle, DisplayMode.NORMAL, "memPercentage");
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE,
cellStyle, DisplayMode.NORMAL, "cpuPercentage");
configRegistry.registerConfigAttribute(
CellConfigAttributes.CELL_PAINTER,
new PercentageBarCellPainter(), DisplayMode.NORMAL,
"cpuPercentage");
configRegistry.registerConfigAttribute(
CellConfigAttributes.CELL_PAINTER,
new PercentageBarCellPainter(), DisplayMode.NORMAL,
"memPercentage");
configRegistry.registerConfigAttribute(
CellConfigAttributes.DISPLAY_CONVERTER,
new CustomPercentageDisplayConverter(), DisplayMode.NORMAL,
"cpuPercentage");
configRegistry.registerConfigAttribute(
CellConfigAttributes.DISPLAY_CONVERTER,
new CustomPercentageDisplayConverter(), DisplayMode.NORMAL,
"memPercentage");
}
}
CustomPercentageDisplayConverter.java
import org.eclipse.nebula.widgets.nattable.data.convert.DisplayConverter;
public class CustomPercentageDisplayConverter extends DisplayConverter {
@Override
public Object canonicalToDisplayValue(Object canonicalValue) {
if (canonicalValue != null) {
double percentageValue = ((Double) canonicalValue).doubleValue();
double display = percentageValue * 100;
display = (double)((int)(display*100))/100;
return String.valueOf(display) + "%"; //$NON-NLS-1$
}
return ""; //$NON-NLS-1$
}
@Override
public Object displayToCanonicalValue(Object displayValue) {
String displayString = (String) displayValue;
displayString = displayString.trim();
if (displayString.endsWith("%")) { //$NON-NLS-1$
displayString = displayString.substring(0,
displayString.length() - 1);
}
displayString = displayString.trim();
double display = Double.valueOf(displayString).doubleValue();
double percentageValue = display / 100;
return Double.valueOf(percentageValue);
}
}
I then added it to my nattable
nattable.addConfiguration(new CustomPaintingConfig());
nattable.configure();
The data provider that I used :
EventList event = GlazedLists.eventList(values);
rowObjects = GlazedLists.threadSafeList(event);
sortList = new SortedList(rowObjects, null);
this.filter = new FilterList(sortList);
this.bodyDataProvider = new ListDataProvider(this.filter,
columnAccessor);
DataLayer bodyDataLayer = new DataLayer(bodyDataProvider);
// Override Label accumulator in order to add progress bar into %CPU and
// %MEM
final ColumnOverrideLabelAccumulator columnLabelAccumulator = new ColumnOverrideLabelAccumulator(
bodyDataLayer);
bodyDataLayer.setConfigLabelAccumulator(columnLabelAccumulator);
registerColumnLabels(columnLabelAccumulator);
GlazedListsEventLayer glazedListsEvent = new GlazedListsEventLayer(
bodyDataLayer, this.filter);
// get the id
IRowIdAccessor rowIdAccessor = new IRowIdAccessor() {
@Override
public Serializable getRowId(Object rowObject) {
return ((TopModel) rowObject).getPid();
}
};
private void registerColumnLabels(
ColumnOverrideLabelAccumulator columnLabelAccumulator) {
columnLabelAccumulator.registerColumnOverrides(0, "time");
columnLabelAccumulator.registerColumnOverrides(1, "pid");
columnLabelAccumulator.registerColumnOverrides(2, "command");
columnLabelAccumulator.registerColumnOverrides(3, "user");
columnLabelAccumulator.registerColumnOverrides(4, "pr");
columnLabelAccumulator.registerColumnOverrides(5, "cpuPercentage");
columnLabelAccumulator.registerColumnOverrides(6, "memPercentage");
columnLabelAccumulator.registerColumnOverrides(7, "ni");
columnLabelAccumulator.registerColumnOverrides(8, "virt");
columnLabelAccumulator.registerColumnOverrides(9, "res");
columnLabelAccumulator.registerColumnOverrides(10, "shr");
columnLabelAccumulator.registerColumnOverrides(11, "s");
}
My NatTable heave the column reorder, sort,filter features. Hope gave you everything you need to help me.
Thank you for your assistance Sir.
Best regards,
Inès E.
|
|
| | |
Re: Custom cell painter example bugs [message #1706874 is a reply to message #1706865] |
Mon, 31 August 2015 14:40 |
Ines El Euch Messages: 35 Registered: March 2015 |
Member |
|
|
Hello Sir,
I don't know if this is the fix that you mentioned or not. But I tried to make a local class and made this modification :
Pattern pattern = new Pattern(Display.getCurrent(), rectangle.x, rectangle.y,
rectangle.x + rectangle.width, rectangle.y + rectangle.height, color1,
color2);
But this didn't solve the problem for me . However it caused another issue, it used the default colors and note those that I specified.
Need to note that I am displaying a double values not int like the example. However that is not the issue in my case because I tested int values and still same output.
[Updated on: Mon, 31 August 2015 14:41] Report message to a moderator
|
|
| |
Re: Custom cell painter example bugs [message #1706878 is a reply to message #1706877] |
Mon, 31 August 2015 15:07 |
Ines El Euch Messages: 35 Registered: March 2015 |
Member |
|
|
I think I understand why it did not work, When I made a debug to see why the colors are ignored (the one that I chose) and suprisingly, the debugger never entered in the class. So do you have any idea of why my class is ignored while I clearly speicified that I wanted to use it ?
import org.eclipse.nebula.widgets.nattable.config.AbstractRegistryConfiguration;
import org.eclipse.nebula.widgets.nattable.config.CellConfigAttributes;
import org.eclipse.nebula.widgets.nattable.config.IConfigRegistry;
import org.eclipse.nebula.widgets.nattable.painter.cell.PercentageBarCellPainter;
import org.eclipse.nebula.widgets.nattable.style.CellStyleAttributes;
import org.eclipse.nebula.widgets.nattable.style.DisplayMode;
import org.eclipse.nebula.widgets.nattable.style.HorizontalAlignmentEnum;
import org.eclipse.nebula.widgets.nattable.style.Style;
import org.eclipse.nebula.widgets.nattable.style.VerticalAlignmentEnum;
import org.eclipse.nebula.widgets.nattable.util.GUIHelper;
import org.eclipse.swt.graphics.RGB;
public class CustomPaintingConfig extends AbstractRegistryConfiguration {
@Override
public void configureRegistry(IConfigRegistry configRegistry) {
Style cellStyle = new Style();
cellStyle.setAttributeValue(CellStyleAttributes.HORIZONTAL_ALIGNMENT,
HorizontalAlignmentEnum.CENTER);
cellStyle.setAttributeValue(CellStyleAttributes.VERTICAL_ALIGNMENT,
VerticalAlignmentEnum.MIDDLE);
cellStyle
.setAttributeValue(
CustomPercentageBarDecartor.PERCENTAGE_BAR_COMPLETE_REGION_START_COLOR,
GUIHelper.getColor(new RGB(251, 149, 123)));
cellStyle
.setAttributeValue(
CustomPercentageBarDecartor.PERCENTAGE_BAR_COMPLETE_REGION_END_COLOR,
GUIHelper.getColor(new RGB(248, 253, 219)));
cellStyle.setAttributeValue(
CustomPercentageBarDecartor.PERCENTAGE_BAR_INCOMPLETE_REGION_COLOR,
GUIHelper.getColor(new RGB(254, 254, 254)));
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE,
cellStyle, DisplayMode.NORMAL, "cpuPercentage");
configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE,
cellStyle, DisplayMode.NORMAL, "memPercentage");
configRegistry.registerConfigAttribute(
CellConfigAttributes.CELL_PAINTER,
new PercentageBarCellPainter(), DisplayMode.NORMAL,
"cpuPercentage");
configRegistry.registerConfigAttribute(
CellConfigAttributes.CELL_PAINTER,
new PercentageBarCellPainter(), DisplayMode.NORMAL,
"memPercentage");
configRegistry.registerConfigAttribute(
CellConfigAttributes.DISPLAY_CONVERTER,
new CustomPercentageDisplayConverter(), DisplayMode.NORMAL,
"cpuPercentage");
configRegistry.registerConfigAttribute(
CellConfigAttributes.DISPLAY_CONVERTER,
new CustomPercentageDisplayConverter(), DisplayMode.NORMAL,
"memPercentage");
}
}
import org.eclipse.nebula.widgets.nattable.config.IConfigRegistry;
import org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell;
import org.eclipse.nebula.widgets.nattable.painter.cell.CellPainterWrapper;
import org.eclipse.nebula.widgets.nattable.painter.cell.ICellPainter;
import org.eclipse.nebula.widgets.nattable.style.CellStyleUtil;
import org.eclipse.nebula.widgets.nattable.style.ConfigAttribute;
import org.eclipse.nebula.widgets.nattable.util.GUIHelper;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Pattern;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.graphics.Region;
import org.eclipse.swt.widgets.Display;
/**
* Draws a rectangular bar in cell proportional to the value of the cell.
*/
public class CustomPercentageBarDecartor extends CellPainterWrapper {
public static final ConfigAttribute<Color> PERCENTAGE_BAR_COMPLETE_REGION_START_COLOR = new ConfigAttribute<Color>();
public static final ConfigAttribute<Color> PERCENTAGE_BAR_COMPLETE_REGION_END_COLOR = new ConfigAttribute<Color>();
public static final ConfigAttribute<Color> PERCENTAGE_BAR_INCOMPLETE_REGION_COLOR = new ConfigAttribute<Color>();
private static final Color DEFAULT_COMPLETE_REGION_START_COLOR = GUIHelper
.getColor(new RGB(187, 216, 254));
private static final Color DEFAULT_COMPLETE_REGION_END_COLOR = GUIHelper
.getColor(new RGB(255, 255, 255));
public CustomPercentageBarDecartor(ICellPainter interiorPainter) {
super(interiorPainter);
}
@Override
public void paintCell(ILayerCell cell, GC gc, Rectangle rectangle,
IConfigRegistry configRegistry) {
Pattern originalBackgroundPattern = gc.getBackgroundPattern();
double factor = Math.min(1.0,
((Double) cell.getDataValue()).doubleValue());
factor = Math.max(0.0, factor);
Rectangle bar = new Rectangle(rectangle.x, rectangle.y,
(int) (rectangle.width * factor), rectangle.height);
// Rectangle bounds = cell.getBounds();
Color color1 = CellStyleUtil.getCellStyle(cell, configRegistry)
.getAttributeValue(PERCENTAGE_BAR_COMPLETE_REGION_START_COLOR);
Color color2 = CellStyleUtil.getCellStyle(cell, configRegistry)
.getAttributeValue(PERCENTAGE_BAR_COMPLETE_REGION_END_COLOR);
if (color1 == null)
color1 = DEFAULT_COMPLETE_REGION_START_COLOR;
if (color2 == null)
color2 = DEFAULT_COMPLETE_REGION_END_COLOR;
Pattern pattern = new Pattern(Display.getCurrent(), rectangle.x, rectangle.y,
rectangle.x + rectangle.width, rectangle.y + rectangle.height, color1,
color2);
gc.setBackgroundPattern(pattern);
gc.fillRectangle(bar);
gc.setBackgroundPattern(originalBackgroundPattern);
pattern.dispose();
Color incompleteRegionColor = CellStyleUtil.getCellStyle(cell,
configRegistry).getAttributeValue(
PERCENTAGE_BAR_INCOMPLETE_REGION_COLOR);
if (incompleteRegionColor != null) {
Region incompleteRegion = new Region();
incompleteRegion.add(rectangle);
incompleteRegion.subtract(bar);
Color originalBackgroundColor = gc.getBackground();
gc.setBackground(incompleteRegionColor);
gc.fillRectangle(incompleteRegion.getBounds());
gc.setBackground(originalBackgroundColor);
incompleteRegion.dispose();
}
super.paintCell(cell, gc, rectangle, configRegistry);
}
}
|
|
| | | | | | |
Goto Forum:
Current Time: Fri Mar 29 02:15:59 GMT 2024
Powered by FUDForum. Page generated in 0.03580 seconds
|