Skip to content

Instantly share code, notes, and snippets.

@brsanthu
Created February 28, 2018 17:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save brsanthu/cd2f91da7777aa994e011f7acedd900a to your computer and use it in GitHub Desktop.
Save brsanthu/cd2f91da7777aa994e011f7acedd900a to your computer and use it in GitHub Desktop.
Nattable Combination (Multiple Images) Image Painter
import java.util.ArrayList;
import java.util.List;
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.BackgroundPainter;
import org.eclipse.nebula.widgets.nattable.style.CellStyleUtil;
import org.eclipse.nebula.widgets.nattable.style.IStyle;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
/**
* Paints the one or more images in a given cell based on the labels. Since label check happens only after this painter
* has been invoked, it is required that this painter be registered with a always on label. Then this paint will decide
* which image to paint based on other configured labels.
*
* Below is an example of this usage.
*
* <pre>
* CombinationImagePainter painter = new CombinationImagePainter(4, true);
*
* //Replace ... below with appropriate images
* painter.add("CREATE", ...);
* painter.add("NO_CREATE", ...);
* painter.add("READ", ...);
* painter.add("NO_READ", ...);
* painter.add("UPDATE", ...);
* painter.add("NO_UPDATE", ...);
* painter.add("DELETE", ...);
* painter.add("NO_DELETE", ...);
*
* // Here COMBINATION_PAINTER is label that needs to be added by data provider whenever this painter needs to be invoked
* configRegistry.registerConfigAttribute(CellConfigAttributes.CELL_PAINTER, painter, DisplayMode.NORMAL, "COMBINATION_PAINTER");
* </pre>
*/
public class CombinationImagePainter extends BackgroundPainter {
private List<LabelImagePair> labelImages = new ArrayList<>();
private final boolean paintBg;
private int spacing;
/**
* Creates a combination painter
*
* @param spacing number of pixels of spacing between each image. It can be -ve, in that case images will overlap
* @param paintBg paint the background or not
*/
public CombinationImagePainter(int spacing, boolean paintBg) {
this.spacing = spacing;
this.paintBg = paintBg;
}
/**
* Adds a given label and image combination
*/
public CombinationImagePainter add(String label, Image image) {
labelImages.add(new LabelImagePair(label, image));
return this;
}
@Override
public int getPreferredWidth(ILayerCell cell, GC gc, IConfigRegistry configRegistry) {
int width = 0;
for (Image image : getImages(cell)) {
if (width != 0) {
width += spacing;
}
width += image.getBounds().width;
}
return width;
}
protected List<Image> getImages(ILayerCell cell) {
List<Image> images = new ArrayList<>();
for (LabelImagePair labelImage : labelImages) {
if (cell.getConfigLabels().hasLabel(labelImage.label)) {
images.add(labelImage.image);
}
}
return images;
}
@Override
public int getPreferredHeight(ILayerCell cell, GC gc, IConfigRegistry configRegistry) {
int height = 0;
for (Image image : getImages(cell)) {
height = Math.max(height, image.getBounds().height);
}
return height;
}
@Override
public void paintCell(ILayerCell cell, GC gc, Rectangle bounds, IConfigRegistry configRegistry) {
if (this.paintBg) {
super.paintCell(cell, gc, bounds, configRegistry);
}
IStyle cellStyle = CellStyleUtil.getCellStyle(cell, configRegistry);
List<Image> images = getImages(cell);
int boundX = bounds.x + CellStyleUtil.getHorizontalAlignmentPadding(cellStyle, bounds, getPreferredWidth(cell, gc, configRegistry));
int paddingY = CellStyleUtil.getVerticalAlignmentPadding(cellStyle, bounds, getPreferredHeight(cell, gc, configRegistry));
for (Image image : images) {
Rectangle imageBounds = image.getBounds();
gc.drawImage(image, boundX, bounds.y + paddingY);
boundX += imageBounds.width + spacing;
}
}
}
class LabelImagePair {
public final String label;
public final Image image;
public LabelImagePair(String label, Image image) {
this.label = label;
this.image = image;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment