com.jacob.com.DispatchEvents

Here are the examples of the java api com.jacob.com.DispatchEvents taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

10 Examples 7

19 Source : ExcelEventTest.java
with GNU Lesser General Public License v2.1
from freemansoft

/**
 * 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 listener to " + programId);
    }
}

19 Source : VisioApp.java
with GNU Lesser General Public License v2.1
from freemansoft

/**
 * creates a DispatchEvents object to register o as a listener
 *
 * @param o
 */
public void addEventListener(VisioEventListener o) {
    DispatchEvents events = new DispatchEvents(this, o);
    if (events == null) {
        System.out.println("You should never get null back when creating a DispatchEvents object");
    }
}

18 Source : ScriptTest3ActiveX.java
with GNU Lesser General Public License v2.1
from freemansoft

/**
 * Here we create the ScriptControl component in a separate MTA thread and then
 * call the Eval method from the main thread. The main thread must also be an
 * MTA thread. If you try to create it as an STA then you will not be able to
 * make calls into a component running in another thread.
 * <p>
 * May need to run with some command line options (including from inside
 * Eclipse). Look in the docs area at the Jacob usage doreplacedent for command line
 * options.
 */
public clreplaced ScriptTest3ActiveX extends BaseTestCase {

    public static ActiveXComponent sC;

    public static DispatchEvents de = null;

    public static boolean quit = false;

    public void testYetAnotherScriptTest() {
        try {
            ComThread.InitMTA();
            ScriptTest3ActiveXInner script = new ScriptTest3ActiveXInner();
            script.start();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ie) {
            // should we get this?
            }
            Variant result = sC.invoke("Eval", getSampleVPScriptForEval());
            System.out.println("eval(" + getSampleVPScriptForEval() + ") = " + result);
            System.out.println("setting quit");
            ScriptTest3ActiveX.quit = true;
        } catch (ComException e) {
            e.printStackTrace();
            fail("Caught ComException " + e);
        } finally {
            System.out.println("main done");
            ComThread.Release();
        }
    }

    public clreplaced ScriptTest3ActiveXInner extends Thread {

        public void run() {
            try {
                ComThread.InitMTA();
                System.out.println("OnInit");
                String lang = "VBScript";
                sC = new ActiveXComponent("ScriptControl");
                sC.setProperty("Language", lang);
                ScriptTestErrEvents te = new ScriptTestErrEvents();
                de = new DispatchEvents(sC, te);
                System.out.println("sControl=" + sC);
                while (!quit) {
                    sleep(100);
                }
                ComThread.Release();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                System.out.println("worker thread exits");
            }
        }
    }
}

18 Source : MathTest.java
with GNU Lesser General Public License v2.1
from freemansoft

/**
 * not clear why we need a clreplaced and run method but that's the way it was
 * written
 */
public void runTest() {
    // deprecated
    // System.runFinalizersOnExit(true);
    Dispatch test = new ActiveXComponent("MathTest.Math");
    TestEvents te = new TestEvents();
    DispatchEvents de = new DispatchEvents(test, te);
    if (de == null) {
        System.out.println("null returned when trying to create DispatchEvents");
    }
    System.out.println(Dispatch.call(test, "Add", new Variant(1), new Variant(2)));
    System.out.println(Dispatch.call(test, "Mult", new Variant(2), new Variant(2)));
    Variant v = Dispatch.call(test, "Mult", new Variant(2), new Variant(2));
    // this should return false
    System.out.println("v.isNull=" + v.isNull());
    v = Dispatch.call(test, "getNothing");
    // these should return nothing
    System.out.println("v.isNull=" + v.isNull());
    System.out.println("v.toDispatch=" + v.toDispatch());
}

18 Source : IiTunes.java
with GNU General Public License v3.0
from 5zig-reborn

/**
 * Defines the top-level iTunes application object.
 * <p>
 * This interface defines the top-level iTunes application object. All other iTunes interfaces are accessed through this object.
 */
