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.AlphaComposite;
15 import java.awt.BasicStroke;
16 import java.awt.Color;
17 import java.awt.Composite;
19 import java.awt.FontMetrics;
20 import java.awt.Graphics2D;
21 import java.awt.RenderingHints;
22 import java.awt.Stroke;
23 import java.awt.geom.Rectangle2D;
24 import java.awt.geom.RoundRectangle2D;
26 import org.simantics.scenegraph.g2d.G2DNode;
27 import org.simantics.scenegraph.utils.TextUtil;
30 * @author Tuukka Lehtonen
32 public class MessageNotificationNode extends G2DNode {
34 private static final long serialVersionUID = -7518450802171689793L;
36 private static final Stroke PROGRESS_STROKE = new BasicStroke(1f);
38 private Rectangle2D bounds;
40 private double margin;
41 private double progress;
42 private String message;
44 @SyncField({"bounds", "font", "margin", "progress", "message"})
45 public void init(Rectangle2D bounds, Font font, double margin, double progress, String message) {
49 this.progress = progress;
50 this.message = message;
54 public void render(Graphics2D g2d) {
55 double x = bounds.getX();
56 double y = bounds.getY();
57 double width = bounds.getWidth();
58 double height = bounds.getHeight();
62 RoundRectangle2D rr = new RoundRectangle2D.Double(x, y, width, height, rx, ry);
64 Composite oc = g2d.getComposite();
68 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
69 g2d.setComposite(AlphaComposite.SrcOver.derive(0.5f));
70 g2d.setPaint(Color.BLACK);
76 g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
77 g2d.setComposite(AlphaComposite.Src);
79 FontMetrics fm = g2d.getFontMetrics(font);
80 String[] lines = TextUtil.wordWrap(message, fm, (int) (width - 2*margin));
81 g2d.setColor(Color.WHITE);
82 float fh = fm.getHeight() + 1;
83 float tx = (float)(x + margin);
84 float ty = (float)(y + fm.getMaxAscent() + margin);
85 for (int i = 0; i < lines.length; ++i) {
86 if ((ty + fm.getMaxDescent() + margin) > rr.getMaxY())
88 g2d.drawString(lines[i], tx, ty);
95 g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
96 float c = (float) (0.15f + 0.75f*Math.sin(progress*Math.PI));
97 if(c >= 0 && c <= 1.0f)
98 g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, c));
99 g2d.setStroke(PROGRESS_STROKE);
100 g2d.draw(new Rectangle2D.Double(x + margin, y + height - margin/2 - 2, (width - 2*margin), margin/2));
101 g2d.fill(new Rectangle2D.Double(x + margin + 2, y + height - margin/2, (width - 2*margin - 4)*progress, margin/2 - 3));
104 g2d.setComposite(oc);
109 public String toString() {
110 return super.toString() + "[bounds=" + bounds + ", font=" + font + ", margin=" + margin + ", progress="
111 + progress + ", message=" + message + "]";
115 public Rectangle2D getBoundsInLocal() {