/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.zaproxy.zap.extension.automacrobuilder.generated;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.ResourceBundle;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import org.zaproxy.zap.extension.automacrobuilder.FileReadLine;
import org.zaproxy.zap.extension.automacrobuilder.TemporaryValueStorage;
import org.zaproxy.zap.extension.automacrobuilder.EnvironmentVariables;

/**
 *
 * @author gdgd009xcd
 */
@SuppressWarnings("serial")
public class ParmGenCSVLoader extends javax.swing.JFrame {
    private static final ResourceBundle bundle = ResourceBundle.getBundle("burp/Bundle");
    String csvfile;
    FileReadLine frn;
    DefaultTableModel model;
    InterfaceParmGenWin parentwin;

    /**
     * new instance method<br>
     * you must define this in your extended classes for instantiation
     *
     * @param _parent
     * @param _csvfile
     * @return this
     */
    public static ParmGenCSVLoader newInstance(InterfaceParmGenWin _parent, String _csvfile) {
        return new ParmGenCSVLoader(_parent, _csvfile).buildThis(_parent, _csvfile);
    }

    /**
     * Do not call this constructor directly for instantiating this class.<br>
     * use newInstance() method instead.
     *
     * @param _parent
     * @param _csvfile
     */
    protected ParmGenCSVLoader(InterfaceParmGenWin _parent, String _csvfile) {
        super();
    }

    /**
     * you must call this method in newInstance method after creating this object<br>
     * See newInstance() method.
     * In extended class, you must call parent class's buildThis() method in your buildThis() method.
     *
     * @param _parent
     * @param _csvfile
     * @return this
     */
    protected final ParmGenCSVLoader buildThis(InterfaceParmGenWin _parent, String _csvfile) {
        parentwin = _parent;
        initComponents();
        model = (DefaultTableModel)ColumnTable.getModel();
        csvfile = _csvfile;
        frn= new FileReadLine(null, csvfile);
        return this;
    }

    public boolean  readOneLine(){
        ArrayList<String> columns = frn.readOneRecordWithColumns();
        if(columns==null){
            JOptionPane.showMessageDialog(this, bundle.getString("ParmGenCSVLoader.CSVLoadErrorOptionPaneMessage.text"), bundle.getString("ParmGenCSVLoader.CSVLoadErrorOptionPaneTitle.text"), JOptionPane.ERROR_MESSAGE);
            return false;
        }else{
            while(model.getRowCount()>0){
                model.removeRow(0);
            }
            Iterator<String> it= columns.iterator();
            int i=0;
            while(it.hasNext()){
                model.addRow(new Object[] {i++, it.next()});
            }
        }
        return true;
    }
    /**
     * 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.
     */
    @SuppressWarnings({"unchecked","rawtypes","serial"})
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        CSelect = new javax.swing.JButton();
        Cancel = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jScrollPane1 = new javax.swing.JScrollPane();
        ColumnTable = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        ReadNext = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle(bundle.getString("ParmGenCSVLoader.DialogTitle.text")); // NOI18N

        CSelect.setText(bundle.getString("ParmGenCSVLoader.CSelectBtn.text")); // NOI18N
        CSelect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CSelectActionPerformed(evt);
            }
        });

        Cancel.setText(bundle.getString("ParmGenCSVLoader.CancelBtn.text")); // NOI18N
        Cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelActionPerformed(evt);
            }
        });

        ColumnTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "position", "value"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Object.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        ColumnTable.setToolTipText(bundle.getString("ParmGenCSVLoader.tooltip.text")); // NOI18N
        jScrollPane1.setViewportView(ColumnTable);
        if (ColumnTable.getColumnModel().getColumnCount() > 0) {
            ColumnTable.getColumnModel().getColumn(0).setHeaderValue(bundle.getString("ParmGenCSVLoader.columntitle.text")); // NOI18N
            ColumnTable.getColumnModel().getColumn(1).setHeaderValue(bundle.getString("ParmGenCSVLoader.columnvalue.text")); // NOI18N
        }

        jLabel1.setText(bundle.getString("ParmGenCSVLoader.InstructionForSelectingColumnTitleLabel1.text")); // NOI18N

        ReadNext.setText(bundle.getString("ParmGenCSVLoader.ReadNextBtn.text")); // NOI18N
        ReadNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ReadNextActionPerformed(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()
                .addGap(42, 42, 42)
                .addComponent(CSelect)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(Cancel)
                .addGap(44, 44, 44))
            .addGroup(layout.createSequentialGroup()
                .addGap(13, 13, 13)
                .addComponent(jScrollPane1)
                .addGap(12, 12, 12))
            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 439, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(ReadNext)
                .addGap(30, 30, 30))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ReadNext))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 286, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(Cancel)
                    .addComponent(CSelect))
                .addGap(25, 25, 25))
        );

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

    private void CSelectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CSelectActionPerformed
        // TODO add your handling code here:
        int[] rowsSelected = ColumnTable.getSelectedRows();
        dispose();
        if(rowsSelected.length> 0){
            for (int i = 0;i < rowsSelected.length;i++ ){
                int colpos = (int)model.getValueAt(rowsSelected[i], 0);
                EnvironmentVariables.getTemporaryValueStorageInstance().put(
                        i,
                        TemporaryValueStorage.Keys.K_COLUMN,
                        TemporaryValueStorage.Keys.Class_K_COLUMN,
                        Integer.toString(colpos));
            }
            dispose();
            ParmGenAddParms.newInstance((ParmGenNew)parentwin, true).setVisible(true);
        }
    }//GEN-LAST:event_CSelectActionPerformed

    private void CancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_CancelActionPerformed

    private void ReadNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ReadNextActionPerformed
        // TODO add your handling code here:
        readOneLine();
    }//GEN-LAST:event_ReadNextActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CSelect;
    private javax.swing.JButton Cancel;
    private javax.swing.JTable ColumnTable;
    private javax.swing.JButton ReadNext;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    // End of variables declaration//GEN-END:variables
}
