package nxm.sys.libg;

import java.awt.Component;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenuItem;

/** A class to run a graphical midas Panel that supports push and pop. This is
    basically a container with a title bar (with push/pop 'X') and a <code>MidasDisplay </code>

    @see MidasDisplay

    @author J Czechowski
    @since NeXtMidas 2.7.2
 */
@SuppressWarnings("serial")
public class PushPopPanel extends javax.swing.JPanel {

  /** Creates new form MJPushPopPanel */
  public PushPopPanel () {
    initComponents();
    midasDisplay.setTitle(getTitle());

    GraphicsDevice[] devices = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
    for (final GraphicsDevice d : devices) {
      JMenuItem menuItem = new JMenuItem();
      menuItem.setText("  Pop to "+d.getIDstring());
      menuItem.addActionListener(new ActionListener () {
        public void actionPerformed (ActionEvent evt) {
          MWindow mw = midasDisplay.getMWindow();
          if (mw != null) mw.pop(1, d);
        }
      });
      optionsMenu.add(menuItem);
    }

  }

  /** gets the MidasDisplay to draw to. Internal use only to PANEL
   *  @return the MidasDisplay to draw on */
  public MidasDisplay getMidasDisplay () { return midasDisplay; }

  /** set title
   *  @param t  title
   */
  public void setTitle (String t) {
    titleLabel.setText(t);
    midasDisplay.setTitle(t);
  }

  /** get title
   *  @return title
   */
  public String getTitle () { return titleLabel.getText(); }

  /** This method is called from within the constructor to initialize the form
   *  WARNING: Do NOT modify this code. The content of this method is
   *  always regenerated by the Form Editor
   */
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    optionsMenu = new javax.swing.JPopupMenu();
    pushMenuItem = new javax.swing.JMenuItem();
    popMenuItem = new javax.swing.JMenuItem();
    midasDisplay = new nxm.sys.libg.MidasDisplay();
    jPanel1 = new javax.swing.JPanel();
    titleLabel = new javax.swing.JLabel();
    pushPopButton = new javax.swing.JButton();
    optionsButton = new javax.swing.JButton();

    pushMenuItem.setText("Push");
    pushMenuItem.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        pushMenuItemActionPerformed(evt);
      }
    });
    optionsMenu.add(pushMenuItem);

    popMenuItem.setText("Pop");
    popMenuItem.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        popMenuItemActionPerformed(evt);
      }
    });
    optionsMenu.add(popMenuItem);

    setLayout(new java.awt.BorderLayout());

    midasDisplay.setBorder(javax.swing.BorderFactory.createEtchedBorder());
    add(midasDisplay, java.awt.BorderLayout.CENTER);

    jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
    jPanel1.setLayout(new java.awt.BorderLayout());

    titleLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    titleLabel.setText("  ----  TITLE  ----  ");
    titleLabel.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    titleLabel.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
    titleLabel.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mousePressed(java.awt.event.MouseEvent evt) {
        titleLabelMousePressed(evt);
      }
    });
    jPanel1.add(titleLabel, java.awt.BorderLayout.CENTER);

    pushPopButton.setText("X");
    pushPopButton.setToolTipText("Push/Pop");
    pushPopButton.setBorderPainted(false);
    pushPopButton.setContentAreaFilled(false);
    pushPopButton.setFocusable(false);
    pushPopButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    pushPopButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        pushPopButtonActionPerformed(evt);
      }
    });
    jPanel1.add(pushPopButton, java.awt.BorderLayout.EAST);

    optionsButton.setText("V");
    optionsButton.setToolTipText("Options");
    optionsButton.setBorderPainted(false);
    optionsButton.setContentAreaFilled(false);
    optionsButton.setFocusable(false);
    optionsButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
    optionsButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        optionsButtonActionPerformed(evt);
      }
    });
    jPanel1.add(optionsButton, java.awt.BorderLayout.WEST);

    add(jPanel1, java.awt.BorderLayout.NORTH);
  }// </editor-fold>//GEN-END:initComponents

  private void pushPopButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pushPopButtonActionPerformed
    MWindow mw = midasDisplay.getMWindow();
    if (mw != null) mw.pop(-1);
  }//GEN-LAST:event_pushPopButtonActionPerformed

  private void optionsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_optionsButtonActionPerformed
    // JC4: not sure why I need to use this clumsy method for setting the location of the menu
    // (see enxt three lines), but for some reason, just calling show with 0, optionsButton.height just
    // does not work all the time (xinerama issue?)
    optionsMenu.show(optionsButton, 0, 0);
    Point p = optionsButton.getLocationOnScreen();
    optionsMenu.setLocation(p.x, p.y+optionsButton.getHeight());

    MWindow mw = midasDisplay.getMWindow();
    if (mw != null) {
      boolean popped = mw.isPopped();
      for (Component c : optionsMenu.getComponents()) {
        if (c instanceof JMenuItem) {
          JMenuItem jmi = (JMenuItem)c;
          if (jmi == pushMenuItem) jmi.setEnabled(popped);
          else                     jmi.setEnabled(!popped);
        }
      }
    }
  }//GEN-LAST:event_optionsButtonActionPerformed

  private void pushMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pushMenuItemActionPerformed
    MWindow mw = midasDisplay.getMWindow();
    if (mw != null) mw.pop(0);
  }//GEN-LAST:event_pushMenuItemActionPerformed

  private void popMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_popMenuItemActionPerformed
    MWindow mw = midasDisplay.getMWindow();
    if (mw != null) mw.pop(1);
  }//GEN-LAST:event_popMenuItemActionPerformed

  private void titleLabelMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_titleLabelMousePressed

  }//GEN-LAST:event_titleLabelMousePressed

  // Variables declaration - do not modify//GEN-BEGIN:variables
  /** JPanel */                               private javax.swing.JPanel jPanel1;
  /** Midas Display */                        private nxm.sys.libg.MidasDisplay midasDisplay;
  /** Options button */                       private javax.swing.JButton optionsButton;
  /** Options Popup Menu */                   private javax.swing.JPopupMenu optionsMenu;
  /** Menu item (implementation) for 'Pop'*/  private javax.swing.JMenuItem popMenuItem;
  /** Menu item (implementation) for 'Push'*/ private javax.swing.JMenuItem pushMenuItem;
  /** Push Pop Button */                      private javax.swing.JButton pushPopButton;
  /** JLabel holding title */                 private javax.swing.JLabel titleLabel;
  // End of variables declaration//GEN-END:variables

}
