/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package turgen.tapeimageextractor;

import java.awt.event.ItemEvent;

/**
 *
 * @author michael
 */
public class SetHintDialog extends javax.swing.JDialog {

    
    private HintsCrate hintsCrate=null;
    
    /**
     * Creates new form SetHintDialog
     */
    public SetHintDialog() {
        setModal(true);
        initComponents();
    }
    
    

    /**
     * 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")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        pButtons = new javax.swing.JPanel();
        jbtOK = new javax.swing.JButton();
        jbtCancel = new javax.swing.JButton();
        pControls = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jspFirstBytes = new javax.swing.JSpinner();
        jLabel2 = new javax.swing.JLabel();
        jspLastBytes = new javax.swing.JSpinner();
        jLabel3 = new javax.swing.JLabel();
        jspLengthOffset = new javax.swing.JSpinner();
        jcbNoLengthOffset = new javax.swing.JCheckBox();
        jlbFiller1 = new javax.swing.JLabel();
        jcbLengthOffset16Bit = new javax.swing.JCheckBox();
        jlbFiller2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Set Extraction Hints");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                onClose(evt);
            }
        });

        jbtOK.setMnemonic('O');
        jbtOK.setText("OK");
        jbtOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onOK(evt);
            }
        });
        pButtons.add(jbtOK);

        jbtCancel.setText("Cancel");
        jbtCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onCancel(evt);
            }
        });
        pButtons.add(jbtCancel);

        getContentPane().add(pButtons, java.awt.BorderLayout.SOUTH);

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

        jLabel1.setText("Skip first n bytes");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 0, 0, 0);
        pControls.add(jLabel1, gridBagConstraints);

        jspFirstBytes.setModel(new javax.swing.SpinnerNumberModel(0, 0, 65536, 1));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 0, 0);
        pControls.add(jspFirstBytes, gridBagConstraints);

        jLabel2.setText("Skip last n bytes");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 0, 0, 0);
        pControls.add(jLabel2, gridBagConstraints);

        jspLastBytes.setModel(new javax.swing.SpinnerNumberModel(0, 0, 65536, 1));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 0, 0);
        pControls.add(jspLastBytes, gridBagConstraints);

        jLabel3.setText("Block length offset");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 0, 0, 0);
        pControls.add(jLabel3, gridBagConstraints);

        jspLengthOffset.setModel(new javax.swing.SpinnerNumberModel(0, 0, 65536, 1));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 0, 0);
        pControls.add(jspLengthOffset, gridBagConstraints);

        jcbNoLengthOffset.setText("None");
        jcbNoLengthOffset.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                onLengthOffsetNoneChanged(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 0, 0);
        pControls.add(jcbNoLengthOffset, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        pControls.add(jlbFiller1, gridBagConstraints);

        jcbLengthOffset16Bit.setText("16-bit");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 0, 0);
        pControls.add(jcbLengthOffset16Bit, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        pControls.add(jlbFiller2, gridBagConstraints);

        getContentPane().add(pControls, java.awt.BorderLayout.CENTER);

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

    private void onLengthOffsetNoneChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_onLengthOffsetNoneChanged
        if (evt.getStateChange()==ItemEvent.SELECTED) {
            jspLengthOffset.setEnabled(false);
            jcbLengthOffset16Bit.setEnabled(false);
        }
        else {
            jspLengthOffset.setEnabled(true);
            jcbLengthOffset16Bit.setEnabled(true);
        }
    }//GEN-LAST:event_onLengthOffsetNoneChanged

    private void onCancel(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onCancel
            hintsCrate=null;
            setVisible(false);
    }//GEN-LAST:event_onCancel

    private void onClose(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_onClose
        onCancel(null);
    }//GEN-LAST:event_onClose

    private void onOK(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onOK
        hintsCrate=createHintsCrate();
        setVisible(false);
    }//GEN-LAST:event_onOK

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(SetHintDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(SetHintDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(SetHintDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(SetHintDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new SetHintDialog().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JButton jbtCancel;
    private javax.swing.JButton jbtOK;
    private javax.swing.JCheckBox jcbLengthOffset16Bit;
    private javax.swing.JCheckBox jcbNoLengthOffset;
    private javax.swing.JLabel jlbFiller1;
    private javax.swing.JLabel jlbFiller2;
    private javax.swing.JSpinner jspFirstBytes;
    private javax.swing.JSpinner jspLastBytes;
    private javax.swing.JSpinner jspLengthOffset;
    private javax.swing.JPanel pButtons;
    private javax.swing.JPanel pControls;
    // End of variables declaration//GEN-END:variables
    
    void fillValues(int firstBytes,int lastBytes,int lengthOffset,boolean is16BitLength) {
        
        if (lengthOffset==ExtractorChunk.NO_LENGTH_OFFSET) {
            jspLengthOffset.setValue(Integer.valueOf(0));
            jcbNoLengthOffset.setSelected(true);
        }
        else {
            jspLengthOffset.setValue(Integer.valueOf(lengthOffset));
            jcbLengthOffset16Bit.setSelected(is16BitLength);
            jcbNoLengthOffset.setSelected(false);
        }
        
        jspFirstBytes.setValue(Integer.valueOf(firstBytes));
        jspLastBytes.setValue(Integer.valueOf(lastBytes));
        
        
    }
    
    private HintsCrate createHintsCrate() {
        int first = ((Integer)jspFirstBytes.getValue()).intValue();
        int last  = ((Integer)jspLastBytes.getValue()).intValue();
        int length = ((Integer)jspLengthOffset.getValue()).intValue();
                
        if (jcbNoLengthOffset.isSelected()) {
            return new HintsCrate(first,last,ExtractorChunk.NO_LENGTH_OFFSET,false);
        }
        else {
            return new HintsCrate(first,last,length,jcbLengthOffset16Bit.isSelected());
        }
    }
    
    HintsCrate getHintsCrate() {
        return hintsCrate;
    }
    
}
