/**
 * ***********************************************************************************************
 *@author  D-Logic team
 *@version 2.1
 * 
 **************************************************************************************************
 */
package uFCoderSimple;

import com.sun.jna.ptr.ByteByReference;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.ShortByReference;
import java.awt.Color;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.concurrent.*;
import java.util.regex.Pattern;
import javax.swing.*;

/**
 *
 * @author D-Logic
 * @version 2.0
 */
public class UFCoderSimple extends javax.swing.JFrame implements KeyListener, FocusListener {

    final static String ANY_DATA = "You must enter any data !",
            APPROPRIATE_FORMAT = "Enter a number between 0 and 255 decimal or 0 and FF hexadecimal !",
            NEW_CARD_KEY_A = "txtNewCardKeyA",
            NEW_CARD_KEY_B = "txtNewCardKeyB",
            NEW_READER_KEY = "txtNewReaderKey";

    private final static byte MIFARE_AUTHENT1A = 0x60,
            MIFARE_AUTHENT1B = 0x61,
            KEY_INDEX = 0;

    final static byte MAX_SECTORS_1k = 16,
            MAX_SECTORS_4k = 40;

    private static final int FUNCT_LIGHT_OK = 4,
            FUNCT_SOUND_OK = 0,//4 Tripple sound
            FUNCT_LIGHT_ERROR = 2,
            FUNCT_SOUND_ERROR = 0,//2 Long sound
            DL_OK = 0,
            SLEEP_VALUE = 500;

    private boolean boCONN = false;

    ByteByReference bDLCardType = new ByteByReference();
    ByteByReference bCardType = new ByteByReference();
    ByteByReference bCardUIDSize = new ByteByReference();
    IntByReference iCardSerial = new IntByReference();

    int[] iaReaderType = new int[1];
    int[] iaReaderSerial = new int[3];
    byte[] baCardUID = new byte[9];

    int iRResult, iCResult;
    byte bTypeOfCard;

    private String[] iaErrorCodes = new String[200];

    Declarations.ERRORCODES EC;
    Declarations.ERRORCODES[] ERRCODES = Declarations.ERRORCODES.values();

    ExecutorService exec = Executors.newCachedThreadPool();

    private void FillEcArray() {
        for (Declarations.ERRORCODES ECC : EC.values()) {
            iaErrorCodes[ECC.getValue()] = ECC.name();
        }
    }

    private boolean bFunctionStart;

    public void SetFunct(boolean bValue) {
        bFunctionStart = bValue;
    }

    public boolean GetFunct() {
        return bFunctionStart;
    }

    private boolean bLoopStart;

    public void SetLoop(boolean bValue) {
        bLoopStart = bValue;
    }

    public boolean GetLoop() {
        return bLoopStart;
    }

    private void SetStatusBar(JTextField txtErrorCode, JTextField txtErrorExplain, int iResultValue) {

        txtErrorCode.setText("0x" + Integer.toHexString(iResultValue).toUpperCase());
        txtErrorExplain.setText(iaErrorCodes[iResultValue]);
    }

    private JTextField TB = new JTextField();

