package oracle.forms.ms;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;

import java.io.File;

import javax.swing.JFrame;

import oracle.ewt.event.AnyEventListener;

import oracle.forms.ui.VBean;

import org.jdic.web.BrComponent;


/**
 * A Bean to display a full Web Browser based on an idea of Francios Degrelle.
 *
 * @author Mark Striekwold
 * @version 1.0
 */
   
public class ehb extends VBean{
    //
    // Variables declaration
    private   oracle.forms.ui.ExtendedFrame ef ;
    private   int  x,y,w,h;
    private   org.jdic.web.BrComponent brView;

    @SuppressWarnings("unchecked")
    
    /** Creates new Embedded HTML Browser */
    public ehb() {
         BrComponent.DESIGN_MODE = false;
         BrComponent.setDefaultPaintAlgorithm(BrComponent.PAINT_NATIVE);
         brView = new org.jdic.web.BrComponent();
         add(brView, BorderLayout.CENTER);
    }
    
    /******************
    *  Set the URL   *
    *****************/
    public boolean setUrl(String pValue) // set the url
    { 
        String url = "";
        brView.setVisible(true);
        brView.setBounds(x,y,w,h);
        try {
            if(! pValue.startsWith("http")) {
                File f = new File(pValue);
                url = new String("file:///"+f.getAbsolutePath());
            }
            else if( pValue.startsWith("http")) {
                url = new String(pValue);
            }
            else {
                System.out.println("Item not recognized : "+pValue);
                return false;
            }  
            System.out.println(url);
            brView.setURL(url);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return true;
    }   

    /***********************
    *  Navigation action  *
    **********************/
    public boolean setNavigation(String pValue) // set the navigation action
    {
        System.out.println("Navigation="+pValue);
        
        if(pValue.equalsIgnoreCase("back")) brView.back();
        else if(pValue.equalsIgnoreCase("forward")) brView.forward();
        else if(pValue.equalsIgnoreCase("refresh")) brView.refresh();
        else return false;
        return true;
    }             
    
    /********************************************
    *   get the window that handle the bean    *
    *******************************************/
    public boolean infoBean()
    {
        boolean bCont  = true ;    
        Container cont = this.getParent();
        String s = "" ;
        while (cont!=null) {
            // search for the window
            s = "" + cont.getClass() ;
            if(s.indexOf("oracle.forms.ui.ExtendedFrame")>-1) {
                /*********************
                *   current window  *
                ********************/
                ef = (oracle.forms.ui.ExtendedFrame)cont ;
                System.out.println("------> ExtendedFrame title : "+ef.getTitle());
                // add the listener to the window
                ef.addAnyEventListener(ael);
            }
            cont = cont.getParent() ;
        }
        return true;
    }            

    void getRect()
    {
        Rectangle rec = this.getBounds() ;
        x = (int)rec.getX() ;
        y = (int)rec.getY() ;
        w = (int)rec.getWidth() ;
        h = (int)rec.getHeight() ;             
    }

    AnyEventListener ael  = new AnyEventListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        String keyText = actionEvent.getActionCommand();
      }
      public void processEventStart(java.util.EventObject eo) {
        String keyText = eo.toString() ;
        if(keyText.indexOf("COMPONENT_MOVED")> -1)
        {
          getRect();
          brView.setBounds(x,y,w,h);
        }
      }          
      public void processEventEnd(java.util.EventObject eo) {
        String keyText = eo.toString() ;
      }      
    };

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        JFrame frame  = new JFrame("EHB");
        ehb e = new ehb(); //.setVisible(true);
        // Add the control panel and desktop (JDesktopPane) to the application content pane.    
        frame.getContentPane().add(e);
        frame.pack();
        frame.setSize(800, 600); // adjust the frame size using specific dimensions.
        frame.setVisible(true); // show the frame.
        e.brView.setURL("http://www.google.nl");
    }
}

