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.Component;
035: import java.awt.FlowLayout;
036: import java.awt.GridBagConstraints;
037: import java.awt.GridBagLayout;
038: import java.awt.GridLayout;
039: import java.awt.Insets;
040: import java.awt.Window;
041: import java.awt.event.ActionEvent;
042: import java.awt.event.ActionListener;
043:
044: import javax.swing.BorderFactory;
045: import javax.swing.JButton;
046: import javax.swing.JDialog;
047: import javax.swing.JFrame;
048: import javax.swing.JPanel;
049: import javax.swing.SwingConstants;
050: import javax.swing.SwingUtilities;
051:
052: /**
053: *
054: *
055: * @author $author$
056: * @version $Revision: 1.14 $
057: */
058: public class SshToolsConnectionPanel extends JPanel {
059: // Strings
060: final static String DEFAULT = "<Default>";
061: final static int DEFAULT_PORT = 22;
062:
063: //
064:
065: /** */
066: protected Log log = LogFactory
067: .getLog(SshToolsConnectionPanel.class);
068:
069: //
070: private Tabber tabber;
071: private SshToolsConnectionProfile profile;
072:
073: /**
074: * Creates a new SshToolsConnectionPanel object.
075: *
076: * @param showConnectionTabs
077: */
078: public SshToolsConnectionPanel(boolean showConnectionTabs) {
079: super ();
080: tabber = new Tabber();
081:
082: if (showConnectionTabs) {
083: // Add the common tabs
084: addTab(new SshToolsConnectionHostTab());
085: addTab(new SshToolsConnectionProtocolTab());
086: addTab(new SshToolsConnectionProxyTab());
087: }
088:
089: // Build this panel
090: setLayout(new GridLayout(1, 1));
091: add(tabber);
092: }
093:
094: /**
095: *
096: *
097: * @return
098: */
099: public boolean validateTabs() {
100: return tabber.validateTabs();
101: }
102:
103: /**
104: *
105: */
106: public void applyTabs() {
107: tabber.applyTabs();
108: }
109:
110: /**
111: *
112: *
113: * @param tab
114: */
115: public void addTab(SshToolsConnectionTab tab) {
116: tabber.addTab(tab);
117: }
118:
119: /**
120: *
121: *
122: * @param profile
123: */
124: public void setConnectionProfile(SshToolsConnectionProfile profile) {
125: this .profile = profile;
126:
127: for (int i = 0; i < tabber.getTabCount(); i++) {
128: ((SshToolsConnectionTab) tabber.getTabAt(i))
129: .setConnectionProfile(profile);
130: }
131: }
132:
133: /**
134: *
135: *
136: * @param parent
137: * @param optionalTabs
138: *
139: * @return
140: */
141: public static SshToolsConnectionProfile showConnectionDialog(
142: Component parent, SshToolsConnectionTab[] optionalTabs) {
143: return showConnectionDialog(parent, null, optionalTabs);
144: }
145:
146: /**
147: *
148: *
149: * @param parent
150: * @param profile
151: * @param optionalTabs
152: *
153: * @return
154: */
155: public static SshToolsConnectionProfile showConnectionDialog(
156: Component parent, SshToolsConnectionProfile profile,
157: SshToolsConnectionTab[] optionalTabs) {
158: // If no properties are provided, then use the default
159: if (profile == null) {
160: profile = new SshToolsConnectionProfile();
161: profile.setHost(PreferencesStore.get(
162: SshToolsApplication.PREF_CONNECTION_LAST_HOST, ""));
163: profile.setPort(PreferencesStore.getInt(
164: SshToolsApplication.PREF_CONNECTION_LAST_PORT,
165: DEFAULT_PORT));
166: profile.setUsername(PreferencesStore.get(
167: SshToolsApplication.PREF_CONNECTION_LAST_USER, ""));
168: }
169:
170: final SshToolsConnectionPanel conx = new SshToolsConnectionPanel(
171: true);
172:
173: if (optionalTabs != null) {
174: for (int i = 0; i < optionalTabs.length; i++) {
175: conx.addTab(optionalTabs[i]);
176: }
177: }
178:
179: conx.setConnectionProfile(profile);
180:
181: JDialog d = null;
182: Window w = (Window) SwingUtilities.getAncestorOfClass(
183: Window.class, parent);
184:
185: if (w instanceof JDialog) {
186: d = new JDialog((JDialog) w, "Connection Profile", true);
187: } else if (w instanceof JFrame) {
188: d = new JDialog((JFrame) w, "Connection Profile", true);
189: } else {
190: d = new JDialog((JFrame) null, "Connection Profile", true);
191: }
192:
193: final JDialog dialog = d;
194:
195: class UserAction {
196: boolean connect;
197: }
198:
199: final UserAction userAction = new UserAction();
200:
201: // Create the bottom button panel
202: final JButton cancel = new JButton("Cancel");
203: cancel.setMnemonic('c');
204: cancel.addActionListener(new ActionListener() {
205: public void actionPerformed(ActionEvent evt) {
206: dialog.setVisible(false);
207: }
208: });
209:
210: final JButton connect = new JButton("Connect");
211: connect.setMnemonic('t');
212: connect.addActionListener(new ActionListener() {
213: public void actionPerformed(ActionEvent evt) {
214: if (conx.validateTabs()) {
215: userAction.connect = true;
216: dialog.setVisible(false);
217: }
218: }
219: });
220: dialog.getRootPane().setDefaultButton(connect);
221:
222: JPanel buttonPanel = new JPanel(new GridBagLayout());
223: GridBagConstraints gbc = new GridBagConstraints();
224: gbc.fill = GridBagConstraints.HORIZONTAL;
225: gbc.anchor = GridBagConstraints.CENTER;
226: gbc.insets = new Insets(6, 6, 0, 0);
227: gbc.weighty = 1.0;
228: UIUtil.jGridBagAdd(buttonPanel, connect, gbc,
229: GridBagConstraints.RELATIVE);
230: UIUtil.jGridBagAdd(buttonPanel, cancel, gbc,
231: GridBagConstraints.REMAINDER);
232:
233: JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT,
234: 0, 0));
235: southPanel.add(buttonPanel);
236:
237: //
238: JPanel mainPanel = new JPanel(new BorderLayout());
239: mainPanel
240: .setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
241: mainPanel.add(conx, BorderLayout.CENTER);
242: mainPanel.add(southPanel, BorderLayout.SOUTH);
243:
244: // Show the dialog
245: dialog.getContentPane().setLayout(new GridLayout(1, 1));
246: dialog.getContentPane().add(mainPanel);
247: dialog.pack();
248: dialog.setResizable(false);
249: UIUtil.positionComponent(SwingConstants.CENTER, dialog);
250: dialog.setVisible(true);
251:
252: if (!userAction.connect) {
253: return null;
254: }
255:
256: conx.applyTabs();
257:
258: // Make sure we didn't cancel
259: PreferencesStore.put(
260: SshToolsApplication.PREF_CONNECTION_LAST_HOST, profile
261: .getHost());
262: PreferencesStore.put(
263: SshToolsApplication.PREF_CONNECTION_LAST_USER, profile
264: .getUsername());
265: PreferencesStore.putInt(
266: SshToolsApplication.PREF_CONNECTION_LAST_PORT, profile
267: .getPort());
268:
269: // Return the connection properties
270: return profile;
271: }
272: }
|