📄 proxyconfigurationpanel.java
字号:
package de.fhm.jkf.launch.cl;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Properties;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
* @author Theodor Willax
*
* <br><br><center><table border="1" width="80%"><hr>
* <strong><a href="http://jkf.sourceforge.net">The JKF Project</a></strong>
* <p>
* Copyright (C) 2002 by Theodor Willax
* <p>
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* <p>
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* <p>
* You should have received a copy of the <a href="http://www.gnu.org/copyleft/lesser.html">
* GNU Lesser General Public License</a> along with this library; if not, write to
* the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
* <hr></table></center>
*
* Configuration for proxy settings. The following settings must
* be made to the jvm to use an proxy:
* <pre>
* Properties properties = System.getProperties();
* properties.put("http.proxySet", "true");
* properties.put("http.proxyHost", (String) host);
* properties.put("http.proxyPort", (String) port);
* System.setProperties(properties);
* </pre>.
*/
class ProxyConfigurationPanel extends JPanel
implements ItemListener, KeyListener {
/** Default length for proxy host name. */
final static int DEFAULT_HOST_NAME_LENGTH = 35;
/** Maximum length for proxy port number. */
final static int MAX_PORT_NUMBER_LENGTH = 5;
private JCheckBox proxyCheckBox = new JCheckBox("Proxy");
private JLabel proxyHostLabel = new JLabel("Host");
private JLabel proxyPortLabel = new JLabel("Port");
private JTextField proxyHostText =
new JTextField(DEFAULT_HOST_NAME_LENGTH);
private JTextField proxyPortText =
new JTextField(MAX_PORT_NUMBER_LENGTH);
private JPanel proxyFieldPanel = new JPanel();
/**
* Creates a new <tt>ProxyConfigurationPanel</tt>. The
* panel items are by default disabled.
*/
public ProxyConfigurationPanel() {
proxyCheckBox.addItemListener(this);
proxyHostText.addKeyListener(this);
proxyPortText.addKeyListener(this);
proxyFieldPanel.add(proxyHostLabel);
proxyFieldPanel.add(proxyHostText);
proxyFieldPanel.add(proxyPortLabel);
proxyFieldPanel.add(proxyPortText);
setLayout(new BorderLayout());
add(proxyCheckBox, BorderLayout.CENTER);
add(proxyFieldPanel, BorderLayout.SOUTH);
disablePanel(); // default disabled
}
/**
* Enables the panel items.
*/
private void enablePanel() {
proxyHostText.setBackground(Color.white);
proxyHostText.setEnabled(true);
proxyPortText.setBackground(Color.white);
proxyPortText.setEnabled(true);
}
/**
* Disables the panel items.
*/
private void disablePanel() {
proxyHostText.setBackground(Color.lightGray);
proxyHostText.setEnabled(false);
proxyPortText.setBackground(Color.lightGray);
proxyPortText.setEnabled(false);
}
/**
* Checks if proxy configuration is activated.
* Enables the panel items if checkbox is checked,
* disables the items otherwise.
*
* @see java.awt.event.ItemListener#itemStateChanged(ItemEvent)
*/
public void itemStateChanged(ItemEvent e) {
Object source = e.getItemSelectable();
if (source == proxyCheckBox) {
if (isEnabled())
enablePanel();
else
disablePanel();
}
}
/**
* @see java.awt.event.KeyListener#keyTyped(KeyEvent)
*/
public void keyTyped(KeyEvent e) {
}
/**
* @see java.awt.event.KeyListener#keyPressed(KeyEvent)
*/
public void keyPressed(KeyEvent e) {
}
/**
* Checks the text fields if the maximum length for the
* textentries is reached.
*
* @see java.awt.event.KeyListener#keyReleased(KeyEvent)
*/
public void keyReleased(KeyEvent e) {
if (e.getSource() == proxyHostText) { // nop
} else if (e.getSource() == proxyPortText) {
String s = proxyPortText.getText();
if (s.length() > MAX_PORT_NUMBER_LENGTH) {
proxyPortText.setText(
s.substring(0, s.length() - 1));
}
}
}
/**
* Returns the proxy settings made (host and port).
*
* @return the proxy settings if enabled, <tt>null</tt> otherwise.
*/
public ProxySetting getProxySetting() {
if (isEnabled()) {
return new ProxySetting(proxyHostText.getText(),
Integer.parseInt(proxyPortText.getText()));
} else {
return null;
}
}
/**
* Checks if proxy configuration is enabled.
*
* @return true, if proxy configuration enabled, false otherwise.
*/
public boolean isEnabled() {
return proxyCheckBox.isSelected();
}
/**
* Activates the proxy setting's in the jvm. Activation is only done,
* if the proxy check box is enabled. Otherwise no action is taken.
*/
public void activateProxySetting() {
if (isEnabled()) {
ProxySetting ps = getProxySetting();
Properties properties = new Properties(System.getProperties());
properties.put("http.proxySet", "true");
properties.put("http.proxyHost", ps.getHost());
properties.put("http.proxyPort", ps.getPortString());
System.setProperties(new Properties(properties));
System.out.println("http-proxy activated: "
+ ps.getHost() + ":" + ps.getPort());
}
}
/**
* Deactivates the proxy setting's of the jvm. That means
* it removes the proxy settings from the system properties.
*/
public void disableProxySetting() {
Properties properties = new Properties(System.getProperties());
properties.remove("http.proxySet");
properties.remove("http.proxyHost");
properties.remove("http.proxyPort");
System.setProperties(new Properties(properties));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -