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

PDF Version Portrait Landscape

Help Did you spot something wrong or missing on this page? If you have an account on this TWiki, you can fix it yourself by editing the page. If you don't have an account, you can send a message to the Editorial Board to tell us about it. Thank you in advance!

Common utilities in HIPE

This section explains some common and recurrent tasks in HIPE, like:

  • Communicating Views or Components with other Views or Components, by triggering events.
  • Send Jython statements for being executed by the Console.
  • The common way of showing errors and messages.
  • How to load icons.

Triggering events

SiteEventHandler interface

The standard way of triggering 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() {

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

Sending execution events

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 tracing the original CommandExecutionRequestEvent, so you can easily synchronize on the completion of the execution. However, this is not needed either, since 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 short script for testing the SiteUtil execution functionality directly from the console. Please note:
  • the reference used is _jconsole, which is a 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 for a general discussion about gui and threads).

from herschel.ia.gui.kernel.util 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.setSize(300, 300)

Creating variables

During the processing of a particular view, it may be wanted to create a variable, so that it is shown in the Variables view and the user can do a further work with it.

Creating a variable can be done either with or without echo to the console.

With echo to the console

Just use one of the methods provided by SiteUtil:

  • Synchronous call: Command result = SiteUtil.execAndWait("yourVariable", "Something()");  // result will inform about the executed command

  • Asynchronous call: SiteUtil.execLater("yourVariable", "Something()");  // request execution to the interpreter but return immediately

Tip, idea Prefer this way when you want the variable creation be recorded in the history, so it can be reproduced later.

Example: For the PolygonHistogramPanel we need to construct a Double1d with the corners of the polygon (which are listed in Double1d edges) and add it to the variables map of the task panel :

        Command c = SiteUtil.execAndWait("Double1d(" + edges.toString() + ")", "pyedges");
	String name = c.getOutputName();
	Object value = c.getOutputValue();
	VariableSelection edgesPixel = new VariableSelection(name, value);
	getMap().put(getTask().getSignature().getTaskParameter("edgesPixel"), edgesPixel);

Without echo to the console

In this case, you can create the variable with any Java code (not to be executed by the interpreter), and then publish it like this:

Object variable = ...;
VariablesUtil.addVariable("yourVariable", variable);

Tip, idea Prefer this way when the creation of the variable is so complex that doing it with Jython commands would be too difficult.

Showing popup dialogs

The PopupDialog class

The Java Swing library provides a class for showing popup dialogs: JOptionPane.
This class is used in different modules along the project.

Now, in order to unify the look and feel of dialogs, and to simplify the API as well (the calls to JOptionPane are normally too long), there is a wrapper class which should be used for showing popup dialogs in HIPE: PopupDialog.

Using this class we ensure that we use HIPE-like icons instead of Java-like icons:

Dialogs with HIPE-like icons

hipe-confirmation.png hipe-error.png hipe-warning.png

Dialogs with Java-like icons

java-confirmation.png java-error.png java-information.png

Help You can use PopupDialog as well if your component is used by both HIPE and JIDE. It internally checks whether to show the HIPE style icons or the Java standard ones.

Sample Java code

The API is easy and self-explanatory, just consult PopupDialog.

For example, the first dialog above could be generated with the following code:

int option = PopupDialog.YES_NO_CANCEL_OPTION;
String title = "Unsaved changes";
String message = fileName + " has unsaved changes.\nSave before closing?";
int res =  PopupDialog.showConfirm(this, message, option, title);
if (res == PopupDialog.YES) {
if (res != PopupDialog.CANCEL && res != PopupDialog.CLOSED) {

How to load icons

Icons are widely used in HIPE.

Instead of needing to work with the Java API directly, when you want to load icons, HIPE provides some utilities for making the job easier:

  • ia.gui.kernel.util.IconLibrary provides some static icons already loaded. If the icon you want to use is there, you are done.
  • ia.gui.kernel.util.IconLoader provides static methods for loading an icon: getIcon accepts a full path to an icon image, while getKernelIcon gets the provided icon image from the ia_gui_kernel location for icons.

If you want to add an icon to the system in your own module, use IconLoader.getIcon with the full path.

Please add your comments!

Topic attachments
I Attachment History Action Size Date Who Comment
PNGpng hipe-confirmation.png r1 manage 11.1 K 2008-08-19 - 11:47 JaimeSaiz Hipe-like confirmation dialog
PNGpng hipe-error.png r1 manage 11.4 K 2008-08-19 - 11:47 JaimeSaiz Hipe-like error dialog
PNGpng hipe-warning.png r1 manage 13.0 K 2008-08-19 - 11:48 JaimeSaiz Hipe-like warning dialog
PNGpng java-confirmation.png r1 manage 10.8 K 2008-08-19 - 11:48 JaimeSaiz Java-like confirmation dialog
PNGpng java-error.png r1 manage 13.2 K 2008-08-19 - 11:48 JaimeSaiz Java-like error dialog
PNGpng java-information.png r1 manage 16.0 K 2008-08-19 - 11:49 JaimeSaiz Java-like information dialog
Edit | Attach | Watch | Print version | History: r39 | r26 < r25 < r24 < r23 | Backlinks | Raw View | Raw edit | More topic actions...
Topic revision: r24 - 2011-06-24 - DavideRizzo
This site is powered by the TWiki collaboration platform Powered by Perl