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.ui;
27
28 import com.sshtools.common.configuration.SshToolsConnectionProfile;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33 import java.awt.BorderLayout;
34 import java.awt.Component;
35 import java.awt.FlowLayout;
36 import java.awt.GridBagConstraints;
37 import java.awt.GridBagLayout;
38 import java.awt.GridLayout;
39 import java.awt.Insets;
40 import java.awt.Window;
41 import java.awt.event.ActionEvent;
42 import java.awt.event.ActionListener;
43
44 import javax.swing.BorderFactory;
45 import javax.swing.JButton;
46 import javax.swing.JDialog;
47 import javax.swing.JFrame;
48 import javax.swing.JPanel;
49 import javax.swing.SwingConstants;
50 import javax.swing.SwingUtilities;
51
52
53 /**
54 *
55 *
56 * @author $author$
57 * @version $Revision: 1.14 $
58 */
59 public class SshToolsConnectionPanel extends JPanel {
60 // Strings
61 final static String DEFAULT = "<Default>";
62 final static int DEFAULT_PORT = 22;
63
64 //
65
66 /** */
67 protected Log log = LogFactory.getLog(SshToolsConnectionPanel.class);
68
69 //
70 private Tabber tabber;
71 private SshToolsConnectionProfile profile;
72
73 /**
74 * Creates a new SshToolsConnectionPanel object.
75 *
76 * @param showConnectionTabs
77 */
78 public SshToolsConnectionPanel(boolean showConnectionTabs) {
79 super();
80 tabber = new Tabber();
81
82 if (showConnectionTabs) {
83 // Add the common tabs
84 addTab(new SshToolsConnectionHostTab());
85 addTab(new SshToolsConnectionProtocolTab());
86 addTab(new SshToolsConnectionProxyTab());
87 }
88
89 // Build this panel
90 setLayout(new GridLayout(1, 1));
91 add(tabber);
92 }
93
94 /**
95 *
96 *
97 * @return
98 */
99 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)).setConnectionProfile(profile);
129 }
130 }
131
132 /**
133 *
134 *
135 * @param parent
136 * @param optionalTabs
137 *
138 * @return
139 */
140 public static SshToolsConnectionProfile showConnectionDialog(
141 Component parent, SshToolsConnectionTab[] optionalTabs) {
142 return showConnectionDialog(parent, null, optionalTabs);
143 }
144
145 /**
146 *
147 *
148 * @param parent
149 * @param profile
150 * @param optionalTabs
151 *
152 * @return
153 */
154 public static SshToolsConnectionProfile showConnectionDialog(
155 Component parent, SshToolsConnectionProfile profile,
156 SshToolsConnectionTab[] optionalTabs) {
157 // If no properties are provided, then use the default
158 if (profile == null) {
159 profile = new SshToolsConnectionProfile();
160 profile.setHost(PreferencesStore.get(
161 SshToolsApplication.PREF_CONNECTION_LAST_HOST, ""));
162 profile.setPort(PreferencesStore.getInt(
163 SshToolsApplication.PREF_CONNECTION_LAST_PORT, DEFAULT_PORT));
164 profile.setUsername(PreferencesStore.get(
165 SshToolsApplication.PREF_CONNECTION_LAST_USER, ""));
166 }
167
168 final SshToolsConnectionPanel conx = new SshToolsConnectionPanel(true);
169
170 if (optionalTabs != null) {
171 for (int i = 0; i < optionalTabs.length; i++) {
172 conx.addTab(optionalTabs[i]);
173 }
174 }
175
176 conx.setConnectionProfile(profile);
177
178 JDialog d = null;
179 Window w = (Window) SwingUtilities.getAncestorOfClass(Window.class,
180 parent);
181
182 if (w instanceof JDialog) {
183 d = new JDialog((JDialog) w, "Connection Profile", true);
184 } else if (w instanceof JFrame) {
185 d = new JDialog((JFrame) w, "Connection Profile", true);
186 } else {
187 d = new JDialog((JFrame) null, "Connection Profile", true);
188 }
189
190 final JDialog dialog = d;
191
192 class UserAction {
193 boolean connect;
194 }
195
196 final UserAction userAction = new UserAction();
197
198 // Create the bottom button panel
199 final JButton cancel = new JButton("Cancel");
200 cancel.setMnemonic('c');
201 cancel.addActionListener(new ActionListener() {
202 public void actionPerformed(ActionEvent evt) {
203 dialog.setVisible(false);
204 }
205 });
206
207 final JButton connect = new JButton("Connect");
208 connect.setMnemonic('t');
209 connect.addActionListener(new ActionListener() {
210 public void actionPerformed(ActionEvent evt) {
211 if (conx.validateTabs()) {
212 userAction.connect = true;
213 dialog.setVisible(false);
214 }
215 }
216 });
217 dialog.getRootPane().setDefaultButton(connect);
218
219 JPanel buttonPanel = new JPanel(new GridBagLayout());
220 GridBagConstraints gbc = new GridBagConstraints();
221 gbc.fill = GridBagConstraints.HORIZONTAL;
222 gbc.anchor = GridBagConstraints.CENTER;
223 gbc.insets = new Insets(6, 6, 0, 0);
224 gbc.weighty = 1.0;
225 UIUtil.jGridBagAdd(buttonPanel, connect, gbc,
226 GridBagConstraints.RELATIVE);
227 UIUtil.jGridBagAdd(buttonPanel, cancel, gbc,
228 GridBagConstraints.REMAINDER);
229
230 JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT, 0, 0));
231 southPanel.add(buttonPanel);
232
233 //
234 JPanel mainPanel = new JPanel(new BorderLayout());
235 mainPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
236 mainPanel.add(conx, BorderLayout.CENTER);
237 mainPanel.add(southPanel, BorderLayout.SOUTH);
238
239 // Show the dialog
240 dialog.getContentPane().setLayout(new GridLayout(1, 1));
241 dialog.getContentPane().add(mainPanel);
242 dialog.pack();
243 dialog.setResizable(false);
244 UIUtil.positionComponent(SwingConstants.CENTER, dialog);
245 dialog.setVisible(true);
246
247 if (!userAction.connect) {
248 return null;
249 }
250
251 conx.applyTabs();
252
253 // Make sure we didn't cancel
254 PreferencesStore.put(SshToolsApplication.PREF_CONNECTION_LAST_HOST,
255 profile.getHost());
256 PreferencesStore.put(SshToolsApplication.PREF_CONNECTION_LAST_USER,
257 profile.getUsername());
258 PreferencesStore.putInt(SshToolsApplication.PREF_CONNECTION_LAST_PORT,
259 profile.getPort());
260
261 // Return the connection properties
262 return profile;
263 }
264 }