reorganized the samples, again. Moved the programs that were more test cases to the unit test directory and repackaged the unit test directory.
This commit is contained in:
88
unittest/com/jacob/test/events/ExcelEventTest.java
Normal file
88
unittest/com/jacob/test/events/ExcelEventTest.java
Normal file
@@ -0,0 +1,88 @@
|
||||
package com.jacob.test.events;
|
||||
|
||||
import com.jacob.activeX.ActiveXComponent;
|
||||
import com.jacob.com.ComException;
|
||||
import com.jacob.com.Dispatch;
|
||||
import com.jacob.com.DispatchEvents;
|
||||
import com.jacob.com.InvocationProxy;
|
||||
import com.jacob.com.Variant;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* <p> supported command line options with default values are
|
||||
* -Djava.library.path=d:/jacob/release -Dcom.jacob.autogc=false
|
||||
* -Dcom.jacob.debug=false
|
||||
*/
|
||||
public class ExcelEventTest extends InvocationProxy {
|
||||
|
||||
/**
|
||||
* load up excel, register for events and make stuff happen
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String args[]) {
|
||||
String pid = "Excel.Application";
|
||||
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 ExcelEventTest());
|
||||
} else {
|
||||
de = new DispatchEvents(axc, new ExcelEventTest(), 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);
|
||||
|
||||
}
|
||||
|
||||
System.out.println("version=" + axc.getProperty("Version"));
|
||||
System.out.println("version=" + Dispatch.get(axc, "Version"));
|
||||
axc.setProperty("Visible", true);
|
||||
Dispatch workbooks = axc.getPropertyAsComponent("Workbooks");
|
||||
Dispatch workbook = Dispatch.get(workbooks, "Add").toDispatch();
|
||||
Dispatch sheet = Dispatch.get(workbook, "ActiveSheet").toDispatch();
|
||||
Dispatch a1 = Dispatch.invoke(sheet, "Range", Dispatch.Get,
|
||||
new Object[] { "A1" }, new int[1]).toDispatch();
|
||||
Dispatch a2 = Dispatch.invoke(sheet, "Range", Dispatch.Get,
|
||||
new Object[] { "A2" }, new int[1]).toDispatch();
|
||||
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"));
|
||||
Variant f = new Variant(false);
|
||||
Dispatch.call(workbook, "Close", f);
|
||||
axc.invoke("Quit", new Variant[] {});
|
||||
|
||||
} catch (ComException cfe) {
|
||||
cfe.printStackTrace();
|
||||
System.out.println("Failed to attach to " + pid + ": "
|
||||
+ cfe.getMessage());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* dummy consturctor to create an InvocationProxy that wraps nothing
|
||||
*/
|
||||
public ExcelEventTest() {
|
||||
}
|
||||
|
||||
/**
|
||||
* override the invoke method to log all the events
|
||||
*/
|
||||
public Variant invoke(String methodName, Variant targetParameter[]) {
|
||||
System.out.println("Received event from Windows program" + methodName);
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
176
unittest/com/jacob/test/events/IETest.java
Normal file
176
unittest/com/jacob/test/events/IETest.java
Normal file
@@ -0,0 +1,176 @@
|
||||
package com.jacob.test.events;
|
||||
|
||||
import com.jacob.com.*;
|
||||
import com.jacob.activeX.*;
|
||||
/**
|
||||
* It looks like this test is broken again on the cleanup
|
||||
*
|
||||
* 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>
|
||||
* You can run this in eclipse with the command line options
|
||||
* <code> -Djava.library.path=d:/jacob/release -Dcom.jacob.autogc=false </code>
|
||||
*/
|
||||
|
||||
class IETest
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
// 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("Thread quit, about to quit main sta");
|
||||
// this line only does someting if startMainSTA() was called
|
||||
ComThread.quitMainSTA();
|
||||
System.out.println("did quit main sta");
|
||||
}
|
||||
}
|
||||
|
||||
class IETestThread extends Thread
|
||||
{
|
||||
public static boolean quitHandled = false;
|
||||
|
||||
public IETestThread(){
|
||||
super();
|
||||
}
|
||||
|
||||
public void run()
|
||||
{
|
||||
// this used to be 5 seconds but sourceforge is slow
|
||||
int delay = 10000; // 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(Dispatch.get(ie, "Path"));
|
||||
Dispatch.put(ie, "StatusText", new Variant("My Status Text"));
|
||||
|
||||
IEEvents ieE = new IEEvents();
|
||||
new DispatchEvents((Dispatch) ie, ieE,"InternetExplorer.Application.1");
|
||||
Variant optional = new Variant();
|
||||
optional.noParam();
|
||||
|
||||
Dispatch.call(ie, "Navigate", new Variant("http://sourceforge.net/projects/jacob-project"));
|
||||
try { Thread.sleep(delay); } catch (Exception e) {}
|
||||
Dispatch.call(ie, "Navigate", new Variant("http://groups.yahoo.com/group/jacob-project"));
|
||||
try { Thread.sleep(delay); } catch (Exception e) {}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
ie.invoke("Quit", new Variant[] {});
|
||||
}
|
||||
// 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("IETest: Waiting until we've received the quit callback");
|
||||
while (!quitHandled){
|
||||
try { Thread.sleep(delay/5);} catch (InterruptedException e) {}
|
||||
}
|
||||
// wait a little while for it to end
|
||||
try {Thread.sleep(delay); } catch (InterruptedException e) {}
|
||||
System.out.println("IETest: about to call release in thread " +
|
||||
Thread.currentThread().getName());
|
||||
|
||||
ComThread.Release();
|
||||
}
|
||||
|
||||
/**
|
||||
* the events class must be publicly accessable for reflection to work
|
||||
*/
|
||||
public class IEEvents
|
||||
{
|
||||
public void BeforeNavigate2(Variant[] args) {
|
||||
System.out.println("IEEvents: BeforeNavigate2");
|
||||
}
|
||||
|
||||
public void CommandStateChange(Variant[] args) {
|
||||
System.out.println("IEEvents: CommandStateChange");
|
||||
}
|
||||
|
||||
public void DocumentComplete(Variant[] args) {
|
||||
System.out.println("IEEvents: DocumentComplete");
|
||||
}
|
||||
|
||||
public void DownloadBegin(Variant[] args) {
|
||||
System.out.println("IEEvents: DownloadBegin");
|
||||
}
|
||||
|
||||
public void DownloadComplete(Variant[] args) {
|
||||
System.out.println("IEEvents: DownloadComplete");
|
||||
}
|
||||
|
||||
public void NavigateComplete2(Variant[] args) {
|
||||
System.out.println("IEEvents: NavigateComplete2");
|
||||
}
|
||||
|
||||
public void NewWindow2(Variant[] args) {
|
||||
System.out.println("IEEvents: NewWindow2");
|
||||
}
|
||||
|
||||
public void OnFullScreen(Variant[] args) {
|
||||
System.out.println("IEEvents: OnFullScreen");
|
||||
}
|
||||
|
||||
public void OnMenuBar(Variant[] args) {
|
||||
System.out.println("IEEvents: OnMenuBar");
|
||||
}
|
||||
|
||||
public void OnQuit(Variant[] args) {
|
||||
System.out.println("IEEvents: OnQuit");
|
||||
IETestThread.quitHandled = true;
|
||||
}
|
||||
|
||||
public void OnStatusBar(Variant[] args) {
|
||||
System.out.println("IEEvents: OnStatusBar");
|
||||
}
|
||||
|
||||
public void OnTheaterMode(Variant[] args) {
|
||||
System.out.println("IEEvents: OnTheaterMode");
|
||||
}
|
||||
|
||||
public void OnToolBar(Variant[] args) {
|
||||
System.out.println("IEEvents: OnToolBar");
|
||||
}
|
||||
|
||||
public void OnVisible(Variant[] args) {
|
||||
System.out.println("IEEvents: OnVisible");
|
||||
}
|
||||
|
||||
public void ProgressChange(Variant[] args) {
|
||||
System.out.println("IEEvents: ProgressChange");
|
||||
}
|
||||
|
||||
public void PropertyChange(Variant[] args) {
|
||||
System.out.println("IEEvents: PropertyChange");
|
||||
}
|
||||
|
||||
public void StatusTextChange(Variant[] args) {
|
||||
System.out.println("IEEvents: StatusTextChange");
|
||||
}
|
||||
|
||||
public void TitleChange(Variant[] args) {
|
||||
System.out.println("IEEvents: TitleChange");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
85
unittest/com/jacob/test/events/WordEventTest.java
Normal file
85
unittest/com/jacob/test/events/WordEventTest.java
Normal file
@@ -0,0 +1,85 @@
|
||||
package com.jacob.test.events;
|
||||
|
||||
import com.jacob.activeX.ActiveXComponent;
|
||||
import com.jacob.com.ComException;
|
||||
import com.jacob.com.DispatchEvents;
|
||||
import com.jacob.com.InvocationProxy;
|
||||
import com.jacob.com.Variant;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* <p> supported command line options with default values are
|
||||
* -Djava.library.path=d:/jacob/release -Dcom.jacob.autogc=false
|
||||
* -Dcom.jacob.debug=false
|
||||
*/
|
||||
public class WordEventTest extends InvocationProxy {
|
||||
|
||||
/**
|
||||
* load up word, register for events and make stuff happen
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String args[]) {
|
||||
String pid = "Word.Application";
|
||||
String typeLibLocation = null;
|
||||
|
||||
// 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 WordEventTest());
|
||||
} else {
|
||||
de = new DispatchEvents(axc, new WordEventTest(), pid,
|
||||
typeLibLocation);
|
||||
}
|
||||
if (de == null) {
|
||||
System.out
|
||||
.println("No exception thrown but no dispatch returned for Word events");
|
||||
} else {
|
||||
// Yea!
|
||||
System.out.println("Successfully attached to " + pid);
|
||||
|
||||
}
|
||||
// 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){
|
||||
System.out.println("unable to get documents");
|
||||
}
|
||||
axc.invoke("Quit", new Variant[] {});
|
||||
|
||||
} catch (ComException cfe) {
|
||||
cfe.printStackTrace();
|
||||
System.out.println("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");
|
||||
}
|
||||
|
||||
/**
|
||||
* dummy consturctor to create an InvocationProxy that wraps nothing
|
||||
*/
|
||||
public WordEventTest() {
|
||||
}
|
||||
|
||||
/**
|
||||
* override the invoke method to loga ll the events
|
||||
*/
|
||||
public Variant invoke(String methodName, Variant targetParameter[]) {
|
||||
System.out.println("Received event from Windows program" + methodName);
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user