Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » NatTable » Custom cell painter example bugs
Custom cell painter example bugs [message #1706841] Mon, 31 August 2015 13:01 Go to next message
Ines El Euch is currently offline Ines El EuchFriend
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 :
index.php/fa/23067/0/

My output :

index.php/fa/23066/0/
  • 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 #1706853 is a reply to message #1706841] Mon, 31 August 2015 13:14 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
Without code I don't know how to help. You say that you did the same, but as it doesn't do the same thing, I assume you missed something.
Re: Custom cell painter example bugs [message #1706857 is a reply to message #1706853] Mon, 31 August 2015 13:21 Go to previous messageGo to next message
Ines El Euch is currently offline Ines El EuchFriend
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 #1706863 is a reply to message #1706857] Mon, 31 August 2015 14:12 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
From what I see it looks like a bug in the PercentageBarDecorator. You could try to create the class locally and change the Pattern instance to use rectangle instead of the bounds variable for the definition of the Pattern coordinates?

I tested locally and could see the described rendering issue within a Grid. The modification I mentioned above fixed the issue for me. But it would be great to see if that is also true in your case.

If it is the case, please file a ticket so I can push the fix to our repository. It will then be part of the SNAPSHOT builds and released with 1.4.0
Re: Custom cell painter example bugs [message #1706865 is a reply to message #1706863] Mon, 31 August 2015 14:16 Go to previous messageGo to next message
Ines El Euch is currently offline Ines El EuchFriend
Messages: 35
Registered: March 2015
Member
Hi,

I really hope it will fix it for me too. But I didn't understand your expalantion if you could please share the portion of code that you mentioned, that would be really great. Thanks a lot 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 Go to previous messageGo to next message
Ines El Euch is currently offline Ines El EuchFriend
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 Sad . 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 #1706877 is a reply to message #1706874] Mon, 31 August 2015 14:48 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
The reason for the wrong colours seem to be a class issue, because the config attributes are defined in the painter. IMHO they shouldn't be there, but that is another issue. So changing the imports should solve the issue for you.

Do you have a grid composition? Because I had exactly the same rendering issue as shown in your screenshot, and it was caused by the missing offset that is added because of the row header in the grid. So my modification should solve the issue. At least if I understand the issue correctly.
Re: Custom cell painter example bugs [message #1706878 is a reply to message #1706877] Mon, 31 August 2015 15:07 Go to previous messageGo to next message
Ines El Euch is currently offline Ines El EuchFriend
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);
    }

}

Re: Custom cell painter example bugs [message #1706880 is a reply to message #1706878] Mon, 31 August 2015 15:11 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
Because you only created the new class, but did not create a new PercentageBarCellPainter that uses your custom decorator. Wink

There is no injection magic in NatTable ATM.
Re: Custom cell painter example bugs [message #1706883 is a reply to message #1706880] Mon, 31 August 2015 15:20 Go to previous messageGo to next message
Ines El Euch is currently offline Ines El EuchFriend
Messages: 35
Registered: March 2015
Member
Yes I figured it out when I was looking for the reason why it wasn't called Very Happy , It would be nice to have magic injection in the future though Razz
I am very happy to announce that it worked finally. Thanks a lot Sir, I could'nt have found the solution without your help.

Inès E.
Re: Custom cell painter example bugs [message #1706911 is a reply to message #1706883] Mon, 31 August 2015 18:16 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
Could you please create a ticket pointing to this forum post?
Re: Custom cell painter example bugs [message #1706946 is a reply to message #1706911] Tue, 01 September 2015 07:19 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
I created the ticket and pushed the fix to the repository already:

https://bugs.eclipse.org/bugs/show_bug.cgi?id=476273
Re: Custom cell painter example bugs [message #1707007 is a reply to message #1706946] Tue, 01 September 2015 12:55 Go to previous messageGo to next message
Ines El Euch is currently offline Ines El EuchFriend
Messages: 35
Registered: March 2015
Member
Hello,

Sorry didn't notice your last message so do I need to make the ticket after all or did you do it ?
Re: Custom cell painter example bugs [message #1707010 is a reply to message #1707007] Tue, 01 September 2015 13:10 Go to previous message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
Nope, everything is fine. Thanks for reporting. Smile
Previous Topic:Disable row selection in NatTable
Next Topic:Tree Example With the Filter Problem
Goto Forum:
  


Current Time: Fri Mar 29 02:15:59 GMT 2024

Powered by FUDForum. Page generated in 0.03580 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top