/*
 * DisplayDataDialog.java
 *
 * Created on April 4, 2008, 2:03 PM
 */

package org.statcato.dialogs.data;

import org.statcato.*;
import org.statcato.spreadsheet.*;

/**
 * A dialog allowing the user to select which columns in the current 
 * Datasheet are displayed in the log window.
 * 
 * @author  Margaret Yau
 * @version %I%, %G%
 * @since 1.0
 */
public class DisplayDataDialog extends StatcatoDialog {
  
    /** Creates new form DisplayDataDialog */
    public DisplayDataDialog(java.awt.Frame parent, boolean modal, Statcato app) {
        super(parent, modal);
        initComponents();
        ParentSpreadsheet = app.getSpreadsheet();
        this.app = app;        
        ParentSpreadsheet.populateColumnsList(InputList);
        
        getRootPane().setDefaultButton(OKButton);
        setHelpFile("data-display-data");
        name = "Display Data";
        description = "For displaying the contents of column variables, " +
                "along with the row numbers, in a tabular format in the log window.";
        helpStrings.add("Select the column(s) to be displayed in the log window" +
                " (control-click to select multiple columns).");
    }
    
    /**
     * Updates elements on the dialog so that they have the most
     * current values.  Called by {@link #setVisible} to make
     * sure the dialog displays current values when made visible again.
     * 
     * @see #setVisible(boolean)
     */
    @Override
    public void updateElements() {
        updateColumnsList(InputList);
    }
    
    /** 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() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        InputList = new javax.swing.JList();
        OKButton = new javax.swing.JButton();
        CancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Display Data");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setText("Select Columns to be displayed in Session Window");

        jLabel2.setText("Ctrl-click to select multiple columns");

        jScrollPane1.setViewportView(InputList);

        OKButton.setText("OK");
        OKButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OKButtonActionPerformed(evt);
            }
        });

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

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(29, 29, 29)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 193, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(51, 51, 51)
                        .addComponent(OKButton)
                        .addGap(18, 18, 18)
                        .addComponent(CancelButton)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {CancelButton, OKButton});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(OKButton)
                    .addComponent(CancelButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

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

    // display the chosen columns in log window
    private void OKButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OKButtonActionPerformed
        app.compoundEdit = new DialogEdit("display data");
        
        // get selected input variables
        Object[] SelectedColumns = InputList.getSelectedValues();
        int[] columnNumbers = ParentSpreadsheet.convertColumnLabelsToNumbers(SelectedColumns);     
        
        // check that at least one input variable is selected
        if (columnNumbers.length == 0) {
            app.showErrorDialog("Select at least one input variable.");
            return;
        }
        
        String table = "<table border='0'>";
        int numRows = ParentSpreadsheet.getLastRowNumber();
        
        // header row
        table += "<tr><td>Row</td>";
        for (int i = 0; i < columnNumbers.length; ++i) {        
            String columnLabel = (String) SelectedColumns[i];
            table += "<td>" + columnLabel + "</td>";
        }
        table += "</tr>";
        // data in rows
        for (int row = 1; row <= numRows; ++row) {
            table += "<tr><td>" + row + "</td>";
            for (int i = 0; i < columnNumbers.length; ++i) {
                String data = ((Cell)ParentSpreadsheet.getValueAt(row, columnNumbers[i])).getContents();
                if (data.equals(""))
                    data = "&nbsp;";
                table += "<td>" + data + "</td>";
            }
            table += "</tr>";
        }
        table += "</table>";
        // display data in log windows
        app.addLogParagraph("Data Display", table);

        app.compoundEdit.end();
        app.addCompoundEdit(app.compoundEdit);
        
        setVisible(false);
    }//GEN-LAST:event_OKButtonActionPerformed

    private void CancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelButtonActionPerformed
        // close the dialog
        setVisible(false);
    }//GEN-LAST:event_CancelButtonActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CancelButton;
    private javax.swing.JList InputList;
    private javax.swing.JButton OKButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
    
}
