KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)SimpleAuthenticator.java 1.3 98/03/20
3  *
4  * Copyright (c) 1996-1998 by Sun Microsystems, Inc.
5  * All Rights Reserved.
6  */

7 package net.suberic.pooka.gui;
8
9 import javax.mail.*;
10 import java.net.InetAddress JavaDoc;
11 import java.awt.*;
12 import java.awt.event.*;
13 import javax.swing.*;
14
15 /**
16  * Simple Authenticator for requesting password information.
17  *
18  * @version 1.3, 98/03/20
19  * @author Christopher Cotton
20  * @author Bill Shannon
21  */

22
23 public class SimpleAuthenticator extends Authenticator {
24
25     String JavaDoc username;
26     String JavaDoc password;
27
28     public SimpleAuthenticator() {
29     }
30
31     protected PasswordAuthentication getPasswordAuthentication() {
32
33   // given a prompt?
34
String JavaDoc prompt = getRequestingPrompt();
35   if (prompt == null)
36       prompt = "Please login...";
37
38   // protocol
39
String JavaDoc protocol = getRequestingProtocol();
40   if (protocol == null)
41       protocol = "Unknown protocol";
42
43   // get the host
44
String JavaDoc host = null;
45   InetAddress JavaDoc inet = getRequestingSite();
46   if (inet != null)
47       host = inet.getHostName();
48   if (host == null)
49       host = "Unknown host";
50
51   // port
52
String JavaDoc port = "";
53   int portnum = getRequestingPort();
54   if (portnum != -1)
55       port = ", port " + portnum + " ";
56
57   // Build the info string
58
String JavaDoc info = "Connecting to " + protocol + " mail service on host " +
59                 host + port;
60
61   //JPanel d = new JPanel();
62
// XXX - for some reason using a JPanel here causes JOptionPane
63
// to display incorrectly, so we workaround the problem using
64
// an anonymous JComponent.
65
JComponent d = new JComponent() { };
66
67   GridBagLayout gb = new GridBagLayout();
68   GridBagConstraints c = new GridBagConstraints();
69   d.setLayout(gb);
70   c.insets = new Insets(2, 2, 2, 2);
71
72   c.anchor = GridBagConstraints.WEST;
73   c.gridwidth = GridBagConstraints.REMAINDER;
74   c.weightx = 0.0;
75   d.add(constrain(new JLabel(info), gb, c));
76   d.add(constrain(new JLabel(prompt), gb, c));
77
78   c.gridwidth = 1;
79   c.anchor = GridBagConstraints.EAST;
80   c.fill = GridBagConstraints.NONE;
81   c.weightx = 0.0;
82   d.add(constrain(new JLabel("Username:"), gb, c));
83
84   c.anchor = GridBagConstraints.EAST;
85   c.fill = GridBagConstraints.HORIZONTAL;
86   c.gridwidth = GridBagConstraints.REMAINDER;
87   c.weightx = 1.0;
88   final String JavaDoc user = getDefaultUserName();
89   final JTextField username = new JTextField(user, 20);
90   d.add(constrain(username, gb, c));
91
92   c.gridwidth = 1;
93   c.fill = GridBagConstraints.NONE;
94   c.anchor = GridBagConstraints.EAST;
95   c.weightx = 0.0;
96   d.add(constrain(new JLabel("Password:"), gb, c));
97
98   c.anchor = GridBagConstraints.EAST;
99   c.fill = GridBagConstraints.HORIZONTAL;
100   c.gridwidth = GridBagConstraints.REMAINDER;
101   c.weightx = 1.0;
102   final JPasswordField password = new JPasswordField("", 20);
103   d.add(constrain(password, gb, c));
104
105   final JOptionPane authPane = new JOptionPane(d, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION) {
106       public void selectInitialValue() {
107         if (user != null && user.length() > 0) {
108     password.requestFocus();
109         }
110         else {
111     username.requestFocus();
112         }
113       }
114     };
115
116   final JDialog dialog = authPane.createDialog(getFrame(), "Login");
117
118   authPane.selectInitialValue();
119
120   dialog.setVisible(true);
121
122   Object JavaDoc result = authPane.getValue();
123
124   if (result instanceof Integer JavaDoc) {
125     int resultInt = ((Integer JavaDoc) result).intValue();
126     if (resultInt == JOptionPane.OK_OPTION)
127       return new PasswordAuthentication(username.getText(),
128                 new String JavaDoc(password.getPassword()));
129   }
130   return null;
131     }
132
133     private Component constrain(Component cmp,
134               GridBagLayout gb, GridBagConstraints c) {
135   gb.setConstraints(cmp, c);
136   return (cmp);
137     }
138
139   public Frame getFrame() {
140     MainPanel mp= net.suberic.pooka.Pooka.getMainPanel();
141     if (mp != null)
142       return mp.getParentFrame();
143     else
144       return null;
145   }
146 }
147
Popular Tags