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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: LoginDialog.java,v 1.2 2005/06/06 14:25:36 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. package org.jdesktop.demo.login.romain;
  8. import java.awt.BorderLayout;
  9. import java.awt.Dimension;
  10. import java.awt.GridBagConstraints;
  11. import java.awt.GridBagLayout;
  12. import java.awt.Insets;
  13. import java.awt.event.ActionEvent;
  14. import java.awt.event.ActionListener;
  15. import javax.swing.JComponent;
  16. import javax.swing.JDialog;
  17. import javax.swing.JFrame;
  18. import javax.swing.JLabel;
  19. import javax.swing.JPanel;
  20. import javax.swing.SwingUtilities;
  21. import javax.swing.Timer;
  22. import org.jdesktop.swingx.auth.LoginEvent;
  23. public class LoginDialog extends JDialog implements FadeListener {
  24.     private JComponent contentPane;
  25.     private LoginTextField loginField;
  26.     private PasswordTextField passwordField;
  27.     private Timer animation;
  28.     private FadingPanel glassPane;
  29.     private org.jdesktop.swingx.auth.LoginService loginService;
  30.     
  31.     public LoginDialog(org.jdesktop.swingx.auth.LoginService service) {
  32.         super((JFrame)null, true);
  33.         this.loginService = service;
  34.         glassPane = new FadingPanel(this);
  35.         setGlassPane(glassPane);
  36.         
  37.         buildContentPane();
  38.         buildLoginForm();
  39.         startAnimation();
  40.         
  41.         setSize(new Dimension(400, 300));
  42.         setResizable(false);
  43.         setLocationRelativeTo(null);
  44.     }
  45.     
  46.     private void buildContentPane() {
  47.         contentPane = new CurvesPanel();
  48.         contentPane.setLayout(new BorderLayout());
  49.         setContentPane(contentPane);
  50.     }
  51.     
  52.     private void startAnimation() {
  53.         animation = new Timer(50, new ActionListener() {
  54.             public void actionPerformed(ActionEvent e) {
  55.                 contentPane.repaint();
  56.             }
  57.         });
  58.         animation.start();
  59.     }
  60.     
  61.     private void buildLoginForm() {
  62.         JPanel form = new JPanel(new GridBagLayout());
  63.         form.add(new JLabel(UIHelper.readImageIcon("title.png")), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.3, GridBagConstraints.SOUTH, GridBagConstraints.NONE, new Insets(12, 36, 11, 7), 0, 0));
  64.         
  65.         loginField = new LoginTextField();
  66.         form.add(loginField, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(0, 36, 5, 11), 0, 0));
  67.         
  68.         passwordField = new PasswordTextField();
  69.         form.add(passwordField, new GridBagConstraints(0, 2, 1, 1, 0.0, 0.7, GridBagConstraints.NORTHEAST, GridBagConstraints.NONE, new Insets(0, 36, 11, 11), 0, 0));
  70.         
  71.         passwordField.addActionListner(new ActionListener() {
  72.             public void actionPerformed(ActionEvent e) {
  73.                 glassPane.setVisible(true);
  74.                 new Thread() {
  75.                     public void run() {
  76.                         
  77.                         //wait a few seconds before accepting...
  78. //                        try {
  79. //                            Thread.sleep(5000);
  80. //                        } catch (Exception e) {
  81. //                            //probably a wake up exception
  82. //                        }
  83.                         //perform the login procedures
  84.                         loginService.addLoginListener(new org.jdesktop.swingx.auth.LoginListener() {
  85.                             /**
  86.                              *  Called by the <strong>JXLoginPanel</strong> in the event of a login failure
  87.                              *
  88.                              * @param source panel that fired the event
  89.                              */
  90.                             public void loginFailed(LoginEvent source) {
  91. //                                glassPane.setVisible(false);
  92. //                                setVisible(false);
  93.                             }
  94.                             /**
  95.                              *  Called by the <strong>JXLoginPanel</strong> when the Authentication
  96.                              *  operation is started.
  97.                              * @param source panel that fired the event
  98.                              */
  99.                             public void loginStarted(LoginEvent source) {
  100.                                 
  101.                             }
  102.                             /**
  103.                              *  Called by the <strong>JXLoginPanel</strong> in the event of a login
  104.                              *  cancellation by the user.
  105.                              *
  106.                              * @param source panel that fired the event
  107.                              */
  108.                             public void loginCanceled(LoginEvent source) {
  109. //                                glassPane.setVisible(false);
  110. //                                setVisible(false);
  111.                             }
  112.                             /**
  113.                              *  Called by the <strong>JXLoginPanel</strong> in the event of a
  114.                              *  successful login.
  115.                              *
  116.                              * @param source panel that fired the event
  117.                              */
  118.                             public void loginSucceeded(LoginEvent source) {
  119.                                 glassPane.setVisible(false);
  120.                                 setVisible(false);
  121.                             }
  122.                         });
  123.                         try {
  124.                             loginService.startAuthentication(loginField.getText(), passwordField.getText().toCharArray(), "java.net");
  125.                         } catch (Exception e) {
  126.                             e.printStackTrace();
  127.                         }
  128.                     }
  129.                 }.start();
  130.             }
  131.         });
  132.         
  133.         form.setOpaque(false);
  134.         contentPane.add(form, BorderLayout.CENTER);
  135.     }
  136.     
  137.     public void fadeInFinished() {
  138.         glassPane.setVisible(false);
  139.     }
  140.     
  141.     public void fadeOutFinished() {
  142.         SwingUtilities.invokeLater(new Runnable() {
  143.             public void run() {
  144.                 contentPane = new CirclesPanel();
  145.                 contentPane.setLayout(new BorderLayout());
  146.                 WaitAnimation waitAnimation = new WaitAnimation();
  147.                 contentPane.add(waitAnimation, BorderLayout.CENTER);
  148.                 setContentPane(contentPane);
  149.                 validate();
  150.                 glassPane.switchDirection();
  151.             }
  152.         });
  153.     }
  154.     
  155.     public String getTitle() {
  156.         return "Login...";
  157.     }
  158.     
  159.     public static void main(String[] args) {
  160.         LoginDialog frame = new LoginDialog(null);
  161.         frame.setVisible(true);
  162.     }
  163. }