]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/GEIconPainter.java
Sync git svn branch with SVN repository r33144.
[simantics/platform.git] / bundles / org.simantics.browsing.ui.nattable / src / org / simantics / browsing / ui / nattable / GEIconPainter.java
1 package org.simantics.browsing.ui.nattable;\r
2 \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
17 \r
18 /**\r
19  * Modified org.eclipse.nebula.widgets.nattable.painter.cell.ImagePainter, which does not allow setting wrapped painter\r
20  * \r
21  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
22  *\r
23  */\r
24 public class GEIconPainter extends BackgroundPainter {\r
25         \r
26     protected boolean calculateByWidth;\r
27     protected boolean calculateByHeight;\r
28         \r
29         public GEIconPainter(ICellPainter painter) {\r
30                 super(painter);\r
31         }\r
32         \r
33          @Override\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
38                 } else {\r
39                     return 0;\r
40                 }\r
41             }\r
42 \r
43             @Override\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
48                 } else {\r
49                     return 0;\r
50                 }\r
51             }\r
52 \r
53             @Override\r
54             public ICellPainter getCellPainterAt(int x, int y, ILayerCell cell, GC gc,\r
55                     Rectangle bounds, IConfigRegistry configRegistry) {\r
56 \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
61                     int x0 = bounds.x\r
62                             + CellStyleUtil.getHorizontalAlignmentPadding(\r
63                                     cellStyle, bounds, imageBounds.width);\r
64                     int y0 = bounds.y\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
70                     }\r
71                 }\r
72                 return null;\r
73             }\r
74 \r
75             @Override\r
76             public void paintCell(ILayerCell cell, GC gc, Rectangle bounds, IConfigRegistry configRegistry) {\r
77                \r
78                 \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
83 \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
89                                 layer,\r
90                                 cell.getRowPosition(),\r
91                                 contentHeight + contentToCellDiff));\r
92                     }\r
93 \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
99                                 layer,\r
100                                 cell.getColumnPosition(),\r
101                                 contentWidth + contentToCellDiff));\r
102                     }\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
107                     gc.drawImage(\r
108                             image,\r
109                             bounds.x + px,\r
110                             bounds.y + py);\r
111                 } else {\r
112                         super.paintCell(cell, gc, bounds, configRegistry);\r
113                 }\r
114             }\r
115             \r
116 //          @Override\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
125 //                      return b;\r
126 //                          \r
127 //              }\r
128 //              return super.getWrappedPainterBounds(cell, gc, bounds, configRegistry);\r
129 //          }\r
130 \r
131             /**\r
132              *\r
133              * @param cell\r
134              *            The {@link ILayerCell} for which this {@link ImagePainter} is\r
135              *            called.\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
141              */\r
142             protected Image getImage(ILayerCell cell, IConfigRegistry configRegistry) {\r
143                 return CellStyleUtil.getCellStyle(cell, configRegistry).getAttributeValue(CellStyleAttributes.IMAGE);\r
144             }\r
145 \r
146             /**\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
150              */\r
151             public boolean isCalculateByWidth() {\r
152                 return this.calculateByWidth;\r
153             }\r
154 \r
155             /**\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
159              *\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
164              */\r
165             public void setCalculateByWidth(boolean calculateByWidth) {\r
166                 this.calculateByWidth = calculateByWidth;\r
167             }\r
168 \r
169             /**\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
173              *         painter.\r
174              */\r
175             public boolean isCalculateByHeight() {\r
176                 return this.calculateByHeight;\r
177             }\r
178 \r
179             /**\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
183              *\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
188              */\r
189             public void setCalculateByHeight(boolean calculateByHeight) {\r
190                 this.calculateByHeight = calculateByHeight;\r
191             }\r
192 \r
193 }\r