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