B-1_14-DEV merge back to main

This commit is contained in:
clay_shooter
2008-02-02 01:38:25 +00:00
parent cd58b57a99
commit df0ba33797
97 changed files with 12208 additions and 8816 deletions

View File

@@ -2,6 +2,7 @@ package com.jacob.test.events;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComException;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.DispatchEvents;
import com.jacob.com.InvocationProxy;
@@ -10,41 +11,34 @@ import com.jacob.test.BaseTestCase;
/**
* This test was lifted from a forum posting and shows how you can't listen to
* Excel events (added post 1.9.1 Eclipse Settings.) This also uses the 1.9.1
* InvocationProxy to receive the events.
* Excel events (added post 1.9.1 Eclipse Settings.) This also uses the 1.9.1
* InvocationProxy to receive the events. The test was modified in 1.14 to show
* how to hook up multiple event listeners to various Excel components
* <p>
* May need to run with some command line options (including from inside Eclipse).
* Look in the docs area at the Jacob usage document for command line options.
* May need to run with some command line options (including from inside
* Eclipse). Look in the docs area at the Jacob usage document for command line
* options.
*/
public class ExcelEventTest extends BaseTestCase {
/**
* load up excel, register for events and make stuff happen
*
* @param args
*/
public void testExcelWithInvocationProxy() {
String pid = "Excel.Application";
ComThread.InitSTA();
// we are going to listen to events on Application.
// You can probably also listen Excel.Sheet and Excel.Chart
String excelApplicationProgramId = "Excel.Application";
String excelSheetProgramId = "Excel.Sheet";
String typeLibLocation = "C:\\Program Files\\Microsoft Office\\OFFICE11\\EXCEL.EXE";
// Grab The Component.
ActiveXComponent axc = new ActiveXComponent(pid);
try {
// Add a listener (doesn't matter what it is).
DispatchEvents de;
if (typeLibLocation == null) {
de = new DispatchEvents(axc, new ExcelEvents());
} else {
de = new DispatchEvents(axc, new ExcelEvents(), pid,
typeLibLocation);
}
if (de == null) {
System.out
.println("No exception thrown but no dispatch returned for Excel events");
} else {
// Yea!
System.out.println("Successfully attached to " + pid);
ActiveXComponent axc = new ActiveXComponent(excelApplicationProgramId);
hookupListener(axc, excelApplicationProgramId, typeLibLocation);
}
try {
System.out.println("version=" + axc.getProperty("Version"));
System.out.println("version=" + Dispatch.get(axc, "Version"));
@@ -52,6 +46,7 @@ public class ExcelEventTest extends BaseTestCase {
Dispatch workbooks = axc.getPropertyAsComponent("Workbooks");
Dispatch workbook = Dispatch.get(workbooks, "Add").toDispatch();
Dispatch sheet = Dispatch.get(workbook, "ActiveSheet").toDispatch();
hookupListener(sheet, excelSheetProgramId, typeLibLocation);
Dispatch a1 = Dispatch.invoke(sheet, "Range", Dispatch.Get,
new Object[] { "A1" }, new int[1]).toDispatch();
Dispatch a2 = Dispatch.invoke(sheet, "Range", Dispatch.Get,
@@ -60,36 +55,83 @@ public class ExcelEventTest extends BaseTestCase {
System.out.println("Inserting calculation 2xA1 into A2");
Dispatch.put(a1, "Value", "123.456");
Dispatch.put(a2, "Formula", "=A1*2");
System.out.println("Retrieved a1 from excel:" + Dispatch.get(a1, "Value"));
System.out.println("Retrieved a2 from excel:" + Dispatch.get(a2, "Value"));
System.out.println("Retrieved a1 from excel:"
+ Dispatch.get(a1, "Value"));
System.out.println("Retrieved a2 from excel:"
+ Dispatch.get(a2, "Value"));
Variant f = new Variant(false);
Dispatch.call(workbook, "Close", f);
axc.invoke("Quit", new Variant[] {});
} catch (ComException cfe) {
cfe.printStackTrace();
fail("Failed to attach to " + pid + ": "
fail("Failed to attach to " + excelApplicationProgramId + ": "
+ cfe.getMessage());
}
try {
// the sleep is required to let everything clear out after the quit
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ComThread.Release();
}
/**
* extracted the listener hookup so we could try multiple listeners.
*
* @param axc
* @param programId
* @param typeLibLocation
*/
private void hookupListener(Dispatch axc, String programId,
String typeLibLocation) {
// Add a listener (doesn't matter what it is).
DispatchEvents applicationEvents;
if (typeLibLocation == null) {
applicationEvents = new DispatchEvents(axc, new ExcelEvents(
programId));
} else {
applicationEvents = new DispatchEvents(axc, new ExcelEvents(
programId), programId, typeLibLocation);
}
if (applicationEvents == null) {
System.out
.println("No exception thrown but no dispatch returned for Excel events");
} else {
// Yea!
System.out.println("Successfully attached to " + programId);
}
}
public class ExcelEvents extends InvocationProxy {
/**
* Constructor so we can create an instance that implements invoke()
* Proxy class to verify we receive expected events
*/
public ExcelEvents() {
}
public class ExcelEvents extends InvocationProxy {
/**
* Override the invoke method to log all the events so that we don't have to
* implement all of the specific events.
*/
public Variant invoke(String methodName, Variant targetParameter[]) {
System.out.println("Received event from Windows program" + methodName);
return null;
}
private String listenerPrefix = "-";
}
/**
* Constructor so we can create an instance that implements invoke()
*
* @param interfaceIdentifier
* a string that identifies which listener is speaking
*/
public ExcelEvents(String interfaceIdentifier) {
listenerPrefix = interfaceIdentifier;
}
/**
* Override the invoke method to log all the events so that we don't
* have to implement all of the specific events.
*/
public Variant invoke(String methodName, Variant targetParameter[]) {
System.out.println("Received event from " + listenerPrefix + ": "
+ methodName);
return null;
}
}
}

View File

@@ -6,212 +6,406 @@ import com.jacob.com.Dispatch;
import com.jacob.com.DispatchEvents;
import com.jacob.com.Variant;
import com.jacob.test.BaseTestCase;
/**
* This test runs fine against jdk 1.4 and 1.5
*
* This demonstrates the new event handling code in jacob 1.7
* This example will open up IE and print out some of the events
* it listens to as it havigates to web sites.
* contributed by Niels Olof Bouvin mailto:n.o.bouvin@daimi.au.dk
* and Henning Jae jehoej@daimi.au.dk
* This demonstrates the new event handling code in jacob 1.7 This example will
* open up IE and print out some of the events it listens to as it havigates to
* web sites. contributed by Niels Olof Bouvin mailto:n.o.bouvin@daimi.au.dk and
* Henning Jae jehoej@daimi.au.dk
* <p>
* May need to run with some command line options (including from inside Eclipse).
* Look in the docs area at the Jacob usage document for command line options.
* May need to run with some command line options (including from inside
* Eclipse). Look in the docs area at the Jacob usage document for command line
* options.
*/
public class IETest extends BaseTestCase {
/**
* runs the IE test and feeds it commands
*/
public void testRunIE() {
// this line starts the pump but it runs fine without it
ComThread.startMainSTA();
// remove this line and it dies
///ComThread.InitMTA(true);
IETestThread aThread = new IETestThread();
aThread.start();
while (aThread.isAlive()){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// doen with the sleep
//e.printStackTrace();
}
}
System.out.println("Main: Thread quit, about to quit main sta in thread "
+Thread.currentThread().getName());
// this line only does someting if startMainSTA() was called
ComThread.quitMainSTA();
System.out.println("Main: did quit main sta in thread "
+Thread.currentThread().getName());
if (aThread.threadFailedWithException != null){
fail("caught an unexpected exception "+aThread.threadFailedWithException);
}
}
// this line starts the pump but it runs fine without it
ComThread.startMainSTA();
// remove this line and it dies
// /ComThread.InitMTA(true);
IETestThread aThread = new IETestThread();
aThread.start();
while (aThread.isAlive()) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// doen with the sleep
// e.printStackTrace();
}
}
System.out
.println("Main: Thread quit, about to quit main sta in thread "
+ Thread.currentThread().getName());
// this line only does someting if startMainSTA() was called
ComThread.quitMainSTA();
System.out.println("Main: did quit main sta in thread "
+ Thread.currentThread().getName());
if (aThread.threadFailedWithException != null) {
fail("caught an unexpected exception "
+ aThread.threadFailedWithException);
}
}
}
class IETestThread extends Thread
{
class IETestThread extends Thread {
/** flag that says we got a quit message from IE */
public static boolean quitHandled = false;
/**
* holds any caught exception so the main/test case can see them
*/
public Throwable threadFailedWithException = null;
public IETestThread(){
super();
}
public void run()
{
// this used to be 5 seconds but sourceforge is slow
int delay = 5000; // msec
// paired with statement below that blows up
ComThread.InitMTA();
ActiveXComponent ie = new ActiveXComponent("InternetExplorer.Application");
try {
Dispatch.put(ie, "Visible", new Variant(true));
Dispatch.put(ie, "AddressBar", new Variant(true));
System.out.println("IETestThread: " + Dispatch.get(ie, "Path"));
Dispatch.put(ie, "StatusText", new Variant("My Status Text"));
System.out.println("IETestThread: About to hookup event listener");
IEEvents ieE = new IEEvents();
new DispatchEvents(ie, ieE,"InternetExplorer.Application.1");
System.out.println("IETestThread: Did hookup event listener");
/// why is this here? Was there some other code here in the past?
Variant optional = new Variant();
optional.putNoParam();
System.out.println("IETestThread: About to call navigate to sourceforge");
Dispatch.call(ie, "Navigate", new Variant("http://sourceforge.net/projects/jacob-project"));
System.out.println("IETestThread: Did call navigate to sourceforge");
try { Thread.sleep(delay); } catch (Exception e) {}
System.out.println("IETestThread: About to call navigate to yahoo");
Dispatch.call(ie, "Navigate", new Variant("http://groups.yahoo.com/group/jacob-project"));
System.out.println("IETestThread: Did call navigate to yahoo");
try { Thread.sleep(delay); } catch (Exception e) {}
} catch (Exception e) {
threadFailedWithException = e;
e.printStackTrace();
} catch (Throwable re){
threadFailedWithException = re;
re.printStackTrace();
} finally {
System.out.println("IETestThread: About to send Quit");
ie.invoke("Quit", new Variant[] {});
System.out.println("IETestThread: Did send Quit");
}
// this blows up when it tries to release a DispatchEvents object
// I think this is because there is still one event we should get back
// "OnQuit" that will came after we have released the thread pool
// this is probably messed up because DispatchEvent object will have been
// freed before the callback
// commenting out ie.invoke(quit...) causes this to work without error
// this code tries to wait until the quit has been handled but that doesn't work
System.out.println("IETestThread: Waiting until we've received the quit callback");
while (!quitHandled){
try { Thread.sleep(delay/5);} catch (InterruptedException e) {}
}
System.out.println("IETestThread: Received the quit callback");
// wait a little while for it to end
//try {Thread.sleep(delay); } catch (InterruptedException e) {}
System.out.println("IETestThread: about to call ComThread.Release in thread " +
Thread.currentThread().getName());
ComThread.Release();
}
/**
* constructor for the test thread
*/
public IETestThread() {
super();
}
/**
* The events class must be publicly accessable for reflection to work.
* The list of available events is located at http://msdn2.microsoft.com/en-us/library/aa768280.aspx
*/
public class IEEvents
{
public void BeforeNavigate2(Variant[] args) {
System.out.println("IEEvents Received ("+Thread.currentThread().getName()+"): BeforeNavigate2 "+args.length);
}
public void run() {
// this used to be 5 seconds but sourceforge is slow
int delay = 5000; // msec
// paired with statement below that blows up
ComThread.InitMTA();
ActiveXComponent ie = new ActiveXComponent(
"InternetExplorer.Application");
try {
Dispatch.put(ie, "Visible", new Variant(true));
Dispatch.put(ie, "AddressBar", new Variant(true));
System.out.println("IETestThread: " + Dispatch.get(ie, "Path"));
Dispatch.put(ie, "StatusText", new Variant("My Status Text"));
public void CommandStateChange(Variant[] args) {
System.out.println("IEEvents Received ("+Thread.currentThread().getName()+"): CommandStateChange "+args.length);
}
System.out.println("IETestThread: About to hookup event listener");
IEEvents ieE = new IEEvents();
new DispatchEvents(ie, ieE, "InternetExplorer.Application.1");
System.out.println("IETestThread: Did hookup event listener");
// / why is this here? Was there some other code here in the past?
Variant optional = new Variant();
optional.putNoParam();
public void DocumentComplete(Variant[] args) {
System.out.println("IEEvents Received ("+Thread.currentThread().getName()+"): DocumentComplete "+args.length);
}
System.out
.println("IETestThread: About to call navigate to sourceforge");
Dispatch.call(ie, "Navigate", new Variant(
"http://sourceforge.net/projects/jacob-project"));
System.out
.println("IETestThread: Did call navigate to sourceforge");
try {
Thread.sleep(delay);
} catch (Exception e) {
}
System.out.println("IETestThread: About to call navigate to yahoo");
Dispatch.call(ie, "Navigate", new Variant(
"http://groups.yahoo.com/group/jacob-project"));
System.out.println("IETestThread: Did call navigate to yahoo");
try {
Thread.sleep(delay);
} catch (Exception e) {
}
} catch (Exception e) {
threadFailedWithException = e;
e.printStackTrace();
} catch (Throwable re) {
threadFailedWithException = re;
re.printStackTrace();
} finally {
System.out.println("IETestThread: About to send Quit");
ie.invoke("Quit", new Variant[] {});
System.out.println("IETestThread: Did send Quit");
}
// this blows up when it tries to release a DispatchEvents object
// I think this is because there is still one event we should get back
// "OnQuit" that will came after we have released the thread pool
// this is probably messed up because DispatchEvent object will have
// been
// freed before the callback
// commenting out ie.invoke(quit...) causes this to work without error
// this code tries to wait until the quit has been handled but that
// doesn't work
System.out
.println("IETestThread: Waiting until we've received the quit callback");
while (!quitHandled) {
try {
Thread.sleep(delay / 5);
} catch (InterruptedException e) {
}
}
System.out.println("IETestThread: Received the quit callback");
// wait a little while for it to end
// try {Thread.sleep(delay); } catch (InterruptedException e) {}
System.out
.println("IETestThread: about to call ComThread.Release in thread "
+ Thread.currentThread().getName());
public void DownloadBegin(Variant[] args) {
System.out.println("IEEvents Received ("+Thread.currentThread().getName()+"): DownloadBegin "+args.length);
}
ComThread.Release();
}
public void DownloadComplete(Variant[] args) {
System.out.println("IEEvents Received ("+Thread.currentThread().getName()+"): DownloadComplete "+args.length);
}
/**
* The events class must be publicly accessable for reflection to work. The
* list of available events is located at
* http://msdn2.microsoft.com/en-us/library/aa768280.aspx
*/
public class IEEvents {
/**
* Internet explorer event this proxy can receive
*
* @param args
* the COM Variant objects that this event passes in.
*/
public void BeforeNavigate2(Variant[] args) {
System.out.println("IEEvents Received ("
+ Thread.currentThread().getName() + "): BeforeNavigate2 "
+ args.length);
}
public void NavigateError(Variant[] args) {
System.out.println("IEEvents Received ("+Thread.currentThread().getName()+"): NavigateError "+args.length);
}
/**
* Internet explorer event this proxy can receive
*
* @param args
* the COM Variant objects that this event passes in.
*/
public void CommandStateChange(Variant[] args) {
System.out.println("IEEvents Received ("
+ Thread.currentThread().getName()
+ "): CommandStateChange " + args.length);
}
public void NavigateComplete2(Variant[] args) {
System.out.println("IEEvents Received ("+Thread.currentThread().getName()+"): NavigateComplete "+args.length);
}
/**
* Internet explorer event this proxy can receive
*
* @param args
* the COM Variant objects that this event passes in.
*/
public void DocumentComplete(Variant[] args) {
System.out.println("IEEvents Received ("
+ Thread.currentThread().getName() + "): DocumentComplete "
+ args.length);
}
public void NewWindow2(Variant[] args) {
System.out.println("IEEvents Received ("+Thread.currentThread().getName()+"): NewWindow2 "+args.length);
}
/**
* Internet explorer event this proxy can receive
*
* @param args
* the COM Variant objects that this event passes in.
*/
public void DownloadBegin(Variant[] args) {
System.out.println("IEEvents Received ("
+ Thread.currentThread().getName() + "): DownloadBegin "
+ args.length);
}
public void OnFullScreen(Variant[] args) {
System.out.println("IEEvents Received ("+Thread.currentThread().getName()+"): OnFullScreen "+args.length);
}
/**
* Internet explorer event this proxy can receive
*
* @param args
* the COM Variant objects that this event passes in.
*/
public void DownloadComplete(Variant[] args) {
System.out.println("IEEvents Received ("
+ Thread.currentThread().getName() + "): DownloadComplete "
+ args.length);
}
public void OnMenuBar(Variant[] args) {
System.out.println("IEEvents Received ("+Thread.currentThread().getName()+"): OnMenuBar "+args.length);
}
/**
* Internet explorer event this proxy can receive
*
* @param args
* the COM Variant objects that this event passes in.
*/
public void NavigateError(Variant[] args) {
System.out.println("IEEvents Received ("
+ Thread.currentThread().getName() + "): NavigateError "
+ args.length);
}
public void OnQuit(Variant[] args) {
System.out.println("IEEvents Received ("+Thread.currentThread().getName()+"): OnQuit "+args.length);
IETestThread.quitHandled = true;
}
/**
* Internet explorer event this proxy can receive
*
* @param args
* the COM Variant objects that this event passes in.
*/
public void NavigateComplete2(Variant[] args) {
System.out.println("IEEvents Received ("
+ Thread.currentThread().getName() + "): NavigateComplete "
+ args.length);
}
public void OnStatusBar(Variant[] args) {
System.out.println("IEEvents Received ("+Thread.currentThread().getName()+"): OnStatusBar "+args.length);
}
/**
* Internet explorer event this proxy can receive
*
* @param args
* the COM Variant objects that this event passes in.
*/
public void NewWindow2(Variant[] args) {
System.out.println("IEEvents Received ("
+ Thread.currentThread().getName() + "): NewWindow2 "
+ args.length);
}
public void OnTheaterMode(Variant[] args) {
System.out.println("IEEvents Received ("+Thread.currentThread().getName()+"): OnTheaterMode "+args.length);
}
/**
* Internet explorer event this proxy can receive
*
* @param args
* the COM Variant objects that this event passes in.
*/
public void OnFullScreen(Variant[] args) {
System.out.println("IEEvents Received ("
+ Thread.currentThread().getName() + "): OnFullScreen "
+ args.length);
}
public void OnToolBar(Variant[] args) {
System.out.println("IEEvents Received ("+Thread.currentThread().getName()+"): OnToolBar "+args.length);
}
/**
* Internet explorer event this proxy can receive
*
* @param args
* the COM Variant objects that this event passes in.
*/
public void OnMenuBar(Variant[] args) {
System.out.println("IEEvents Received ("
+ Thread.currentThread().getName() + "): OnMenuBar "
+ args.length);
}
public void OnVisible(Variant[] args) {
System.out.println("IEEvents Received ("+Thread.currentThread().getName()+"): OnVisible "+args.length);
}
/**
* Internet explorer event this proxy can receive
*
* @param args
* the COM Variant objects that this event passes in.
*/
public void OnQuit(Variant[] args) {
System.out.println("IEEvents Received ("
+ Thread.currentThread().getName() + "): OnQuit "
+ args.length);
IETestThread.quitHandled = true;
}
public void ProgressChange(Variant[] args) {
System.out.println("IEEvents Received ("+Thread.currentThread().getName()+"): ProgressChange "+args.length);
}
/**
* Internet explorer event this proxy can receive
*
* @param args
* the COM Variant objects that this event passes in.
*/
public void OnStatusBar(Variant[] args) {
System.out.println("IEEvents Received ("
+ Thread.currentThread().getName() + "): OnStatusBar "
+ args.length);
}
public void PropertyChange(Variant[] args) {
System.out.println("IEEvents Received ("+Thread.currentThread().getName()+"): PropertyChange "+args.length);
}
/**
* Internet explorer event this proxy can receive
*
* @param args
* the COM Variant objects that this event passes in.
*/
public void OnTheaterMode(Variant[] args) {
System.out.println("IEEvents Received ("
+ Thread.currentThread().getName() + "): OnTheaterMode "
+ args.length);
}
public void SetSecureLockIcon(Variant[] args) {
System.out.println("IEEvents Received ("+Thread.currentThread().getName()+"): setSecureLockIcon "+args.length);
}
/**
* Internet explorer event this proxy can receive
*
* @param args
* the COM Variant objects that this event passes in.
*/
public void OnToolBar(Variant[] args) {
System.out.println("IEEvents Received ("
+ Thread.currentThread().getName() + "): OnToolBar "
+ args.length);
}
public void StatusTextChange(Variant[] args) {
System.out.println("IEEvents Received ("+Thread.currentThread().getName()+"): StatusTextChange "+args.length);
}
/**
* Internet explorer event this proxy can receive
*
* @param args
* the COM Variant objects that this event passes in.
*/
public void OnVisible(Variant[] args) {
System.out.println("IEEvents Received ("
+ Thread.currentThread().getName() + "): OnVisible "
+ args.length);
}
public void TitleChange(Variant[] args) {
System.out.println("IEEvents Received ("+Thread.currentThread().getName()+"): TitleChange "+args.length);
}
/**
* Internet explorer event this proxy can receive
*
* @param args
* the COM Variant objects that this event passes in.
*/
public void ProgressChange(Variant[] args) {
System.out.println("IEEvents Received ("
+ Thread.currentThread().getName() + "): ProgressChange "
+ args.length);
}
public void WindowClosing(Variant[] args) {
System.out.println("IEEvents Received ("+Thread.currentThread().getName()+"): WindowClosing "+args.length);
}
}
/**
* Internet explorer event this proxy can receive
*
* @param args
* the COM Variant objects that this event passes in.
*/
public void PropertyChange(Variant[] args) {
System.out.println("IEEvents Received ("
+ Thread.currentThread().getName() + "): PropertyChange "
+ args.length);
}
/**
* Internet explorer event this proxy can receive
*
* @param args
* the COM Variant objects that this event passes in.
*/
public void SetSecureLockIcon(Variant[] args) {
System.out.println("IEEvents Received ("
+ Thread.currentThread().getName()
+ "): setSecureLockIcon " + args.length);
}
/**
* Internet explorer event this proxy can receive
*
* @param args
* the COM Variant objects that this event passes in.
*/
public void StatusTextChange(Variant[] args) {
System.out.println("IEEvents Received ("
+ Thread.currentThread().getName() + "): StatusTextChange "
+ args.length);
}
/**
* Internet explorer event this proxy can receive
*
* @param args
* the COM Variant objects that this event passes in.
*/
public void TitleChange(Variant[] args) {
System.out.println("IEEvents Received ("
+ Thread.currentThread().getName() + "): TitleChange "
+ args.length);
}
/**
* Internet explorer event this proxy can receive
*
* @param args
* the COM Variant objects that this event passes in.
*/
public void WindowClosing(Variant[] args) {
System.out.println("IEEvents Received ("
+ Thread.currentThread().getName() + "): WindowClosing "
+ args.length);
}
}
}

View File

@@ -6,213 +6,284 @@ import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import com.jacob.test.BaseTestCase;
/**
* This test runs fine against jdk 1.4 and 1.5
*
* This demonstrates the new event handling code in jacob 1.7
* This example will open up IE and print out some of the events
* it listens to as it havigates to web sites.
* contributed by Niels Olof Bouvin mailto:n.o.bouvin@daimi.au.dk
* and Henning Jae jehoej@daimi.au.dk
* This demonstrates the new event handling code in jacob 1.7 This example will
* open up IE and print out some of the events it listens to as it havigates to
* web sites. contributed by Niels Olof Bouvin mailto:n.o.bouvin@daimi.au.dk and
* Henning Jae jehoej@daimi.au.dk
* <p>
* May need to run with some command line options (including from inside Eclipse).
* Look in the docs area at the Jacob usage document for command line options.
* May need to run with some command line options (including from inside
* Eclipse). Look in the docs area at the Jacob usage document for command line
* options.
*/
public class IETestActiveXProxy extends BaseTestCase {
public void testIEActiveProxyCallback() {
// this line starts the pump but it runs fine without it
ComThread.startMainSTA();
// remove this line and it dies
///ComThread.InitMTA(true);
IETestActiveProxyThread aThread = new IETestActiveProxyThread();
aThread.start();
while (aThread.isAlive()){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// doen with the sleep
//e.printStackTrace();
}
}
System.out.println("Main: Thread quit, about to quit main sta in thread "
+Thread.currentThread().getName());
// this line only does someting if startMainSTA() was called
ComThread.quitMainSTA();
System.out.println("Main: did quit main sta in thread "
+Thread.currentThread().getName());
if (aThread.threadFailedWithException != null){
fail("caught an unexpected exception "+aThread.threadFailedWithException);
}
}
/**
* the main test method that builds up the connection and runs the test
*/
public void testIEActiveProxyCallback() {
// this line starts the pump but it runs fine without it
ComThread.startMainSTA();
// remove this line and it dies
// /ComThread.InitMTA(true);
IETestActiveProxyThread aThread = new IETestActiveProxyThread();
aThread.start();
while (aThread.isAlive()) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// doen with the sleep
// e.printStackTrace();
}
}
System.out
.println("Main: Thread quit, about to quit main sta in thread "
+ Thread.currentThread().getName());
// this line only does someting if startMainSTA() was called
ComThread.quitMainSTA();
System.out.println("Main: did quit main sta in thread "
+ Thread.currentThread().getName());
if (aThread.threadFailedWithException != null) {
fail("caught an unexpected exception "
+ aThread.threadFailedWithException);
}
}
}
class IETestActiveProxyThread extends Thread
{
class IETestActiveProxyThread extends Thread {
/** says that the quit message has been received from the target application */
public static boolean quitHandled = false;
/**
* holds any caught exception so the main/test case can see them
*/
public Throwable threadFailedWithException = null;
public IETestActiveProxyThread(){
super();
}
public void run()
{
// this used to be 5 seconds but sourceforge is slow
int delay = 5000; // msec
// paired with statement below that blows up
ComThread.InitMTA();
ActiveXComponent ie = new ActiveXComponent("InternetExplorer.Application");
try {
Dispatch.put(ie, "Visible", new Variant(true));
Dispatch.put(ie, "AddressBar", new Variant(true));
System.out.println("IETestActiveProxyThread: " + Dispatch.get(ie, "Path"));
Dispatch.put(ie, "StatusText", new Variant("My Status Text"));
System.out.println("IETestActiveProxyThread: About to hookup event listener");
IEEventsActiveProxy ieE = new IEEventsActiveProxy();
new ActiveXDispatchEvents(ie, ieE,"InternetExplorer.Application.1");
System.out.println("IETestActiveProxyThread: Did hookup event listener");
/// why is this here? Was there some other code here in the past?
Variant optional = new Variant();
optional.putNoParam();
System.out.println("IETestActiveProxyThread: About to call navigate to sourceforge");
Dispatch.call(ie, "Navigate", new Variant("http://sourceforge.net/projects/jacob-project"));
System.out.println("IETestActiveProxyThread: Did call navigate to sourceforge");
try { Thread.sleep(delay); } catch (Exception e) {}
System.out.println("IETestActiveProxyThread: About to call navigate to yahoo");
Dispatch.call(ie, "Navigate", new Variant("http://groups.yahoo.com/group/jacob-project"));
System.out.println("IETestActiveProxyThread: Did call navigate to yahoo");
try { Thread.sleep(delay); } catch (Exception e) {}
} catch (Exception e) {
threadFailedWithException = e;
e.printStackTrace();
} catch (Throwable re){
threadFailedWithException = re;
re.printStackTrace();
} finally {
System.out.println("IETestActiveProxyThread: About to send Quit");
ie.invoke("Quit", new Variant[] {});
System.out.println("IETestActiveProxyThread: Did send Quit");
}
// this blows up when it tries to release a DispatchEvents object
// I think this is because there is still one event we should get back
// "OnQuit" that will came after we have released the thread pool
// this is probably messed up because DispatchEvent object will have been
// freed before the callback
// commenting out ie.invoke(quit...) causes this to work without error
// this code tries to wait until the quit has been handled but that doesn't work
System.out.println("IETestActiveProxyThread: Waiting until we've received the quit callback");
while (!quitHandled){
try { Thread.sleep(delay/5);} catch (InterruptedException e) {}
}
System.out.println("IETestActiveProxyThread: Received the quit callback");
// wait a little while for it to end
//try {Thread.sleep(delay); } catch (InterruptedException e) {}
System.out.println("IETestActiveProxyThread: about to call ComThread.Release in thread " +
Thread.currentThread().getName());
ComThread.Release();
}
/** the thread's constructor */
public IETestActiveProxyThread() {
super();
}
/**
* The events class must be publicly accessable for reflection to work.
* The list of available events is located at http://msdn2.microsoft.com/en-us/library/aa768280.aspx
*/
public class IEEventsActiveProxy
{
public void BeforeNavigate2(Dispatch pDisp, String url, Integer flags,
String targetFrameName, Variant postData, String headers, Boolean cancel) {
System.out.println("IEEventsActiveProxy Received ("+Thread.currentThread().getName()+"): BeforeNavigate2 "+url);
}
public void run() {
// this used to be 5 seconds but sourceforge is slow
int delay = 5000; // msec
// paired with statement below that blows up
ComThread.InitMTA();
ActiveXComponent ie = new ActiveXComponent(
"InternetExplorer.Application");
try {
Dispatch.put(ie, "Visible", new Variant(true));
Dispatch.put(ie, "AddressBar", new Variant(true));
System.out.println("IETestActiveProxyThread: "
+ Dispatch.get(ie, "Path"));
Dispatch.put(ie, "StatusText", new Variant("My Status Text"));
public void CommandStateChange(Integer command, Boolean enable) {
System.out.println("IEEventsActiveProxy Received ("+Thread.currentThread().getName()+"): CommandStateChange "+command);
}
System.out
.println("IETestActiveProxyThread: About to hookup event listener");
IEEventsActiveProxy ieE = new IEEventsActiveProxy();
new ActiveXDispatchEvents(ie, ieE, "InternetExplorer.Application.1");
System.out
.println("IETestActiveProxyThread: Did hookup event listener");
// / why is this here? Was there some other code here in the past?
Variant optional = new Variant();
optional.putNoParam();
public void DocumentComplete(Dispatch pDisp, String url) {
System.out.println("IEEventsActiveProxy Received ("+Thread.currentThread().getName()+"): DocumentComplete "+url);
}
System.out
.println("IETestActiveProxyThread: About to call navigate to sourceforge");
Dispatch.call(ie, "Navigate", new Variant(
"http://sourceforge.net/projects/jacob-project"));
System.out
.println("IETestActiveProxyThread: Did call navigate to sourceforge");
try {
Thread.sleep(delay);
} catch (Exception e) {
}
System.out
.println("IETestActiveProxyThread: About to call navigate to yahoo");
Dispatch.call(ie, "Navigate", new Variant(
"http://groups.yahoo.com/group/jacob-project"));
System.out
.println("IETestActiveProxyThread: Did call navigate to yahoo");
try {
Thread.sleep(delay);
} catch (Exception e) {
}
} catch (Exception e) {
threadFailedWithException = e;
e.printStackTrace();
} catch (Throwable re) {
threadFailedWithException = re;
re.printStackTrace();
} finally {
System.out.println("IETestActiveProxyThread: About to send Quit");
ie.invoke("Quit", new Variant[] {});
System.out.println("IETestActiveProxyThread: Did send Quit");
}
// this blows up when it tries to release a DispatchEvents object
// I think this is because there is still one event we should get back
// "OnQuit" that will came after we have released the thread pool
// this is probably messed up because DispatchEvent object will have
// been
// freed before the callback
// commenting out ie.invoke(quit...) causes this to work without error
// this code tries to wait until the quit has been handled but that
// doesn't work
System.out
.println("IETestActiveProxyThread: Waiting until we've received the quit callback");
while (!quitHandled) {
try {
Thread.sleep(delay / 5);
} catch (InterruptedException e) {
}
}
System.out
.println("IETestActiveProxyThread: Received the quit callback");
// wait a little while for it to end
// try {Thread.sleep(delay); } catch (InterruptedException e) {}
System.out
.println("IETestActiveProxyThread: about to call ComThread.Release in thread "
+ Thread.currentThread().getName());
public void DownloadBegin() {
System.out.println("IEEventsActiveProxy Received ("+Thread.currentThread().getName()+"): DownloadBegin ");
}
ComThread.Release();
}
public void DownloadComplete() {
System.out.println("IEEventsActiveProxy Received ("+Thread.currentThread().getName()+"): DownloadComplete ");
}
/**
* The events class must be publicly accessable for reflection to work. The
* list of available events is located at
* http://msdn2.microsoft.com/en-us/library/aa768280.aspx
*/
public class IEEventsActiveProxy {
public void NavigateComplete2(Dispatch pDisp, String url) {
System.out.println("IEEventsActiveProxy Received ("+Thread.currentThread().getName()+"): NavigateComplete "+url);
}
public void BeforeNavigate2(Dispatch pDisp, String url, Integer flags,
String targetFrameName, Variant postData, String headers,
Boolean cancel) {
System.out.println("IEEventsActiveProxy Received ("
+ Thread.currentThread().getName() + "): BeforeNavigate2 "
+ url);
}
public void NavigateError(Dispatch pDispatch, String url, String targetFrameName, Integer statusCode, Boolean Cancel) {
System.out.println("IEEvents Received ("+Thread.currentThread().getName()+"): NavigateError "+statusCode);
}
public void CommandStateChange(Integer command, Boolean enable) {
System.out.println("IEEventsActiveProxy Received ("
+ Thread.currentThread().getName()
+ "): CommandStateChange " + command);
}
public void NewWindow2(Dispatch pDisp, Boolean cancel) {
System.out.println("IEEventsActiveProxy Received ("+Thread.currentThread().getName()+"): NewWindow2 "+pDisp);
}
public void DocumentComplete(Dispatch pDisp, String url) {
System.out.println("IEEventsActiveProxy Received ("
+ Thread.currentThread().getName() + "): DocumentComplete "
+ url);
}
public void OnFullScreen(Boolean fullScreen) {
System.out.println("IEEventsActiveProxy Received ("+Thread.currentThread().getName()+"): OnFullScreen "+fullScreen);
}
public void DownloadBegin() {
System.out.println("IEEventsActiveProxy Received ("
+ Thread.currentThread().getName() + "): DownloadBegin ");
}
public void OnMenuBar(Boolean menuBar) {
System.out.println("IEEventsActiveProxy Received ("+Thread.currentThread().getName()+"): OnMenuBar "+menuBar);
}
public void DownloadComplete() {
System.out
.println("IEEventsActiveProxy Received ("
+ Thread.currentThread().getName()
+ "): DownloadComplete ");
}
public void OnQuit() {
System.out.println("IEEventsActiveProxy Received ("+Thread.currentThread().getName()+"): OnQuit ");
IETestActiveProxyThread.quitHandled = true;
}
public void NavigateComplete2(Dispatch pDisp, String url) {
System.out.println("IEEventsActiveProxy Received ("
+ Thread.currentThread().getName() + "): NavigateComplete "
+ url);
}
public void OnStatusBar(Boolean statusBar) {
System.out.println("IEEventsActiveProxy Received ("+Thread.currentThread().getName()+"): OnStatusBar "+statusBar);
}
public void NavigateError(Dispatch pDispatch, String url,
String targetFrameName, Integer statusCode, Boolean Cancel) {
System.out.println("IEEvents Received ("
+ Thread.currentThread().getName() + "): NavigateError "
+ statusCode);
}
public void OnTheaterMode(Boolean theaterMode) {
System.out.println("IEEventsActiveProxy Received ("+Thread.currentThread().getName()+"): OnTheaterMode "+theaterMode);
}
public void NewWindow2(Dispatch pDisp, Boolean cancel) {
System.out.println("IEEventsActiveProxy Received ("
+ Thread.currentThread().getName() + "): NewWindow2 "
+ pDisp);
}
public void OnToolBar(Boolean onToolBar) {
System.out.println("IEEventsActiveProxy Received ("+Thread.currentThread().getName()+"): OnToolBar "+onToolBar);
}
public void OnFullScreen(Boolean fullScreen) {
System.out.println("IEEventsActiveProxy Received ("
+ Thread.currentThread().getName() + "): OnFullScreen "
+ fullScreen);
}
public void OnVisible(Boolean onVisible) {
System.out.println("IEEventsActiveProxy Received ("+Thread.currentThread().getName()+"): onVisible "+ onVisible);
}
public void OnMenuBar(Boolean menuBar) {
System.out.println("IEEventsActiveProxy Received ("
+ Thread.currentThread().getName() + "): OnMenuBar "
+ menuBar);
}
public void ProgressChange() {
System.out.println("IEEventsActiveProxy Received ("+Thread.currentThread().getName()+"): ProgressChange ");
}
public void OnQuit() {
System.out.println("IEEventsActiveProxy Received ("
+ Thread.currentThread().getName() + "): OnQuit ");
IETestActiveProxyThread.quitHandled = true;
}
public void PropertyChange() {
System.out.println("IEEventsActiveProxy Received ("+Thread.currentThread().getName()+"): PropertyChange ");
}
public void OnStatusBar(Boolean statusBar) {
System.out.println("IEEventsActiveProxy Received ("
+ Thread.currentThread().getName() + "): OnStatusBar "
+ statusBar);
}
public void SetSecureLockIcon(Integer secureLockIcon) {
System.out.println("IEEvents Received ("+Thread.currentThread().getName()+"): setSecureLockIcon "+secureLockIcon);
}
public void OnTheaterMode(Boolean theaterMode) {
System.out.println("IEEventsActiveProxy Received ("
+ Thread.currentThread().getName() + "): OnTheaterMode "
+ theaterMode);
}
public void StatusTextChange() {
System.out.println("IEEventsActiveProxy Received ("+Thread.currentThread().getName()+"): StatusTextChange ");
}
public void OnToolBar(Boolean onToolBar) {
System.out.println("IEEventsActiveProxy Received ("
+ Thread.currentThread().getName() + "): OnToolBar "
+ onToolBar);
}
public void TitleChange() {
System.out.println("IEEventsActiveProxy Received ("+Thread.currentThread().getName()+"): TitleChange ");
}
public void OnVisible(Boolean onVisible) {
System.out.println("IEEventsActiveProxy Received ("
+ Thread.currentThread().getName() + "): onVisible "
+ onVisible);
}
public void WindowClosing(Boolean isChildWindow) {
System.out.println("IEEvents Received ("+Thread.currentThread().getName()+"): WindowClosing "+isChildWindow);
}
}
public void ProgressChange() {
System.out.println("IEEventsActiveProxy Received ("
+ Thread.currentThread().getName() + "): ProgressChange ");
}
public void PropertyChange() {
System.out.println("IEEventsActiveProxy Received ("
+ Thread.currentThread().getName() + "): PropertyChange ");
}
public void SetSecureLockIcon(Integer secureLockIcon) {
System.out.println("IEEvents Received ("
+ Thread.currentThread().getName()
+ "): setSecureLockIcon " + secureLockIcon);
}
public void StatusTextChange() {
System.out
.println("IEEventsActiveProxy Received ("
+ Thread.currentThread().getName()
+ "): StatusTextChange ");
}
public void TitleChange() {
System.out.println("IEEventsActiveProxy Received ("
+ Thread.currentThread().getName() + "): TitleChange ");
}
public void WindowClosing(Boolean isChildWindow) {
System.out.println("IEEvents Received ("
+ Thread.currentThread().getName() + "): WindowClosing "
+ isChildWindow);
}
}
}

View File

@@ -9,19 +9,20 @@ import com.jacob.test.BaseTestCase;
/**
* This test was lifted from a forum posting and shows how you can't listen to
* Excel events (added post 1.9.1 Eclipse Settings.)
* That test was modified make this a MSWord event listener to demonstrate
* that the InvocationProxy code works with MS Word Events
* This also uses the 1.10
* InvocationProxy to receive the events.
* Excel events (added post 1.9.1 Eclipse Settings.) That test was modified make
* this a MSWord event listener to demonstrate that the InvocationProxy code
* works with MS Word Events This also uses the 1.10 InvocationProxy to receive
* the events.
* <p>
* May need to run with some command line options (including from inside Eclipse).
* Look in the docs area at the Jacob usage document for command line options.
* May need to run with some command line options (including from inside
* Eclipse). Look in the docs area at the Jacob usage document for command line
* options.
*/
public class WordEventTest extends BaseTestCase {
/**
* load up word, register for events and make stuff happen
*
* @param args
*/
public void testCaptureWordEvents() {
@@ -49,38 +50,44 @@ public class WordEventTest extends BaseTestCase {
// this is different from the ExcelEventTest because it uses
// the jacob active X api instead of the Dispatch api
System.out.println("version=" + axc.getPropertyAsString("Version"));
axc.setProperty("Visible",true);
ActiveXComponent documents = axc.getPropertyAsComponent("Documents");
if (documents == null){
axc.setProperty("Visible", true);
ActiveXComponent documents = axc
.getPropertyAsComponent("Documents");
if (documents == null) {
fail("unable to get documents");
}
axc.invoke("Quit", new Variant[] {});
} catch (ComException cfe) {
cfe.printStackTrace();
fail("Failed to attach to " + pid + ": " + cfe.getMessage());
fail("Failed to attach to " + pid + ": " + cfe.getMessage());
}
System.out.println(
"Someone needs to add some MSWord commands to this to " +
"make some on screen stuff happens so the tester " +
"thinks we tested something");
}
public class WordEvents extends InvocationProxy {
/**
* Constructor so we can create an instance that implements invoke()
*/
public WordEvents() {
System.out
.println("Someone needs to add some MSWord commands to this to "
+ "make some on screen stuff happens so the tester "
+ "thinks we tested something");
}
/**
* override the invoke() method to log all the events without writing a bunch of code
* a class that receives messages from word
*/
public Variant invoke(String methodName, Variant targetParameter[]) {
System.out.println("Received event from Windows program" + methodName);
return null;
}
public class WordEvents extends InvocationProxy {
/**
* Constructor so we can create an instance that implements invoke()
*/
public WordEvents() {
}
}
/**
* override the invoke() method to log all the events without writing a
* bunch of code
*/
public Variant invoke(String methodName, Variant targetParameter[]) {
System.out.println("Received event from Windows program"
+ methodName);
return null;
}
}
}