/*
* JFrameLogginConfigurator.java
*
* Created on 7. November 2005, 20:58
*/
package xplanetconfigurator.gui;
import java.awt.Image;
import java.awt.Toolkit;
import java.util.logging.*;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JCheckBox;
import javax.swing.JTextField;
import xplanetconfigurator.util.OwnPreferences;
/**
*
* @author wiedthom
*/
public class JFrameProxyConfigurator extends javax.swing.JFrame {
private static final String FRAME_X = "frame_proxy_configurator_frame_x";
private static final String FRAME_Y = "frame_proxy_configurator_frame_y";
private static final String FRAME_W = "frame_proxy_configurator_frame_w";
private static final String FRAME_H = "frame_proxy_configurator_frame_h";
private static JFrameProxyConfigurator instance;
private Logger logger;
private MainFrame caller;
public static JFrameProxyConfigurator getInstance(MainFrame caller) {
if (instance == null) {
instance = new JFrameProxyConfigurator(caller);
}
return instance;
}
/** Creates new form JFrameLogginConfigurator */
private JFrameProxyConfigurator(MainFrame caller) {
this.caller = caller;
logger = Logger.getLogger(this.getClass().getName());
initComponents();
this.getRootPane().setDefaultButton(this.jButtonOK);
this.loadUserPrefsGUI();
this.loadProxySettingFromDownloader();
}
/** 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() {
java.awt.GridBagConstraints gridBagConstraints;
jPanelMain = new javax.swing.JPanel();
jPanelFileHandler = new javax.swing.JPanel();
jLabelProxyHost = new javax.swing.JLabel();
jTextFieldHost = new javax.swing.JTextField();
jLabelProxyPort = new javax.swing.JLabel();
jTextFieldPort = new javax.swing.JTextField();
jLabelUser = new javax.swing.JLabel();
jTextFieldUser = new javax.swing.JTextField();
jLabelPass = new javax.swing.JLabel();
jTextFieldPass = new javax.swing.JTextField();
jCheckBoxProxySet = new javax.swing.JCheckBox();
jPanelButtons = new javax.swing.JPanel();
jPanelSpace = new javax.swing.JPanel();
jButtonCancel = new javax.swing.JButton();
jButtonOK = new javax.swing.JButton();
setTitle("Proxy Configuration");
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
formWindowClosing(evt);
}
});
addComponentListener(new java.awt.event.ComponentAdapter() {
public void componentShown(java.awt.event.ComponentEvent evt) {
formComponentShown(evt);
}
});
getContentPane().setLayout(new java.awt.GridBagLayout());
jPanelMain.setLayout(new java.awt.GridBagLayout());
jPanelFileHandler.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
jPanelFileHandler.setLayout(new java.awt.GridBagLayout());
jLabelProxyHost.setText("Proxy Host");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 0);
jPanelFileHandler.add(jLabelProxyHost, gridBagConstraints);
jTextFieldHost.setMinimumSize(new java.awt.Dimension(500, 20));
jTextFieldHost.setPreferredSize(new java.awt.Dimension(500, 20));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 1;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 5);
jPanelFileHandler.add(jTextFieldHost, gridBagConstraints);
jLabelProxyPort.setText("Proxy Port");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 2;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 0);
jPanelFileHandler.add(jLabelProxyPort, gridBagConstraints);
jTextFieldPort.setMinimumSize(new java.awt.Dimension(100, 20));
jTextFieldPort.setPreferredSize(new java.awt.Dimension(100, 20));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 2;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 5);
jPanelFileHandler.add(jTextFieldPort, gridBagConstraints);
jLabelUser.setText("User");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 4;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 0);
jPanelFileHandler.add(jLabelUser, gridBagConstraints);
jTextFieldUser.setMinimumSize(new java.awt.Dimension(100, 20));
jTextFieldUser.setPreferredSize(new java.awt.Dimension(100, 20));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 4;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 0);
jPanelFileHandler.add(jTextFieldUser, gridBagConstraints);
jLabelPass.setText("Pass");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 5;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 0);
jPanelFileHandler.add(jLabelPass, gridBagConstraints);
jTextFieldPass.setMinimumSize(new java.awt.Dimension(100, 20));
jTextFieldPass.setPreferredSize(new java.awt.Dimension(100, 20));
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 5;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
gridBagConstraints.insets = new java.awt.Insets(0, 5, 5, 0);
jPanelFileHandler.add(jTextFieldPass, gridBagConstraints);
jCheckBoxProxySet.setMnemonic('P');
jCheckBoxProxySet.setText("Use Proxy");
jCheckBoxProxySet.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jCheckBoxProxySetActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 0;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
jPanelFileHandler.add(jCheckBoxProxySet, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 1;
gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
jPanelMain.add(jPanelFileHandler, gridBagConstraints);
jPanelButtons.setLayout(new java.awt.GridBagLayout());
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
jPanelButtons.add(jPanelSpace, gridBagConstraints);
jButtonCancel.setMnemonic('C');
jButtonCancel.setText("Cancel");
jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonCancelActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 0;
gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 7);
jPanelButtons.add(jButtonCancel, gridBagConstraints);
jButtonOK.setMnemonic('O');
jButtonOK.setText("OK");
jButtonOK.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButtonOKActionPerformed(evt);
}
});
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 3;
gridBagConstraints.gridy = 0;
gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
jPanelButtons.add(jButtonOK, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 4;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
gridBagConstraints.insets = new java.awt.Insets(12, 0, 0, 0);
jPanelMain.add(jPanelButtons, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
gridBagConstraints.insets = new java.awt.Insets(12, 12, 12, 12);
getContentPane().add(jPanelMain, gridBagConstraints);
pack();
}// </editor-fold>//GEN-END:initComponents
private void formComponentShown(java.awt.event.ComponentEvent evt)//GEN-FIRST:event_formComponentShown
{//GEN-HEADEREND:event_formComponentShown
this.loadUserPrefsGUI();
this.loadProxySettingFromDownloader();
}//GEN-LAST:event_formComponentShown
private void formWindowClosing(java.awt.event.WindowEvent evt)//GEN-FIRST:event_formWindowClosing
{//GEN-HEADEREND:event_formWindowClosing
this.closeMySelf();
}//GEN-LAST:event_formWindowClosing
private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonCancelActionPerformed
{//GEN-HEADEREND:event_jButtonCancelActionPerformed
this.closeMySelf();
}//GEN-LAST:event_jButtonCancelActionPerformed
private void jButtonOKActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonOKActionPerformed
{//GEN-HEADEREND:event_jButtonOKActionPerformed
this.save();
this.closeMySelf();
}//GEN-LAST:event_jButtonOKActionPerformed
private void jCheckBoxProxySetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBoxProxySetActionPerformed
this.toggleComponents();
}//GEN-LAST:event_jCheckBoxProxySetActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
/*
new JFrameProxyConfigurator(new JFrame("Test")).setVisible(true);
* */
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jButtonCancel;
private javax.swing.JButton jButtonOK;
private javax.swing.JCheckBox jCheckBoxProxySet;
private javax.swing.JLabel jLabelPass;
private javax.swing.JLabel jLabelProxyHost;
private javax.swing.JLabel jLabelProxyPort;
private javax.swing.JLabel jLabelUser;
private javax.swing.JPanel jPanelButtons;
private javax.swing.JPanel jPanelFileHandler;
private javax.swing.JPanel jPanelMain;
private javax.swing.JPanel jPanelSpace;
private javax.swing.JTextField jTextFieldHost;
private javax.swing.JTextField jTextFieldPass;
private javax.swing.JTextField jTextFieldPort;
private javax.swing.JTextField jTextFieldUser;
// End of variables declaration//GEN-END:variables
public static Image getImage(String url) {
URL u = null;
try {
u = JFrameProxyConfigurator.class.getResource(url);
String file = u.getPath();
} catch (Exception e) {
e.printStackTrace();
return null;
}
Image image = Toolkit.getDefaultToolkit().getImage(u);
return image;
}
/**
* Loads the stored frame position and dimension or packs if never used.
* Fills the components like text fielsd. Leaves the default values if no
* value is found in the user preferences. The values where set prior to this
* operation to default values.
*/
private void loadUserPrefsGUI() {
OwnPreferences prefs =
OwnPreferences.userNodeForPackage(this.getClass());
// Position frame
int x = prefs.getInt(JFrameProxyConfigurator.FRAME_X, 10);
int y = prefs.getInt(JFrameProxyConfigurator.FRAME_Y, 10);
int w = prefs.getInt(JFrameProxyConfigurator.FRAME_W, 10);
int h = prefs.getInt(JFrameProxyConfigurator.FRAME_H, 10);
this.pack();
if (x == 10 && y == 10) // Default = first useage
{
this.setLocationRelativeTo(null); //center it
} else {
this.setLocation(x, y);
this.setSize(w, h);
}
}
private void save() {
this.sendDownloadParameter(this.jCheckBoxProxySet, "(proxy\\.set=)(.*)", "proxy.set");
this.sendDownloadParameter(this.jTextFieldHost, "(?i)(http\\.proxyHost=)(.*)", "http.proxyHost");
this.sendDownloadParameter(this.jTextFieldPort, "(?i)(http\\.proxyPort=)(.*)", "http.proxyPort");
this.sendDownloadParameter(this.jTextFieldUser, "(?i)(http\\.proxyUser=)(.*)", "http.proxyUser");
this.sendDownloadParameter(this.jTextFieldPass, "(?i)(http\\.proxyPass=)(.*)", "http.proxyPass");
// Store it to file that is is not overwritten by the example configs
this.caller.writeProxyConfigToFile();
}
private void saveGUISettings() {
logger.finer("Saving GUI settings (position, size) to user preferences...");
int x = this.getX();
int y = this.getY();
java.awt.Dimension d = this.getSize();
int h = (int) d.getHeight();
int w = (int) d.getWidth();
OwnPreferences prefs =
OwnPreferences.userNodeForPackage(this.getClass());
// Frame position an size
prefs.putInt(JFrameProxyConfigurator.FRAME_X, x);
prefs.putInt(JFrameProxyConfigurator.FRAME_Y, y);
prefs.putInt(JFrameProxyConfigurator.FRAME_H, h);
prefs.putInt(JFrameProxyConfigurator.FRAME_W, w);
}
private void closeMySelf() {
logger.finer("About to close frame...");
this.saveGUISettings();
logger.fine("Hiding...");
this.setVisible(false);
}
private void loadProxySettingFromDownloader() {
String params = this.caller.getDownloaderConfig();
// host
String regExpr = "(?i)(http\\.proxyHost=)(.*)";
String found = this.receiveParameter(params, regExpr, this.jTextFieldHost, 2);
// port
regExpr = "(?i)(http\\.proxyPort=)(.*)";
found = this.receiveParameter(params, regExpr, this.jTextFieldPort, 2);
// user
regExpr = "(?i)(http\\.proxyUser=)(.*)";
found = this.receiveParameter(params, regExpr, this.jTextFieldUser, 2);
// pass
regExpr = "(?i)(http\\.proxyPass=)(.*)";
found = this.receiveParameter(params, regExpr, this.jTextFieldPass, 2);
// set proxy
regExpr = "(?i)(proxy\\.set\\b=true)";
this.receiveParameter(params, regExpr, this.jCheckBoxProxySet);
// Enable/disable
this.toggleComponents();
}
private void toggleComponents() {
this.jTextFieldHost.setEnabled(this.jCheckBoxProxySet.isSelected());
this.jTextFieldPort.setEnabled(this.jCheckBoxProxySet.isSelected());
this.jTextFieldUser.setEnabled(this.jCheckBoxProxySet.isSelected());
this.jTextFieldPass.setEnabled(this.jCheckBoxProxySet.isSelected());
this.jLabelProxyHost.setEnabled(this.jCheckBoxProxySet.isSelected());
this.jLabelProxyPort.setEnabled(this.jCheckBoxProxySet.isSelected());
this.jLabelUser.setEnabled(this.jCheckBoxProxySet.isSelected());
this.jLabelPass.setEnabled(this.jCheckBoxProxySet.isSelected());
}
private String receiveParameter(String parameters, String regExpr, JTextField field, int group) {
field.setText("");
String found = "";
Pattern p = Pattern.compile(regExpr);
if (parameters == null) {
parameters = "";
}
Matcher matcher = p.matcher(parameters);
while (matcher.find()) {
found = matcher.group();
this.logger.finer("Found own parameters: " + found);
String foundValue = matcher.group(group);
if (foundValue != null) {
foundValue = foundValue.trim();
// do not allow blanks
foundValue = foundValue.replaceAll("\\s+", "");
if (!foundValue.equals("")) {
field.setText(foundValue);
}
}
}
return found;
}
private String receiveParameter(String parameters, String regExpr, JCheckBox checkBox) {
checkBox.setSelected(false);
String found = "false";
Pattern p = Pattern.compile(regExpr);
if (parameters == null) {
parameters = "";
}
Matcher matcher = p.matcher(parameters);
while (matcher.find()) {
found = matcher.group();
this.logger.finer("Found own parameters: " + found);
checkBox.setSelected(true);
}
return found;
}
private void sendDownloadParameter(
JTextField textField,
String regExpressionKeyValue, String key) {
// Get the downlaod config
String parameters = this.caller.getDownloaderConfig();
// Remove all key-value-pair
parameters = parameters.replaceAll(regExpressionKeyValue, "");
// Get the selected item
String value = textField.getText();
String keyValue = key + "=" + value;
// Append the selected item
parameters = parameters + "\n" + keyValue;
// Remove all empty lines
parameters = parameters.replaceAll("(?m)^\\s*$^\\s*", "");
this.caller.setDownloaderConfiguration(parameters);
}
private void sendDownloadParameter(
JCheckBox checkBox,
String regExpressionKeyValue, String key) {
// Get the downlaod config
String parameters = this.caller.getDownloaderConfig();
// Remove all key-value-pair
parameters = parameters.replaceAll(regExpressionKeyValue, "");
// Get the selected item
boolean selected = checkBox.isSelected();
String keyValue = key + "=false";
if (selected) {
keyValue = key + "=true";
}
// Append the selected item
parameters = parameters + "\n" + keyValue;
// Remove all empty lines
parameters = parameters.replaceAll("(?m)^\\s*$^\\s*", "");
this.caller.setDownloaderConfiguration(parameters);
}
}