001: package net.suberic.pooka.gui;
002:
003: import net.suberic.pooka.Pooka;
004: import net.suberic.util.VariableBundle;
005:
006: import javax.swing.*;
007: import java.awt.*;
008: import java.net.*;
009: import java.io.IOException;
010: import java.io.InputStream;
011:
012: /**
013: * Gives the option of loading configuration settings from an http file.
014: */
015: public class LoadHttpConfigPooka {
016:
017: URL mUrl;
018: JFrame mFrame;
019:
020: /**
021: * Starts the dialog.
022: */
023: public void start() {
024: Runnable runMe = new Runnable() {
025: public void run() {
026: mFrame = new JFrame();
027: java.net.Authenticator
028: .setDefault(new HttpAuthenticator(mFrame));
029: mFrame.show();
030: showChoices();
031: }
032: };
033:
034: if (SwingUtilities.isEventDispatchThread())
035: runMe.run();
036: else {
037: try {
038: SwingUtilities.invokeAndWait(runMe);
039: } catch (Exception ie) {
040: }
041: }
042: }
043:
044: /**
045: * Shows the choices.
046: */
047: public void showChoices() {
048: String urlString = JOptionPane
049: .showInputDialog("Choose a remote file.");
050: if (urlString == null)
051: return;
052:
053: try {
054: URL configUrl = new URL(urlString);
055: InputStream is = configUrl.openStream();
056: VariableBundle newBundle = new VariableBundle(is, Pooka
057: .getResources());
058: Pooka.setResources(newBundle);
059: } catch (MalformedURLException mue) {
060: JOptionPane.showMessageDialog(mFrame, "Malformed URL.");
061: showChoices();
062: } catch (java.io.IOException ioe) {
063: JOptionPane.showMessageDialog(mFrame,
064: "Could not connect to URL: " + ioe.toString());
065: showChoices();
066: }
067:
068: }
069:
070: public class HttpAuthenticator extends Authenticator {
071:
072: Frame frame;
073: String username;
074: String password;
075:
076: public HttpAuthenticator(Frame f) {
077: this .frame = f;
078: }
079:
080: protected PasswordAuthentication getPasswordAuthentication() {
081:
082: // given a prompt?
083: String prompt = getRequestingPrompt();
084: if (prompt == null) {
085: prompt = "Enter Username and Password...";
086: }
087:
088: // protocol
089: String protocol = getRequestingProtocol();
090: if (protocol == null)
091: protocol = "Unknown protocol";
092:
093: // get the host
094: String host = null;
095: InetAddress inet = getRequestingSite();
096: if (inet != null)
097: host = inet.getHostName();
098: if (host == null)
099: host = "Unknown host";
100:
101: // port
102: String port = "";
103: int portnum = getRequestingPort();
104: if (portnum != -1)
105: port = ", port " + portnum + " ";
106:
107: // Build the info string
108: String info = "Connecting to " + protocol
109: + " resource on host " + host + port;
110:
111: //JPanel d = new JPanel();
112: // XXX - for some reason using a JPanel here causes JOptionPane
113: // to display incorrectly, so we workaround the problem using
114: // an anonymous JComponent.
115: JComponent d = new JComponent() {
116: };
117:
118: GridBagLayout gb = new GridBagLayout();
119: GridBagConstraints c = new GridBagConstraints();
120: d.setLayout(gb);
121: c.insets = new Insets(2, 2, 2, 2);
122:
123: c.anchor = GridBagConstraints.WEST;
124: c.gridwidth = GridBagConstraints.REMAINDER;
125: c.weightx = 0.0;
126: d.add(constrain(new JLabel(info), gb, c));
127: d.add(constrain(new JLabel(prompt), gb, c));
128:
129: c.gridwidth = 1;
130: c.anchor = GridBagConstraints.EAST;
131: c.fill = GridBagConstraints.NONE;
132: c.weightx = 0.0;
133: d.add(constrain(new JLabel("Username:"), gb, c));
134:
135: c.anchor = GridBagConstraints.EAST;
136: c.fill = GridBagConstraints.HORIZONTAL;
137: c.gridwidth = GridBagConstraints.REMAINDER;
138: c.weightx = 1.0;
139: String user = "";
140: JTextField username = new JTextField(user, 20);
141: d.add(constrain(username, gb, c));
142:
143: c.gridwidth = 1;
144: c.fill = GridBagConstraints.NONE;
145: c.anchor = GridBagConstraints.EAST;
146: c.weightx = 0.0;
147: d.add(constrain(new JLabel("Password:"), gb, c));
148:
149: c.anchor = GridBagConstraints.EAST;
150: c.fill = GridBagConstraints.HORIZONTAL;
151: c.gridwidth = GridBagConstraints.REMAINDER;
152: c.weightx = 1.0;
153: JPasswordField password = new JPasswordField("", 20);
154: d.add(constrain(password, gb, c));
155: // XXX - following doesn't work
156: if (user != null && user.length() > 0)
157: password.requestFocusInWindow();
158: else
159: username.requestFocusInWindow();
160:
161: int result = JOptionPane.showConfirmDialog(frame, d,
162: "Login", JOptionPane.OK_CANCEL_OPTION,
163: JOptionPane.QUESTION_MESSAGE);
164:
165: if (result == JOptionPane.OK_OPTION)
166: return new PasswordAuthentication(username.getText(),
167: password.getPassword());
168: else
169: return null;
170: }
171:
172: private Component constrain(Component cmp, GridBagLayout gb,
173: GridBagConstraints c) {
174: gb.setConstraints(cmp, c);
175: return (cmp);
176: }
177: }
178: }
|