001/* 002 * Copyright 2010-2015 Institut Pasteur. 003 * 004 * This file is part of Icy. 005 * 006 * Icy is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * Icy is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with Icy. If not, see <http://www.gnu.org/licenses/>. 018 */ 019package icy.gui.preferences; 020 021import icy.gui.component.IcyTextField; 022import icy.gui.component.IcyTextField.TextChangeListener; 023import icy.network.NetworkUtil; 024import icy.preferences.NetworkPreferences; 025 026import java.awt.GridBagConstraints; 027import java.awt.GridBagLayout; 028import java.awt.Insets; 029import java.awt.event.ActionEvent; 030import java.awt.event.ActionListener; 031 032import javax.swing.DefaultComboBoxModel; 033import javax.swing.JCheckBox; 034import javax.swing.JComboBox; 035import javax.swing.JLabel; 036import javax.swing.JPasswordField; 037import javax.swing.JSpinner; 038import javax.swing.SpinnerNumberModel; 039import javax.swing.event.ChangeEvent; 040import javax.swing.event.ChangeListener; 041import javax.swing.event.DocumentEvent; 042import javax.swing.event.DocumentListener; 043 044/** 045 * @author stephane 046 */ 047public class NetworkPreferencePanel extends PreferencePanel implements ActionListener, TextChangeListener, 048 ChangeListener, DocumentListener 049{ 050 /** 051 * 052 */ 053 private static final long serialVersionUID = -2311019090865779672L; 054 055 public static final String NODE_NAME = "Network"; 056 057 private JComboBox proxySettingComboBox; 058 private IcyTextField httpHostField; 059 private JSpinner httpPortField; 060 private IcyTextField httpsHostField; 061 private IcyTextField ftpHostField; 062 private JSpinner httpsPortField; 063 private JSpinner ftpPortField; 064 private IcyTextField socksHostField; 065 private JSpinner socksPortField; 066 private JCheckBox useAuthenticationChkBox; 067 private JLabel lblLogin; 068 private JLabel lblPassword; 069 private IcyTextField userField; 070 private JPasswordField passwordField; 071 072 public NetworkPreferencePanel(PreferenceFrame parent) 073 { 074 super(parent, NODE_NAME, PreferenceFrame.NODE_NAME); 075 076 initialize(); 077 078 validate(); 079 load(); 080 081 updateComponentsState(); 082 083 proxySettingComboBox.addActionListener(this); 084 httpHostField.addTextChangeListener(this); 085 httpPortField.addChangeListener(this); 086 httpsHostField.addTextChangeListener(this); 087 httpsPortField.addChangeListener(this); 088 ftpHostField.addTextChangeListener(this); 089 ftpPortField.addChangeListener(this); 090 socksHostField.addTextChangeListener(this); 091 socksPortField.addChangeListener(this); 092 093 userField.addTextChangeListener(this); 094 passwordField.getDocument().addDocumentListener(this); 095 096 useAuthenticationChkBox.addActionListener(this); 097 } 098 099 void initialize() 100 { 101 GridBagLayout gridBagLayout = new GridBagLayout(); 102 gridBagLayout.columnWidths = new int[] {69, 239, 97, 0, 0}; 103 gridBagLayout.rowHeights = new int[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; 104 gridBagLayout.columnWeights = new double[] {0.0, 1.0, 0.0, 1.0, Double.MIN_VALUE}; 105 gridBagLayout.rowWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE}; 106 mainPanel.setLayout(gridBagLayout); 107 108 JLabel lblProxy = new JLabel("Proxy"); 109 GridBagConstraints gbc_lblProxy = new GridBagConstraints(); 110 gbc_lblProxy.anchor = GridBagConstraints.EAST; 111 gbc_lblProxy.insets = new Insets(0, 0, 5, 5); 112 gbc_lblProxy.gridx = 0; 113 gbc_lblProxy.gridy = 0; 114 mainPanel.add(lblProxy, gbc_lblProxy); 115 116 proxySettingComboBox = new JComboBox(); 117 proxySettingComboBox.setModel(new DefaultComboBoxModel( 118 new String[] {"No proxy", "System proxy", "Manual proxy"})); 119 proxySettingComboBox.setToolTipText("Proxy setting"); 120 GridBagConstraints gbc_proxySettingComboBox = new GridBagConstraints(); 121 gbc_proxySettingComboBox.insets = new Insets(0, 0, 5, 5); 122 gbc_proxySettingComboBox.fill = GridBagConstraints.HORIZONTAL; 123 gbc_proxySettingComboBox.gridx = 1; 124 gbc_proxySettingComboBox.gridy = 0; 125 mainPanel.add(proxySettingComboBox, gbc_proxySettingComboBox); 126 127 JLabel lblNewLabel = new JLabel("HTTP"); 128 GridBagConstraints gbc_lblNewLabel = new GridBagConstraints(); 129 gbc_lblNewLabel.anchor = GridBagConstraints.EAST; 130 gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5); 131 gbc_lblNewLabel.gridx = 0; 132 gbc_lblNewLabel.gridy = 1; 133 mainPanel.add(lblNewLabel, gbc_lblNewLabel); 134 135 httpHostField = new IcyTextField(); 136 httpHostField.setToolTipText("HTTP proxy host"); 137 GridBagConstraints gbc_httpHostField = new GridBagConstraints(); 138 gbc_httpHostField.insets = new Insets(0, 0, 5, 5); 139 gbc_httpHostField.fill = GridBagConstraints.HORIZONTAL; 140 gbc_httpHostField.gridx = 1; 141 gbc_httpHostField.gridy = 1; 142 mainPanel.add(httpHostField, gbc_httpHostField); 143 httpHostField.setColumns(10); 144 145 httpPortField = new JSpinner(); 146 httpPortField.setModel(new SpinnerNumberModel(80, 0, 65535, 1)); 147 httpPortField.setToolTipText("HTTPS proxy port"); 148 GridBagConstraints gbc_httpPortField = new GridBagConstraints(); 149 gbc_httpPortField.fill = GridBagConstraints.HORIZONTAL; 150 gbc_httpPortField.insets = new Insets(0, 0, 5, 5); 151 gbc_httpPortField.gridx = 2; 152 gbc_httpPortField.gridy = 1; 153 mainPanel.add(httpPortField, gbc_httpPortField); 154 155 JLabel lblNewLabel_1 = new JLabel("HTTPS"); 156 GridBagConstraints gbc_lblNewLabel_1 = new GridBagConstraints(); 157 gbc_lblNewLabel_1.anchor = GridBagConstraints.EAST; 158 gbc_lblNewLabel_1.insets = new Insets(0, 0, 5, 5); 159 gbc_lblNewLabel_1.gridx = 0; 160 gbc_lblNewLabel_1.gridy = 2; 161 mainPanel.add(lblNewLabel_1, gbc_lblNewLabel_1); 162 163 httpsHostField = new IcyTextField(); 164 httpsHostField.setToolTipText("HTTPS proxy host"); 165 GridBagConstraints gbc_httpsHostField = new GridBagConstraints(); 166 gbc_httpsHostField.insets = new Insets(0, 0, 5, 5); 167 gbc_httpsHostField.fill = GridBagConstraints.HORIZONTAL; 168 gbc_httpsHostField.gridx = 1; 169 gbc_httpsHostField.gridy = 2; 170 mainPanel.add(httpsHostField, gbc_httpsHostField); 171 httpsHostField.setColumns(10); 172 173 httpsPortField = new JSpinner(); 174 httpsPortField.setModel(new SpinnerNumberModel(443, 0, 65535, 1)); 175 httpsPortField.setToolTipText("HTTPS proxy port"); 176 GridBagConstraints gbc_httpsPortField = new GridBagConstraints(); 177 gbc_httpsPortField.insets = new Insets(0, 0, 5, 5); 178 gbc_httpsPortField.fill = GridBagConstraints.HORIZONTAL; 179 gbc_httpsPortField.gridx = 2; 180 gbc_httpsPortField.gridy = 2; 181 mainPanel.add(httpsPortField, gbc_httpsPortField); 182 183 JLabel lblNewLabel_2 = new JLabel("FTP"); 184 GridBagConstraints gbc_lblNewLabel_2 = new GridBagConstraints(); 185 gbc_lblNewLabel_2.anchor = GridBagConstraints.EAST; 186 gbc_lblNewLabel_2.insets = new Insets(0, 0, 5, 5); 187 gbc_lblNewLabel_2.gridx = 0; 188 gbc_lblNewLabel_2.gridy = 3; 189 mainPanel.add(lblNewLabel_2, gbc_lblNewLabel_2); 190 191 ftpHostField = new IcyTextField(); 192 ftpHostField.setToolTipText("FTP proxy host"); 193 GridBagConstraints gbc_ftpHostField = new GridBagConstraints(); 194 gbc_ftpHostField.insets = new Insets(0, 0, 5, 5); 195 gbc_ftpHostField.fill = GridBagConstraints.HORIZONTAL; 196 gbc_ftpHostField.gridx = 1; 197 gbc_ftpHostField.gridy = 3; 198 mainPanel.add(ftpHostField, gbc_ftpHostField); 199 ftpHostField.setColumns(10); 200 201 ftpPortField = new JSpinner(); 202 ftpPortField.setModel(new SpinnerNumberModel(21, 0, 65535, 1)); 203 ftpPortField.setToolTipText("FTP proxy port"); 204 GridBagConstraints gbc_ftpPortField = new GridBagConstraints(); 205 gbc_ftpPortField.insets = new Insets(0, 0, 5, 5); 206 gbc_ftpPortField.fill = GridBagConstraints.HORIZONTAL; 207 gbc_ftpPortField.gridx = 2; 208 gbc_ftpPortField.gridy = 3; 209 mainPanel.add(ftpPortField, gbc_ftpPortField); 210 211 JLabel lblNewLabel_3 = new JLabel("SOCKS"); 212 GridBagConstraints gbc_lblNewLabel_3 = new GridBagConstraints(); 213 gbc_lblNewLabel_3.anchor = GridBagConstraints.EAST; 214 gbc_lblNewLabel_3.insets = new Insets(0, 0, 5, 5); 215 gbc_lblNewLabel_3.gridx = 0; 216 gbc_lblNewLabel_3.gridy = 4; 217 mainPanel.add(lblNewLabel_3, gbc_lblNewLabel_3); 218 219 socksHostField = new IcyTextField(); 220 socksHostField.setToolTipText("SOCKS host"); 221 GridBagConstraints gbc_socksHostField = new GridBagConstraints(); 222 gbc_socksHostField.insets = new Insets(0, 0, 5, 5); 223 gbc_socksHostField.fill = GridBagConstraints.HORIZONTAL; 224 gbc_socksHostField.gridx = 1; 225 gbc_socksHostField.gridy = 4; 226 mainPanel.add(socksHostField, gbc_socksHostField); 227 socksHostField.setColumns(10); 228 229 socksPortField = new JSpinner(); 230 socksPortField.setModel(new SpinnerNumberModel(1080, 0, 65535, 1)); 231 socksPortField.setToolTipText("SOCKS port"); 232 GridBagConstraints gbc_socksPortField = new GridBagConstraints(); 233 gbc_socksPortField.insets = new Insets(0, 0, 5, 5); 234 gbc_socksPortField.fill = GridBagConstraints.HORIZONTAL; 235 gbc_socksPortField.gridx = 2; 236 gbc_socksPortField.gridy = 4; 237 mainPanel.add(socksPortField, gbc_socksPortField); 238 239 useAuthenticationChkBox = new JCheckBox("Use authentication"); 240 GridBagConstraints gbc_chckbxUseAuthentication = new GridBagConstraints(); 241 gbc_chckbxUseAuthentication.anchor = GridBagConstraints.WEST; 242 gbc_chckbxUseAuthentication.insets = new Insets(0, 0, 5, 5); 243 gbc_chckbxUseAuthentication.gridx = 1; 244 gbc_chckbxUseAuthentication.gridy = 5; 245 mainPanel.add(useAuthenticationChkBox, gbc_chckbxUseAuthentication); 246 247 lblLogin = new JLabel("User"); 248 GridBagConstraints gbc_lblLogin = new GridBagConstraints(); 249 gbc_lblLogin.anchor = GridBagConstraints.EAST; 250 gbc_lblLogin.insets = new Insets(0, 0, 5, 5); 251 gbc_lblLogin.gridx = 0; 252 gbc_lblLogin.gridy = 6; 253 mainPanel.add(lblLogin, gbc_lblLogin); 254 255 userField = new IcyTextField(); 256 GridBagConstraints gbc_userField = new GridBagConstraints(); 257 gbc_userField.insets = new Insets(0, 0, 5, 5); 258 gbc_userField.fill = GridBagConstraints.HORIZONTAL; 259 gbc_userField.gridx = 1; 260 gbc_userField.gridy = 6; 261 mainPanel.add(userField, gbc_userField); 262 userField.setColumns(10); 263 264 lblPassword = new JLabel("Password"); 265 GridBagConstraints gbc_lblPassword = new GridBagConstraints(); 266 gbc_lblPassword.anchor = GridBagConstraints.EAST; 267 gbc_lblPassword.insets = new Insets(0, 0, 5, 5); 268 gbc_lblPassword.gridx = 0; 269 gbc_lblPassword.gridy = 7; 270 mainPanel.add(lblPassword, gbc_lblPassword); 271 272 passwordField = new JPasswordField(); 273 GridBagConstraints gbc_passwordField = new GridBagConstraints(); 274 gbc_passwordField.insets = new Insets(0, 0, 5, 5); 275 gbc_passwordField.fill = GridBagConstraints.HORIZONTAL; 276 gbc_passwordField.gridx = 1; 277 gbc_passwordField.gridy = 7; 278 mainPanel.add(passwordField, gbc_passwordField); 279 } 280 281 private void updateComponentsState() 282 { 283 final boolean enabled = proxySettingComboBox.getSelectedIndex() == 2; 284 285 httpHostField.setEnabled(enabled); 286 httpPortField.setEnabled(enabled); 287 httpsHostField.setEnabled(enabled); 288 httpsPortField.setEnabled(enabled); 289 ftpHostField.setEnabled(enabled); 290 ftpPortField.setEnabled(enabled); 291 socksHostField.setEnabled(enabled); 292 socksPortField.setEnabled(enabled); 293 useAuthenticationChkBox.setEnabled(enabled); 294 295 final boolean authEnabled = enabled && useAuthenticationChkBox.isSelected(); 296 297 userField.setEnabled(authEnabled); 298 passwordField.setEnabled(authEnabled); 299 } 300 301 @Override 302 public void actionPerformed(ActionEvent e) 303 { 304 final Object source = e.getSource(); 305 306 if ((source == proxySettingComboBox) || (source == useAuthenticationChkBox)) 307 updateComponentsState(); 308 309 // network setting changed, restart needed 310 getPreferenceFrame().setNeedRestart(); 311 } 312 313 @Override 314 public void textChanged(IcyTextField source, boolean validate) 315 { 316 // network setting changed, restart needed 317 if (validate) 318 getPreferenceFrame().setNeedRestart(); 319 } 320 321 @Override 322 public void stateChanged(ChangeEvent e) 323 { 324 // network setting changed, restart needed 325 getPreferenceFrame().setNeedRestart(); 326 } 327 328 @Override 329 protected void load() 330 { 331 proxySettingComboBox.setSelectedIndex(NetworkPreferences.getProxySetting()); 332 httpHostField.setText(NetworkPreferences.getProxyHTTPHost()); 333 httpPortField.setValue(Integer.valueOf(NetworkPreferences.getProxyHTTPPort())); 334 httpsHostField.setText(NetworkPreferences.getProxyHTTPSHost()); 335 httpsPortField.setValue(Integer.valueOf(NetworkPreferences.getProxyHTTPSPort())); 336 ftpHostField.setText(NetworkPreferences.getProxyFTPHost()); 337 ftpPortField.setValue(Integer.valueOf(NetworkPreferences.getProxyFTPPort())); 338 socksHostField.setText(NetworkPreferences.getProxySOCKSHost()); 339 socksPortField.setValue(Integer.valueOf(NetworkPreferences.getProxySOCKSPort())); 340 useAuthenticationChkBox.setSelected(NetworkPreferences.getProxyAuthentication()); 341 userField.setText(NetworkPreferences.getProxyUser()); 342 passwordField.setText(NetworkPreferences.getProxyPassword()); 343 } 344 345 @Override 346 protected void save() 347 { 348 NetworkPreferences.setProxySetting(proxySettingComboBox.getSelectedIndex()); 349 NetworkPreferences.setProxyHTTPHost(httpHostField.getText()); 350 NetworkPreferences.setProxyHTTPPort(((Integer) httpPortField.getValue()).intValue()); 351 NetworkPreferences.setProxyHTTPSHost(httpsHostField.getText()); 352 NetworkPreferences.setProxyHTTPSPort(((Integer) httpsPortField.getValue()).intValue()); 353 NetworkPreferences.setProxyFTPHost(ftpHostField.getText()); 354 NetworkPreferences.setProxyFTPPort(((Integer) ftpPortField.getValue()).intValue()); 355 NetworkPreferences.setProxySOCKSHost(socksHostField.getText()); 356 NetworkPreferences.setProxySOCKSPort(((Integer) socksPortField.getValue()).intValue()); 357 NetworkPreferences.setProxyAuthentication(useAuthenticationChkBox.isSelected()); 358 NetworkPreferences.setProxyUser(userField.getText()); 359 NetworkPreferences.setProxyPassword(new String(passwordField.getPassword())); 360 361 NetworkUtil.updateNetworkSetting(); 362 } 363 364 @Override 365 public void insertUpdate(DocumentEvent e) 366 { 367 // network setting changed, restart needed 368 getPreferenceFrame().setNeedRestart(); 369 } 370 371 @Override 372 public void removeUpdate(DocumentEvent e) 373 { 374 // network setting changed, restart needed 375 getPreferenceFrame().setNeedRestart(); 376 } 377 378 @Override 379 public void changedUpdate(DocumentEvent e) 380 { 381 // network setting changed, restart needed 382 getPreferenceFrame().setNeedRestart(); 383 } 384}