]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/MessageNotificationNode.java
G2DParentNode handles "undefined" child bounds separately
[simantics/platform.git] / bundles / org.simantics.scenegraph / src / org / simantics / scenegraph / g2d / nodes / MessageNotificationNode.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.AlphaComposite;
15 import java.awt.BasicStroke;
16 import java.awt.Color;
17 import java.awt.Composite;
18 import java.awt.Font;
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;
25
26 import org.simantics.scenegraph.g2d.G2DNode;
27 import org.simantics.scenegraph.utils.TextUtil;
28
29 /**
30  * @author Tuukka Lehtonen
31  */
32 public class MessageNotificationNode extends G2DNode {
33
34     private static final long   serialVersionUID = -7518450802171689793L;
35
36     private static final Stroke PROGRESS_STROKE  = new BasicStroke(1f);
37
38     private Rectangle2D       bounds;
39     private Font              font;
40     private double            margin;
41     private double            progress;
42     private String            message;
43
44     @SyncField({"bounds", "font", "margin", "progress", "message"})
45     public void init(Rectangle2D bounds, Font font, double margin, double progress, String message) {
46         this.bounds = bounds;
47         this.font = font;
48         this.margin = margin;
49         this.progress = progress;
50         this.message = message;
51     }
52
53     @Override
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();
59         double rx = 20;
60         double ry = 20;
61
62         RoundRectangle2D rr = new RoundRectangle2D.Double(x, y, width, height, rx, ry);
63
64         Composite oc = g2d.getComposite();
65
66         // Base
67         {
68             g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
69             g2d.setComposite(AlphaComposite.SrcOver.derive(0.5f));
70             g2d.setPaint(Color.BLACK);
71             g2d.fill(rr);
72         }
73
74         // Message
75         {
76             g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
77             g2d.setComposite(AlphaComposite.Src);
78             g2d.setFont(font);
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())
87                     break;
88                 g2d.drawString(lines[i], tx, ty);
89                 ty += fh;
90             }
91         }
92
93         // Progress
94         {
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));
102         }
103
104         g2d.setComposite(oc);
105
106     }
107
108     @Override
109     public String toString() {
110         return super.toString() + "[bounds=" + bounds + ", font=" + font + ", margin=" + margin + ", progress="
111         + progress + ", message=" + message + "]";
112     }
113
114     @Override
115     public Rectangle2D getBoundsInLocal() {
116         return bounds;
117     }
118
119 }