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 org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032:
033: import java.awt.BorderLayout;
034: import java.awt.Color;
035: import java.awt.Component;
036: import java.awt.GridBagConstraints;
037: import java.awt.GridBagLayout;
038: import java.awt.Insets;
039:
040: import javax.swing.BorderFactory;
041: import javax.swing.ButtonGroup;
042: import javax.swing.Icon;
043: import javax.swing.JLabel;
044: import javax.swing.JPanel;
045: import javax.swing.JPasswordField;
046: import javax.swing.JRadioButton;
047: import javax.swing.JTextField;
048: import javax.swing.event.ChangeEvent;
049: import javax.swing.event.ChangeListener;
050:
051: /**
052: *
053: *
054: * @author $author$
055: * @version $Revision: 1.15 $
056: */
057: public class SshToolsConnectionProxyTab extends JPanel implements
058: SshToolsConnectionTab {
059: /** */
060: public final static String PROXY_ICON = "/com/sshtools/common/ui/proxy.png";
061:
062: /** */
063: protected JRadioButton noProxy = new JRadioButton("No proxy");
064:
065: /** */
066: protected JRadioButton httpProxy = new JRadioButton("HTTP");
067:
068: /** */
069: protected JRadioButton socks4Proxy = new JRadioButton("SOCKS 4");
070:
071: /** */
072: protected JRadioButton socks5Proxy = new JRadioButton("SOCKS 5");
073:
074: /** */
075: protected ButtonGroup group = new ButtonGroup();
076:
077: /** */
078: protected JPanel proxyframe = new JPanel(new GridBagLayout());
079:
080: /** */
081: protected JTextField username = new JTextField();
082:
083: /** */
084: protected JPasswordField password = new JPasswordField();
085:
086: /** */
087: protected JTextField proxy = new JTextField();
088:
089: /** */
090: protected NumericTextField port = new NumericTextField(new Integer(
091: 1), new Integer(65535));
092:
093: /** */
094: protected SshToolsConnectionProfile profile;
095:
096: /** */
097: protected Log log = LogFactory
098: .getLog(SshToolsConnectionProxyTab.class);
099:
100: /**
101: * Creates a new SshToolsConnectionProxyTab object.
102: */
103: public SshToolsConnectionProxyTab() {
104: super ();
105: group.add(noProxy);
106: group.add(httpProxy);
107: group.add(socks4Proxy);
108: group.add(socks5Proxy);
109:
110: ChangeListener listener = new ChangeListener() {
111: public void stateChanged(ChangeEvent e) {
112: if (noProxy.isSelected()) {
113: username.setEnabled(false);
114: password.setEnabled(false);
115: proxy.setEnabled(false);
116:
117: //port.setEnabled(false);
118: port.setForeground(Color.white);
119: } else {
120: username.setEnabled(true);
121: password.setEnabled(true);
122: proxy.setEnabled(true);
123:
124: //port.setEnabled(true);
125: port.setForeground(Color.black);
126:
127: if (httpProxy.isSelected()) {
128: port.setText("80");
129: } else {
130: port.setText("1080");
131: }
132: }
133: }
134: };
135:
136: noProxy.getModel().addChangeListener(listener);
137: httpProxy.getModel().addChangeListener(listener);
138: socks4Proxy.getModel().addChangeListener(listener);
139: socks5Proxy.getModel().addChangeListener(listener);
140:
141: // Create the main connection details panel
142: GridBagConstraints gbc = new GridBagConstraints();
143: gbc.fill = GridBagConstraints.HORIZONTAL;
144: gbc.anchor = GridBagConstraints.NORTH;
145: gbc.insets = new Insets(0, 2, 2, 2);
146: gbc.weightx = 1.0;
147: proxyframe
148: .setBorder(BorderFactory
149: .createTitledBorder("Connect using the following proxy"));
150:
151: // No proxy label
152: gbc.insets = new Insets(2, 10, 2, 2);
153: UIUtil.jGridBagAdd(proxyframe, noProxy, gbc,
154: GridBagConstraints.RELATIVE);
155:
156: // Socks 4 label
157: gbc.insets = new Insets(2, 15, 2, 2);
158: UIUtil.jGridBagAdd(proxyframe, socks4Proxy, gbc,
159: GridBagConstraints.REMAINDER);
160:
161: //gbc.fill = GridBagConstraints.HORIZONTAL;
162: // Http Proxy
163: gbc.insets = new Insets(2, 10, 2, 2);
164: UIUtil.jGridBagAdd(proxyframe, httpProxy, gbc,
165: GridBagConstraints.RELATIVE);
166:
167: // Socks 5 label
168: gbc.insets = new Insets(2, 15, 2, 2);
169: UIUtil.jGridBagAdd(proxyframe, socks5Proxy, gbc,
170: GridBagConstraints.REMAINDER);
171: gbc.insets = new Insets(2, 10, 2, 10);
172:
173: JPanel connect = new JPanel(new GridBagLayout());
174: connect.setBorder(BorderFactory
175: .createTitledBorder("Proxy Details"));
176: UIUtil.jGridBagAdd(connect, new JLabel("Host"), gbc,
177: GridBagConstraints.REMAINDER);
178: UIUtil.jGridBagAdd(connect, proxy, gbc,
179: GridBagConstraints.REMAINDER);
180: UIUtil.jGridBagAdd(connect, new JLabel("Port"), gbc,
181: GridBagConstraints.REMAINDER);
182: gbc.anchor = GridBagConstraints.WEST;
183: gbc.fill = GridBagConstraints.NONE;
184: UIUtil.jGridBagAdd(connect, port, gbc,
185: GridBagConstraints.REMAINDER);
186: gbc.fill = GridBagConstraints.HORIZONTAL;
187: UIUtil.jGridBagAdd(connect, new JLabel("Username"), gbc,
188: GridBagConstraints.REMAINDER);
189: UIUtil.jGridBagAdd(connect, username, gbc,
190: GridBagConstraints.REMAINDER);
191: UIUtil.jGridBagAdd(connect, new JLabel("Password"), gbc,
192: GridBagConstraints.REMAINDER);
193: gbc.insets = new Insets(2, 10, 10, 10);
194: UIUtil.jGridBagAdd(connect, password, gbc,
195: GridBagConstraints.REMAINDER);
196:
197: JPanel main = new JPanel(new GridBagLayout());
198: gbc.insets = new Insets(2, 2, 2, 2);
199: UIUtil.jGridBagAdd(main, proxyframe, gbc,
200: GridBagConstraints.REMAINDER);
201: UIUtil.jGridBagAdd(main, connect, gbc,
202: GridBagConstraints.REMAINDER);
203:
204: IconWrapperPanel iconProxyDetailsPanel = new IconWrapperPanel(
205: new ResourceIcon(PROXY_ICON), main);
206: noProxy.setSelected(true);
207:
208: // This panel
209: setLayout(new BorderLayout());
210: setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
211: gbc = new GridBagConstraints();
212: gbc.fill = GridBagConstraints.HORIZONTAL;
213: gbc.anchor = GridBagConstraints.NORTH;
214: gbc.insets = new Insets(2, 2, 2, 2);
215: gbc.weightx = 1.0;
216: add(iconProxyDetailsPanel, BorderLayout.NORTH);
217: }
218:
219: /**
220: *
221: *
222: * @param profile
223: */
224: public void setConnectionProfile(SshToolsConnectionProfile profile) {
225: this .profile = profile;
226:
227: if (profile.getTransportProvider() == SshToolsConnectionProfile.USE_HTTP_PROXY) {
228: httpProxy.setSelected(true);
229: } else if (profile.getTransportProvider() == SshToolsConnectionProfile.USE_SOCKS4_PROXY) {
230: socks4Proxy.setSelected(true);
231: } else if (profile.getTransportProvider() == SshToolsConnectionProfile.USE_SOCKS5_PROXY) {
232: socks5Proxy.setSelected(true);
233: }
234:
235: proxy.setText(profile.getProxyHost());
236:
237: if (profile.getProxyPort() > 0) {
238: port.setValue(new Integer(profile.getProxyPort()));
239: }
240:
241: username.setText(profile.getProxyUsername());
242: password.setText(profile.getProxyPassword());
243: }
244:
245: /**
246: *
247: *
248: * @return
249: */
250: public SshToolsConnectionProfile getConnectionProfile() {
251: return profile;
252: }
253:
254: /**
255: *
256: *
257: * @return
258: */
259: public String getTabContext() {
260: return "Connection";
261: }
262:
263: /**
264: *
265: *
266: * @return
267: */
268: public Icon getTabIcon() {
269: return null;
270: }
271:
272: /**
273: *
274: *
275: * @return
276: */
277: public String getTabTitle() {
278: return "Proxy";
279: }
280:
281: /**
282: *
283: *
284: * @return
285: */
286: public String getTabToolTipText() {
287: return "Configure the proxy connection.";
288: }
289:
290: /**
291: *
292: *
293: * @return
294: */
295: public int getTabMnemonic() {
296: return 'p';
297: }
298:
299: /**
300: *
301: *
302: * @return
303: */
304: public Component getTabComponent() {
305: return this ;
306: }
307:
308: /**
309: *
310: *
311: * @return
312: */
313: public boolean validateTab() {
314: return true;
315: }
316:
317: /**
318: *
319: */
320: public void applyTab() {
321: if (httpProxy.isSelected()) {
322: profile
323: .setTransportProvider(SshToolsConnectionProfile.USE_HTTP_PROXY);
324: } else if (socks4Proxy.isSelected()) {
325: profile
326: .setTransportProvider(SshToolsConnectionProfile.USE_SOCKS4_PROXY);
327: } else if (socks5Proxy.isSelected()) {
328: profile
329: .setTransportProvider(SshToolsConnectionProfile.USE_SOCKS5_PROXY);
330: } else {
331: profile
332: .setTransportProvider(SshToolsConnectionProfile.USE_STANDARD_SOCKET);
333: }
334:
335: profile.setProxyHost(proxy.getText());
336: profile.setProxyPort(port.getValue().intValue());
337: profile.setProxyUsername(username.getText());
338: profile.setProxyPassword(new String(password.getPassword()));
339: }
340:
341: /**
342: *
343: */
344: public void tabSelected() {
345: }
346: }
|