How to create context tool items


Introduction

This tutorial is about the Jaspersoft Studio extensions that add actions or contribution items for the selected element or elements to the global eclipse toolbar. This actions and contribution items are organized into groups, visibility of this groups of items is controlled by the user using preferences.

Extension Points

com.jaspersoft.studio.toolitems – contains all the actions or ContributionItems that could be found on the toolbar

  • id – the toolitem unique identifier

  • contributionItemClass – ContributionItem class name, null if this is an action

  • ActionID – action ID, null if this is a ContributionItem

  • label – item label, make sens for actions

  • tooltip – item tooltip, description

  • icon – if not null override action icon

for each toolitem it's necessary to add classes for each this item apply

com.jaspersoft.studio.toolitemsets – defines item groups, and their visibility

  • id – group unique identifier

  • name – the name that is visible in preferences page

  • visibility – default group visibility

  • description – description, tooltip of the group

  • toolbaruri – the toolbar uri from global eclipse toolbar namespace

  • menuuri – the menu uri from global eclipse menu (not used for the moment)

for each group, a list of id of toolitems from com.jaspersoft.studio.toolitems should be defined

Steps to add an Action

A good example are actions we already have to set some boolean properties for text elements and actions to help arrange elements in the report, look at:

			com.jaspersoft.studio.editor.action.order.BringBackwardAction
			com.jaspersoft.studio.editor.action.text.BoldAction
			

in this tutorial we will explain BoldAction, this action set bold property on a text element

We have to create an action that usually extends a SelectionAction, in this tutorial we'll extend AbooleanPropertyAction class, this clas implements functionality to set/get a boolean property from the model, we just have to override a method that check if this action apply to the model, and a method that return the property name the action should be added to the editors ActionsRegistry, in out example we'll add this lines to com.jaspersoft.studio.editor.report.AbstractVisualEditor.

		protected void createActions() {

		...

		action = new BoldAction(this);

		registry.registerAction(action);

		selectionActions.add(action.getId());

		...

		}
		

add the item to plugin.xml:

		<extension point="com.jaspersoft.studio.toolitems">

			...

			<toolitems ActionID="com.jaspersoft.studio.editor.action.text.bold" icon="icons/resources/eclipse/font-bold.gif" id="com.jaspersoft.studio.editor.action.text.bold" label="Bold"
				tooltip="Set font to Bold">

				<selectedObject class="com.jaspersoft.studio.model.text.MTextElement"></selectedObject>

			</toolitems>

			...

		</extension>
		

add item to the group, we will use “com.jaspersoft.studio.graphic.text” uri for our toolbar

		<extension point="com.jaspersoft.studio.toolitemsets">

			...

			<toolitemsets description="Text Element properties" id="com.jaspersoft.studio.graphic.text" name="Text Properties" toolbaruri="com.jaspersoft.studio.graphic.text" visibility="true">

				...

				<toolitem toolitemID="com.jaspersoft.studio.editor.action.text.bold"></toolitem>

				...

		</extension>
		

test, select a Static Text element and look if “bold” button is visible in the eclipse global toolbar

Steps to add an ContributionItem

A contribution item give us possibility to add a widget to the toolbar like a combo. In this example we will look at FontNameContributionItem, for more examples look in com.jaspersoft.studio.editor.action.text package.

  1. create the contribution item we will extend ApropertyComboContributionItem class , this class implements the functionality necessary to get and set values in the model, we have just set an ID, property name and implement a method that will populate our combo box.

  2. add the item to plugin.xml:

        <extension point="com.jaspersoft.studio.toolitems">
    
        ...
    
        <toolitems contributionItemClass="com.jaspersoft.studio.editor.action.text.FontNameComboContributionItem" id="com.jaspersoft.studio.editor.action.text.fontname" tooltip="Font Name">
    
        <selectedObject class="com.jaspersoft.studio.model.text.MTextElement"></selectedObject>
    
        </toolitems>
    
        ...
    
        </extension>
    

    meaning is straightforward we tell to extension which class implements contribution item, tooltip for this contribution item and for what selected model classes this item is visible

  3. add item to the group, we will use “com.jaspersoft.studio.graphic.text” uri for our toolbar

        <extension
    
        point="com.jaspersoft.studio.toolitemsets">
    
        ...
    
        <toolitemsets
    
        description="Text Element properties"
    
        id="com.jaspersoft.studio.graphic.text"
    
        name="Text Properties"
    
        toolbaruri="com.jaspersoft.studio.graphic.text"
    
        visibility="true">
    
        ...
    
        <toolitem
    
        toolitemID="com.jaspersoft.studio.editor.action.text.fontname">
    
        </toolitem>
    
        ...
    
        </toolitemsets>
    
        ...
    
        </extension>

  4. test, select a Static Text element and look if combo with font names is visible in the eclipse global toolbar

Preferences

To hide or show a group of items, go to Preferences → Jaspersoft Studio → Toolbars and use checkboxes.

Figure 1. Toolbar visibility preferences page

Toolbar visibility preferences page