001: // AuthPanel.java
002: // $Id: AuthPanel.java,v 1.5 2000/08/16 21:37:31 ylafon Exp $
003: // (c) COPYRIGHT MIT and INRIA, 1998.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.jigadmin.gui;
007:
008: import javax.swing.JPanel;
009: import javax.swing.JTextField;
010: import javax.swing.JPasswordField;
011: import javax.swing.JButton;
012: import javax.swing.JLabel;
013: import javax.swing.BorderFactory;
014:
015: import java.awt.event.ActionListener;
016: import java.awt.event.ActionEvent;
017:
018: import java.awt.GridLayout;
019: import java.awt.GridBagLayout;
020: import java.awt.GridBagConstraints;
021: import java.awt.Insets;
022:
023: import org.w3c.www.http.HttpCredential;
024: import org.w3c.www.http.HttpFactory;
025:
026: import org.w3c.tools.codec.Base64Encoder;
027:
028: /**
029: * The Authentication dialog.
030: * @version $Revision: 1.5 $
031: * @author Benoît Mahé (bmahe@w3.org)
032: */
033: public class AuthPanel extends JPanel {
034:
035: protected boolean ok;
036: protected ServerBrowser sb;
037: protected JTextField user;
038: protected JPasswordField passwd;
039:
040: /**
041: * Constructor.
042: * @param sb The ServerBrowser
043: * @param name the realm name.
044: */
045: protected AuthPanel(ServerBrowser sb, String name) {
046: this .sb = sb;
047: this .ok = false;
048:
049: ActionListener al = new ActionListener() {
050: public void actionPerformed(ActionEvent ae) {
051: if (ae.getActionCommand().equals("Ok")
052: || ae.getSource().equals(passwd)) {
053: if (!user.getText().equals("")) {
054: HttpCredential credential;
055: credential = HttpFactory
056: .makeCredential("Basic");
057: String password = new String(passwd
058: .getPassword());
059: Base64Encoder encoder = new Base64Encoder(user
060: .getText()
061: + ":" + password);
062: credential.setAuthParameter("cookie", encoder
063: .processString());
064: AuthPanel.this .sb.getAdminContext()
065: .setCredential(credential);
066: AuthPanel.this .sb.dispose(true);
067: done();
068: } else {
069: user.requestFocus();
070: }
071: } else if (ae.getActionCommand().equals("Cancel")) {
072: AuthPanel.this .sb.dispose(false);
073: done();
074: } else if (ae.getSource().equals(user)) {
075: passwd.requestFocus();
076: }
077: }
078: };
079:
080: GridBagLayout gbl = new GridBagLayout();
081: GridBagConstraints gbc = new GridBagConstraints();
082: GridBagLayout mgbl = new GridBagLayout();
083: GridBagConstraints mgbc = new GridBagConstraints();
084: JLabel l;
085: JButton b;
086: JPanel p = new JPanel(gbl);
087:
088: user = new JTextField(10);
089:
090: passwd = new JPasswordField(10);
091: passwd.addActionListener(al);
092:
093: gbc.fill = GridBagConstraints.HORIZONTAL;
094: gbc.weightx = 0;
095: gbc.weighty = 0;
096: mgbc.fill = GridBagConstraints.NONE;
097: mgbc.weightx = 0;
098: mgbc.weighty = 0;
099: mgbc.insets = new Insets(16, 10, 16, 5);
100: setLayout(mgbl);
101:
102: l = new JLabel("User name: ", JLabel.RIGHT);
103: gbc.gridwidth = 1;
104: gbl.setConstraints(l, gbc);
105: p.add(l);
106: gbc.gridwidth = GridBagConstraints.REMAINDER;
107: gbl.setConstraints(user, gbc);
108: p.add(user);
109:
110: l = new JLabel("Password: ", JLabel.RIGHT);
111: gbc.gridwidth = 1;
112: gbl.setConstraints(l, gbc);
113: p.add(l);
114: gbc.gridwidth = GridBagConstraints.REMAINDER;
115: gbl.setConstraints(passwd, gbc);
116: p.add(passwd);
117: mgbc.gridwidth = GridBagConstraints.REMAINDER;
118: mgbl.setConstraints(p, mgbc);
119: add(p);
120:
121: // and now the usual button bar
122: p = new JPanel(new GridLayout(1, 2, 20, 20));
123: b = new JButton("Ok");
124: b.addActionListener(al);
125: p.add(b);
126: b = new JButton("Cancel");
127: b.addActionListener(al);
128: p.add(b);
129: mgbl.setConstraints(p, mgbc);
130: add(p);
131: if (name != null)
132: setBorder(BorderFactory
133: .createTitledBorder("Realm: " + name));
134: else
135: setBorder(BorderFactory
136: .createTitledBorder("Authentication"));
137: }
138:
139: /**
140: * Request the focus on the user TextField.
141: */
142: protected void getFocus() {
143: user.requestFocus();
144: }
145:
146: /**
147: * Notify All thread that the username/password are entered.
148: */
149: protected synchronized void done() {
150: ok = true;
151: notifyAll();
152: }
153:
154: /**
155: * Wait until it's done.
156: */
157: public synchronized boolean waitForCompletion() {
158: try {
159: wait();
160: } catch (InterruptedException ex) {
161: //nothing to do
162: }
163: return ok;
164: }
165:
166: }
|