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