/*
 * Copyright (C) 2005 - 2011 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 Affero General Public License  as
 * published by the Free Software Foundation, either version 3 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 Affero  General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public  License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package com.jaspersoft.jasperserver.irplugin.gui;
import com.jaspersoft.jasperserver.irplugin.IRPlugin;
import it.businesslogic.ireport.util.I18n;
import it.businesslogic.ireport.util.Misc;
import java.util.Properties;
import javax.swing.JOptionPane;

/**
 *
 * @author  Administrator
 */
public class JRPluginConfigurationDialog extends javax.swing.JDialog  {
    
    /** Creates new form CheckUpdateDialog */
    public JRPluginConfigurationDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        applyI18n();
        
        
        Properties props = IRPlugin.getMainInstance().getProperties();
        this.jCheckBox1.setSelected(props.getProperty(IRPlugin.PROPERTY_CHECK_FOR_UPDATE, "true").equals("true") );
        this.jCheckBoxEnableProxy.setSelected( props.getProperty(IRPlugin.PROPERTY_USE_PROXY, "false").equals("true") );
        this.jTextFieldURL.setText( props.getProperty(IRPlugin.PROPERTY_PROXY_URL, "") );
        this.jCheckBoxEnableAuth.setSelected( props.getProperty(IRPlugin.PROPERTY_PROXY_USE_AUTH, "false").equals("true") );
        this.jTextFieldUsername.setText( props.getProperty(IRPlugin.PROPERTY_PROXY_USERNAME, "") );
        this.jPasswordField1.setText( props.getProperty(IRPlugin.PROPERTY_PROXY_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();
        jLabelProxy1 = new javax.swing.JLabel();
        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();

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

        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.setBorder(javax.swing.BorderFactory.createTitledBorder("Proxy configuration"));
        jPanel3.setPreferredSize(new java.awt.Dimension(404, 220));
        jPanel3.setLayout(new java.awt.GridBagLayout());

        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.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);

        jLabelProxy1.setText("<html><b>You must restart the application in order for the proxy changes to take effect.");
        jLabelProxy1.setPreferredSize(new java.awt.Dimension(380, 28));
        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(jLabelProxy1, 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

        if (jCheckBoxEnableProxy.isSelected())
        {
            String urlStr =  jTextFieldURL.getText();
            if (urlStr.length() == 0)
            {
                JOptionPane.showMessageDialog(this,"Invalid proxy url","",JOptionPane.ERROR_MESSAGE);
                return;
            }
            
            
            if (!IRPlugin.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;
                }
            }
        }
        
        Properties props = IRPlugin.getMainInstance().getProperties();
        props.setProperty(IRPlugin.PROPERTY_CHECK_FOR_UPDATE, ""+jCheckBox1.isSelected() );
        props.setProperty(IRPlugin.PROPERTY_USE_PROXY, ""+jCheckBoxEnableProxy.isSelected() );
        props.setProperty(IRPlugin.PROPERTY_PROXY_URL, ""+jTextFieldURL.getText() );
        props.setProperty(IRPlugin.PROPERTY_PROXY_USE_AUTH, ""+jCheckBoxEnableAuth.isSelected() );
        props.setProperty(IRPlugin.PROPERTY_PROXY_USERNAME, ""+jTextFieldUsername.getText() );
        props.setProperty(IRPlugin.PROPERTY_PROXY_PASSWORD, ""+ new String(jPasswordField1.getPassword()) );
        
        IRPlugin.getMainInstance().saveConfiguration();
        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 jLabelProxy1;
    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( IRPlugin.getString("checkUpdateDialog.checkbox1", "Check for updates when the plugin starts") );
                // 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 );
     }
}
