

package ufrcoderadvance;

import java.awt.Color;
import java.awt.event.*;
import java.beans.PropertyVetoException;
import java.util.regex.*;
import javax.swing.*;

public class Global implements KeyListener,FocusListener,ActionListener
{
     JTextField tf;
     
     String[] ERR_CODE=new String[180];
     JCheckBox HEX_BOX=new JCheckBox();
     String TEXTFIELD=null;
     
     private static final int KEYLENGTH=6;
     
     
     @Override
	public void keyPressed(KeyEvent e) {
      
     }
     
  @Override
	public void keyReleased(KeyEvent e) {


	}
  @Override
	public void keyTyped(KeyEvent arg0) {
        JTextField tf=(JTextField) arg0.getSource();    
        
       // System.out.println(tf.getName());
        
        try{                                       
        if (HEX_BOX.isSelected() && tf.getName()==TEXTFIELD){
          if (tf.getText().length()>1) {
                arg0.consume();
            }
          boolean bol_check=Pattern.matches("[a-fA-F_0-9]", String.valueOf(arg0.getKeyChar()));
          if (!bol_check){
              arg0.consume();             
          }
        }
        else
        {
           if (tf.getText().length()>2) {
                arg0.consume();
            }
           boolean bol_check=Pattern.matches("[0-9]", String.valueOf(arg0.getKeyChar()));
           if (!bol_check){
              arg0.consume();             
          }
        }
        }catch(NumberFormatException NFE){
                     
        }
        
  }
  
  @Override
  public void focusGained(FocusEvent arg0) {
		
                
	}
  
  
    @Override
    public void focusLost(FocusEvent arg)  {

        String sMessages = null;        
        JTextField tf = (JTextField) arg.getSource();         
        try {
            if (tf.getText().trim().isEmpty() || (Integer.parseInt(tf.getText(),16)>255)) {

                if (HEX_BOX.isSelected() && tf.getName()==TEXTFIELD) {
                    sMessages = "Must enter a hex value between 0 and FF !";
                    JOptionPane.showMessageDialog(new frmMain().getRootPane(), sMessages, "Warning !", JOptionPane.WARNING_MESSAGE);
                    tf.requestFocus();
                    tf.selectAll();
                    return;
                } 
            }
                    sMessages = "Must enter a decimal value between 0 and 255 !";
                    if (tf.getText().trim().isEmpty() || Short.parseShort(tf.getText()) > 255 || Short.parseShort(tf.getText())<0) {
                        JOptionPane.showMessageDialog(new frmMain().getRootPane(), sMessages, "Warning !", JOptionPane.WARNING_MESSAGE);
                        tf.requestFocus();
                        tf.selectAll();
                        return;
                    }
                
               
            }catch(NumberFormatException NFE){
                // tf.requestFocus();
                // tf.selectAll();    
            }  
         }
        
  @Override
  public void actionPerformed(ActionEvent e)
   {
      
   }
      
    public void Check_box(JPanel pnlContainer, String key_name, java.awt.event.ItemEvent evt) {
         short conv=0;
        if (evt.getStateChange()==ItemEvent.SELECTED)
                {
                for (int br=0;br<pnlContainer.getComponentCount();++br)
                   {
                if (pnlContainer.getComponent(br).getName()==key_name)
                     {
                        conv=Short.parseShort(((JTextField)pnlContainer.getComponent(br)).getText());
                       ((JTextField)pnlContainer.getComponent(br)).setText(Integer.toHexString(conv).toUpperCase());
                     }
                  }
                }
        else
        {
             for (int br=0;br<pnlContainer.getComponentCount();++br)
                   {
                if (pnlContainer.getComponent(br).getName()==key_name)
                     {
                        conv=Short.parseShort(((JTextField)pnlContainer.getComponent(br)).getText(),16);
                       ((JTextField)pnlContainer.getComponent(br)).setText(Integer.toString(conv));
                     }
                  }
         }

    }

