1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.scenegraph.g2d.nodes;
14 import java.awt.Color;
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;
22 import org.simantics.scenegraph.g2d.IG2DNode;
23 import org.simantics.scenegraph.utils.TextUtil;
25 public class GalleryItemNode extends TransformNode {
27 private static final long serialVersionUID = -423827087534629381L;
29 protected String text = "";
30 protected Rectangle2D bounds = null;
31 protected Rectangle2D imagebounds = null;
32 protected Font font = null;
34 @SyncField({"text", "bounds", "font", "imagebounds"})
35 public void init(String text, Rectangle2D bounds, Font font, Rectangle2D imagebounds) {
39 this.imagebounds = imagebounds;
43 public void render(Graphics2D g2d) {
44 double contentDim = Math.min(bounds.getWidth(), bounds.getHeight());
46 AffineTransform ot = g2d.getTransform();
48 if(!transform.isIdentity()) {
49 g2d.transform(transform);
53 Graphics2D g = (Graphics2D) g2d.create();
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);
61 g.setFont(font); // FIXME
62 FontMetrics metrics = g.getFontMetrics();
64 int fontHeight = metrics.getHeight();
65 float width = (float) bounds.getWidth();
66 float y = (float) bounds.getWidth() + fontHeight;
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);
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;
84 AffineTransform at = g2d.getTransform();
85 g2d.scale(scaleFactor, scaleFactor);
86 g2d.translate(-imagebounds.getMinX(), -imagebounds.getMinY());
87 g2d.translate(xCenteringOffset, yCenteringOffset);
96 public Rectangle2D getBoundsInLocal() {
101 public String toString() {
102 return super.toString() + " [text=" + text + "]";