📄 optionsnetworkjpanel.java
字号:
/*
* OptionsNetworkJPanel.java
*
* Created on June 19, 2007, 8:16 PM
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program 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
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Copyright (C) 2007 Cheok YanCheng <yccheok@yahoo.com>
*/
package org.yccheok.jstock.gui;
import java.io.IOException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.swing.*;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;
import java.net.*;
import java.io.*;
import java.text.*;
import javax.swing.text.*;
/**
*
* @author yccheok
*/
public class OptionsNetworkJPanel extends javax.swing.JPanel implements JStockOptionsObserver {
/** Creates new form OptionsNetworkJPanel */
public OptionsNetworkJPanel() {
initComponents();
}
private JFormattedTextField getPortNumberJFormattedTextField() {
DecimalFormat df = new DecimalFormat("#####");
NumberFormatter nf = new NumberFormatter(df) {
public String valueToString(Object iv) throws ParseException {
if ((iv == null) || (((Integer)iv).intValue() == -1)) {
return "";
}
else {
return super.valueToString(iv);
}
}
public Object stringToValue(String text) throws ParseException {
if ("".equals(text)) {
return null;
}
return super.stringToValue(text);
}
};
nf.setMinimum(0);
nf.setMaximum(65534);
nf.setValueClass(Integer.class);
return new JFormattedTextField(nf);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
private void initComponents() {
jXHeader1 = new org.jdesktop.swingx.JXHeader();
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jTextField1 = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
jButton1 = new javax.swing.JButton();
jLabel4 = new javax.swing.JLabel();
jFormattedTextField1 = getPortNumberJFormattedTextField();
setLayout(new java.awt.BorderLayout());
jXHeader1.setDescription("If you are behind an organization network, you may need to specific network proxy server. Consult your network administrator for more info.");
jXHeader1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/32x32/connect_to_network.png")));
jXHeader1.setTitle("Proxy Server");
add(jXHeader1, java.awt.BorderLayout.NORTH);
jLabel1.setText("Proxy Server");
jLabel2.setText("Port");
jLabel3.setFont(new java.awt.Font("Dialog", 0, 10));
jLabel3.setText("(e.g. 192.168.0.165)");
jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/16x16/internet.png")));
jButton1.setText("Test connection");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jLabel4.setFont(new java.awt.Font("Dialog", 0, 10));
jLabel4.setText("(e.g. 1024)");
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton1)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jFormattedTextField1, 0, 0, Short.MAX_VALUE)
.addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 106, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel4)
.addComponent(jLabel3))))
.addContainerGap(118, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(jFormattedTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel4))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton1)
.addContainerGap(122, Short.MAX_VALUE))
);
add(jPanel1, java.awt.BorderLayout.CENTER);
}// </editor-fold>//GEN-END:initComponents
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
final String httpproxyHost = System.getProperties().getProperty("http.proxyHost");
final String httpproxyPort = System.getProperties().getProperty("http.proxyPort");
if((jTextField1.getText().length() > 0) && org.yccheok.jstock.engine.Utils.isValidPortNumber(jFormattedTextField1.getText())) {
System.getProperties().put("http.proxyHost", jTextField1.getText());
System.getProperties().put("http.proxyPort", jFormattedTextField1.getText());
}
else {
System.getProperties().remove("http.proxyHost");
System.getProperties().remove("http.proxyPort");
}
/*
try {
java.net.InetAddress address = java.net.InetAddress.getByName("google.com");
if(address.isReachable(5000) == false)
{
JOptionPane.showMessageDialog(this, "Wrong proxy server or port. Unable connect to internet.", "Internet down", JOptionPane.WARNING_MESSAGE);
return;
}
}
catch(java.net.UnknownHostException exp) {
log.error("", exp);
JOptionPane.showMessageDialog(this, "Wrong proxy server or port. Unable connect to internet.", "Internet down", JOptionPane.WARNING_MESSAGE);
return;
}
catch (IOException exp) {
log.error("", exp);
JOptionPane.showMessageDialog(this, "Wrong proxy server or port. Unable connect to internet.", "Internet down", JOptionPane.WARNING_MESSAGE);
return;
}
*/
boolean success = false;
HttpClient httpClient = new HttpClient();
HttpMethod method = new GetMethod("http://www.google.com");
try {
org.yccheok.jstock.engine.Utils.setHttpClientProxyFromSystemProperties(httpClient);
httpClient.executeMethod(method);
final String responde = method.getResponseBodyAsString();
success = true;
}
catch(HttpException exp) {
log.error("", exp);
JOptionPane.showMessageDialog(this, "Wrong proxy server or port. Unable connect to internet.", "Internet down", JOptionPane.WARNING_MESSAGE);
return;
}
catch(IOException exp) {
log.error("", exp);
JOptionPane.showMessageDialog(this, "Wrong proxy server or port. Unable connect to internet.", "Internet down", JOptionPane.WARNING_MESSAGE);
return;
}
finally {
method.releaseConnection();
}
/*
try
{
URL url = new URL("http://www.google.com");
URLConnection connection = url.openConnection();
connection.setConnectTimeout(5000);
BufferedReader dis = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String tmpLine = tmpLine = dis.readLine();
dis.close();
success = true;
} catch (MalformedURLException me) {
log.error("", me);
} catch (IOException ioe) {
log.error("", ioe);
}
*/
if(success)
JOptionPane.showMessageDialog(this, "Correct proxy server and port. Internet connection success.", "Connection success", JOptionPane.INFORMATION_MESSAGE);
else
JOptionPane.showMessageDialog(this, "Wrong proxy server or port. Unable connect to internet.", "Internet down", JOptionPane.WARNING_MESSAGE);
if(httpproxyHost != null) {
System.getProperties().put("http.proxyHost", httpproxyHost);
}
else {
System.getProperties().remove("http.proxyHost");
}
if(httpproxyPort != null) {
System.getProperties().put("http.proxyPort", httpproxyPort);
}
else {
System.getProperties().remove("http.proxyPort");
}
}//GEN-LAST:event_jButton1ActionPerformed
public void set(JStockOptions jStockOptions) {
jTextField1.setText(jStockOptions.getProxyServer());
if(org.yccheok.jstock.engine.Utils.isValidPortNumber(jStockOptions.getProxyPort()))
jFormattedTextField1.setText("" + jStockOptions.getProxyPort());
else
jFormattedTextField1.setText("");
}
public boolean apply(JStockOptions jStockOptions) {
if((jTextField1.getText().length() > 0) && org.yccheok.jstock.engine.Utils.isValidPortNumber(jFormattedTextField1.getText())) {
System.getProperties().put("http.proxyHost", jTextField1.getText());
System.getProperties().put("http.proxyPort", jFormattedTextField1.getText());
}
else {
System.getProperties().remove("http.proxyHost");
System.getProperties().remove("http.proxyPort");
}
jStockOptions.setProxyServer(jTextField1.getText());
int port = -1;
if(jFormattedTextField1.getText().length() > 0) {
try {
port = Integer.parseInt(jFormattedTextField1.getText());
}
catch(NumberFormatException exp) {
log.error("", exp);
}
}
jStockOptions.setProxyPort(port);
return true;
}
private static final Log log = LogFactory.getLog(OptionsNetworkJPanel.class);
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButton1;
private javax.swing.JFormattedTextField jFormattedTextField1;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField jTextField1;
private org.jdesktop.swingx.JXHeader jXHeader1;
// End of variables declaration//GEN-END:variables
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -