/*
 * iReport - Visual Designer for JasperReports.
 * Copyright (C) 2002 - 2009 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 part of iReport.
 *
 * iReport 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.
 *
 * iReport 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 iReport. If not, see <http://www.gnu.org/licenses/>.
 */
package com.jaspersoft.ireport.designer.data;

import com.jaspersoft.ireport.designer.sheet.Tag;
import com.jaspersoft.ireport.locale.I18n;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import net.sf.jasperreports.engine.design.JRDesignDataset;
import net.sf.jasperreports.engine.design.JRDesignField;
import net.sf.jasperreports.engine.design.JRDesignSortField;
/**
 *
 * @author  Administrator
 */
public class SortFieldDialog extends javax.swing.JDialog {
    /**
     * Creates new form SortFieldDialog
     */
    private JRDesignSortField tmpSortField = null;
    
    public SortFieldDialog(java.awt.Dialog parent, boolean modal) {
        
        super(parent, modal);
        initComponents();
        jComboBoxSortBy.setEditable(false);

        jComboBoxSortType.addItem(new Tag(JRDesignSortField.SORT_ORDER_ASCENDING,"Ascending"));
        jComboBoxSortType.addItem(new Tag(JRDesignSortField.SORT_ORDER_DESCENDING,"Descending"));

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


        //to make the default button ...
        this.getRootPane().setDefaultButton(this.jButtonOK);
        
        //applyI18n();
        this.pack();
        
    }
    
    /*
    public void applyI18n()
    {
                // Start autogenerated code ----------------------
                // End autogenerated code ----------------------
        this.setTitle( I18n.getString("sortFieldDialog.title","Sort field") );
        jLabelSortBy.setText( I18n.getString("sortFieldDialog.labelSortBy","Sort by..."));
        jLabelSortType.setText( I18n.getString("sortFieldDialog.labelSortType","Sort type"));
        this.jComboBoxSortType.removeAllItems();
        this.jComboBoxSortType.addItem(new Tag("Ascending",I18n.getString("sortFieldDialog.Ascending","Ascending")));
        this.jComboBoxSortType.addItem(new Tag("Descending",I18n.getString("sortFieldDialog.Descending","Descending")));
        Misc.setComboboxSelectedTagValue(jComboBoxSortType, "Ascending");
        jButtonOK.setText( I18n.getString("ok","Ok"));
        jButtonCancel.setText(I18n.getString("cancel","Cancel"));
    }
    */
    
    /**
     * Populate the fields combobox
     */
    @SuppressWarnings("unchecked")
    private void setFieldNames(List fields)
    {
        jComboBoxSortBy.removeAllItems();
        List newList = new ArrayList();
        if (fields != null)
        {
            for (int i=0; i<fields.size(); ++i)
            {
                JRDesignField f = (JRDesignField)fields.get(i);
                newList.add(f.getName());
            }
        }
        Object[] names = newList.toArray();
        Arrays.sort(names);
        jComboBoxSortBy.setModel(new DefaultComboBoxModel( names ) );
    }
    