  public void CreateKey(int key_X,int key_Y,int key_height,int key_width,String key_name,JPanel pnlContainer)
  {
     
      for(int br=0;br<6;br++)
      {          
          tf=new JTextField();
          tf.setBounds(key_X+(key_width*br+2),key_Y, 30, 21);
          tf.setBorder(javax.swing.BorderFactory.createLineBorder(Color.BLACK));
          tf.setHorizontalAlignment(javax.swing.JTextField.CENTER);
          tf.setFont(new java.awt.Font("Verdana",1,11));
          
          tf.setName(key_name);
          
          tf.addKeyListener(this);
          tf.addFocusListener(this);
          tf.addActionListener(this);
          tf.setText("255");
          
          tf.setVisible(true);
          pnlContainer.add(tf);
      }
          pnlContainer.repaint();
  }
  public void AddFrame(JInternalFrame internal_frame,JDesktopPane pnlContainer,JLabel header_panel,String header_text)
  {      
      JInternalFrame[]child_frame=pnlContainer.getAllFrames();
      JInternalFrame jframe=new JInternalFrame();
      pnlContainer.remove(jframe);
      try
      {    
      for(JInternalFrame int_frame:child_frame){
       //   if(int_frame.getName().equals(internal_frame.getName()))
       //   {
           pnlContainer.remove(int_frame);
           pnlContainer.repaint();          
           header_panel.setText(header_text);
           header_panel.setVisible(true);           
        //  }
      }
          internal_frame.pack();
          internal_frame.setBounds(10, 0, 565, 386);
          internal_frame.setVisible(true);          
          pnlContainer.add(internal_frame);
          header_panel.setText(header_text);
          header_panel.setVisible(true);
          pnlContainer.repaint();
          internal_frame.setSelected(true);                           
      }catch(PropertyVetoException pe)
      {
          
      }      
  }
  public byte[] PK_KEY(JPanel pnlHeader)
    {
       JTextField text = null; 
       byte bPosition=0; 
       byte bComponentCount=0;
       byte count=0;
       byte[]pk_key=new byte[6];
        for (;bComponentCount<pnlHeader.getComponentCount();bComponentCount++)
                {                   
                    if (pnlHeader.getComponent(bComponentCount).getName()=="txtPKKey")
                    {                                          
                        pk_key[count]=(byte)Integer.parseInt(((JTextField)(pnlHeader.getComponent(bComponentCount))).getText());                                 
                       count++;                                                                  
                    }
                }
       return pk_key;
    }
  
  
  public byte[] KEY(JPanel pnlKEYs,String name_of_key,JCheckBox check_box)
  {
       byte bCountIndex=0;
       byte[]KEY=new byte[KEYLENGTH];
       
      if (check_box.isSelected()) {
          for (int iCounter = 0; iCounter <pnlKEYs.getComponentCount(); iCounter++) {
              if (pnlKEYs.getComponent(iCounter).getName() == name_of_key) {
                  KEY[bCountIndex] = (byte) Integer.parseInt(((JTextField) pnlKEYs.getComponent(iCounter)).getText(), 16);
                  bCountIndex++;
              }
          }
      } else {
          for (int iCounter = 0; iCounter < pnlKEYs.getComponentCount(); iCounter++) {
             
              if (pnlKEYs.getComponent(iCounter).getName() == name_of_key) {
                  KEY[bCountIndex] = (byte) Integer.parseInt(((JTextField) (pnlKEYs.getComponent(iCounter))).getText());
                  bCountIndex++;
              }
          }
      }
       return KEY;

         
  }

  public void ERRORS_CODE(int result,JLabel function_error,JLabel function_expl)
  {
                ERR_CODE[0] = "DL_OK ";
                ERR_CODE[1] = "COMMUNICATION_ERROR ";
                ERR_CODE[3] = "READING_ERROR ";
                ERR_CODE[4] = "WRITING_ERROR ";
                ERR_CODE[6] = "MAX_ADDRESS_EXCEEDED ";
                ERR_CODE[7] = "MAX_KEY_INDEX_EXCEEDED ";
                ERR_CODE[8] = "NO_CARD ";
                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[80] = "COMMUNICATION_BREAK ";
                ERR_CODE[82] = "CAN_NOT_OPEN_READER ";
                ERR_CODE[84] = "READER_OPENING_ERROR ";
                ERR_CODE[85] = "READER_PORT_NOT_OPENED ";
                ERR_CODE[114] = "VALUE_BLOCK_INVALID ";
                ERR_CODE[113] = "BUFFER_SIZE_EXCEEDED ";
                ERR_CODE[115] = "VALUE_BLOCK_ADDR_INVALID ";
                ERR_CODE[116] = "VALUE_BLOCK_MANIPULATION_ERROR ";
                ERR_CODE[117] = "WRONG_UI_MODE";
                ERR_CODE[121] = "CAN_NOT_LOCK_DEVICE";
                ERR_CODE[122] = "CAN_NOT_UNLOCK_DEVICE";
                ERR_CODE[123] = "DEVICE_EEPROM_BUSY";
                ERR_CODE[160] = "HARDWARE_ERROR";
                ERR_CODE[161] = "HARDWARE_ERROR";

               function_error.setText("0x"+Integer.toHexString(result).toUpperCase());
               function_expl.setText(ERR_CODE[result]);
  }
  
   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;

        }
  
}
