Running a script within IE. : WIN32 « SWT JFace Eclipse « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java Tutorial
Java Book
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
SCJP
Java » SWT JFace Eclipse » WIN32Screenshots 
Running a script within IE.
Running a script within IE.


import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.ole.win32.OLE;
import org.eclipse.swt.ole.win32.OleAutomation;
import org.eclipse.swt.ole.win32.OleControlSite;
import org.eclipse.swt.ole.win32.OleEvent;
import org.eclipse.swt.ole.win32.OleFrame;
import org.eclipse.swt.ole.win32.OleListener;
import org.eclipse.swt.ole.win32.Variant;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/*
 * Running a script within IE.
 
 * For a list of all SWT example snippets see
 * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
 */

public class Snippet187 {

  public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    OleControlSite controlSite;
    try {
      OleFrame frame = new OleFrame(shell, SWT.NONE);
      controlSite = new OleControlSite(frame, SWT.NONE, "Shell.Explorer");
      controlSite.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);
    catch (SWTError e) {
      System.out.println("Unable to open activeX control");
      return;
    }

    // IWebBrowser
    final OleAutomation webBrowser = new OleAutomation(controlSite);

    // When the document is loaded, access the document object for the new
    // page
    // and evalute expression using Script.
    int DownloadComplete = 104;
    controlSite.addEventListener(DownloadComplete, new OleListener() {
      public void handleEvent(OleEvent event) {
        int[] htmlDocumentID = webBrowser
            .getIDsOfNames(new String[] { "Document" });
        if (htmlDocumentID == null)
          return;
        Variant pVarResult = webBrowser.getProperty(htmlDocumentID[0]);
        if (pVarResult == null || pVarResult.getType() == 0)
          return;
        // IHTMLDocument2
        OleAutomation htmlDocument = null;
        try {
          htmlDocument = pVarResult.getAutomation();
          pVarResult.dispose();

          int[] scriptID = htmlDocument
              .getIDsOfNames(new String[] { "Script" });
          if (scriptID == null)
            return;
          pVarResult = htmlDocument.getProperty(scriptID[0]);
          if (pVarResult == null || pVarResult.getType() == 0)
            return;
          OleAutomation htmlWindow = null;
          try {
            // IHTMLWindow2
            htmlWindow = pVarResult.getAutomation();
            pVarResult.dispose();
            int[] evaluateID = htmlWindow
                .getIDsOfNames(new String[] { "evaluate" });
            if (evaluateID == null)
              return;
            String expression = "5+Math.sin(9)";
            Variant[] rgvarg = new Variant[] { new Variant(
                expression) };
            pVarResult = htmlWindow.invoke(evaluateID[0], rgvarg,
                null);
            if (pVarResult == null || pVarResult.getType() == 0)
              return;
            System.out.println(expression + " ="
                + pVarResult.getString());
          finally {
            htmlWindow.dispose();
          }
        finally {
          htmlDocument.dispose();
        }
      }
    });

    // Navigate to a web site
    int[] ids = webBrowser
        .getIDsOfNames(new String[] { "Navigate""URL" });
    Variant[] rgvarg = new Variant[] { new Variant(
        "http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet187.html") };
    int[] rgdispidNamedArgs = new int[] { ids[1] };
    webBrowser.invoke(ids[0], rgvarg, rgdispidNamedArgs);

    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    // Remember to release OleAutomation Object
    webBrowser.dispose();
    display.dispose();

  }
}


           
       
Related examples in the same category
1.Invoke the system text editor on autoexec.bat
2.Place an icon with a popup menu on the system trayPlace an icon with a popup menu on the system tray
3.SWT Ole FrameSWT Ole Frame
4.OLE and ActiveX example snippet
5.Word OLEWord OLE
6.Invoke an external batch fileInvoke an external batch file
7.Find the icon of the program that edits .bmp filesFind the icon of the program that edits .bmp files
8.Reading and writing to a SAFEARRAY
9.Embed Word in an applet
10.OLE and ActiveX example: browse the typelibinfo for a program id
11.OLE and ActiveX example: get events from IE controlOLE and ActiveX example: get events from IE control
12.How to access About, Preferences and Quit menus on carbon
w_w___w__._j__a_v__a_2s._c_o___m_ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.