HOWTOs

How to center an image in a table cell

If you have a table column with images, it is sometimes makes sense to center the images. Assuming you work with a subclass of StdTableView. Just override the method centerImageInCell and return true if the specified image is an image you want to center or if the given colIndex is in the column with images you want center. Here is an example:

@Override
protected boolean centerImageInCell( Image image, int colIndex )
{
if ( getTableContainer() instanceof FieldBasedTableContainer )
{
// if you use a field-based view, just compare the given colIndex with the index of the image column
FieldBasedTableContainer container = ( FieldBasedTableContainer ) getTableContainer();
int imageColumnIndex = container.getColumnIndex( ANONYM_LOGGEDIN_IDENTIFIER );
return ObjectUtils.equals( colIndex, imageColumnIndex + StdTableContainer.RESERVED_COLUMNS );
}
// otherwise, you can compare the given image with the images which should be centered
StdAbstractUIPlugin plugin = UserManagementUiPlugin.getDefault();
Image active = plugin.getImage( ResourceConst.IMG_ACTIVE );
return ObjectUtils.equals( active, image );
}

As result your image will be centered in the table cell:

images/download/attachments/29819092/users_logged_in_small.png

How to contribute your own status line in the table view

If you want to implement your own status line component in the table view, just contribute the extension point com.heiler.ppm.std.ui.tableViewStatusLines. The default contribution of the status line component should be commented then. Here is the default contribution as example:

<extension point="com.heiler.ppm.std.ui.tableViewStatusLines">
<statusLine
id="com.heiler.ppm.std.ui.table.statusline.defaultTableViewStatusLine"
class="com.heiler.ppm.std.ui.table.statusline.DefaultTableViewStatusLine">
</statusLine>
</extension>

The status line component should contains a status text control (usually a label), to show the status text in the table view. Additionaly you can add other controls to the status line component. The status line component will be added to the footer area of the table view.

How to customize the status text in the table view

If you want to show you own text in the table view's status line, just contribute the extension point com.heiler.ppm.std.ui.statusTextResolvers. The default contribution of the status text resolver should be commented then. Here is the default contribution as example:

<extension point="com.heiler.ppm.std.ui.statusTextResolvers">
<resolver
id="com.heiler.ppm.std.ui.table.statusline.defaultStatusTextResolver"
class="com.heiler.ppm.std.ui.table.statusline.DefaultStatusTextResolver">
</resolver>
</extension>

Your own StatusTextResolver will be called each time the selection of the table view has been changed. As parameter your resolver receives a tableViewer and should return a suitable status text for the current table state. The default resolver just shows the number of selected items in the table:

images/download/attachments/29819092/table_view_status_line_text.png

How to add a custom menu item to the text celleditor context menu

Since we have our own context menu for the text celleditor, it is possible to append a custom menu item there. Let us assume, we would add a new menu item "To Upper Case" on the context menu. When the user clicks this menu item, the selected text in the celleditor will be converted to uppercase.

To do this we need the following:

  1. Define a command, which will be executed, when the menu item is clicked:

    <command
    defaultHandler="com.heiler.ppm.std.ui.example.ToUpperCaseHandler"
    id="com.heiler.ppm.std.ui.example.toUpperCase"
    name="To Upper Case">
    </command>
  2. Implement the default handler for this command, which converts the selected text in the celleditor to uppecase (see com.heiler.ppm.std.ui.example.ToUpperCaseHandler for implementation details).

  3. Now you should add a contribution for the extension point org.eclipse.ui.menus:

    <extension
    point="org.eclipse.ui.menus">
    <menuContribution
    allPopups="false"
    locationURI="popup:com.heiler.ppm.std.ui.celleditors">
    <separator
    visible="true"
    name="edit">
    </separator>
    <command
    commandId="com.heiler.ppm.std.ui.example.toUpperCase"
    style="push"
    label="%menu.toUpperCase.label"
    icon="icons/UpperCase.png">
    </command>
    </menuContribution>
    </extension>

    As result, your custom menu item is added to the celleditor:
    images/download/attachments/29819092/celleditor_context_menu_custom_item.png

Note: if you enable the native context menu for the text celleditors (in client plugin_customization.ini), your custom menu item will be not appear.

How to contribute a context menu item for a specific Entity

If you want to add a context menu item for several views, which show entries of a specific entity, you can use the corresponding PropertyTester.

  • Create a menu contribution with the locationURI "popup:org.eclipse.ui.popup.any"

  • Add a visibleWhen condition to the menu contribution with a property tester which tests, if the selected entries are of a specific entity. This should prevent that the menu item appears in all views regardless of the showing entity.

