/****************************************************************************
  
   Program                :  uFr Simple
   File                   :  GLFunctions.java 
   Description            :  Other functions
   Author                 :  VladanS
   Manufacturer           :  D-Logic
   Development enviroment :  NetBeans 7.2 
   Java Platform          :  JDK 1.7
   Revisions	          :  
   Version                :  1.6
   
*****************************************************************************/

package uFRSimple;
import java.awt.event.*;
import javax.swing.*;
public class GLFunctions 
{
      private static final int KEYLENGTH=6;
      String[] ERR_CODE = new String[180];
      
  public void ERRORS_CODE(int iResult, JLabel lblFunctionErrorCodePane, JLabel lblFunctionErrorExplainPane) {
       
            ERR_CODE[0] = " DL_OK ";
            ERR_CODE[1] = "COMMUNICATION_ERROR";
            ERR_CODE[2] = "CHKSUM_ERROR";
            ERR_CODE[3] = "READING_ERROR";
            ERR_CODE[4] = "WRITING_ERROR";
            ERR_CODE[5] = "BUFFER_OVERFLOW";
            ERR_CODE[6] = "MAX_ADDRESS_EXCEEDED";
            ERR_CODE[7] = "MAX_KEY_INDEX_EXCEEDED";
            ERR_CODE[8] = "NO_CARD";
            ERR_CODE[9] = "COMMAND_NOT_SUPPORTED";
            ERR_CODE[10] = " FORBIDEN_DIRECT_WRITE_IN_SECTOR_TRAILER";
            ERR_CODE[11] = " ADDRESSED_BLOCK_IS_NOT_SECTOR_TRAILER  ";
            ERR_CODE[12] = " WRONG_ADDRESS_MODE  ";
            ERR_CODE[13] = " WRONG_ACCESS_BITS_VALUES  ";
            ERR_CODE[14] = " AUTH_ERROR  ";
            ERR_CODE[15] = " PARAMETERS_ERROR  ";
            ERR_CODE[16] = " MAX_SIZE_EXCEEDED  ";

            ERR_CODE[80] = " COMMUNICATION_BREAK  ";
            ERR_CODE[81] = " NO_MEMORY_ERROR  ";
            ERR_CODE[82] = " CAN_NOT_OPEN_READER  ";
            ERR_CODE[83] = " READER_NOT_SUPPORTED  ";
            ERR_CODE[84] = " READER_OPENING_ERROR  ";
            ERR_CODE[85] = " READER_PORT_NOT_OPENED  ";
            ERR_CODE[86] = " CANT_CLOSE_READER_PORT  ";

            ERR_CODE[112] = " WRITE_VERIFICATION_ERROR  ";
            ERR_CODE[113] = "  BUFFER_SIZE_EXCEEDED  ";
            ERR_CODE[114] = " VALUE_BLOCK_INVALID  ";
            ERR_CODE[115] = " VALUE_BLOCK_ADDR_INVALID  ";
            ERR_CODE[116] = " VALUE_BLOCK_MANIPULATION_ERROR  ";
            ERR_CODE[117] = " WRONG_UI_MODE ";
            ERR_CODE[118] = " KEYS_LOCKED ";
            ERR_CODE[119] = " KEYS_UNLOCKED ";
            ERR_CODE[120] = " WRONG_PASSWORD ";
            ERR_CODE[121] = " CAN_NOT_LOCK_DEVICE ";
            ERR_CODE[122] = " CAN_NOT_UNLOCK_DEVICE ";
            ERR_CODE[123] = " DEVICE_EEPROM_BUSY ";
            ERR_CODE[124] = " RTC_SET_ERROR ";

            ERR_CODE[160] = " FT_STATUS_ERROR_1 ";
            ERR_CODE[161] = " FT_STATUS_ERROR_2 ";
            ERR_CODE[162] = " FT_STATUS_ERROR_3 ";
            ERR_CODE[163] = " FT_STATUS_ERROR_4 ";
            ERR_CODE[164] = " FT_STATUS_ERROR_5 ";
            ERR_CODE[165] = " FT_STATUS_ERROR_6 ";
            ERR_CODE[166] = " FT_STATUS_ERROR_7 ";
            ERR_CODE[167] = " FT_STATUS_ERROR_8 ";

        lblFunctionErrorCodePane.setText("0x" + Integer.toHexString(iResult).toUpperCase());
        lblFunctionErrorExplainPane.setText(ERR_CODE[iResult]);
    }
     
  /** Dec to hex convesion and vice versa for textfields
   * 
   * @param pnlContainer   panel textbox fields
   * @param sKeyName       key name
   * @param evt            CheckBox Selected ? 
   */ 
  public void CheckBoxHexDec(JPanel pnlContainer,String sKeyName,java.awt.event.ItemEvent evt)
  {               
        for (int br = 0; br < pnlContainer.getComponentCount(); ++br) {
          if (pnlContainer.getComponent(br).getName() == sKeyName) {
              if (((JTextField) pnlContainer.getComponent(br)).getText().trim().isEmpty()) {
                  continue;
              } else {
                  if (evt.getStateChange() == ItemEvent.SELECTED) {
                      ((JTextField) pnlContainer.getComponent(br)).setText(Integer.toHexString(Short.parseShort(((JTextField) pnlContainer.getComponent(br)).getText())).toUpperCase());
                  } else {

                      ((JTextField) pnlContainer.getComponent(br)).setText(Integer.toString(Short.parseShort(((JTextField) pnlContainer.getComponent(br)).getText(), 16)));
                  }

              }
          }

      }
  }

  /**  Hex to dec conversion for keys
   *    
   * @param pnlContainer   panel container with dynamic textfields
   * @param sKeyName       name of key
   * @param CheckBox       which checkbox is selected?
   * @return               array of bytes
   */
  public byte[] DecHexConversionForKey(JPanel pnlContainer,String sKeyName,JCheckBox CheckBox)
  {
       byte bCountIndex=0;
       byte[]KEY=new byte[KEYLENGTH];
       
      if (CheckBox.isSelected()) {
          for (int iCounter = 0; iCounter <pnlContainer.getComponentCount(); iCounter++) {
              if (pnlContainer.getComponent(iCounter).getName() == sKeyName) {
                  KEY[bCountIndex] = (byte) Short.parseShort(((JTextField) pnlContainer.getComponent(iCounter)).getText(), 16);
                  bCountIndex++;
              }
          }
      } else {
          for (int iCounter = 0; iCounter < pnlContainer.getComponentCount(); iCounter++) {
             
              if (pnlContainer.getComponent(iCounter).getName() == sKeyName) {
                  KEY[bCountIndex] = (byte) Short.parseShort(((JTextField) (pnlContainer.getComponent(iCounter))).getText());
                  bCountIndex++;
              }
          }
      }
       return KEY;
  }
 /**  Filler bytes behind the entered string   
  *   
  * @param bGetBytesArray   
  * @param iDataLength
  * @param iMaxBytes
  * @return       
  */
  public byte[] WriteArray(byte[] bGetBytesArray,int iDataLength,int iMaxBytes)
        {
            byte[] bCloneArray = new byte[iMaxBytes];
            System.arraycopy(bGetBytesArray,0, bCloneArray,0, bGetBytesArray.length);
            for (int br = bGetBytesArray.length; br < iDataLength; br++)
            {
                bCloneArray[br] = 32;
            }
                                    
            return bCloneArray;

        }
  
  
  
}