public clreplaced IiTunes {

    private ActiveXComponent iTunes;

    private DispatchEvents dispatchEvents;

    /**
     * Initiate iTunes Controller.
     */
    public IiTunes() {
        iTunes = new ActiveXComponent("iTunes.Application");
    }

    /**
     * Sets an event handler to the iTunes controller.
     *
     * @param eventHandler The clreplaced that will handle the iTunes events.
     */
    public void addEventHandler(IiTunesEventHandler eventHandler) {
        IiTunesEvents iTunesEvents = new IiTunesEvents(eventHandler);
        dispatchEvents = new DispatchEvents(iTunes, iTunesEvents);
    }

    /**
     * Play the currently targeted track.
     */
    public void play() {
        iTunes.invoke("Play");
    }

    /**
     * Pause playback.
     */
    public void pause() {
        iTunes.invoke("Pause");
    }

    /**
     * Toggle the playing/paused state of the current track.
     */
    public void playPause() {
        iTunes.invoke("PlayPause");
    }

    /**
     * Disable fast forward/rewind and resume playback, if playing.
     */
    public void resume() {
        iTunes.invoke("Resume");
    }

    /**
     * Stop playback.
     */
    public void stop() {
        iTunes.invoke("Stop");
    }

    /**
     * Reposition to the beginning of the current track or go to the previous
     * track if already at start of current track.
     */
    public void backTrack() {
        iTunes.invoke("BackTrack");
    }

    /**
     * Skip forward in a playing track.
     */
    public void fastForward() {
        iTunes.invoke("FastForward");
    }

    /**
     * Advance to the next track in the current playlist.
     */
    public void nextTrack() {
        iTunes.invoke("NextTrack");
    }

    /**
     * Skip backwards in a playing track.
     */
    public void rewind() {
        iTunes.invoke("Rewind");
    }

    /**
     * Return to the previous track in the current playlist.
     */
    public void previousTrack() {
        iTunes.invoke("PreviousTrack");
    }

    /**
     * Returns the currently targetd track.
     *
     * @return An ITTrack object corresponding to the currently targeted track.
     * Will be set to NULL if there is no currently targeted track.
     */
    public IITTrack getCurrentTrack() {
        Variant variant = iTunes.getProperty("CurrentTrack");
        if (variant.isNull()) {
            return null;
        }
        Dispatch item = variant.toDispatch();
        IITTrack track = new IITTrack(item);
        if (track.getKind() == IITTrackKind.ITTrackKindFile) {
            return new IITFileOrCDTrack(item);
        } else if (track.getKind() == IITTrackKind.ITTrackKindCD) {
            return new IITFileOrCDTrack(item);
        } else if (track.getKind() == IITTrackKind.ITTrackKindURL) {
            return new ITURLTrack(item);
        } else {
            return track;
        }
    }

    /**
     * Returns the current player state.
     *
     * @return Returns the current player state.
     */
    public IITPlayerState getPlayerState() {
        return IITPlayerState.values()[Dispatch.get(iTunes, "PlayerState").getInt()];
    }

    /**
     * Sets the player's position within the currently playing track in
     * seconds.
     * If playerPos specifies a position before the beginning of the track,
     * the position will be set to the beginning. If playerPos specifies a
     * position after the end of the track, the position will be set to the
     * end.
     *
     * @param playerPos The player's position within the currently playing
     *                  track in seconds.
     */
    public void setPlayerPosition(int playerPos) {
        iTunes.setProperty("playerPosition", playerPos);
    }

    /**
     * Returns the player's position within the currently playing track in
     * seconds.
     *
     * @return The player's position within the currently playing track in
     * seconds.
     */
    public int getPlayerPosition() {
        return iTunes.getPropertyAsInt("playerPosition");
    }

    /**
     * Exits the iTunes application.
     */
    public void quit() {
        iTunes.invoke("Quit");
    }

    public void release() {
        dispatchEvents.safeRelease();
        iTunes.safeRelease();
    }
}

17 Source : ScriptTest3.java
with GNU Lesser General Public License v2.1
from freemansoft

