package nxm.ucl.libg;

import java.awt.Graphics;

import javax.swing.JLabel;
import javax.swing.JPanel;

import nxm.sys.inc.MessageHandler;
import nxm.sys.lib.MidasException;
import nxm.sys.libg.BeanWidget;
import nxm.sys.libg.GWidget;

/**
  A simple class to create a Swing-based JLabel from the GCONTROL BEAN widget.
  This class is currently used as a simple example within the NeXtMidas User Guide.
  and will be expanded upon as new examples or techniques are added
  to the NeXtMidas User's Guide.

  Requirements for BEAN GCONTROL Custom Widgets:

  The custom control's class should be added to the UCL/libg folder or your own option tree.
  The class must extend {@link GWidget} and implement the {@link BeanWidget} interface.
  The <code>BeanWidget</code> interface requires any implementing class to define the functionality
  for the <code>setupBean()</code> method.  In addition, the class must contain an
  empty constructor.  Both the empty constructor and the <code>setupBean()</code>
  method are required in order for your custom control to invoked correctly.

  @author Chris Simons
  @since NeXtMidas 2.9.1
 */
public class GSwingLabel extends GWidget implements BeanWidget {

  private static final String CLASS_CAST_ERROR_MSG =
          "Unable to cast LABELTEXT to a String object.";

  private MessageHandler mh;
  private String labelText = "";

  protected JLabel swingLabel;
  protected JPanel labelPanel;

  /** Empty constructor. */
  public GSwingLabel () {}

  /** Called by GCONTROL to "create" this BEAN-based widget.
      @param name The name of the widget/control.
      @param parent The parent widget/control.
      @param title The Midas title of the widget/control.
      @param flags Any applicable flags passed into the widget/control.
      @param mh The MessageHandler reference for sending processMessages.
      @param argTable The table of Midas arguments sent into this widget/control.
   */
  public void setupBean (String name, Object parent, String title,
    int flags, MessageHandler mh, nxm.sys.lib.Table argTable) {

    // Must call GWidget.setupBean
    super.setupBean(name, parent, title, flags, mh, argTable);

    // Define attributes of this class
    this.mh = mh;

    // Iterate through the control arguments
    if (argTable != null) {

      try {
        // Argument "P1" is already used for the class name
        if (argTable.size() > 0)
          labelText = argTable.getS("LABELTEXT");
        // Iterate through additional arguments as necessary
      }
      catch (ClassCastException cce) {
        throw new MidasException(CLASS_CAST_ERROR_MSG, cce);
      }
    }

    // Additional functionality
    if (labelText != null)
      open(labelText);

    addTo(parent);
  }

  /** Add a new Label to the graphics window
   *  @param labelText the text for the new Label
   */
  public void open (String labelText) {
    if (labelText != null) {
      super.open(); // added in 3.1.3 to make sure size is set
      int width = (labelText.length() * tw) + bw + bw; // modded in 3.1.3 to make sure size is set based on text size
      int height = th + ta;  // modded in 3.1.3 to make sure size is set based on text size

      swingLabel = new JLabel(labelText);
      swingLabel.setVisible(true);
      swingLabel.setSize(width, height);

      pos.w = swingLabel.getWidth();
      pos.h = swingLabel.getHeight();
      panel.setSize(pos.w, pos.h);

      // Add the JLabel to the NeXtMidas panel
      panel.add(swingLabel);
    }
    else {
      M.error("LABELTEXT cannot be null.");
    }

  }

  @Deprecated
  public void paint () {
//    if (g == null) return; // as of 3.9.3 removing old code and switching to new paintComponent (Graphics g) method
//    super.paint();
    refresh();
  }

  public void paintComponent (Graphics g) {
    super.paintComponent(g);
  }

  @Override
  protected int processEventMessage (String name, int info, Object data) {
    if (button==3 && !is(PERM)) { // right mouse button (close if NOT permanent)
      close();
      return 1;
    }
    else {
      //  System.out.println("Process Event Message: " + name + " info " + info + " data  " + data);
    }
    return -1;
  }

  /** Sets the text value of this JLabel.
      @param value The new label text for this object.
   */
  public void setValue(Object value) {
    try {
      this.labelText = (String) value;
    }
    catch (ClassCastException cce) {
      M.error("Could not set a new value, could not cast to a String object.");
    }
  }

  /** Returns the value (label text) of this control.
      @return The current value of this control.
   */
  public Object getValue() {
    return this.labelText;
  }

  /** An accessory method for the LABELTEXT parameter.
      @param labelText The value of the JLabel
   */
  public void setLabelText(Object labelText) {
    setValue(labelText);
  }

}