    /** 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;

        jLabelSortBy = new javax.swing.JLabel();
        jComboBoxSortBy = new javax.swing.JComboBox();
        jSeparator1 = new javax.swing.JSeparator();
        jPanel1 = new javax.swing.JPanel();
        jButtonOK = new javax.swing.JButton();
        jButtonCancel = new javax.swing.JButton();
        jLabelSortType = new javax.swing.JLabel();
        jComboBoxSortType = new javax.swing.JComboBox();

        setTitle(I18n.getString("SortFieldDialog.Title.AddModParam")); // NOI18N
        setModal(true);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });
        getContentPane().setLayout(new java.awt.GridBagLayout());

        jLabelSortBy.setText(I18n.getString("SortFieldDialog.Label.SortBy")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 0, 4);
        getContentPane().add(jLabelSortBy, gridBagConstraints);

        jComboBoxSortBy.setEditable(true);
        jComboBoxSortBy.setPreferredSize(new java.awt.Dimension(250, 22));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        getContentPane().add(jComboBoxSortBy, gridBagConstraints);

        jSeparator1.setMinimumSize(new java.awt.Dimension(2, 2));
        jSeparator1.setPreferredSize(new java.awt.Dimension(2, 2));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 8;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
        gridBagConstraints.weighty = 1.0;
        getContentPane().add(jSeparator1, gridBagConstraints);

        jPanel1.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        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);
            }
        });
        jPanel1.add(jButtonOK);

        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);
            }
        });
        jPanel1.add(jButtonCancel);

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

        jLabelSortType.setText(I18n.getString("SortFieldDialog.Label.SortType")); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 0, 4);
        getContentPane().add(jLabelSortType, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 8, 4);
        getContentPane().add(jComboBoxSortType, gridBagConstraints);

        pack();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        java.awt.Dimension dialogSize = getSize();
        setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
    }// </editor-fold>//GEN-END:initComponents

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

    private void jButtonOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOKActionPerformed
        
        String fieldName = (String)this.jComboBoxSortBy.getSelectedItem();
        if (fieldName == null) fieldName="";
        fieldName = fieldName.trim();
        
        if (fieldName.length() <= 0)
        {
            javax.swing.JOptionPane.showMessageDialog(this,
                    //I18n.getString("sortFieldDialog.messageNotValidField",
                    I18n.getString("SortFieldDialog.Message.Error"),
                    //I18n.getString("sortFieldDialog.messageTitleNotValidField",
                    I18n.getString("SortFieldDialog.Message.Error2"),
                    javax.swing.JOptionPane.WARNING_MESSAGE );
            return;
        }
        
        tmpSortField = new JRDesignSortField();
        tmpSortField.setName(fieldName);
        tmpSortField.setOrder( (jComboBoxSortType.getSelectedIndex() == 1) ? 
                    JRDesignSortField.SORT_ORDER_DESCENDING : JRDesignSortField.SORT_ORDER_ASCENDING);

        setVisible(false);
        this.setDialogResult( javax.swing.JOptionPane.OK_OPTION);
        dispose();
    }//GEN-LAST:event_jButtonOKActionPerformed
    
    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible(false);
        this.setDialogResult( javax.swing.JOptionPane.CLOSED_OPTION);
        dispose();
    }//GEN-LAST:event_closeDialog
    
    
    /** Getter for property dialogResult.
     * @return Value of property dialogResult.
     *
     */
    public int getDialogResult() {
        return dialogResult;
    }
    
    /** Setter for property dialogResult.
     * @param dialogResult New value of property dialogResult.
     *
     */
    public void setDialogResult(int dialogResult) {
        this.dialogResult = dialogResult;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JButton jButtonOK;
    private javax.swing.JComboBox jComboBoxSortBy;
    private javax.swing.JComboBox jComboBoxSortType;
    private javax.swing.JLabel jLabelSortBy;
    private javax.swing.JLabel jLabelSortType;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JSeparator jSeparator1;
    // End of variables declaration//GEN-END:variables

    private int dialogResult;    
    
    public void setDataset(JRDesignDataset dataset) {
    
       setFieldNames( dataset.getFieldsList() );
       
    }

    /**
     * return a new sortField instance
     */
    public JRDesignSortField getSortField() {
        return tmpSortField;
    }

    public void setSortField(JRDesignSortField sortField) {
        this.tmpSortField = new JRDesignSortField();
        this.tmpSortField.setName(  sortField.getName() );
        this.tmpSortField.setOrder(  sortField.getOrder() );
        this.jComboBoxSortBy.setSelectedItem( tmpSortField.getName() );
        this.jComboBoxSortType.setSelectedIndex( ((tmpSortField.getOrder() == JRDesignSortField.SORT_ORDER_DESCENDING) ? 1 : 0) );
    }
    
}
