home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / ActiveX / JBrowser / JBrowser.java < prev    next >
Encoding:
Java Source  |  2000-05-04  |  25.9 KB  |  939 lines

  1. //////////////////////////////////////////////////////////////////////////
  2. // JBrowser.java
  3. // This is a java application which hosts the WebBrowser control (IE4.0)
  4. // and provides similar UI.
  5. //
  6. // (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  7. //////////////////////////////////////////////////////////////////////////
  8.  
  9. // AFC
  10. import com.ms.ui.*;
  11. import com.ms.ui.event.*;
  12. import com.ms.fx.*;
  13.  
  14. // Component peers
  15. import com.ms.awt.peer.ComponentPeerX;
  16.  
  17. // the IE ActiveX control
  18. //import com.ms.ie.*;
  19. import shdocvw.*;
  20.  
  21. // for hosting ActiveX Controls
  22. import com.ms.activeX.*;
  23. import com.ms.com.Variant;
  24. import com.ms.com.ComSuccessException;
  25. import com.ms.wfc.ax.IOleInPlaceActiveObject;
  26.  
  27. // standard Java
  28. import java.awt.*;
  29. import java.awt.event.*;
  30.  
  31. // Win32 classes
  32. import com.ms.dll.*;
  33. import com.ms.win32.*;
  34.  
  35.  
  36. //
  37. //
  38. // JBrowser
  39. //
  40. //
  41. class JBrowser extends Frame implements IUIActionListener, ActiveXControlListener, WebBrowserEventListener
  42. {
  43.     UIBandBox bandBox = new UIBandBox();                                                    // this contains all the bands.
  44.     UIBand mainMenuBand = new UIBand();                                                        // the main menu band
  45.     UIMenuList fileMenuList = new UIMenuList();                                                // the list of items in the file menu
  46.     UIMenuButton fileMenu = new UIMenuButton("File", 0, fileMenuList);                        // the "File" menu
  47.     UIMenuItem openItem = new UIMenuItem("Open");                                            // the open item in the file menu
  48.     UIMenuItem exitItem = new UIMenuItem("Exit");                                            // the exit item in the file menu
  49.     UIMenuList goMenuList = new UIMenuList();                                                // the list of items in the go menu
  50.     UIMenuButton goMenu = new UIMenuButton("Go", 0, goMenuList);                            // the "Go" menu
  51.     UIMenuItem backItem = new UIMenuItem("Back");                                            // the back item in the go menu
  52.     UIMenuItem forwardItem = new UIMenuItem("Forward");                                        // the forward item in the go menu
  53.     UIMenuItem homeItem = new UIMenuItem("Home");                                            // the home item in the go menu
  54.     UIMenuItem searchItem = new UIMenuItem("Search");                                        // the search item in the go menu
  55.     UIMenuList helpMenuList = new UIMenuList();                                                // the list of items in the help menu
  56.     UIMenuButton helpMenu = new UIMenuButton("Help", 0, helpMenuList);                        // the "Help" menu
  57.     UIMenuItem tutorialItem = new UIMenuItem("Web Tutorial");                                // the Web Tutorial item in the help menu
  58.     UIMenuItem aboutItem = new UIMenuItem("About Java Browser");                            // the About ... item in the help menu.
  59.     UIBand buttonsBand = new UIBand();                                                        // the band of buttons
  60.     UIPushButton backButton;                                                                // go back to the last page button
  61.     UIPushButton forwardButton;                                                                // fo forward to the next page button
  62.     UIPushButton stopButton;                                                                // stop current transfer
  63.     UIPushButton refreshButton;                                                                // refresh current page button
  64.     UIPushButton homeButton;                                                                // go to the home page
  65.     UIPushButton searchButton;                                                                // go to the search page
  66.     UIBand addressBand = new UIBand("Address ");                                            // the band of addresses
  67.     EditBox address = new EditBox();                                                        // the edit box for entering the text
  68.     UIBand linksBand = new UIBand("Links");                                                    // the band of links
  69.     UIPushButton bestOfWebButton;                                                            // the "Best of the Web" button
  70.     UIPushButton microsoftJavaButton;                                                        // the microsoft Java page
  71.     UIPushButton microsoftButton;                                                            // the "microsoft" page
  72.     UIDrawText status = new UIDrawText("Status");                                            // to display the status text
  73.     ProgressBar progress = new ProgressBar(300,20,100,0);                                    // to display the progress of the download
  74.     StatusBar statusBar = new StatusBar(302,22);                                            // the status bar which displays progres and the status text
  75.     WebBrowser browser = new WebBrowser();                                                    // the web browser object
  76.     Thread animationThread;                                                                    // The thread which controls the animation of the image
  77.     JAnim animatedImage;                                                                    // the animated image object
  78.     UIPushButton animatedButton;                                                            // The button which has the animated image
  79.     public Font dialogFont;                                                                    // the font to use in dialog boxes
  80.  
  81.     // message hook support
  82.     HOOKPRC hproc;
  83.  
  84.     /**
  85.      * The main method which creates a JBrowser object.
  86.      * @param    String[]    The command line parameters
  87.      */
  88.     public static void main(String[] args)
  89.     {
  90.         new JBrowser();
  91.     }
  92.  
  93.     /**
  94.      * Null constructor for JBrowser object. It creates the following
  95.      * <ol>
  96.      * <li>Main Menu
  97.      *    <ul>
  98.      *    <li>File
  99.      *        <ul>
  100.      *        <li>Open</li>
  101.      *        <li>Exit</li>
  102.      *        </ul>
  103.      *    </li>
  104.      *    <li>Go
  105.      *        <ul>
  106.      *        <li>Back</li>
  107.      *        <li>Forward</li>
  108.      *        <li>Home Page</li>
  109.      *        <li>Search The Web</li>
  110.      *        </ul>
  111.      *    </li>
  112.      *    <li>Help
  113.      *        <ul>
  114.      *        <li>Web Tutorial</li>
  115.      *        <li>About JBrowser</li>
  116.      *        </ul>
  117.      *    </li>
  118.      * </li>
  119.      * <li>An animated button
  120.      * </li>
  121.      * <li>Tool Bar with following icons
  122.      *    <ul>
  123.      *    <li>Back</li>
  124.      *    <li>Forward</li>
  125.      *    <li>Stop</li>
  126.      *    <li>Refresh</li>
  127.      *    <li>Home</li>
  128.      *    <li>Search</li>    
  129.      *    </ul>
  130.      * </li>
  131.      * <li>Creates a bar with the following
  132.      *    <ul>
  133.      *    <li>An Address edit box</li>
  134.      *    <li>A links bar with the following
  135.      *        <ul>
  136.      *        <li>Best of the web</li>
  137.      *        <li>Microsoft Java</li>
  138.      *        <li>Microsoft</li>
  139.      *        <li>Yahoo</li>
  140.      *        </ul>
  141.      *    </li>
  142.      *    </ul>
  143.      * </li>
  144.      * <li>Creates a status bar which has the following
  145.      *    <ul>
  146.      *        <li>A text label which prints the status text</li>
  147.      *        <li>A progress bar which shows the progress of the current operation</li>
  148.      *    </ul>
  149.      * </li>
  150.      * </ol>
  151.      * 
  152.      */
  153.  
  154.     public JBrowser()
  155.     {
  156.         // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  157.         // Add event hooks to the JBrowser
  158.         // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  159.         addWindowListener(new JBrowserWindow());
  160.         bandBox.addActionListener(this);
  161.         browser.addWebBrowserEventListener(this);
  162.         browser.addActiveXControlListener(this);
  163.  
  164.         // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  165.         // Add the main menu band
  166.         // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  167.         
  168.         // give the band box a new layout manager.
  169.         bandBox.setLayout( new UIBarLayout(0,0) );
  170.  
  171.         // add the file menu 
  172.         fileMenuList.add(openItem);
  173.         fileMenuList.add(exitItem);
  174.         mainMenuBand.add(fileMenu);
  175.                 
  176.         // add the go menu
  177.         goMenuList.add(backItem);
  178.         goMenuList.add(forwardItem);
  179.         goMenuList.add(homeItem);
  180.         goMenuList.add(searchItem);
  181.         mainMenuBand.add(goMenu);
  182.  
  183.         // the forward and the back item will be enabled by browser events
  184.         backItem.setEnabled(false);
  185.         forwardItem.setEnabled(false);
  186.  
  187.         // add the help menu
  188.         helpMenuList.add(tutorialItem);
  189.         helpMenuList.add(aboutItem);
  190.         mainMenuBand.add(helpMenu);
  191.  
  192.         mainMenuBand.setEdge(IFxGraphicsConstants.BDR_RAISED);
  193.         
  194.         bandBox.add(mainMenuBand);
  195.  
  196.         // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  197.         // create and add an animated image
  198.         // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  199.         animatedImage = new JAnim( getToolkit().getImage("images/e.gif") , 46, 4 ) ;
  200.         animatedButton = new UIPushButton(animatedImage);
  201.         bandBox.add( animatedButton);
  202.  
  203.         // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  204.         // Add the buttons band
  205.         // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  206.         
  207.         // back button
  208.         backButton = createButton("images/previous.gif", "images/previous_g.gif", "Back", UIItem.ABOVE);
  209.         buttonsBand.add(backButton);
  210.  
  211.         // forward button
  212.         forwardButton = createButton("images/next.gif", "images/next_g.gif", "Forward", UIItem.ABOVE);
  213.         buttonsBand.add(forwardButton);
  214.  
  215.         // the forward and back buttons will be enabled by browser events
  216.         backButton.setEnabled(false);
  217.         forwardButton.setEnabled(false);
  218.  
  219.         // stop button
  220.         stopButton = createButton("images/stop.gif", "images/stop_g.gif", "Stop", UIItem.ABOVE);
  221.         buttonsBand.add(stopButton);
  222.  
  223.         //stop button will be enabled by a download begin event
  224.         stopButton.setEnabled(false);
  225.  
  226.         // refresh button
  227.         refreshButton = createButton("images/refresh.gif", "images/refresh_g.gif", "Refresh", UIItem.ABOVE);
  228.         buttonsBand.add(refreshButton);
  229.  
  230.         // home button
  231.         homeButton = createButton("images/home.gif", "images/home_g.gif", "Home", UIItem.ABOVE);
  232.         buttonsBand.add(homeButton);
  233.  
  234.         // search button
  235.         searchButton = createButton("images/search.gif", "images/search_g.gif", "Search", UIItem.ABOVE);
  236.         buttonsBand.add(searchButton);
  237.  
  238.         buttonsBand.setEdge(IFxGraphicsConstants.BDR_RAISED);
  239.         buttonsBand.setStyle(UIBand.BREAK);
  240.  
  241.         bandBox.add(buttonsBand);
  242.         
  243.         // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  244.         // Add the address band
  245.         // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  246.  
  247.         // a panel for hosting the editbox
  248.         UIPanel addressPanel = new UIPanel();
  249.         addressPanel.setBackground(FxColor.white);
  250.         addressPanel.setEdge(IFxGraphicsConstants.BDR_SUNKEN);
  251.         addressPanel.add(address, "center");
  252.         addressBand.add(addressPanel);
  253.  
  254.         addressBand.setEdge(IFxGraphicsConstants.BDR_RAISED);
  255.         addressBand.setStyle(UIBand.BREAK);
  256.         bandBox.add(addressBand);
  257.         
  258.         // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  259.         // Add the links band
  260.         // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%        
  261.         bestOfWebButton = createButton("images/favorite_small.gif", "images/favorite_small_g.gif", "Best of the Web", UIItem.ONLEFT);
  262.         linksBand.add(bestOfWebButton);
  263.  
  264.         microsoftButton = createButton("images/favorite_small.gif", "images/favorite_small_g.gif", "Microsoft", UIItem.ONLEFT);
  265.         linksBand.add(microsoftButton);
  266.  
  267.         microsoftJavaButton = createButton("images/favorite_small.gif", "images/favorite_small_g.gif", "Microsoft Java", UIItem.ONLEFT);
  268.         linksBand.add(microsoftJavaButton);
  269.  
  270.         linksBand.setEdge(IFxGraphicsConstants.BDR_RAISED);
  271.         linksBand.setStyle(UIBand.DRAGBREAK);
  272.  
  273.         bandBox.add(linksBand);
  274.  
  275.         // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  276.         // Add the band box
  277.         // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  278.         setLayout(new BorderLayout() );
  279.         
  280.         setBackground(FxColor.lightGray);
  281.  
  282.         bandBox.setEdge(IFxGraphicsConstants.BDR_SUNKEN);
  283.         add("North", new AwtUIHost(bandBox) );
  284.  
  285.  
  286.         // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  287.         // Add the ActiveX WebBrowser control
  288.         // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  289.         add("Center", browser);
  290.  
  291.         // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  292.         // Add the status bar with status text and the progress bar in it.
  293.         // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  294.  
  295.         // this panel will host the status text and the progress bar
  296.         statusBar.setEdge(IFxGraphicsConstants.BDR_RAISED);
  297.         statusBar.setLayout( new UIBorderLayout() );
  298.  
  299.         // the status text
  300.         statusBar.add(status, "center");
  301.  
  302.         // the progress bar
  303.         statusBar.add(progress, "east");
  304.  
  305.         add("South", new AwtUIHost(statusBar) );
  306.  
  307.         // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  308.         // Show the JBrowser
  309.         // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  310.         setBounds(150,100,900,800);
  311.         setTitle("JBrowser");
  312.         setVisible(true);
  313.         dialogFont = new Font("TimesRoman", Font.BOLD, 18) ;
  314.     }
  315.  
  316.  
  317.     /**
  318.      * This class creates a push button with the specified hot and normal images and
  319.      * a text to be displayed with the image. The button is hot-tracked
  320.      * 
  321.      * @param String    The name of the normal image
  322.      * @param String    The name of the hot image
  323.      * @param String    The text to be displayed with image
  324.      * @param int        The location of the image with respect to the text
  325.      */
  326.     UIPushButton createButton( String normal, String hot, String text, int where)
  327.     {
  328.         Image           gifnormal       = null;         // button in normal state
  329.         Image           gifhot          = null;         // button in hot state
  330.         MediaTracker    tracker;                        // to wait for the images to be loaded
  331.         UIPushButton    button;                         // button to be returned
  332.  
  333.         // create a media tracker
  334.         tracker = new MediaTracker(this);
  335.  
  336.         // load the normal image
  337.         if (normal != null)
  338.         {
  339.             // load the normal image
  340.             gifnormal = getToolkit().getImage(normal);
  341.  
  342.             // track the image neing loaded
  343.             tracker.addImage(gifnormal, 0);
  344.         }
  345.  
  346.        if (hot != null)
  347.         {
  348.                // load the hot-tracked image
  349.             gifhot = getToolkit().getImage(hot);
  350.  
  351.             // listen on this image being loaded
  352.             tracker.addImage(gifhot, 0);
  353.         }
  354.  
  355.         // create the button
  356.         button = new UIPushButton(new UIItem(gifnormal, text, UIItem.HOTTRACK, where), 0);
  357.         //button = new UIPushButton( new UIStateItem(gifnormal, gifHot, text, UIStateItem.CENTERED, where), 0);
  358.  
  359.         // wait for all the images to be loaded
  360.         try 
  361.         {
  362.             tracker.waitForAll();
  363.         } 
  364.         catch (InterruptedException e) 
  365.         {
  366.         }
  367.  
  368.         return button;
  369.     }
  370.  
  371.     // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  372.     // Perform the browser actions
  373.     // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  374.  
  375.     /**
  376.      * This shows a dialog box describing the browser in response to the about
  377.      * item in the help menu
  378.      */
  379.     public void about()
  380.     {
  381.         UIMessageBox aboutMsg = new UIMessageBox(new UIFrame(),"About JBrowser", "This is a web-browser written in Java which uses the ActiveX control IE4.0",
  382.             UIMessageBox.INFORMATION, UIButtonBar.OK    );
  383.  
  384.         aboutMsg.setFont(dialogFont);
  385.         aboutMsg.doModal();
  386.     }
  387.  
  388.     /**
  389.      * This navigates the browser object to a specified URL
  390.      *
  391.      * @param    String    The URL to go to.
  392.      */
  393.     public void navigate(String url)
  394.     {
  395.         browser.Navigate(url, null, null, null, null);
  396.     }
  397.  
  398.     /**
  399.      * This method displays a dialog box which takes a user specified URL
  400.      * as input and navigates to that URL.
  401.      */
  402.     public void open()
  403.     {
  404.         new JBrowserOpen(this);
  405.     }
  406.       
  407.     /**
  408.      * This closes the browser application
  409.      */
  410.     public void exit()
  411.     {
  412.         // Unhook MsgProc Hook
  413.         Win32.UnhookWindowsHookEx(hproc.hhk);
  414.         hproc = null;
  415.  
  416.         browser.removeWebBrowserEventListener(this);
  417.         browser.removeActiveXControlListener(this);
  418.         System.exit(0);
  419.     }
  420.  
  421.     // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  422.     // Event handlers for the browser
  423.     // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  424.  
  425.     // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  426.     // Interface IUIActionListener requires the following method
  427.     // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  428.  
  429.     /**
  430.      * This procedure receives action events which occurred in the browser like keyclicks
  431.      * on buttons or menuitems. It then calls the appropriate function to handle the event.
  432.      *
  433.      * @param    UIActionEvent    The event which occurred
  434.      */
  435.     public void actionPerformed(UIActionEvent evt)
  436.     {        
  437.         Object target = evt.getActionItem();
  438.  
  439.         // ignore UIBand events
  440.         if( evt.getSource() instanceof UIBand)
  441.             return;
  442.  
  443.         if(target == address)
  444.         {
  445.             navigate( address.getValueText() );
  446.         }
  447.  
  448.         else if(target == aboutItem)
  449.         {
  450.             about();
  451.         }
  452.  
  453.         else if(target == backItem || target == backButton )
  454.         {
  455.             browser.GoBack();
  456.         }
  457.  
  458.         else if( target == exitItem )
  459.         {
  460.             exit();
  461.         }
  462.  
  463.         else if( target == forwardItem || target == forwardButton)
  464.         {
  465.             browser.GoForward();
  466.         }
  467.  
  468.         else if (target == homeItem || target == homeButton)
  469.         {
  470.             browser.GoHome();
  471.         }
  472.  
  473.         else if( target == microsoftButton)
  474.         {
  475.             navigate("http://www.microsoft.com");
  476.         }
  477.  
  478.         else if(target == microsoftJavaButton)
  479.         {
  480.             navigate("http://www.microsoft.com/java");
  481.         }
  482.  
  483.         else if(target == openItem)
  484.         {
  485.             open();
  486.         }
  487.         
  488.         else if(target == refreshButton)
  489.         {
  490.             browser.Refresh();
  491.         }
  492.         
  493.         else if(target == searchItem || target == searchButton)
  494.         {
  495.             browser.GoSearch();
  496.         }
  497.         
  498.         else if(target == stopButton)
  499.         {
  500.             browser.Stop();
  501.         }
  502.  
  503.         else if(target == tutorialItem)
  504.         {
  505.             navigate("http://www.microsoft.com/magazine/guides/internet/default.asp?");
  506.         }
  507.  
  508.         else if(target == bestOfWebButton)
  509.         {
  510.             navigate("http://home.microsoft.com/exploring/exploring.asp");
  511.         }
  512.  
  513.         else if(target == animatedButton)
  514.         {
  515.             navigate("http://www.microsoft.com/ie/default.asp");
  516.         }
  517.     }
  518.  
  519.  
  520.     // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  521.     // Interface ActiveXControlListener requires the following method
  522.     // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  523.  
  524.  
  525.     /**
  526.      * This procedure receives events from the activeX controls after they have been created
  527.      * The browser control is made to start up from the home page.
  528.      *
  529.      * @param    Object    The control which was created
  530.      */
  531.     public void controlCreated(Object target)
  532.     {
  533.         if(target == browser)
  534.         {
  535.             browser.GoHome();
  536.  
  537.             // Hook a message filter into the message pump for the web browser
  538.             // control, so that we can intercept messages and call
  539.             // IOleInPlaceActiveObject.TranslateAccelerator as appropriate.
  540.             ComponentPeerX apeer = (ComponentPeerX)browser.getPeer();
  541.             if (apeer != null) {
  542.                 int hwnd = apeer.getTopHwnd();
  543.  
  544.                 if (hwnd != 0) {
  545.                     EnumCallback ec = new EnumCallback();
  546.                     User32.EnumChildWindows(hwnd, ec, 0);
  547.                     hwnd = ec.myChildWnd;
  548.  
  549.                     hproc = new HOOKPRC();
  550.                     hproc.browser_obj = (IOleInPlaceActiveObject)browser.getObject();
  551.                     int threadID = Win32.GetWindowThreadProcessId(hwnd, 0);
  552.                     hproc.hhk = Win32.SetWindowsHookEx(Win32.WH_GETMESSAGE, 
  553.                                                        hproc, 0, threadID);
  554.                 }
  555.             }
  556.  
  557.         }
  558.     }
  559.  
  560.     // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  561.     // Interface WebBrowserEventListener requires the following methods
  562.     // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  563.  
  564.     /**
  565.      * This method is invoked when the text in the status bar has to be changed
  566.      *
  567.      * @param    String    The new text
  568.      */
  569.     public void StatusTextChange(String text)
  570.     {
  571.         status.setValueText(text);
  572.     }
  573.  
  574.     /**
  575.      * This is invoked when the progress bar's position is to be changed
  576.      *
  577.      * @param    int    The position
  578.      * @param    int    The maximum position
  579.      */
  580.     public void ProgressChange(int pos, int range)
  581.     {
  582.         // UIProgress doesn't like a range of 0 ;-)
  583.         if(range <= 0)
  584.             progress.setRange(100);
  585.         else
  586.             progress.setRange(range);
  587.  
  588.         // the position is set to -1 when the download has finished
  589.         if(pos < 0)
  590.             progress.setPos(0);
  591.         else
  592.             progress.setPos(pos);
  593.     }
  594.  
  595.     /**
  596.      * The title of the page being displayed
  597.      *
  598.      * @param    String    The new title
  599.      */
  600.     public void TitleChange(String text)
  601.     {
  602.         setTitle(text);
  603.     }
  604.  
  605.     /**
  606.      * This method is invoked when the Browser object has finished loading a document
  607.      *
  608.      * @param    Object    The web-browser frame in which this document was loaded
  609.      * @param    Variant    The URL of the loaded document
  610.      */
  611.     public void DocumentComplete(Object pDisp, Variant URL)
  612.     {
  613.         address.setValueText( URL.getString() );
  614.     }
  615.  
  616.     /**
  617.      * This method is invoked when the enabled state of a command changes
  618.      * for example when the forward command or the back command is no longer
  619.      * enabled
  620.      *
  621.      * @param    int        The identifier of the command that changed
  622.      * @param    boolean    Whether the command is enabled (=>true) or disabled
  623.      */
  624.     public void CommandStateChange(int Command, boolean Enable)
  625.     {
  626.         switch(Command)
  627.         {
  628.         case CommandStateChangeConstants.CSC_NAVIGATEFORWARD:
  629.             forwardButton.setEnabled(Enable);
  630.             forwardItem.setEnabled(Enable);
  631.             break;
  632.  
  633.         case CommandStateChangeConstants.CSC_NAVIGATEBACK:
  634.             backButton.setEnabled(Enable);
  635.             backItem.setEnabled(Enable);
  636.             break;
  637.         }
  638.     }
  639.  
  640.     /**
  641.      * This method is called when the browser begins downloading
  642.      */
  643.     public void DownloadBegin()
  644.     {
  645.         // start a new thread for the animated image
  646.         animationThread = new Thread( animatedImage, "Animated Image");
  647.         animationThread.start();
  648.  
  649.         stopButton.setEnabled(true);
  650.         progress.setVisible(true);
  651.     }
  652.  
  653.     /**
  654.      * This method is called when the browser ends downloading
  655.      */
  656.     public void DownloadComplete()
  657.     {
  658.         // stop the animated image thread.
  659.         animationThread.stop();
  660.         animatedImage.reset();
  661.  
  662.         stopButton.setEnabled(false);
  663.         progress.setVisible(false);
  664.     }
  665.  
  666.  
  667.     // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  668.     // The following methods of interface WebBrowserEventListener
  669.     // are not implemented
  670.     // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  671.     public void PropertyChange(String szProperty)
  672.     {
  673.     }
  674.  
  675.     public void BeforeNavigate2(Object pDisp, Variant URL, Variant Flags, Variant TargetFrameName, Variant PostData, Variant Headers, boolean[] Cancel)
  676.     {
  677.     }
  678.  
  679.     public void NewWindow2(Object[] ppDisp, boolean[] Cancel)
  680.     {
  681.     }
  682.     
  683.     public void NavigateComplete2(Object pDisp, Variant URL)
  684.     {
  685.     }
  686.  
  687.     public void OnQuit()
  688.     {
  689.     }
  690.     
  691.     public void OnVisible(boolean Visible)
  692.     {
  693.     }
  694.     
  695.     public void OnToolBar(boolean ToolBar)
  696.     {
  697.     }
  698.     
  699.     public void OnMenuBar(boolean MenuBar)
  700.     {
  701.     }
  702.     
  703.     public void OnStatusBar(boolean StatusBar)
  704.     {
  705.     }
  706.     
  707.     public void OnFullScreen(boolean FullScreen)
  708.     {
  709.     }
  710.     
  711.     public void OnTheaterMode(boolean TheaterMode)
  712.     {
  713.     }
  714.  
  715.     // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  716.     // Interface WindowListener requires the following inner class
  717.     // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  718.  
  719.     /**
  720.      * This adapter class handles window events like when a user clicks on the 
  721.      * close window button it closes the browser.
  722.      */
  723.     class JBrowserWindow extends WindowAdapter
  724.     {
  725.         /**
  726.          * Handles the event generated when the window is being closed
  727.          *
  728.          * @param    WindowEvent    The window event generated
  729.          */
  730.         public void windowClosing(WindowEvent event)
  731.         {
  732.             exit();
  733.         }
  734.  
  735.  
  736.     }
  737. }
  738.  
  739. /**
  740.  * The progress bar extends the AFC's UIProgress and specifies a size for the same
  741.  */
  742. class ProgressBar extends UIProgress
  743. {
  744.     private int width;                    // The width of the progress bar
  745.     private int height;                    // The height of the progress bar
  746.  
  747.     /**
  748.      * This constructor specifies the width and the height of the progress bar.
  749.      *
  750.      * @param    int    width
  751.      * @param    int    height
  752.      * @param    int    range -- The maximum range of the progress bar
  753.      * @param    int    pos   -- The current position of the progress bar
  754.      */
  755.     public ProgressBar(int width, int height, int range, int pos)
  756.     {
  757.         // tell UIProgress what the range and the pos is
  758.         super(range, pos);
  759.  
  760.         this.width = width;
  761.         this.height = height;
  762.     }
  763.  
  764.     /**
  765.      * The preferred size of this progress bar
  766.      *
  767.      * @return    Dimension    The preferred dimension
  768.      */
  769.     public Dimension getPreferredSize()
  770.     {
  771.         return new Dimension(width, height);
  772.     }
  773.  
  774.     /**
  775.      * The minimum size of this progress bar
  776.      *
  777.      * @return    Dimension    The minimum size
  778.      */
  779.     public Dimension getMinimumSize()
  780.     {
  781.         return new Dimension(width, height);
  782.     }
  783. }
  784.  
  785. /**
  786.  * The status bar is simply a UIPanel with a minimum size. This prevents it
  787.  * from totally disappearing when it has nothing on it.
  788.  */
  789. class StatusBar extends UIPanel
  790. {
  791.     private int width;                    // The width of the status bar
  792.     private int height;                    // The height of the status bar
  793.  
  794.     /**
  795.      * This constructor specifies the width and the height of the status bar.
  796.      *
  797.      * @param    int    width
  798.      * @param    int    height
  799.      */
  800.     public StatusBar(int width, int height)
  801.     {
  802.         this.width = width;
  803.         this.height = height;
  804.     }
  805.  
  806.     /**
  807.      * The preferred size of this progress bar
  808.      *
  809.      * @return    Dimension    The preferred dimension
  810.      */
  811.     public Dimension getPreferredSize()
  812.     {
  813.         return new Dimension(width, height);
  814.     }
  815.  
  816.     /**
  817.      * The minimum size of this progress bar
  818.      *
  819.      * @return    Dimension    The minimum size
  820.      */
  821.     public Dimension getMinimumSize()
  822.     {
  823.         return new Dimension(width, height);
  824.     }
  825. }
  826.  
  827.  
  828. /**
  829.  * This is an edit-box for entering the address to navigate to in the
  830.  * browser.
  831.  */
  832. class EditBox extends UIEdit
  833. {
  834.  
  835.     /**
  836.      * This creates a single line edit box
  837.      */
  838.     public EditBox()
  839.     {
  840.         setSingleLine(true);
  841.         setBackground(FxColor.white);
  842.     }
  843.  
  844.     /**
  845.      * The preferred size of this edit
  846.      *
  847.      * @return    Dimension    The preferred dimension
  848.      */
  849.     
  850.     public Dimension getPreferredSize()
  851.     {
  852.         return new Dimension(700, 16);
  853.     }
  854.  
  855.     /**
  856.      * The minimum size of this edit box
  857.      *
  858.      * @return    Dimension    The minimum size
  859.      */
  860.     public Dimension getMinimumSize()
  861.     {
  862.         return new Dimension(700, 16);
  863.     }
  864. }
  865.  
  866. /**
  867.  * This is callback class used with User32.EnumChildWindows.
  868.  */
  869. class EnumCallback extends WNDENUMPROC
  870. {
  871.  
  872.     public int myChildWnd;
  873.     public String myChildWndClass;
  874.  
  875.     public boolean wndenumproc(int hwnd, int lparam)
  876.     {
  877.         myChildWnd = hwnd;
  878.         return true; // stop enumerating, only interested in first child
  879.     }
  880. }
  881.  
  882. /**
  883.  * This is callback class which process WndProc Msg filter callbacks.
  884.  * The purpose is to correctly call 
  885.  * IOleInPlaceActiveObject::TranslateAccelerator, therefore allowing the
  886.  * user to tab through the controls on the hosted web browser control.
  887.  */
  888. class HOOKPRC extends Callback {
  889.  
  890.     int hhk;
  891.     IOleInPlaceActiveObject browser_obj;
  892.  
  893.     int callback(int code, int wParam,  int lParam)
  894.     {
  895.         if (code < 0) {
  896.             return Win32.CallNextHookEx(hhk, code, wParam, lParam);
  897.         }
  898.         MSG msg = (MSG) DllLib.ptrToStruct(MSG.class, lParam);
  899.         try {
  900.             // IOleInPlaceActiveObject::TranslateAccelerator potentially
  901.             // returns S_OK, which in this case, we Null out the current
  902.             // message the message pump is processing.
  903.             // If we receive S_FALSE, a ComSuccessException is thrown,
  904.             // and the message will be processed by the message pump.
  905.             browser_obj.TranslateAccelerator(msg);
  906.  
  907.             // we got an S_OK, NULL out message so the message pump will not
  908.             // dispatch it
  909.             msg.message = 0;
  910.         }
  911.         catch (ComSuccessException cse) { }
  912.         catch (Exception e) {
  913.             System.out.println("HOOKPRC EXCEPTION: "+e);
  914.         }
  915.         return 0;
  916.     }
  917. }
  918.  
  919. /**
  920.  * This class contains helper functions and definitions for setting up the 
  921.  * HOOKPROC callback.
  922.  */
  923. class Win32
  924. {
  925.     public static final int WH_GETMESSAGE = 3;
  926.  
  927.     /** @dll.import("USER32",auto) */
  928.     public native static int SetWindowsHookEx (int idHook, HOOKPRC lpfn, int hMod, int dwThreadId);
  929.  
  930.     /** @dll.import("USER32",auto) */
  931.     public native static int CallNextHookEx (int hhk, int nCode, int wParam, int lParam);
  932.  
  933.     /** @dll.import("USER32",auto) */
  934.     public native static int UnhookWindowsHookEx (int hhk);
  935.  
  936.     /** @dll.import("USER32",auto) */
  937.     public native static int GetWindowThreadProcessId (int hWnd, int lpdwProcessId);
  938. }
  939.