001: package net.sourceforge.squirrel_sql.client.preferences;
002:
003: /*
004: * Copyright (C) 2002-2003 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import java.awt.Component;
022: import java.awt.GridBagConstraints;
023: import java.awt.GridBagLayout;
024: import java.awt.Insets;
025: import java.awt.event.ActionEvent;
026: import java.awt.event.ActionListener;
027:
028: import javax.swing.BorderFactory;
029: import javax.swing.JCheckBox;
030: import javax.swing.JLabel;
031: import javax.swing.JPanel;
032: import javax.swing.JPasswordField;
033: import javax.swing.JTextField;
034:
035: import net.sourceforge.squirrel_sql.client.IApplication;
036: import net.sourceforge.squirrel_sql.fw.gui.MultipleLineLabel;
037: import net.sourceforge.squirrel_sql.fw.util.ProxySettings;
038: import net.sourceforge.squirrel_sql.fw.util.StringManager;
039: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
040:
041: class ProxyPreferencesPanel implements IGlobalPreferencesPanel {
042: /** Internationalized strings for this class. */
043: private static final StringManager s_stringMgr = StringManagerFactory
044: .getStringManager(ProxyPreferencesPanel.class);
045:
046: /** Panel to be displayed in preferences dialog. */
047: private MyPanel _myPanel;
048:
049: /** Application API. */
050: private IApplication _app;
051:
052: /**
053: * Default ctor.
054: */
055: public ProxyPreferencesPanel() {
056: super ();
057: }
058:
059: public void initialize(IApplication app) {
060: if (app == null) {
061: throw new IllegalArgumentException(
062: "Null IApplication passed");
063: }
064:
065: _app = app;
066:
067: ((MyPanel) getPanelComponent()).loadData(_app, _app
068: .getSquirrelPreferences());
069: }
070:
071: public void uninitialize(IApplication app) {
072:
073: }
074:
075: public synchronized Component getPanelComponent() {
076: if (_myPanel == null) {
077: _myPanel = new MyPanel();
078: }
079: return _myPanel;
080: }
081:
082: public void applyChanges() {
083: _myPanel.applyChanges(_app.getSquirrelPreferences());
084: }
085:
086: public String getTitle() {
087: return s_stringMgr.getString("ProxyPreferencesPanel.title");
088: }
089:
090: public String getHint() {
091: return s_stringMgr.getString("ProxyPreferencesPanel.hint");
092: }
093:
094: private static final class MyPanel extends JPanel {
095: private JCheckBox _httpUseProxyChk = new JCheckBox(s_stringMgr
096: .getString("ProxyPreferencesPanel.useproxy"));
097: private JTextField _httpProxyServer = new JTextField();
098: private JTextField _httpProxyPort = new JTextField();
099: private JTextField _httpNonProxyHosts = new JTextField();
100: private JTextField _httpProxyUser = new JTextField();
101: private JPasswordField _httpProxyPassword = new JPasswordField();
102: private JCheckBox _socksUseProxyChk = new JCheckBox(s_stringMgr
103: .getString("ProxyPreferencesPanel.useproxy"));
104: private JTextField _socksProxyServer = new JTextField();
105: private JTextField _socksProxyPort = new JTextField();
106:
107: MyPanel() {
108: super (new GridBagLayout());
109: createUserInterface();
110: }
111:
112: void loadData(IApplication app, SquirrelPreferences prefs) {
113: final ProxySettings proxySettings = prefs
114: .getProxySettings();
115:
116: _httpUseProxyChk.setSelected(proxySettings
117: .getHttpUseProxy());
118: _httpProxyServer
119: .setText(proxySettings.getHttpProxyServer());
120: _httpProxyPort.setText(proxySettings.getHttpProxyPort());
121: _httpNonProxyHosts.setText(proxySettings
122: .getHttpNonProxyHosts());
123: _httpProxyUser.setText(proxySettings.getHttpProxyUser());
124: _httpProxyPassword.setText(proxySettings
125: .getHttpProxyPassword());
126:
127: _socksUseProxyChk.setSelected(proxySettings
128: .getSocksUseProxy());
129: _socksProxyServer.setText(proxySettings
130: .getSocksProxyServer());
131: _socksProxyPort.setText(proxySettings.getSocksProxyPort());
132:
133: updateControlStatus();
134: }
135:
136: void applyChanges(SquirrelPreferences prefs) {
137: final ProxySettings proxySettings = new ProxySettings();
138:
139: proxySettings
140: .setHttpUseProxy(_httpUseProxyChk.isSelected());
141: proxySettings
142: .setHttpProxyServer(_httpProxyServer.getText());
143: proxySettings.setHttpProxyPort(_httpProxyPort.getText());
144: proxySettings.setHttpNonProxyHosts(_httpNonProxyHosts
145: .getText());
146: proxySettings.setHttpProxyUser(_httpProxyUser.getText());
147:
148: String password = new String(_httpProxyPassword
149: .getPassword());
150: proxySettings.setHttpProxyPassword(password);
151:
152: proxySettings.setSocksUseProxy(_socksUseProxyChk
153: .isSelected());
154: proxySettings.setSocksProxyServer(_socksProxyServer
155: .getText());
156: proxySettings.setSocksProxyPort(_socksProxyPort.getText());
157:
158: prefs.setProxySettings(proxySettings);
159: }
160:
161: private void updateControlStatus() {
162: final boolean http = _httpUseProxyChk.isSelected();
163: _httpProxyServer.setEnabled(http);
164: _httpProxyPort.setEnabled(http);
165: _httpNonProxyHosts.setEnabled(http);
166: _httpProxyUser.setEnabled(http);
167: _httpProxyPassword.setEnabled(http);
168:
169: final boolean socks = _socksUseProxyChk.isSelected();
170: _socksProxyServer.setEnabled(socks);
171: _socksProxyPort.setEnabled(socks);
172: }
173:
174: private void createUserInterface() {
175: final GridBagConstraints gbc = new GridBagConstraints();
176: gbc.fill = GridBagConstraints.HORIZONTAL;
177: gbc.insets = new Insets(4, 4, 4, 4);
178: gbc.gridx = 0;
179: gbc.gridy = 0;
180: gbc.weightx = 1;
181: add(createHTTPPanel(), gbc);
182: ++gbc.gridy;
183: add(createSOCKSPanel(), gbc);
184:
185: final ActionListener lis = new MyActionHandler();
186: _httpUseProxyChk.addActionListener(lis);
187: _socksUseProxyChk.addActionListener(lis);
188: }
189:
190: private JPanel createHTTPPanel() {
191:
192: JPanel pnl = new JPanel(new GridBagLayout());
193: pnl.setBorder(BorderFactory.createTitledBorder(s_stringMgr
194: .getString("ProxyPreferencesPanel.httpproxy")));
195:
196: final GridBagConstraints gbc = new GridBagConstraints();
197: gbc.anchor = GridBagConstraints.WEST;
198: gbc.insets = new Insets(4, 4, 4, 4);
199: gbc.gridx = 0;
200: gbc.gridy = 0;
201: pnl.add(_httpUseProxyChk, gbc);
202:
203: gbc.fill = GridBagConstraints.HORIZONTAL;
204: ++gbc.gridx;
205: pnl.add(new JLabel(s_stringMgr
206: .getString("ProxyPreferencesPanel.server"),
207: JLabel.RIGHT), gbc);
208:
209: ++gbc.gridy;
210: pnl.add(new JLabel(s_stringMgr
211: .getString("ProxyPreferencesPanel.port"),
212: JLabel.RIGHT), gbc);
213:
214: ++gbc.gridy;
215: pnl.add(new JLabel(s_stringMgr
216: .getString("ProxyPreferencesPanel.user"),
217: JLabel.RIGHT), gbc);
218:
219: ++gbc.gridy;
220: pnl.add(new JLabel(s_stringMgr
221: .getString("ProxyPreferencesPanel.password"),
222: JLabel.RIGHT), gbc);
223:
224: ++gbc.gridy;
225: pnl.add(new JLabel(s_stringMgr
226: .getString("ProxyPreferencesPanel.noproxyfor"),
227: JLabel.RIGHT), gbc);
228:
229: ++gbc.gridy;
230: --gbc.gridx;
231: gbc.gridwidth = GridBagConstraints.REMAINDER;
232: pnl.add(new MultipleLineLabel(s_stringMgr
233: .getString("ProxyPreferencesPane.notes")), gbc);
234: gbc.gridwidth = 1;
235: ++gbc.gridx;
236:
237: ++gbc.gridx;
238: gbc.gridy = 0;
239: gbc.weightx = 1;
240: pnl.add(_httpProxyServer, gbc);
241:
242: ++gbc.gridy;
243: pnl.add(_httpProxyPort, gbc);
244:
245: ++gbc.gridy;
246: pnl.add(_httpProxyUser, gbc);
247:
248: ++gbc.gridy;
249: pnl.add(_httpProxyPassword, gbc);
250:
251: ++gbc.gridy;
252: pnl.add(_httpNonProxyHosts, gbc);
253:
254: return pnl;
255: }
256:
257: private JPanel createSOCKSPanel() {
258: JPanel pnl = new JPanel(new GridBagLayout());
259: pnl.setBorder(BorderFactory.createTitledBorder(s_stringMgr
260: .getString("ProxyPreferencesPanel.socksproxy")));
261:
262: final GridBagConstraints gbc = new GridBagConstraints();
263: gbc.anchor = GridBagConstraints.WEST;
264: gbc.insets = new Insets(4, 4, 4, 4);
265: gbc.gridx = 0;
266: gbc.gridy = 0;
267: pnl.add(_socksUseProxyChk, gbc);
268:
269: gbc.fill = GridBagConstraints.HORIZONTAL;
270: ++gbc.gridx;
271: pnl.add(new JLabel(s_stringMgr
272: .getString("ProxyPreferencesPanel.server"),
273: JLabel.RIGHT), gbc);
274:
275: ++gbc.gridy;
276: pnl.add(new JLabel(s_stringMgr
277: .getString("ProxyPreferencesPanel.port"),
278: JLabel.RIGHT), gbc);
279:
280: ++gbc.gridx;
281: gbc.gridy = 0;
282: gbc.weightx = 1;
283: pnl.add(_socksProxyServer, gbc);
284:
285: ++gbc.gridy;
286: pnl.add(_socksProxyPort, gbc);
287:
288: return pnl;
289: }
290:
291: private final class MyActionHandler implements ActionListener {
292: public void actionPerformed(ActionEvent evt) {
293: updateControlStatus();
294: }
295: }
296: }
297: }
|