KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > JXGlassBox


1 /*
2  * $Id: JXGlassBox.java,v 1.1 2004/07/28 21:21:11 aim Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing;
9
10 import java.applet.Applet JavaDoc;
11 import java.awt.AlphaComposite JavaDoc;
12 import java.awt.BorderLayout JavaDoc;
13 import java.awt.Color JavaDoc;
14 import java.awt.Component JavaDoc;
15 import java.awt.Container JavaDoc;
16 import java.awt.Composite JavaDoc;
17 import java.awt.Dimension JavaDoc;
18 import java.awt.Graphics JavaDoc;
19 import java.awt.Graphics2D JavaDoc;
20 import java.awt.Insets JavaDoc;
21 import java.awt.Point JavaDoc;
22 import java.awt.Rectangle JavaDoc;
23 import java.awt.Window JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.awt.event.ActionListener JavaDoc;
26 import java.awt.event.MouseAdapter JavaDoc;
27 import java.awt.event.MouseEvent JavaDoc;
28
29 import javax.swing.JComponent JavaDoc;
30 import javax.swing.JFrame JavaDoc;
31 import javax.swing.JLabel JavaDoc;
32 import javax.swing.JPanel JavaDoc;
33 import javax.swing.JTextArea JavaDoc;
34 import javax.swing.JTextPane JavaDoc;
35 import javax.swing.SwingConstants JavaDoc;
36 import javax.swing.SwingUtilities JavaDoc;
37 import javax.swing.Timer JavaDoc;
38 import javax.swing.border.CompoundBorder JavaDoc;
39 import javax.swing.border.LineBorder JavaDoc;
40
41
42 /**
43  * Component used to display transluscent user-interface content.
44  * This component and all of its content will be displayed with the specified
45  * "alpha" transluscency property value. When this component is made visible,
46  * it's content will fade in until the alpha transluscency level is reached.
47  * <p>
48  * If the glassbox's &quot;dismissOnClick&quot; property is <code>true</code>
49  * (the default) then the glassbox will be made invisible when the user
50  * clicks on it.</p>
51  * <p>
52  * This component is particularly useful for displaying transient messages
53  * on the glasspane.</p>
54  *
55  * @author Amy Fowler
56  * @version 1.0
57  */

