--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.scenegraph.g2d.nodes;\r
+\r
+import java.awt.Color;\r
+import java.awt.Font;\r
+import java.awt.FontMetrics;\r
+import java.awt.Graphics2D;\r
+import java.awt.RenderingHints;\r
+import java.awt.geom.AffineTransform;\r
+import java.awt.geom.Rectangle2D;\r
+\r
+import org.simantics.scenegraph.g2d.IG2DNode;\r
+import org.simantics.scenegraph.utils.TextUtil;\r
+\r
+public class GalleryItemNode extends TransformNode {\r
+\r
+ private static final long serialVersionUID = -423827087534629381L;\r
+\r
+ protected String text = "";\r
+ protected Rectangle2D bounds = null;\r
+ protected Rectangle2D imagebounds = null;\r
+ protected Font font = null;\r
+\r
+ @SyncField({"text", "bounds", "font", "imagebounds"})\r
+ public void init(String text, Rectangle2D bounds, Font font, Rectangle2D imagebounds) {\r
+ this.text = text;\r
+ this.bounds = bounds;\r
+ this.font = font;\r
+ this.imagebounds = imagebounds;\r
+ }\r
+\r
+ @Override\r
+ public void render(Graphics2D g2d) {\r
+ double contentDim = Math.min(bounds.getWidth(), bounds.getHeight());\r
+\r
+ AffineTransform ot = g2d.getTransform();\r
+\r
+ if(!transform.isIdentity()) {\r
+ g2d.transform(transform);\r
+ }\r
+\r
+ if (text != null) {\r
+ Graphics2D g = (Graphics2D) g2d.create();\r
+\r
+ g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);\r
+ //Rectangle2D textRectangle = new Rectangle2D.Double(0,0, 0, bounds.getHeight()-contentDim);\r
+ Rectangle2D textRectangle = new Rectangle2D.Double(0, contentDim, bounds.getWidth(), bounds.getHeight()-contentDim);\r
+ g.setClip(textRectangle);\r
+ g.setColor(Color.BLACK);\r
+\r
+ g.setFont(font); // FIXME\r
+ FontMetrics metrics = g.getFontMetrics();\r
+\r
+ int fontHeight = metrics.getHeight();\r
+ float width = (float) bounds.getWidth();\r
+ float y = (float) bounds.getWidth() + fontHeight;\r
+ int i = 0;\r
+ for (String s : TextUtil.wordWrap(text, metrics, (int) textRectangle.getWidth())) {\r
+ int sWidth = metrics.stringWidth(s);\r
+ g.drawString(s, (width - sWidth) / 2, y + i * fontHeight);\r
+ ++i;\r
+ }\r
+\r
+ g.dispose();\r
+ }\r
+\r
+ for(IG2DNode n : getSortedNodes()) {\r
+ double imgMaxDim = Math.max(imagebounds.getWidth(), imagebounds.getHeight());\r
+ double scaleFactor = contentDim / imgMaxDim;\r
+ // Offsets for centering the image and the shadow\r
+ double xCenteringOffset = -(imagebounds.getWidth() - imgMaxDim) / 2;\r
+ double yCenteringOffset = -(imagebounds.getHeight() - imgMaxDim) / 2;\r
+\r
+ AffineTransform at = g2d.getTransform();\r
+ g2d.scale(scaleFactor, scaleFactor);\r
+ g2d.translate(-imagebounds.getMinX(), -imagebounds.getMinY());\r
+ g2d.translate(xCenteringOffset, yCenteringOffset);\r
+ n.render(g2d);\r
+ g2d.setTransform(at);\r
+ }\r
+\r
+ g2d.setTransform(ot);\r
+ }\r
+\r
+ @Override\r
+ public Rectangle2D getBoundsInLocal() {\r
+ return bounds;\r
+ }\r
+\r
+ @Override\r
+ public String toString() {\r
+ return super.toString() + " [text=" + text + "]";\r
+ }\r
+\r
+}\r