/**
 * Here we create the ScriptControl component in a separate MTA thread and then
 * call the Eval method from the main thread. The main thread must also be an
 * MTA thread. If you try to create it as an STA then you will not be able to
 * make calls into a component running in another thread.
 * <p>
 * May need to run with some command line options (including from inside
 * Eclipse). Look in the docs area at the Jacob usage doreplacedent for command line
 * options.
 */
public clreplaced ScriptTest3 extends BaseTestCase {

    public static ActiveXComponent sC;

    public static DispatchEvents de = null;

    public static Dispatch sControl = null;

    public static boolean quit = false;

    public void testScript() {
        try {
            ComThread.InitMTA();
            ScriptTest3Inner script = new ScriptTest3Inner();
            script.start();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ie) {
            // should we get this?
            }
            Variant result = Dispatch.call(sControl, "Eval", getSampleVPScriptForEval());
            System.out.println("eval(" + getSampleVPScriptForEval() + ") = " + result);
            System.out.println("setting quit");
            ScriptTest3.quit = true;
        } catch (ComException e) {
            e.printStackTrace();
            fail("Caught excpetion running script with MTA");
        } finally {
            System.out.println("main done");
            ComThread.Release();
        }
    }

    clreplaced ScriptTest3Inner extends Thread {

        public void run() {
            try {
                ComThread.InitMTA();
                System.out.println("OnInit");
                String lang = "VBScript";
                sC = new ActiveXComponent("ScriptControl");
                sControl = sC.getObject();
                Dispatch.put(sControl, "Language", lang);
                ScriptTestErrEvents te = new ScriptTestErrEvents();
                de = new DispatchEvents(sControl, te);
                System.out.println("sControl=" + sControl);
                while (!quit) {
                    sleep(100);
                }
                ComThread.Release();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                System.out.println("worker thread exits");
            }
        }
    }
}

17 Source : ScriptTest2ActiveX.java
with GNU Lesser General Public License v2.1
from freemansoft

/**
 * This example demonstrates how to make calls between two different STA's.
 * First, to create an STA, you need to extend the STA clreplaced and override its
 * OnInit() method. This method will be called in the STA's thread so you can
 * use it to create your COM components that will run in that STA. If you then
 * try to call methods on those components from other threads (STA or MTA) -
 * this will fail. You cannot create a component in an STA and call its methods
 * from another thread. You can use the DispatchProxy to get a proxy to any
 * Dispatch that lives in another STA. This object has to be created in the STA
 * that houses the Dispatch (in this case it's created in the OnInit method).
 * Then, another thread can call the toDispatch() method of DispatchProxy to get
 * a local proxy. At most ONE (!) thread can call toDispatch(), and the call can
 * be made only once. This is because a IStream object is used to preplaced the
 * proxy, and it is only written once and closed when you read it. If you need
 * multiple threads to access a Dispatch pointer, then create that many
 * DispatchProxy objects.
 * <p>
 * May need to run with some command line options (including from inside
 * Eclipse). Look in the docs area at the Jacob usage doreplacedent for command line
 * options.
 */
public clreplaced ScriptTest2ActiveX extends BaseTestCase {

    public static ActiveXComponent sC;

    public static DispatchEvents de = null;

    public static DispatchProxy sCon = null;

    public void testActiveXSTA() {
        try {
            ComThread.InitSTA();
            ScriptTest2ActiveXSTA script = new ScriptTest2ActiveXSTA();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ie) {
            // should we get this?
            }
            // get a thread-local Dispatch from sCon
            ActiveXComponent sc = new ActiveXComponent(sCon.toDispatch());
            // call a method on the thread-local Dispatch obtained
            // from the DispatchProxy. If you try to make the same
            // method call on the sControl object - you will get a
            // ComException.
            String scriptCommand = getSampleVPScriptForEval();
            Variant result = sc.invoke("Eval", scriptCommand);
            System.out.println("eval(" + scriptCommand + ") = " + result);
            script.quit();
            System.out.println("called quit");
        } catch (ComException e) {
            e.printStackTrace();
            fail("blew up with Com Exception " + e);
        } finally {
            Integer I = null;
            for (int i = 1; i < 1000000; i++) {
                I = new Integer(i);
            }
            System.out.println(I);
            ComThread.Release();
        }
    }

    public clreplaced ScriptTest2ActiveXSTA extends STA {

        public boolean OnInit() {
            try {
                System.out.println("OnInit");
                System.out.println(Thread.currentThread());
                String lang = "VBScript";
                sC = new ActiveXComponent("ScriptControl");
                // sCon can be called from another thread
                sCon = new DispatchProxy(sC);
                sC.setProperty("Language", lang);
                ScriptTestErrEvents te = new ScriptTestErrEvents();
                de = new DispatchEvents(sC, te);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }

        public void OnQuit() {
            System.out.println("OnQuit");
        }
    }
}

