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 java.awt.BorderLayout;
034: import java.awt.Color;
035: import java.awt.Component;
036: import java.awt.Dialog;
037: import java.awt.FlowLayout;
038: import java.awt.Frame;
039: import java.awt.GridLayout;
040: import java.awt.Window;
041: import java.awt.event.ActionEvent;
042: import java.awt.event.ActionListener;
043:
044: import java.util.ArrayList;
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.JList;
051: import javax.swing.JPanel;
052: import javax.swing.JScrollPane;
053: import javax.swing.SwingConstants;
054: import javax.swing.SwingUtilities;
055:
056: /**
057: *
058: *
059: * @author $author$
060: * @version $Revision: 1.14 $
061: */
062: public class AuthenticationDialog extends JDialog {
063: JList jListAuths = new JList();
064: JLabel messageLabel = new JLabel();
065: boolean cancelled = false;
066:
067: /**
068: * Creates a new AuthenticationDialog object.
069: */
070: public AuthenticationDialog() {
071: super ((Frame) null, "Select Authentication Method(s)", true);
072: init();
073: }
074:
075: /**
076: * Creates a new AuthenticationDialog object.
077: *
078: * @param frame
079: */
080: public AuthenticationDialog(Frame frame) {
081: super (frame, "Select Authentication Method(s)", true);
082: init();
083: }
084:
085: /**
086: * Creates a new AuthenticationDialog object.
087: *
088: * @param dialog
089: */
090: public AuthenticationDialog(Dialog dialog) {
091: super (dialog, "Select Authentication Method(s)", true);
092: init();
093: }
094:
095: void init() {
096: try {
097: jbInit();
098: pack();
099: } catch (Exception ex) {
100: ex.printStackTrace();
101: }
102: }
103:
104: private void setMethodList(java.util.List methods) {
105: jListAuths.setListData(methods.toArray());
106:
107: if (methods.size() > 0) {
108: jListAuths.setSelectedIndex(0);
109: }
110: }
111:
112: void jbInit() throws Exception {
113: //
114: setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
115:
116: //
117: messageLabel.setForeground(Color.red);
118: messageLabel.setHorizontalAlignment(JLabel.CENTER);
119:
120: // Create the list of available methods and put in in a scroll panel
121: jListAuths = new JList();
122: jListAuths.setVisibleRowCount(5);
123:
124: JPanel listPanel = new JPanel(new GridLayout(1, 1));
125: listPanel
126: .setBorder(BorderFactory.createEmptyBorder(4, 0, 0, 0));
127: listPanel.add(new JScrollPane(jListAuths));
128:
129: // Main panel
130: JPanel centerPanel = new JPanel(new BorderLayout());
131: centerPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4,
132: 4));
133: centerPanel
134: .add(
135: new JLabel(
136: "Please select an authentication method(s) to continue."),
137: BorderLayout.NORTH);
138: centerPanel.add(listPanel, BorderLayout.CENTER);
139:
140: // Create the bottom button panel
141: JButton proceed = new JButton("Proceed");
142: proceed.setMnemonic('p');
143: proceed.setDefaultCapable(true);
144: proceed.addActionListener(new ActionListener() {
145: public void actionPerformed(ActionEvent evt) {
146: // I presume this component **is** reused?
147: setVisible(false);
148: }
149: });
150: getRootPane().setDefaultButton(proceed);
151:
152: JButton cancel = new JButton("Cancel");
153: cancel.setMnemonic('c');
154: cancel.addActionListener(new ActionListener() {
155: public void actionPerformed(ActionEvent evt) {
156: cancelled = true;
157: setVisible(false);
158: }
159: });
160:
161: JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT,
162: 0, 0));
163: southPanel.setBorder(BorderFactory
164: .createEmptyBorder(4, 0, 0, 0));
165: southPanel.add(cancel);
166: southPanel.add(proceed);
167:
168: // Create the center banner panel
169: IconWrapperPanel iconPanel = new IconWrapperPanel(
170: new ResourceIcon(SshToolsConnectionHostTab.AUTH_ICON),
171: centerPanel);
172: iconPanel
173: .setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
174:
175: // The main panel contains everything and is surrounded by a border
176: JPanel mainPanel = new JPanel(new BorderLayout());
177: mainPanel
178: .setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
179: mainPanel.add(messageLabel, BorderLayout.NORTH);
180: mainPanel.add(iconPanel, BorderLayout.CENTER);
181: mainPanel.add(southPanel, BorderLayout.SOUTH);
182:
183: // Build the main panel
184: getContentPane().setLayout(new GridLayout(1, 1));
185: getContentPane().add(mainPanel);
186: }
187:
188: /**
189: *
190: *
191: * @param parent
192: * @param support
193: *
194: * @return
195: */
196: public static java.util.List showAuthenticationDialog(
197: Component parent, java.util.List support) {
198: return showAuthenticationDialog(parent, support, null);
199: }
200:
201: /**
202: *
203: *
204: * @param parent
205: * @param support
206: * @param message
207: *
208: * @return
209: */
210: public static java.util.List showAuthenticationDialog(
211: Component parent, java.util.List support, String message) {
212: Window w = (Window) SwingUtilities.getAncestorOfClass(
213: Window.class, parent);
214: AuthenticationDialog dialog = null;
215:
216: if (w instanceof Frame) {
217: dialog = new AuthenticationDialog((Frame) w);
218: } else if (w instanceof Dialog) {
219: dialog = new AuthenticationDialog((Dialog) w);
220: } else {
221: dialog = new AuthenticationDialog();
222: }
223:
224: UIUtil.positionComponent(SwingConstants.CENTER, dialog);
225:
226: return dialog.showAuthenticationMethods(support, message);
227: }
228:
229: /**
230: *
231: *
232: * @param supported
233: * @param message
234: *
235: * @return
236: */
237: public java.util.List showAuthenticationMethods(
238: java.util.List supported, String message) {
239: // Set the list
240: this .setMethodList(supported);
241:
242: // Show the dialog
243: UIUtil.positionComponent(SwingConstants.CENTER, this );
244:
245: if (message != null) {
246: messageLabel.setVisible(true);
247: messageLabel.setText(message);
248: } else {
249: messageLabel.setVisible(false);
250: }
251:
252: pack();
253: toFront();
254: setVisible(true);
255:
256: // Put the selected values into a new list and return
257: java.util.List list = new ArrayList();
258:
259: if (!cancelled) {
260: Object[] methods = jListAuths.getSelectedValues();
261:
262: if (methods != null) {
263: for (int i = 0; i < methods.length; i++) {
264: list.add(methods[i]);
265: }
266: }
267: }
268:
269: return list;
270: }
271:
272: void jButtonProceed_actionPerformed(ActionEvent e) {
273: setVisible(false);
274: }
275: }
|