import java.awt.EventQueue;

import javax.smartcardio.CardException;
import javax.swing.JFrame;
import java.awt.BorderLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.border.EtchedBorder;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JComboBox;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.Font;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.BorderFactory;
import javax.swing.JTabbedPane;
import javax.swing.border.TitledBorder;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.JCheckBox;
import javax.swing.JRadioButton;
import Pcsc.*;
import java.awt.Choice;
import javax.swing.ButtonGroup;
import java.awt.Component;
import java.awt.Container;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.ActionEvent;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class ACR1552UAdvancedDeviceProgramming extends JFrame implements ReaderEvent.TransmitApduHandler, KeyListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Acr1552U acr1552u = new Acr1552U();
	
	private JFrame frmWrr;
	
	private JTextArea textArea;
	private JScrollPane scrollPane;
	
	private JTabbedPane tabbedPane;
	private JPanel panelPeripheralControl;
	private JPanel panelNFC;
	
	private JLabel lblActiveD000Option;
	private JLabel lblActiveD001Option;
	private JLabel lblActiveD010Option;
	private JLabel lblActiveD011Option;
	private JLabel lblSuspendD200Option;
	private JLabel lblSuspendD201Option;
	private JLabel lblSuspendD210Option;
	private JLabel lblSuspendD211Option;
	
	private JButton btnInitialize;
	private JButton btnConnect;
	private JButton ButtonReset;
	
	private JSpinner spinner;
	
	private JComboBox<String> comboBoxReader = new JComboBox<String>();
	
	private final ButtonGroup buttonGroupPICCStatusPICCType = new ButtonGroup();
	private final ButtonGroup buttonGroupPICCStatusRFControl = new ButtonGroup();
	private final ButtonGroup buttonGroupPICCStatus = new ButtonGroup();
	private final ButtonGroup buttonGroupPollingOption = new ButtonGroup();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ACR1552UAdvancedDeviceProgramming window = new ACR1552UAdvancedDeviceProgramming();
					window.frmWrr.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public ACR1552UAdvancedDeviceProgramming() {
		// Instantiate an event handler object 
		acr1552u.setEventHandler(new ReaderEvent());
		// Register the event handler implementation of this class
		acr1552u.getEventHandler().addEventListener(this);
		initialize();
		resetControls();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmWrr = new JFrame();
		frmWrr.setResizable(false);
		frmWrr.getContentPane().setEnabled(false);
		frmWrr.setTitle("ACR1552U Advanced Device Programming");
		frmWrr.setBounds(100, 100, 1100, 750);
		frmWrr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmWrr.setResizable(false);
		
		JPanel panelLog = new JPanel();
		panelLog.setLayout(new BorderLayout());
		textArea = new JTextArea();
		textArea.setEditable(false);
		textArea.setLineWrap(true);
		textArea.setWrapStyleWord(true);
		textArea.setFont(new Font("Verdana", Font.PLAIN, 11));
		textArea.setBorder(BorderFactory.createLineBorder(Color.gray));		
		scrollPane = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		panelLog.add(scrollPane);
		
		JLabel lblSelectReader = new JLabel("Select Reader");
		lblSelectReader.setFont(new Font("Verdana", Font.PLAIN, 12));
		
		comboBoxReader.setFont(new Font("Verdana", Font.PLAIN, 12));
		
		btnInitialize = new JButton("Initialize");
		btnInitialize.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String[] readerList;
				comboBoxReader.setEnabled(true);
				comboBoxReader.removeAllItems();
				
				try
				{
					readerList = acr1552u.listTerminals();
					
					for (int i = 0; i < readerList.length; i++)
						comboBoxReader.addItem(readerList[i]);
				}
				catch (CardException cardException)
				{
					displayErrorMessage(Pcsc.PcscProvider.getScardErrorMessage(cardException));			
				}
				catch (Exception exception)
				{
					displayErrorMessage(exception.getMessage() + "\r\n");			
				}		
				
				btnConnect.setEnabled(true);
			}
		});
		btnInitialize.setFont(new Font("Verdana", Font.PLAIN, 12));
		
		btnConnect = new JButton("Connect");
		btnConnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String readerConnection = (String)comboBoxReader.getSelectedItem();	
				
				if (comboBoxReader.getSelectedIndex() < 0)
				{			
					displayErrorMessage("Please select smartcard reader.");
					return; 			
				}
				
				try
				{
					if(acr1552u.isConnectionActive())
						acr1552u.disconnect();
					
					if(!readerConnection.contains("ACR1552"))
					{
						displayErrorMessage("Reader not supported. Please connect to an ACR1552U Reader.");		
						return;					
					}
					
					// Connect directly to the smart card reader
					acr1552u.connectDirect(comboBoxReader.getSelectedIndex(), true);

				}
				catch (CardException cardException)
				{
					displayErrorMessage(Pcsc.PcscProvider.getScardErrorMessage(cardException));			
				}
				catch (Exception exception)
				{
					displayErrorMessage(exception.getMessage() + "\r\n");			
				}
				
				setEnabled(tabbedPane, true);

				btnInitialize.setEnabled(false);
				btnConnect.setEnabled(false);
				ButtonReset.setEnabled(true);
				
                if (readerConnection.contains(" SAM"))
                {
                	setEnabled(panelNFC, false);
                }
				
                displayMessage("\nSuccessful connection to " + comboBoxReader.getSelectedItem() + "\n");
			}
		});
		btnConnect.setFont(new Font("Verdana", Font.PLAIN, 12));
		
		JLabel lblAPDULogs = new JLabel("APDU Logs");
		lblAPDULogs.setFont(new Font("Verdana", Font.PLAIN, 12));
		
		JButton ButtonQuit = new JButton("Quit");
		ButtonQuit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try
				{
					if (acr1552u.isConnectionActive())
						acr1552u.disconnect();

					System.gc();
					frmWrr.dispose();
				}
				catch (CardException cardException)
				{
					displayErrorMessage(Pcsc.PcscProvider.getScardErrorMessage(cardException));			
				}
				catch (Exception exception)
				{
					displayErrorMessage(exception.getMessage() + "\r\n");			
				}
			}
		});
		ButtonQuit.setFont(new Font("Verdana", Font.PLAIN, 12));
		
		ButtonReset = new JButton("Reset");
		ButtonReset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try
				{
					if (acr1552u.isConnectionActive())
						acr1552u.disconnect();

					btnConnect.setEnabled(false);
					resetControls();
				}
				catch (CardException cardException)
				{
					displayErrorMessage(Pcsc.PcscProvider.getScardErrorMessage(cardException));			
				}
				catch (Exception exception)
				{
					displayErrorMessage(exception.getMessage() + "\r\n");			
				}
			}
		});
		ButtonReset.setFont(new Font("Verdana", Font.PLAIN, 12));
		
		JButton ButtonClear = new JButton("Clear");
		ButtonClear.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				textArea.setText("");
			}
		});
		ButtonClear.setFont(new Font("Verdana", Font.PLAIN, 12));
		
		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		
		GroupLayout groupLayout = new GroupLayout(frmWrr.getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(panelLog, GroupLayout.DEFAULT_SIZE, 353, Short.MAX_VALUE)
						.addComponent(btnInitialize, GroupLayout.DEFAULT_SIZE, 353, Short.MAX_VALUE)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(lblSelectReader)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(comboBoxReader, GroupLayout.PREFERRED_SIZE, 252, GroupLayout.PREFERRED_SIZE))
						.addComponent(btnConnect, GroupLayout.DEFAULT_SIZE, 353, Short.MAX_VALUE)
						.addComponent(lblAPDULogs, GroupLayout.PREFERRED_SIZE, 87, GroupLayout.PREFERRED_SIZE)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(ButtonClear, GroupLayout.PREFERRED_SIZE, 106, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(ButtonReset, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(ButtonQuit, GroupLayout.DEFAULT_SIZE, 125, Short.MAX_VALUE)))
					.addGap(18)
					.addComponent(tabbedPane, GroupLayout.PREFERRED_SIZE, 693, GroupLayout.PREFERRED_SIZE)
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(tabbedPane, GroupLayout.DEFAULT_SIZE, 689, Short.MAX_VALUE)
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblSelectReader)
								.addComponent(comboBoxReader, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnInitialize)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnConnect, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(lblAPDULogs, GroupLayout.PREFERRED_SIZE, 16, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(panelLog, GroupLayout.PREFERRED_SIZE, 543, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(ButtonClear, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
								.addComponent(ButtonQuit, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
								.addComponent(ButtonReset, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE))))
					.addContainerGap())
		);
		
		panelPeripheralControl = new JPanel();
		tabbedPane.addTab("Peripheral Control", null, panelPeripheralControl, null);
		panelPeripheralControl.setLayout(null);
		
		JPanel panelGetFirmwareVersion = new JPanel();
		panelGetFirmwareVersion.setBorder(new TitledBorder(null, "Get Firmware Version", TitledBorder.LEFT, TitledBorder.TOP, null, null));
		panelGetFirmwareVersion.setBounds(10, 11, 154, 63);
		panelPeripheralControl.add(panelGetFirmwareVersion);
		panelGetFirmwareVersion.setLayout(null);
		
		JButton btnGetVersion = new JButton("Get Version");
		btnGetVersion.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				displayMessage("Get Firmware Version");
				String firmwareVersion;
				try
				{
					firmwareVersion = acr1552u.getFirmwareVersion();
					displayMessage("Firmware Version:\n" + firmwareVersion);
				}
				catch (Exception exception)
				{
					displayErrorMessage(exception.getMessage() + "\r\n");			
				}
			}
		});
		btnGetVersion.setBounds(10, 17, 134, 35);
		panelGetFirmwareVersion.add(btnGetVersion);
		
		JPanel panelGetSerialNumber = new JPanel();
		panelGetSerialNumber.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, new Color(255, 255, 255), new Color(160, 160, 160)), "Get Serial Number", TitledBorder.LEFT, TitledBorder.TOP, null, new Color(0, 0, 0)));
		panelGetSerialNumber.setBounds(174, 11, 185, 63);
		panelPeripheralControl.add(panelGetSerialNumber);
		panelGetSerialNumber.setLayout(null);
		
		JButton btnGetSerialNumber = new JButton("Get Serial Number");
		btnGetSerialNumber.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				displayMessage("Get Serial Number");
				String serialNumber;
				try
				{
					serialNumber = acr1552u.getSerialNumber();
					displayMessage("Serial No.:\n" + serialNumber);
				}
				catch (Exception exception)
				{
					displayErrorMessage(exception.getMessage() + "\r\n");			
				}
			}
		});
		btnGetSerialNumber.setBounds(10, 17, 165, 35);
		panelGetSerialNumber.add(btnGetSerialNumber);
		
		JPanel panelBuzzerControl = new JPanel();
		panelBuzzerControl.setLayout(null);
		panelBuzzerControl.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, new Color(255, 255, 255), new Color(160, 160, 160)), "Buzzer Control - Single Time", TitledBorder.LEFT, TitledBorder.TOP, null, new Color(0, 0, 0)));
		panelBuzzerControl.setBounds(10, 85, 349, 78);
		panelPeripheralControl.add(panelBuzzerControl);
		
		JLabel lblDuration = new JLabel("Duration:");
		lblDuration.setBounds(10, 21, 90, 14);
		panelBuzzerControl.add(lblDuration);
		
		JButton btnSetBuzzerStatus = new JButton("Set Status");
		btnSetBuzzerStatus.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				displayMessage("Set Buzzer State - Single Time");
				try
				{ 
					int duration = (int) spinner.getValue();
					acr1552u.setBuzzerState((byte) duration);
				}
				catch (Exception exception)
				{
					displayErrorMessage(exception.getMessage() + "\r\n");			
				}
			}
		});
		btnSetBuzzerStatus.setBounds(10, 46, 329, 23);
		panelBuzzerControl.add(btnSetBuzzerStatus);
		
	    spinner = new JSpinner();
		spinner.setModel(new SpinnerNumberModel(0, null, 100, 1));
		spinner.setBounds(110, 18, 85, 20);
		panelBuzzerControl.add(spinner);
		
		JLabel lblx10ms = new JLabel("x 10ms");
		lblx10ms.setBounds(205, 21, 46, 14);
		panelBuzzerControl.add(lblx10ms);
		
		JPanel panelBuzzer = new JPanel();
		panelBuzzer.setLayout(null);
		panelBuzzer.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, new Color(255, 255, 255), new Color(160, 160, 160)), "Buzzer Control - Repeatable", TitledBorder.LEFT, TitledBorder.TOP, null, new Color(0, 0, 0)));
		panelBuzzer.setBounds(10, 174, 349, 133);
		panelPeripheralControl.add(panelBuzzer);
		
		JLabel lblOnTimePeriod = new JLabel("On Time Period:");
		lblOnTimePeriod.setBounds(10, 21, 90, 14);
		panelBuzzer.add(lblOnTimePeriod);
		
		JButton btnSetBuzzerStatus_1 = new JButton("Set Status");
		btnSetBuzzerStatus_1.setBounds(10, 102, 329, 23);
		panelBuzzer.add(btnSetBuzzerStatus_1);
		
		JSpinner spinner_1 = new JSpinner();
		spinner_1.setBounds(110, 18, 85, 20);
		panelBuzzer.add(spinner_1);
		
		JLabel lblx10ms_1 = new JLabel("x 10ms");
		lblx10ms_1.setBounds(205, 21, 46, 14);
		panelBuzzer.add(lblx10ms_1);
		
		JLabel lblOffTimePeriod = new JLabel("Off Time Period:");
		lblOffTimePeriod.setBounds(10, 49, 97, 14);
		panelBuzzer.add(lblOffTimePeriod);
		
		JSpinner spinner_1_1 = new JSpinner();
		spinner_1_1.setBounds(110, 46, 85, 20);
		panelBuzzer.add(spinner_1_1);
		
		JLabel lblx10ms_1_1 = new JLabel("x 10ms");
		lblx10ms_1_1.setBounds(205, 49, 46, 14);
		panelBuzzer.add(lblx10ms_1_1);
		
		JLabel lblNoOfRepeat = new JLabel("No. Of Repeat:");
		lblNoOfRepeat.setBounds(10, 77, 90, 14);
		panelBuzzer.add(lblNoOfRepeat);
		
		JSpinner spinner_1_2 = new JSpinner();
		spinner_1_2.setBounds(110, 74, 85, 20);
		panelBuzzer.add(spinner_1_2);
		
		btnSetBuzzerStatus_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				displayMessage("Set Buzzer State - Single Time");
				try
				{ 
					int onTime = (int) spinner_1.getValue();
					int offTime = (int) spinner_1_1.getValue();
					int repeat = (int) spinner_1_2.getValue();
					acr1552u.setBuzzerState((byte) onTime, (byte) offTime, (byte) repeat);
				}
				catch (Exception exception)
				{
					displayErrorMessage(exception.getMessage() + "\r\n");			
				}
			}
		});
		
		JPanel panelLEDControl = new JPanel();
		panelLEDControl.setLayout(null);
		panelLEDControl.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, new Color(255, 255, 255), new Color(160, 160, 160)), "LED Control", TitledBorder.LEFT, TitledBorder.TOP, null, new Color(0, 0, 0)));
		panelLEDControl.setBounds(10, 318, 349, 78);
		panelPeripheralControl.add(panelLEDControl);
		
		JButton btnGetLEDStatus = new JButton("Get LED Status");
		btnGetLEDStatus.setBounds(10, 46, 329, 23);
		panelLEDControl.add(btnGetLEDStatus);
		
		JCheckBox chckbxGreenLED = new JCheckBox("Green LED");
		chckbxGreenLED.setBounds(10, 16, 97, 23);
		panelLEDControl.add(chckbxGreenLED);
		
		JCheckBox chckbxBlueLED = new JCheckBox("Blue LED");
		chckbxBlueLED.setHorizontalAlignment(SwingConstants.CENTER);
		chckbxBlueLED.setBounds(144, 16, 97, 23);
		panelLEDControl.add(chckbxBlueLED);
		
		chckbxGreenLED.addActionListener(new ActionListener() {
		    @Override
		    public void actionPerformed(ActionEvent event) {
		        displayMessage("Set LED State");
	        	try
				{ 
	        		acr1552u.setLEDState(chckbxBlueLED.isSelected(), chckbxGreenLED.isSelected());
				}
				catch (Exception exception)
				{
					displayErrorMessage(exception.getMessage() + "\r\n");			
				}
		    }
		});
		
		chckbxBlueLED.addActionListener(new ActionListener() {
		    @Override
		    public void actionPerformed(ActionEvent event) {
		        displayMessage("Set LED State");
	        	try
				{ 
	        		acr1552u.setLEDState(chckbxBlueLED.isSelected(), chckbxGreenLED.isSelected());
				}
				catch (Exception exception)
				{
					displayErrorMessage(exception.getMessage() + "\r\n");			
				}
		    }
		});
		
		btnGetLEDStatus.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
		        displayMessage("Refresh LED");
	        	try
				{ 
	        		 byte LED = acr1552u.getLEDState();
	        		 chckbxBlueLED.setSelected((LED & 0x01) == 0x01);
	        		 chckbxGreenLED.setSelected((LED & 0x02) == 0x02);
				}
				catch (Exception exception)
				{
					displayErrorMessage(exception.getMessage() + "\r\n");			
				}
			}
		});
		
		JPanel panelUIBehaviour = new JPanel();
		panelUIBehaviour.setLayout(null);
		panelUIBehaviour.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, new Color(255, 255, 255), new Color(160, 160, 160)), "UI Behaviour", TitledBorder.LEFT, TitledBorder.TOP, null, new Color(0, 0, 0)));
		panelUIBehaviour.setBounds(10, 407, 349, 215);
		panelPeripheralControl.add(panelUIBehaviour);
		
		JButton btnSetUIBehaviour = new JButton("Set UI Behaviour");
		btnSetUIBehaviour.setBounds(10, 150, 329, 23);
		panelUIBehaviour.add(btnSetUIBehaviour);
		
		JCheckBox chckbxReadWriteActivity = new JCheckBox("Read/Write Activity     (LED Fast Blinking)");
		chckbxReadWriteActivity.setBounds(10, 16, 329, 23);
		panelUIBehaviour.add(chckbxReadWriteActivity);
		
		JButton btnGetUIBehaviour = new JButton("Get UI Behaviour");
		btnGetUIBehaviour.setBounds(10, 184, 329, 23);
		panelUIBehaviour.add(btnGetUIBehaviour);
		
		JCheckBox chckbxWaitingforPCCPresence = new JCheckBox("Waiting for PCC/Presence     (LED Slow Blinking)");
		chckbxWaitingforPCCPresence.setBounds(10, 42, 329, 23);
		panelUIBehaviour.add(chckbxWaitingforPCCPresence);
		
		JCheckBox chckbxPICCPresenceActivated = new JCheckBox("PICC Presence/Activated     (LED On)");
		chckbxPICCPresenceActivated.setBounds(10, 68, 329, 23);
		panelUIBehaviour.add(chckbxPICCPresenceActivated);
		
		JCheckBox chckbxPICCPresenceEvent = new JCheckBox("PICC Presence Event     (Short Buzzer Beep)");
		chckbxPICCPresenceEvent.setBounds(10, 94, 329, 23);
		panelUIBehaviour.add(chckbxPICCPresenceEvent);
		
		JCheckBox chckbxPICCRemovalEvent = new JCheckBox("PICC Removal Event     (Short Buzzer Beep)");
		chckbxPICCRemovalEvent.setBounds(10, 120, 329, 23);
		panelUIBehaviour.add(chckbxPICCRemovalEvent);
		btnSetUIBehaviour.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				displayMessage("Set UI Behaviour");
	        	try
				{ 
	        		 byte flag = 0x00; 
	        		 if (chckbxReadWriteActivity.isSelected())
	                     flag += 0x01;
	                 if (chckbxWaitingforPCCPresence.isSelected())
	                     flag += 0x02;
	                 if (chckbxPICCPresenceActivated.isSelected())
	                     flag += 0x04;
	                 if (chckbxPICCPresenceEvent.isSelected())
	                     flag += 0x08;
	                 if (chckbxPICCRemovalEvent.isSelected())
	                     flag += 0x10;
	        		 acr1552u.setUIBehaviour(flag);
				}
				catch (Exception exception)
				{
					displayErrorMessage(exception.getMessage() + "\r\n");			
				}
			}
		});
		btnGetUIBehaviour.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				displayMessage("Refresh UI Behaviour");
	        	try
				{ 
	        		 byte flag = acr1552u.getUIBehaviour();
	        		 chckbxReadWriteActivity.setSelected((flag & 0x01) == 0x01);
	        		 chckbxWaitingforPCCPresence.setSelected((flag & 0x02) == 0x02);
	        		 chckbxPICCPresenceActivated.setSelected((flag & 0x04) == 0x04);
	        		 chckbxPICCPresenceEvent.setSelected((flag & 0x08) == 0x08);
	        		 chckbxPICCRemovalEvent.setSelected((flag & 0x10) == 0x10);
				}
				catch (Exception exception)
				{
					displayErrorMessage(exception.getMessage() + "\r\n");			
				}
			}
		});
		
		panelNFC = new JPanel();
		tabbedPane.addTab("NFC", null, panelNFC, null);
		panelNFC.setLayout(null);
		
		JPanel panelPICCPollingType = new JPanel();
		panelPICCPollingType.setLayout(null);
		panelPICCPollingType.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, new Color(255, 255, 255), new Color(160, 160, 160)), "PICC Polling Type", TitledBorder.LEFT, TitledBorder.TOP, null, new Color(0, 0, 0)));
		panelPICCPollingType.setBounds(424, 11, 254, 344);
		panelNFC.add(panelPICCPollingType);
		
		JButton btnSetPollingType = new JButton("Set Polling Type");
		btnSetPollingType.setBounds(10, 280, 234, 23);
		panelPICCPollingType.add(btnSetPollingType);
		
		JCheckBox chckbxISO14443A = new JCheckBox("ISO14443A-3");
		chckbxISO14443A.setBounds(10, 16, 234, 23);
		panelPICCPollingType.add(chckbxISO14443A);
		
		JButton btnGetPollingType = new JButton("Get Polling Type");
		btnGetPollingType.setBounds(10, 314, 234, 23);
		panelPICCPollingType.add(btnGetPollingType);
		
		JCheckBox chckbxISO14443B = new JCheckBox("ISO14443B-3");
		chckbxISO14443B.setBounds(10, 42, 234, 23);
		panelPICCPollingType.add(chckbxISO14443B);
		
		JCheckBox chckbxApple = new JCheckBox("Apple ECP/VAS");
		chckbxApple.setBounds(10, 68, 234, 23);
		panelPICCPollingType.add(chckbxApple);
		
		JCheckBox chckbxFelica = new JCheckBox("Felica");
		chckbxFelica.setBounds(10, 94, 234, 23);
		panelPICCPollingType.add(chckbxFelica);
		
		JCheckBox chckbxSRI = new JCheckBox("SRI (ISO14443B-2)");
		chckbxSRI.setBounds(10, 120, 234, 23);
		panelPICCPollingType.add(chckbxSRI);
		
		JCheckBox chckbxCTS = new JCheckBox("CTS");
		chckbxCTS.setBounds(10, 250, 234, 23);
		panelPICCPollingType.add(chckbxCTS);
		
		JCheckBox chckbxInnovatron = new JCheckBox("Innovatron");
		chckbxInnovatron.setBounds(10, 224, 234, 23);
		panelPICCPollingType.add(chckbxInnovatron);
		
		JCheckBox chckbxPicoPass2 = new JCheckBox("PicoPass (ISO15693-2)");
		chckbxPicoPass2.setBounds(10, 198, 234, 23);
		panelPICCPollingType.add(chckbxPicoPass2);
		
		JCheckBox chckbxISO15693 = new JCheckBox("ISO15693");
		chckbxISO15693.setBounds(10, 172, 234, 23);
		panelPICCPollingType.add(chckbxISO15693);
		
		JCheckBox chckbxPicoPass = new JCheckBox("PicoPass (ISO14443B-2)");
		chckbxPicoPass.setBounds(10, 146, 234, 23);
		panelPICCPollingType.add(chckbxPicoPass);
		
		btnSetPollingType.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				displayMessage("Set Polling Type");
	        	try
				{ 
	        		 int cardType = 0x0000; 
	        		 
	        		 if (chckbxISO14443A.isSelected())
	        			 cardType += Acr1552U.CONTACTLESS_CARD_TYPE_ISO14443A_3;
	                 if (chckbxISO14443B.isSelected())
	                	 cardType += Acr1552U.CONTACTLESS_CARD_TYPE_ISO14443B_3;
	                 if (chckbxApple.isSelected())
	                	 cardType += 0x0080;
	                 if (chckbxFelica.isSelected())
	                	 cardType += 0x0004;
	                 if (chckbxSRI.isSelected())
	                	 cardType += 0x0040;
	                 if (chckbxPicoPass.isSelected())
	                	 cardType += 0x0100;
	                 if (chckbxISO15693.isSelected())
	                	 cardType += 0x0400;
	                 if (chckbxPicoPass2.isSelected())
	                	 cardType += 0x0200;
	                 if (chckbxInnovatron.isSelected())
	                	 cardType += 0x0020;
	                 if (chckbxCTS.isSelected())
	                	 cardType += Acr1552U.CONTACTLESS_CARD_TYPE_CTS;
	                 
	        		 acr1552u.setPollingType(cardType);
				}
				catch (Exception exception)
				{
					displayErrorMessage(exception.getMessage() + "\r\n");			
				}
			}
		});
		
		btnGetPollingType.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				displayMessage("Refresh Polling Type");
	        	try
				{ 
	        		 int cardType = acr1552u.getPollingType();
	        		 chckbxISO14443A.setSelected((cardType & Acr1552U.CONTACTLESS_CARD_TYPE_ISO14443A_3) == 0x0001);
	        		 chckbxISO14443B.setSelected((cardType & Acr1552U.CONTACTLESS_CARD_TYPE_ISO14443B_3) == 0x0002);
	        		 chckbxApple.setSelected((cardType & 0x0080) == 0x0080);
	        		 chckbxFelica.setSelected((cardType & 0x0004) == 0x0004);
	        		 chckbxSRI.setSelected((cardType & 0x0040) == 0x0040);
	        		 chckbxPicoPass.setSelected((cardType & 0x0100) == 0x0100);
	        		 chckbxISO15693.setSelected((cardType & 0x0400) == 0x0400);
	        		 chckbxPicoPass2.setSelected((cardType & 0x0200) == 0x0200);
	        		 chckbxInnovatron.setSelected((cardType & 0x0020) == 0x0020);
	        		 chckbxCTS.setSelected((cardType & Acr1552U.CONTACTLESS_CARD_TYPE_CTS) == 0x0800);
				}
				catch (Exception exception)
				{
					displayErrorMessage(exception.getMessage() + "\r\n");			
				}
			}
		});
		
		JPanel panelPICCType = new JPanel();
		panelPICCType.setLayout(null);
		panelPICCType.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, new Color(255, 255, 255), new Color(160, 160, 160)), "PICC Type", TitledBorder.LEFT, TitledBorder.TOP, null, new Color(0, 0, 0)));
		panelPICCType.setBounds(424, 366, 254, 179);
		panelNFC.add(panelPICCType);
		
		JButton btnGetPICCType = new JButton("Get PICC Type");
		btnGetPICCType.setBounds(10, 150, 229, 23);
		panelPICCType.add(btnGetPICCType);
		
		JRadioButton rdbtnTypeA = new JRadioButton("Type A, Part4");
		buttonGroupPICCStatusPICCType.add(rdbtnTypeA);
		rdbtnTypeA.setBounds(10, 120, 116, 23);
		panelPICCType.add(rdbtnTypeA);
		
		JRadioButton rdbtnFelica = new JRadioButton("Felica");
		buttonGroupPICCStatusPICCType.add(rdbtnFelica);
		rdbtnFelica.setBounds(10, 94, 116, 23);
		panelPICCType.add(rdbtnFelica);
		
		JRadioButton rdbtnMifare = new JRadioButton("Mifare");
		buttonGroupPICCStatusPICCType.add(rdbtnMifare);
		rdbtnMifare.setBounds(10, 68, 116, 23);
		panelPICCType.add(rdbtnMifare);
		
		JRadioButton rdbtnTopaz = new JRadioButton("Topaz");
		buttonGroupPICCStatusPICCType.add(rdbtnTopaz);
		rdbtnTopaz.setBounds(10, 42, 116, 23);
		panelPICCType.add(rdbtnTopaz);
		
		JRadioButton rdbtnNoPICC = new JRadioButton("No  PICC");
		buttonGroupPICCStatusPICCType.add(rdbtnNoPICC);
		rdbtnNoPICC.setBounds(10, 16, 116, 23);
		panelPICCType.add(rdbtnNoPICC);
		
		JRadioButton rdbtnInnovation = new JRadioButton("Innovation");
		buttonGroupPICCStatusPICCType.add(rdbtnInnovation);
		rdbtnInnovation.setBounds(123, 16, 116, 23);
		panelPICCType.add(rdbtnInnovation);
		
		JRadioButton rdbtnApple = new JRadioButton("Apple ECP/VAS");
		buttonGroupPICCStatusPICCType.add(rdbtnApple);
		rdbtnApple.setBounds(123, 42, 116, 23);
		panelPICCType.add(rdbtnApple);
		
		JRadioButton rdbtnSRIX = new JRadioButton("SRIX");
		buttonGroupPICCStatusPICCType.add(rdbtnSRIX);
		rdbtnSRIX.setBounds(123, 68, 116, 23);
		panelPICCType.add(rdbtnSRIX);
		
		JRadioButton rdbtnPicoPass = new JRadioButton("PicoPass");
		buttonGroupPICCStatusPICCType.add(rdbtnPicoPass);
		rdbtnPicoPass.setBounds(123, 94, 116, 23);
		panelPICCType.add(rdbtnPicoPass);
		
		JRadioButton rdbtnTypeB = new JRadioButton("Type B, Part4");
		buttonGroupPICCStatusPICCType.add(rdbtnTypeB);
		rdbtnTypeB.setBounds(123, 120, 116, 23);
		panelPICCType.add(rdbtnTypeB);
		
		JPanel panelAutoParameterProtocol = new JPanel();
		panelAutoParameterProtocol.setLayout(null);
		panelAutoParameterProtocol.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, new Color(255, 255, 255), new Color(160, 160, 160)), "Auto Parameter Protocol Selection (PPS) Setting", TitledBorder.LEFT, TitledBorder.TOP, null, new Color(0, 0, 0)));
		panelAutoParameterProtocol.setBounds(10, 11, 404, 83);
		panelNFC.add(panelAutoParameterProtocol);
		
		JButton btnSetAutoPPS = new JButton("Set Auto PPS");
		btnSetAutoPPS.setBounds(264, 16, 130, 23);
		panelAutoParameterProtocol.add(btnSetAutoPPS);
		
		JButton btnGetAutoPPS = new JButton("Get Auto PPS");
		btnGetAutoPPS.setBounds(264, 50, 130, 23);
		panelAutoParameterProtocol.add(btnGetAutoPPS);
		
		Choice choiceMax = new Choice();
		choiceMax.setBounds(145, 19, 113, 20);
		choiceMax.addItem("106 kbps");
		choiceMax.addItem("212 kbps");
		choiceMax.addItem("424 kbps");
		choiceMax.addItem("848 kbps");
		panelAutoParameterProtocol.add(choiceMax);
		
		Choice choiceCurrent = new Choice();
		choiceCurrent.setBounds(145, 53, 113, 20);
		choiceCurrent.addItem("106 kbps");
		choiceCurrent.addItem("212 kbps");
		choiceCurrent.addItem("424 kbps");
		choiceCurrent.addItem("848 kbps");
		panelAutoParameterProtocol.add(choiceCurrent);
		
		btnSetAutoPPS.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				displayMessage("Set Polling Type");
	        	try
				{ 
	        		 byte maxSpeed = 0x00; 
	        		 
	        		 switch (choiceMax.getSelectedIndex())
	                 {
	                     case 1:
	                         maxSpeed = Acr1552U.PPS_212K_BPS;
	                         break;
	                     case 2:
	                         maxSpeed = Acr1552U.PPS_424K_BPS;
	                         break;
	                     case 3:
	                         maxSpeed = Acr1552U.PPS_848K_BPS;
	                         break;
	                     default:
	                         maxSpeed = Acr1552U.PPS_106K_BPS;
	                         break;
	                 }

	        		 acr1552u.setAutoPPS(maxSpeed);
				}
				catch (Exception exception)
				{
					displayErrorMessage(exception.getMessage() + "\r\n");			
				}
			}
		});
		
		btnGetAutoPPS.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				displayMessage("Refresh Polling Type");
	        	try
				{ 
	        		Acr1552U.AUTO_PPS_SETTING speed = acr1552u.getAutoPPS();
	        		
	        		choiceMax.select(speed.maxSpeed);
	        		choiceCurrent.select(speed.currentSpeed);
				}
				catch (Exception exception)
				{
					displayErrorMessage(exception.getMessage() + "\r\n");			
				}
			}
		});
		
		JLabel lblMaxSpeed = new JLabel("Max Speed:");
		lblMaxSpeed.setBounds(10, 20, 90, 14);
		panelAutoParameterProtocol.add(lblMaxSpeed);
		
		JLabel lblCurrendSpeed = new JLabel("Current Speed:");
		lblCurrendSpeed.setBounds(10, 54, 90, 14);
		panelAutoParameterProtocol.add(lblCurrendSpeed);
		
		JPanel panelRFControl = new JPanel();
		panelRFControl.setLayout(null);
		panelRFControl.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, new Color(255, 255, 255), new Color(160, 160, 160)), "RF Control", TitledBorder.LEFT, TitledBorder.TOP, null, new Color(0, 0, 0)));
		panelRFControl.setBounds(10, 105, 404, 76);
		panelNFC.add(panelRFControl);
		
		JButton btnSetRFControl = new JButton("Set RF Control");
		btnSetRFControl.setBounds(10, 46, 384, 23);
		panelRFControl.add(btnSetRFControl);
		
		JRadioButton rdbtnRFOff = new JRadioButton("RF Off");
		buttonGroupPICCStatusRFControl.add(rdbtnRFOff);
		rdbtnRFOff.setBounds(10, 16, 75, 23);
		panelRFControl.add(rdbtnRFOff);
		
		JRadioButton rdbtnRFOnWithPolling = new JRadioButton("RF On with Polling");
		buttonGroupPICCStatusRFControl.add(rdbtnRFOnWithPolling);
		rdbtnRFOnWithPolling.setBounds(87, 16, 145, 23);
		panelRFControl.add(rdbtnRFOnWithPolling);
		
		JRadioButton rdbtnRFOnWithoutPolling = new JRadioButton("RF On without Polling");
		buttonGroupPICCStatusRFControl.add(rdbtnRFOnWithoutPolling);
		rdbtnRFOnWithoutPolling.setBounds(234, 16, 160, 23);
		panelRFControl.add(rdbtnRFOnWithoutPolling);
		
		btnSetRFControl.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				displayMessage("Set RF Control");
	        	try
				{ 
	        		 byte RFStatus = 0x00; 
	        		 
	        		 if (rdbtnRFOff.isSelected())
	        			 RFStatus = 0x00;
	        		 else if (rdbtnRFOnWithPolling.isSelected())
	                	 RFStatus = 0x01;
	        		 else if (rdbtnRFOnWithoutPolling.isSelected())
	                	 RFStatus = 0x02;

	        		 acr1552u.setRFControl(RFStatus);
				}
				catch (Exception exception)
				{
					displayErrorMessage(exception.getMessage() + "\r\n");			
				}
			}
		});
		
		JPanel panelPCDPICCStatus = new JPanel();
		panelPCDPICCStatus.setLayout(null);
		panelPCDPICCStatus.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, new Color(255, 255, 255), new Color(160, 160, 160)), "PCD/PICC Status", TitledBorder.LEFT, TitledBorder.TOP, null, new Color(0, 0, 0)));
		panelPCDPICCStatus.setBounds(10, 424, 404, 102);
		panelNFC.add(panelPCDPICCStatus);
		
		JButton btnGetPICCStatus = new JButton("Get PCD/PICC Status");
		btnGetPICCStatus.setBounds(10, 72, 384, 23);
		panelPCDPICCStatus.add(btnGetPICCStatus);
		
		JRadioButton rdbtnRFOffPCDPICC = new JRadioButton("RF Off");
		buttonGroupPICCStatus.add(rdbtnRFOffPCDPICC);
		rdbtnRFOffPCDPICC.setBounds(10, 16, 75, 23);
		panelPCDPICCStatus.add(rdbtnRFOffPCDPICC);
		
		JRadioButton rdbtnNoPCDPICC = new JRadioButton("No PICC");
		buttonGroupPICCStatus.add(rdbtnNoPCDPICC);
		rdbtnNoPCDPICC.setBounds(150, 16, 145, 23);
		panelPCDPICCStatus.add(rdbtnNoPCDPICC);
		
		JRadioButton rdbtnPICCReady = new JRadioButton("PICC Ready");
		buttonGroupPICCStatus.add(rdbtnPICCReady);
		rdbtnPICCReady.setBounds(10, 42, 121, 23);
		panelPCDPICCStatus.add(rdbtnPICCReady);
		
		JRadioButton rdbtnPICCSelected = new JRadioButton("PICC Selected/Activated");
		buttonGroupPICCStatus.add(rdbtnPICCSelected);
		rdbtnPICCSelected.setBounds(150, 42, 211, 23);
		panelPCDPICCStatus.add(rdbtnPICCSelected);
		
		btnGetPICCStatus.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				displayMessage("Refresh PCD/PICC Status");
	        	try
				{ 
	        		 byte status = acr1552u.getPCDPICCStatus(); 
	        		 
	        		 switch (status)
		                {
		                    case 0:
		                    	rdbtnRFOffPCDPICC.setSelected(true);
		                        break;
		                    case 1:
		                    	rdbtnNoPCDPICC.setSelected(true);
		                        break;
		                    case 2:
		                    	rdbtnPICCReady.setSelected(true);
		                        break;
		                    case 3:
		                    	rdbtnPICCSelected.setSelected(true);
		                        break;
		                }
				}
				catch (Exception exception)
				{
					displayErrorMessage(exception.getMessage() + "\r\n");			
				}
			}
		});
		
		btnGetPICCType.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				displayMessage("Refresh PICC Type and PCD/PICC Status");
	        	try
				{
	        		int type = acr1552u.getPICCType();
	        		int mode = (type & 0xFF) >> 8;
					switch (mode)
	                {
		                case 0:
	                    	rdbtnRFOffPCDPICC.setSelected(true);
	                        break;
	                    case 1:
	                    	rdbtnNoPCDPICC.setSelected(true);
	                        break;
	                    case 2:
	                    	rdbtnPICCReady.setSelected(true);
	                        break;
	                    case 3:
	                    	rdbtnPICCSelected.setSelected(true);
	                        break;
	                }
					
					switch ((byte)type)
	                {
	                    case (byte) 0xCC:
	                        rdbtnNoPICC.setSelected(true);
	                        break;
	                    case 0x04:
	                    	rdbtnTopaz.setSelected(true);
	                        break;
	                    case 0x10:
	                    	rdbtnMifare.setSelected(true);
	                        break;
	                    case 0x11:
	                    	rdbtnFelica.setSelected(true);
	                        break;
	                    case 0x20:
	                    	rdbtnTypeA.setSelected(true);
	                        break;
	                    case 0x23:
	                    	rdbtnTypeB.setSelected(true);
	                        break;
	                    case 0x25:
	                    	rdbtnInnovation.setSelected(true);
	                        break;
	                    case 0x27:
	                    	rdbtnApple.setSelected(true);
	                        break;
	                    case 0x28:
	                    	rdbtnSRIX.setSelected(true);
	                        break;
	                    case 0x30:
	                    	rdbtnPicoPass.setSelected(true);
	                        break;
	                    default:
	                        break;
	                }
				}
				catch (Exception exception)
				{
					displayErrorMessage(exception.getMessage() + "\r\n");			
				}
			}
		});
		
		JPanel panelPollingOption = new JPanel();
		panelPollingOption.setLayout(null);
		panelPollingOption.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, new Color(255, 255, 255), new Color(160, 160, 160)), "Polling Option", TitledBorder.LEFT, TitledBorder.TOP, null, new Color(0, 0, 0)));
		panelPollingOption.setBounds(10, 192, 404, 221);
		panelNFC.add(panelPollingOption);
		
		JButton btnSetPolling = new JButton("Set Polling");
		btnSetPolling.setBounds(247, 152, 147, 23);
		panelPollingOption.add(btnSetPolling);
		
		JCheckBox chckbxEnablePolling = new JCheckBox("Enable Polling");
		chckbxEnablePolling.setBounds(10, 16, 163, 23);
		panelPollingOption.add(chckbxEnablePolling);
		
		JButton btnGetPolling = new JButton("Get Polling");
		btnGetPolling.setBounds(247, 186, 147, 23);
		panelPollingOption.add(btnGetPolling);
		
		JCheckBox chckbxEnableExtraMifare = new JCheckBox("Enable extra Mifare type identification for Part 3 card  in ATR");
		chckbxEnableExtraMifare.setBounds(10, 42, 384, 23);
		panelPollingOption.add(chckbxEnableExtraMifare);
		
		JCheckBox chckbxEnablePart = new JCheckBox("Enable ATR for SmartMX/JCOS card with Mifare emulation");
		chckbxEnablePart.setBounds(10, 68, 384, 23);
		panelPollingOption.add(chckbxEnablePart);
		
		JCheckBox chckbxEnableRfOff = new JCheckBox("Enable RF Off");
		chckbxEnableRfOff.setBounds(196, 16, 163, 23);
		panelPollingOption.add(chckbxEnableRfOff);
		
		JPanel panel = new JPanel();
		panel.setBorder(new LineBorder(new Color(0, 0, 0)));
		panel.setBounds(116, 98, 121, 23);
		panelPollingOption.add(panel);
		
		JLabel lblUSBSuspend = new JLabel("USB Suspend (D2)");
		panel.add(lblUSBSuspend);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new LineBorder(new Color(0, 0, 0)));
		panel_1.setBounds(37, 98, 81, 23);
		panelPollingOption.add(panel_1);
		
		JLabel lblUSBActive = new JLabel("USB Active");
		panel_1.add(lblUSBActive);
		
		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new LineBorder(new Color(0, 0, 0)));
		panel_2.setBounds(116, 120, 121, 23);
		panelPollingOption.add(panel_2);
		
		lblSuspendD200Option = new JLabel("250 ms");
		panel_2.add(lblSuspendD200Option);
		
		JPanel panel_1_1 = new JPanel();
		panel_1_1.setBorder(new LineBorder(new Color(0, 0, 0)));
		panel_1_1.setBounds(37, 120, 81, 23);
		panelPollingOption.add(panel_1_1);
		
		lblActiveD000Option = new JLabel("No RF Off");
		panel_1_1.add(lblActiveD000Option);
		
		JPanel panel_3 = new JPanel();
		panel_3.setBorder(new LineBorder(new Color(0, 0, 0)));
		panel_3.setBounds(116, 142, 121, 23);
		panelPollingOption.add(panel_3);
		
		lblSuspendD201Option = new JLabel("500 ms");
		panel_3.add(lblSuspendD201Option);
		
		JPanel panel_1_2 = new JPanel();
		panel_1_2.setBorder(new LineBorder(new Color(0, 0, 0)));
		panel_1_2.setBounds(37, 142, 81, 23);
		panelPollingOption.add(panel_1_2);
		
		lblActiveD001Option = new JLabel("No RF Off");
		panel_1_2.add(lblActiveD001Option);
		
		JPanel panel_2_1 = new JPanel();
		panel_2_1.setBorder(new LineBorder(new Color(0, 0, 0)));
		panel_2_1.setBounds(116, 164, 121, 23);
		panelPollingOption.add(panel_2_1);
		
		lblSuspendD210Option = new JLabel("1000 ms");
		panel_2_1.add(lblSuspendD210Option);
		
		JPanel panel_1_1_1 = new JPanel();
		panel_1_1_1.setBorder(new LineBorder(new Color(0, 0, 0)));
		panel_1_1_1.setBounds(37, 164, 81, 23);
		panelPollingOption.add(panel_1_1_1);
		
		lblActiveD010Option = new JLabel("No RF Off");
		panel_1_1_1.add(lblActiveD010Option);
		
		JPanel panel_2_1_1 = new JPanel();
		panel_2_1_1.setBorder(new LineBorder(new Color(0, 0, 0)));
		panel_2_1_1.setBounds(116, 186, 121, 23);
		panelPollingOption.add(panel_2_1_1);
		
		lblSuspendD211Option = new JLabel("2500 ms");
		panel_2_1_1.add(lblSuspendD211Option);
		
		JPanel panel_1_1_1_1 = new JPanel();
		panel_1_1_1_1.setBorder(new LineBorder(new Color(0, 0, 0)));
		panel_1_1_1_1.setBounds(37, 186, 81, 23);
		panelPollingOption.add(panel_1_1_1_1);
		
		lblActiveD011Option = new JLabel("No RF Off");
		panel_1_1_1_1.add(lblActiveD011Option);
		
		JRadioButton rdbtn250 = new JRadioButton("");
		buttonGroupPollingOption.add(rdbtn250);
		rdbtn250.setBounds(10, 120, 21, 23);
		panelPollingOption.add(rdbtn250);
		
		JRadioButton rdbtn500 = new JRadioButton("");
		buttonGroupPollingOption.add(rdbtn500);
		rdbtn500.setBounds(10, 142, 21, 23);
		panelPollingOption.add(rdbtn500);
		
		JRadioButton rdbtn1000 = new JRadioButton("");
		buttonGroupPollingOption.add(rdbtn1000);
		rdbtn1000.setBounds(10, 164, 21, 23);
		panelPollingOption.add(rdbtn1000);
		
		JRadioButton rdbtn2500 = new JRadioButton("");
		buttonGroupPollingOption.add(rdbtn2500);
		rdbtn2500.setBounds(10, 186, 21, 23);
		panelPollingOption.add(rdbtn2500);
		chckbxEnableRfOff.addActionListener(new ActionListener() {
		    @Override
		    public void actionPerformed(ActionEvent event) {
		        JCheckBox cb = (JCheckBox) event.getSource();
		        
		        setUSBText(cb.isSelected());
		    }
		});
		
		btnSetPolling.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				displayMessage("Set Polling Option");
				try
				{		
					Acr1552U.PICC_POLLING_OPTION cPollingOption = new Acr1552U.PICC_POLLING_OPTION();
                	cPollingOption.enablePolling = chckbxEnablePolling.isSelected();
        			cPollingOption.enableRFOff = chckbxEnableRfOff.isSelected();
        			cPollingOption.enablePart3 = chckbxEnableExtraMifare.isSelected();
        			cPollingOption.enablePart4 = chckbxEnablePart.isSelected();
        			
	                if (rdbtn250.isSelected())
	                    cPollingOption.pollInterval = 0x00;
	                else if (rdbtn500.isSelected())
	                    cPollingOption.pollInterval = 0x01;
	                else if (rdbtn1000.isSelected())
	                    cPollingOption.pollInterval = 0x02;
	                else if (rdbtn2500.isSelected())
	                    cPollingOption.pollInterval = 0x03;
	                
	                acr1552u.setPollingOption(cPollingOption);
				}
				catch (Exception exception)
				{
					displayErrorMessage(exception.getMessage() + "\r\n");			
				}
			}
		});
		
		btnGetPolling.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				displayMessage("Refresh Polling Option");
				try
				{		
					Acr1552U.PICC_POLLING_OPTION cPollingOption = new Acr1552U.PICC_POLLING_OPTION();
					cPollingOption = acr1552u.getPollingOption();
					
					chckbxEnablePolling.setSelected(cPollingOption.enablePolling);
					chckbxEnableRfOff.setSelected(cPollingOption.enableRFOff);
					chckbxEnableExtraMifare.setSelected(cPollingOption.enablePart3);
					chckbxEnablePart.setSelected(cPollingOption.enablePart4);
					
					setUSBText(cPollingOption.enableRFOff);
					
					switch (cPollingOption.pollInterval)
	                {
	                    case 0:
	                    	rdbtn250.setSelected(true);
	                        break;
	                    case 1:
	                    	rdbtn500.setSelected(true);
	                        break;
	                    case 2:
	                    	rdbtn1000.setSelected(true);
	                        break;
	                    case 3:
	                    	rdbtn2500.setSelected(true);
	                        break;
	                }
				}
				catch (Exception exception)
				{
					displayErrorMessage(exception.getMessage() + "\r\n");			
				}
			}
		});
		frmWrr.getContentPane().setLayout(groupLayout);
	}
	
	void setEnabled(Component component, boolean enable) 
	{
		component.setEnabled(enable);
		// Check for instance of container.
		if (component instanceof Container) {
			// Continue setEnabled if there are more components.
		    Component components[] = ((Container) component).getComponents();
		    for (int i = 0; i < components.length; i++) {
		    	if (components[i] != null) {
		    		setEnabled(components[i], enable); // <- Here is the recursive call.
	    		}
	    	}
	    }
	}
	
	void resetControls()
	{
		textArea.setText("");
		btnInitialize.setEnabled(true);
		btnConnect.setEnabled(false);
		ButtonReset.setEnabled(false);
		comboBoxReader.removeAllItems();
		comboBoxReader.setEnabled(false);
		setEnabled(tabbedPane,false);
		
		displayMessage("Program Ready");
	}
	
	void displayMessage(String message)
	{
		textArea.append(message + "\r\n");
	}

	void displayErrorMessage(String message)
	{		
		JOptionPane.showMessageDialog(null, message, "Failed", JOptionPane.ERROR_MESSAGE);
		displayMessage(message);
	}
	
	public void onSendCommand(Pcsc.ReaderEvent.TransmitApduEventArg event)
	{
		displayMessage("<< " + event.getAsString(true));
	}

	public void onReceiveCommand(Pcsc.ReaderEvent.TransmitApduEventArg event)
	{
		displayMessage(">> " + event.getAsString(true));
	}
	
	public void setUSBText(boolean enable)
	{
        if(enable)
        {
        	lblActiveD000Option.setText("250 ms");
        	lblActiveD001Option.setText("500 ms");
        	lblActiveD010Option.setText("1000 ms");
        	lblActiveD011Option.setText("2500 ms");
        	lblSuspendD200Option.setText("500 ms");
        	lblSuspendD201Option.setText("1000 ms");
        	lblSuspendD210Option.setText("2500 ms");
        	lblSuspendD211Option.setText("2500 ms");
        }
        else
        {
        	lblActiveD000Option.setText("No RF Off");
        	lblActiveD001Option.setText("No RF Off");
        	lblActiveD010Option.setText("No RF Off");
        	lblActiveD011Option.setText("No RF Off");
        	lblSuspendD200Option.setText("250 ms");
        	lblSuspendD201Option.setText("500 ms");
        	lblSuspendD210Option.setText("1000 ms");
        	lblSuspendD211Option.setText("2500 ms");
        }
	}

	@Override
	public void keyPressed(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
