Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » NatTable » GAP between the custom image and base content with background image(There is a gap between custom image and base content if have background image)
GAP between the custom image and base content with background image [message #1785750] Thu, 19 April 2018 10:47 Go to next message
chun liang Wen is currently offline chun liang WenFriend
Messages: 10
Registered: December 2017
Junior Member
Hi,

Experts.

I'm try to add an image to a cell which has background image. But there is a gap between the custom image and the cell's original content. (see my attachment)

my configuration code is :

public class ColumnHeaderStyleWithRedStarConfigurations extends DefaultColumnHeaderStyleConfiguration {
    private String columnWithRedStarLabel;
    public static final Image RED_STAR_ICON_IMAGE = GUIHelper.getImageByURL(Images.getURL(Images.COLUMN_HEADER_BACKGROUND_IMAGE_WITH_RED_STAR));
    public static final Image COLUMN_HEADER_BACKGROUND_ICON_IMAGE = GUIHelper.getImageByURL(Images.getURL(Images.COLUMN_HEADER_BACKGROUND_IMAGE));

    public ColumnHeaderStyleWithRedStarConfigurations(String columnWithRedStarLabel) {
        this.columnWithRedStarLabel = columnWithRedStarLabel;
    }

    @Override
    public void configureRegistry(IConfigRegistry configRegistry) {
        Style cellStyle = new Style();

        cellStyle.setAttributeValue(CellStyleAttributes.HORIZONTAL_ALIGNMENT, HorizontalAlignmentEnum.LEFT);

        configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE, cellStyle, DisplayMode.NORMAL, columnWithRedStarLabel);
        configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_STYLE, cellStyle, DisplayMode.SELECT, columnWithRedStarLabel);

        configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_PAINTER, getCellPainterDecorator(), DisplayMode.NORMAL, columnWithRedStarLabel);

        configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_PAINTER, getCellPainterDecorator(), DisplayMode.SELECT, columnWithRedStarLabel);
    }

    private CellPainterDecorator getCellPainterDecorator() {
           /* try 1 */
        return new CellPainterDecorator(new BackgroundImagePainter(new TextPainter(true, false, false), COLUMN_HEADER_BACKGROUND_ICON_IMAGE), CellEdgeEnum.LEFT, new ImagePainter(RED_STAR_ICON_IMAGE));


        /* try 2 */
//        return new CellPainterDecorator(new ScalingBackgroundImagePainter(new TextPainter(true, false, false), COLUMN_HEADER_BACKGROUND_ICON_IMAGE,
//                ISSPTheme.COLOMN_HEADER_BACKGROUND_IMAGE), CellEdgeEnum.LEFT, new ImagePainter(RED_STAR_ICON_IMAGE));

        /* try 3 */
        // return new CellPainterDecorator(new TextPainter(true, false, false), CellEdgeEnum.LEFT, new ImagePainter(RED_STAR_ICON_IMAGE));

    }

}


I added the configuration to my NatTable instance:
 
natTable.addConfiguration(new ColumnHeaderStyleWithRedStarConfigurations(COLUMN_WITH_RED_STAR_LABEL));


I've added the label COLUMN_WITH_RED_STAR_LABEL for the columns I want,

see code :

 private void updateColumnNames() {
        Set<Integer> filterIndexes = selectedFilterValues.keySet();
       ColumnOverrideLabelAccumulator labelAccumulator = new ColumnOverrideLabelAccumulator(costTable.getColumnHeaderLayer());
        for (int i = 0; i < costTable.getColumns().size(); i++) {
            String colname = costTable.getColumns().get(i).getColumnName();
            if (NAME_OF_TOTAL_COLUMN.equals(colname)) {
                break;
            }
            Column col = costTable.getColumns().get(i);
            boolean hasStar = col.getColumnName().startsWith(StringUtils.SPACE); // placeholder "space" with the column means that the column is filtered
            boolean filteredCol = filterIndexes.contains(i);
            if (hasStar && !filteredCol) {
                costTable.getColumns().get(i).setColumnName(colname.substring(1)); // remove the "space"
            } else if (!hasStar && filteredCol) {
                col.setColumnName(StringUtils.SPACE + colname); // add the "space"
               labelAccumulator.registerColumnOverridesOnTop(i, COLUMN_WITH_RED_STAR_LABEL);
            }
        }

        costTable.getColumnHeaderLayer().setConfigLabelAccumulator(labelAccumulator);

        costTable.refreshColumnLabels();
        costTable.refreshData();
    }


