package nxm.sys.libxm;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;

import nxm.sys.lib.MidasException;

/** X-Midas Display
    @author  prp / J. Czechowski IV
 */
public class XmDisplay extends Canvas implements HierarchyListener {
  /** NOTE: this should be changed if the structure/fields of this class changes. */
  private static final long serialVersionUID = 2012062700313L; // for Serializable interface

  /** The X-Midas /XS= value (i.e. X-windows Setup ID for XPIPE's auto setup/learn function). */
  private int xs = 0;

  /** XmDisplay Default Constructor */
  public XmDisplay() {
    setBackground(Color.black);
    setForeground(Color.white);
    Dimension size = new Dimension(100, 100);
    setSize(size.width, size.height);
    setPreferredSize(size);
  }

  /** Sets the ID of the X-Midas window to map into this canvas. Note that once the xs value has
      been set it can not be changed.
      @param xs The value matching the /XS= switch on the X-Midas command.
   */
  public void setXS(int xs) {
    if (this.xs == xs) {
      // Already set
    } else if (this.xs == 0) {
      this.xs = xs;
      XMidas.xAttach(this, xs);
      XMidas.xSetBounds(this, xs);
    } else {
      throw new MidasException("Attempting to bind multiple /XS= windows to one XCanvas.");
    }
  }

  /** Get X-windows Setup ID for XPIPE's auto setup/learn function
   *  @return the X-windows Setup ID for XPIPE's auto setup/learn function
   */
  public int getXS() {
    return xs;
  }

  /** Test XmDisplay */
  public static void test() {
    javax.swing.JFrame   frame   = new javax.swing.JFrame();
    javax.swing.JMenuBar menuBar = new javax.swing.JMenuBar();
    javax.swing.JMenu    menu    = new javax.swing.JMenu("File");
    menuBar.add(menu);
    frame.setLayout(new java.awt.GridLayout(2,2));
    frame.setJMenuBar(menuBar);

    XmDisplay canvas2 = new XmDisplay();
    XmDisplay canvas3 = new XmDisplay();

    frame.add(canvas2);
    frame.add(canvas3);
    frame.setVisible(true);

    canvas2.setXS(2);
    canvas3.setXS(3);
  }

  //////////////////////////////////////////////////////////////////////////////////////////////////
  // Resize methods -- Need to override to update size of X11 window accordingly.
  //////////////////////////////////////////////////////////////////////////////////////////////////
  public void setBounds(int x, int y, int width, int height) {
    super.setBounds(x,y,width,height);
    if (xs != 0) {
      XMidas.xSetBounds(this, xs);
    }
  }

  public void setBounds(Rectangle r) {
    super.setBounds(r);
    if (xs != 0) {
      XMidas.xSetBounds(this, xs);
    }
  }

  //////////////////////////////////////////////////////////////////////////////////////////////////
  // HierarchyListener methods
  //////////////////////////////////////////////////////////////////////////////////////////////////
  public void hierarchyChanged(HierarchyEvent e) {
//    long flags = e.getChangeFlags();
//    if ((flags & HierarchyEvent.DISPLAYABILITY_CHANGED) > 0 &&
//        !isDisplayable() && (xs > 0)) {
//      XPrimitive.sendBreak(xs);
//    }
  }

}
