/*
 * Copyright (C) 2005 - 2007 JasperSoft Corporation.  All rights reserved. 
 * http://www.jaspersoft.com.
 *
 * Unless you have purchased a commercial license agreement from JasperSoft,
 * the following license terms apply:
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation.
 *
 * This program is distributed WITHOUT ANY WARRANTY; and without 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, see http://www.gnu.org/licenses/gpl.txt
 * or write to:
 *
 * Free Software Foundation, Inc.,
 * 59 Temple Place - Suite 330,
 * Boston, MA  USA  02111-1307
 *
 *
 *
 *
 * CheckUpdateDialog.java
 * 
 * Created on 22 maggio 2005, 0.39
 *
 */

package it.businesslogic.ireport.plugin.checkupdate;
import it.businesslogic.ireport.util.I18n;
import it.businesslogic.ireport.util.Misc;
import java.net.URL;
import java.util.Properties;
import javax.swing.JOptionPane;

/**
 *
 * @author  Administrator
 */
public class CheckUpdateDialog extends javax.swing.JDialog  {
    
    /** Creates new form CheckUpdateDialog */
    public CheckUpdateDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        applyI18n();
        
        
        Properties props = it.businesslogic.ireport.gui.MainFrame.getMainInstance().getProperties();
        this.jCheckBox1.setSelected( props.getProperty("updateOnStartup", "true").equals("true") );
        this.jCheckBoxEnableProxy.setSelected( props.getProperty("update.useProxy", "false").equals("true") );
        this.jTextFieldURL.setText( props.getProperty("update.proxyUrl", "") );
        this.jCheckBoxEnableAuth.setSelected( props.getProperty("update.useAuth", "false").equals("true") );
        this.jTextFieldUsername.setText( props.getProperty("update.username", "") );
        this.jPasswordField1.setText( props.getProperty("update.password", "") );  
        
        updateProxyUse();
        
        it.businesslogic.ireport.util.Misc.centerFrame(this);
        
