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.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.Border;

import nxm.sys.libg.Theme;

/** A class to handle a text field. Extends JTextField to allow right mouse exit.
    @author Jeff Schoen
*/
public class MJTextField extends JTextField {
  /** NOTE: this should be changed if the structure/fields of this class changes. */
  private static final long serialVersionUID = 2012020970311L; // for Serializable interface

  /** Creates a new TextField initialized with the specified text to be displayed, and wide enough
      to display the number of columns indicated.
      @param seed The text to be displayed.
      @param columns The number of columns.
  */
  public MJTextField (String seed, int columns) {
    super(seed,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 mouse button
        processActionEvent(new ActionEvent(this,0,"EXIT"));
    }
    super.processEvent(e);
  }

  protected void processActionEvent(ActionEvent e) {
    // NTN: Resolved an issue when there are no listeners that caused
    // an ArrayOutOfBoundsException
    ActionListener[] listeners = super.getActionListeners();
    if (listeners != null && listeners.length > 0) {
      ActionListener listener = listeners[0];
      if (listener != null) {
        listener.actionPerformed(e);
      }
    }
  }

  /** Method to override JTextField.  This is here because of a bug in Java 5.0
      where the caret position is not valid after a setText invocation.
      @param str The input string
   */
  public void setText(String str) {
    // JPH -- This is a workaround for a JDK 5 bug where the caret position
    // can be greater than the length of the string causing
    //     Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: bad position: ...
    // The check is because JDK 1.4 does not have this problem but does not allow setting
    // the caret position but instead complains with
    //    java.awt.IllegalComponentStateException: Cannot set caret position until after the peer has been created
    super.setText(str);
    if (nxm.sys.lib.StringUtil.compareVersions("1.5.0", nxm.sys.lib.Shell.getJavaVersion()) <= 0) {
      super.setCaretPosition(str.length());
    }
  }
}
