package turgen.plugin.csturbo;

import turgen.plugin.*;
import java.awt.event.ItemEvent;
import javax.swing.*;
import java.io.*;
import turgen.dtb.DOS2Binary;
import turgen.Utils;

public class CsTurboPluginsPanel extends javax.swing.JPanel {
    
    InOutProvider ioProvider;
    
    private static final int XEX_NOTRECOGNIZED=0;
    private static final int XEX_COMPATIBLE=1;
    private static final int XEX_NONCOMPATIBLE=2;

    private final boolean superTurbo;
    
    /** Creates new form DiscretePluginsPanel
     * @param superTurbo Panel is for Super Turbo plugin
     */
    public CsTurboPluginsPanel(boolean superTurbo) {
        initComponents();
        setupToolTips();

        if (superTurbo==false) {
            jcoConversionType.addItem("Monolithic binary file to Turbo 2000");
            jcoConversionType.addItem("ChainLoading of segmented binary file");
            jcoConversionType.addItem("BlockLoading of segmented binary file");
            jcoConversionType.addItem("Tokenized BASIC to Turbo 2000");
            jcoConversionType.addItem("Plain DATA to Turbo 2000");
            jcoConversionType.addItem("Binary file to binary turbo");
        }
        else {
            jcoConversionType.addItem("Monolithic binary file to Super Turbo");
            jcoConversionType.addItem("ChainLoading of segmented binary file");
            jcoConversionType.addItem("BlockLoading of segmented binary file");
            jcoConversionType.addItem("Tokenized BASIC to Super Turbo");
            jcoConversionType.addItem("Plain DATA to Super Turbo");
        }

        this.superTurbo=superTurbo;

    }
    
    public void setIOProvider(InOutProvider prov) {
        this.ioProvider=prov;
        prov.registerFileChooser(jfcLoadHeader);
    }
    
