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