1 /*
2 * SSHTools - Java SSH2 API
3 *
4 * Copyright (C) 2002-2003 Lee David Painter and Contributors.
5 *
6 * Contributions made by:
7 *
8 * Brett Smith
9 * Richard Pernavas
10 * Erwin Bolwidt
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 */
26 package com.sshtools.common.authentication;
27
28 import com.sshtools.common.ui.IconWrapperPanel;
29 import com.sshtools.common.ui.ResourceIcon;
30 import com.sshtools.common.ui.UIUtil;
31
32 import java.awt.BorderLayout;
33 import java.awt.Color;
34 import java.awt.Dialog;
35 import java.awt.FlowLayout;
36 import java.awt.Frame;
37 import java.awt.GridBagConstraints;
38 import java.awt.GridBagLayout;
39 import java.awt.GridLayout;
40 import java.awt.Insets;
41 import java.awt.Window;
42 import java.awt.event.ActionEvent;
43 import java.awt.event.WindowAdapter;
44 import java.awt.event.WindowEvent;
45
46 import javax.swing.BorderFactory;
47 import javax.swing.JButton;
48 import javax.swing.JDialog;
49 import javax.swing.JLabel;
50 import javax.swing.JPanel;
51 import javax.swing.JPasswordField;
52
53
54 /**
55 *
56 *
57 * @author $author$
58 * @version $Revision: 1.15 $
59 */
60 public class PassphraseDialog extends JDialog {
61 // Statics
62 final static String PASSPHRASE_ICON = "/com/sshtools/common/authentication/largepassphrase.png";
63 JButton jButtonCancel = new JButton();
64 JButton jButtonOK = new JButton();
65 JLabel message = new JLabel("Enter passphrase");
66 JPasswordField jPasswordField = new JPasswordField(20);
67 boolean userCancelled = false;
68
69 /**
70 * Creates a new PassphraseDialog object.
71 */
72 public PassphraseDialog() {
73 super((Frame) null, "Passphrase", true);
74 init(null);
75 }
76
77 /**
78 * Creates a new PassphraseDialog object.
79 *
80 * @param parent
81 */
82 public PassphraseDialog(Frame parent) {
83 super(parent, "Passphrase", true);
84 init(parent);
85 }
86
87 /**
88 * Creates a new PassphraseDialog object.
89 *
90 * @param parent
91 * @param identity
92 */
93 public PassphraseDialog(Frame parent, String identity) {
94 super(parent, "Passphrase", true);
95 init(parent);
96 setTitle(identity + " - Identity");
97 }
98
99 /**
100 * Creates a new PassphraseDialog object.
101 *
102 * @param parent
103 */
104 public PassphraseDialog(Dialog parent) {
105 super(parent, "Passphrase", true);
106 init(parent);
107 }
108
109 /*public void setVisible(boolean visible) {
110 if (visible) {
111 UIUtil.positionComponent(UIUtil.CENTER, PassphraseDialog.this);
112 }
113 }*/
114
115 /**
116 *
117 *
118 * @return
119 */
120 public boolean isCancelled() {
121 return userCancelled;
122 }
123
124 /**
125 *
126 *
127 * @param message
128 */
129 public void setMessage(String message) {
130 this.message.setText(message);
131 }
132
133 /**
134 *
135 *
136 * @param color
137 */
138 public void setMessageForeground(Color color) {
139 message.setForeground(color);
140 }
141
142 /**
143 *
144 *
145 * @return
146 */
147 public char[] getPassphrase() {
148 return jPasswordField.getPassword();
149 }
150
151 void init(Window parent) {
152 getContentPane().setLayout(new GridLayout(1, 1));
153
154 if (parent != null) {
155 this.setLocationRelativeTo(parent);
156 }
157
158 try {
159 jbInit();
160 pack();
161 UIUtil.positionComponent(UIUtil.CENTER, PassphraseDialog.this);
162 } catch (Exception ex) {
163 ex.printStackTrace();
164 }
165 }
166
167 void jButtonCancel_actionPerformed(ActionEvent e) {
168 userCancelled = true;
169 setVisible(false);
170 }
171
172 void jButtonOK_actionPerformed(ActionEvent e) {
173 userCancelled = false;
174 setVisible(false);
175 }
176
177 void jbInit() throws Exception {
178 // Add a window listener to see when the window closes without
179 // selecting OK
180 addWindowListener(new WindowAdapter() {
181 public void windowClosing(WindowEvent evt) {
182 userCancelled = true;
183 }
184 });
185
186 // Ok button
187 jButtonOK.addActionListener(new java.awt.event.ActionListener() {
188 public void actionPerformed(ActionEvent e) {
189 jButtonOK_actionPerformed(e);
190 }
191 });
192 jButtonOK.setText("OK");
193 jButtonOK.setMnemonic('o');
194 getRootPane().setDefaultButton(jButtonOK);
195
196 // Cancel button
197 jButtonCancel.setText("Cancel");
198 jButtonCancel.setMnemonic('c');
199 jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
200 public void actionPerformed(ActionEvent e) {
201 jButtonCancel_actionPerformed(e);
202 }
203 });
204
205 // Passphrase panel
206 JPanel passphrasePanel = new JPanel(new GridBagLayout());
207 GridBagConstraints gbc = new GridBagConstraints();
208 gbc.fill = GridBagConstraints.HORIZONTAL;
209 gbc.anchor = GridBagConstraints.WEST;
210 gbc.insets = new Insets(0, 2, 2, 2);
211 gbc.weightx = 1.0;
212 UIUtil.jGridBagAdd(passphrasePanel, message, gbc,
213 GridBagConstraints.REMAINDER);
214 UIUtil.jGridBagAdd(passphrasePanel, jPasswordField, gbc,
215 GridBagConstraints.REMAINDER);
216
217 // Create the center banner panel
218 IconWrapperPanel centerPanel = new IconWrapperPanel(new ResourceIcon(
219 PASSPHRASE_ICON), passphrasePanel);
220 centerPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
221
222 //
223 JPanel buttonPanel = new JPanel(new GridBagLayout());
224 gbc = new GridBagConstraints();
225 gbc.fill = GridBagConstraints.HORIZONTAL;
226 gbc.anchor = GridBagConstraints.CENTER;
227 gbc.insets = new Insets(6, 6, 0, 0);
228 gbc.weighty = 1.0;
229 UIUtil.jGridBagAdd(buttonPanel, jButtonOK, gbc,
230 GridBagConstraints.RELATIVE);
231 UIUtil.jGridBagAdd(buttonPanel, jButtonCancel, gbc,
232 GridBagConstraints.REMAINDER);
233
234 JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 0, 0));
235 southPanel.add(buttonPanel);
236
237 // Wrap the whole thing in an empty border
238 JPanel mainPanel = new JPanel(new BorderLayout());
239 mainPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
240 mainPanel.add(centerPanel, BorderLayout.CENTER);
241 mainPanel.add(southPanel, BorderLayout.SOUTH);
242
243 // Build the main panel
244 getContentPane().add(mainPanel);
245
246 //
247 jPasswordField.grabFocus();
248 }
249 }