/**  
   @author                             D-Logic Team   
   @version                            2.1   
*/
package ufcodersimplest;

import com.sun.jna.ptr.ByteByReference;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.ShortByReference;
import java.util.concurrent.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JTextField;



 
    
/**
 * Main class 
 * 
 */

public class uFCoderSimplestApplet extends javax.swing.JApplet 
{
   
    final static byte MIFARE_AUTHENT1A       = 0x60,
                      MIFARE_AUTHENT1B       = 0x61,
                      KEY_INDEX              = 0,
                      MAX_BLOCK              = 16; 
      
    final static short 
                       FORMAT_SIGN            = 0xFF;
    final static 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;
    
    
   
     private int[] iaReaderType            =  new int[1];
     private byte[]  baCardUID             =  new byte[9];
     ByteByReference bDLCardType   =  new ByteByReference();
     ByteByReference bCardType     =  new ByteByReference();
     ByteByReference bCardUIDSize  =  new ByteByReference();
     IntByReference   iCardSerial  =  new IntByReference();    
     
    private int     iRResult;
    private int     iCResult;
    private int     iFResult;
    
    private Declarations.DLCARDTYPE DLT;
    private Declarations.MAX_BYTES MB;
    private Declarations.MAX_SECTORS_PAGES_VALUE MSP;
    
    
    private String[] iaErrorCodes  =  new String[200];
    
    Declarations.ERRORCODES   EC;
    Declarations.ERRORCODES[] ERRCODES = Declarations.ERRORCODES.values();
    
    ExecutorService exec = Executors.newCachedThreadPool();

    
   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 FillEcArray(){        
        for (Declarations.ERRORCODES ECC : EC.values()) {               
               iaErrorCodes[ECC.getValue()]=ECC.name();               
            }
    }
    
    private void SetStatusBar(JTextField txtErrorCode, JTextField txtErrorExplain, int iResultValue) {
        
                txtErrorCode.setText("0x" + Integer.toHexString(iResultValue).toUpperCase());
                txtErrorExplain.setText(iaErrorCodes[iResultValue]);           
    }
     
    
    //max block in card
     private int MaxBlock(ByteByReference bCardType)
     {
         int iResult=0;
                         
        if (bCardType.getValue() == DLT.DL_MIFARE_CLASSIC_1K.getValue()) {
            iResult = (MSP.MAX_SECTORS_1k.getValue() * 4);
        } else if (bCardType.getValue() == DLT.DL_MIFARE_CLASSIC_4K.getValue()) {
            iResult = ((MSP.MAX_SECTORS_1k.getValue() * 2) * 4) + ((MSP.MAX_SECTORS_1k.getValue() - 8) * 16);
        } else if (bCardType.getValue() == DLT.DL_MIFARE_ULTRALIGHT.getValue()) {
            iResult = MSP.MAX_PAGE_ULTRALIGHT.getValue();
        } else if (bCardType.getValue() == DLT.DL_MIFARE_ULTRALIGHT_C.getValue()) {
            iResult = MSP.MAX_PAGE_ULTRALIGHT_C.getValue();
        } else if (bCardType.getValue() == DLT.DL_NTAG_203.getValue()) {
            iResult = MSP.MAX_PAGE_NTAG203.getValue();
        }
        return iResult;
    } 
                     
       
        
    private int MaxBytes(int bCardType){
       short usMaxBytes = 0;
         if (bCardType == DLT.DL_NTAG_203.getValue())              
                usMaxBytes=(short) MB.MAX_BYTES_NTAG_203.getValue();
         else if (bCardType == DLT.DL_MIFARE_ULTRALIGHT.getValue())    
                 usMaxBytes = (short)MB.MAX_BYTES_ULTRALIGHT.getValue();
         else if (bCardType == DLT.DL_MIFARE_ULTRALIGHT_C.getValue())                 
                 usMaxBytes = (short) MB.MAX_BYTES_ULTRALIGHT_C.getValue();
         else if (bCardType == DLT.DL_MIFARE_CLASSIC_1K.getValue())             
                 usMaxBytes = (short) MB.MAX_BYTES_CLASSIC_1K.getValue();
         else if (bCardType == DLT.DL_MIFARE_CLASSIC_4K.getValue() || 
                  bCardType == DLT.DL_MIFARE_PLUS_S_4K.getValue())
                  usMaxBytes = (short) MB.MAX_BYTES_CLASSIC_4k.getValue();
                            
          return usMaxBytes;
    }
    
