package ufrcoderadvance;

import com.sun.jna.Platform;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import java.util.concurrent.*;

public class frmMain extends javax.swing.JFrame {

    private static int sleep_value = 300,
            DL_OK = 0,
            RES_LIGHT_OK = 4,
            RES_SOUND_OK = 0,//4
            ERR_LIGHT = 2,
            ERR_SOUND = 0;//2;
    private boolean CONN = false;
    private int result;
    private int[] reader_type = new int[2];
    private int[] reader_serial = new int[2];
    private int[] card_serial = new int[2];
    private byte[] card_id = new byte[2];
    private byte[] read_user_data = new byte[16];
    public static boolean reader_stop = false,
            thread_start = false,
            functionOn = false;
    ExecutorService exec = Executors.newCachedThreadPool();
    private Global GL = new Global();

    public static void ReaderOn() {
        reader_stop = false;
    }

    public static void ReaderOff() {
        reader_stop = true;
    }
    
    /*************************************
    
    *  Platform.getOSType()
    *  const-value:
    *  Linux=1,Windows=2;
    * 
    **************************************/
    
    public static String PlatformOsType(){
        String platform_lib = null;
        switch(Platform.getOSType()){
            case 1:platform_lib =(Platform.is64Bit() ? "libuFCoder1x64.so" : "libuFCoder1x86.so");
                break;
            case 2:platform_lib="uFCoder1x.dll";    
        }
        return platform_lib;
    }
    public frmMain() {
        initComponents();
    }

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

        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();
        lblCardType = new javax.swing.JLabel();
        txtCardType = new javax.swing.JTextField();
        lblCardSerial = new javax.swing.JLabel();
        txtCardSerial = new javax.swing.JTextField();
        stbReader = new javax.swing.JPanel();
        lblConnected = new javax.swing.JLabel();
        lblReader_function_error = new javax.swing.JLabel();
        lblReader_function_expl = new javax.swing.JLabel();
        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();
        btnSoftRestart = new javax.swing.JButton();
        btnReaderReset = new javax.swing.JButton();
        jtabKeysUserData1 = new javax.swing.JTabbedPane();
        pnlReaderKey = new javax.swing.JPanel();
        chkHex = new javax.swing.JCheckBox();
        lblKeyIndex = new javax.swing.JLabel();
        cboKeyIndex = new javax.swing.JComboBox();
        btnReaderSaveKey = new javax.swing.JButton();
        jpnlUserData1 = new javax.swing.JPanel();
        lblUserData5 = new javax.swing.JLabel();
        txtReadUserData = new javax.swing.JTextField();
        lblNewUserData2 = new javax.swing.JLabel();
        txtNewUserData = new javax.swing.JTextField();
        btnSaveUserData = new javax.swing.JButton();
        pnlCard = new javax.swing.JPanel();
        lblCardStatus = new javax.swing.JLabel();
        lblCard_funct_error = new javax.swing.JLabel();
        lblCard_funct_explain = new javax.swing.JLabel();
        pnlDesktop = new javax.swing.JDesktopPane();
        lblHeader = new javax.swing.JLabel();
        mnuMeni = new javax.swing.JMenuBar();
        mnuFunctionsItem = new javax.swing.JMenu();
        mnuLinearReadWriteItem = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        mnuBlockReadWriteItems = new javax.swing.JMenuItem();
        mnuBlockInSectorReadWriteItem = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JPopupMenu.Separator();
        mnuValueBlockReadWriteItems = new javax.swing.JMenuItem();
        mnuValueBlockIncrDecrItems = new javax.swing.JMenuItem();
        jSeparator3 = new javax.swing.JPopupMenu.Separator();
        mnuValueBlockInSectorReadWriteItems = new javax.swing.JMenuItem();
        mnuValueBlockInSectorIncrDecrItems = new javax.swing.JMenuItem();
        jSeparator4 = new javax.swing.JPopupMenu.Separator();
        mnuSectorTrailerWriteItems = new javax.swing.JMenuItem();
        mnuLinearFormatCardItems = new javax.swing.JMenuItem();
        jSeparator5 = new javax.swing.JPopupMenu.Separator();
        mnuViewAllItems = new javax.swing.JMenuItem();
        jSeparator6 = new javax.swing.JPopupMenu.Separator();
        mnuExitItems = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("uFRCoder Advance");
        setFont(new java.awt.Font("Verdana", 0, 10)); // NOI18N
        setLocationByPlatform(true);
        setMaximumSize(new java.awt.Dimension(592, 730));
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowActivated(java.awt.event.WindowEvent evt) {
                formWindowActivated(evt);
            }
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

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

