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:
032: import java.awt.BorderLayout;
033: import java.awt.Color;
034: import java.awt.Dialog;
035: import java.awt.FlowLayout;
036: import java.awt.Frame;
037: import java.awt.GridBagConstraints;
038: import java.awt.GridBagLayout;
039: import java.awt.GridLayout;
040: import java.awt.Insets;
041: import java.awt.Window;
042: import java.awt.event.ActionEvent;
043: import java.awt.event.WindowAdapter;
044: import java.awt.event.WindowEvent;
045:
046: import javax.swing.BorderFactory;
047: import javax.swing.JButton;
048: import javax.swing.JDialog;
049: import javax.swing.JLabel;
050: import javax.swing.JPanel;
051: import javax.swing.JPasswordField;
052:
053: /**
054: *
055: *
056: * @author $author$
057: * @version $Revision: 1.15 $
058: */
059: public class PassphraseDialog extends JDialog {
060: // Statics
061: final static String PASSPHRASE_ICON = "/com/sshtools/common/authentication/largepassphrase.png";
062: JButton jButtonCancel = new JButton();
063: JButton jButtonOK = new JButton();
064: JLabel message = new JLabel("Enter passphrase");
065: JPasswordField jPasswordField = new JPasswordField(20);
066: boolean userCancelled = false;
067:
068: /**
069: * Creates a new PassphraseDialog object.
070: */
071: public PassphraseDialog() {
072: super ((Frame) null, "Passphrase", true);
073: init(null);
074: }
075:
076: /**
077: * Creates a new PassphraseDialog object.
078: *
079: * @param parent
080: */
081: public PassphraseDialog(Frame parent) {
082: super (parent, "Passphrase", true);
083: init(parent);
084: }
085:
086: /**
087: * Creates a new PassphraseDialog object.
088: *
089: * @param parent
090: * @param identity
091: */
092: public PassphraseDialog(Frame parent, String identity) {
093: super (parent, "Passphrase", true);
094: init(parent);
095: setTitle(identity + " - Identity");
096: }
097:
098: /**
099: * Creates a new PassphraseDialog object.
100: *
101: * @param parent
102: */
103: public PassphraseDialog(Dialog parent) {
104: super (parent, "Passphrase", true);
105: init(parent);
106: }
107:
108: /*public void setVisible(boolean visible) {
109: if (visible) {
110: UIUtil.positionComponent(UIUtil.CENTER, PassphraseDialog.this);
111: }
112: }*/
113:
114: /**
115: *
116: *
117: * @return
118: */
119: public boolean isCancelled() {
120: return userCancelled;
121: }
122:
123: /**
124: *
125: *
126: * @param message
127: */
128: public void setMessage(String message) {
129: this .message.setText(message);
130: }
131:
132: /**
133: *
134: *
135: * @param color
136: */
137: public void setMessageForeground(Color color) {
138: message.setForeground(color);
139: }
140:
141: /**
142: *
143: *
144: * @return
145: */
146: public char[] getPassphrase() {
147: return jPasswordField.getPassword();
148: }
149:
150: void init(Window parent) {
151: getContentPane().setLayout(new GridLayout(1, 1));
152:
153: if (parent != null) {
154: this .setLocationRelativeTo(parent);
155: }
156:
157: try {
158: jbInit();
159: pack();
160: UIUtil.positionComponent(UIUtil.CENTER,
161: 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
188: .addActionListener(new java.awt.event.ActionListener() {
189: public void actionPerformed(ActionEvent e) {
190: jButtonOK_actionPerformed(e);
191: }
192: });
193: jButtonOK.setText("OK");
194: jButtonOK.setMnemonic('o');
195: getRootPane().setDefaultButton(jButtonOK);
196:
197: // Cancel button
198: jButtonCancel.setText("Cancel");
199: jButtonCancel.setMnemonic('c');
200: jButtonCancel
201: .addActionListener(new java.awt.event.ActionListener() {
202: public void actionPerformed(ActionEvent e) {
203: jButtonCancel_actionPerformed(e);
204: }
205: });
206:
207: // Passphrase panel
208: JPanel passphrasePanel = new JPanel(new GridBagLayout());
209: GridBagConstraints gbc = new GridBagConstraints();
210: gbc.fill = GridBagConstraints.HORIZONTAL;
211: gbc.anchor = GridBagConstraints.WEST;
212: gbc.insets = new Insets(0, 2, 2, 2);
213: gbc.weightx = 1.0;
214: UIUtil.jGridBagAdd(passphrasePanel, message, gbc,
215: GridBagConstraints.REMAINDER);
216: UIUtil.jGridBagAdd(passphrasePanel, jPasswordField, gbc,
217: GridBagConstraints.REMAINDER);
218:
219: // Create the center banner panel
220: IconWrapperPanel centerPanel = new IconWrapperPanel(
221: new ResourceIcon(PASSPHRASE_ICON), passphrasePanel);
222: centerPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4,
223: 4));
224:
225: //
226: JPanel buttonPanel = new JPanel(new GridBagLayout());
227: gbc = new GridBagConstraints();
228: gbc.fill = GridBagConstraints.HORIZONTAL;
229: gbc.anchor = GridBagConstraints.CENTER;
230: gbc.insets = new Insets(6, 6, 0, 0);
231: gbc.weighty = 1.0;
232: UIUtil.jGridBagAdd(buttonPanel, jButtonOK, gbc,
233: GridBagConstraints.RELATIVE);
234: UIUtil.jGridBagAdd(buttonPanel, jButtonCancel, gbc,
235: GridBagConstraints.REMAINDER);
236:
237: JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT,
238: 0, 0));
239: southPanel.add(buttonPanel);
240:
241: // Wrap the whole thing in an empty border
242: JPanel mainPanel = new JPanel(new BorderLayout());
243: mainPanel
244: .setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
245: mainPanel.add(centerPanel, BorderLayout.CENTER);
246: mainPanel.add(southPanel, BorderLayout.SOUTH);
247:
248: // Build the main panel
249: getContentPane().add(mainPanel);
250:
251: //
252: jPasswordField.grabFocus();
253: }
254: }
|