    private void MainLoop() {
               
        SetLoop(true);
        String sBuffer="";
        if (!boCONN) {
            iRResult = Declarations.uFrFunctions.UFRInstance.ReaderOpen();
            if (iRResult == 0) {
                boCONN = true;
                txtConnected.setText("CONNECTED");
                SetStatusBar(txtRCodeError, txtRErrorExplain, iRResult);
            } else {
                     txtConnected.setText("NOT CONNECTED");
                     SetStatusBar(txtRCodeError, txtRErrorExplain, iRResult);
            }            
        }
        
        if (boCONN) {
            iRResult = Declarations.uFrFunctions.UFRInstance.GetReaderType(iaReaderType);
            if (iRResult == DL_OK) {
                iCResult = Declarations.uFrFunctions.UFRInstance.GetDlogicCardType(bDLCardType);
                if (iCResult == DL_OK) {
                    btnFormat.setEnabled(bDLCardType.getValue()>0x0A);
                    iCResult = Declarations.uFrFunctions.UFRInstance.GetCardIdEx(bCardType, baCardUID, bCardUIDSize);
                    if (iCResult == DL_OK){
                        for (byte bBr=0;bBr<bCardUIDSize.getValue();bBr++){
                             sBuffer+=Integer.toHexString((((char)baCardUID[bBr] & 0xFF))).toUpperCase();
                         }             
                    }
                    txtCardType   .setText("0x" + Integer.toHexString(bDLCardType.getValue()).toUpperCase());                                                                     
                    txtCardUIDSize.setText("0x" + Integer.toHexString(bCardUIDSize.getValue()).toUpperCase());
                    txtCardUID    .setText("0x" + sBuffer);                    
                    SetStatusBar(txtCCodeError, txtCErrorExplain, iCResult);
                } else {                        
                        txtCardType   .setText(null);
                        txtCardUIDSize.setText(null);
                        txtCardUID    .setText(null); 
                        btnFormat.setEnabled(true);
                        SetStatusBar(txtCCodeError, txtCErrorExplain, iCResult);
                }
            } else {
                boCONN        = false;                                
                txtCardType   .setText(null);
                txtCardUIDSize.setText(null);
                txtCardUID    .setText(null);
                btnFormat.setEnabled(true);
                Declarations .uFrFunctions.UFRInstance.ReaderClose();
                SetStatusBar(txtRCodeError, txtRErrorExplain, iRResult);
            }
            
        }
               
        SetLoop(false);
    }
     
    
    
private class Main_Thread implements Runnable
{    
    public void run() {
        try {
            while (true) {
                if (!GetFunct())
                     MainLoop();
                     TimeUnit.MILLISECONDS.sleep(SLEEP_VALUE);
            }
        } catch (Exception InEx) {
            Logger.getLogger(uFCoderSimplestApplet.class.getName()).log(Level.SEVERE, null, InEx);
        }    
    }    
    }

class FormatCard extends Thread {
        
