
package fortuna.entropySources.keyboard;

/**
 *
 * @author Viliam Krizan
 */
import fortuna.entropySources.EntropySource;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class KeyboardES extends EntropySource implements KeyListener, ActionListener{
    
    private JFrame frame;
    private JTextArea displayArea;
    private JTextField typingArea;
    private static final String newline = System.getProperty("line.separator");
    private Date date;
    
    public KeyboardES(){
        //Create and set up the window.
        frame = new JFrame("KeyboardES");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         
        //Set up the content pane.
        addComponentsToPane();
         
         
        //Display the window.
        //frame.pack();
        frame.setSize(350, 300);
        frame.setVisible(true);
    }
        
    
    
    @Override
    public void run(){
        super.run();
        while(running){}
        System.exit(0);
    }
    
    private void addComponentsToPane() {
         
        JButton button = new JButton("Clear");
        button.addActionListener(this);
         
        typingArea = new JTextField(20);
        typingArea.addKeyListener(this);
         
        typingArea.setFocusTraversalKeysEnabled(false);
         
        displayArea = new JTextArea();
        displayArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(displayArea);
        scrollPane.setPreferredSize(new Dimension(375, 125));
         
        frame.getContentPane().add(typingArea, BorderLayout.PAGE_START);
        frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
        frame.getContentPane().add(button, BorderLayout.PAGE_END);
    }

    @Override
    public void keyTyped(KeyEvent e) {
        
    }

    @Override
    public void keyPressed(KeyEvent e) {
        displayInfo(e);
    }

    @Override
    public void keyReleased(KeyEvent e) {
        
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        //Clear the text components.
        displayArea.setText("");
        typingArea.setText("");
         
        //Return the focus to the typing area.
        typingArea.requestFocusInWindow();
    }
    
    private void displayInfo(KeyEvent e){
        
        String keyString;
        
        int keyCode = e.getKeyCode();
            keyString = "code = " + keyCode
                    + " ("
                    + KeyEvent.getKeyText(keyCode)
                    + ")";
         
        date = new Date();
        int milis = (int) (date.getTime() % 1000l);
        byte[] data = new byte[1];
        data[0] = (byte) milis;
        acumulator.saveToPool(data, actualPool);
        poolCounterIncrement();
         
        displayArea.append("KEY PRESSED:" 
                + "   " + keyString 
                + "   " + "ms: " + milis + newline);
        displayArea.setCaretPosition(displayArea.getDocument().getLength());
    }
}
