package nxm.sys.libg;

import java.awt.AWTEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.JPasswordField;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;

import nxm.sys.libg.Theme;

/** A class to handle a password text field. Extends JPasswordField to allow right mouse exit.
    @author Jeff Schoen / J Bauer
    @since neXtMidas 2.9.0
*/
public class MJPasswordField extends JPasswordField {
  /** NOTE: this should be changed if the structure/fields of this class changes. */
  private static final long serialVersionUID = 2012021100311L; // for Serializable interface

  /** Constructor
      @param text The initial text to display
      @param columns Initial width
   */
  public MJPasswordField (String text, int columns) {
    super(text,columns);
    /* Specify the events of interest */
    enableEvents(AWTEvent.MOUSE_EVENT_MASK);
    enableEvents(AWTEvent.KEY_EVENT_MASK);
    Border outer = BorderFactory.createEtchedBorder(Theme.global().cwbh,Theme.global().cwbs );
    Border inner = BorderFactory.createEtchedBorder(Theme.global().cwbh,Theme.global().cwbs );
    super.setBorder(BorderFactory.createCompoundBorder(outer, inner));
    super.setEditable(true);
  }

  /** Overrides <CODE>java.awt.ProcessEvent(AWTEvent e)</CODE> in order to allow a right mouse exit.
      @param e The event.
   */
  public void processEvent (AWTEvent e) {
    if (e.getID()==MouseEvent.MOUSE_RELEASED &&
            (SwingUtilities.isRightMouseButton((MouseEvent) e))) { // Updated in 3.9.0 to use SwingUtilities to find out the mouse button
        processActionEvent(new ActionEvent(this,0,"EXIT"));
    }
    super.processEvent(e);
  }

  protected void processActionEvent(ActionEvent e) {
    ActionListener listener = super.getActionListeners()[0];
    if (listener != null) {
      listener.actionPerformed(e);
    }
  }

}