        @Override
        public void run() {             
                                                        
            try
          {                                
               SetFunct(true);                            
               short shBISCount     = 0;
               short shBlockCount   = 0;
               byte  bSectorCounter = 0;
               byte  bBISCounter    = 3;
               byte[]baBlockWrite   = new byte[MAX_BLOCK];
               int   iMaxBlock      = MaxBlock(bDLCardType);               
               
               pBar.setVisible(true);
               
               
               for(byte bCounter=0;bCounter<MAX_BLOCK;bCounter++)
               {
                   baBlockWrite[bCounter] = (((byte)FORMAT_SIGN));
               }
              
               pBar.setMaximum(iMaxBlock);
                                                                              
                        while (shBlockCount <iMaxBlock ) 
                        {
                            shBISCount=0;
                             while (shBISCount<bBISCounter)
                               {
                                 iFResult = Declarations.uFrFunctions.UFRInstance.BlockWrite(baBlockWrite, shBlockCount,MIFARE_AUTHENT1A,KEY_INDEX);                                
                                 shBISCount++;
                                 shBlockCount++;
                               }
                               if (iFResult !=DL_OK){                                   
                                   break;
                                 }
                                 shBlockCount++;
                                 pBar.setValue(shBlockCount);
                                                                   
                                 if (bSectorCounter >= 31 && shBlockCount % 16 == 0)
                                    {
                                       bSectorCounter++;
                                       bBISCounter = 15;
                                    }
                                    else
                                       bSectorCounter++; 
                              TimeUnit.MILLISECONDS.sleep(80);    
                         }    
                        
                                                                                                       
                    } catch (InterruptedException ex) {
                        Logger.getLogger(uFCoderSimplestApplet.class.getName()).log(Level.SEVERE, null, ex);
                    }
                 finally {  
                    SetStatusBar(txtFCodeError, txtFErrorExplain, iFResult);
                    SetFunct(false);
                    pBar.setVisible(false);                     
                }            
        }
    }

    @Override
    public void init() {
        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(uFCoderSimplestApplet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(uFCoderSimplestApplet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(uFCoderSimplestApplet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(uFCoderSimplestApplet.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        try {
            java.awt.EventQueue.invokeAndWait(new Runnable()
            {
                public void run() {                   
                    initComponents();
                    pBar.setVisible(false);                    
                }
            });
            
           }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
    }
    @Override
    public void start(){
       FillEcArray();
       exec.execute(new Main_Thread());
    }
    
    @Override
    public void stop()
    {        
       exec.shutdown();
    }
       
    @Override
    public void destroy(){
      exec.shutdown();
      
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlMain = new javax.swing.JPanel();
        lblCardType = new javax.swing.JLabel();
        btnFormat = new javax.swing.JButton();
        lblLinearWrite = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtLinearWrite = new javax.swing.JTextArea();
        btnWrite = new javax.swing.JButton();
        lblLinearRead = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtLinearRead = new javax.swing.JTextArea();
        btnRead = new javax.swing.JButton();
        txtCardType = new javax.swing.JTextField();
        lblCardUIDSize = new javax.swing.JLabel();
        txtCardUIDSize = new javax.swing.JTextField();
        txtRErrorExplain = new javax.swing.JTextField();
        txtConnected = new javax.swing.JTextField();
        txtRCodeError = new javax.swing.JTextField();
        txtCardStatus = new javax.swing.JTextField();
        txtCCodeError = new javax.swing.JTextField();
        txtCErrorExplain = new javax.swing.JTextField();
        txtFunctionStatus = new javax.swing.JTextField();
        txtFCodeError = new javax.swing.JTextField();
        txtFErrorExplain = new javax.swing.JTextField();
        pBar = new javax.swing.JProgressBar();
        lblCardUID = new javax.swing.JLabel();
        txtCardUID = new javax.swing.JTextField();

        setStub(null);

        pnlMain.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        pnlMain.setPreferredSize(new java.awt.Dimension(550, 466));
        pnlMain.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

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

        btnFormat.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        btnFormat.setText("FORMAT CARD");
        btnFormat.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnFormat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFormatActionPerformed(evt);
            }
        });
        pnlMain.add(btnFormat, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 30, 224, 40));

        lblLinearWrite.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        lblLinearWrite.setText("Linear Write");
        pnlMain.add(lblLinearWrite, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 110, -1, -1));

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

