/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package usda.weru.util;

import javax.swing.JPasswordField;

/**
 *
 * @author Joseph Levin <joelevin@weru.ksu.edu>
 */
public class WepsPasswordField extends JPasswordField {

    private static final long serialVersionUID = 1L;

    private String c_key;

    /**
     *
     * @param key
     */
    public WepsPasswordField(String key) {
        c_key = key;
    }

    /**
     *
     */
    public WepsPasswordField() {
    }

    /**
     *
     * @param key
     */
    public void setKey(String key) {
        String old = c_key;
        c_key = key;
        firePropertyChange("key", old, c_key);
    }

    /**
     *
     * @return
     */
    public String getKey() {
        return c_key;
    }

    /**
     *
     * @param t
     */
    @Override
    public void setText(String t) {
        String temp = decrypt(t);
        super.setText(temp);
    }

    /**
     *
     * @return
     */
    @Override
    public char[] getPassword() {
        StringBuilder password = new StringBuilder();
        password.append(super.getPassword());
        String temp = encyrpt(password.toString());
        return temp.toCharArray();
    }

    /**
     *
     * @return
     */
    /* @Override
    public String getText() {
    String temp = super.getText();
    return encyrpt(temp);
    }*/

    /**
     *
     * @param offs
     * @param len
     * @return
     * @throws BadLocationException
     */
    /*@Override
    public String getText(int offs, int len) throws BadLocationException {
    String temp = super.getText(offs, len);
    return encyrpt(temp);
    }*/

    /**
     *
     * @param input
     * @return
     */
    protected String encyrpt(String input) {
        return Util.encrypt(input, c_key);
    }

    /**
     *
     * @param input
     * @return
     */
    protected String decrypt(String input) {
        return Util.decrypt(input, c_key);
    }

}
