Tutorial: Application events

This tutorial describes how to use application events to handle multiple threads in your application. If you bump into an unknown concept or term, take a look in the concepts or the API section.

Adding application events

If you don't know what application events are, you can take a quick look on the concepts page, but basically it is a way of telling JUseCase to wait for some external event (non-GUI event) to occur, such as a file being fully loaded, an incoming network connection etc. We'll try to fake this in our demo application by adding a button that when clicked will allow us to close the window, i.e. not until the button is clicked the window can be closed.

Compile the updated application (source code here) and temporarily disable replaying by removing the "replay=usecase.txt" line from jusecase.properties. Then run the application and make sure you can't close the window until you have clicked the button.

Now re-enable replaying by inserting the "replay=usecase.txt" line again. Start the application and notice that JUseCase no longer closes the window for you. It tries to, but since you haven't clicked the button it won't work - JUseCase doesn't know it has to wait for you to click the button. This is where application events come in!

At the same time we activate the closing mechanism we must also inform JUseCase of this. This is done by the following call after activeM is set to true:

ScriptEngine.instance().applicationEvent("activation");

When called, this will (in recording mode) record the line "wait for activation" to the use case script. In replaying mode the JUseCase replayer will pause when reading the "wait for ..." command, and not until the applicationEvent call is reached it will continue. So, to try it out we edit the use case script:

wait for activation quit

We could have configured JUseCase for recording mode, start the application, clicked the button and then closed the window, and that would have created a proper use case script for us, but since the change is so small, we might as well edit the script by hand.

So, run the application (which now should be launched in replay mode since we haven't changed the configuration file) and wait for the main window to appear. JUseCase has now read the "wait for..." command, and is waiting for us to click the button before it will try to close the window. So, click the button and watch JUseCase close the window for us again.

Conclusion

Now you have been introduced to application events and how to use them. The next tutorial describes how you create your own event recorder/simulator classes to deal with custom components.