package org.simantics.browsing.ui.nattable; import org.eclipse.nebula.widgets.nattable.config.IConfigRegistry; import org.eclipse.nebula.widgets.nattable.layer.ILayer; import org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell; import org.eclipse.nebula.widgets.nattable.painter.cell.BackgroundPainter; import org.eclipse.nebula.widgets.nattable.painter.cell.ICellPainter; import org.eclipse.nebula.widgets.nattable.painter.cell.ImagePainter; import org.eclipse.nebula.widgets.nattable.resize.command.ColumnResizeCommand; import org.eclipse.nebula.widgets.nattable.resize.command.RowResizeCommand; import org.eclipse.nebula.widgets.nattable.style.CellStyleAttributes; 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; /** * Modified org.eclipse.nebula.widgets.nattable.painter.cell.ImagePainter, which does not allow setting wrapped painter * * @author Marko Luukkainen * */ public class GEIconPainter extends BackgroundPainter { protected boolean calculateByWidth; protected boolean calculateByHeight; public GEIconPainter(ICellPainter painter) { super(painter); } @Override public int getPreferredWidth(ILayerCell cell, GC gc, IConfigRegistry configRegistry) { Image image = getImage(cell, configRegistry); if (image != null) { return image.getBounds().width; } else { return 0; } } @Override public int getPreferredHeight(ILayerCell cell, GC gc, IConfigRegistry configRegistry) { Image image = getImage(cell, configRegistry); if (image != null) { return image.getBounds().height; } else { return 0; } } @Override public ICellPainter getCellPainterAt(int x, int y, ILayerCell cell, GC gc, Rectangle bounds, IConfigRegistry configRegistry) { Image image = getImage(cell, configRegistry); if (image != null) { Rectangle imageBounds = image.getBounds(); IStyle cellStyle = CellStyleUtil.getCellStyle(cell, configRegistry); int x0 = bounds.x + CellStyleUtil.getHorizontalAlignmentPadding( cellStyle, bounds, imageBounds.width); int y0 = bounds.y + CellStyleUtil.getVerticalAlignmentPadding( cellStyle, bounds, imageBounds.height); if (x >= x0 && x < x0 + imageBounds.width && y >= y0 && y < y0 + imageBounds.height) { return super.getCellPainterAt(x, y, cell, gc, bounds, configRegistry); } } return null; } @Override public void paintCell(ILayerCell cell, GC gc, Rectangle bounds, IConfigRegistry configRegistry) { Image image = getImage(cell, configRegistry); if (image != null) { Rectangle imageBounds = image.getBounds(); IStyle cellStyle = CellStyleUtil.getCellStyle(cell, configRegistry); int contentHeight = imageBounds.height; if (this.calculateByHeight && (contentHeight > bounds.height)) { int contentToCellDiff = (cell.getBounds().height - bounds.height); ILayer layer = cell.getLayer(); layer.doCommand(new RowResizeCommand( layer, cell.getRowPosition(), contentHeight + contentToCellDiff)); } int contentWidth = imageBounds.width; if (this.calculateByWidth && (contentWidth > bounds.width)) { int contentToCellDiff = (cell.getBounds().width - bounds.width); ILayer layer = cell.getLayer(); layer.doCommand(new ColumnResizeCommand( layer, cell.getColumnPosition(), contentWidth + contentToCellDiff)); } int px = CellStyleUtil.getHorizontalAlignmentPadding(cellStyle, bounds, imageBounds.width); int py = CellStyleUtil.getVerticalAlignmentPadding(cellStyle, bounds, imageBounds.height); Rectangle b = new Rectangle(bounds.x + px + imageBounds.width, bounds.y, bounds.width - px - imageBounds.width, bounds.height); super.paintCell(cell, gc, b, configRegistry); gc.drawImage( image, bounds.x + px, bounds.y + py); } else { super.paintCell(cell, gc, bounds, configRegistry); } } // @Override // public Rectangle getWrappedPainterBounds(ILayerCell cell, GC gc, Rectangle bounds, IConfigRegistry configRegistry) { // Image image = getImage(cell, configRegistry); // if (image != null) { // Rectangle imageBounds = image.getBounds(); // IStyle cellStyle = CellStyleUtil.getCellStyle(cell, configRegistry); // int px = CellStyleUtil.getHorizontalAlignmentPadding(cellStyle, bounds, imageBounds.width); // int py = CellStyleUtil.getVerticalAlignmentPadding(cellStyle, bounds, imageBounds.height); // Rectangle b = new Rectangle(bounds.x + px + imageBounds.width, bounds.y, bounds.width - px - imageBounds.width, bounds.height); // return b; // // } // return super.getWrappedPainterBounds(cell, gc, bounds, configRegistry); // } /** * * @param cell * The {@link ILayerCell} for which this {@link ImagePainter} is * called. * @param configRegistry * The current {@link IConfigRegistry} to retrieve the cell style * information from. * @return The {@link Image} that should be painted by this * {@link ImagePainter}. */ protected Image getImage(ILayerCell cell, IConfigRegistry configRegistry) { return CellStyleUtil.getCellStyle(cell, configRegistry).getAttributeValue(CellStyleAttributes.IMAGE); } /** * @return true if this {@link ImagePainter} is resizing the * cell width to show the whole configured image, false * if the cell width is not touched by this painter. */ public boolean isCalculateByWidth() { return this.calculateByWidth; } /** * Configure whether the {@link ImagePainter} should calculate the cell * dimensions by containing image width. This means the width of the * cell is calculated by image width. * * @param calculateByWidth * true to calculate and modify the cell dimension * according to the image width, false to not * modifying the cell dimensions. */ public void setCalculateByWidth(boolean calculateByWidth) { this.calculateByWidth = calculateByWidth; } /** * @return true if this {@link ImagePainter} is resizing the * cell height to show the whole configured image, * false if the cell height is not touched by this * painter. */ public boolean isCalculateByHeight() { return this.calculateByHeight; } /** * Configure whether the {@link ImagePainter} should calculate the cell * dimensions by containing image height. This means the height of * the cell is calculated by image height. * * @param calculateByHeight * true to calculate and modify the cell dimension * according to the image height, false to not * modifying the cell dimensions. */ public void setCalculateByHeight(boolean calculateByHeight) { this.calculateByHeight = calculateByHeight; } }