001: /*
002: * SSHTools - Java SSH2 API
003: *
004: * Copyright (C) 2002-2003 Lee David Painter and Contributors.
005: *
006: * Contributions made by:
007: *
008: * Brett Smith
009: * Richard Pernavas
010: * Erwin Bolwidt
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
025: */
026: package com.sshtools.common.authentication;
027:
028: import com.sshtools.common.ui.IconWrapperPanel;
029: import com.sshtools.common.ui.ResourceIcon;
030: import com.sshtools.common.ui.UIUtil;
031: import com.sshtools.common.ui.XTextField;
032:
033: import com.sshtools.j2ssh.authentication.AuthenticationProtocolException;
034: import com.sshtools.j2ssh.authentication.PasswordAuthenticationClient;
035: import com.sshtools.j2ssh.authentication.SshAuthenticationClient;
036: import com.sshtools.j2ssh.authentication.SshAuthenticationPrompt;
037:
038: import java.awt.BorderLayout;
039: import java.awt.Dialog;
040: import java.awt.FlowLayout;
041: import java.awt.Frame;
042: import java.awt.GridBagConstraints;
043: import java.awt.GridBagLayout;
044: import java.awt.GridLayout;
045: import java.awt.Insets;
046: import java.awt.Window;
047: import java.awt.event.ActionEvent;
048: import java.awt.event.WindowAdapter;
049: import java.awt.event.WindowEvent;
050:
051: import java.lang.reflect.Method;
052:
053: import javax.swing.BorderFactory;
054: import javax.swing.JButton;
055: import javax.swing.JDialog;
056: import javax.swing.JLabel;
057: import javax.swing.JOptionPane;
058: import javax.swing.JPanel;
059: import javax.swing.JPasswordField;
060: import javax.swing.SwingConstants;
061:
062: /**
063: *
064: *
065: * @author $author$
066: * @version $Revision: 1.16 $
067: */
068: public class PasswordAuthenticationDialog extends JDialog implements
069: SshAuthenticationPrompt {
070: // Statics
071: final static String KEY_ICON = "largecard.png";
072: PasswordAuthenticationClient instance;
073: JButton jButtonCancel = new JButton();
074: JButton jButtonOK = new JButton();
075: JPasswordField jPasswordField = new JPasswordField(20);
076: XTextField jTextUsername = new XTextField(20);
077: boolean userCancelled = false;
078:
079: /**
080: * Creates a new PasswordAuthenticationDialog object.
081: */
082: public PasswordAuthenticationDialog() {
083: super ((Frame) null, "Password Authentication", true);
084: init(null);
085: }
086:
087: /**
088: * Creates a new PasswordAuthenticationDialog object.
089: *
090: * @param parent
091: */
092: public PasswordAuthenticationDialog(Frame parent) {
093: super (parent, "Password Authentication", true);
094: init(parent);
095: }
096:
097: /**
098: * Creates a new PasswordAuthenticationDialog object.
099: *
100: * @param parent
101: */
102: public PasswordAuthenticationDialog(Dialog parent) {
103: super (parent, "Password Authentication", true);
104: init(parent);
105: }
106:
107: /**
108: *
109: *
110: * @return
111: */
112: public String getPassword() {
113: return String.valueOf(jPasswordField.getPassword());
114: }
115:
116: /**
117: *
118: *
119: * @return
120: */
121: public String getUsername() {
122: return jTextUsername.getText();
123: }
124:
125: /**
126: *
127: *
128: * @param username
129: */
130: public void setUsername(String username) {
131: if (username != null) {
132: jTextUsername.setText(username);
133: }
134: }
135:
136: /**
137: *
138: *
139: * @param instance
140: *
141: * @throws AuthenticationProtocolException
142: */
143: public void setInstance(SshAuthenticationClient instance)
144: throws AuthenticationProtocolException {
145: if (instance instanceof PasswordAuthenticationClient) {
146: this .instance = (PasswordAuthenticationClient) instance;
147: } else {
148: throw new AuthenticationProtocolException(
149: "PasswordAuthenticationClient instance required");
150: }
151: }
152:
153: /**
154: *
155: *
156: * @return
157: */
158: public boolean showPrompt(SshAuthenticationClient inst)
159: throws AuthenticationProtocolException {
160: if (inst instanceof PasswordAuthenticationClient) {
161: instance = (PasswordAuthenticationClient) inst;
162:
163: if (instance.getUsername() != null) {
164: jTextUsername.setText(instance.getUsername());
165: }
166:
167: if (!jTextUsername.getText().equals("")) {
168: jPasswordField.grabFocus();
169: }
170:
171: UIUtil.positionComponent(SwingConstants.CENTER, this );
172: setVisible(true);
173:
174: if (!userCancelled) {
175: instance.setUsername(getUsername());
176: instance.setPassword(getPassword());
177:
178: return true;
179: } else {
180: return false;
181: }
182: } else {
183: throw new AuthenticationProtocolException(
184: "PasswordAuthenticationClient instance required");
185: }
186: }
187:
188: void init(Window parent) {
189: setModal(true);
190:
191: getContentPane().setLayout(new GridLayout(1, 1));
192:
193: if (parent != null) {
194: try {
195: Method m = getClass().getMethod(
196: "setLocationRelativeTo",
197: new Class[] { parent.getClass() });
198: m.invoke(this , new Object[] { parent });
199: } catch (Throwable t) {
200: //
201: }
202: }
203:
204: try {
205: jbInit();
206: pack();
207: } catch (Exception ex) {
208: ex.printStackTrace();
209: }
210: }
211:
212: void jButtonCancel_actionPerformed(ActionEvent e) {
213: userCancelled = true;
214: setVisible(false);
215: }
216:
217: void jButtonOK_actionPerformed(ActionEvent e) {
218: if (jTextUsername.getText().trim().equals("")) {
219: JOptionPane.showMessageDialog(this ,
220: "You must enter a username!",
221: "Password Authentication", JOptionPane.OK_OPTION);
222:
223: return;
224: }
225:
226: setVisible(false);
227: }
228:
229: void jbInit() throws Exception {
230: // Add a window listener to see when the window closes without
231: // selecting OK
232: addWindowListener(new WindowAdapter() {
233: public void windowClosing(WindowEvent evt) {
234: userCancelled = true;
235: }
236: });
237:
238: // Ok button
239: jButtonOK
240: .addActionListener(new java.awt.event.ActionListener() {
241: public void actionPerformed(ActionEvent e) {
242: jButtonOK_actionPerformed(e);
243: }
244: });
245: jButtonOK.setText("OK");
246: jButtonOK.setMnemonic('o');
247: getRootPane().setDefaultButton(jButtonOK);
248:
249: // Cancel button
250: jButtonCancel.setText("Cancel");
251: jButtonCancel.setMnemonic('c');
252: jButtonCancel
253: .addActionListener(new java.awt.event.ActionListener() {
254: public void actionPerformed(ActionEvent e) {
255: jButtonCancel_actionPerformed(e);
256: }
257: });
258:
259: // User / password panel
260: JPanel userPasswordPanel = new JPanel(new GridBagLayout());
261: GridBagConstraints gbc = new GridBagConstraints();
262: gbc.fill = GridBagConstraints.HORIZONTAL;
263: gbc.anchor = GridBagConstraints.WEST;
264: gbc.insets = new Insets(0, 2, 2, 2);
265: gbc.weightx = 1.0;
266:
267: // Username
268: UIUtil.jGridBagAdd(userPasswordPanel, new JLabel("User"), gbc,
269: GridBagConstraints.REMAINDER);
270: gbc.fill = GridBagConstraints.HORIZONTAL;
271: UIUtil.jGridBagAdd(userPasswordPanel, jTextUsername, gbc,
272: GridBagConstraints.REMAINDER);
273: gbc.fill = GridBagConstraints.NONE;
274:
275: // Username
276: UIUtil.jGridBagAdd(userPasswordPanel, new JLabel("Password"),
277: gbc, GridBagConstraints.REMAINDER);
278: gbc.fill = GridBagConstraints.HORIZONTAL;
279: UIUtil.jGridBagAdd(userPasswordPanel, jPasswordField, gbc,
280: GridBagConstraints.REMAINDER);
281: gbc.fill = GridBagConstraints.NONE;
282:
283: // Create the center banner panel
284: IconWrapperPanel centerPanel = new IconWrapperPanel(
285: new ResourceIcon(PasswordAuthenticationDialog.class,
286: KEY_ICON), userPasswordPanel);
287: centerPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4,
288: 4));
289:
290: //
291: JPanel buttonPanel = new JPanel(new GridBagLayout());
292: gbc = new GridBagConstraints();
293: gbc.fill = GridBagConstraints.HORIZONTAL;
294: gbc.anchor = GridBagConstraints.CENTER;
295: gbc.insets = new Insets(6, 6, 0, 0);
296: gbc.weighty = 1.0;
297: UIUtil.jGridBagAdd(buttonPanel, jButtonOK, gbc,
298: GridBagConstraints.RELATIVE);
299: UIUtil.jGridBagAdd(buttonPanel, jButtonCancel, gbc,
300: GridBagConstraints.REMAINDER);
301:
302: JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT,
303: 0, 0));
304: southPanel.add(buttonPanel);
305:
306: // Wrap the whole thing in an empty border
307: JPanel mainPanel = new JPanel(new BorderLayout());
308: mainPanel
309: .setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
310: mainPanel.add(centerPanel, BorderLayout.CENTER);
311: mainPanel.add(southPanel, BorderLayout.SOUTH);
312:
313: // Build the main panel
314: getContentPane().add(mainPanel);
315:
316: //
317: jPasswordField.grabFocus();
318: }
319: }
|