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