Contributing to context toolbar

From Aptana Development

Contributing to Context Toolbar

This page describes how to contibute items to Aptana Context toolbar .

We contributing items to context toolbar by extending extension-point “com.aptana.ide.editors.toolbarContribution”. Aptana editors look up for items that may be contributed to toolbar for the content types they edit. Whenever current context is changed(for example user moved cursor into script section in the html editor), recalculation of visible items is performed. Each contributed item must be attached to particular mime type and is visible when and only when user edits content of corresponding mime type. Also each item has attached name icon and tooltip.

Note: In case when two items has same name only first of them is shown currently.

Instructions

As a simple, hypothetical example, let us contribute an simple item that will count hyperlinks in the html file and then show it to the user.

1. Add <element> entry to “com.aptana.ide.editors.toolbarContribution” extension point.


    <extension
         point="com.aptana.ide.editors.toolbarContribution">
      <element            class="com.aptana.ide.sample.toolbar.SampleToolBarItem"
            name="Sample item"
            language="text/html"
            tooltip="This is a sample item"
            icon="/icons/sample.png"
      />
    </extension>  

class 
Fully qualified Java class name implementing com.aptana.ide.editors.toolbar.IToolBarMember interface.
language 
MIME type of content to wich this item is related.
tooltip 
Tooltip for toolbar item.
icon 
path to icon toolbar item.
name 
Descriptive name of the toolbar item.

2. Write Java class specified in step 1 above, implementing IToolBarMember interface.

The com.aptana.ide.editors.IToolBarMember interface has just one function:


    void execute(IUnifiedEditor editor, String string);

editor
editor on which this toolbar member should operate.
string
name of the toolbar item.


For the example scenario at hand, we can have a simple implementation as below.


    public void execute(IUnifiedEditor editor, String string)
    {
	UnifiedEditor u_editor = (UnifiedEditor) editor;
	IParseState parseState = u_editor.getFileContext().getParseState();
	LexemeList lst = parseState.getLexemeList();
	int acount = 0;
	for (int a = 0; a < lst.size(); a++)
	{
		Lexeme lexeme = lst.get(a);
		if (lexeme.getLanguage().equals(HTMLMimeType.MimeType))
		{
			if (lexeme.getToken().getTypeIndex() == HTMLTokenTypes.START_TAG)
			{
				String tagName = HTMLUtils.stripTagEndings(lexeme.getText());
				if (tagName.equals("a")) //$NON-NLS-1$
				{
					acount++;
				}
			}
		}
	}
	MessageDialog.openInformation(editor.getViewer().getTextWidget().getShell(), "Count", acount //$NON-NLS-1$
			+ " hyperlinks was found"); //$NON-NLS-1$
    }

3. Verify that the contribution thus registered is detected in Aptana. Build everything and launch IDE. Open html editor. You should see you item added to context toolbar.

Building on this example, one can devise a generalized scheme to contribute actions with context driven visibility to toolbar.

If you need to manage your contributions programmatically for example add and remove your contributions at runtime you should follow other way to contibute items to context driven toolbar. In this case you should add <contribution> entry to “com.aptana.ide.editors.toolbarContribution” extension point.


    <extension
         point="com.aptana.ide.editors.toolbarContribution">
      <contributor class="com.aptana.ide.snippets.ToolbarRegistryContributor"/>
    </extension>  

class 
Fully qualified Java class name implementing com.aptana.ide.editors.toolbar.IToolbarRegistryContributor interface.

Instances of com.aptana.ide.editors.toolbar.IToolbarRegistryContributor has just one method:


    void contributeToToolbarRegistry(IToolbarContributionRegistry registry);

which is called when context driven toolbar is first shown. At this point you may contribute items programmatically using



    void addContribution(String language, ToolBarContribution contribution);
  
    void removeContribution(String language, ToolBarContribution contribution);
      

methods of IToolbarContributionRegistry, or remember instance for future use.

Note: You also may obtain this instance using static method getInstance() of class com.aptana.ide.editors.toolbar.ToolBarContributionRegistry.

Via Snippets

This can also be done non-programmatically via the snippets system. See http://www.aptana.com/docs/index.php/Adding_a_new_Snippet for more details.