1 18 package org.apache.batik.apps.svgbrowser; 19 20 import java.awt.BorderLayout ; 21 import java.awt.Container ; 22 import java.awt.EventQueue ; 23 import java.awt.Frame ; 24 import java.awt.GridBagConstraints ; 25 import java.awt.GridBagLayout ; 26 import java.awt.event.ActionEvent ; 27 import java.awt.event.ActionListener ; 28 import java.awt.event.WindowAdapter ; 29 import java.awt.event.WindowEvent ; 30 import java.net.Authenticator ; 31 import java.net.PasswordAuthentication ; 32 33 import javax.swing.JButton ; 34 import javax.swing.JComponent ; 35 import javax.swing.JDialog ; 36 import javax.swing.JLabel ; 37 import javax.swing.JPanel ; 38 import javax.swing.JPasswordField ; 39 import javax.swing.JTextField ; 40 import javax.swing.SwingConstants ; 41 42 47 public class JAuthenticator extends Authenticator { 48 49 52 public static final String TITLE 53 = "JAuthenticator.title"; 54 public static final String LABEL_SITE 55 = "JAuthenticator.label.site"; 56 public static final String LABEL_REQ 57 = "JAuthenticator.label.req"; 58 public static final String LABEL_USERID 59 = "JAuthenticator.label.userID"; 60 public static final String LABEL_PASSWORD 61 = "JAuthenticator.label.password"; 62 63 public static final String LABEL_CANCEL 64 = "JAuthenticator.label.cancel"; 65 public static final String LABEL_OK 66 = "JAuthenticator.label.ok"; 67 68 protected JDialog window; 69 protected JButton cancelButton; 70 protected JButton okButton; 71 72 protected JLabel label1; 73 protected JLabel label2; 74 protected JTextField JUserID; 75 protected JPasswordField JPassword; 76 77 Object lock = new Object (); 78 79 private boolean result; 80 private boolean wasNotified; 81 private String userID; 82 private char [] password; 83 84 public JAuthenticator() { 85 initWindow(); 86 } 87 88 protected void initWindow() { 89 String title = Resources.getString(TITLE); 90 window = new JDialog ((Frame )null, title, true); 91 92 Container mainPanel = window.getContentPane(); 93 mainPanel.setLayout(new BorderLayout ()); 94 mainPanel.add(buildAuthPanel(), BorderLayout.CENTER); 95 mainPanel.add(buildButtonPanel(), BorderLayout.SOUTH); 96 window.pack(); 97 98 window.addWindowListener( new WindowAdapter () { 99 public void windowClosing(WindowEvent e) { 100 cancelListener.actionPerformed 101 (new ActionEvent (e.getWindow(), 102 ActionEvent.ACTION_PERFORMED, 103 "Close")); 104 } 105 }); 106 } 107 108 protected JComponent buildAuthPanel() { 109 GridBagLayout gridBag = new GridBagLayout (); 110 GridBagConstraints c = new GridBagConstraints (); 111 JPanel proxyPanel = new JPanel (gridBag); 112 c.fill = GridBagConstraints.BOTH; 113 c.weightx = 1.0; 114 115 c.gridwidth = 1; 116 JLabel labelS = new JLabel (Resources.getString(LABEL_SITE)); 117 labelS.setHorizontalAlignment(SwingConstants.LEFT); 118 gridBag.setConstraints(labelS, c); 119 proxyPanel.add(labelS); 120 121 c.gridwidth = GridBagConstraints.REMAINDER; 122 label1 = new JLabel (""); 123 label1.setHorizontalAlignment(SwingConstants.LEFT); 124 gridBag.setConstraints(label1, c); 125 proxyPanel.add(label1); 126 127 c.gridwidth = 1; 128 JLabel labelR = new JLabel (Resources.getString(LABEL_REQ)); 129 labelR.setHorizontalAlignment(SwingConstants.LEFT); 130 gridBag.setConstraints(labelR, c); 131 proxyPanel.add(labelR); 132 133 c.gridwidth = GridBagConstraints.REMAINDER; 134 label2 = new JLabel (""); 135 label2.setHorizontalAlignment(SwingConstants.LEFT); 136 gridBag.setConstraints(label2, c); 137 proxyPanel.add(label2); 138 139 c.gridwidth = 1; 140 JLabel labelUserID = new JLabel (Resources.getString(LABEL_USERID)); 141 labelUserID.setHorizontalAlignment(SwingConstants.LEFT); 142 gridBag.setConstraints(labelUserID, c); 143 proxyPanel.add(labelUserID); 144 145 c.gridwidth = GridBagConstraints.REMAINDER; 146 JUserID = new JTextField (20); 147 gridBag.setConstraints(JUserID, c); 148 proxyPanel.add(JUserID); 149 150 c.gridwidth = 1; 151 JLabel labelPassword = new JLabel (Resources.getString(LABEL_PASSWORD)); 152 labelPassword.setHorizontalAlignment(SwingConstants.LEFT); 153 gridBag.setConstraints(labelPassword, c); 154 proxyPanel.add(labelPassword); 155 156 c.gridwidth = GridBagConstraints.REMAINDER; 157 JPassword = new JPasswordField (20); 158 JPassword.setEchoChar('*'); 159 JPassword.addActionListener(okListener); 160 gridBag.setConstraints(JPassword, c); 161 proxyPanel.add(JPassword); 162 163 return proxyPanel; 164 } 165 166 167 168 protected JComponent buildButtonPanel() { 169 JPanel buttonPanel = new JPanel (); 170 cancelButton = new JButton (Resources.getString(LABEL_CANCEL)); 171 cancelButton.addActionListener(cancelListener); 172 buttonPanel.add(cancelButton); 173 174 okButton = new JButton (Resources.getString(LABEL_OK)); 175 okButton.addActionListener( okListener); 176 buttonPanel.add(okButton); 177 178 return buttonPanel; 179 } 180 181 187 public PasswordAuthentication getPasswordAuthentication() { 188 synchronized (lock) { 189 EventQueue.invokeLater(new Runnable () { 190 public void run() { 191 label1.setText(getRequestingSite().getHostName()); 192 label2.setText(getRequestingPrompt()); 193 window.setVisible(true); 194 } 195 }); 196 wasNotified = false; 197 while (!wasNotified) { 198 try { 199 lock.wait(); 200 } catch(InterruptedException ie) { } 201 } 202 if (!result) 203 return null; 204 205 return new PasswordAuthentication (userID, password); 206 } 207 } 208 209 ActionListener okListener = new ActionListener () { 210 public void actionPerformed(ActionEvent e) { 211 synchronized (lock) { 212 window.setVisible(false); 213 214 userID = JUserID.getText(); 215 password = JPassword.getPassword(); 216 JPassword.setText(""); 217 result = true; 218 wasNotified = true; 219 lock.notifyAll(); 220 } 221 } 222 }; 223 224 ActionListener cancelListener = new ActionListener () { 225 public void actionPerformed(ActionEvent e) { 226 synchronized (lock) { 227 window.setVisible(false); 228 229 userID = null; 230 JUserID.setText(""); 231 password = null; 232 JPassword.setText(""); 233 result = false; 234 wasNotified = true; 235 lock.notifyAll(); 236 } 237 } 238 }; 239 } 240 | Popular Tags |