]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/GalleryItemNode.java
Merge commit 'd186091'
[simantics/platform.git] / bundles / org.simantics.scenegraph / src / org / simantics / scenegraph / g2d / nodes / GalleryItemNode.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.scenegraph.g2d.nodes;\r
13 \r
14 import java.awt.Color;\r
15 import java.awt.Font;\r
16 import java.awt.FontMetrics;\r
17 import java.awt.Graphics2D;\r
18 import java.awt.RenderingHints;\r
19 import java.awt.geom.AffineTransform;\r
20 import java.awt.geom.Rectangle2D;\r
21 \r
22 import org.simantics.scenegraph.g2d.IG2DNode;\r
23 import org.simantics.scenegraph.utils.TextUtil;\r
24 \r
25 public class GalleryItemNode extends TransformNode {\r
26 \r
27     private static final long serialVersionUID = -423827087534629381L;\r
28 \r
29     protected String text = "";\r
30     protected Rectangle2D bounds = null;\r
31     protected Rectangle2D imagebounds = null;\r
32     protected Font font = null;\r
33 \r
34     @SyncField({"text", "bounds", "font", "imagebounds"})\r
35     public void init(String text, Rectangle2D bounds, Font font, Rectangle2D imagebounds) {\r
36         this.text = text;\r
37         this.bounds = bounds;\r
38         this.font = font;\r
39         this.imagebounds = imagebounds;\r
40     }\r
41 \r
42     @Override\r
43     public void render(Graphics2D g2d) {\r
44         double contentDim = Math.min(bounds.getWidth(), bounds.getHeight());\r
45 \r
46         AffineTransform ot = g2d.getTransform();\r
47 \r
48         if(!transform.isIdentity()) {\r
49             g2d.transform(transform);\r
50         }\r
51 \r
52         if (text != null) {\r
53             Graphics2D g = (Graphics2D) g2d.create();\r
54 \r
55             g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);\r
56             //Rectangle2D textRectangle = new Rectangle2D.Double(0,0, 0, bounds.getHeight()-contentDim);\r
57             Rectangle2D textRectangle = new Rectangle2D.Double(0, contentDim, bounds.getWidth(), bounds.getHeight()-contentDim);\r
58             g.setClip(textRectangle);\r
59             g.setColor(Color.BLACK);\r
60 \r
61             g.setFont(font); // FIXME\r
62             FontMetrics metrics = g.getFontMetrics();\r
63 \r
64             int fontHeight = metrics.getHeight();\r
65             float width = (float) bounds.getWidth();\r
66             float y = (float) bounds.getWidth() + fontHeight;\r
67             int i = 0;\r
68             for (String s : TextUtil.wordWrap(text, metrics, (int) textRectangle.getWidth())) {\r
69                 int sWidth = metrics.stringWidth(s);\r
70                 g.drawString(s, (width - sWidth) / 2,  y + i * fontHeight);\r
71                 ++i;\r
72             }\r
73 \r
74             g.dispose();\r
75         }\r
76 \r
77         for(IG2DNode n : getSortedNodes()) {\r
78             double imgMaxDim = Math.max(imagebounds.getWidth(), imagebounds.getHeight());\r
79             double scaleFactor = contentDim / imgMaxDim;\r
80             // Offsets for centering the image and the shadow\r
81             double xCenteringOffset = -(imagebounds.getWidth() - imgMaxDim) / 2;\r
82             double yCenteringOffset = -(imagebounds.getHeight() - imgMaxDim) / 2;\r
83 \r
84             AffineTransform at = g2d.getTransform();\r
85             g2d.scale(scaleFactor, scaleFactor);\r
86             g2d.translate(-imagebounds.getMinX(), -imagebounds.getMinY());\r
87             g2d.translate(xCenteringOffset, yCenteringOffset);\r
88             n.render(g2d);\r
89             g2d.setTransform(at);\r
90         }\r
91 \r
92         g2d.setTransform(ot);\r
93     }\r
94 \r
95     @Override\r
96     public Rectangle2D getBoundsInLocal() {\r
97         return bounds;\r
98     }\r
99 \r
100     @Override\r
101     public String toString() {\r
102         return super.toString() + " [text=" + text + "]";\r
103     }\r
104 \r
105 }\r