Context menu
Another attractive and convenient UI feature is the context menu. The context menu displays links to specific actions and appears when a user right-clicks a selected object with the mouse. To support a custom context menu, implement the extension org.eclipse.birt.report.designer.ui.menuBuilders. The implementer class implements the org.eclipse.birt.report.designer.ui.extensions.IMenuBuilder interface, as shown in Figure 23‑29.
Figure 23‑29 Adding the menuBuilders extension
As shown in Figure 23‑30, the RotatedText elementName binds the menuBuilders extension to the model extension. The implementer class name is org.eclipse.birt.sample.reportitem.rotatedtext.RotatedTextMenuBuilder.
Figure 23‑30 Setting the menuBuilder properties
The menuBuilder extension for RotatedText item contains four custom actions to perform quick rotations at angle values of -90, 90, 0, and 180 degrees. The code is shown in Listing 23‑20.
Listing 23‑20 RotatedTextMenuBuilder class
public class RotatedTextMenuBuilder implements IMenuBuilder
{
public void buildMenu( IMenuManager menu, List selectedList )
{
if ( selectedList != null && selectedList.size( ) == 1
&& selectedList.get( 0 ) instanceof ExtendedItemHandle )
{
ExtendedItemHandle handle =
(ExtendedItemHandle) selectedList.get( 0 );
 
if ( !RotatedTextItem.EXTENSION_NAME.equals(
handle.getExtensionName( ) ) )
{
return;
}
 
RotatedTextItem item = null;
try {
item = (RotatedTextItem) handle.getReportItem( );
} catch ( ExtendedElementException e ) {
e.printStackTrace( );
}
if ( item == null )
{
return;
}
 
Separator separator = new Separator(
"group.rotatedtext" ); //$NON-NLS-1$
if ( menu.getItems( ).length > 0 )
{
menu.insertBefore( menu.getItems( )[0].getId( ),
separator );
}
else
{
menu.add( separator );
}
 
menu.appendToGroup( separator.getId( ),
new RotateAction( item, -90 ) );
menu.appendToGroup( separator.getId( ),
new RotateAction( item, 90 ) );
menu.appendToGroup( separator.getId( ),
new RotateAction( item, 0 ) );
menu.appendToGroup( separator.getId( ),
new RotateAction( item, 180 ) );
}
}
 
static class RotateAction extends Action
{
private RotatedTextItem item;
private int angle;
 
RotateAction( RotatedTextItem item, int angle )
{
this.item = item;
this.angle = angle;
 
setText( "Rotate to " + angle + "\u00BA" );
//$NON-NLS-1$ //$NON-NLS-2$
}
 
public void run( )
{
try {
item.setRotationAngle( angle );
}
catch ( SemanticException e ) {
e.printStackTrace( );
}
}
}
}
Run the designer to test the context menu. Right-click any RotatedText report item in the layout editor. The context menu appears as shown in Figure 23‑31.
Figure 23‑31 Testing the RotatedText item context menu