<extension
point="org.eclipse.ui.menus">
<menuContribution
allPopups="false"
locationURI="popup:org.eclipse.ui.popup.any?after=edit-ext">
<command
commandId="com.heiler.ppm.assortment.ui.command.cloneAssortment"
id="com.heiler.ppm.product2g.assortment.ui.cloneProductAssortment"
style="push"
icon="icons/16x16/Copy.gif"
label="%command.CloneProductAssortment.label"
tooltip="%command.CloneProductAssortment.tooltip">
<visibleWhen
checkEnabled="false">
<with
variable="selection">
<test
property="com.heiler.ppm.std.ui.adapter.entity"
value="Product2GAssortment">
</test>
</with>
</visibleWhen>
</command>
</menuContribution>
</extension>

In this example the contributed menu item will be only added to views which show the product assortments.

How to create a dynamic menu contribution item

Sometimes it is necessary to create a menu item with dynamic sub-menus, which are determined and built at runtime in a Java class. For this you need first your own implementation of class CompoundContributionItem:

VERY IMPORTANT: don't override the constructor with the parameter 'id', otherwise the contribution item will be NOT appear in the menu.

public class ExampleDynamicContributionItem extends CompoundContributionItem
{
@Override
protected IContributionItem[] getContributionItems()
{
MenuManager menu = new MenuManager( "Example menu", getId() ); //$NON-NLS-1$
 
IContributionItem subMenu1 = createExampleContributionItem( "Submenu 1" ); //$NON-NLS-1$
menu.add( subMenu1 );
IContributionItem subMenu2 = createExampleContributionItem( "Submenu 2" ); //$NON-NLS-1$
menu.add( subMenu2 );
IContributionItem subMenu3 = createExampleContributionItem( "Submenu 3" ); //$NON-NLS-1$
menu.add( subMenu3 );
 
return new IContributionItem[] { menu };
}
 
/** Creates a new example command contribution item with the given label. */
private IContributionItem createExampleContributionItem( String label )
{
String itemId = hashCode() + "." + label; //$NON-NLS-1$
String commandId = ExampleHandler.COMMAND_ID;
CommandContributionItemParameter parameter = new CommandContributionItemParameter(
StdUiUtils.getActiveSite(),
itemId,
commandId,
CommandContributionItem.STYLE_PUSH );
parameter.label = label;
CommandContributionItem item = new CommandContributionItem( parameter );
return item;
}
}

Now you only need to contribute your dynamic menu item:

<menuContribution
locationURI="menu:com.heiler.ppm.example.ui.views.ExampleView">
<dynamic
id="com.heiler.ppm.example.ui.handler.example"
class="com.heiler.ppm.example.ui.handler.ExampleDynamicContributionItem">
</dynamic>
</menuContribution>

How to create/override a key binding for a command contribution

If you want to bind a command contribution on a key sequence, you just add a contribution for the extension point "org.eclipse.ui.bindings". But if the key sequence is already bound on an other command?

You can override the key binding for the current context using "contextId" parameter of the binding contribution.

Here is an example for the "delete" key. The key sequence "DEL" is bound in HPM main plugin on the command "org.eclipse.ui.edit.delete". So if you want to call a specific command in a specific view, when the user press the "delete" key, you should "override" the key binding for the view context.

Therefor you need to define a context for the view:

<extension point="org.eclipse.ui.contexts">
<context
parentId="org.eclipse.ui.contexts.window"
name="StructureFeatureTableView context"
id="com.heiler.ppm.structurefeature.ui.internal.view.StructureFeatureTableView">
</extension>

Then in the view class we activate this context in the context service:

public class StructureFeatureTableView extends ListModelTableView implements IEntityChangeListener
{
public static final String VIEW_ID = "com.heiler.ppm.structurefeature.ui.internal.view.StructureFeatureTableView"; //$NON-NLS-1$
...
 
@Override
public void createPartControl( final Composite parent )
{
...
 
// set the view context to support the key binding for 'delete' command contribution
IContextService contextService = ( IContextService ) getSite().getService( IContextService.class );
contextService.activateContext( VIEW_ID );
}

Now you can bind the "delete" key on your command with the specified context:

<extension
point="org.eclipse.ui.bindings">
<key
commandId="com.heiler.ppm.structurefeature.ui.handler.deleteStructureFeature"
contextId="com.heiler.ppm.structurefeature.ui.internal.view.StructureFeatureTableView"
schemeId="com.heiler.ppm.main.defaultAcceleratorConfiguration"
sequence="DEL">
</key>
</extension>  

How to add a permission to popupmenu action

Note: Actions were used before commands exist. Please use commands instead of actions.

If you need to add a permission for the visibility of an action you need to add following at the plugin.xml

<extension
point="org.eclipse.ui.popupMenus">
<viewerContribution
id="ArticleTableView.PopupMenu"
targetID="com.heiler.ppm.article.ui.views.ArticleTableView">
<visibility>
<objectState
name="permissions" value="com.heiler.ppm.structure.core.permission.StructureGroupRead">
</objectState>
</visibility>
<action .... />
</viewerContribution>
</extension>