    /** 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;

        jrbgHandling = new javax.swing.ButtonGroup();
        jfcLoadHeader = new javax.swing.JFileChooser();
        pTHeader = new javax.swing.JPanel();
        jlbLoad = new javax.swing.JLabel();
        jtfLoad = new javax.swing.JTextField();
        jlbRun = new javax.swing.JLabel();
        jtfRun = new javax.swing.JTextField();
        jlbLength = new javax.swing.JLabel();
        jtfLength = new javax.swing.JTextField();
        jlbType = new javax.swing.JLabel();
        jtfType = new javax.swing.JTextField();
        jtfName = new javax.swing.JTextField();
        jlbName = new javax.swing.JLabel();
        jlbFiller = new javax.swing.JLabel();
        pHeaderControl = new javax.swing.JPanel();
        btnGuessHeader = new javax.swing.JButton();
        btnLoadHeader = new javax.swing.JButton();
        pHandling = new javax.swing.JPanel();
        jcoConversionType = new javax.swing.JComboBox();
        btnCheckLoader = new javax.swing.JButton();
        pBinaryFile = new javax.swing.JPanel();
        jlbPauses = new javax.swing.JLabel();
        jtfPauses = new javax.swing.JTextField();
        pFiller = new javax.swing.JPanel();

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

        pTHeader.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Header block"));
        pTHeader.setLayout(new java.awt.GridBagLayout());

        jlbLoad.setText("Load:");
        jlbLoad.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 0, 0);
        pTHeader.add(jlbLoad, gridBagConstraints);

        jtfLoad.setColumns(8);
        jtfLoad.setFont(new java.awt.Font("Monospaced", 0, 12)); // NOI18N
        jtfLoad.setToolTipText("Load address (0-65535)");
        jtfLoad.setAlignmentX(0.0F);
        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);
        pTHeader.add(jtfLoad, gridBagConstraints);

        jlbRun.setText("Run:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 0, 0);
        pTHeader.add(jlbRun, gridBagConstraints);

        jtfRun.setColumns(8);
        jtfRun.setFont(new java.awt.Font("Monospaced", 0, 12)); // NOI18N
        jtfRun.setToolTipText("Run address (0-65535)");
        jtfRun.setAlignmentX(0.0F);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 0, 4);
        pTHeader.add(jtfRun, gridBagConstraints);

        jlbLength.setText("Length:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 0, 0);
        pTHeader.add(jlbLength, gridBagConstraints);

        jtfLength.setColumns(8);
        jtfLength.setFont(new java.awt.Font("Monospaced", 0, 12)); // NOI18N
        jtfLength.setToolTipText("File length (0-65535)");
        jtfLength.setAlignmentX(0.0F);
        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);
        pTHeader.add(jtfLength, gridBagConstraints);

        jlbType.setText("Type:");
        jlbType.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 0, 0);
        pTHeader.add(jlbType, gridBagConstraints);

        jtfType.setColumns(8);
        jtfType.setFont(new java.awt.Font("Monospaced", 0, 12)); // NOI18N
        jtfType.setToolTipText("File type (0-255)");
        jtfType.setAlignmentX(0.0F);
        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);
        pTHeader.add(jtfType, gridBagConstraints);

        jtfName.setColumns(22);
        jtfName.setFont(new java.awt.Font("Monospaced", 0, 12)); // NOI18N
        jtfName.setToolTipText("File name");
        jtfName.setAlignmentX(0.0F);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 5;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 0, 0);
        pTHeader.add(jtfName, gridBagConstraints);

        jlbName.setText("File name:");
        jlbName.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 0, 0);
        pTHeader.add(jlbName, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        gridBagConstraints.weighty = 1.0;
        pTHeader.add(jlbFiller, gridBagConstraints);

        pHeaderControl.setAlignmentX(0.0F);
        pHeaderControl.setAlignmentY(0.0F);
        pHeaderControl.setLayout(new java.awt.GridBagLayout());

        btnGuessHeader.setMnemonic('h');
        btnGuessHeader.setText("Auto set header");
        btnGuessHeader.setToolTipText("<HTML>\nAutomatically fill the turbo header using:<BR>\n1.Analysis of input file<BR>\n2.Selected conversion type<BR>\n</HTML>\n\n");
        btnGuessHeader.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                on_guess_header(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        pHeaderControl.add(btnGuessHeader, gridBagConstraints);

        btnLoadHeader.setMnemonic('l');
        btnLoadHeader.setText("Load header");
        btnLoadHeader.setToolTipText("Load header from external file");
        btnLoadHeader.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                onLoadHeader(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(0, 4, 0, 0);
        pHeaderControl.add(btnLoadHeader, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.gridwidth = 6;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 0, 0, 0);
        pTHeader.add(pHeaderControl, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 2, 2);
        add(pTHeader, gridBagConstraints);

        pHandling.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Conversion type"));
        pHandling.setLayout(new java.awt.GridBagLayout());

        jcoConversionType.setToolTipText("Choose conversion type according to the type of the input file");
        jcoConversionType.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                on_cvTypeChanged(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 0);
        pHandling.add(jcoConversionType, gridBagConstraints);

        btnCheckLoader.setMnemonic('c');
        btnCheckLoader.setText("Check loader");
        btnCheckLoader.setToolTipText("<HTML>\nCheck whether Chainloader or Blockloader<BR>\nwill be destroyed by input binary file<BR>\n</HTML>");
        btnCheckLoader.setEnabled(false);
        btnCheckLoader.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCheckLoaderActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        pHandling.add(btnCheckLoader, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.1;
        add(pHandling, gridBagConstraints);

        pBinaryFile.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Binary file"));
        pBinaryFile.setLayout(new java.awt.GridBagLayout());

        jlbPauses.setText("Silence list:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 0);
        pBinaryFile.add(jlbPauses, gridBagConstraints);

        jtfPauses.setFont(new java.awt.Font("Monospaced", 0, 12)); // NOI18N
        jtfPauses.setToolTipText("<HTML>\nPlace <b>comma-separated list of numebrs</b> here. <br>\nEach number is <b>length of silence</b> (in seconds) generated <b>after INIT segments.</b><br>\nThis is useful only if the MOTOR CTRL signal is ignored by device you are using.<br>\n</HTML>");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        pBinaryFile.add(jtfPauses, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints.insets = new java.awt.Insets(2, 0, 2, 0);
        add(pBinaryFile, gridBagConstraints);
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
        gridBagConstraints.weighty = 0.9;
        add(pFiller, gridBagConstraints);
    }// </editor-fold>//GEN-END:initComponents
    
    private void on_guess_header(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_on_guess_header

        /*What conversion type was selected*/
        int idx = this.jcoConversionType.getSelectedIndex();

        /*Clearing text fields*/
        jtfName.setText("");
        jtfType.setText("");
        jtfLoad.setText("");
        jtfRun.setText("");
        jtfLength.setText("");
        
