View Javadoc

1   package org.telscenter.pas.common.ui;
2   
3   import java.awt.AlphaComposite;
4   import java.awt.Color;
5   import java.awt.Component;
6   import java.awt.Composite;
7   import java.awt.Dimension;
8   import java.awt.Font;
9   import java.awt.FontMetrics;
10  import java.awt.GradientPaint;
11  import java.awt.Graphics;
12  import java.awt.Graphics2D;
13  import java.awt.Point;
14  import java.awt.RenderingHints;
15  import java.awt.Shape;
16  import java.awt.event.ActionEvent;
17  import java.awt.event.ActionListener;
18  import java.awt.event.ComponentEvent;
19  import java.awt.event.ComponentListener;
20  import java.awt.event.MouseEvent;
21  import java.awt.event.MouseListener;
22  import java.awt.event.MouseMotionListener;
23  import java.awt.geom.Area;
24  import java.awt.geom.GeneralPath;
25  import java.awt.geom.Rectangle2D;
26  import java.awt.geom.RoundRectangle2D;
27  import java.awt.image.BufferedImage;
28  import java.awt.image.ConvolveOp;
29  import java.awt.image.Kernel;
30  
31  import javax.swing.JFrame;
32  import javax.swing.JPanel;
33  import javax.swing.Timer;
34  
35  import org.telscenter.pas.ui.frames.PasFrame;
36  
37  
38  /***
39   * A glasspane that can be used to notify the user (for a specified time)
40   * what the x and y coordinates of their JFrame (window) are after they
41   * have moved it.
42   *
43   * <p/>
44   * Copyright (C) 2005 by Jon Lipsky
45   * <p/>
46   * Licensed under the Apache License, Version 2.0 (the "License");
47   * you may not use this file except in compliance with the License. Y
48   * ou may obtain a copy of the License at
49   * <p/>
50   * http://www.apache.org/licenses/LICENSE-2.0
51   * <p/>
52   * Unless required by applicable law or agreed to in writing, software d
53   * istributed under the License is distributed on an "AS IS" BASIS,
54   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
55   * See the License for the specific language governing permissions and
56   * limitations under the License.
57   */
58  public class WelcomeGlassPane extends JPanel implements ComponentListener, MouseMotionListener, MouseListener, ActionListener
59  {
60  	public static float[] BLUR = {0.10f, 0.10f, 0.10f, 0.10f, 0.30f, 0.10f, 0.10f, 0.10f, 0.10f};
61  	public static ConvolveOp blurOp = new ConvolveOp(new Kernel(3, 3, BLUR));
62  
63  	// ------------------------------------------------------------------------------------------------------------------
64  	//  Fields
65  	// ------------------------------------------------------------------------------------------------------------------
66  
67  	private JFrame frame;
68  	private boolean installed = false;
69  	private Component previousGlassPane;
70  	private Timer timer;
71  	private int delay = 3500;
72  	private Point lastLocation;
73  	private String text;
74  
75  	// ------------------------------------------------------------------------------------------------------------------
76  	//  Constructors and Getter/Setters
77  	// ------------------------------------------------------------------------------------------------------------------
78  
79  	public WelcomeGlassPane(JFrame aFrame)
80  	{
81  		frame = aFrame;
82  		frame.addComponentListener(this);
83  		frame.addMouseMotionListener(this);
84  		frame.addMouseListener(this);
85  		setOpaque(false);
86  		//text = ((WiseFrame)frame).getWelcomeMessage();
87  	}
88  
89  	public int getDelay()
90  	{
91  		return delay;
92  	}
93  
94  	public void setDelay(int aDelay)
95  	{
96  		delay = aDelay;
97  	}
98  
99  	// ------------------------------------------------------------------------------------------------------------------
100 	//  Implementation of the methods from ComponentListener
101 	// ------------------------------------------------------------------------------------------------------------------
102 
103 	public void componentHidden(ComponentEvent e)
104 	{
105 		// Do nothing
106 	}
107 
108 	public void componentMoved(ComponentEvent e)
109 	{
110 //		lastLocation = frame.getLocation();
111 //		repaint();
112 //
113 //		if (!installed)
114 //		{
115 //			previousGlassPane = frame.getGlassPane();
116 //			frame.setGlassPane(this);
117 //			setVisible(true);
118 //			installed = true;
119 //		}
120 //
121 //		if (timer == null)
122 //		{
123 //			timer = new Timer(delay, this);
124 //		}
125 //		else
126 //		{
127 //			timer.stop();
128 //			timer.setDelay(delay);
129 //		}
130 //
131 //		timer.start();
132 	}
133 
134 	public void componentResized(ComponentEvent e)
135 	{
136 		// Do nothing
137 	}
138 
139 	public void componentShown(ComponentEvent e)
140 	{
141 		text = ((PasFrame)frame).getWelcomeMessageForUsers();
142 		repaint();
143 
144 		if (!installed)
145 		{
146 			previousGlassPane = frame.getGlassPane();
147 			frame.setGlassPane(this);
148 			setVisible(true);
149 			installed = true;
150 		}
151 
152 //		if (timer == null)
153 //		{
154 //			timer = new Timer(delay, this);
155 //		}
156 //		else
157 //		{
158 //			timer.stop();
159 //			timer.setDelay(delay);
160 //		}
161 //
162 //		timer.start();
163 	}
164 
165 	// ------------------------------------------------------------------------------------------------------------------
166 	//  Implementation of the methods from ComponentListener
167 	// ------------------------------------------------------------------------------------------------------------------
168 
169 	public void actionPerformed(ActionEvent e)
170 	{
171 		timer.stop();
172 
173 		installed = false;
174 		setVisible(false);
175 		//frame.setGlassPane(previousGlassPane);
176 		previousGlassPane = null;
177 	}
178 
179 	// ------------------------------------------------------------------------------------------------------------------
180 	//  Override methods of JPanel
181 	// ------------------------------------------------------------------------------------------------------------------
182 
183 	protected void paintComponent(Graphics g)
184 	{
185 		super.paintComponent(g);
186 
187 		Graphics2D g2d = (Graphics2D)g;
188 		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
189 
190 		Font font = g.getFont();
191 
192 		Dimension size = getSize();
193 		int h = size.height;
194 		int w = size.width;
195 		int arc = 0;
196 
197 		// Figure out what size font to use, and what size arc to use
198 		if (size.width > 300)
199 		{
200 			font = font.deriveFont(Font.PLAIN,48);
201 			arc = 20;
202 		}
203 		else if (size.width > 150)
204 		{
205 			font = font.deriveFont(Font.PLAIN,24);
206 			arc = 10;
207 		}
208 		else
209 		{
210 			font = font.deriveFont(Font.PLAIN,12);
211 			arc = 3;
212 		}
213 
214 
215 		//String text = lastLocation.x +","+lastLocation.y;
216 		//String text = "Welcome Tony and BOB";
217 		
218 		g.setFont(font);
219 		FontMetrics metrics = g.getFontMetrics();
220 		Rectangle2D stringBounds = metrics.getStringBounds(text,g);
221 
222 		// Figure out how big we want our rounded rectangle to be
223 		int preferredWidth = (int)stringBounds.getWidth()+metrics.getHeight();
224 		int preferredHeight = (int)stringBounds.getHeight()+metrics.getHeight();
225 
226 		w = Math.min(preferredWidth,w);
227 		h = Math.min(preferredHeight,h);
228 
229 		int x = (size.width - w) / 2;
230 		int y = (size.height - h) / 2;
231 
232 		// Create the path that runs through the rounded rectangle
233 		float h2 = h/2;
234 		float h4 = h/4;
235 
236 		GeneralPath path = new GeneralPath();
237 		path.moveTo(0,h);
238 		path.curveTo(0,h-h4,h4,h2,h2,h2);
239 		path.lineTo(w-h2,h2);
240 		path.curveTo(w-h4,h2,w,h4,w,0);
241 		path.lineTo(w,h);
242 
243 		// Create a buffered image to paint the rounded rectangle
244 		BufferedImage vBuffer = new BufferedImage(preferredWidth, preferredHeight, BufferedImage.TYPE_INT_RGB);
245 		Graphics2D bg2d = vBuffer.createGraphics();
246 		bg2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
247 
248 		// Paint the background
249 		RoundRectangle2D fillArea = new RoundRectangle2D.Double(0.0D, 0.0D, w, h,arc,arc);
250 		bg2d.setClip(fillArea);
251 
252 		Area area = new Area(fillArea);
253 		area.subtract(new Area(path));
254 
255 		Color vStartColor = new Color(10,0,40);
256 		Color vEndColor = new Color(175,165,225);
257 
258 		java.awt.Paint p = new GradientPaint(0.0F, 0.0F, vStartColor, 0.0F, h, vEndColor);
259 		bg2d.setPaint(p);
260 
261 		bg2d.fill(area);
262 
263 		vStartColor = new Color(5,0,50);
264 		vEndColor = new Color(105,100,155);
265 
266 		p = new GradientPaint(0.0F, 0.0F, vStartColor, 0.0F, h, vEndColor);
267 		bg2d.setPaint(p);
268 
269 		bg2d.fill(path);
270 
271 		// Blur the background
272 		vBuffer = blurOp.filter(vBuffer, null);
273 		bg2d = vBuffer.createGraphics();
274 
275 		// Figure out where to place the background and the text
276 		int insetX = (size.width - w) / 2;
277 		int insetY = (size.height - h) / 2;
278 
279 		g2d.translate(insetX,insetY);
280 
281 		Composite composite = g2d.getComposite();
282 		g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .9f));
283 		Shape clip = g2d.getClip();
284 		g2d.setClip(fillArea);
285 		g2d.drawImage(vBuffer,0,0,null);
286 		g2d.setClip(clip);
287 		g2d.setComposite(composite);
288 
289 		// Paint a border around the background since it was clipped and the edges
290 		// weren't anti-aliased
291 		Color vWrapColor = new Color(175,165,225);
292 		g2d.setColor( vWrapColor );
293 		g2d.drawRoundRect(0, 0, w, h,arc,arc);
294 
295 		// Figure out where to draw the text
296 		x = (w - (int)stringBounds.getWidth()) / 2;
297 		y = (h / 2) + ((metrics.getAscent()- metrics.getDescent()) / 2);
298 
299 		// Draw a shadwo
300 		g2d.setColor(new Color(0,0,0,70));
301 		g2d.drawString(text,x+2,y+2);
302 
303 		// Draw the text
304 		g2d.setColor(Color.WHITE);
305 		g2d.drawString(text,x,y);
306 
307 		g2d.translate(-insetX,-insetY);
308 	}
309 
310 	// ------------------------------------------------------------------------------------------------------------------
311 	//  Utility Methods
312 	// ------------------------------------------------------------------------------------------------------------------
313 
314 	public static void registerFrame(JFrame aFrame)
315 	{
316 		new WelcomeGlassPane(aFrame);
317 	}
318 
319 	public void mouseDragged(MouseEvent e) {
320 		// TODO Auto-generated method stub
321 		
322 	}
323 
324 	public void mouseMoved(MouseEvent e) {
325 		setVisible(false);
326 	}
327 
328 	public void mouseClicked(MouseEvent e) {
329 		setVisible(false);
330 	}
331 
332 	public void mousePressed(MouseEvent e) {
333 		// TODO Auto-generated method stub
334 		
335 	}
336 
337 	public void mouseReleased(MouseEvent e) {
338 		// TODO Auto-generated method stub
339 		
340 	}
341 
342 	public void mouseEntered(MouseEvent e) {
343 		// TODO Auto-generated method stub
344 		
345 	}
346 
347 	public void mouseExited(MouseEvent e) {
348 		// TODO Auto-generated method stub
349 		
350 	}
351 }