TWiki> Public Web>DpHipe>DpHipeCommonUtilities (revision 5)EditAttach

Triggering Events and Executing Statements In Hipe

It is possible to communicate to Hipe and to all its Views by triggering events, with this you can also achieve to execute Jython statement whenever is necessary.

This section explains how you can send events to Hipe.


SiteEventHandler interface

The standard way to trigger events is to get a reference to the %EVENTHADLER% interface, which contains all related methods to the Hipe event system. This is typically done via the ViewPart interface that is passed on through the init method of the Viewable.
    private void someMethod() {
        :
        getPart().getEventHandler().trigger(aSiteEvent);
        :
    }

However there are cases where you do not have access to a Viewable and you still want to trigger events for informing the rest of the system about something. In order to achieve this you can use a utility method available in a dedicated utility class.

SiteUtil class

The SiteUtil class provides a set of convenience methods to get access to the Hipe architecture. In order to get its access one condition must be met:

you know at least a reference to a Component that is part of Hipe (i.e. you know at least one java.awt.Component visible in Hipe).

Just assume that you are developing a Modifier for a task dialog and you want to inform Hipe about executing a required statement: the Modifier interface gives you no reference to a ViewPart so that you cannot trigger events in the previous way. However you know that the Modifier is visible in Hipe and therefore you accomplish the SiteUtil constraint:

    private void someMethod() {
        :
        // this is the reference to the modifier
        SiteUtil.trigger(this, aSiteEvent);
        :
    }

Executing Jython statements via the Event system

Hipe has a dedicated Event for asking the execution of Jython statement: CommandExecutionRequestEvent. The event is served by the ConsoleView and the result of its execution is visible both in the screen of the ConsoleView and in the list of the HistoryView (but you don't need to know about these views and their api).

Every time a CommandExecutionRequestEvent event is served Hipe generates an equivalent CommandExecutedEvent event tracing the original CommandExecutionRequestEvent so you can easily synchronize on the completion of the execution. However this is not needed neither as SiteUtil class provides a convenient method for doing it: execAndWait.

    private void someMethod() {
        :
        // this is the reference to the modifier
        Command c = SiteUtil.execAndWait(this, "print 'something'");
        :
    }
The execAndWait method returns a Command tracing all the information about its execution including the statement result (isSuccesful()), last but not least, SiteUtil contains other utility methods that allow for more options including a un-synchronized execution method.

See SiteUtil for the full detail.

A Jython Example

This example provides a quick script for testing the SiteUtil execution functionality directly from the console. Please note:
  • the reference used is _jconsole which is name available in jython and pointing to the jconsole (indeed visible)
  • the call is synchronous and therefore it stops the gui thread for the time of the execution (see http://java.sun.com/docs/books/tutorial/uiswing/concurrency/ for a general discussion about gui and thread).

from herschel.ia.gui.kernel import SiteUtil
from javax.swing import *

frame = JFrame("Hello Jython")

def perform(event):
   SiteUtil.execAndWait(_jconsole, "2*2", "A")
   
button = JButton("Press me", actionPerformed=perform)

frame.add(button)
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE)
frame.setSize(300, 300)
frame.show()

-- NicolaDeCandussio - 05 Jun 2008

Edit | Attach | Watch | Print version | History: r39 | r7 < r6 < r5 < r4 | Backlinks | Raw View | Raw edit | More topic actions...
Topic revision: r5 - 2008-07-24 - JaimeSaiz
 
This site is powered by the TWiki collaboration platform Powered by Perl