001: /*
002: * Copyright (c) 2000, Jacob Smullyan.
003: *
004: * This is part of SkunkDAV, a WebDAV client. See http://skunkdav.sourceforge.net/
005: * for the latest version.
006: *
007: * SkunkDAV is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License as published
009: * by the Free Software Foundation; either version 2, or (at your option)
010: * any later version.
011: *
012: * SkunkDAV is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with SkunkDAV; see the file COPYING. If not, write to the Free
019: * Software Foundation, 59 Temple Place - Suite 330, Boston, MA
020: * 02111-1307, USA.
021: */
022:
023: package org.skunk.dav.client.gui;
024:
025: import java.awt.GridBagConstraints;
026: import java.awt.GridBagLayout;
027: import java.awt.Insets;
028: import java.io.IOException;
029: import java.net.PasswordAuthentication;
030: import javax.swing.JCheckBox;
031: import javax.swing.JLabel;
032: import javax.swing.JOptionPane;
033: import javax.swing.JPanel;
034: import javax.swing.JPasswordField;
035: import javax.swing.JTextField;
036: import org.skunk.dav.client.DAVAuthenticator;
037: import org.skunk.trace.Debug;
038:
039: public class DAVAuthenticatorImpl extends DAVAuthenticator {
040: public static final String LOGIN_TITLE = ResourceManager
041: .getMessage(ResourceManager.LOGIN_TITLE);
042: private String loginMessage, username, password;
043: private boolean rememberPassword;
044: private boolean testedOnce = false;
045: private ServerData sd;
046:
047: //private boolean showRemember=true;
048:
049: public DAVAuthenticatorImpl() {
050: loginMessage = LOGIN_TITLE;
051: }
052:
053: public DAVAuthenticatorImpl(String host, int port,
054: String initialPath) {
055: //mea culpa -- this needs to be internationalized
056: loginMessage = ResourceManager.getMessage(
057: ResourceManager.LOGIN_MESSAGE, new Object[] { host
058: + ":" + port + initialPath });
059: //"Login to "+host+":"+port+initialPath;
060: username = null;
061: password = null;
062: rememberPassword = false;
063: }
064:
065: public DAVAuthenticatorImpl(ServerData sd) {
066: this (sd.getHost(), sd.getPort(), sd.getInitialPath());
067: username = sd.getUsername();
068: rememberPassword = sd.getRememberPassword();
069: password = rememberPassword ? sd.getPassword() : null;
070: this .sd = sd;
071: }
072:
073: // public DAVAuthenticatorImpl(String host, int port, String initialPath, boolean showRemember)
074: // {
075: // this(host, port, initialPath);
076: // this.showRemember=false;
077: // }
078:
079: public void reset() {
080: testedOnce = false;
081: }
082:
083: protected PasswordAuthentication getPasswordAuthentication() {
084: if (username == null || password == null || username.equals("")
085: || password.equals("") || !testedOnce) {
086: AuthenticatorPanel messagePanel = new AuthenticatorPanel(
087: username, password, rememberPassword);
088: // if (!showRemember)
089: // messagePanel.setShowRemember(false);
090: int result = JOptionPane.showConfirmDialog(Explorer
091: .guessActiveExplorer(), messagePanel,
092: getLoginMessage(), JOptionPane.OK_CANCEL_OPTION,
093: JOptionPane.PLAIN_MESSAGE);
094: if (result == JOptionPane.OK_OPTION) {
095: String oldUsername = username;
096: username = messagePanel.getUsername();
097: String oldPassword = password;
098: password = messagePanel.getPassword();
099: boolean oldRememberPassword = rememberPassword;
100: rememberPassword = messagePanel.getRememberPassword();
101: boolean changed = false;
102: if (sd != null) {
103: if (oldUsername != null
104: && !oldUsername.equals(username)) {
105: sd.setUsername(username);
106: changed = true;
107: }
108: if (rememberPassword) {
109: if (!oldRememberPassword) {
110: sd.setRememberPassword(true);
111: changed = true;
112: }
113: if (oldPassword != null
114: && !oldPassword.equals(password)) {
115: sd.setPassword(password);
116: changed = true;
117: }
118: } else {
119: if (oldRememberPassword) {
120: sd.setRememberPassword(false);
121: changed = true;
122: }
123: if (oldPassword != null) {
124: sd.setPassword(null);
125: changed = true;
126: }
127: }
128: }
129: if (changed) {
130: try {
131: ServerData.saveServers();
132: } catch (IOException oyVeh) {
133: Debug.trace(this , Debug.DP2, oyVeh);
134: }
135: }
136: } else
137: return null;
138: }
139: testedOnce = true;
140: return new PasswordAuthentication(username, password
141: .toCharArray());
142: }
143:
144: protected String getLoginMessage() {
145: return loginMessage;
146: }
147:
148: class AuthenticatorPanel extends JPanel {
149: JTextField usernameField = null;
150: JTextField passwordField = null;
151: JCheckBox rememberBox = null;
152: String username, password;
153: boolean rememberPassword;
154: final String USERNAME_PROMPT = ResourceManager
155: .getMessage(ResourceManager.USERNAME_PROMPT);
156: final String PASSWORD_PROMPT = ResourceManager
157: .getMessage(ResourceManager.PASSWORD_PROMPT);
158: final String REMEMBER_PASSWORD_PROMPT = ResourceManager
159: .getMessage(ResourceManager.REMEMBER_PASSWORD_PROMPT);
160:
161: //making it possible to turn off the rememberbox
162: //boolean showRemember=true;
163:
164: AuthenticatorPanel(String username, String password,
165: boolean rememberPassword) {
166: super ();
167: this .username = username;
168: this .password = password;
169: this .rememberPassword = rememberPassword;
170: initComponents();
171: }
172:
173: AuthenticatorPanel(String username, String password) {
174: this (username, password, true);
175: }
176:
177: // void setShowRemember(boolean showRemember)
178: // {
179: // this.showRemember=showRemember;
180: // }
181:
182: private void initComponents() {
183: JLabel usernameLbl = new JLabel(USERNAME_PROMPT);
184: JLabel passwordLbl = new JLabel(PASSWORD_PROMPT);
185:
186: usernameField = new JTextField(18);
187: if (username != null)
188: usernameField.setText(username);
189: passwordField = new JPasswordField(18);
190: if (password != null)
191: passwordField.setText(password);
192: rememberBox = new JCheckBox(REMEMBER_PASSWORD_PROMPT,
193: this .rememberPassword);
194:
195: setLayout(new GridBagLayout());
196: GridBagConstraints gbc = new GridBagConstraints();
197: gbc.gridx = 0;
198: gbc.gridy = 0;
199: gbc.gridwidth = 1;
200: gbc.gridheight = 1;
201: gbc.weightx = 0.1;
202: gbc.insets = new Insets(2, 2, 2, 2);
203: gbc.fill = gbc.NONE;
204: gbc.ipadx = 1;
205: gbc.ipady = 1;
206: add(usernameLbl, gbc);
207: gbc.gridx++;
208: gbc.weightx = 0.5;
209: add(usernameField, gbc);
210: gbc.weightx = 0;
211: gbc.gridx = 0;
212: gbc.gridy++;
213: add(passwordLbl, gbc);
214: gbc.gridx++;
215: add(passwordField, gbc);
216: gbc.gridx = 0;
217: gbc.gridy++;
218: gbc.gridwidth = 2;
219: gbc.anchor = GridBagConstraints.CENTER;
220: //if (showRemember)
221: add(rememberBox, gbc);
222:
223: }
224:
225: String getUsername() {
226: if (usernameField != null)
227: return usernameField.getText().trim();
228: return new String();
229: }
230:
231: String getPassword() {
232: if (passwordField != null)
233: return passwordField.getText().trim();
234: return new String();
235: }
236:
237: boolean getRememberPassword() {
238: if (rememberBox != null)
239: return rememberBox.isSelected();
240: return false;
241: }
242: }
243:
244: }
|