        javax.swing.KeyStroke escape =  javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, 0, false);
            javax.swing.Action escapeAction = new javax.swing.AbstractAction() {
                public void actionPerformed(java.awt.event.ActionEvent e) {
                    jButton2ActionPerformed(e);
                }
            };

            getRootPane().getInputMap(javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).put(escape, "ESCAPE");
            getRootPane().getActionMap().put("ESCAPE", escapeAction);


            //to make the default button ...
            this.getRootPane().setDefaultButton(this.jButton1);
        this.pack();
    }
    
    /** 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;

        jCheckBox1 = new javax.swing.JCheckBox();
        jPanel3 = new javax.swing.JPanel();
        jLabelProxy = new javax.swing.JLabel();
        jCheckBoxEnableProxy = new javax.swing.JCheckBox();
        jLabelProxyURL = new javax.swing.JLabel();
        jTextFieldURL = new javax.swing.JTextField();
        jCheckBoxEnableAuth = new javax.swing.JCheckBox();
        jLabelUsername = new javax.swing.JLabel();
        jTextFieldUsername = new javax.swing.JTextField();
        jLabelPassword = new javax.swing.JLabel();
        jPasswordField1 = new javax.swing.JPasswordField();
        jSeparator1 = new javax.swing.JSeparator();
        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        getContentPane().setLayout(new java.awt.GridBagLayout());

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        jCheckBox1.setText("Check for update on iReport startup");
        jCheckBox1.setMinimumSize(new java.awt.Dimension(350, 50));
        jCheckBox1.setPreferredSize(new java.awt.Dimension(350, 24));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 4);
        getContentPane().add(jCheckBox1, gridBagConstraints);

        jPanel3.setLayout(new java.awt.GridBagLayout());

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Proxy configuration"));
        jLabelProxy.setText("<html>If you need a HTTP proxy, enter it here in the format HOST:PORT<br>e.g. squid.mysite.com:3128");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel3.add(jLabelProxy, gridBagConstraints);

        jCheckBoxEnableProxy.setText("Enable HTTP Proxy");
        jCheckBoxEnableProxy.setActionCommand("Enable HTTP Proxy");
        jCheckBoxEnableProxy.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jCheckBoxEnableProxy.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jCheckBoxEnableProxy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBoxEnableProxyActionPerformed(evt);
            }
        });
        jCheckBoxEnableProxy.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jCheckBoxEnableProxyStateChanged(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel3.add(jCheckBoxEnableProxy, gridBagConstraints);

        jLabelProxyURL.setText("Proxy address");
        jLabelProxyURL.setEnabled(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel3.add(jLabelProxyURL, gridBagConstraints);

        jTextFieldURL.setEnabled(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        jPanel3.add(jTextFieldURL, gridBagConstraints);

        jCheckBoxEnableAuth.setText("Use Authentication");
        jCheckBoxEnableAuth.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jCheckBoxEnableAuth.setEnabled(false);
        jCheckBoxEnableAuth.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jCheckBoxEnableAuth.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jCheckBoxEnableAuthStateChanged(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel3.add(jCheckBoxEnableAuth, gridBagConstraints);

        jLabelUsername.setText("Username");
        jLabelUsername.setEnabled(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel3.add(jLabelUsername, gridBagConstraints);

        jTextFieldUsername.setEnabled(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        jPanel3.add(jTextFieldUsername, gridBagConstraints);

        jLabelPassword.setText("Password");
        jLabelPassword.setEnabled(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel3.add(jLabelPassword, gridBagConstraints);

        jPasswordField1.setText("jPasswordField1");
        jPasswordField1.setEnabled(false);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        jPanel3.add(jPasswordField1, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        getContentPane().add(jPanel3, gridBagConstraints);

        jSeparator1.setMinimumSize(new java.awt.Dimension(0, 4));
        jSeparator1.setPreferredSize(new java.awt.Dimension(0, 4));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 0, 4, 0);
        getContentPane().add(jSeparator1, gridBagConstraints);

        jPanel1.setLayout(new java.awt.GridBagLayout());

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.weightx = 1.0;
        jPanel1.add(jPanel2, gridBagConstraints);

        jButton1.setText("Save");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(4, 0, 4, 4);
        jPanel1.add(jButton1, gridBagConstraints);

        jButton2.setText("Cancel");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(4, 0, 4, 10);
        jPanel1.add(jButton2, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        getContentPane().add(jPanel1, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jCheckBoxEnableAuthStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jCheckBoxEnableAuthStateChanged
        updateAuthUse();
    }//GEN-LAST:event_jCheckBoxEnableAuthStateChanged

    private void jCheckBoxEnableProxyStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_jCheckBoxEnableProxyStateChanged
        updateProxyUse();
    }//GEN-LAST:event_jCheckBoxEnableProxyStateChanged

    private void jCheckBoxEnableProxyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBoxEnableProxyActionPerformed

        
        
    }//GEN-LAST:event_jCheckBoxEnableProxyActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        setVisible(false);
        dispose();
    }//GEN-LAST:event_jButton2ActionPerformed
    
    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened

    }//GEN-LAST:event_formWindowOpened
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        Properties props = it.businesslogic.ireport.gui.MainFrame.getMainInstance().getProperties();
        
        if (jCheckBoxEnableProxy.isSelected())
        {
            String urlStr =  jTextFieldURL.getText();
            if (urlStr.length() == 0)
            {
                JOptionPane.showMessageDialog(this,"Invalid proxy url","",JOptionPane.ERROR_MESSAGE);
                return;
            }
            
            
            if (!Misc.isValidUrl(jTextFieldURL.getText()))
            {
                JOptionPane.showMessageDialog(this,I18n.getString("checkUpdateDialog.message.invalidUrl","Invalid proxy url"),"",JOptionPane.ERROR_MESSAGE);
                return;
            }
            
            if (jCheckBoxEnableAuth.isSelected())
            {
                if (jTextFieldUsername.getText().length() == 0)
                {
                    JOptionPane.showMessageDialog(this,I18n.getString("checkUpdateDialog.message.invalidUsername","Invalid username"),"",JOptionPane.ERROR_MESSAGE);
                    return;
                }
            }
        }
        
        props.setProperty("updateOnStartup", ""+jCheckBox1.isSelected() );
        props.setProperty("update.useProxy", ""+jCheckBoxEnableProxy.isSelected() );
        props.setProperty("update.proxyUrl", ""+jTextFieldURL.getText() );
        props.setProperty("update.useAuth", ""+jCheckBoxEnableAuth.isSelected() );
        props.setProperty("update.username", ""+jTextFieldUsername.getText() );
        props.setProperty("update.password", ""+ new String(jPasswordField1.getPassword()) );
        
        
        it.businesslogic.ireport.gui.MainFrame.getMainInstance().saveiReportConfiguration();
        setVisible(false);
        dispose();
        
    }//GEN-LAST:event_jButton1ActionPerformed

    
    

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JCheckBox jCheckBoxEnableAuth;
    private javax.swing.JCheckBox jCheckBoxEnableProxy;
    private javax.swing.JLabel jLabelPassword;
    private javax.swing.JLabel jLabelProxy;
    private javax.swing.JLabel jLabelProxyURL;
    private javax.swing.JLabel jLabelUsername;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField jTextFieldURL;
    private javax.swing.JTextField jTextFieldUsername;
    // End of variables declaration//GEN-END:variables
    
    public void applyI18n(){
                // Start autogenerated code ----------------------
                jCheckBox1.setText(I18n.getString("checkUpdateDialog.checkBox1","Check for update on iReport startup"));
                // End autogenerated code ----------------------
                // Start autogenerated code ----------------------
                jButton1.setText(I18n.getString("checkUpdateDialog.button1","Save"));
                jButton2.setText(I18n.getString("checkUpdateDialog.button2","Cancel"));
                
                jCheckBoxEnableProxy.setText(I18n.getString("checkUpdateDialog.enableProxy","Enable HTTP Proxy"));
                jCheckBoxEnableAuth.setText(I18n.getString("checkUpdateDialog.useAuthentication","Use Authentication"));
                jLabelProxyURL.setText(I18n.getString("checkUpdateDialog.jLabelProxyURL","Proxy address"));
                jLabelUsername.setText(I18n.getString("checkUpdateDialog.jLabelUsername","Username"));
                jLabelPassword.setText(I18n.getString("checkUpdateDialog.jLabelPassword","Password"));
                jLabelProxy.setText("<html>" + I18n.getString("checkUpdateDialog.jLabelProxy","If you need a HTTP proxy, enter it here in the format HOST:PORT<br>e.g. squid.mysite.com:3128"));
                
                // End autogenerated code ----------------------
                jButton1.setMnemonic(I18n.getString("checkUpdateDialog.button1Mnemonic","s").charAt(0));
                jButton2.setMnemonic(I18n.getString("checkUpdateDialog.button2Mnemonic","c").charAt(0));
    }

    private void updateProxyUse() {
        
        boolean enabled =  jCheckBoxEnableProxy.isSelected();
        this.jTextFieldURL.setEnabled( enabled );
        this.jLabelProxyURL.setEnabled(  enabled );
        this.jCheckBoxEnableAuth.setEnabled( enabled );
        
        updateAuthUse();
    }
    
     private void updateAuthUse() {
         
         boolean authEnabled = jCheckBoxEnableProxy.isSelected() && jCheckBoxEnableAuth.isSelected();
         this.jLabelUsername.setEnabled(  authEnabled );
         this.jLabelPassword.setEnabled(  authEnabled );
         this.jTextFieldUsername.setEnabled(  authEnabled );
         this.jPasswordField1.setEnabled(  authEnabled );
     }
}
