/*
 * 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
 *
 *
 *
 *
 * MeterIntervalDialog.java
 * 
 * Created on 17 agosto 2005, 11.19
 *
 */

package com.jaspersoft.ireport.designer.tools;

import com.jaspersoft.ireport.locale.I18n;
import com.jaspersoft.ireport.designer.editor.ExpressionContext;
import com.jaspersoft.ireport.designer.utils.Misc;
import java.awt.Color;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.Window;
import javax.swing.SpinnerNumberModel;
import net.sf.jasperreports.charts.design.JRDesignDataRange;
import net.sf.jasperreports.charts.util.JRMeterInterval;
import net.sf.jasperreports.engine.JRExpression;
import net.sf.jasperreports.engine.design.JRDesignExpression;

/**
 *
 * @author  Administrator
 */
public class MeterIntervalDialog extends javax.swing.JDialog {
    
    private JRMeterInterval meterInterval = null;
    Color backGroundColor = null;
    
    private int dialogResult = javax.swing.JOptionPane.CANCEL_OPTION;
    
    /** Creates new form CategorySeriesDialog */
    public MeterIntervalDialog(Dialog parent, boolean modal) 
    {
         super(parent,modal);
         initAll();
    }

    /** Creates new form ReportQueryFrame */
    public MeterIntervalDialog(Frame parent, boolean modal) 
    {
         super(parent,modal);
         initAll();
    }

    
    public void initAll(){ 

        
        initComponents();
        //applyI18n();
        
        this.setSize(500,500);
        setLocationRelativeTo(null);
        
        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) {
                jButtonCancelActionPerformed(e);
            }
        };
       
        getRootPane().getInputMap(javax.swing.JComponent.WHEN_IN_FOCUSED_WINDOW).put(escape, I18n.getString("Global.Pane.Escape"));
        getRootPane().getActionMap().put(I18n.getString("Global.Pane.Escape"), escapeAction);
        
        
        jSpinnerAlpha.setModel(new SpinnerNumberModel(1.0d, 0.0d, 1.0d, 0.01d));
        
        //to make the default button ...
        this.getRootPane().setDefaultButton(this.jButtonOK);
    }
    
    
    /**
     * this method is used to pass the correct subdataset to the expression editor
     */
    public void setExpressionContext( ExpressionContext expressionContext )
    {
        jRTextExpressionHigh.setExpressionContext(expressionContext);
        jRTextExpressionLow.setExpressionContext(expressionContext);
    }
    
    /** 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;

        jPanel1 = new javax.swing.JPanel();
        jLabelLabel = new javax.swing.JLabel();
        jTextFieldLabel = new javax.swing.JTextField();
        jLabelColor = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        colorSelectorPanel = new javax.swing.JPanel();
        JButtonSelectColor = new javax.swing.JButton();
        jButtonDefaultColor = new javax.swing.JButton();
        jLabelAlpha = new javax.swing.JLabel();
        jSpinnerAlpha = new javax.swing.JSpinner();
        jLabelRangeLow = new javax.swing.JLabel();
        jRTextExpressionLow = new com.jaspersoft.ireport.designer.editor.ExpressionEditorArea();
        jLabelRangeHigh = new javax.swing.JLabel();
        jRTextExpressionHigh = new com.jaspersoft.ireport.designer.editor.ExpressionEditorArea();
        jSeparator1 = new javax.swing.JSeparator();
        jPanel6 = new javax.swing.JPanel();
        jPanel7 = new javax.swing.JPanel();
        jButtonOK = new javax.swing.JButton();
        jButtonCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(I18n.getString("MeterIntervalDialog.Dialog.Title")); // NOI18N
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jPanel1.setPreferredSize(new java.awt.Dimension(400, 250));
        jPanel1.setLayout(new java.awt.GridBagLayout());

        jLabelLabel.setText(I18n.getString("MeterIntervalDialog.Label.Label")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(4, 0, 0, 4);
        jPanel1.add(jLabelLabel, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 0, 0, 0);
        jPanel1.add(jTextFieldLabel, gridBagConstraints);

        jLabelColor.setText(I18n.getString("MeterIntervalDialog.Label.Color")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(4, 0, 0, 4);
        jPanel1.add(jLabelColor, gridBagConstraints);

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

        colorSelectorPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 0, 4, 6);
        jPanel2.add(colorSelectorPanel, gridBagConstraints);

        JButtonSelectColor.setText(I18n.getString("MeterIntervalDialog.Button.SelectColor")); // NOI18N
        JButtonSelectColor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JButtonSelectColorActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        jPanel2.add(JButtonSelectColor, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 0, 4);
        jPanel1.add(jPanel2, gridBagConstraints);

        jButtonDefaultColor.setText(I18n.getString("MeterIntervalDialog.Button.UseDefaultColor")); // NOI18N
        jButtonDefaultColor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonDefaultColorActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 0, 0, 4);
        jPanel1.add(jButtonDefaultColor, gridBagConstraints);

        jLabelAlpha.setText(I18n.getString("MeterIntervalDialog.Label.Alpha")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(4, 0, 0, 4);
        jPanel1.add(jLabelAlpha, gridBagConstraints);

        jSpinnerAlpha.setMinimumSize(new java.awt.Dimension(100, 18));
        jSpinnerAlpha.setPreferredSize(new java.awt.Dimension(130, 18));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 0, 0, 0);
        jPanel1.add(jSpinnerAlpha, gridBagConstraints);

        jLabelRangeLow.setText(I18n.getString("MeterIntervalDialog.Label.DataRangeLowExpression")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints.insets = new java.awt.Insets(4, 0, 0, 0);
        jPanel1.add(jLabelRangeLow, gridBagConstraints);

        jRTextExpressionLow.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jRTextExpressionLow.setMinimumSize(new java.awt.Dimension(10, 10));
        jRTextExpressionLow.setPreferredSize(new java.awt.Dimension(10, 10));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 4, 0);
        jPanel1.add(jRTextExpressionLow, gridBagConstraints);

        jLabelRangeHigh.setText(I18n.getString("MeterIntervalDialog.Label.DataRangeHighExpression")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints.insets = new java.awt.Insets(4, 0, 0, 0);
        jPanel1.add(jLabelRangeHigh, gridBagConstraints);

        jRTextExpressionHigh.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jRTextExpressionHigh.setMinimumSize(new java.awt.Dimension(10, 10));
        jRTextExpressionHigh.setPreferredSize(new java.awt.Dimension(10, 10));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 4, 0);
        jPanel1.add(jRTextExpressionHigh, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        getContentPane().add(jPanel1, gridBagConstraints);
        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, 0, 4, 0);
        getContentPane().add(jSeparator1, gridBagConstraints);

        jPanel6.setLayout(new java.awt.GridBagLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        jPanel6.add(jPanel7, gridBagConstraints);

        jButtonOK.setMnemonic('o');
        jButtonOK.setText(I18n.getString("Global.Button.Ok")); // NOI18N
        jButtonOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOKActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel6.add(jButtonOK, gridBagConstraints);

        jButtonCancel.setMnemonic('c');
        jButtonCancel.setText(I18n.getString("Global.Button.Cancel")); // NOI18N
        jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.insets = new java.awt.Insets(4, 0, 4, 0);
        jPanel6.add(jButtonCancel, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 18;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        getContentPane().add(jPanel6, gridBagConstraints);

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

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

        colorSelectorPanel.setBackground(null);
        backGroundColor = null;
    }//GEN-LAST:event_jButtonDefaultColorActionPerformed

    private void JButtonSelectColorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JButtonSelectColorActionPerformed
        
        java.awt.Color c = HexColorChooserPanel.showDialog(this,I18n.getString("MeterIntervalDialog.Dialog.PickColor"),null);
        if (c != null) {
            colorSelectorPanel.setBackground(c);
            backGroundColor = c;
        }
}//GEN-LAST:event_JButtonSelectColorActionPerformed

    private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
        this.setVisible(false);
        this.dispose();
    }//GEN-LAST:event_jButtonCancelActionPerformed

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

        JRMeterInterval tmpmi = getMeterInterval();
        if (tmpmi == null)
        {
            tmpmi = new JRMeterInterval();
        }
        
        tmpmi.setLabel( jTextFieldLabel.getText());
        tmpmi.setBackgroundColor(backGroundColor);
        
        tmpmi.setAlpha( ((SpinnerNumberModel)jSpinnerAlpha.getModel()).getNumber().doubleValue() );
        
        tmpmi.setDataRange(new JRDesignDataRange(null));
        
        JRDesignExpression expLow = new JRDesignExpression();
        expLow.setValueClassName("java.lang.Number");
        expLow.setText(jRTextExpressionLow.getText());
        ((JRDesignDataRange)tmpmi.getDataRange()).setLowExpression( expLow  );
        
        JRDesignExpression expHigh = new JRDesignExpression();
        expHigh.setValueClassName("java.lang.Number");
        expHigh.setText(jRTextExpressionHigh.getText());
        ((JRDesignDataRange)tmpmi.getDataRange()).setHighExpression( expHigh  );
        
        setMeterInterval( tmpmi );
        
        this.setDialogResult(javax.swing.JOptionPane.OK_OPTION);
        this.setVisible(false);
        this.dispose();
    }//GEN-LAST:event_jButtonOKActionPerformed
    
    
    public int getDialogResult() {
        return dialogResult;
    }

    public void setDialogResult(int dialogResult) {
        this.dialogResult = dialogResult;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton JButtonSelectColor;
    private javax.swing.JPanel colorSelectorPanel;
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JButton jButtonDefaultColor;
    private javax.swing.JButton jButtonOK;
    private javax.swing.JLabel jLabelAlpha;
    private javax.swing.JLabel jLabelColor;
    private javax.swing.JLabel jLabelLabel;
    private javax.swing.JLabel jLabelRangeHigh;
    private javax.swing.JLabel jLabelRangeLow;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private com.jaspersoft.ireport.designer.editor.ExpressionEditorArea jRTextExpressionHigh;
    private com.jaspersoft.ireport.designer.editor.ExpressionEditorArea jRTextExpressionLow;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSpinner jSpinnerAlpha;
    private javax.swing.JTextField jTextFieldLabel;
    // End of variables declaration//GEN-END:variables
    
    /*
        public void applyI18n()
        {
                // Start autogenerated code ----------------------
                jButtonDefaultColor.setText(I18n.getString("meterIntervalDialog.buttonDefaultColor","Use default color"));
                // End autogenerated code ----------------------
            jButtonOK.setText( it.businesslogic.ireport.util.I18n.getString("ok","Ok"));
            jButtonCancel.setText( it.businesslogic.ireport.util.I18n.getString("cancel","Cancel"));
             
            jLabelLabel.setText( it.businesslogic.ireport.util.I18n.getString("meterinterval.label","Label"));
            jLabelColor.setText( it.businesslogic.ireport.util.I18n.getString("meterinterval.color","Color"));
            jLabelAlpha.setText( it.businesslogic.ireport.util.I18n.getString("meterinterval.alpha","Alpha"));
            jLabelRangeHigh.setText( it.businesslogic.ireport.util.I18n.getString("meterinterval.highexp","Data range high expression"));
            jLabelRangeLow.setText( it.businesslogic.ireport.util.I18n.getString("meterinterval.lowexp","Data range low expression"));
            
            setTitle(I18n.getString("meterIntervalDialog.title","Meter interval"));
            jButtonCancel.setMnemonic(I18n.getString("meterIntervalDialog.buttonCancelMnemonic","c").charAt(0));
            jButtonOK.setMnemonic(I18n.getString("meterIntervalDialog.buttonOKMnemonic","o").charAt(0));
                
            this.getRootPane().updateUI();
        }
    */
    
    public JRMeterInterval getMeterInterval() {
        return meterInterval;
    }

    /*
     * The MeterInterval is stored and modified only when ok is pressed.
     * No further modifications are required to the original object like
     * all around iReport.
     *
     */
    public void setMeterInterval(JRMeterInterval meterInterval) {
        this.meterInterval = meterInterval;
        
        if (meterInterval != null)
        {
            jTextFieldLabel.setText( meterInterval.getLabel() );
            ((SpinnerNumberModel)jSpinnerAlpha.getModel()).setValue( new Double( meterInterval.getAlpha() ));
            if ( meterInterval.getBackgroundColor() != null)
            {
                backGroundColor = meterInterval.getBackgroundColor();
                colorSelectorPanel.setBackground(backGroundColor);
            }
            if (meterInterval.getDataRange() != null)
            {
                jRTextExpressionLow.setText( getExpressionText(meterInterval.getDataRange().getLowExpression()) );
                jRTextExpressionHigh.setText( getExpressionText(meterInterval.getDataRange().getHighExpression()) );
            }
        }
    }
    
    private String getExpressionText(JRExpression exp)
    {
        if (exp == null) return "";
        if (exp.getText() == null) return "";
        return exp.getText();
    }
    
    public static final int COMPONENT_NONE=0;
    public static final int COMPONENT_LOW_EXPRESSION=1;
    public static final int COMPONENT_HIGH_EXPRESSION=2;
        
    /**
     * This method set the focus on a specific component.
     * 
     * expressionInfo[0] can be something like:
     * COMPONENT_CATEGORY_EXPRESSION, COMPONENT_SERIES_EXPRESSION, COMPONENT_VALUE_EXPRESSION...
     *
     * If it is COMPONENT_HYPERLINK, other parameters are expected...
     * otherInfo is used here only for COMPONENT_HYPERLINK
     * otherInfo[0] = expression ID
     * otherInfo[1] = parameter #
     * otherInfo[2] = parameter expression ID
     */
    public void setFocusedExpression(Object[] expressionInfo)
    {
        if (expressionInfo == null) return;
        int expID = ((Integer)expressionInfo[0]).intValue();
        
        switch (expID)
        {
            case COMPONENT_LOW_EXPRESSION:
                Misc.selectTextAndFocusArea(jRTextExpressionLow.getExpressionEditorPane());
                break;
            case COMPONENT_HIGH_EXPRESSION:
                Misc.selectTextAndFocusArea(jRTextExpressionHigh.getExpressionEditorPane());
                break;
        }
    }
}
