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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: JXLoginDialog.java,v 1.3 2005/07/02 15:39:59 l2fprod 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.swingx;
  8. import java.awt.BorderLayout;
  9. import java.awt.Dimension;
  10. import java.awt.Font;
  11. import java.awt.GradientPaint;
  12. import java.awt.Graphics2D;
  13. import java.awt.GridBagConstraints;
  14. import java.awt.GridBagLayout;
  15. import java.awt.Insets;
  16. import java.awt.RenderingHints;
  17. import java.awt.event.ActionEvent;
  18. import java.awt.event.ActionListener;
  19. import java.awt.geom.GeneralPath;
  20. import java.awt.image.BufferedImage;
  21. import javax.swing.JButton;
  22. import javax.swing.JDialog;
  23. import javax.swing.JFrame;
  24. import javax.swing.JLabel;
  25. import javax.swing.JProgressBar;
  26. import javax.swing.UIManager;
  27. import org.jdesktop.swingx.JXLoginPanel;
  28. import org.jdesktop.swingx.auth.LoginEvent;
  29. import org.jdesktop.swingx.auth.LoginListener;
  30. import org.jdesktop.swingx.auth.LoginService;
  31. import org.jdesktop.swingx.auth.PasswordStore;
  32. import org.jdesktop.swingx.auth.UserNameStore;
  33. import org.jdesktop.swingx.util.WindowUtils;
  34. /**
  35.  * A standard login dialog that provides a reasonable amount of flexibility
  36.  * while also providing ease of use and a professional look.
  37.  *
  38.  * @author rbair
  39.  */
  40. public class JXLoginDialog extends JDialog {
  41.     /**
  42.      * An optional banner at the top of the dialog
  43.      */
  44.     private JXImagePanel banner;
  45.     /**
  46.      * Custom label allowing the developer to display some message to the user
  47.      */
  48.     private JLabel label;
  49.     /**
  50.      * Shows a message such as "user name or password incorrect" or
  51.      * "could not contact server" or something like that if something
  52.      * goes wrong
  53.      */
  54.     private JLabel messageLabel;
  55.     /**
  56.      * If something goes wrong, this link will be displayed so the user can
  57.      * click on it to be shown the exception, etc
  58.      */
  59.     private JXHyperlink detailsLink;
  60.     /**
  61.      * The login panel containing the username & password fields, and handling
  62.      * the login procedures.
  63.      */
  64.     private JXLoginPanel loginPanel;
  65.     
  66.     private JXPanel contentPanel;
  67.     private JXPanel buttonPanel;
  68.     private JXPanel progressPanel;
  69.     
  70.     /**
  71.      * Only true if the user cancels their login operation. This is reset to false
  72.      * after the login thread is cancelled and the proper message shown
  73.      */
  74.     private boolean cancelled;
  75.     
  76.     /** Creates a new instance of JXLoginDialog */
  77.     public JXLoginDialog() {
  78.         this(null, null, null);
  79.     }
  80.     
  81.     public JXLoginDialog(LoginService service, PasswordStore ps, UserNameStore us) {
  82.         loginPanel = new JXLoginPanel(service, ps, us);
  83.         initComponents();
  84.     }
  85.     
  86.     private void initComponents() {
  87.         //initialize dialog itself
  88.         setModal(true);
  89.         setTitle("Login");//UIManager.getString(CLASS_NAME + ".loginString"));
  90.         loginPanel.getLoginService().addLoginListener(new Listener());
  91.         progressPanel = new ProgressPane();
  92.         
  93.         //create the default banner
  94.         banner = new JXImagePanel();
  95.         banner.setImage(createLoginBanner());
  96.         
  97.         //create the default label
  98.         label = new JLabel("Enter your user name and password");
  99.         label.setFont(label.getFont().deriveFont(Font.BOLD));
  100.         
  101.         //create the message and hyperlink and hide them
  102.         messageLabel = new JLabel(" ");
  103.         messageLabel.setVisible(false);
  104.         detailsLink = new JXHyperlink();
  105.         detailsLink.setVisible(false);
  106.         
  107.         //create the buttons
  108.         JButton okButton = new JButton("OK");//UIManager.getString(CLASS_NAME + ".okString"));
  109.         okButton.addActionListener(new ActionListener() {
  110.             public void actionPerformed(ActionEvent ae) {
  111.                 cancelled = false;
  112.                 loginPanel.startLogin();
  113.             }
  114.         });
  115.         okButton.setMnemonic('O');//UIManager.getInt(CLASS_NAME + ".okString.mnemonic")); 
  116.         okButton.setPreferredSize(new Dimension(80, okButton.getPreferredSize().height));
  117.         JButton cancelButton = new JButton("Cancel");//UIManager.getString(CLASS_NAME + ".cancelString"));
  118.         cancelButton.addActionListener(new ActionListener() {
  119.             public void actionPerformed(ActionEvent ae) {
  120.                 setVisible(false);
  121.             }
  122.         });
  123.         cancelButton.setMnemonic('C');//UIManager.getInt(CLASS_NAME + ".cancelString.mnemonic"));
  124.         cancelButton.setPreferredSize(new Dimension(80, okButton.getPreferredSize().height));
  125.         
  126.         //layout the dialog
  127.         setLayout(new BorderLayout());
  128.         add(banner, BorderLayout.NORTH);
  129.         
  130.         contentPanel = new JXPanel(new GridBagLayout());
  131.         contentPanel.add(label, new GridBagConstraints(0, 0, 2, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(12, 12, 7, 11), 0, 0));
  132.         contentPanel.add(loginPanel, new GridBagConstraints(1, 1, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0, 36, 7, 11), 0, 0));
  133.         contentPanel.add(messageLabel, new GridBagConstraints(1, 2, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 36, 0, 11), 0, 0));
  134. //        contentPanel.add(detailsLink, )
  135.         add(contentPanel, BorderLayout.CENTER);
  136.         
  137.         buttonPanel = new JXPanel(new GridBagLayout());
  138.         buttonPanel.add(okButton, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(17, 12, 11, 5), 0, 0));
  139.         buttonPanel.add(cancelButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.EAST, GridBagConstraints.NONE, new Insets(17, 0, 11, 11), 0, 0));
  140.         add(buttonPanel, BorderLayout.SOUTH);
  141.         
  142. //        service.addLoginListener(this);
  143. //        panel.okButton.addActionListener(this);
  144. //        panel.cancelButton.addActionListener(this);
  145.         getRootPane().setDefaultButton(okButton);
  146.         pack();
  147.         setResizable(false);
  148.         setLocation(WindowUtils.getPointForCentering(this));
  149.         setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  150.         
  151. //        progressIndicator = new JProgressBar();
  152. //        loginProgress = new JLabel(UIManager.getString(CLASS_NAME + ".loginIntructionString"));
  153. //        cancelLogin = new JButton(UIManager.getString(CLASS_NAME + ".cancelString"));
  154. //        cancelLogin.addActionListener(new ActionListener() {
  155. //            public void actionPerformed(ActionEvent ev) {
  156. //                loginService.cancelAuthentication();
  157. //                progressIndicator.setIndeterminate(false);
  158. //                loginPanel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
  159. //                cancelLogin.setEnabled(false);
  160. //            }
  161. //        });
  162. //        cancelLogin.setEnabled(false);
  163.     }
  164. //    public void loginFailed(LoginEvent source) {
  165. //        finishedLogin(false);
  166. //        loginPanel.loginProgress.setText(UIManager.getString(CLASS_NAME + ".loginFailed"));
  167. //    }
  168. //
  169. //    public void loginSucceeded(LoginEvent source) {
  170. //        finishedLogin(true);
  171. //        dialog.dispose();
  172. //    }
  173. //
  174. //    public void loginStarted(LoginEvent source) {
  175. //
  176. //    }
  177. //
  178. //
  179. //    void finishedLogin(boolean result) {
  180. //        loginPanel.cancelLogin.setEnabled(false);
  181. //        loginPanel.progressIndicator.setIndeterminate(false);
  182. //        loginPanel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
  183. //    }
  184. //
  185. //    void cancelAuthentication() {
  186. //        service.cancelAuthentication();
  187. //        loginPanel.cancelLogin.setEnabled(false);
  188. //        loginPanel.progressIndicator.setIndeterminate(false);
  189. //        loginPanel.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
  190. //    }
  191. //
  192. //    public void loginCanceled(LoginEvent source) {
  193. //        cancelled = true;
  194. //    }
  195. //
  196. //    public void actionPerformed(ActionEvent ae) {
  197. //        Object source = ae.getSource();
  198. //        if (source == loginPanel.okButton) {
  199. //            startLogin();
  200. //        } else if (source == loginPanel.cancelLogin) {
  201. //            cancelAuthentication();
  202. //        } else if (source == loginPanel.cancelButton) {
  203. //            dialog.dispose();
  204. //        }
  205. //    }
  206.     
  207.     private BufferedImage createLoginBanner() {
  208.         int w = 400;
  209.         int h = 60;
  210.         
  211.         BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
  212.         Graphics2D g2 = img.createGraphics();
  213.         
  214.         g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
  215.         g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  216.         g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
  217.         g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
  218.         //draw a big square
  219.         g2.setColor(UIManager.getColor("JXTitledPanel.title.darkBackground"));
  220.         g2.fillRect(0, 0, w, h);
  221.         
  222.         //create the curve shape
  223.         GeneralPath curveShape = new GeneralPath(GeneralPath.WIND_NON_ZERO);
  224.         curveShape.moveTo(0, h * .6f);
  225.         curveShape.curveTo(w * .167f, h * 1.2f, w * .667f, h * -.5f, w, h * .75f);
  226.         curveShape.lineTo(w, h);
  227.         curveShape.lineTo(0, h);
  228.         curveShape.lineTo(0, h * .8f);
  229.         curveShape.closePath();
  230.         
  231.         //draw into the buffer a gradient (bottom to top), and the text "Login"
  232.         GradientPaint gp = new GradientPaint(0, h, UIManager.getColor("JXTitledPanel.title.darkBackground"), 
  233.                 0, 0, UIManager.getColor("JXTitledPanel.title.lightBackground"));
  234.         g2.setPaint(gp);
  235.         g2.fill(curveShape);
  236.         Font font = new Font("Arial Bold", Font.PLAIN, 36);
  237.         g2.setFont(font);
  238.         g2.setColor(UIManager.getColor("JXTitledPanel.title.foreground"));
  239.         g2.drawString("Login", w * .05f, h * .75f);
  240.         return img;
  241.     }
  242.     /**
  243.      * Used as a glass pane when doing the login procedure
  244.      */
  245.     private final class ProgressPane extends JXPanel {
  246.         public ProgressPane() {
  247.             setLayout(new BorderLayout(24, 24));
  248.             JXPanel contentPanel = new JXPanel(new GridBagLayout());
  249.             add(contentPanel, BorderLayout.CENTER);
  250.             
  251.             JLabel label = new JLabel("Please wait, logging in....");
  252.             label.setFont(label.getFont().deriveFont(Font.BOLD));
  253.             
  254.             JProgressBar pb = new JProgressBar();
  255.             pb.setIndeterminate(true);
  256.             
  257.             JButton stopButton = new JButton("Stop login");
  258.             stopButton.addActionListener(new ActionListener() {
  259.                 public void actionPerformed(ActionEvent ae) {
  260.                     loginPanel.cancelLogin();
  261.                 }
  262.             });
  263.             
  264.             contentPanel.add(label, new GridBagConstraints(0, 0, 2, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(12, 12, 11, 11), 0, 0));
  265.             contentPanel.add(pb, new GridBagConstraints(0, 1, 1, 1, 1.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 24, 11, 7), 0, 0));
  266.             contentPanel.add(stopButton, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 11, 11), 0, 0));
  267.         }
  268.     }
  269.     
  270.     private final class Listener implements LoginListener {
  271.         public void loginSucceeded(LoginEvent source) {
  272.             setVisible(false);
  273.         }
  274.         public void loginStarted(LoginEvent source) {
  275.             //switch to login animation
  276.             buttonPanel.setVisible(false);
  277.             remove(contentPanel);
  278.             add(progressPanel, BorderLayout.CENTER);
  279.         }
  280.         public void loginFailed(LoginEvent source) {
  281.             //switch to input fields, show error
  282.             buttonPanel.setVisible(true);
  283.             remove(progressPanel);
  284.             add(contentPanel, BorderLayout.CENTER);
  285.         }
  286.         public void loginCanceled(LoginEvent source) {
  287.             //switch to input fields, show message
  288.             buttonPanel.setVisible(true);
  289.             remove(progressPanel);
  290.             add(contentPanel, BorderLayout.CENTER);
  291.         }
  292.     }
  293. }