15 Source : ScriptTestActiveX.java
with GNU Lesser General Public License v2.1
from freemansoft

public void testActiveXScript() {
    ComThread.InitSTA(true);
    DispatchEvents de = null;
    try {
        String lang = "VBScript";
        ActiveXComponent sC = new ActiveXComponent("ScriptControl");
        sC.setProperty("Language", lang);
        ScriptTestErrEvents te = new ScriptTestErrEvents();
        de = new DispatchEvents(sC, te);
        if (de == null) {
            System.out.println("null returned when trying to create DispatchEvents");
        }
        Variant result;
        result = sC.invoke("Eval", getSampleVPScriptForEval());
        // call it twice to see the objects reused
        result = sC.invoke("Eval", getSampleVPScriptForEval());
        // call it 3 times to see the objects reused
        result = sC.invoke("Eval", getSampleVPScriptForEval());
        System.out.println("eval(" + getSampleVPScriptForEval() + ") = " + result);
    } catch (ComException e) {
        e.printStackTrace();
    } finally {
        Integer I = null;
        for (int i = 1; i < 1000000; i++) {
            I = new Integer(i);
        }
        System.out.println(I);
        ComThread.Release();
        ComThread.quitMainSTA();
    }
}

15 Source : ScriptTest.java
with GNU Lesser General Public License v2.1
from freemansoft

public void testCreatingDispatchEvents() {
    ComThread.InitSTA(true);
    DispatchEvents de = null;
    Dispatch sControl = null;
    try {
        String scriptCommand = getSampleVPScriptForEval();
        String lang = "VBScript";
        ActiveXComponent sC = new ActiveXComponent("ScriptControl");
        sControl = sC.getObject();
        Dispatch.put(sControl, "Language", lang);
        ScriptTestErrEvents te = new ScriptTestErrEvents();
        de = new DispatchEvents(sControl, te);
        if (de == null) {
            System.out.println("Received null when trying to create new DispatchEvents");
        }
        Variant result = Dispatch.call(sControl, "Eval", scriptCommand);
        // call it twice to see the objects reused
        result = Dispatch.call(sControl, "Eval", scriptCommand);
        // call it 3 times to see the objects reused
        result = Dispatch.call(sControl, "Eval", scriptCommand);
        System.out.println("eval(" + scriptCommand + ") = " + result);
    } catch (ComException e) {
        e.printStackTrace();
        fail("Caught Exception " + e);
    } finally {
        Integer I = null;
        for (int i = 1; i < 1000000; i++) {
            I = new Integer(i);
        }
        System.out.println(I);
        ComThread.Release();
        ComThread.quitMainSTA();
    }
}

15 Source : WordEventTest.java
with GNU Lesser General Public License v2.1
from freemansoft

/**
 * load up word, register for events and make stuff happen
 *
 * @param args
 */
public void testCaptureWordEvents() {
    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) {
            fail("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.getPropertyreplacedtring("Version"));
        axc.setProperty("Visible", true);
        ActiveXComponent doreplacedents = axc.getPropertyAsComponent("Doreplacedents");
        if (doreplacedents == null) {
            fail("unable to get doreplacedents");
        }
        axc.invoke("Quit", new Variant[] {});
    } catch (ComException cfe) {
        cfe.printStackTrace();
        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");
}