Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-25-2008, 03:34 PM
Java Tip's Avatar
Moderator
 
Join Date: Nov 2007
Posts: 1,691
Rep Power: 5
Java Tip will become famous soon enoughJava Tip will become famous soon enough
Default OLE and ActiveX example which gets events from IE control
Code:
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.internal.ole.win32.COM;
import org.eclipse.swt.internal.ole.win32.COMObject;
import org.eclipse.swt.internal.ole.win32.GUID;
import org.eclipse.swt.internal.ole.win32.IDispatch;
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;

public class Snippet123 {

  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;
    }
    shell.open();

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

    // When a new document is loaded, access the document object for the new
    // page.
    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 = pVarResult.getAutomation();

        // Request to be notified of click, double click and key down
        // events
        EventDispatch myDispatch = new EventDispatch(
            EventDispatch.onclick);
        IDispatch idispatch = new IDispatch(myDispatch.getAddress());
        Variant dispatch = new Variant(idispatch);
        htmlDocument.setProperty(EventDispatch.onclick, dispatch);

        myDispatch = new EventDispatch(EventDispatch.ondblclick);
        idispatch = new IDispatch(myDispatch.getAddress());
        dispatch = new Variant(idispatch);
        htmlDocument.setProperty(EventDispatch.ondblclick, dispatch);

        myDispatch = new EventDispatch(EventDispatch.onkeydown);
        idispatch = new IDispatch(myDispatch.getAddress());
        dispatch = new Variant(idispatch);
        htmlDocument.setProperty(EventDispatch.onkeydown, dispatch);

        // Remember to release OleAutomation Object
        htmlDocument.dispose();
      }
    });

    // Navigate to a web site
    int[] ids = webBrowser
        .getIDsOfNames(new String[] { "Navigate", "URL" });
    Variant[] rgvarg = new Variant[] { new Variant("http://www.google.com") };
    int[] rgdispidNamedArgs = new int[] { ids[1] };
    webBrowser.invoke(ids[0], rgvarg, rgdispidNamedArgs);

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

  }
}

// EventDispatch implements a simple IDispatch interface which will be called
// when the event is fired.
class EventDispatch {
  private COMObject iDispatch;

  private int refCount = 0;

  private int eventID;

  final static int onhelp = 0x8001177d;

  final static int onclick = 0x80011778;

  final static int ondblclick = 0x80011779;

  final static int onkeyup = 0x80011776;

  final static int onkeydown = 0x80011775;

  final static int onkeypress = 0x80011777;

  final static int onmouseup = 0x80011773;

  final static int onmousedown = 0x80011772;

  final static int onmousemove = 0x80011774;

  final static int onmouseout = 0x80011771;

  final static int onmouseover = 0x80011770;

  final static int onreadystatechange = 0x80011789;

  final static int onafterupdate = 0x80011786;

  final static int onrowexit = 0x80011782;

  final static int onrowenter = 0x80011783;

  final static int ondragstart = 0x80011793;

  final static int onselectstart = 0x80011795;

  EventDispatch(int eventID) {
    this.eventID = eventID;
    createCOMInterfaces();
  }

  int getAddress() {
    return iDispatch.getAddress();
  }

  private void createCOMInterfaces() {
    iDispatch = new COMObject(new int[] { 2, 0, 0, 1, 3, 4, 8 }) {
      public int method0(int[] args) {
        return QueryInterface(args[0], args[1]);
      }

      public int method1(int[] args) {
        return AddRef();
      }

      public int method2(int[] args) {
        return Release();
      }

      // method3 GetTypeInfoCount - not implemented
      // method4 GetTypeInfo - not implemented
      // method5 GetIDsOfNames - not implemented
      public int method6(int[] args) {
        return Invoke(args[0], args[1], args[2], args[3], args[4],
            args[5], args[6], args[7]);
      }
    };
  }

  private void disposeCOMInterfaces() {
    if (iDispatch != null)
      iDispatch.dispose();
    iDispatch = null;

  }

  private int AddRef() {
    refCount++;
    return refCount;
  }

  private int Invoke(int dispIdMember, int riid, int lcid, int dwFlags,
      int pDispParams, int pVarResult, int pExcepInfo, int pArgErr) {
    switch (eventID) {
    case onhelp:
      System.out.println("onhelp");
      break;
    case onclick:
      System.out.println("onclick");
      break;
    case ondblclick:
      System.out.println("ondblclick");
      break;
    case onkeyup:
      System.out.println("onkeyup");
      break;
    case onkeydown:
      System.out.println("onkeydown");
      break;
    case onkeypress:
      System.out.println("onkeypress");
      break;
    case onmouseup:
      System.out.println("onmouseup");
      break;
    case onmousedown:
      System.out.println("onmousedown");
      break;
    case onmousemove:
      System.out.println("onmousemove");
      break;
    case onmouseout:
      System.out.println("onmouseout");
      break;
    case onmouseover:
      System.out.println("onmouseover");
      break;
    case onreadystatechange:
      System.out.println("onreadystatechange");
      break;
    case onafterupdate:
      System.out.println("onafterupdate");
      break;
    case onrowexit:
      System.out.println("onrowexit");
      break;
    case onrowenter:
      System.out.println("onrowenter");
      break;
    case ondragstart:
      System.out.println("ondragstart");
      break;
    case onselectstart:
      System.out.println("onselectstart");
      break;
    }
    return COM.S_OK;
  }

  private int QueryInterface(int riid, int ppvObject) {
    if (riid == 0 || ppvObject == 0)
      return COM.E_INVALIDARG;
    GUID guid = new GUID();
    COM.MoveMemory(guid, riid, GUID.sizeof);

    if (COM.IsEqualGUID(guid, COM.IIDIUnknown)
        || COM.IsEqualGUID(guid, COM.IIDIDispatch)) {
      COM.MoveMemory(ppvObject, new int[] { iDispatch.getAddress() }, 4);
      AddRef();
      return COM.S_OK;
    }
    COM.MoveMemory(ppvObject, new int[] { 0 }, 4);
    return COM.E_NOINTERFACE;
  }

  int Release() {
    refCount--;
    if (refCount == 0) {
      disposeCOMInterfaces();
    }
    return refCount;
  }
}
__________________
"The sole cause of man’s unhappiness is that he does not know how to stay quietly in his room." - Blaise Pascal
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
control app width based on certain control thebillybobjr SWT / JFace 0 05-15-2008 05:52 PM
SWT Events JavaForums Java Blogs 0 12-31-2007 05:53 PM
Need Help With Button Events adlb1300 New To Java 5 11-03-2007 06:36 AM
Help with keyboard events? Bibendum New To Java 2 11-02-2007 03:51 AM
To capture events with struts Ed Web Frameworks 2 07-04-2007 07:01 AM


All times are GMT +2. The time now is 12:59 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org