        lblReaderType.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        lblReaderType.setText("Reader Type");

        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)));

        lblReaderSerial.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        lblReaderSerial.setText("Reader Serial");

        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)));

        lblCardType.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        lblCardType.setText("Card Type");

        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)));

        lblCardSerial.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        lblCardSerial.setText("Card Serial");

        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)));

        stbReader.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        lblConnected.setBackground(new java.awt.Color(255, 255, 255));
        lblConnected.setFont(new java.awt.Font("Verdana", 1, 11)); // NOI18N
        lblConnected.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblConnected.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        lblConnected.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        lblReader_function_error.setBackground(new java.awt.Color(255, 255, 255));
        lblReader_function_error.setFont(new java.awt.Font("Verdana", 1, 11)); // NOI18N
        lblReader_function_error.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblReader_function_error.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        lblReader_function_error.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        lblReader_function_expl.setBackground(new java.awt.Color(255, 255, 255));
        lblReader_function_expl.setFont(new java.awt.Font("Verdana", 1, 11)); // NOI18N
        lblReader_function_expl.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblReader_function_expl.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        lblReader_function_expl.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        javax.swing.GroupLayout stbReaderLayout = new javax.swing.GroupLayout(stbReader);
        stbReader.setLayout(stbReaderLayout);
        stbReaderLayout.setHorizontalGroup(
            stbReaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(stbReaderLayout.createSequentialGroup()
                .addComponent(lblConnected, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblReader_function_error, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblReader_function_expl, javax.swing.GroupLayout.DEFAULT_SIZE, 393, Short.MAX_VALUE))
        );
        stbReaderLayout.setVerticalGroup(
            stbReaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(lblConnected, javax.swing.GroupLayout.DEFAULT_SIZE, 13, Short.MAX_VALUE)
            .addComponent(lblReader_function_error, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(lblReader_function_expl, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        lblLightMode.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        lblLightMode.setText("Light Mode");

        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.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        cboLightMode.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));

        lblSoundMode.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        lblSoundMode.setText("Sound Mode");

        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.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        cboSoundMode.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));

        btnReaderUISignal.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        btnReaderUISignal.setText("Reader UI Signal");
        btnReaderUISignal.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        btnReaderUISignal.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnReaderUISignal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnReaderUISignalActionPerformed(evt);
            }
        });

        btnSoftRestart.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        btnSoftRestart.setText("Soft Restart");
        btnSoftRestart.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        btnSoftRestart.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnSoftRestart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSoftRestartActionPerformed(evt);
            }
        });

        btnReaderReset.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        btnReaderReset.setText("Reader Reset");
        btnReaderReset.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        btnReaderReset.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnReaderReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnReaderResetActionPerformed(evt);
            }
        });

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

        chkHex.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        chkHex.setText("Hex");
        chkHex.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        chkHex.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                chkHexItemStateChanged(evt);
            }
        });

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

        cboKeyIndex.setFont(new java.awt.Font("Verdana", 1, 12)); // NOI18N
        cboKeyIndex.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", " " }));
        cboKeyIndex.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        cboKeyIndex.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));

        btnReaderSaveKey.setText("SAVE KEY");
        btnReaderSaveKey.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnReaderSaveKey.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnReaderSaveKeyActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlReaderKeyLayout = new javax.swing.GroupLayout(pnlReaderKey);
        pnlReaderKey.setLayout(pnlReaderKeyLayout);
        pnlReaderKeyLayout.setHorizontalGroup(
            pnlReaderKeyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlReaderKeyLayout.createSequentialGroup()
                .addGroup(pnlReaderKeyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlReaderKeyLayout.createSequentialGroup()
                        .addGap(225, 225, 225)
                        .addComponent(lblKeyIndex)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cboKeyIndex, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pnlReaderKeyLayout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addComponent(chkHex)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                .addComponent(btnReaderSaveKey, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(27, 27, 27))
        );
        pnlReaderKeyLayout.setVerticalGroup(
            pnlReaderKeyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlReaderKeyLayout.createSequentialGroup()
                .addGap(16, 16, 16)
                .addGroup(pnlReaderKeyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(chkHex)
                    .addGroup(pnlReaderKeyLayout.createSequentialGroup()
                        .addGroup(pnlReaderKeyLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cboKeyIndex, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblKeyIndex)
                            .addComponent(btnReaderSaveKey, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jtabKeysUserData1.addTab("Reader Key", pnlReaderKey);

        lblUserData5.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        lblUserData5.setText("User Data");

        txtReadUserData.setEditable(false);
        txtReadUserData.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtReadUserData.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        lblNewUserData2.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        lblNewUserData2.setText("New User Data");

        txtNewUserData.setFont(new java.awt.Font("Verdana", 1, 11)); // NOI18N
        txtNewUserData.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtNewUserData.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        btnSaveUserData.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        btnSaveUserData.setText("Save User Data");
        btnSaveUserData.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnSaveUserData.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveUserDataActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jpnlUserData1Layout = new javax.swing.GroupLayout(jpnlUserData1);
        jpnlUserData1.setLayout(jpnlUserData1Layout);
        jpnlUserData1Layout.setHorizontalGroup(
            jpnlUserData1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnlUserData1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpnlUserData1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblUserData5, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtReadUserData, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(13, 13, 13)
                .addGroup(jpnlUserData1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jpnlUserData1Layout.createSequentialGroup()
                        .addComponent(txtNewUserData, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnSaveUserData, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(lblNewUserData2))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jpnlUserData1Layout.setVerticalGroup(
            jpnlUserData1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnlUserData1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpnlUserData1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblUserData5)
                    .addComponent(lblNewUserData2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpnlUserData1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNewUserData, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtReadUserData, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSaveUserData))
                .addContainerGap(13, Short.MAX_VALUE))
        );

        jtabKeysUserData1.addTab("User Data", jpnlUserData1);

        javax.swing.GroupLayout pnlReaderLayout = new javax.swing.GroupLayout(pnlReader);
        pnlReader.setLayout(pnlReaderLayout);
        pnlReaderLayout.setHorizontalGroup(
            pnlReaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlReaderLayout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(pnlReaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(pnlReaderLayout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jtabKeysUserData1, javax.swing.GroupLayout.PREFERRED_SIZE, 530, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pnlReaderLayout.createSequentialGroup()
                        .addGroup(pnlReaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(pnlReaderLayout.createSequentialGroup()
                                .addGroup(pnlReaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addGroup(pnlReaderLayout.createSequentialGroup()
                                        .addComponent(lblReaderType, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(txtReaderType, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(pnlReaderLayout.createSequentialGroup()
                                        .addComponent(lblReaderSerial, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addGroup(pnlReaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(txtReaderSerial, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addGroup(pnlReaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                                .addComponent(cboSoundMode, javax.swing.GroupLayout.Alignment.LEADING, 0, 118, Short.MAX_VALUE)
                                                .addComponent(cboLightMode, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(lblCardType, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(pnlReaderLayout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(lblCardSerial))
                            .addGroup(pnlReaderLayout.createSequentialGroup()
                                .addGroup(pnlReaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lblLightMode, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(lblSoundMode, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addGap(124, 124, 124)
                                .addComponent(btnReaderUISignal, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(18, 18, 18)
                        .addGroup(pnlReaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnSoftRestart, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnReaderReset, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlReaderLayout.createSequentialGroup()
                                .addGroup(pnlReaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(txtCardType, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtCardSerial, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(1, 1, 1)))))
                .addGap(29, 29, 29))
            .addComponent(stbReader, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        pnlReaderLayout.setVerticalGroup(
            pnlReaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlReaderLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlReaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlReaderLayout.createSequentialGroup()
                        .addGroup(pnlReaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtReaderType, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblReaderType))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(pnlReaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblReaderSerial)
                            .addComponent(txtReaderSerial, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(pnlReaderLayout.createSequentialGroup()
                        .addGap(2, 2, 2)
                        .addGroup(pnlReaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(pnlReaderLayout.createSequentialGroup()
                                .addGap(26, 26, 26)
                                .addComponent(lblCardSerial))
                            .addGroup(pnlReaderLayout.createSequentialGroup()
                                .addGroup(pnlReaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(txtCardType, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(lblCardType))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtCardSerial, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(pnlReaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlReaderLayout.createSequentialGroup()
                        .addComponent(btnSoftRestart, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnReaderReset, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(pnlReaderLayout.createSequentialGroup()
                        .addGroup(pnlReaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cboLightMode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblLightMode))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(pnlReaderLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cboSoundMode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lblSoundMode)))
                    .addComponent(btnReaderUISignal, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jtabKeysUserData1, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(stbReader, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pnlCard.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        lblCardStatus.setFont(new java.awt.Font("Verdana", 1, 11)); // NOI18N
        lblCardStatus.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblCardStatus.setText("CARD STATUS");
        lblCardStatus.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        lblCardStatus.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        lblCard_funct_error.setFont(new java.awt.Font("Verdana", 1, 11)); // NOI18N
        lblCard_funct_error.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblCard_funct_error.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        lblCard_funct_error.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        lblCard_funct_explain.setFont(new java.awt.Font("Verdana", 1, 11)); // NOI18N
        lblCard_funct_explain.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblCard_funct_explain.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        lblCard_funct_explain.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        javax.swing.GroupLayout pnlCardLayout = new javax.swing.GroupLayout(pnlCard);
        pnlCard.setLayout(pnlCardLayout);
        pnlCardLayout.setHorizontalGroup(
            pnlCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlCardLayout.createSequentialGroup()
                .addComponent(lblCardStatus, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblCard_funct_error, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblCard_funct_explain, javax.swing.GroupLayout.DEFAULT_SIZE, 413, Short.MAX_VALUE))
        );
        pnlCardLayout.setVerticalGroup(
            pnlCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnlCardLayout.createSequentialGroup()
                .addGroup(pnlCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblCard_funct_explain, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(pnlCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblCardStatus, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(lblCard_funct_error, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addGap(2, 2, 2))
        );

        lblCardStatus.getAccessibleContext().setAccessibleName("lblCardStatus");

        lblHeader.setBackground(new java.awt.Color(255, 255, 255));
        lblHeader.setFont(new java.awt.Font("Verdana", 1, 14)); // NOI18N
        lblHeader.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblHeader.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        lblHeader.setOpaque(true);
        lblHeader.setBounds(0, 0, 592, 30);
        pnlDesktop.add(lblHeader, javax.swing.JLayeredPane.MODAL_LAYER);

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

        mnuFunctionsItem.setText("Functions");
        mnuFunctionsItem.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        mnuFunctionsItem.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N

        mnuLinearReadWriteItem.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        mnuLinearReadWriteItem.setText("Linear Read/Write");
        mnuLinearReadWriteItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuLinearReadWriteItemActionPerformed(evt);
            }
        });
        mnuFunctionsItem.add(mnuLinearReadWriteItem);
        mnuFunctionsItem.add(jSeparator1);

        mnuBlockReadWriteItems.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        mnuBlockReadWriteItems.setText("Block Read/Write");
        mnuBlockReadWriteItems.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuBlockReadWriteItemsActionPerformed(evt);
            }
        });
        mnuFunctionsItem.add(mnuBlockReadWriteItems);

        mnuBlockInSectorReadWriteItem.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        mnuBlockInSectorReadWriteItem.setText("BlockInSector Read/Write");
        mnuBlockInSectorReadWriteItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuBlockInSectorReadWriteItemActionPerformed(evt);
            }
        });
        mnuFunctionsItem.add(mnuBlockInSectorReadWriteItem);
        mnuFunctionsItem.add(jSeparator2);

        mnuValueBlockReadWriteItems.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        mnuValueBlockReadWriteItems.setText("ValueBlock Read/Write");
        mnuValueBlockReadWriteItems.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuValueBlockReadWriteItemsActionPerformed(evt);
            }
        });
        mnuFunctionsItem.add(mnuValueBlockReadWriteItems);

        mnuValueBlockIncrDecrItems.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        mnuValueBlockIncrDecrItems.setText("ValueBlock Increment/Decrement");
        mnuValueBlockIncrDecrItems.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuValueBlockIncrDecrItemsActionPerformed(evt);
            }
        });
        mnuFunctionsItem.add(mnuValueBlockIncrDecrItems);
        mnuFunctionsItem.add(jSeparator3);

        mnuValueBlockInSectorReadWriteItems.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        mnuValueBlockInSectorReadWriteItems.setText("ValueBlockInSector Read/Write");
        mnuValueBlockInSectorReadWriteItems.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuValueBlockInSectorReadWriteItemsActionPerformed(evt);
            }
        });
        mnuFunctionsItem.add(mnuValueBlockInSectorReadWriteItems);

        mnuValueBlockInSectorIncrDecrItems.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        mnuValueBlockInSectorIncrDecrItems.setText("ValueBlockInSector Increment/Decrement");
        mnuValueBlockInSectorIncrDecrItems.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuValueBlockInSectorIncrDecrItemsActionPerformed(evt);
            }
        });
        mnuFunctionsItem.add(mnuValueBlockInSectorIncrDecrItems);
        mnuFunctionsItem.add(jSeparator4);

        mnuSectorTrailerWriteItems.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        mnuSectorTrailerWriteItems.setText("Sector Trailer Write");
        mnuSectorTrailerWriteItems.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuSectorTrailerWriteItemsActionPerformed(evt);
            }
        });
        mnuFunctionsItem.add(mnuSectorTrailerWriteItems);

        mnuLinearFormatCardItems.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        mnuLinearFormatCardItems.setText("Linear Format Card");
        mnuLinearFormatCardItems.setToolTipText("");
        mnuLinearFormatCardItems.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuLinearFormatCardItemsActionPerformed(evt);
            }
        });
        mnuFunctionsItem.add(mnuLinearFormatCardItems);
        mnuFunctionsItem.add(jSeparator5);

        mnuViewAllItems.setFont(new java.awt.Font("Verdana", 0, 11)); // NOI18N
        mnuViewAllItems.setText("View All");
        mnuViewAllItems.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuViewAllItemsActionPerformed(evt);
            }
        });
        mnuFunctionsItem.add(mnuViewAllItems);
        mnuFunctionsItem.add(jSeparator6);

        mnuExitItems.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Q, java.awt.event.InputEvent.CTRL_MASK));
        mnuExitItems.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
        mnuExitItems.setText("EXIT");
        mnuExitItems.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuExitItemsActionPerformed(evt);
            }
        });
        mnuFunctionsItem.add(mnuExitItems);

        mnuMeni.add(mnuFunctionsItem);

        setJMenuBar(mnuMeni);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(pnlCard, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(pnlDesktop, javax.swing.GroupLayout.PREFERRED_SIZE, 592, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(pnlReader, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(pnlReader, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlDesktop, javax.swing.GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnlCard, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

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

    private void btnReaderUISignalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnReaderUISignalActionPerformed
        int function_result;
        if (functionOn) {
            return;
        }
        functionOn = true;
        ReaderOff();
        if (thread_start) {
            ReaderOn();
            functionOn = false;
            return;
        }
        try {
            function_result = UFRCoderWrapper.UFRCoderDeclaration.UFRInstance.ReaderUISignal(cboLightMode.getSelectedIndex(), cboSoundMode.getSelectedIndex());
            if (function_result == DL_OK) {
                UFRCoderWrapper.UFRCoderDeclaration.UFRInstance.ReaderUISignal(RES_LIGHT_OK, RES_SOUND_OK);
            } else {
                UFRCoderWrapper.UFRCoderDeclaration.UFRInstance.ReaderUISignal(ERR_LIGHT, ERR_SOUND);
            }
        } finally {
            ReaderOn();
            functionOn = false;
        }
    }//GEN-LAST:event_btnReaderUISignalActionPerformed

    private void btnSoftRestartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSoftRestartActionPerformed
        int function_result;
        if (functionOn) {
            return;
        }
        functionOn = true;
        ReaderOff();
        if (thread_start) {
            ReaderOn();
            functionOn = false;
            return;
        }
        try {
            function_result = UFRCoderWrapper.UFRCoderDeclaration.UFRInstance.ReaderSoftRestart();
            if (function_result == DL_OK) {
                UFRCoderWrapper.UFRCoderDeclaration.UFRInstance.ReaderUISignal(RES_LIGHT_OK, RES_SOUND_OK);
            } else {
                UFRCoderWrapper.UFRCoderDeclaration.UFRInstance.ReaderUISignal(ERR_LIGHT, ERR_SOUND);
            }
        } finally {
            ReaderOn();
            functionOn = false;
        }
    }//GEN-LAST:event_btnSoftRestartActionPerformed

    private void btnReaderResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnReaderResetActionPerformed
        int function_result;
        if (functionOn) {
            return;
        }
        functionOn = true;
        ReaderOff();
        if (thread_start) {
            ReaderOn();
            functionOn = false;
            return;
        }
        try {
            function_result = UFRCoderWrapper.UFRCoderDeclaration.UFRInstance.ReaderReset();
            if (function_result == DL_OK) {
                UFRCoderWrapper.UFRCoderDeclaration.UFRInstance.ReaderUISignal(RES_LIGHT_OK, RES_SOUND_OK);
            } else {
                UFRCoderWrapper.UFRCoderDeclaration.UFRInstance.ReaderUISignal(ERR_LIGHT, ERR_SOUND);
            }
        } finally {
            ReaderOn();
            functionOn = false;
        }

    }//GEN-LAST:event_btnReaderResetActionPerformed

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

        //  frmLinearReadWrite LRW=new frmLinearReadWrite();
        //  GL.AddFrame(LRW,pnlDesktop,lblHeader,"Linear Read/Write (AKM1,AKM2,PK)");
       // GL.CreateKey(27, 20, 31, 29, "txtReaderKey", pnlReaderKey);

    }//GEN-LAST:event_formWindowActivated

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

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

        frmBlockReadWrite BRW = new frmBlockReadWrite();
        GL.AddFrame(BRW, pnlDesktop, lblHeader, "Block Read/Write(AKM1,AKM2,PK");
    }//GEN-LAST:event_mnuBlockReadWriteItemsActionPerformed

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

        frmLinearReadWrite LRW = new frmLinearReadWrite();
        GL.AddFrame(LRW, pnlDesktop, lblHeader, "Linear Read/Write (AKM1,AKM2,PK)");
    }//GEN-LAST:event_mnuLinearReadWriteItemActionPerformed

    private void mnuBlockInSectorReadWriteItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuBlockInSectorReadWriteItemActionPerformed
        frmBlockInSectorReadWrite BSRW = new frmBlockInSectorReadWrite();
        GL.AddFrame(BSRW, pnlDesktop, lblHeader, "BlockInSector Read/Write (AKM1,AKM2,PK)");
    }//GEN-LAST:event_mnuBlockInSectorReadWriteItemActionPerformed

    private void mnuValueBlockReadWriteItemsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuValueBlockReadWriteItemsActionPerformed
        frmValueBlockReadWrite VBRW = new frmValueBlockReadWrite();
        GL.AddFrame(VBRW, pnlDesktop, lblHeader, "ValueBlock Read/Write (AKM1,AKM2,PK)");
    }//GEN-LAST:event_mnuValueBlockReadWriteItemsActionPerformed

    private void mnuValueBlockIncrDecrItemsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuValueBlockIncrDecrItemsActionPerformed
        frmValueBlockIncrDecr VBID = new frmValueBlockIncrDecr();
        GL.AddFrame(VBID, pnlDesktop, lblHeader, "ValueBlock Increment/Decrement (AKM1,AKM2,PK)");
    }
