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.SshToolsConnectionHostTab;
031: import com.sshtools.common.ui.UIUtil;
032:
033: import com.sshtools.j2ssh.authentication.PasswordChangePrompt;
034:
035: import java.awt.BorderLayout;
036: import java.awt.Component;
037: import java.awt.Dialog;
038: import java.awt.FlowLayout;
039: import java.awt.Frame;
040: import java.awt.GridBagConstraints;
041: import java.awt.GridBagLayout;
042: import java.awt.GridLayout;
043: import java.awt.Insets;
044: import java.awt.Window;
045: import java.awt.event.ActionEvent;
046: import java.awt.event.ActionListener;
047:
048: import javax.swing.BorderFactory;
049: import javax.swing.JButton;
050: import javax.swing.JDialog;
051: import javax.swing.JLabel;
052: import javax.swing.JOptionPane;
053: import javax.swing.JPanel;
054: import javax.swing.JPasswordField;
055: import javax.swing.SwingConstants;
056: import javax.swing.SwingUtilities;
057:
058: /**
059: *
060: *
061: * @author $author$
062: * @version $Revision: 1.15 $
063: */
064: public class PasswordChange implements PasswordChangePrompt {
065: //
066:
067: /** */
068: public final static String PASSWORD_ICON = "/com/sshtools/common/authentication/largepassword.png";
069:
070: //
071: private static PasswordChange instance;
072:
073: //
074: private Component parent;
075:
076: private PasswordChange() {
077: }
078:
079: /**
080: *
081: *
082: * @param parent
083: */
084: public void setParentComponent(Component parent) {
085: this .parent = parent;
086: }
087:
088: /**
089: *
090: *
091: * @param prompt
092: *
093: * @return
094: */
095: public String changePassword(String prompt) {
096: Window w = (parent == null) ? null : (Window) SwingUtilities
097: .getAncestorOfClass(Window.class, parent);
098: PasswordChangeDialog dialog = null;
099:
100: if (w instanceof Frame) {
101: dialog = new PasswordChangeDialog((Frame) w, prompt);
102: } else if (w instanceof Dialog) {
103: dialog = new PasswordChangeDialog((Dialog) w, prompt);
104: } else {
105: dialog = new PasswordChangeDialog(prompt);
106: }
107:
108: char[] p = dialog.getPassword();
109:
110: return (p == null) ? null : new String(p);
111: }
112:
113: /**
114: *
115: *
116: * @return
117: */
118: public static PasswordChange getInstance() {
119: if (instance == null) {
120: instance = new PasswordChange();
121: }
122:
123: return instance;
124: }
125:
126: class PasswordChangeDialog extends JDialog {
127: JLabel promptLabel = new JLabel();
128: JPasswordField password = new JPasswordField(15);
129: JPasswordField confirm = new JPasswordField(15);
130: boolean cancelled;
131:
132: PasswordChangeDialog(String prompt) {
133: super ((Frame) null, "Password Change", true);
134: init(prompt);
135: }
136:
137: PasswordChangeDialog(Frame frame, String prompt) {
138: super (frame, "Password Change", true);
139: init(prompt);
140: }
141:
142: PasswordChangeDialog(Dialog dialog, String prompt) {
143: super (dialog, "Password Change", true);
144: init(prompt);
145: }
146:
147: char[] getPassword() {
148: return (cancelled == true) ? null : password.getPassword();
149: }
150:
151: void init(String prompt) {
152: setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
153:
154: JPanel g = new JPanel(new GridBagLayout());
155: GridBagConstraints gbc = new GridBagConstraints();
156: gbc.insets = new Insets(0, 0, 2, 2);
157: gbc.anchor = GridBagConstraints.WEST;
158: gbc.fill = GridBagConstraints.HORIZONTAL;
159: gbc.weightx = 0.0;
160: UIUtil.jGridBagAdd(g, new JLabel("Password: "), gbc,
161: GridBagConstraints.RELATIVE);
162: gbc.weightx = 1.0;
163: UIUtil.jGridBagAdd(g, password, gbc,
164: GridBagConstraints.REMAINDER);
165: gbc.weightx = 0.0;
166: UIUtil.jGridBagAdd(g, new JLabel("Confirm: "), gbc,
167: GridBagConstraints.RELATIVE);
168: gbc.weightx = 1.0;
169: UIUtil.jGridBagAdd(g, confirm, gbc,
170: GridBagConstraints.REMAINDER);
171:
172: //
173: promptLabel.setHorizontalAlignment(JLabel.CENTER);
174:
175: // Main panel
176: JPanel centerPanel = new JPanel(new BorderLayout());
177: centerPanel.setBorder(BorderFactory.createEmptyBorder(4, 4,
178: 4, 4));
179: centerPanel.add(promptLabel, BorderLayout.NORTH);
180: centerPanel.add(g, BorderLayout.CENTER);
181:
182: // Create the bottom button panel
183: JButton ok = new JButton("Ok");
184: ok.setMnemonic('o');
185: ok.setDefaultCapable(true);
186: ok.addActionListener(new ActionListener() {
187: public void actionPerformed(ActionEvent evt) {
188: if (!new String(password.getPassword())
189: .equals(new String(confirm.getPassword()))) {
190: JOptionPane
191: .showMessageDialog(
192: PasswordChangeDialog.this ,
193: "Passwords do not match. Please try again.",
194: "Passwords do not match",
195: JOptionPane.ERROR_MESSAGE);
196: } else {
197: setVisible(false);
198: }
199: }
200: });
201: getRootPane().setDefaultButton(ok);
202:
203: JButton cancel = new JButton("Cancel");
204: cancel.setMnemonic('c');
205: cancel.addActionListener(new ActionListener() {
206: public void actionPerformed(ActionEvent evt) {
207: cancelled = true;
208: setVisible(false);
209: }
210: });
211:
212: JPanel southPanel = new JPanel(new FlowLayout(
213: FlowLayout.RIGHT, 0, 0));
214: southPanel.setBorder(BorderFactory.createEmptyBorder(4, 0,
215: 0, 0));
216: southPanel.add(cancel);
217: southPanel.add(ok);
218:
219: // Create the center banner panel
220: IconWrapperPanel iconPanel = new IconWrapperPanel(
221: new ResourceIcon(
222: SshToolsConnectionHostTab.AUTH_ICON),
223: centerPanel);
224: iconPanel.setBorder(BorderFactory.createEmptyBorder(4, 4,
225: 4, 4));
226:
227: // The main panel contains everything and is surrounded by a border
228: JPanel mainPanel = new JPanel(new BorderLayout());
229: mainPanel.setBorder(BorderFactory.createEmptyBorder(4, 4,
230: 4, 4));
231: mainPanel.add(iconPanel, BorderLayout.CENTER);
232: mainPanel.add(southPanel, BorderLayout.SOUTH);
233:
234: // Build the main panel
235: getContentPane().setLayout(new GridLayout(1, 1));
236: getContentPane().add(mainPanel);
237: pack();
238: toFront();
239: UIUtil.positionComponent(SwingConstants.CENTER, this );
240: setVisible(true);
241: }
242: }
243: }
|