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 import com.sshtools.common.ui.XTextField;
32
33 import com.sshtools.j2ssh.authentication.KBIPrompt;
34 import com.sshtools.j2ssh.authentication.KBIRequestHandler;
35
36 import java.awt.BorderLayout;
37 import java.awt.Dialog;
38 import java.awt.FlowLayout;
39 import java.awt.Frame;
40 import java.awt.GridBagConstraints;
41 import java.awt.GridBagLayout;
42 import java.awt.GridLayout;
43 import java.awt.Insets;
44 import java.awt.event.ActionEvent;
45 import java.awt.event.ActionListener;
46
47 import javax.swing.BorderFactory;
48 import javax.swing.JButton;
49 import javax.swing.JDialog;
50 import javax.swing.JLabel;
51 import javax.swing.JPanel;
52 import javax.swing.JPasswordField;
53 import javax.swing.SwingConstants;
54 import javax.swing.text.JTextComponent;
55
56
57 /**
58 *
59 *
60 * @author $author$
61 * @version $Revision: 1.15 $
62 */
63 public class KBIRequestHandlerDialog extends JDialog
64 implements KBIRequestHandler {
65 /** */
66 public final static String KBI_ICON = "largekbi.png";
67 boolean cancelled;
68 JLabel instructionLabel = new JLabel();
69 JPanel buttonsPanel = new JPanel();
70 JTextComponent[] promptReply;
71
72 /**
73 * Creates a new KBIRequestHandlerDialog object.
74 */
75 public KBIRequestHandlerDialog() {
76 super((Frame) null, "", true);
77 init();
78 }
79
80 /**
81 * Creates a new KBIRequestHandlerDialog object.
82 *
83 * @param frame
84 */
85 public KBIRequestHandlerDialog(Frame frame) {
86 super(frame, "", true);
87 init();
88 }
89
90 /**
91 * Creates a new KBIRequestHandlerDialog object.
92 *
93 * @param dialog
94 */
95 public KBIRequestHandlerDialog(Dialog dialog) {
96 super(dialog, "", true);
97 init();
98 }
99
100 void init() {
101 setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
102 instructionLabel.setHorizontalAlignment(JLabel.CENTER);
103 instructionLabel.setBorder(BorderFactory.createEmptyBorder(4, 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, 0));
125 buttonsPanel.add(cancel);
126 buttonsPanel.add(ok);
127 }
128
129 /**
130 *
131 *
132 * @param name
133 * @param instruction
134 * @param prompts
135 */
136 public void showPrompts(String name, String instruction, KBIPrompt[] prompts) {
137 setTitle(name);
138 getContentPane().invalidate();
139 getContentPane().removeAll();
140 instructionLabel.setText(instruction);
141
142 JPanel promptPanel = new JPanel(new GridBagLayout());
143 GridBagConstraints gbc = new GridBagConstraints();
144 gbc.insets = new Insets(0, 0, 4, 4);
145 gbc.fill = GridBagConstraints.CENTER;
146 gbc.anchor = GridBagConstraints.WEST;
147 promptReply = new JTextComponent[prompts.length];
148
149 for (int i = 0; i < prompts.length; i++) {
150 if (prompts[i].echo()) {
151 promptReply[i] = new XTextField(prompts[i].getResponse(), 15);
152 } else {
153 promptReply[i] = new JPasswordField(prompts[i].getResponse(), 15);
154 }
155
156 System.out.println("Creating prompt " + prompts[i].getPrompt() +
157 " and setting to " + prompts[i].getResponse());
158 gbc.weightx = 0.0;
159 UIUtil.jGridBagAdd(promptPanel,
160 new JLabel(prompts[i].getPrompt() + " "), gbc,
161 GridBagConstraints.RELATIVE);
162 gbc.weightx = 1.0;
163 UIUtil.jGridBagAdd(promptPanel, promptReply[i], gbc,
164 GridBagConstraints.REMAINDER);
165 }
166
167 JPanel centerPanel = new JPanel(new BorderLayout());
168 centerPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
169 centerPanel.add(instructionLabel, BorderLayout.NORTH);
170 centerPanel.add(promptPanel, BorderLayout.CENTER);
171
172 // Create the center banner panel
173 IconWrapperPanel iconPanel = new IconWrapperPanel(new ResourceIcon(
174 KBIRequestHandlerDialog.class, KBI_ICON), centerPanel);
175 iconPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
176
177 // The main panel contains everything and is surrounded by a border
178 JPanel mainPanel = new JPanel(new BorderLayout());
179 mainPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
180 mainPanel.add(iconPanel, BorderLayout.CENTER);
181 mainPanel.add(buttonsPanel, BorderLayout.SOUTH);
182
183 // Build the main panel
184 getContentPane().setLayout(new GridLayout(1, 1));
185 getContentPane().add(mainPanel);
186 getContentPane().validate();
187 pack();
188 UIUtil.positionComponent(SwingConstants.CENTER, this);
189 setVisible(true);
190
191 if (!cancelled) {
192 for (int i = 0; i < promptReply.length; i++) {
193 System.out.println("Setting reply " + i + " to " +
194 promptReply[i].getText());
195 prompts[i].setResponse(promptReply[i].getText());
196 }
197 }
198 }
199 }