//GEN-LAST:event_mnuValueBlockIncrDecrItemsActionPerformed

    private void mnuValueBlockInSectorReadWriteItemsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuValueBlockInSectorReadWriteItemsActionPerformed
        frmValueBlockInSectorReadWrite VBSRW = new frmValueBlockInSectorReadWrite();
        GL.AddFrame(VBSRW, pnlDesktop, lblHeader, "ValueBlockInSector Read/Write (AKM1,AKM2,PK)");
    }//GEN-LAST:event_mnuValueBlockInSectorReadWriteItemsActionPerformed

    private void mnuValueBlockInSectorIncrDecrItemsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuValueBlockInSectorIncrDecrItemsActionPerformed
        frmValueBlockInSectorIncrDecr VBSID = new frmValueBlockInSectorIncrDecr();
        GL.AddFrame(VBSID, pnlDesktop, lblHeader, "ValueBlockInSector Increment/Decrement (AKM1,AKM2,PK)");
    }//GEN-LAST:event_mnuValueBlockInSectorIncrDecrItemsActionPerformed

    private void mnuLinearFormatCardItemsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuLinearFormatCardItemsActionPerformed
        frmLinearFormatCard LFC = new frmLinearFormatCard();
        GL.AddFrame(LFC, pnlDesktop, lblHeader, "LinearFormat Card (AKM1,AKM2,PK)");
    }//GEN-LAST:event_mnuLinearFormatCardItemsActionPerformed

    private void mnuSectorTrailerWriteItemsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuSectorTrailerWriteItemsActionPerformed
        frmSectorTrailerWrite STW = new frmSectorTrailerWrite();
        GL.AddFrame(STW, pnlDesktop, lblHeader, "SectorTrailer Write (AKM1,AKM2,PK)");
    }//GEN-LAST:event_mnuSectorTrailerWriteItemsActionPerformed

    private void mnuViewAllItemsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuViewAllItemsActionPerformed
        frmViewAll VA = new frmViewAll();
        GL.AddFrame(VA, pnlDesktop, lblHeader, "View All");
    }//GEN-LAST:event_mnuViewAllItemsActionPerformed

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        
        
        frmLinearReadWrite LRW = new frmLinearReadWrite();
        GL.AddFrame(LRW, pnlDesktop, lblHeader, "Linear Read/Write (AKM1,AKM2,PK)");
        GL.CreateKey(27, 20, 31, 29, "txtReaderKey", pnlReaderKey);
        exec.execute(new Main_Thread());

    }//GEN-LAST:event_formWindowOpened

    private void chkHexItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_chkHexItemStateChanged
         GL.Check_box(pnlReaderKey, "txtReaderKey", evt);
         GL.HEX_BOX=chkHex;
         GL.TEXTFIELD="txtReaderKey";
         
    }//GEN-LAST:event_chkHexItemStateChanged

    private void btnSaveUserDataActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveUserDataActionPerformed
        int function_result;
        if (functionOn) {
            return;
        }
        functionOn = true;
        ReaderOff();
        if (thread_start) {
            ReaderOn();
            functionOn = false;
            return;
        } 
        try
        {
        if (txtNewUserData.getText().trim().isEmpty())
       {
           JOptionPane.showMessageDialog(rootPane,"You must enter any data !",
"Warning !", JOptionPane.WARNING_MESSAGE);
           txtNewUserData.requestFocus();
           return;
       }
       byte[] new_user_data=new byte[16];
       new_user_data=txtNewUserData.getText().getBytes();
     
       result=UFRCoderWrapper.UFRCoderDeclaration.UFRInstance.WriteUserData(new_user_data);
       if (result==DL_OK) {
            UFRCoderWrapper.UFRCoderDeclaration.UFRInstance.ReaderUISignal(RES_LIGHT_OK,RES_SOUND_OK);
        }
       else {
            UFRCoderWrapper.UFRCoderDeclaration.UFRInstance.ReaderUISignal(ERR_LIGHT,ERR_SOUND);
        }
        }
       finally {
            ReaderOn();
            functionOn = false;
        }
    }//GEN-LAST:event_btnSaveUserDataActionPerformed

    private void btnReaderSaveKeyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnReaderSaveKeyActionPerformed
   
        if (functionOn) {
            return;
        }
        functionOn = true;
        ReaderOff();
        if (thread_start) {
            ReaderOn();
            functionOn = false;
            return;
        }
        
        int iFunctionResult=0;
        byte bKeyIndex=0;
        
        try
        {
           bKeyIndex=(byte)Integer.parseInt(cboKeyIndex.getSelectedItem().toString());
           iFunctionResult=UFRCoderWrapper.UFRCoderDeclaration.UFRInstance.ReaderKeyWrite(GL.KEY(pnlReaderKey,"txtReaderKey", chkHex), bKeyIndex);
           if (iFunctionResult==DL_OK){
               UFRCoderWrapper.UFRCoderDeclaration.UFRInstance.ReaderUISignal(RES_LIGHT_OK,RES_SOUND_OK);
        }
       else {
            UFRCoderWrapper.UFRCoderDeclaration.UFRInstance.ReaderUISignal(ERR_LIGHT,ERR_SOUND);
        }    
           
        }
        finally{
           ReaderOn();
           functionOn = false; 
        }
    }//GEN-LAST:event_btnReaderSaveKeyActionPerformed
    


 private void MainThread() {
        thread_start = true;
        if (!CONN) {
            if ((result = UFRCoderWrapper.UFRCoderDeclaration.UFRInstance.ReaderOpen()) == DL_OK) {
                lblConnected.setText("CONNECTED");

                GL.ERRORS_CODE(result, lblReader_function_error, lblReader_function_expl);
                CONN = true;
            } else {
                lblConnected.setText("NOT CONNECTED");
                txtReaderType.setText(null);
                txtReaderSerial.setText(null);
                txtCardType.setText(null);
                txtCardSerial.setText(null);
                GL.ERRORS_CODE(result, lblReader_function_error, lblReader_function_expl);

            }
        }
        if (CONN) {
            if ((result = UFRCoderWrapper.UFRCoderDeclaration.UFRInstance.GetReaderType(reader_type)) == DL_OK) {
                txtReaderType.setText("0x" + Integer.toHexString(reader_type[0]).toUpperCase());
            if ((result = UFRCoderWrapper.UFRCoderDeclaration.UFRInstance.GetReaderSerialNumber(reader_serial)) == DL_OK) {
                txtReaderSerial.setText("0x" + Integer.toHexString(reader_serial[0]).toUpperCase());
                GL.ERRORS_CODE(result, lblReader_function_error, lblReader_function_expl);
            }

            if ((result = UFRCoderWrapper.UFRCoderDeclaration.UFRInstance.GetCardId(card_id, card_serial)) == DL_OK) {
                txtCardType.setText("0x" + Integer.toHexString(card_id[0]));
                txtCardSerial.setText("0x" + Integer.toHexString(card_serial[0]).toUpperCase());
                GL.ERRORS_CODE(result, lblCard_funct_error, lblCard_funct_explain);
            } else {
                txtCardType.setText(null);
                txtCardSerial.setText(null);
                GL.ERRORS_CODE(result, lblCard_funct_error, lblCard_funct_explain);
            }
                
            } else {
                UFRCoderWrapper.UFRCoderDeclaration.UFRInstance.ReaderClose();
                CONN = false;

            }
            
            if ((result = UFRCoderWrapper.UFRCoderDeclaration.UFRInstance.ReadUserData(read_user_data)) == DL_OK) 

                
                txtReadUserData.setText(new String(read_user_data));
            
        }
        thread_start = false;
    }

    class Main_Thread implements Runnable {

        @Override
        public void run() {
            try {
                while (true) {
                    if (!reader_stop) {
                        MainThread();
                    }
                    TimeUnit.MILLISECONDS.sleep(sleep_value);                    
                }
            } catch (InterruptedException ex) {
                Logger.getLogger(frmMain.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    }

    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(frmMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(frmMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(frmMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(frmMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }

        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {

                frmMain frmUfrCoder = new frmMain();
                frmUfrCoder.setVisible(true);
                frmUfrCoder.setLocationRelativeTo(null);

            }
        });


    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnReaderReset;
    private javax.swing.JButton btnReaderSaveKey;
    private javax.swing.JButton btnReaderUISignal;
    private javax.swing.JButton btnSaveUserData;
    private javax.swing.JButton btnSoftRestart;
    private javax.swing.JComboBox cboKeyIndex;
    private javax.swing.JComboBox cboLightMode;
    private javax.swing.JComboBox cboSoundMode;
    private javax.swing.JCheckBox chkHex;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JPopupMenu.Separator jSeparator2;
    private javax.swing.JPopupMenu.Separator jSeparator3;
    private javax.swing.JPopupMenu.Separator jSeparator4;
    private javax.swing.JPopupMenu.Separator jSeparator5;
    private javax.swing.JPopupMenu.Separator jSeparator6;
    private javax.swing.JPanel jpnlUserData1;
    private javax.swing.JTabbedPane jtabKeysUserData1;
    private javax.swing.JLabel lblCardSerial;
    private javax.swing.JLabel lblCardStatus;
    private javax.swing.JLabel lblCardType;
    private javax.swing.JLabel lblCard_funct_error;
    private javax.swing.JLabel lblCard_funct_explain;
    private javax.swing.JLabel lblConnected;
    private javax.swing.JLabel lblHeader;
    private javax.swing.JLabel lblKeyIndex;
    private javax.swing.JLabel lblLightMode;
    private javax.swing.JLabel lblNewUserData2;
    private javax.swing.JLabel lblReaderSerial;
    private javax.swing.JLabel lblReaderType;
    private javax.swing.JLabel lblReader_function_error;
    private javax.swing.JLabel lblReader_function_expl;
    private javax.swing.JLabel lblSoundMode;
    private javax.swing.JLabel lblUserData5;
    private javax.swing.JMenuItem mnuBlockInSectorReadWriteItem;
    private javax.swing.JMenuItem mnuBlockReadWriteItems;
    private javax.swing.JMenuItem mnuExitItems;
    private javax.swing.JMenu mnuFunctionsItem;
    private javax.swing.JMenuItem mnuLinearFormatCardItems;
    private javax.swing.JMenuItem mnuLinearReadWriteItem;
    private javax.swing.JMenuBar mnuMeni;
    private javax.swing.JMenuItem mnuSectorTrailerWriteItems;
    private javax.swing.JMenuItem mnuValueBlockInSectorIncrDecrItems;
    private javax.swing.JMenuItem mnuValueBlockInSectorReadWriteItems;
    private javax.swing.JMenuItem mnuValueBlockIncrDecrItems;
    private javax.swing.JMenuItem mnuValueBlockReadWriteItems;
    private javax.swing.JMenuItem mnuViewAllItems;
    private javax.swing.JPanel pnlCard;
    private javax.swing.JDesktopPane pnlDesktop;
    private javax.swing.JPanel pnlReader;
    private javax.swing.JPanel pnlReaderKey;
    private javax.swing.JPanel stbReader;
    private javax.swing.JTextField txtCardSerial;
    private javax.swing.JTextField txtCardType;
    private javax.swing.JTextField txtNewUserData;
    private javax.swing.JTextField txtReadUserData;
    private javax.swing.JTextField txtReaderSerial;
    private javax.swing.JTextField txtReaderType;
    // End of variables declaration//GEN-END:variables
}
