B-1_14-DEV merge back to main
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user