home
products
education
support
partners
company

introduction

get started

articles

manual

tutorials

reference docs

case studies

knowledge base

goodies

screenshots

demos

echoes

Carousel zone




home

forums

downloads

bugs

mailing list
XUI Zone - manual

number of ratings 0
average rating 0.00

18 Menus

Some applications require menus for common operations like File, Save, Print and so on. Although rarely used, Carousel provides a means to include a menu bar and a simple mechanism for defining the content of that menu.

Creating a Menu

In the XML page descriptions menus can be added by inserting an MenuBar element. The menu bar element can contain Menu elements which in turn can contain MenuItems . For the Menu and MenuItem elements the content attribute is used as the menu text. A MenuItem without a content attribute will be added as a menu separator. The content of the menu can be the key into the language resource bundle for the current application language.

Code Sample 18-1 - Creating a simple menu

An important consideration when creating menus is the lifecycle of a menu. While created as part of a page a menu does not come and go with the defining page but instead lives and is displayed for the duration of the application. Only one instance of the menubar is ever created, although items may be added to the menu at any stage.

A menu is an application wide component and is defined once for the application. One useful way of reinforcing this is to include the menu in the start page rather than inserting the menu definition directly as above.

Hooking up Events

Menu events can be hooked up to response handlers using the same event handling mechanism as used by any other component.

Code Sample 18-2 - Menu Event Handling

When considering the menu lifecycle and the longevity of the menu for conceptual reasons it is worth considering using a menus in conjunction with a frameset so that the menu handlers can be defined as part of a long lived page. Alternatively the menu definition can be declared in a separate XML file and included in another page using the include statement.

Because menus are not owned by a particular page in the application they cannot be looked up in the same way as other components which use the findComponent() method of the XPage class. Instead the root menu can be obtained from the XApplet for the current project.

Code Sample 18-3 - Retrieving the application menu

XProjectManager.getCurrentProject().getApplet().getMenuBar();

If a reference to a named menu item is required, then it's necessary to iterate the menus and find the child whose name corresponds. In a localized application, although the menus will be created automatically with the correct language, if the language is changed and the page manager reset, the menus will need to be reset manually. This is illustrated below.

Code Sample 18-4 - Localizing a menu

public class SomePage extends XPage {

String[] name = { "mnuFile", "mnuFileNew", "mnuFileSave" };

String[] tran = { "MNU_FILE", "MNU_NEW_PROJ", "MNU_FILE_SAVE" };

public void changeLanguage()

{

JMenuBar mBar = project.getApplet().getMenuBar();

for ( int i = 0; i < mBar.getMenuCount(); i++ ) {

iterateMenus( mBar.getMenu( i ) );

}

}

private void iterateMenus( JMenu menu )

{

setMenuText( menu );

for ( int i = 0; i < menu.getMenuComponentCount(); i++ ) {

setMenuText( menu.getMenuComponent( i ) );

}

}

private void setMenuText( Component c )

{

if ( c instanceof XMenu ) {

( ( XMenu ) c ).setText( getText( ( XMenu ) c ) );

}

}

private String getText( XMenu menu )

{

String name = menu.getName();

for ( int i = 0; i < names.length; i++ ) {

if ( name.compareTo( names[ i ] ) == 0 ) {

return translate( translations [ i ] );

}

}

return menu.getText();

}

}

comments


If you were logged in you could rate this article or add a comment.