    public UFCoderSimple() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btngAUTH1AB = new javax.swing.ButtonGroup();
        pnlReader = new javax.swing.JPanel();
        lblReaderType = new javax.swing.JLabel();
        txtReaderType = new javax.swing.JTextField();
        lblReaderSerial = new javax.swing.JLabel();
        txtReaderSerial = new javax.swing.JTextField();
        lblCardSerial = new javax.swing.JLabel();
        txtCardSerial = new javax.swing.JTextField();
        lblLightMode = new javax.swing.JLabel();
        cboLightMode = new javax.swing.JComboBox();
        lblSoundMode = new javax.swing.JLabel();
        cboSoundMode = new javax.swing.JComboBox();
        btnReaderUISignal = new javax.swing.JButton();
        lblCardType = new javax.swing.JLabel();
        lblCardUID = new javax.swing.JLabel();
        txtCardType = new javax.swing.JTextField();
        txtCardUIDSize = new javax.swing.JTextField();
        pnlAUTH1AB = new javax.swing.JPanel();
        rbAUTH1A = new javax.swing.JRadioButton();
        rbAUTH1B = new javax.swing.JRadioButton();
        pnlFormat = new javax.swing.JPanel();
        pgCardReaderKeys = new javax.swing.JTabbedPane();
        pnlNewCardKeys = new javax.swing.JPanel();
        lblKeyA = new javax.swing.JLabel();
        lblKeyB = new javax.swing.JLabel();
        chkNewCardKeys = new javax.swing.JCheckBox();
        btnFormatCardKeys = new javax.swing.JButton();
        lblSectorsFormatted = new javax.swing.JLabel();
        txtSectorsFormatted = new javax.swing.JTextField();
        pnlNewReaderKey = new javax.swing.JPanel();
        chkNewReaderKey = new javax.swing.JCheckBox();
        btnFormatReaderKey = new javax.swing.JButton();
        lblKeyIndex = new javax.swing.JLabel();
        txtKeyIndex = new javax.swing.JLabel();
        pgLinearReadWrite = new javax.swing.JTabbedPane();
        tabLinearRead = new javax.swing.JPanel();
        lblReadData = new javax.swing.JLabel();
        lblLRLinearAddress = new javax.swing.JLabel();
        txtLRLinearAddress = new javax.swing.JTextField();
        lblLRDataLength = new javax.swing.JLabel();
        txtLRDataLength = new javax.swing.JTextField();
        lblBytesRead = new javax.swing.JLabel();
        txtBytesRead = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtLinearRead = new javax.swing.JTextArea();
        btnLinearRead = new javax.swing.JButton();
        tabLinearWrite = new javax.swing.JPanel();
        lblWriteData = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtWriteData = new javax.swing.JTextArea();
        lblLWLinearAddress = new javax.swing.JLabel();
        txtLWLinearAddress = new javax.swing.JTextField();
        lblLWDataLength = new javax.swing.JLabel();
        txtLWDataLength = new javax.swing.JTextField();
        lblBytesWritten = new javax.swing.JLabel();
        txtBytesWritten = new javax.swing.JTextField();
        btnLinearWrite = new javax.swing.JButton();
        txtFErrorExplain = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        txtFErrorCode = new javax.swing.JTextField();
        txtConnection = new javax.swing.JTextField();
        txtRErrorCode = new javax.swing.JTextField();
        txtRErrorExplain = new javax.swing.JTextField();
        jTextField7 = new javax.swing.JTextField();
        txtCErrorCode = new javax.swing.JTextField();
        txtCErrorExplain = new javax.swing.JTextField();
        mnuMeni = new javax.swing.JMenuBar();
        mnuFileItem = new javax.swing.JMenu();
        mnuExitItems = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("uFCoderSimple");
        setLocationByPlatform(true);
        setName("frmUFRSimple"); // NOI18N
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        pnlReader.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        pnlReader.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        lblReaderType.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        lblReaderType.setText("Reader Type");
        pnlReader.add(lblReaderType, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 10, -1, -1));

        txtReaderType.setEditable(false);
        txtReaderType.setBackground(new java.awt.Color(255, 255, 255));
        txtReaderType.setFont(new java.awt.Font("Verdana", 1, 11)); // NOI18N
        txtReaderType.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtReaderType.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        txtReaderType.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtReaderTypeActionPerformed(evt);
            }
        });
        pnlReader.add(txtReaderType, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 10, 100, 20));

        lblReaderSerial.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        lblReaderSerial.setText("Reader Serial");
        pnlReader.add(lblReaderSerial, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 40, -1, -1));

        txtReaderSerial.setEditable(false);
        txtReaderSerial.setBackground(new java.awt.Color(255, 255, 255));
        txtReaderSerial.setFont(new java.awt.Font("Verdana", 1, 11)); // NOI18N
        txtReaderSerial.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtReaderSerial.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        pnlReader.add(txtReaderSerial, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 40, 100, 20));

        lblCardSerial.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        lblCardSerial.setText("Card Serial");
        pnlReader.add(lblCardSerial, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 40, -1, -1));

        txtCardSerial.setEditable(false);
        txtCardSerial.setBackground(new java.awt.Color(255, 255, 255));
        txtCardSerial.setFont(new java.awt.Font("Verdana", 1, 11)); // NOI18N
        txtCardSerial.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtCardSerial.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        pnlReader.add(txtCardSerial, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 40, 150, 20));

        lblLightMode.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        lblLightMode.setText("Light Mode");
        pnlReader.add(lblLightMode, new org.netbeans.lib.awtextra.AbsoluteConstraints(45, 73, -1, -1));

        cboLightMode.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        cboLightMode.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "None", "Long Green", "Long Red", "Alternation", "Flash" }));
        cboLightMode.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        pnlReader.add(cboLightMode, new org.netbeans.lib.awtextra.AbsoluteConstraints(142, 69, 123, -1));

        lblSoundMode.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        lblSoundMode.setText("Sound Mode");
        pnlReader.add(lblSoundMode, new org.netbeans.lib.awtextra.AbsoluteConstraints(45, 102, -1, -1));

        cboSoundMode.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        cboSoundMode.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "None", "Short", "Long", "Double Short", "Tripple Short", "Tripplet Melody" }));
        cboSoundMode.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        pnlReader.add(cboSoundMode, new org.netbeans.lib.awtextra.AbsoluteConstraints(142, 98, 123, -1));

        btnReaderUISignal.setText("READER UI SIGNAL");
        btnReaderUISignal.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        btnReaderUISignal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnReaderUISignalActionPerformed(evt);
            }
        });
        pnlReader.add(btnReaderUISignal, new org.netbeans.lib.awtextra.AbsoluteConstraints(304, 70, 190, 51));

        lblCardType.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        lblCardType.setText("Card Type");
        pnlReader.add(lblCardType, new org.netbeans.lib.awtextra.AbsoluteConstraints(300, 10, -1, -1));

        lblCardUID.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        lblCardUID.setText("UID Size");
        pnlReader.add(lblCardUID, new org.netbeans.lib.awtextra.AbsoluteConstraints(420, 10, -1, 20));

        txtCardType.setEditable(false);
        txtCardType.setBackground(new java.awt.Color(255, 255, 255));
        txtCardType.setFont(new java.awt.Font("Verdana", 1, 11)); // NOI18N
        txtCardType.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtCardType.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        pnlReader.add(txtCardType, new org.netbeans.lib.awtextra.AbsoluteConstraints(370, 10, 40, 20));

        txtCardUIDSize.setEditable(false);
        txtCardUIDSize.setBackground(new java.awt.Color(255, 255, 255));
        txtCardUIDSize.setFont(new java.awt.Font("Verdana", 1, 11)); // NOI18N
        txtCardUIDSize.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtCardUIDSize.setToolTipText("");
        txtCardUIDSize.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        pnlReader.add(txtCardUIDSize, new org.netbeans.lib.awtextra.AbsoluteConstraints(480, 10, 40, 20));

        getContentPane().add(pnlReader, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 539, 128));

        pnlAUTH1AB.setBackground(new java.awt.Color(255, 255, 255));
        pnlAUTH1AB.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        pnlAUTH1AB.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        rbAUTH1A.setBackground(new java.awt.Color(255, 255, 255));
        btngAUTH1AB.add(rbAUTH1A);
        rbAUTH1A.setFont(new java.awt.Font("Verdana", 1, 11)); // NOI18N
        rbAUTH1A.setSelected(true);
        rbAUTH1A.setText("AUTH 1A");
        rbAUTH1A.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        pnlAUTH1AB.add(rbAUTH1A, new org.netbeans.lib.awtextra.AbsoluteConstraints(102, 1, -1, -1));

        rbAUTH1B.setBackground(new java.awt.Color(255, 255, 255));
        btngAUTH1AB.add(rbAUTH1B);
        rbAUTH1B.setFont(new java.awt.Font("Verdana", 1, 11)); // NOI18N
        rbAUTH1B.setText("AUTH 1B");
        rbAUTH1B.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        pnlAUTH1AB.add(rbAUTH1B, new org.netbeans.lib.awtextra.AbsoluteConstraints(329, 1, -1, -1));

        getContentPane().add(pnlAUTH1AB, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 154, 539, 30));

        pnlFormat.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        pnlFormat.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        pgCardReaderKeys.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N

        lblKeyA.setText("Key A");

        lblKeyB.setText("Key B");

        chkNewCardKeys.setText("Hex");
        chkNewCardKeys.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                chkNewCardKeysItemStateChanged(evt);
            }
        });

        btnFormatCardKeys.setText("FORMAT CARD KEYS");
        btnFormatCardKeys.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFormatCardKeysActionPerformed(evt);
            }
        });

        lblSectorsFormatted.setText("Sectors Formatted");

        txtSectorsFormatted.setEditable(false);
        txtSectorsFormatted.setBackground(new java.awt.Color(204, 204, 204));
        txtSectorsFormatted.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        txtSectorsFormatted.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtSectorsFormatted.setAutoscrolls(false);
        txtSectorsFormatted.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        txtSectorsFormatted.setPreferredSize(new java.awt.Dimension(5, 16));

        javax.swing.GroupLayout pnlNewCardKeysLayout = new javax.swing.GroupLayout(pnlNewCardKeys);
        pnlNewCardKeys.setLayout(pnlNewCardKeysLayout);
        pnlNewCardKeysLayout.setHorizontalGroup(
            pnlNewCardKeysLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlNewCardKeysLayout.createSequentialGroup()
                .addGap(45, 45, 45)
                .addGroup(pnlNewCardKeysLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblKeyA)
                    .addComponent(lblKeyB))
                .addGap(253, 253, 253)
                .addComponent(btnFormatCardKeys, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(pnlNewCardKeysLayout.createSequentialGroup()
                .addGap(85, 85, 85)
                .addComponent(chkNewCardKeys)
                .addGap(196, 196, 196)
                .addComponent(lblSectorsFormatted)
                .addGap(39, 39, 39)
                .addComponent(txtSectorsFormatted, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        pnlNewCardKeysLayout.setVerticalGroup(
            pnlNewCardKeysLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlNewCardKeysLayout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addGroup(pnlNewCardKeysLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlNewCardKeysLayout.createSequentialGroup()
                        .addComponent(lblKeyA)
                        .addGap(18, 18, 18)
                        .addComponent(lblKeyB))
                    .addComponent(btnFormatCardKeys, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(pnlNewCardKeysLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(chkNewCardKeys, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(pnlNewCardKeysLayout.createSequentialGroup()
                        .addGap(4, 4, 4)
                        .addComponent(lblSectorsFormatted))
                    .addComponent(txtSectorsFormatted, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        pgCardReaderKeys.addTab("New Card Keys", pnlNewCardKeys);

        chkNewReaderKey.setText("Hex");
        chkNewReaderKey.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                chkNewReaderKeyItemStateChanged(evt);
            }
        });

        btnFormatReaderKey.setText("FORMAT READER KEY");
        btnFormatReaderKey.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFormatReaderKeyActionPerformed(evt);
            }
        });

        lblKeyIndex.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        lblKeyIndex.setText("Key Index");

        txtKeyIndex.setBackground(new java.awt.Color(255, 255, 255));
        txtKeyIndex.setFont(new java.awt.Font("Verdana", 1, 20)); // NOI18N
        txtKeyIndex.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        txtKeyIndex.setText("0");
        txtKeyIndex.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        txtKeyIndex.setEnabled(false);

        javax.swing.GroupLayout pnlNewReaderKeyLayout = new javax.swing.GroupLayout(pnlNewReaderKey);
        pnlNewReaderKey.setLayout(pnlNewReaderKeyLayout);
        pnlNewReaderKeyLayout.setHorizontalGroup(
            pnlNewReaderKeyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlNewReaderKeyLayout.createSequentialGroup()
                .addContainerGap(87, Short.MAX_VALUE)
                .addComponent(chkNewReaderKey)
                .addGap(58, 58, 58)
                .addComponent(lblKeyIndex)
                .addGap(17, 17, 17)
                .addComponent(txtKeyIndex, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnFormatReaderKey, javax.swing.GroupLayout.PREFERRED_SIZE, 174, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(28, 28, 28))
        );
        pnlNewReaderKeyLayout.setVerticalGroup(
            pnlNewReaderKeyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlNewReaderKeyLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(pnlNewReaderKeyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlNewReaderKeyLayout.createSequentialGroup()
                        .addGroup(pnlNewReaderKeyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(chkNewReaderKey)
                            .addComponent(lblKeyIndex))
                        .addGap(16, 16, 16))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlNewReaderKeyLayout.createSequentialGroup()
                        .addComponent(txtKeyIndex, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
            .addGroup(pnlNewReaderKeyLayout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(btnFormatReaderKey, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 12, Short.MAX_VALUE))
        );

        pgCardReaderKeys.addTab("New Reader Key", pnlNewReaderKey);

        pnlFormat.add(pgCardReaderKeys, new org.netbeans.lib.awtextra.AbsoluteConstraints(12, 13, -1, 110));

        getContentPane().add(pnlFormat, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 190, 539, 140));

        pgLinearReadWrite.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        pgLinearReadWrite.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N

        tabLinearRead.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        lblReadData.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        lblReadData.setText("Read Data");
        tabLinearRead.add(lblReadData, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 11, -1, -1));

        lblLRLinearAddress.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        lblLRLinearAddress.setText("Linear Address");
        tabLinearRead.add(lblLRLinearAddress, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 158, -1, -1));

        txtLRLinearAddress.setFont(new java.awt.Font("Verdana", 1, 11)); // NOI18N
        txtLRLinearAddress.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtLRLinearAddress.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        tabLinearRead.add(txtLRLinearAddress, new org.netbeans.lib.awtextra.AbsoluteConstraints(105, 155, 50, 21));

        lblLRDataLength.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        lblLRDataLength.setText("Data Length");
        tabLinearRead.add(lblLRDataLength, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 185, -1, -1));

        txtLRDataLength.setFont(new java.awt.Font("Verdana", 1, 11)); // NOI18N
        txtLRDataLength.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtLRDataLength.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        tabLinearRead.add(txtLRDataLength, new org.netbeans.lib.awtextra.AbsoluteConstraints(105, 182, 50, 21));

        lblBytesRead.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        lblBytesRead.setText("Bytes Read");
        tabLinearRead.add(lblBytesRead, new org.netbeans.lib.awtextra.AbsoluteConstraints(176, 172, -1, -1));

        txtBytesRead.setEditable(false);
        txtBytesRead.setBackground(new java.awt.Color(204, 204, 204));
        txtBytesRead.setFont(new java.awt.Font("Verdana", 1, 11)); // NOI18N
        txtBytesRead.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtBytesRead.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        tabLinearRead.add(txtBytesRead, new org.netbeans.lib.awtextra.AbsoluteConstraints(251, 166, 50, 28));

        txtLinearRead.setEditable(false);
        txtLinearRead.setColumns(20);
        txtLinearRead.setFont(new java.awt.Font("Verdana", 1, 11)); // NOI18N
        txtLinearRead.setLineWrap(true);
        txtLinearRead.setRows(5);
        txtLinearRead.setWrapStyleWord(true);
        txtLinearRead.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jScrollPane1.setViewportView(txtLinearRead);

        tabLinearRead.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 32, 510, 113));

        btnLinearRead.setText("LINEAR READ");
        btnLinearRead.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLinearReadActionPerformed(evt);
            }
        });
        tabLinearRead.add(btnLinearRead, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 160, 170, 40));

        pgLinearReadWrite.addTab("Linear Read", tabLinearRead);

        tabLinearWrite.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        lblWriteData.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        lblWriteData.setText("Write Data");
        tabLinearWrite.add(lblWriteData, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 11, -1, -1));

        txtWriteData.setColumns(20);
        txtWriteData.setFont(new java.awt.Font("Verdana", 1, 11)); // NOI18N
        txtWriteData.setLineWrap(true);
        txtWriteData.setRows(5);
        txtWriteData.setWrapStyleWord(true);
        txtWriteData.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        txtWriteData.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtWriteDataKeyTyped(evt);
            }
        });
        jScrollPane2.setViewportView(txtWriteData);

        tabLinearWrite.add(jScrollPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 32, 512, 112));

        lblLWLinearAddress.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        lblLWLinearAddress.setText("Linear Address");
        tabLinearWrite.add(lblLWLinearAddress, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 153, -1, -1));

        txtLWLinearAddress.setFont(new java.awt.Font("Verdana", 1, 11)); // NOI18N
        txtLWLinearAddress.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtLWLinearAddress.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        tabLinearWrite.add(txtLWLinearAddress, new org.netbeans.lib.awtextra.AbsoluteConstraints(105, 150, 50, 21));

        lblLWDataLength.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        lblLWDataLength.setText("Data Length");
        tabLinearWrite.add(lblLWDataLength, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 180, -1, -1));

        txtLWDataLength.setFont(new java.awt.Font("Verdana", 1, 11)); // NOI18N
        txtLWDataLength.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtLWDataLength.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        tabLinearWrite.add(txtLWDataLength, new org.netbeans.lib.awtextra.AbsoluteConstraints(105, 177, 50, 21));

        lblBytesWritten.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        lblBytesWritten.setText("Bytes Written");
        tabLinearWrite.add(lblBytesWritten, new org.netbeans.lib.awtextra.AbsoluteConstraints(178, 170, -1, -1));

        txtBytesWritten.setEditable(false);
        txtBytesWritten.setBackground(new java.awt.Color(204, 204, 204));
        txtBytesWritten.setFont(new java.awt.Font("Verdana", 1, 11)); // NOI18N
        txtBytesWritten.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtBytesWritten.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        tabLinearWrite.add(txtBytesWritten, new org.netbeans.lib.awtextra.AbsoluteConstraints(259, 164, 50, 28));

        btnLinearWrite.setText("LINEAR WRITE");
        btnLinearWrite.setPreferredSize(new java.awt.Dimension(99, 23));
        btnLinearWrite.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLinearWriteActionPerformed(evt);
            }
        });
        tabLinearWrite.add(btnLinearWrite, new org.netbeans.lib.awtextra.AbsoluteConstraints(344, 158, 178, 41));

        pgLinearReadWrite.addTab("Linear Write", tabLinearWrite);

        getContentPane().add(pgLinearReadWrite, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 345, 539, 255));

        txtFErrorExplain.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        txtFErrorExplain.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        getContentPane().add(txtFErrorExplain, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 610, 330, -1));

        jTextField2.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jTextField2.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jTextField2.setText("Function Error");
        getContentPane().add(jTextField2, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 610, 140, -1));

        txtFErrorCode.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        txtFErrorCode.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        getContentPane().add(txtFErrorCode, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 610, 70, -1));

        txtConnection.setBackground(new java.awt.Color(226, 218, 218));
        txtConnection.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        txtConnection.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        getContentPane().add(txtConnection, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 130, 140, -1));

        txtRErrorCode.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        txtRErrorCode.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        getContentPane().add(txtRErrorCode, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 130, 70, -1));

        txtRErrorExplain.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        txtRErrorExplain.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        getContentPane().add(txtRErrorExplain, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 130, 330, -1));

        jTextField7.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        jTextField7.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        jTextField7.setText("CARD STATUS");
        getContentPane().add(jTextField7, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 630, 140, -1));

        txtCErrorCode.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        txtCErrorCode.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        getContentPane().add(txtCErrorCode, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 630, 70, -1));

        txtCErrorExplain.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        txtCErrorExplain.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        getContentPane().add(txtCErrorExplain, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 630, 330, -1));

        mnuMeni.setBackground(new java.awt.Color(255, 255, 255));
        mnuMeni.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        mnuMeni.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        mnuMeni.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N

        mnuFileItem.setText("File");
        mnuFileItem.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N

        mnuExitItems.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.CTRL_MASK));
        mnuExitItems.setBackground(new java.awt.Color(255, 255, 255));
        mnuExitItems.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        mnuExitItems.setText("Exit");
        mnuExitItems.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        mnuExitItems.setFocusable(true);
        mnuExitItems.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuExitItemsActionPerformed(evt);
            }
        });
        mnuFileItem.add(mnuExitItems);

        mnuMeni.add(mnuFileItem);

        setJMenuBar(mnuMeni);

        getAccessibleContext().setAccessibleName("uFRSimple");

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

    public void CreateKey(int iKeyX, int iKeyY, int iKeyHeight, int iKeyWidth, String sKeyName, JPanel pnlContainer) {
        JTextField txtTextField;
        for (int br = 0; br < 6; br++) {
            txtTextField = new JTextField();
            txtTextField.setBounds(iKeyX + (iKeyWidth * br + 2), iKeyY, 30, 21);
            txtTextField.setBorder(javax.swing.BorderFactory.createLineBorder(Color.BLACK));
            txtTextField.setHorizontalAlignment(javax.swing.JTextField.CENTER);
            txtTextField.setFont(new java.awt.Font("Verdana", 1, 11));
            txtTextField.setName(sKeyName);
            txtTextField.addKeyListener(this);
            txtTextField.addFocusListener(this);
            txtTextField.setText("255");
            txtTextField.setVisible(true);
            pnlContainer.add(txtTextField);
        }
        pnlContainer.repaint();
    }

    @Override
    public void keyTyped(KeyEvent e) {
        boolean bPattern = false;
        TB = (JTextField) e.getSource();

        try {
            if ((chkNewCardKeys.isSelected() && TB.getName() == NEW_CARD_KEY_A)
                    || (chkNewCardKeys.isSelected() && TB.getName() == NEW_CARD_KEY_B)
                    || chkNewReaderKey.isSelected() && TB.getName() == NEW_READER_KEY) {

                if (TB.getText().length() > 1) {
                    e.consume();
                } else {
                    bPattern = Pattern.matches("[a-fA-F_0-9]", String.valueOf(e.getKeyChar()));
                    if (!bPattern) {
                        e.consume();
                    }
                }
            } else {
                if (TB.getText().length() > 2) {
                    e.consume();
                } else {
                    bPattern = Pattern.matches("[0-9]", String.valueOf(e.getKeyChar()));
                    if (!bPattern) {
                        e.consume();
                    }
                }
            }
        } catch (NumberFormatException NFE) {

        }
    }

    @Override
    public void keyPressed(KeyEvent e) {

    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void focusGained(FocusEvent e) {

    }

    @Override
    public void focusLost(FocusEvent e) {
        TB = (JTextField) e.getSource();
        if (TB.getText().trim().isEmpty()) {
            return;
        }

        if (Short.parseShort(TB.getText().trim(), 10) > 255) {
            TB.requestFocus();
            TB.selectAll();
            JOptionPane.showMessageDialog(rootPane, "You must enter a number between 0 and 255 !");
        }
    }

    /**
     * MainThread for loop
     */
    private void MainThread() {

        SetLoop(true);
        String sBuffer = "";
        if (!boCONN) {
            if ((iRResult = Declarations.UFRSimpleFunction.uFrINSTANCE.ReaderOpen()) == DL_OK) {
                txtConnection.setText("CONNECTED");
                boCONN = true;
                SetStatusBar(txtRErrorCode, txtRErrorExplain, iRResult);
            } else {
                txtConnection.setText("NOT CONNECTED");
                txtReaderType.setText(null);
                txtReaderSerial.setText(null);
                txtCardType.setText(null);
                txtCardSerial.setText(null);
                txtCardUIDSize.setText(null);
                SetStatusBar(txtRErrorCode, txtRErrorExplain, iRResult);
            }
        }
        if (boCONN) {
            if ((iRResult = Declarations.UFRSimpleFunction.uFrINSTANCE.GetReaderType(iaReaderType)) == DL_OK) {
                txtReaderType.setText("0x" + Integer.toHexString(iaReaderType[0]).toUpperCase());

                if ((iRResult = Declarations.UFRSimpleFunction.uFrINSTANCE.GetReaderSerialNumber(iaReaderSerial)) == DL_OK) {
                    txtReaderSerial.setText("0x" + Integer.toHexString(iaReaderSerial[0]).toUpperCase());
                    SetStatusBar(txtRErrorCode, txtRErrorExplain, iRResult);
                }
                iCResult = Declarations.UFRSimpleFunction.uFrINSTANCE.GetDlogicCardType(bDLCardType);

                if (iCResult == DL_OK) {
                    btnFormatCardKeys.setEnabled(bDLCardType.getValue()>0x0A);
                    Declarations.UFRSimpleFunction.uFrINSTANCE.GetCardIdEx(bCardType, baCardUID, bCardUIDSize);

                    for (int iBr = 0; iBr < bCardUIDSize.getValue(); iBr++) {
                        sBuffer += Integer.toHexString((((char) baCardUID[iBr] & 0xFF)));
                    }
                    txtCardType.setText("0x" + Integer.toHexString(bDLCardType.getValue()).toUpperCase());
                    txtCardUIDSize.setText("0x" + Integer.toHexString(bCardUIDSize.getValue()).toUpperCase());
                    txtCardSerial.setText("0x" + sBuffer.toUpperCase());
                    SetStatusBar(txtCErrorCode, txtCErrorExplain, iCResult);
                } else {
                    txtCardType.setText(null);
                    txtCardSerial.setText(null);
                    txtCardUIDSize.setText(null);
                    btnFormatCardKeys.setEnabled(true);
                    SetStatusBar(txtCErrorCode, txtCErrorExplain, iCResult);
                }
            } else {
                Declarations.UFRSimpleFunction.uFrINSTANCE.ReaderClose();
                boCONN = false;
            }
        }
        SetLoop(false);
    }

    class ThreadMain implements Runnable {

        @Override
        public void run() {
            try {
                while (!Thread.interrupted()) {
                    if (!GetFunct()) {
                        MainThread();
                    }
                    TimeUnit.MILLISECONDS.sleep(SLEEP_VALUE);
                }
            } catch (InterruptedException ex) {

            }
        }
    }

    private void mnuExitItemsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuExitItemsActionPerformed
        exec.shutdown();
        System.exit(0);
    }//GEN-LAST:event_mnuExitItemsActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened

        CreateKey(88, 9, 31, 29, NEW_CARD_KEY_A, pnlNewCardKeys);
        CreateKey(88, 35, 31, 29, NEW_CARD_KEY_B, pnlNewCardKeys);
        CreateKey(88, 15, 31, 29, NEW_READER_KEY, pnlNewReaderKey);
        //bKeyIndex=Byte.parseByte(txtKeyIndex.getText(),10);
        FillEcArray();
        exec.execute(new ThreadMain());
    }//GEN-LAST:event_formWindowOpened

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        exec.shutdown();
        System.exit(0);
    }//GEN-LAST:event_formWindowClosing

    private void btnReaderUISignalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnReaderUISignalActionPerformed
        if (GetFunct() || GetLoop()) {
            return;
        }
        try {
            SetFunct(true);
            int iFResult = 0;
            iFResult = Declarations.UFRSimpleFunction.uFrINSTANCE.ReaderUISignal(cboLightMode.getSelectedIndex(), cboSoundMode.getSelectedIndex());
        } finally {
            SetFunct(false);
        }
    }//GEN-LAST:event_btnReaderUISignalActionPerformed

    private void btnFormatCardKeysActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFormatCardKeysActionPerformed
        if (GetFunct() || GetLoop()) {
            return;
        }
        try {
            SetFunct(true);
            int iFResult = 0;

            byte bAuthMode = 0,
                    bBlockAccessBits = 0,
                    bSectorAccessBits = 1,
                    bSectorTrailersByte9 = 45;

            ByteByReference bSectorsFormatted = new ByteByReference();
            byte[] baKeyA = new byte[5];
            byte[] baKeyB = new byte[5];

            bAuthMode = (byte) (rbAUTH1A.isSelected() ? MIFARE_AUTHENT1A : MIFARE_AUTHENT1B);
            baKeyA = new GLFunctions().DecHexConversionForKey(pnlNewCardKeys, NEW_CARD_KEY_A, chkNewCardKeys);
            baKeyB = new GLFunctions().DecHexConversionForKey(pnlNewCardKeys, NEW_CARD_KEY_B, chkNewCardKeys);

            iFResult = Declarations.UFRSimpleFunction.uFrINSTANCE.LinearFormatCard(baKeyA, bBlockAccessBits, bSectorAccessBits,
                    bSectorTrailersByte9, baKeyB, bSectorsFormatted, bAuthMode, KEY_INDEX);

            if (iFResult == DL_OK) {
                Declarations.UFRSimpleFunction.uFrINSTANCE.ReaderUISignal(FUNCT_LIGHT_OK, FUNCT_SOUND_OK);
                SetStatusBar(txtFErrorCode, txtFErrorExplain, iFResult);
                txtSectorsFormatted.setText(Byte.toString(bSectorsFormatted.getValue()));
                JOptionPane.showMessageDialog(rootPane, "Card keys are formatted successfully !", "Information", JOptionPane.INFORMATION_MESSAGE);
            } else {
                Declarations.UFRSimpleFunction.uFrINSTANCE.ReaderUISignal(FUNCT_LIGHT_ERROR, FUNCT_SOUND_ERROR);
                SetStatusBar(txtFErrorCode, txtFErrorExplain, iFResult);
                txtSectorsFormatted.setText(Byte.toString(bSectorsFormatted.getValue()));
                JOptionPane.showMessageDialog(rootPane, "Card keys are not formatted successfully !", "Error", JOptionPane.ERROR_MESSAGE);
            }

        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(rootPane, APPROPRIATE_FORMAT, "Error", JOptionPane.ERROR_MESSAGE);
        } finally {
            SetFunct(false);
        }
    }//GEN-LAST:event_btnFormatCardKeysActionPerformed

    private void chkNewCardKeysItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_chkNewCardKeysItemStateChanged
        new GLFunctions().CheckBoxHexDec(pnlNewCardKeys, NEW_CARD_KEY_A, evt);
        new GLFunctions().CheckBoxHexDec(pnlNewCardKeys, NEW_CARD_KEY_B, evt);
    }//GEN-LAST:event_chkNewCardKeysItemStateChanged

    private void chkNewReaderKeyItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_chkNewReaderKeyItemStateChanged
        new GLFunctions().CheckBoxHexDec(pnlNewReaderKey, NEW_READER_KEY, evt);
    }//GEN-LAST:event_chkNewReaderKeyItemStateChanged

    private void btnFormatReaderKeyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFormatReaderKeyActionPerformed
        if (GetFunct() || GetLoop()) {
            return;
        }
        try {
            SetFunct(true);
            int iFResult = 0;

            byte[] baReaderKey = new byte[6];

            baReaderKey = new GLFunctions().DecHexConversionForKey(pnlNewReaderKey, NEW_READER_KEY, chkNewReaderKey);
            iFResult = Declarations.UFRSimpleFunction.uFrINSTANCE.ReaderKeyWrite(baReaderKey, KEY_INDEX);
            if (iFResult == DL_OK) {
                Declarations.UFRSimpleFunction.uFrINSTANCE.ReaderUISignal(FUNCT_LIGHT_OK, FUNCT_SOUND_OK);
                SetStatusBar(txtFErrorCode, txtFErrorExplain, iFResult);

                JOptionPane.showMessageDialog(rootPane, "Reader key is formatted successfully !", "Information", JOptionPane.INFORMATION_MESSAGE);
            } else {
                Declarations.UFRSimpleFunction.uFrINSTANCE.ReaderUISignal(FUNCT_LIGHT_ERROR, FUNCT_SOUND_ERROR);
                SetStatusBar(txtFErrorCode, txtFErrorExplain, iFResult);

                JOptionPane.showMessageDialog(rootPane, "Reader key is not formatted successfully !", "Error", JOptionPane.ERROR_MESSAGE);
            }

        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(rootPane, APPROPRIATE_FORMAT, "Error", JOptionPane.ERROR_MESSAGE);
        } finally {
            SetFunct(false);
        }

    }//GEN-LAST:event_btnFormatReaderKeyActionPerformed

    private void btnLinearReadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLinearReadActionPerformed
        if (GetFunct() || GetLoop()) {
            return;
        }
        try {
            SetFunct(true);
            int iFResult = 0;
            byte bAuthMode = (byte) (rbAUTH1A.isSelected() ? MIFARE_AUTHENT1A : MIFARE_AUTHENT1B);
            ShortByReference shBytesRet = new ShortByReference();

            if (txtLRLinearAddress.getText().trim().isEmpty()) {
                JOptionPane.showMessageDialog(rootPane, "You must enter LINEAR ADDRESS", "Warning", JOptionPane.WARNING_MESSAGE);
                txtLRLinearAddress.requestFocus();
                return;
            }
            if (txtLRDataLength.getText().trim().isEmpty()) {
                JOptionPane.showMessageDialog(rootPane, "You must enter DATA LENGTH", "Warning", JOptionPane.WARNING_MESSAGE);
                txtLRDataLength.requestFocus();
                return;
            }

            short shLinearAddress = Short.parseShort(txtLRLinearAddress.getText()),
                    shDataLength = Short.parseShort(txtLRDataLength.getText());
            byte[] baReadData = new byte[shDataLength];

            txtBytesRead.setText("");
            iFResult = Declarations.UFRSimpleFunction.uFrINSTANCE.LinearRead(baReadData, shLinearAddress, shDataLength, shBytesRet, bAuthMode, KEY_INDEX);

            if (iFResult == DL_OK) {
                txtLinearRead.setText(new String(baReadData, 0, shDataLength));
                txtBytesRead.setText(Short.toString(shBytesRet.getValue()));
                Declarations.UFRSimpleFunction.uFrINSTANCE.ReaderUISignal(FUNCT_LIGHT_OK, FUNCT_SOUND_OK);
                SetStatusBar(txtFErrorCode, txtFErrorExplain, iFResult);

            } else {
                txtLinearRead.setText(new String(baReadData, 0, shBytesRet.getValue()));
                txtBytesRead.setText(Short.toString(shBytesRet.getValue()));
                Declarations.UFRSimpleFunction.uFrINSTANCE.ReaderUISignal(FUNCT_LIGHT_ERROR, FUNCT_SOUND_ERROR);
                SetStatusBar(txtFErrorCode, txtFErrorExplain, iFResult);

            }
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(rootPane, APPROPRIATE_FORMAT, "Error !", JOptionPane.ERROR_MESSAGE);
        } finally {
            SetFunct(false);
        }
    }//GEN-LAST:event_btnLinearReadActionPerformed

    private void btnLinearWriteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLinearWriteActionPerformed
        if (GetFunct() || GetLoop()) {
            return;
        }
        try {
            SetFunct(true);
            int iFResult = 0;

            byte bAuthMode = (byte) (rbAUTH1A.isSelected() ? MIFARE_AUTHENT1A : MIFARE_AUTHENT1B);
            ShortByReference shBytesRet = new ShortByReference();

            if (txtWriteData.getText().trim().isEmpty()) {
                JOptionPane.showMessageDialog(rootPane, ANY_DATA, "Warning", JOptionPane.WARNING_MESSAGE);
                txtWriteData.requestFocus();
                return;
            }
            if (txtLWLinearAddress.getText().trim().isEmpty()) {
                JOptionPane.showMessageDialog(rootPane, "You must enter LINEAR ADDRESS", "Warning", JOptionPane.WARNING_MESSAGE);
                txtLWLinearAddress.requestFocus();
                return;
            }
            if (txtLWDataLength.getText().trim().isEmpty()) {
                JOptionPane.showMessageDialog(rootPane, "You must enter DATA LENGTH", "Warning", JOptionPane.WARNING_MESSAGE);
                txtLWDataLength.requestFocus();
                return;
            }

            short shLinearAddress = Short.parseShort(txtLWLinearAddress.getText()),
                    shDataLength = Short.parseShort(txtLWDataLength.getText());

            byte[] baWriteData = new byte[shDataLength];
            baWriteData = txtWriteData.getText().getBytes();

            txtBytesWritten.setText("");
            iFResult = Declarations.UFRSimpleFunction.uFrINSTANCE.LinearWrite(baWriteData, shLinearAddress, shDataLength, shBytesRet, bAuthMode, KEY_INDEX);

            if (iFResult == DL_OK) {
                txtBytesWritten.setText(Short.toString(shBytesRet.getValue()));
                Declarations.UFRSimpleFunction.uFrINSTANCE.ReaderUISignal(FUNCT_LIGHT_OK, FUNCT_SOUND_OK);
                SetStatusBar(txtFErrorCode, txtFErrorExplain, iFResult);

            } else {
                txtBytesWritten.setText(Short.toString(shBytesRet.getValue()));
                Declarations.UFRSimpleFunction.uFrINSTANCE.ReaderUISignal(FUNCT_LIGHT_ERROR, FUNCT_SOUND_ERROR);
                SetStatusBar(txtFErrorCode, txtFErrorExplain, iFResult);

            }
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(rootPane, APPROPRIATE_FORMAT, "Error !", JOptionPane.ERROR_MESSAGE);
        } finally {
            SetFunct(false);
        }
    }//GEN-LAST:event_btnLinearWriteActionPerformed

    private void txtWriteDataKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtWriteDataKeyTyped
        txtLWDataLength.setText(Integer.toString(txtWriteData.getText().length() + 1));
    }//GEN-LAST:event_txtWriteDataKeyTyped

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

    public static void main(String args[]) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(UFCoderSimple.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(UFCoderSimple.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(UFCoderSimple.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(UFCoderSimple.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                UFCoderSimple US = new UFCoderSimple();
                US.setLocationRelativeTo(null);
                US.setVisible(true);

            }
        });
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnFormatCardKeys;
    private javax.swing.JButton btnFormatReaderKey;
    private javax.swing.JButton btnLinearRead;
    private javax.swing.JButton btnLinearWrite;
    private javax.swing.JButton btnReaderUISignal;
    private javax.swing.ButtonGroup btngAUTH1AB;
    private javax.swing.JComboBox cboLightMode;
    private javax.swing.JComboBox cboSoundMode;
    private javax.swing.JCheckBox chkNewCardKeys;
    private javax.swing.JCheckBox chkNewReaderKey;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField7;
    private javax.swing.JLabel lblBytesRead;
    private javax.swing.JLabel lblBytesWritten;
    private javax.swing.JLabel lblCardSerial;
    private javax.swing.JLabel lblCardType;
    private javax.swing.JLabel lblCardUID;
    private javax.swing.JLabel lblKeyA;
    private javax.swing.JLabel lblKeyB;
    private javax.swing.JLabel lblKeyIndex;
    private javax.swing.JLabel lblLRDataLength;
    private javax.swing.JLabel lblLRLinearAddress;
    private javax.swing.JLabel lblLWDataLength;
    private javax.swing.JLabel lblLWLinearAddress;
    private javax.swing.JLabel lblLightMode;
    private javax.swing.JLabel lblReadData;
    private javax.swing.JLabel lblReaderSerial;
    private javax.swing.JLabel lblReaderType;
    private javax.swing.JLabel lblSectorsFormatted;
    private javax.swing.JLabel lblSoundMode;
    private javax.swing.JLabel lblWriteData;
    private javax.swing.JMenuItem mnuExitItems;
    private javax.swing.JMenu mnuFileItem;
    private javax.swing.JMenuBar mnuMeni;
    private javax.swing.JTabbedPane pgCardReaderKeys;
    private javax.swing.JTabbedPane pgLinearReadWrite;
    private javax.swing.JPanel pnlAUTH1AB;
    private javax.swing.JPanel pnlFormat;
    private javax.swing.JPanel pnlNewCardKeys;
    private javax.swing.JPanel pnlNewReaderKey;
    private javax.swing.JPanel pnlReader;
    private javax.swing.JRadioButton rbAUTH1A;
    private javax.swing.JRadioButton rbAUTH1B;
    private javax.swing.JPanel tabLinearRead;
    private javax.swing.JPanel tabLinearWrite;
    private javax.swing.JTextField txtBytesRead;
    private javax.swing.JTextField txtBytesWritten;
    private javax.swing.JTextField txtCErrorCode;
    private javax.swing.JTextField txtCErrorExplain;
    private javax.swing.JTextField txtCardSerial;
    private javax.swing.JTextField txtCardType;
    private javax.swing.JTextField txtCardUIDSize;
    private javax.swing.JTextField txtConnection;
    private javax.swing.JTextField txtFErrorCode;
    private javax.swing.JTextField txtFErrorExplain;
    private javax.swing.JLabel txtKeyIndex;
    private javax.swing.JTextField txtLRDataLength;
    private javax.swing.JTextField txtLRLinearAddress;
    private javax.swing.JTextField txtLWDataLength;
    private javax.swing.JTextField txtLWLinearAddress;
    private javax.swing.JTextArea txtLinearRead;
    private javax.swing.JTextField txtRErrorCode;
    private javax.swing.JTextField txtRErrorExplain;
    private javax.swing.JTextField txtReaderSerial;
    private javax.swing.JTextField txtReaderType;
    private javax.swing.JTextField txtSectorsFormatted;
    private javax.swing.JTextArea txtWriteData;
    // End of variables declaration//GEN-END:variables
}
