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.ui;
027:
028: import com.sshtools.common.configuration.SshToolsConnectionProfile;
029:
030: import com.sshtools.j2ssh.authentication.SshAuthenticationClient;
031: import com.sshtools.j2ssh.authentication.SshAuthenticationClientFactory;
032: import com.sshtools.j2ssh.transport.AlgorithmNotSupportedException;
033:
034: import org.apache.commons.logging.Log;
035: import org.apache.commons.logging.LogFactory;
036:
037: import java.awt.Component;
038: import java.awt.GridBagConstraints;
039: import java.awt.GridBagLayout;
040: import java.awt.Insets;
041:
042: import java.util.ArrayList;
043: import java.util.HashMap;
044: import java.util.Iterator;
045: import java.util.Map;
046:
047: import javax.swing.BorderFactory;
048: import javax.swing.Icon;
049: import javax.swing.JCheckBox;
050: import javax.swing.JLabel;
051: import javax.swing.JList;
052: import javax.swing.JOptionPane;
053: import javax.swing.JPanel;
054: import javax.swing.JScrollPane;
055: import javax.swing.ListModel;
056:
057: /**
058: *
059: *
060: * @author $author$
061: * @version $Revision: 1.19 $
062: */
063: public class SshToolsConnectionHostTab extends JPanel implements
064: SshToolsConnectionTab {
065: //
066:
067: /** */
068: public final static int DEFAULT_PORT = 22;
069:
070: //
071:
072: /** */
073: public final static String CONNECT_ICON = "largeserveridentity.png";
074:
075: /** */
076: public final static String AUTH_ICON = "largelock.png";
077:
078: /** */
079: public final static String SHOW_AVAILABLE = "<Show available methods>";
080:
081: //
082:
083: /** */
084: protected XTextField jTextHostname = new XTextField();
085:
086: /** */
087: protected NumericTextField jTextPort = new NumericTextField(
088: new Integer(0), new Integer(65535), new Integer(
089: DEFAULT_PORT));
090:
091: /** */
092: protected XTextField jTextUsername = new XTextField();
093:
094: /** */
095: protected JList jListAuths = new JList();
096:
097: /** */
098: protected java.util.List methods = new ArrayList();
099:
100: /** */
101: protected SshToolsConnectionProfile profile;
102:
103: /** */
104: protected JCheckBox allowAgentForwarding;
105:
106: /** */
107: protected Log log = LogFactory
108: .getLog(SshToolsConnectionHostTab.class);
109:
110: /**
111: * Creates a new SshToolsConnectionHostTab object.
112: */
113: public SshToolsConnectionHostTab() {
114: super ();
115:
116: // Create the main connection details panel
117: JPanel mainConnectionDetailsPanel = new JPanel(
118: new GridBagLayout());
119: GridBagConstraints gbc = new GridBagConstraints();
120: gbc.fill = GridBagConstraints.HORIZONTAL;
121: gbc.anchor = GridBagConstraints.NORTHWEST;
122: gbc.insets = new Insets(0, 2, 2, 2);
123: gbc.weightx = 1.0;
124:
125: // Host name
126: UIUtil.jGridBagAdd(mainConnectionDetailsPanel, new JLabel(
127: "Hostname"), gbc, GridBagConstraints.REMAINDER);
128: gbc.fill = GridBagConstraints.HORIZONTAL;
129: UIUtil.jGridBagAdd(mainConnectionDetailsPanel, jTextHostname,
130: gbc, GridBagConstraints.REMAINDER);
131: gbc.fill = GridBagConstraints.NONE;
132:
133: // Port
134: UIUtil.jGridBagAdd(mainConnectionDetailsPanel, new JLabel(
135: "Port"), gbc, GridBagConstraints.REMAINDER);
136: UIUtil.jGridBagAdd(mainConnectionDetailsPanel, jTextPort, gbc,
137: GridBagConstraints.REMAINDER);
138:
139: // Username
140: UIUtil.jGridBagAdd(mainConnectionDetailsPanel, new JLabel(
141: "Username"), gbc, GridBagConstraints.REMAINDER);
142: gbc.fill = GridBagConstraints.HORIZONTAL;
143: gbc.weighty = 1.0;
144: UIUtil.jGridBagAdd(mainConnectionDetailsPanel, jTextUsername,
145: gbc, GridBagConstraints.REMAINDER);
146: gbc.fill = GridBagConstraints.NONE;
147:
148: //
149: IconWrapperPanel iconMainConnectionDetailsPanel = new IconWrapperPanel(
150: new ResourceIcon(SshToolsConnectionHostTab.class,
151: CONNECT_ICON), mainConnectionDetailsPanel);
152:
153: // Authentication methods panel
154: JPanel authMethodsPanel = new JPanel(new GridBagLayout());
155: authMethodsPanel.setBorder(BorderFactory.createEmptyBorder(4,
156: 0, 0, 0));
157: gbc = new GridBagConstraints();
158: gbc.fill = GridBagConstraints.NONE;
159: gbc.anchor = GridBagConstraints.NORTHWEST;
160: gbc.insets = new Insets(2, 2, 2, 2);
161: gbc.weightx = 1.0;
162:
163: // Authentication methods
164: UIUtil.jGridBagAdd(authMethodsPanel, new JLabel(
165: "Authentication Methods"), gbc,
166: GridBagConstraints.REMAINDER);
167: gbc.fill = GridBagConstraints.HORIZONTAL;
168: gbc.weighty = 1.0;
169: jListAuths.setVisibleRowCount(5);
170: UIUtil.jGridBagAdd(authMethodsPanel,
171: new JScrollPane(jListAuths), gbc,
172: GridBagConstraints.REMAINDER);
173: allowAgentForwarding = new JCheckBox("Allow agent forwarding");
174: UIUtil.jGridBagAdd(authMethodsPanel, allowAgentForwarding, gbc,
175: GridBagConstraints.REMAINDER);
176:
177: //
178: IconWrapperPanel iconAuthMethodsPanel = new IconWrapperPanel(
179: new ResourceIcon(SshToolsConnectionHostTab.class,
180: AUTH_ICON), authMethodsPanel);
181:
182: // This panel
183: setLayout(new GridBagLayout());
184: setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
185: gbc = new GridBagConstraints();
186: gbc.fill = GridBagConstraints.BOTH;
187: gbc.anchor = GridBagConstraints.WEST;
188: gbc.insets = new Insets(2, 2, 2, 2);
189: gbc.weightx = 1.0;
190: UIUtil.jGridBagAdd(this , iconMainConnectionDetailsPanel, gbc,
191: GridBagConstraints.REMAINDER);
192: gbc.weighty = 1.0;
193: UIUtil.jGridBagAdd(this , iconAuthMethodsPanel, gbc,
194: GridBagConstraints.REMAINDER);
195:
196: // Set up the values in the various components
197: addAuthenticationMethods();
198: }
199:
200: /**
201: *
202: *
203: * @param profile
204: */
205: public void setConnectionProfile(SshToolsConnectionProfile profile) {
206: this .profile = profile;
207: jTextHostname.setText((profile == null) ? "" : profile
208: .getHost());
209: jTextUsername.setText((profile == null) ? "" : profile
210: .getUsername());
211: jTextPort.setValue(new Integer((profile == null) ? 22 : profile
212: .getPort()));
213:
214: if (System.getProperty("sshtools.agent") == null) {
215: allowAgentForwarding.setSelected(false);
216: allowAgentForwarding.setEnabled(false);
217: } else {
218: allowAgentForwarding.setEnabled(true);
219: allowAgentForwarding.setSelected((profile != null)
220: && profile.getAllowAgentForwarding());
221: }
222:
223: // Match the authentication methods
224: Map auths = (profile == null) ? new HashMap() : profile
225: .getAuthenticationMethods();
226: Iterator it = auths.entrySet().iterator();
227: Map.Entry entry;
228: String authmethod;
229: int[] selectionarray = new int[auths.values().size()];
230: int count = 0;
231: ListModel model = jListAuths.getModel();
232:
233: while (it.hasNext()) {
234: entry = (Map.Entry) it.next();
235: authmethod = (String) entry.getKey();
236:
237: for (int i = 0; i < model.getSize(); i++) {
238: if (model.getElementAt(i).equals(authmethod)) {
239: selectionarray[count++] = i;
240:
241: break;
242: }
243: }
244:
245: /*if (jListAuths.getNextMatch(authmethod, 0, Position.Bias.Forward) > -1) {
246: selectionarray[count] = jListAuths.getNextMatch(authmethod, 0,
247: Position.Bias.Forward);
248: count++;
249: }*/
250: jListAuths.clearSelection();
251: jListAuths.setSelectedIndices(selectionarray);
252: }
253: }
254:
255: /**
256: *
257: *
258: * @return
259: */
260: public SshToolsConnectionProfile getConnectionProfile() {
261: return profile;
262: }
263:
264: private void addAuthenticationMethods() {
265: java.util.List methods = new ArrayList();
266: methods.add(SHOW_AVAILABLE);
267: methods.addAll(SshAuthenticationClientFactory
268: .getSupportedMethods());
269: jListAuths.setListData(methods.toArray());
270: jListAuths.setSelectedIndex(0);
271: }
272:
273: /**
274: *
275: *
276: * @return
277: */
278: public String getTabContext() {
279: return "Connection";
280: }
281:
282: /**
283: *
284: *
285: * @return
286: */
287: public Icon getTabIcon() {
288: return null;
289: }
290:
291: /**
292: *
293: *
294: * @return
295: */
296: public String getTabTitle() {
297: return "Host";
298: }
299:
300: /**
301: *
302: *
303: * @return
304: */
305: public String getTabToolTipText() {
306: return "The main host connection details.";
307: }
308:
309: /**
310: *
311: *
312: * @return
313: */
314: public int getTabMnemonic() {
315: return 'h';
316: }
317:
318: /**
319: *
320: *
321: * @return
322: */
323: public Component getTabComponent() {
324: return this ;
325: }
326:
327: /**
328: *
329: *
330: * @return
331: */
332: public boolean validateTab() {
333: // Validate that we have enough information
334: if (jTextHostname.getText().equals("")
335: || jTextPort.getText().equals("")
336: || jTextUsername.getText().equals("")) {
337: JOptionPane.showMessageDialog(this ,
338: "Please enter all details!", "Connect",
339: JOptionPane.OK_OPTION);
340:
341: return false;
342: }
343:
344: // Setup the authentications selected
345: java.util.List chosen = getChosenAuth();
346:
347: if (chosen != null) {
348: Iterator it = chosen.iterator();
349:
350: while (it.hasNext()) {
351: String method = (String) it.next();
352:
353: try {
354: SshAuthenticationClient auth = SshAuthenticationClientFactory
355: .newInstance(method);
356: } catch (AlgorithmNotSupportedException anse) {
357: JOptionPane.showMessageDialog(this , method
358: + " is not supported!");
359:
360: return false;
361: }
362: }
363: }
364:
365: return true;
366: }
367:
368: private java.util.List getChosenAuth() {
369: // Determine whether any authenticaiton methods we selected
370: Object[] auths = jListAuths.getSelectedValues();
371: String a;
372: java.util.List l = new java.util.ArrayList();
373:
374: if (auths != null) {
375: for (int i = 0; i < auths.length; i++) {
376: a = (String) auths[i];
377:
378: if (a.equals(SHOW_AVAILABLE)) {
379: return null;
380: } else {
381: l.add(a);
382: }
383: }
384: } else {
385: return null;
386: }
387:
388: return l;
389: }
390:
391: /**
392: *
393: */
394: public void applyTab() {
395: profile.setHost(jTextHostname.getText());
396: profile
397: .setPort(Integer.valueOf(jTextPort.getText())
398: .intValue());
399: profile.setUsername(jTextUsername.getText());
400: profile.setAllowAgentForwarding(allowAgentForwarding.getModel()
401: .isSelected());
402:
403: java.util.List chosen = getChosenAuth();
404:
405: // Remove the authentication methods and re-apply them
406: profile.removeAuthenticationMethods();
407:
408: if (chosen != null) {
409: Iterator it = chosen.iterator();
410:
411: while (it.hasNext()) {
412: String method = (String) it.next();
413:
414: try {
415: SshAuthenticationClient auth = SshAuthenticationClientFactory
416: .newInstance(method);
417: auth.setUsername(jTextUsername.getText());
418: profile.addAuthenticationMethod(auth);
419: } catch (AlgorithmNotSupportedException anse) {
420: log
421: .error(
422: "This should have been caught by validateTab()?",
423: anse);
424: }
425: }
426: }
427: }
428:
429: /**
430: *
431: */
432: public void tabSelected() {
433: }
434: }
|