import javax.swing.JDialog;
import javax.swing.JTextArea;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.Action;
import javax.swing.text.StyledEditorKit;
import javax.swing.text.StyleConstants;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.Frame;
import java.awt.Color;
import java.awt.Rectangle;

/**
Sexy confirmation and error message class.
@author Daniel Sykes
**/

final class MsgDlg
{
  public static final int OK = 0;
  public static final int CANCEL = 1;
  public static final int YES = 2;
  public static final int NO = 3;
    
  public static final int OKCANCEL = -1;
  public static final int YESNO = -2;
  public static final int OKONLY = -3;
  public static final int YESNOCANCEL = -4;
    
  private static int returnValue = 1;
  
  public static int error(Frame owner, String caption)
  {
    return show(owner, caption, "Error", OKONLY);
  }
  
  public static int error(Frame owner, String caption, String title)
  {
    return show(owner, caption, title, OKONLY);
  }
    
  public static int question(Frame owner, String caption)
  {
    return show(owner, caption, "Question", YESNO);
  }
  
  public static int question(Frame owner, String caption, int buttons)
  {
    return show(owner, caption, "Question", buttons);
  }
  
  public static int question(Frame owner, String caption, String title)
  {
    return show(owner, caption, title, YESNO);
  }
  
  public static int question(Frame owner, String caption, String title, int buttons)
  {
    return show(owner, caption, title, buttons);
  }
  
  public static int show(Frame owner, String caption)
  {
    return show(owner, caption, "Message", OKONLY);
  }
  
  public static int show(Frame owner, String caption, int buttons)
  {
    return show(owner, caption, "Message", buttons);
  }
  
  public static int show(Frame owner, String caption, String title)
  {
    return show(owner, caption, title, OKONLY);
  }
  
  public static int show(Frame owner, String caption, String title, final int buttons)
  {
    final JDialog f = new JDialog(owner, title, true);
    JTextArea txt = new JTextArea(caption);
    JPanel panel = new JPanel();
    
    Rectangle screen = f.getGraphicsConfiguration().getBounds();
    
    f.setBounds((screen.width/2) - 150, (screen.height/2) - 75, 300, 150);
    f.setResizable(false);
    f.getContentPane().setLayout(null);
    
    txt.setBounds(10, 10, 280, 70);
    txt.setLineWrap(true);
    txt.setVisible(true);
    //txt.selectAll();
    //(new StyledEditorKit.AlignmentAction(Action.NAME, StyleConstants.ALIGN_CENTER)).actionPerformed(new ActionEvent(txt, 0, ""));
    //txt.setCaretPosition(0);
    txt.setEditable(false);
    txt.setBackground(panel.getBackground());
    txt.addKeyListener(new KeyAdapter()
      {
        public void keyPressed(KeyEvent e)
        {
          if (e.getKeyCode() == KeyEvent.VK_ENTER)
          {
            if (buttons == YESNO || buttons == YESNOCANCEL)
              returnValue = YES;
            else
              returnValue = OK;
            f.dispose();
          }
        }
      });
    f.getContentPane().add(txt);
    
    if (buttons == YESNO || buttons == YESNOCANCEL)
    {
      JButton yesButton = new JButton("Yes");
      yesButton.setMnemonic('y');
      yesButton.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            returnValue = YES;
            f.dispose();
          }
        });
      f.getRootPane().setDefaultButton(yesButton);
      panel.add(yesButton);
      
      JButton noButton = new JButton("No");
      noButton.setMnemonic('n');
      noButton.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            returnValue = NO;
            f.dispose();
          }
        });
      panel.add(noButton);
      
      if (buttons == YESNOCANCEL)
        makeCancelButton(f, panel);
    }
    else
    {
      JButton okButton = new JButton("OK");
      okButton.setMnemonic('o');
      okButton.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            returnValue = OK;
            f.dispose();
          }
        });
      f.getRootPane().setDefaultButton(okButton);
      panel.add(okButton);
      
      if (buttons == OKCANCEL)
        makeCancelButton(f, panel);
    }
    
    panel.setBounds(10, 85, 280, 50);
    panel.setVisible(true);
    f.getContentPane().add(panel);
    
    f.setVisible(true);
    return returnValue;
  }
  
  private static void makeCancelButton(final JDialog f, JPanel p)
  {
    JButton cancelButton = new JButton("Cancel");
    cancelButton.setMnemonic('c');
    cancelButton.addActionListener(new ActionListener()
      {
        public void actionPerformed(ActionEvent e)
        {
          returnValue = CANCEL;
          f.dispose();
        }
      });
    p.add(cancelButton);
  }
}