1 7 package net.suberic.pooka.gui; 8 9 import javax.mail.*; 10 import java.net.InetAddress ; 11 import java.awt.*; 12 import java.awt.event.*; 13 import javax.swing.*; 14 15 22 23 public class SimpleAuthenticator extends Authenticator { 24 25 String username; 26 String password; 27 28 public SimpleAuthenticator() { 29 } 30 31 protected PasswordAuthentication getPasswordAuthentication() { 32 33 String prompt = getRequestingPrompt(); 35 if (prompt == null) 36 prompt = "Please login..."; 37 38 String protocol = getRequestingProtocol(); 40 if (protocol == null) 41 protocol = "Unknown protocol"; 42 43 String host = null; 45 InetAddress inet = getRequestingSite(); 46 if (inet != null) 47 host = inet.getHostName(); 48 if (host == null) 49 host = "Unknown host"; 50 51 String port = ""; 53 int portnum = getRequestingPort(); 54 if (portnum != -1) 55 port = ", port " + portnum + " "; 56 57 String info = "Connecting to " + protocol + " mail service on host " + 59 host + port; 60 61 JComponent d = new JComponent() { }; 66 67 GridBagLayout gb = new GridBagLayout(); 68 GridBagConstraints c = new GridBagConstraints(); 69 d.setLayout(gb); 70 c.insets = new Insets(2, 2, 2, 2); 71 72 c.anchor = GridBagConstraints.WEST; 73 c.gridwidth = GridBagConstraints.REMAINDER; 74 c.weightx = 0.0; 75 d.add(constrain(new JLabel(info), gb, c)); 76 d.add(constrain(new JLabel(prompt), gb, c)); 77 78 c.gridwidth = 1; 79 c.anchor = GridBagConstraints.EAST; 80 c.fill = GridBagConstraints.NONE; 81 c.weightx = 0.0; 82 d.add(constrain(new JLabel("Username:"), gb, c)); 83 84 c.anchor = GridBagConstraints.EAST; 85 c.fill = GridBagConstraints.HORIZONTAL; 86 c.gridwidth = GridBagConstraints.REMAINDER; 87 c.weightx = 1.0; 88 final String user = getDefaultUserName(); 89 final JTextField username = new JTextField(user, 20); 90 d.add(constrain(username, gb, c)); 91 92 c.gridwidth = 1; 93 c.fill = GridBagConstraints.NONE; 94 c.anchor = GridBagConstraints.EAST; 95 c.weightx = 0.0; 96 d.add(constrain(new JLabel("Password:"), gb, c)); 97 98 c.anchor = GridBagConstraints.EAST; 99 c.fill = GridBagConstraints.HORIZONTAL; 100 c.gridwidth = GridBagConstraints.REMAINDER; 101 c.weightx = 1.0; 102 final JPasswordField password = new JPasswordField("", 20); 103 d.add(constrain(password, gb, c)); 104 105 final JOptionPane authPane = new JOptionPane(d, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION) { 106 public void selectInitialValue() { 107 if (user != null && user.length() > 0) { 108 password.requestFocus(); 109 } 110 else { 111 username.requestFocus(); 112 } 113 } 114 }; 115 116 final JDialog dialog = authPane.createDialog(getFrame(), "Login"); 117 118 authPane.selectInitialValue(); 119 120 dialog.setVisible(true); 121 122 Object result = authPane.getValue(); 123 124 if (result instanceof Integer ) { 125 int resultInt = ((Integer ) result).intValue(); 126 if (resultInt == JOptionPane.OK_OPTION) 127 return new PasswordAuthentication(username.getText(), 128 new String (password.getPassword())); 129 } 130 return null; 131 } 132 133 private Component constrain(Component cmp, 134 GridBagLayout gb, GridBagConstraints c) { 135 gb.setConstraints(cmp, c); 136 return (cmp); 137 } 138 139 public Frame getFrame() { 140 MainPanel mp= net.suberic.pooka.Pooka.getMainPanel(); 141 if (mp != null) 142 return mp.getParentFrame(); 143 else 144 return null; 145 } 146 } 147 | Popular Tags |