Is there a way to solve this problem? Or would you please give some advice?

Thanks in advance.


Re: GAP between the custom image and base content with background image [message #1785753 is a reply to message #1785750] Thu, 19 April 2018 10:57 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
The CellPainterDecorator also has a paintBackground configuration. Because of backwards compatibility it is set to true by default.

You can either use a constructor with all parameters or (my suggestion) use CellPainterDecorator#setPaintBackground(false) to avoid rendering the background which is causing the gap, by still using the simple constructor.
Re: GAP between the custom image and base content with background image [message #1785792 is a reply to message #1785753] Fri, 20 April 2018 02:50 Go to previous messageGo to next message
chun liang Wen is currently offline chun liang WenFriend
Messages: 10
Registered: December 2017
Junior Member
It worked after I change my code like this:

return new CellPainterDecorator(new ScalingBackgroundImagePainter(new TextPainter(true, false, false), COLUMN_HEADER_BACKGROUND_ICON_IMAGE,
                ISSPTheme.COLOMN_HEADER_BACKGROUND_IMAGE), CellEdgeEnum.LEFT, 0, new ImagePainter(RED_STAR_ICON_IMAGE), true, false);


The root cause is that for the constructor I used before there are 2 pixels as spacing between base and decoration painter. So, for my scenario, it should be 0.

Dirk, Thank you very much for your help.

Re: GAP between the custom image and base content with background image [message #1785793 is a reply to message #1785792] Fri, 20 April 2018 03:20 Go to previous messageGo to next message
chun liang Wen is currently offline chun liang WenFriend
Messages: 10
Registered: December 2017
Junior Member
And I've tried another solution using RichTextCellPainter . At first glance feeling no problem, but because it has a SortHeaderLayer above ColumnGroupHeaderLayer, if I click the column header, it will do sort action, and the column name will show the original html code I assigned to the column. Is there a way to resolve this issue?

If it can be fixed, which solution is better? the ImagePainter one OR this RichTextCellPainter one? Thanks very much for you help in advance.

my code snippet:

        // build the column header layer
        columnHeaderDataProvider = new IsspColumnHeaderDataProvider(columns);
        columnHeaderDataLayer = new DefaultColumnHeaderDataLayer(columnHeaderDataProvider);
        columnHeaderHoverLayer = new HoverLayer(columnHeaderDataLayer, false);
        columnHeaderLayer = new ColumnHeaderLayer(columnHeaderHoverLayer, bodyLayerStack, bodyLayerStack.getSelectionLayer(), false);
        columnHeaderLayer.addConfiguration(new ColumnHeaderHoverLayerConfiguration(columnHeaderHoverLayer));

        // create column groups
        columnGroupHeaderLayer = new IsspColumnGroupHeaderLayer(columnHeaderLayer, bodyLayerStack.getSelectionLayer(), bodyLayerStack.getColumnGroupModel());
        columnGroupHeaderLayer.setDisplayHeaderLabel(displayColumnGroupLabels);
        createColumnGroups(columnGroupHeaderLayer);

        // calculate the height of the column header area dependent if column groups exist or not
        columnGroupHeaderLayer.setCalculateHeight(true);

        if (hasSortedHeader) {
            configRegistry.registerConfigAttribute(SortConfigAttributes.SORT_COMPARATOR, IsspTableGenerator.this.getCustomComparator(), DisplayMode.NORMAL,
                    CUSTOM_NUMBER_COMPARATOR_LABEL);

            sortHeaderLayer = new SortHeaderLayer<>(columnGroupHeaderLayer,
                    new GlazedListsSortModel<>(bodyLayerStack.getSortedList(), columnPropertyAccessor, configRegistry, columnHeaderHoverLayer), false);
            sortHeaderLayer.addConfiguration(new SingleClickSortConfiguration(new BeveledBorderDecorator(new SortableHeaderTextPainter(new RichTextCellPainter()))));

        }

        // as the auto configuration of the NatTable is turned off, we have to
        // add the DefaultNatTableStyleConfiguration and the ConfigRegistry manually
        ColumnOverrideLabelAccumulator columnLabelHeaderAccumulator = new ColumnOverrideLabelAccumulator(columnHeaderHoverLayer);
        columnHeaderHoverLayer.setConfigLabelAccumulator(columnLabelHeaderAccumulator);
        for (int i = 0; i < columns.size(); i++) {
            if (columns.get(i).hasNumericData()) {
                columnLabelHeaderAccumulator.registerColumnOverrides(i, CUSTOM_NUMBER_COMPARATOR_LABEL);
            }
        }


add the label code snippet is :

private void updateColumnNames() {
        Set<Integer> filterIndexes = selectedFilterValues.keySet();
        ColumnOverrideLabelAccumulator labelAccumulator = new ColumnOverrideLabelAccumulator(costTable.getColumnHeaderLayer());
        for (int i = 0; i < costTable.getColumns().size(); i++) {
            String colname = costTable.getColumns().get(i).getColumnName();
            if ("Total".equals(colname)) {
                break;
            }
            Column col = costTable.getColumns().get(i);
            boolean hasStar = col.getColumnName().startsWith("<span style=\"color:rgb(255,0,0)\">*</span>"); // placeholder a red *
            boolean filteredCol = filterIndexes.contains(i);
            if (hasStar && !filteredCol) {
                costTable.getColumns().get(i).setColumnName(colname.substring(41));
            } else if (!hasStar && filteredCol) {
                col.setColumnName("<span style=\"color:rgb(255,0,0)\">*</span>" + colname);

                labelAccumulator.registerColumnOverridesOnTop(i, COLUMN_WITH_RED_STAR_LABEL);
            }
        }

        costTable.getColumnHeaderLayer().setConfigLabelAccumulator(labelAccumulator);

        costTable.refreshColumnLabels();
        costTable.refreshData();
    }
Re: GAP between the custom image and base content with background image [message #1785799 is a reply to message #1785793] Fri, 20 April 2018 05:53 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
If you see the HTML code, it means the RichtTextCellPainter is not in use. Maybe because you set your COLUMN_WITH_RED_STAR_LABEL on top of the layer stack, so it wins over the sort related labels, and therefore the painter you register for your label is taken in advance to the SortableHeaderTextPainter. You need to debug in CellLayerPainter#paintCell() to see which painter is used for your sorted column header and why. My wild guess is an issue with the label stack.
Re: GAP between the custom image and base content with background image [message #1785812 is a reply to message #1785799] Fri, 20 April 2018 08:32 Go to previous messageGo to next message
chun liang Wen is currently offline chun liang WenFriend
Messages: 10
Registered: December 2017
Junior Member
I found when I click the column header, the label stack for the SortHeaderLayer will be
[SORT, SORT_UP, SORT_SEQ_0, columnwithredstar]
. Is this the cause why RichTextCellPainter is not used?
Re: GAP between the custom image and base content with background image [message #1785815 is a reply to message #1785812] Fri, 20 April 2018 08:44 Go to previous messageGo to next message
Dirk Fauth is currently offline Dirk FauthFriend
Messages: 2902
Registered: July 2012
Senior Member
might be ... depends on what is registered. from what you posted it should work, but I don't know which painter is returned for SORT
Re: GAP between the custom image and base content with background image [message #1785996 is a reply to message #1785815] Tue, 24 April 2018 02:36 Go to previous message
chun liang Wen is currently offline chun liang WenFriend
Messages: 10
Registered: December 2017
Junior Member
Thanks for your reply. Assuming it's true, I will abort this solution. Because image one works fine.
Previous Topic:Custom persistence behavior and modifying the PersistenceDialog
Next Topic:Null Pointer Exception when adding new data to eventlist
Goto Forum:
  


Current Time: Thu Apr 18 16:33:37 GMT 2024

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

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

Back to the top