1 package net.suberic.pooka.gui; 2 3 import net.suberic.pooka.Pooka; 4 import net.suberic.util.VariableBundle; 5 6 import javax.swing.*; 7 import java.awt.*; 8 import java.net.*; 9 import java.io.IOException ; 10 import java.io.InputStream ; 11 12 13 16 public class LoadHttpConfigPooka { 17 18 URL mUrl; 19 JFrame mFrame; 20 21 24 public void start() { 25 Runnable runMe = new Runnable () { 26 public void run() { 27 mFrame = new JFrame(); 28 java.net.Authenticator.setDefault(new HttpAuthenticator(mFrame)); 29 mFrame.setVisible(true); 30 showChoices(); 31 } 32 }; 33 34 if (SwingUtilities.isEventDispatchThread()) 35 runMe.run(); 36 else { 37 try { 38 SwingUtilities.invokeAndWait(runMe); 39 } catch (Exception ie) { 40 } 41 } 42 } 43 44 47 public void showChoices() { 48 String urlString = JOptionPane.showInputDialog("Choose a remote file."); 49 if (urlString == null) 50 return; 51 52 try { 53 URL configUrl = new URL(urlString); 54 InputStream is = configUrl.openStream(); 55 VariableBundle newBundle = new VariableBundle(is, Pooka.getResources()); 56 Pooka.setResources(newBundle); 57 } catch (MalformedURLException mue) { 58 JOptionPane.showMessageDialog(mFrame, "Malformed URL."); 59 showChoices(); 60 } catch (java.io.IOException ioe) { 61 JOptionPane.showMessageDialog(mFrame, "Could not connect to URL: " + ioe.toString()); 62 showChoices(); 63 } 64 65 } 66 67 public class HttpAuthenticator extends Authenticator { 68 69 Frame frame; 70 String username; 71 String password; 72 73 public HttpAuthenticator(Frame f) { 74 this.frame = f; 75 } 76 77 protected PasswordAuthentication getPasswordAuthentication() { 78 79 String prompt = getRequestingPrompt(); 81 if (prompt == null) { 82 prompt = "Enter Username and Password..."; 83 } 84 85 String protocol = getRequestingProtocol(); 87 if (protocol == null) 88 protocol = "Unknown protocol"; 89 90 String host = null; 92 InetAddress inet = getRequestingSite(); 93 if (inet != null) 94 host = inet.getHostName(); 95 if (host == null) 96 host = "Unknown host"; 97 98 String port = ""; 100 int portnum = getRequestingPort(); 101 if (portnum != -1) 102 port = ", port " + portnum + " "; 103 104 String info = "Connecting to " + protocol + " resource on host " + 106 host + port; 107 108 JComponent d = new JComponent() { }; 113 114 GridBagLayout gb = new GridBagLayout(); 115 GridBagConstraints c = new GridBagConstraints(); 116 d.setLayout(gb); 117 c.insets = new Insets(2, 2, 2, 2); 118 119 c.anchor = GridBagConstraints.WEST; 120 c.gridwidth = GridBagConstraints.REMAINDER; 121 c.weightx = 0.0; 122 d.add(constrain(new JLabel(info), gb, c)); 123 d.add(constrain(new JLabel(prompt), gb, c)); 124 125 c.gridwidth = 1; 126 c.anchor = GridBagConstraints.EAST; 127 c.fill = GridBagConstraints.NONE; 128 c.weightx = 0.0; 129 d.add(constrain(new JLabel("Username:"), gb, c)); 130 131 c.anchor = GridBagConstraints.EAST; 132 c.fill = GridBagConstraints.HORIZONTAL; 133 c.gridwidth = GridBagConstraints.REMAINDER; 134 c.weightx = 1.0; 135 String user = ""; 136 JTextField username = new JTextField(user, 20); 137 d.add(constrain(username, gb, c)); 138 139 c.gridwidth = 1; 140 c.fill = GridBagConstraints.NONE; 141 c.anchor = GridBagConstraints.EAST; 142 c.weightx = 0.0; 143 d.add(constrain(new JLabel("Password:"), gb, c)); 144 145 c.anchor = GridBagConstraints.EAST; 146 c.fill = GridBagConstraints.HORIZONTAL; 147 c.gridwidth = GridBagConstraints.REMAINDER; 148 c.weightx = 1.0; 149 JPasswordField password = new JPasswordField("", 20); 150 d.add(constrain(password, gb, c)); 151 if (user != null && user.length() > 0) 153 password.requestFocusInWindow(); 154 else 155 username.requestFocusInWindow(); 156 157 int result = JOptionPane.showConfirmDialog(frame, d, "Login", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE); 158 159 if (result == JOptionPane.OK_OPTION) 160 return new PasswordAuthentication(username.getText(), 161 password.getPassword()); 162 else 163 return null; 164 } 165 166 private Component constrain(Component cmp, 167 GridBagLayout gb, GridBagConstraints c) { 168 gb.setConstraints(cmp, c); 169 return (cmp); 170 } 171 } 172 } 173 | Popular Tags |