package oracle.forms.fd;

import java.awt.Dimension;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import oracle.forms.handler.IHandler;
import oracle.forms.properties.ID;
import oracle.forms.ui.VBean;
import javax.swing.JOptionPane;

/**
 * A javabean input dialog box for Oracle Forms
 * 
 * @author Francois Degrelle
 * @version 1.1
 *   (add the multi-lines input box)
 */

public class InputDialog extends VBean {

    static String title = "" ;
    static String text  = "" ;
    static String result = "" ;
    static int    icon  = JOptionPane.QUESTION_MESSAGE ;

    private IHandler mHandler;
    private static final ID pSetTitle      = ID.registerProperty("SETTITLE");
    private static final ID pSetText       = ID.registerProperty("SETTEXT");
    private static final ID pSetTextMulti  = ID.registerProperty("SETTEXTMULTI");    
    private static final ID pGetString     = ID.registerProperty("GETSTRING");    
    // TextArea for multi-lines input
    private static JTextArea textArea = new JTextArea(""); 
    private static JScrollPane scrollArea = new JScrollPane(textArea); 


    public InputDialog() {
      super();
      try
      {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        SwingUtilities.updateComponentTreeUI(this);
      }
      catch (Exception ex)
      {
        ex.printStackTrace();
      }    
    }

    public void init(IHandler handler) {
        super.init(handler);
        mHandler = handler;
        scrollArea.setPreferredSize(new Dimension(200,80));
    }

    /**
     * Set the properties from Forms
     **/
    public boolean setProperty(ID id, Object value) {

        if (id == pSetTitle) { /** Set the title **/
            title = (String)value ;
            return true;
        }
        else if (id == pSetText) { /** Set the message and display the dialog box **/
            text = (String)value ;
            result = ShowDialog(1);
            return true;
        }
        else if (id == pSetTextMulti) { /** Set the message and display the 
                                         *  multi-lines dialog box **/
            text = (String)value ;
            result = ShowDialog(2);
            return true;
        }        
        else {
            return true;
        }
    }

  /**
   * Get the result string from Forms
   **/
  public Object getProperty(ID pId)
  {
    if (pId == pGetString)
    {
      return "" + result ;
    }
    else
    {
      return super.getProperty(pId);
    }
  } // getProperty()

    /**
     * Display the dialog box
     */
    public static String ShowDialog(int iChoice) {

        String sReturn = "" ;
        if(iChoice==1)
        {
           sReturn =  JOptionPane.showInputDialog(null, text, title, icon);
        }
        else
        {
           JOptionPane.showConfirmDialog( 
             null, 
             new Object[] {text,  scrollArea}, 
             title, 
             JOptionPane.OK_CANCEL_OPTION, 
             icon);        
           sReturn = textArea.getText();   
        }
        return sReturn ;

    }
}
