KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > gui > LoadHttpConfigPooka


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 JavaDoc;
10 import java.io.InputStream JavaDoc;
11
12
13 /**
14  * Gives the option of loading configuration settings from an http file.
15  */

16 public class LoadHttpConfigPooka {
17
18   URL mUrl;
19   JFrame mFrame;
20
21   /**
22    * Starts the dialog.
23    */

24   public void start() {
25     Runnable JavaDoc runMe = new Runnable JavaDoc() {
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 JavaDoc ie) {
40       }
41     }
42   }
43
44   /**
45    * Shows the choices.
46    */

47   public void showChoices() {
48     String JavaDoc urlString = JOptionPane.showInputDialog("Choose a remote file.");
49     if (urlString == null)
50       return;
51
52     try {
53       URL configUrl = new URL(urlString);
54       InputStream JavaDoc 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 JavaDoc 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 JavaDoc username;
71     String JavaDoc password;
72
73     public HttpAuthenticator(Frame f) {
74       this.frame = f;
75     }
76
77     protected PasswordAuthentication getPasswordAuthentication() {
78
79       // given a prompt?
80
String JavaDoc prompt = getRequestingPrompt();
81       if (prompt == null) {
82   prompt = "Enter Username and Password...";
83       }
84
85       // protocol
86
String JavaDoc protocol = getRequestingProtocol();
87       if (protocol == null)
88   protocol = "Unknown protocol";
89
90       // get the host
91
String JavaDoc host = null;
92       InetAddress inet = getRequestingSite();
93       if (inet != null)
94   host = inet.getHostName();
95       if (host == null)
96   host = "Unknown host";
97
98       // port
99
String JavaDoc port = "";
100       int portnum = getRequestingPort();
101       if (portnum != -1)
102   port = ", port " + portnum + " ";
103
104       // Build the info string
105
String JavaDoc info = "Connecting to " + protocol + " resource on host " +
106   host + port;
107
108       //JPanel d = new JPanel();
109
// XXX - for some reason using a JPanel here causes JOptionPane
110
// to display incorrectly, so we workaround the problem using
111
// an anonymous JComponent.
112
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 JavaDoc 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       // XXX - following doesn't work
152
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