1 7 8 package org.jdesktop.swing; 9 10 import java.applet.Applet ; 11 import java.awt.AlphaComposite ; 12 import java.awt.BorderLayout ; 13 import java.awt.Color ; 14 import java.awt.Component ; 15 import java.awt.Container ; 16 import java.awt.Composite ; 17 import java.awt.Dimension ; 18 import java.awt.Graphics ; 19 import java.awt.Graphics2D ; 20 import java.awt.Insets ; 21 import java.awt.Point ; 22 import java.awt.Rectangle ; 23 import java.awt.Window ; 24 import java.awt.event.ActionEvent ; 25 import java.awt.event.ActionListener ; 26 import java.awt.event.MouseAdapter ; 27 import java.awt.event.MouseEvent ; 28 29 import javax.swing.JComponent ; 30 import javax.swing.JFrame ; 31 import javax.swing.JLabel ; 32 import javax.swing.JPanel ; 33 import javax.swing.JTextArea ; 34 import javax.swing.JTextPane ; 35 import javax.swing.SwingConstants ; 36 import javax.swing.SwingUtilities ; 37 import javax.swing.Timer ; 38 import javax.swing.border.CompoundBorder ; 39 import javax.swing.border.LineBorder ; 40 41 42 58 59 public class JXGlassBox extends JPanel { 60 private static final int SHOW_DELAY = 30; private static final int TIMER_INCREMENT = 5; 63 private float alphaStart = 0.01f; 64 private float alphaEnd = 0.8f; 65 66 private Timer animateTimer; 67 private float animateAlpha = alphaStart; 68 private float alphaIncrement = 0.02f; 69 70 private boolean dismissOnClick = false; 71 private MouseAdapter dismissListener = null; 72 73 private transient Insets insets = new Insets (0,0,0,0); 75 public JXGlassBox() { 76 setOpaque(false); 77 setBackground(Color.white); 78 setDismissOnClick(true); 79 80 animateTimer = new Timer (TIMER_INCREMENT, new ActionListener () { 81 public void actionPerformed(ActionEvent 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 () { 108 public void mouseClicked(MouseEvent e) { 109 JComponent glassBox = JXGlassBox.this; 110 JComponent parent = (JComponent ) glassBox.getParent(); 111 Container 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 g) { 126 getInsets(insets); 127 Graphics2D g2d = (Graphics2D )g; 128 Composite oldComp = g2d.getComposite(); 129 Composite 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 getTopLevel() { 153 Container p = getParent(); 154 while (p != null && !(p instanceof Window || p instanceof Applet )) { 155 p = p.getParent(); 156 } 157 return p; 158 } 159 160 public void showOnGlassPane(Container glassPane, Component component, 161 int componentX, int componentY, int positionHint) { 162 Dimension boxPrefSize = getPreferredSize(); 163 Dimension glassSize = glassPane.getSize(); 164 Rectangle 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 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 topLevel = getTopLevel(); 197 topLevel.validate(); 198 topLevel.repaint(); 199 200 } 201 202 public void showOnGlassPane(Container glassPane, int originX, int originY) { 203 Dimension boxPrefSize = getPreferredSize(); 204 Dimension 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 topLevel = getTopLevel(); 235 topLevel.validate(); 236 topLevel.repaint(); 237 } 238 239 } | Popular Tags |