        File f = new File(ioProvider.getInFile());
        
        String msg = Utils.checkIfHeaderCanBeSet(f);
        if (msg!=null) {
            JOptionPane.showMessageDialog(null, msg,"Unable to set header",JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        /*File name*/
        String fn=f.getName();
        int ml=(superTurbo==true)? 20 : 10;
        if (fn.length()>ml) fn=fn.substring(0,ml);

        /*Setting header according to conversion type*/
        switch (idx) {

            /*Monolithic binary file*/
            case CsTurboPlaylistItem.CV_MONOLITHIC: {

                DOS2Binary dtb = new DOS2Binary(ioProvider.getInFile());
                try {
                    dtb.analyzeFromFile();
                }
                catch(Exception e) {
                    JOptionPane.showMessageDialog(this, Utils.getTitledExceptionMessage("Unable to auto set header", e),"Unable to auto set header",JOptionPane.ERROR_MESSAGE);
                    return;
                }

                if (!dtb.isMonolithic()) {
                    JOptionPane.showMessageDialog(this,NONCOMPATIBLE_MSG,"Not a monolithic binary file",JOptionPane.WARNING_MESSAGE);
                    return;
                }

                /*Filling-in the fields, now we know that it has at least one DATA
                  segment*/
                jtfType.setText("3");
                jtfLoad.setText(Integer.toString(dtb.getFirstDataSegment().start));
                jtfLength.setText(Integer.toString((dtb.getFirstDataSegment().stop-dtb.getFirstDataSegment().start)+1));

                if (dtb.getJumpSegmentCount()==0) {
                    jtfRun.setText(Integer.toString(dtb.getFirstDataSegment().start));
                }
                else {
                    jtfRun.setText(Integer.toString(dtb.getFirstJumpSegment().jump));
                }
       
            break;
        }

        /*Chainloading or BlockLoading - nothing to do!*/
        case CsTurboPlaylistItem.CV_SEGMENTED_CHAIN:break;
        case CsTurboPlaylistItem.CV_SEGMENTED_BLOCK:break;

        /*BASIC*/
        case CsTurboPlaylistItem.CV_BASIC: {
            jtfType.setText("255");
            jtfLoad.setText("2034");
            jtfRun.setText("50750");
            jtfLength.setText(Long.toString(f.length()));
            break;
        }
        /*So called binary turbo*/
        case CsTurboPlaylistItem.CV_BINTURBO: {
            jtfType.setText("4");
            jtfLoad.setText("0");
            jtfRun.setText("0");
            jtfLength.setText(Long.toString(f.length()));
            break;
        }
        /*Plain data*/
        case CsTurboPlaylistItem.CV_DATA: {
            jtfType.setText("1");
            jtfLength.setText(Long.toString(f.length()));
            break;
        }
    }

    /*File name is common*/
    jtfName.setText(fn);
    

    }//GEN-LAST:event_on_guess_header

    private void on_cvTypeChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_on_cvTypeChanged
        if (evt.getStateChange()==ItemEvent.SELECTED) {

            int idx = jcoConversionType.getSelectedIndex();
            this.setSpecificComponentsEnabled(idx);

            if (idx==1 || idx==2) {
                btnCheckLoader.setEnabled(true);
            }
            else {
                btnCheckLoader.setEnabled(false);
            }
        }
    }//GEN-LAST:event_on_cvTypeChanged

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


        int sct = jcoConversionType.getSelectedIndex();
        String msg="";
        String fname=ioProvider.getInFile();