        pnlMain.add(jScrollPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 130, 226, 187));

        btnWrite.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        btnWrite.setText("LINEAR WRITE");
        btnWrite.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnWrite.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnWriteActionPerformed(evt);
            }
        });
        pnlMain.add(btnWrite, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 330, 226, 40));

        lblLinearRead.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        lblLinearRead.setText("Linear Read");
        pnlMain.add(lblLinearRead, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 110, -1, -1));

        txtLinearRead.setEditable(false);
        txtLinearRead.setBackground(new java.awt.Color(219, 216, 216));
        txtLinearRead.setColumns(20);
        txtLinearRead.setFont(new java.awt.Font("Verdana", 0, 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);

        pnlMain.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 130, 226, 187));

        btnRead.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        btnRead.setText("LINEAR READ");
        btnRead.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnRead.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnReadActionPerformed(evt);
            }
        });
        pnlMain.add(btnRead, new org.netbeans.lib.awtextra.AbsoluteConstraints(290, 330, 226, 40));

        txtCardType.setFont(new java.awt.Font("Verdana", 1, 11)); // NOI18N
        txtCardType.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        pnlMain.add(txtCardType, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 30, 40, -1));

        lblCardUIDSize.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        lblCardUIDSize.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblCardUIDSize.setText("UID Size");
        pnlMain.add(lblCardUIDSize, new org.netbeans.lib.awtextra.AbsoluteConstraints(150, 30, -1, -1));

        txtCardUIDSize.setFont(new java.awt.Font("Verdana", 1, 11)); // NOI18N
        txtCardUIDSize.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtCardUIDSize.setToolTipText(" ");
        pnlMain.add(txtCardUIDSize, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 30, 50, -1));

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

        txtConnected.setBackground(new java.awt.Color(204, 204, 204));
        txtConnected.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        txtConnected.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        pnlMain.add(txtConnected, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 450, 110, 20));

        txtRCodeError.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        txtRCodeError.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        pnlMain.add(txtRCodeError, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 450, 90, 20));

        txtCardStatus.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        txtCardStatus.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtCardStatus.setText("CARD STATUS");
        pnlMain.add(txtCardStatus, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 430, 110, 20));

        txtCCodeError.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        txtCCodeError.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        pnlMain.add(txtCCodeError, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 430, 90, 20));

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

        txtFunctionStatus.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        txtFunctionStatus.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtFunctionStatus.setText("Function Status");
        pnlMain.add(txtFunctionStatus, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 410, 110, 20));

        txtFCodeError.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        txtFCodeError.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        pnlMain.add(txtFCodeError, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 410, 90, 20));

        txtFErrorExplain.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        txtFErrorExplain.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        pnlMain.add(txtFErrorExplain, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 410, 330, 20));
        pnlMain.add(pBar, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 380, 530, -1));

        lblCardUID.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        lblCardUID.setText("Card UID");
        pnlMain.add(lblCardUID, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 50, -1, -1));

        txtCardUID.setEditable(false);
        txtCardUID.setBackground(java.awt.Color.white);
        txtCardUID.setFont(new java.awt.Font("Verdana", 1, 11)); // NOI18N
        txtCardUID.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        pnlMain.add(txtCardUID, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 50, 160, -1));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnlMain, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(pnlMain, javax.swing.GroupLayout.PREFERRED_SIZE, 484, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 2, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnWriteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnWriteActionPerformed
         if (GetFunct() || GetLoop()) return;        
          try {                                             
                SetFunct(true);                
                int iLinearAddress = 0;
                int iDataLength = txtLinearWrite.getText().trim().length();
                int iFResult = 0;
                ShortByReference shBytesWritten = new ShortByReference();
                if (iDataLength==0){
                    JOptionPane.showMessageDialog(rootPane, "You need to enter any data !","WARNING !",JOptionPane.WARNING_MESSAGE);
                    txtLinearWrite.requestFocus();
                    return;
                }
                
                byte[] baWriteData = new byte[iDataLength];                
                baWriteData =txtLinearWrite.getText().getBytes();
                iFResult = Declarations.uFrFunctions.UFRInstance.LinearWrite(baWriteData, iLinearAddress, iDataLength, shBytesWritten, MIFARE_AUTHENT1A, KEY_INDEX);
                if (iFResult == DL_OK) {
                      Declarations.uFrFunctions.UFRInstance.ReaderUISignal(FUNCT_LIGHT_OK, FUNCT_SOUND_OK);
                      SetStatusBar(txtFCodeError, txtFErrorExplain, iFResult);
              } else {
                      Declarations.uFrFunctions.UFRInstance.ReaderUISignal(FUNCT_LIGHT_ERROR, FUNCT_SOUND_ERROR);
                      SetStatusBar(txtFCodeError, txtFErrorExplain, iFResult);
                     }
                               
            } finally {               
                SetFunct(false);
            }
    }//GEN-LAST:event_btnWriteActionPerformed

    private void btnFormatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFormatActionPerformed
        if (GetFunct() || GetLoop()) return;
        
        ExecutorService execFormat = Executors.newSingleThreadExecutor();
        execFormat.execute(new FormatCard());
    }//GEN-LAST:event_btnFormatActionPerformed

    private void btnReadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnReadActionPerformed
         if (GetFunct() || GetLoop()) return;
                
            try {
                                                  
                SetFunct(true);
                
                byte[] baReadData           = new byte[MaxBytes(bDLCardType.getValue())];
                int iLinearAddress          = 0;
                int iDataLength             = baReadData.length;
                int iFResult                = 0;
                ShortByReference shBytesRet = new ShortByReference();
                iFResult = Declarations.uFrFunctions.UFRInstance.LinearRead(baReadData, iLinearAddress, iDataLength, shBytesRet,MIFARE_AUTHENT1A, KEY_INDEX);
                if (iFResult == DL_OK) {
                    txtLinearRead.setText(new String(baReadData));
                    Declarations.uFrFunctions.UFRInstance.ReaderUISignal(FUNCT_LIGHT_OK, FUNCT_SOUND_OK);
                    SetStatusBar(txtFCodeError, txtFErrorExplain, iFResult);
              } else {
                    Declarations.uFrFunctions.UFRInstance.ReaderUISignal(FUNCT_LIGHT_ERROR, FUNCT_SOUND_ERROR);
                    SetStatusBar(txtFCodeError, txtFErrorExplain, iFResult);
                  }
                
            } finally {                
                SetFunct(false);
                
            }        
    }//GEN-LAST:event_btnReadActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnFormat;
    private javax.swing.JButton btnRead;
    private javax.swing.JButton btnWrite;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel lblCardType;
    private javax.swing.JLabel lblCardUID;
    private javax.swing.JLabel lblCardUIDSize;
    private javax.swing.JLabel lblLinearRead;
    private javax.swing.JLabel lblLinearWrite;
    private javax.swing.JProgressBar pBar;
    private javax.swing.JPanel pnlMain;
    private javax.swing.JTextField txtCCodeError;
    private javax.swing.JTextField txtCErrorExplain;
    private javax.swing.JTextField txtCardStatus;
    private javax.swing.JTextField txtCardType;
    private javax.swing.JTextField txtCardUID;
    private javax.swing.JTextField txtCardUIDSize;
    private javax.swing.JTextField txtConnected;
    private javax.swing.JTextField txtFCodeError;
    private javax.swing.JTextField txtFErrorExplain;
    private javax.swing.JTextField txtFunctionStatus;
    private javax.swing.JTextArea txtLinearRead;
    private javax.swing.JTextArea txtLinearWrite;
    private javax.swing.JTextField txtRCodeError;
    private javax.swing.JTextField txtRErrorExplain;
    // End of variables declaration//GEN-END:variables

    public void run() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}
