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