        if (superTurbo==false) {

            switch(sct) {
                /*ChainLoader*/
                case 1: {
                    msg=Utils.checkForBinaryLoader(fname, 1800, 2191, 1800, 2191,252);
                    break;
                }
                /*BlockLoader*/
                case 2: {
                    msg=Utils.checkForBinaryLoader(fname, 2048, 2627, 2048, 2627,62);
                    break;
                }

            }
        }
        else {
             switch(sct) {
                /*SuperChain*/
                case 1: {
                    msg=Utils.checkForBinaryLoader(fname, 1800, 2270, 1800, 2270,252);
                    break;
                }
                /*SuperBlock*/
                case 2: {
                    msg=Utils.checkForBinaryLoader(fname, 2048, 2682, 2048, 2682,62);
                    break;
                }

            }
        }
        JOptionPane.showMessageDialog(null, msg,"Loader check",JOptionPane.INFORMATION_MESSAGE);



    }//GEN-LAST:event_btnCheckLoaderActionPerformed

    private void onLoadHeader(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_onLoadHeader
        
        String fspec = ioProvider.getInFile();
        File f = new File(fspec);
        String[] headerElements;
        
        /*First try to load automatically*/
        if (f.exists() && f.isFile()) {
            f = new File(f.getAbsolutePath()+".theader");
            if (f.exists() && f.isFile()) {
                headerElements=Utils.loadcsTurboHeaderFromFile(f.getAbsolutePath());
                if (headerElements!=null) {
                    suckHeaderElements(headerElements);
                    return;
                }
            }
        }
        
        /*Otherwise setup the file chooser*/
        f = new File(fspec);
        if (f.isDirectory()) {
            jfcLoadHeader.setCurrentDirectory(f);
        }
        else {
            jfcLoadHeader.setCurrentDirectory(f.getParentFile());
        }
        
        /*Show the file chooser*/
        int r = jfcLoadHeader.showOpenDialog(this);
        if (r==JFileChooser.APPROVE_OPTION && (f=jfcLoadHeader.getSelectedFile())!=null) {
            headerElements=Utils.loadcsTurboHeaderFromFile(f.getAbsolutePath());
            if (headerElements!=null) {
                suckHeaderElements(headerElements);
            }
            else {
                JOptionPane.showMessageDialog(this,"Unable to load turbo header from the selected file","Unable to load turbo header",JOptionPane.ERROR_MESSAGE);
            }
            
        }
        
    }//GEN-LAST:event_onLoadHeader
    
    
    private int verifyMonolithic() {

        /*Load and analyze file*/
        DOS2Binary ae = new DOS2Binary(ioProvider.getInFile());
        try {
            ae.analyzeFromFile();
        }
        catch (Exception e) {
            return XEX_NOTRECOGNIZED;
        }

        /*Test whether file is monolithic*/
        boolean nonmonolithic = !ae.isMonolithic();
        /*Return appropriate result*/
        return nonmonolithic==true?XEX_NONCOMPATIBLE:XEX_COMPATIBLE;
        
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCheckLoader;
    private javax.swing.JButton btnGuessHeader;
    private javax.swing.JButton btnLoadHeader;
    private javax.swing.JComboBox jcoConversionType;
    private javax.swing.JFileChooser jfcLoadHeader;
    private javax.swing.JLabel jlbFiller;
    private javax.swing.JLabel jlbLength;
    private javax.swing.JLabel jlbLoad;
    private javax.swing.JLabel jlbName;
    private javax.swing.JLabel jlbPauses;
    private javax.swing.JLabel jlbRun;
    private javax.swing.JLabel jlbType;
    private javax.swing.ButtonGroup jrbgHandling;
    private javax.swing.JTextField jtfLength;
    private javax.swing.JTextField jtfLoad;
    private javax.swing.JTextField jtfName;
    private javax.swing.JTextField jtfPauses;
    private javax.swing.JTextField jtfRun;
    private javax.swing.JTextField jtfType;
    private javax.swing.JPanel pBinaryFile;
    private javax.swing.JPanel pFiller;
    private javax.swing.JPanel pHandling;
    private javax.swing.JPanel pHeaderControl;
    private javax.swing.JPanel pTHeader;
    // End of variables declaration//GEN-END:variables
    
    
    void clear() {
        jtfName.setText("");
        jtfLoad.setText("");
        jtfRun.setText("");
        jtfLength.setText("");
        jtfType.setText("");
        jcoConversionType.setSelectedIndex(0);
        jtfPauses.setText("");
        
    }
    
    
    
    public CsTurboPlaylistItem getRecord() {
        CsTurboPlaylistItem cpr =
                new CsTurboPlaylistItem(ioProvider.getInFile(),ioProvider.getOutFile(),ioProvider.getPulses(),jtfType.getText(),jtfName.getText(),jtfLoad.getText(),jtfRun.getText(),jtfLength.getText(),jcoConversionType.getSelectedIndex(),Utils.parseNumberList(jtfPauses.getText()));
        return cpr;
    }
    
    
    public void fill(CsTurboPlaylistItem cpr) {
        jtfName.setText(cpr.fname);
        jtfLoad.setText(cpr.load);
        jtfRun.setText(cpr.run);
        jtfLength.setText(cpr.length);
        jtfType.setText(cpr.type);
        jcoConversionType.setSelectedIndex(cpr.conversionType);

        StringBuffer sb = new StringBuffer();
        for(int i=0;i<cpr.silenceList.length;i++) {
            if (i!=0) sb.append(',');
            sb.append(Integer.toString(cpr.silenceList[i]));
        }
        jtfPauses.setText(sb.toString());
    }
    
    public String verify() {
        
        int ct=jcoConversionType.getSelectedIndex();

        if (ct!=CsTurboPlaylistItem.CV_SEGMENTED_CHAIN && ct!=CsTurboPlaylistItem.CV_SEGMENTED_BLOCK) {

            int tp=-1;
            int ld=-1;
            int ln=-1;
            int rn=-1;
                
        
            try {
                tp=Integer.parseInt(jtfType.getText());
                ld=Integer.parseInt(jtfLoad.getText());
                rn=Integer.parseInt(jtfRun.getText());
                ln=Integer.parseInt(jtfLength.getText());
            }
            catch(Exception e) {
                return "Numbers expected but not found";
            }

        
            if (tp<0 || tp>255) return "Type out of range (0-255)";
            if (ld<0 || ld>65535) return "Load address out of range (0-65535)";
            if (ln<1 || ln>65536) return "Length out of range (1-65536)";
            if (rn<0 || rn>65535) return "Run address out of range (0-65535)";
        }
        
        if (jcoConversionType.getSelectedIndex()==CsTurboPlaylistItem.CV_MONOLITHIC) {
        
            int rt=verifyMonolithic();
            if (rt==XEX_NONCOMPATIBLE) {
                JOptionPane.showMessageDialog(this,NONCOMPATIBLE_MSG,"Not a monolithic binary file",JOptionPane.WARNING_MESSAGE);
            }
        }
       
        
        return null;
    }
    
    private static final String NONCOMPATIBLE_MSG
            = "<HTML>Input file is not a monolithic binary file.</B><BR><BR>"
            + "You have chosen the \"Monolithic binary file to...\" conversion type,<BR> but the input file is not a monolithic binary file.<BR><BR>"
            + "To convert the binary file, use the <U>Blockloading</U> or <U>Chainloading</U> conversion types.<BR>"
            + "Another possiblity is to use the B-TAPE or Turbo 2000 - kilobyte blocks plugins.<BR><BR>"
            + "<I>Technical note: Monolithic binary files are binary files that contain<BR> exactly one DATA segment and at most one RUN segment</I>"
            + "</HTML>";
    
    private void setupToolTips() {
        
    }
    
    private void setSpecificComponentsEnabled(int cvType) {
        switch(cvType) {
            case CsTurboPlaylistItem.CV_BASIC:
            case CsTurboPlaylistItem.CV_DATA:
            case CsTurboPlaylistItem.CV_BINTURBO:
            case CsTurboPlaylistItem.CV_MONOLITHIC: {
                jtfType.setEnabled(true);
                jlbType.setEnabled(true);
                jtfName.setEnabled(true);
                jlbName.setEnabled(true);
                jtfLoad.setEnabled(true);
                jlbLoad.setEnabled(true);
                jtfRun.setEnabled(true);
                jlbRun.setEnabled(true);
                jtfLength.setEnabled(true);
                jlbLength.setEnabled(true);
                pBinaryFile.setEnabled(false);

                
                break;
            }
            case CsTurboPlaylistItem.CV_SEGMENTED_CHAIN:
            case CsTurboPlaylistItem.CV_SEGMENTED_BLOCK: {
                jtfType.setEnabled(false);
                jlbType.setEnabled(false);
                jtfName.setEnabled(true);
                jlbName.setEnabled(true);
                jtfLoad.setEnabled(false);
                jlbLoad.setEnabled(false);
                jtfRun.setEnabled(false);
                jlbRun.setEnabled(false);
                jtfLength.setEnabled(false);
                jlbLength.setEnabled(false);
                pBinaryFile.setEnabled(true);
                break;
            }
        }
    }
    
    private void suckHeaderElements(String[] elements) {
        
        if (jtfName.isEnabled()) jtfName.setText(elements[0]);
        if (jtfType.isEnabled()) jtfType.setText(elements[1]);
        if (jtfLoad.isEnabled()) jtfLoad.setText(elements[2]);
        if (jtfLength.isEnabled())  jtfLength.setText(elements[3]);
        if (jtfRun.isEnabled())jtfRun.setText(elements[4]);
        
    }

}


   