58
59 public class JXGlassBox extends JPanel JavaDoc {
60     private static final int SHOW_DELAY = 30; // ms
61
private static final int TIMER_INCREMENT = 5; // ms
62

63     private float alphaStart = 0.01f;
64     private float alphaEnd = 0.8f;
65
66     private Timer JavaDoc animateTimer;
67     private float animateAlpha = alphaStart;
68     private float alphaIncrement = 0.02f;
69
70     private boolean dismissOnClick = false;
71     private MouseAdapter JavaDoc dismissListener = null;
72
73     private transient Insets JavaDoc insets = new Insets JavaDoc(0,0,0,0); //scatch
74

75     public JXGlassBox() {
76         setOpaque(false);
77         setBackground(Color.white);
78         setDismissOnClick(true);
79
80         animateTimer = new Timer JavaDoc(TIMER_INCREMENT, new ActionListener JavaDoc() {
81             public void actionPerformed(ActionEvent JavaDoc e) {
82                 animateAlpha += alphaIncrement;
83                 paintImmediately(0, 0, getWidth(), getHeight());
84             }
85         });
86     }
87
88     public JXGlassBox(float alpha) {
89         this();
90         setAlpha(alpha);
91     }
92
93     public void setAlpha(float alpha) {
94         this.alphaEnd = alpha;
95         this.alphaIncrement = (alphaEnd - alphaStart)/(SHOW_DELAY/TIMER_INCREMENT);
96     }
97
98     public float getAlpha() {
99         return alphaEnd;
100     }
101
102     public void setDismissOnClick(boolean dismissOnClick) {
103         boolean oldDismissOnClick = this.dismissOnClick;
104         this.dismissOnClick = dismissOnClick;
105         if (dismissOnClick && !oldDismissOnClick) {
106             if (dismissListener == null) {
107                 dismissListener = new MouseAdapter JavaDoc() {
108                     public void mouseClicked(MouseEvent JavaDoc e) {
109                         JComponent JavaDoc glassBox = JXGlassBox.this;
110                         JComponent JavaDoc parent = (JComponent JavaDoc) glassBox.getParent();
111                         Container JavaDoc toplevel = parent.getTopLevelAncestor();
112                         parent.remove(glassBox);
113                         toplevel.validate();
114                         toplevel.repaint();
115                     }
116                 };
117             }
118             addMouseListener(dismissListener);
119         }
120         else if (!dismissOnClick && oldDismissOnClick) {
121             removeMouseListener(dismissListener);
122         }
123     }
124
125     public void paint(Graphics JavaDoc g) {
126         getInsets(insets);
127         Graphics2D JavaDoc g2d = (Graphics2D JavaDoc)g;
128         Composite JavaDoc oldComp = g2d.getComposite();
129         Composite JavaDoc alphaComp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
130             Math.min(alphaEnd, animateAlpha));
131         g2d.setComposite(alphaComp);
132         g2d.setColor(getBackground());
133         g2d.fillRect(insets.left, insets.top,
134                      getWidth() - insets.left - insets.right,
135                      getHeight() - insets.top - insets.bottom);
136         super.paint(g2d);
137         g2d.setComposite(oldComp);
138
139         if (!animateTimer.isRunning() && animateAlpha < alphaEnd ) {
140             animateTimer.start();
141         }
142         if (animateTimer.isRunning() && animateAlpha >= alphaEnd) {
143             animateTimer.stop();
144         }
145     }
146
147     public void setVisible(boolean visible) {
148         animateAlpha = alphaStart;
149         super.setVisible(visible);
150     }
151
152     private Container JavaDoc getTopLevel() {
153         Container JavaDoc p = getParent();
154         while (p != null && !(p instanceof Window JavaDoc || p instanceof Applet JavaDoc)) {
155             p = p.getParent();
156         }
157         return p;
158     }
159
160     public void showOnGlassPane(Container JavaDoc glassPane, Component JavaDoc component,
161                                 int componentX, int componentY, int positionHint) {
162         Dimension JavaDoc boxPrefSize = getPreferredSize();
163         Dimension JavaDoc glassSize = glassPane.getSize();
164         Rectangle JavaDoc compRect = component.getBounds();
165         int boxX = 0;
166         int boxY = 0;
167         int boxWidth = Math.min(boxPrefSize.width, glassSize.width);
168         int boxHeight = Math.min(boxPrefSize.height, glassSize.height);
169
170         Point JavaDoc compLocation = SwingUtilities.convertPoint(component.getParent(),
171                                                 compRect.x, compRect.y,
172                                                 glassPane);
173
174         if (positionHint == SwingConstants.TOP) {
175             if (compLocation.x + componentX + boxWidth <= glassSize.width) {
176                 boxX = compLocation.x + componentX;
177             } else {
178                 boxX = glassSize.width - boxWidth;
179             }
180             boxY = compLocation.y - boxHeight;
181             if (boxY < 0) {
182                 if (compLocation.y + compRect.height <= glassSize.height) {
183                     boxY = compLocation.y + compRect.height;
184                 }
185                 else {
186                     boxY = 0;
187                 }
188             }
189         }
190
191         glassPane.setLayout(null);
192         setBounds(boxX, boxY, boxWidth, boxHeight);
193         glassPane.add(this);
194         glassPane.setVisible(true);
195
196         Container JavaDoc topLevel = getTopLevel();
197         topLevel.validate();
198         topLevel.repaint();
199
200     }
201
202     public void showOnGlassPane(Container JavaDoc glassPane, int originX, int originY) {
203         Dimension JavaDoc boxPrefSize = getPreferredSize();
204         Dimension JavaDoc glassSize = glassPane.getSize();
205         int boxX = 0;
206         int boxY = 0;
207         int boxWidth = 0;
208         int boxHeight = 0;
209
210         boxWidth = Math.min(boxPrefSize.width, glassSize.width);
211         boxHeight = Math.min(boxPrefSize.height, glassSize.height);
212
213         if (originY - boxHeight >= 0) {
214             boxY = originY - boxHeight;
215         } else if (originY + boxHeight <= glassSize.height) {
216             boxY = originY;
217         } else {
218             boxY = glassSize.height - boxHeight;
219         }
220
221         if (originX + boxWidth <= glassSize.width) {
222             boxX = originX;
223         } else if (originX >= boxWidth) {
224             boxX = originX - boxWidth;
225         } else {
226             boxX = glassSize.width - boxWidth;
227         }
228
229         glassPane.setLayout(null);
230         setBounds(boxX, boxY, boxWidth, boxHeight);
231         glassPane.add(this);
232         glassPane.setVisible(true);
233
234         Container JavaDoc topLevel = getTopLevel();
235         topLevel.validate();
236         topLevel.repaint();
237     }
238
239 }
Popular Tags