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.KBIPrompt;
034: import com.sshtools.j2ssh.authentication.KBIRequestHandler;
035:
036: import java.awt.BorderLayout;
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.event.ActionEvent;
045: import java.awt.event.ActionListener;
046:
047: import javax.swing.BorderFactory;
048: import javax.swing.JButton;
049: import javax.swing.JDialog;
050: import javax.swing.JLabel;
051: import javax.swing.JPanel;
052: import javax.swing.JPasswordField;
053: import javax.swing.SwingConstants;
054: import javax.swing.text.JTextComponent;
055:
056: /**
057: *
058: *
059: * @author $author$
060: * @version $Revision: 1.15 $
061: */
062: public class KBIRequestHandlerDialog extends JDialog implements
063: KBIRequestHandler {
064: /** */
065: public final static String KBI_ICON = "largekbi.png";
066: boolean cancelled;
067: JLabel instructionLabel = new JLabel();
068: JPanel buttonsPanel = new JPanel();
069: JTextComponent[] promptReply;
070:
071: /**
072: * Creates a new KBIRequestHandlerDialog object.
073: */
074: public KBIRequestHandlerDialog() {
075: super ((Frame) null, "", true);
076: init();
077: }
078:
079: /**
080: * Creates a new KBIRequestHandlerDialog object.
081: *
082: * @param frame
083: */
084: public KBIRequestHandlerDialog(Frame frame) {
085: super (frame, "", true);
086: init();
087: }
088:
089: /**
090: * Creates a new KBIRequestHandlerDialog object.
091: *
092: * @param dialog
093: */
094: public KBIRequestHandlerDialog(Dialog dialog) {
095: super (dialog, "", true);
096: init();
097: }
098:
099: void init() {
100: setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
101: instructionLabel.setHorizontalAlignment(JLabel.CENTER);
102: instructionLabel.setBorder(BorderFactory.createEmptyBorder(4,
103: 4, 8, 4));
104:
105: JButton ok = new JButton("Ok");
106: ok.setMnemonic('o');
107: ok.setDefaultCapable(true);
108: ok.addActionListener(new ActionListener() {
109: public void actionPerformed(ActionEvent evt) {
110: setVisible(false);
111: }
112: });
113: getRootPane().setDefaultButton(ok);
114:
115: JButton cancel = new JButton("Cancel");
116: cancel.setMnemonic('c');
117: cancel.addActionListener(new ActionListener() {
118: public void actionPerformed(ActionEvent evt) {
119: cancelled = true;
120: setVisible(false);
121: }
122: });
123: buttonsPanel.setLayout(new FlowLayout(FlowLayout.RIGHT, 0, 0));
124: buttonsPanel.setBorder(BorderFactory.createEmptyBorder(4, 0, 0,
125: 0));
126: buttonsPanel.add(cancel);
127: buttonsPanel.add(ok);
128: }
129:
130: /**
131: *
132: *
133: * @param name
134: * @param instruction
135: * @param prompts
136: */
137: public void showPrompts(String name, String instruction,
138: KBIPrompt[] prompts) {
139: setTitle(name);
140: getContentPane().invalidate();
141: getContentPane().removeAll();
142: instructionLabel.setText(instruction);
143:
144: JPanel promptPanel = new JPanel(new GridBagLayout());
145: GridBagConstraints gbc = new GridBagConstraints();
146: gbc.insets = new Insets(0, 0, 4, 4);
147: gbc.fill = GridBagConstraints.CENTER;
148: gbc.anchor = GridBagConstraints.WEST;
149: promptReply = new JTextComponent[prompts.length];
150:
151: for (int i = 0; i < prompts.length; i++) {
152: if (prompts[i].echo()) {
153: promptReply[i] = new XTextField(prompts[i]
154: .getResponse(), 15);
155: } else {
156: promptReply[i] = new JPasswordField(prompts[i]
157: .getResponse(), 15);
158: }
159:
160: System.out.println("Creating prompt "
161: + prompts[i].getPrompt() + " and setting to "
162: + prompts[i].getResponse());
163: gbc.weightx = 0.0;
164: UIUtil.jGridBagAdd(promptPanel, new JLabel(prompts[i]
165: .getPrompt()
166: + " "), gbc, GridBagConstraints.RELATIVE);
167: gbc.weightx = 1.0;
168: UIUtil.jGridBagAdd(promptPanel, promptReply[i], gbc,
169: GridBagConstraints.REMAINDER);
170: }
171:
172: JPanel centerPanel = new JPanel(new BorderLayout());
173: centerPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4,
174: 4));
175: centerPanel.add(instructionLabel, BorderLayout.NORTH);
176: centerPanel.add(promptPanel, BorderLayout.CENTER);
177:
178: // Create the center banner panel
179: IconWrapperPanel iconPanel = new IconWrapperPanel(
180: new ResourceIcon(KBIRequestHandlerDialog.class,
181: KBI_ICON), centerPanel);
182: iconPanel
183: .setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
184:
185: // The main panel contains everything and is surrounded by a border
186: JPanel mainPanel = new JPanel(new BorderLayout());
187: mainPanel
188: .setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
189: mainPanel.add(iconPanel, BorderLayout.CENTER);
190: mainPanel.add(buttonsPanel, BorderLayout.SOUTH);
191:
192: // Build the main panel
193: getContentPane().setLayout(new GridLayout(1, 1));
194: getContentPane().add(mainPanel);
195: getContentPane().validate();
196: pack();
197: UIUtil.positionComponent(SwingConstants.CENTER, this );
198: setVisible(true);
199:
200: if (!cancelled) {
201: for (int i = 0; i < promptReply.length; i++) {
202: System.out.println("Setting reply " + i + " to "
203: + promptReply[i].getText());
204: prompts[i].setResponse(promptReply[i].getText());
205: }
206: }
207: }
208: }
|