JXGlassBox.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:7k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: JXGlassBox.java,v 1.4 2005/10/10 18:02:02 rbair Exp $
  3.  *
  4.  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.swingx;
  22. import java.applet.Applet;
  23. import java.awt.Color;
  24. import java.awt.Component;
  25. import java.awt.Container;
  26. import java.awt.Dimension;
  27. import java.awt.Graphics;
  28. import java.awt.Point;
  29. import java.awt.Rectangle;
  30. import java.awt.Window;
  31. import java.awt.event.ActionEvent;
  32. import java.awt.event.ActionListener;
  33. import java.awt.event.MouseAdapter;
  34. import java.awt.event.MouseEvent;
  35. import javax.swing.JComponent;
  36. import javax.swing.SwingConstants;
  37. import javax.swing.SwingUtilities;
  38. import javax.swing.Timer;
  39. /**
  40.  * Component used to display transluscent user-interface content.
  41.  * This component and all of its content will be displayed with the specified
  42.  * "alpha" transluscency property value.  When this component is made visible,
  43.  * it's content will fade in until the alpha transluscency level is reached.
  44.  * <p>
  45.  * If the glassbox's &quot;dismissOnClick&quot; property is <code>true</code>
  46.  * (the default) then the glassbox will be made invisible when the user
  47.  * clicks on it.</p>
  48.  * <p>
  49.  * This component is particularly useful for displaying transient messages
  50.  * on the glasspane.</p>
  51.  *
  52.  * @author Amy Fowler
  53.  * @version 1.0
  54.  */
  55. public class JXGlassBox extends JXPanel {
  56.     private static final int SHOW_DELAY = 30; // ms
  57.     private static final int TIMER_INCREMENT = 10; // ms
  58.     private float alphaStart = 0.01f;
  59.     private float alphaEnd = 0.8f;
  60.     private Timer animateTimer;
  61.     private float alphaIncrement = 0.02f;
  62.     private boolean dismissOnClick = false;
  63.     private MouseAdapter dismissListener = null;
  64.     public JXGlassBox() {
  65.         setOpaque(false);
  66.         setAlpha(alphaStart);
  67.         setBackground(Color.white);
  68.         setDismissOnClick(true);
  69.         animateTimer = new Timer(TIMER_INCREMENT, new ActionListener() {
  70.             public void actionPerformed(ActionEvent e) {
  71.                 setAlpha(getAlpha() + alphaIncrement);
  72.             }
  73.         });
  74.     }
  75.     public JXGlassBox(float alpha) {
  76.         this();
  77.         setAlpha(alpha);
  78.     }
  79.     public void setAlpha(float alpha) {
  80.         super.setAlpha(alpha);
  81.         this.alphaIncrement = (alphaEnd - alphaStart)/(SHOW_DELAY/TIMER_INCREMENT);
  82.     }
  83.     public void setDismissOnClick(boolean dismissOnClick) {
  84.         boolean oldDismissOnClick = this.dismissOnClick;
  85.         this.dismissOnClick = dismissOnClick;
  86.         if (dismissOnClick && !oldDismissOnClick) {
  87.             if (dismissListener == null) {
  88.                 dismissListener = new MouseAdapter() {
  89.                     public void mouseClicked(MouseEvent e) {
  90.                         JComponent glassBox = JXGlassBox.this;
  91.                         JComponent parent = (JComponent) glassBox.getParent();
  92.                         Container toplevel = parent.getTopLevelAncestor();
  93.                         parent.remove(glassBox);
  94.                         toplevel.validate();
  95.                         toplevel.repaint();
  96.                     }
  97.                 };
  98.             }
  99.             addMouseListener(dismissListener);
  100.         }
  101.         else if (!dismissOnClick && oldDismissOnClick) {
  102.             removeMouseListener(dismissListener);
  103.         }
  104.     }
  105.     public void paint(Graphics g) {
  106.         super.paint(g);
  107.         if (!animateTimer.isRunning() && getAlpha() < alphaEnd ) {
  108.             animateTimer.start();
  109.         }
  110.         if (animateTimer.isRunning() && getAlpha() >= alphaEnd) {
  111.             animateTimer.stop();
  112.         }
  113.     }
  114.     public void setVisible(boolean visible) {
  115.         setAlpha(alphaStart);
  116.         super.setVisible(visible);
  117.     }
  118.     private Container getTopLevel() {
  119.         Container p = getParent();
  120.         while (p != null && !(p instanceof Window || p instanceof Applet)) {
  121.             p = p.getParent();
  122.         }
  123.         return p;
  124.     }
  125.     public void showOnGlassPane(Container glassPane, Component component,
  126.                                 int componentX, int componentY, int positionHint) {
  127.         Dimension boxPrefSize = getPreferredSize();
  128.         Dimension glassSize = glassPane.getSize();
  129.         Rectangle compRect = component.getBounds();
  130.         int boxX = 0;
  131.         int boxY = 0;
  132.         int boxWidth = Math.min(boxPrefSize.width, glassSize.width);
  133.         int boxHeight = Math.min(boxPrefSize.height, glassSize.height);
  134.         Point compLocation = SwingUtilities.convertPoint(component.getParent(),
  135.                                                 compRect.x, compRect.y,
  136.                                                 glassPane);
  137.         if (positionHint == SwingConstants.TOP) {
  138.             if (compLocation.x + componentX + boxWidth <= glassSize.width) {
  139.                 boxX = compLocation.x + componentX;
  140.             } else {
  141.                 boxX = glassSize.width - boxWidth;
  142.             }
  143.             boxY = compLocation.y - boxHeight;
  144.             if (boxY < 0) {
  145.                 if (compLocation.y + compRect.height <= glassSize.height) {
  146.                     boxY = compLocation.y + compRect.height;
  147.                 }
  148.                 else {
  149.                     boxY = 0;
  150.                 }
  151.             }
  152.         }
  153.         glassPane.setLayout(null);
  154.         setBounds(boxX, boxY, boxWidth, boxHeight);
  155.         glassPane.add(this);
  156.         glassPane.setVisible(true);
  157.         Container topLevel = getTopLevel();
  158.         topLevel.validate();
  159.         topLevel.repaint();
  160.     }
  161.     public void showOnGlassPane(Container glassPane, int originX, int originY) {
  162.         Dimension boxPrefSize = getPreferredSize();
  163.         Dimension glassSize = glassPane.getSize();
  164.         int boxX = 0;
  165.         int boxY = 0;
  166.         int boxWidth = 0;
  167.         int boxHeight = 0;
  168.         boxWidth = Math.min(boxPrefSize.width, glassSize.width);
  169.         boxHeight = Math.min(boxPrefSize.height, glassSize.height);
  170.         if (originY - boxHeight >= 0) {
  171.             boxY = originY - boxHeight;
  172.         } else if (originY + boxHeight <= glassSize.height) {
  173.             boxY = originY;
  174.         } else {
  175.             boxY = glassSize.height - boxHeight;
  176.         }
  177.         if (originX + boxWidth <= glassSize.width) {
  178.             boxX = originX;
  179.         } else if (originX >= boxWidth) {
  180.             boxX = originX - boxWidth;
  181.         } else {
  182.             boxX = glassSize.width - boxWidth;
  183.         }
  184.         glassPane.setLayout(null);
  185.         setBounds(boxX, boxY, boxWidth, boxHeight);
  186.         glassPane.add(this);
  187.         glassPane.setVisible(true);
  188.         Container topLevel = getTopLevel();
  189.         topLevel.validate();
  190.         topLevel.repaint();
  191.     }
  192. }