001: /*
002: * $Id: JGraphpadAuthenticator.java,v 1.3 2005/08/07 14:24:57 gaudenz Exp $
003: * Copyright (c) 2001-2005, Gaudenz Alder
004: *
005: * All rights reserved.
006: *
007: * See LICENSE file for license details. If you are unable to locate
008: * this file please contact info (at) jgraph (dot) com.
009: */
010: package com.jgraph.pad.dialog;
011:
012: import java.awt.GridBagConstraints;
013: import java.awt.GridBagLayout;
014: import java.awt.event.ActionEvent;
015: import java.awt.event.ActionListener;
016: import java.awt.event.WindowAdapter;
017: import java.awt.event.WindowEvent;
018: import java.net.Authenticator;
019: import java.net.PasswordAuthentication;
020:
021: import javax.swing.BorderFactory;
022: import javax.swing.JButton;
023: import javax.swing.JComponent;
024: import javax.swing.JLabel;
025: import javax.swing.JPanel;
026: import javax.swing.JPasswordField;
027: import javax.swing.JTextField;
028:
029: import com.jgraph.JGraphpad;
030: import com.jgraph.editor.JGraphEditorResources;
031:
032: /**
033: * Simple authenticator for basic auth.
034: */
035: public class JGraphpadAuthenticator extends Authenticator {
036:
037: // This method is called when a password-protected URL is accessed
038: protected PasswordAuthentication getPasswordAuthentication() {
039: // Get information about the request
040: String title = getRequestingPrompt();
041: String subtitle = String.valueOf(getRequestingSite());
042: AuthDialog auth = new AuthDialog(title, subtitle);
043: auth.setSize(320, 240);
044: auth.setModal(true);
045: JGraphpad.center(auth);
046: auth.setVisible(true);
047:
048: // Return the information
049: return new PasswordAuthentication(auth.getUsername(), auth
050: .getPassword());
051: }
052:
053: /**
054: * Implements the authentication dialog for network connections.
055: */
056: public static class AuthDialog extends JGraphpadDialog {
057:
058: /**
059: * Holds the username field. Note: createContent is called before the
060: * instance initialization so the instance must be constructed there.
061: */
062: protected JTextField username;
063:
064: /**
065: * Holds the password field. Note: createContent is called before the
066: * instance initialization so the instance must be constructed there.
067: */
068: protected JPasswordField password;
069:
070: /**
071: * Constructs a new authentication dialog.
072: *
073: * @param title
074: * The title to be displayed.
075: * @param subtitle
076: * The subtitle to be displayed.
077: */
078: public AuthDialog(String title, String subtitle) {
079: super (title, subtitle, JGraphEditorResources
080: .getImage(JGraphEditorResources
081: .getString("authDialog.icon")));
082:
083: // Adds OK button to close window
084: JButton okButton = new JButton(JGraphEditorResources
085: .getString("OK"));
086: addButtons(new JButton[] { okButton });
087: okButton.addActionListener(new ActionListener() {
088: public void actionPerformed(ActionEvent e) {
089: dispose();
090: }
091: });
092:
093: // Sets default button for enter key
094: getRootPane().setDefaultButton(okButton);
095:
096: // Focused the username field when the window
097: // becomes visible.
098: addWindowListener(new WindowAdapter() {
099:
100: /* (non-Javadoc)
101: */
102: public void windowOpened(WindowEvent e) {
103: username.requestFocus();
104: }
105: });
106: }
107:
108: /**
109: * Overrides {@link JGraphpadDialog#createContent()} to provide the
110: * actual content of the dialog.
111: *
112: * @return Returns the content of the auth dialog.
113: */
114: protected JComponent createContent() {
115: username = new JTextField(16);
116: password = new JPasswordField(16);
117:
118: JPanel panel = new JPanel();
119: panel
120: .setBorder(BorderFactory.createEmptyBorder(4, 8, 4,
121: 8));
122: panel.setLayout(new GridBagLayout());
123:
124: GridBagConstraints c = new GridBagConstraints();
125: c.anchor = GridBagConstraints.NORTHWEST;
126: c.fill = GridBagConstraints.NONE;
127: c.ipadx = 2;
128: c.ipady = 2;
129: c.weighty = 0.0;
130: c.weightx = 1.0;
131: c.gridwidth = 1;
132: c.gridx = 0;
133: c.gridy = 0;
134: panel.add(new JLabel(JGraphEditorResources
135: .getString("username")
136: + ":"), c);
137:
138: c.gridx = 1;
139: c.gridy = 0;
140: panel.add(username, c);
141:
142: c.gridx = 0;
143: c.gridy = 1;
144: panel.add(new JLabel(JGraphEditorResources
145: .getString("password")
146: + ":"), c);
147:
148: c.gridx = 1;
149: c.gridy = 1;
150: panel.add(password, c);
151: return panel;
152: }
153:
154: /**
155: * Returns the username from the username field.
156: */
157: public String getUsername() {
158: return username.getText();
159: }
160:
161: /**
162: * Returns the password from the password field.
163: */
164: public char[] getPassword() {
165: return password.getPassword();
166: }
167:
168: }
169:
170: }
|