]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/MessageNotificationNode.java
Sync git svn branch with SVN repository r33269.
[simantics/platform.git] / bundles / org.simantics.scenegraph / src / org / simantics / scenegraph / g2d / nodes / MessageNotificationNode.java
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.scenegraph.g2d.nodes;\r
13 \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
25 \r
26 import org.simantics.scenegraph.g2d.G2DNode;\r
27 import org.simantics.scenegraph.utils.TextUtil;\r
28 \r
29 /**\r
30  * @author Tuukka Lehtonen\r
31  */\r
32 public class MessageNotificationNode extends G2DNode {\r
33 \r
34     private static final long   serialVersionUID = -7518450802171689793L;\r
35 \r
36     private static final Stroke PROGRESS_STROKE  = new BasicStroke(1f);\r
37 \r
38     private Rectangle2D       bounds;\r
39     private Font              font;\r
40     private double            margin;\r
41     private double            progress;\r
42     private String            message;\r
43 \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
47         this.font = font;\r
48         this.margin = margin;\r
49         this.progress = progress;\r
50         this.message = message;\r
51     }\r
52 \r
53     @Override\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
59         double rx = 20;\r
60         double ry = 20;\r
61 \r
62         RoundRectangle2D rr = new RoundRectangle2D.Double(x, y, width, height, rx, ry);\r
63 \r
64         Composite oc = g2d.getComposite();\r
65 \r
66         // Base\r
67         {\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
71             g2d.fill(rr);\r
72         }\r
73 \r
74         // Message\r
75         {\r
76             g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);\r
77             g2d.setComposite(AlphaComposite.Src);\r
78             g2d.setFont(font);\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
87                     break;\r
88                 g2d.drawString(lines[i], tx, ty);\r
89                 ty += fh;\r
90             }\r
91         }\r
92 \r
93         // Progress\r
94         {\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
102         }\r
103 \r
104         g2d.setComposite(oc);\r
105 \r
106     }\r
107 \r
108     @Override\r
109     public String toString() {\r
110         return super.toString() + "[bounds=" + bounds + ", font=" + font + ", margin=" + margin + ", progress="\r
111         + progress + ", message=" + message + "]";\r
112     }\r
113 \r
114     @Override\r
115     public Rectangle2D getBoundsInLocal() {\r
116         return bounds;\r
117     }\r
118 \r
119 }\r