1 package org.simantics.browsing.ui.nattable;
3 import org.eclipse.nebula.widgets.nattable.config.IConfigRegistry;
4 import org.eclipse.nebula.widgets.nattable.layer.ILayer;
5 import org.eclipse.nebula.widgets.nattable.layer.cell.ILayerCell;
6 import org.eclipse.nebula.widgets.nattable.painter.cell.BackgroundPainter;
7 import org.eclipse.nebula.widgets.nattable.painter.cell.ICellPainter;
8 import org.eclipse.nebula.widgets.nattable.painter.cell.ImagePainter;
9 import org.eclipse.nebula.widgets.nattable.resize.command.ColumnResizeCommand;
10 import org.eclipse.nebula.widgets.nattable.resize.command.RowResizeCommand;
11 import org.eclipse.nebula.widgets.nattable.style.CellStyleAttributes;
12 import org.eclipse.nebula.widgets.nattable.style.CellStyleUtil;
13 import org.eclipse.nebula.widgets.nattable.style.IStyle;
14 import org.eclipse.swt.graphics.GC;
15 import org.eclipse.swt.graphics.Image;
16 import org.eclipse.swt.graphics.Rectangle;
19 * Modified org.eclipse.nebula.widgets.nattable.painter.cell.ImagePainter, which does not allow setting wrapped painter
21 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
24 public class GEIconPainter extends BackgroundPainter {
26 protected boolean calculateByWidth;
27 protected boolean calculateByHeight;
29 public GEIconPainter(ICellPainter painter) {
34 public int getPreferredWidth(ILayerCell cell, GC gc, IConfigRegistry configRegistry) {
35 Image image = getImage(cell, configRegistry);
37 return image.getBounds().width;
44 public int getPreferredHeight(ILayerCell cell, GC gc, IConfigRegistry configRegistry) {
45 Image image = getImage(cell, configRegistry);
47 return image.getBounds().height;
54 public ICellPainter getCellPainterAt(int x, int y, ILayerCell cell, GC gc,
55 Rectangle bounds, IConfigRegistry configRegistry) {
57 Image image = getImage(cell, configRegistry);
59 Rectangle imageBounds = image.getBounds();
60 IStyle cellStyle = CellStyleUtil.getCellStyle(cell, configRegistry);
62 + CellStyleUtil.getHorizontalAlignmentPadding(
63 cellStyle, bounds, imageBounds.width);
65 + CellStyleUtil.getVerticalAlignmentPadding(
66 cellStyle, bounds, imageBounds.height);
67 if (x >= x0 && x < x0 + imageBounds.width
68 && y >= y0 && y < y0 + imageBounds.height) {
69 return super.getCellPainterAt(x, y, cell, gc, bounds, configRegistry);
76 public void paintCell(ILayerCell cell, GC gc, Rectangle bounds, IConfigRegistry configRegistry) {
79 Image image = getImage(cell, configRegistry);
81 Rectangle imageBounds = image.getBounds();
82 IStyle cellStyle = CellStyleUtil.getCellStyle(cell, configRegistry);
84 int contentHeight = imageBounds.height;
85 if (this.calculateByHeight && (contentHeight > bounds.height)) {
86 int contentToCellDiff = (cell.getBounds().height - bounds.height);
87 ILayer layer = cell.getLayer();
88 layer.doCommand(new RowResizeCommand(
90 cell.getRowPosition(),
91 contentHeight + contentToCellDiff));
94 int contentWidth = imageBounds.width;
95 if (this.calculateByWidth && (contentWidth > bounds.width)) {
96 int contentToCellDiff = (cell.getBounds().width - bounds.width);
97 ILayer layer = cell.getLayer();
98 layer.doCommand(new ColumnResizeCommand(
100 cell.getColumnPosition(),
101 contentWidth + contentToCellDiff));
103 int px = CellStyleUtil.getHorizontalAlignmentPadding(cellStyle, bounds, imageBounds.width);
104 int py = CellStyleUtil.getVerticalAlignmentPadding(cellStyle, bounds, imageBounds.height);
105 Rectangle b = new Rectangle(bounds.x + px + imageBounds.width, bounds.y, bounds.width - px - imageBounds.width, bounds.height);
106 super.paintCell(cell, gc, b, configRegistry);
112 super.paintCell(cell, gc, bounds, configRegistry);
117 // public Rectangle getWrappedPainterBounds(ILayerCell cell, GC gc, Rectangle bounds, IConfigRegistry configRegistry) {
118 // Image image = getImage(cell, configRegistry);
119 // if (image != null) {
120 // Rectangle imageBounds = image.getBounds();
121 // IStyle cellStyle = CellStyleUtil.getCellStyle(cell, configRegistry);
122 // int px = CellStyleUtil.getHorizontalAlignmentPadding(cellStyle, bounds, imageBounds.width);
123 // int py = CellStyleUtil.getVerticalAlignmentPadding(cellStyle, bounds, imageBounds.height);
124 // Rectangle b = new Rectangle(bounds.x + px + imageBounds.width, bounds.y, bounds.width - px - imageBounds.width, bounds.height);
128 // return super.getWrappedPainterBounds(cell, gc, bounds, configRegistry);
134 * The {@link ILayerCell} for which this {@link ImagePainter} is
136 * @param configRegistry
137 * The current {@link IConfigRegistry} to retrieve the cell style
139 * @return The {@link Image} that should be painted by this
140 * {@link ImagePainter}.
142 protected Image getImage(ILayerCell cell, IConfigRegistry configRegistry) {
143 return CellStyleUtil.getCellStyle(cell, configRegistry).getAttributeValue(CellStyleAttributes.IMAGE);
147 * @return <code>true</code> if this {@link ImagePainter} is resizing the
148 * cell width to show the whole configured image, <code>false</code>
149 * if the cell width is not touched by this painter.
151 public boolean isCalculateByWidth() {
152 return this.calculateByWidth;
156 * Configure whether the {@link ImagePainter} should calculate the cell
157 * dimensions by containing image width. This means the <b>width</b> of the
158 * cell is calculated by image width.
160 * @param calculateByWidth
161 * <code>true</code> to calculate and modify the cell dimension
162 * according to the image width, <code>false</code> to not
163 * modifying the cell dimensions.
165 public void setCalculateByWidth(boolean calculateByWidth) {
166 this.calculateByWidth = calculateByWidth;
170 * @return <code>true</code> if this {@link ImagePainter} is resizing the
171 * cell height to show the whole configured image,
172 * <code>false</code> if the cell height is not touched by this
175 public boolean isCalculateByHeight() {
176 return this.calculateByHeight;
180 * Configure whether the {@link ImagePainter} should calculate the cell
181 * dimensions by containing image height. This means the <b>height</b> of
182 * the cell is calculated by image height.
184 * @param calculateByHeight
185 * <code>true</code> to calculate and modify the cell dimension
186 * according to the image height, <code>false</code> to not
187 * modifying the cell dimensions.
189 public void setCalculateByHeight(boolean calculateByHeight) {
190 this.calculateByHeight = calculateByHeight;