views:

222

answers:

2

I'm extending Eclipse using the Eclipse plugin infrastructure, and I've come into a problem:

I created a text editor and I would like to add actions to the Eclipse toolbar when my editor is open and has focus. For example:

textViewer.getTextWidget().addFocusListener(new FocusListener(){
public void focusGained(FocusEvent e) {
  /* add actions */         
}

public void focusLost(FocusEvent e) {
 /* remove actions */           
}

});

The following example of extensionPoint: ActionSet, add the action button to the toolbar permanently:

<action
class="MyActionClass"
id="MyActionID"
label="MyActionLabel"
menubarPath="MyActionMenuBarPath"
toolbarPath="MyActionToolBarPath" <-- this property
     ...
</action>

how to make this dynamically?

A: 

You could look at the Eclipse implementation of similar dynamic toolbar updates.

For example, the Breadcrumb bare can only be activated for Java Editor, and the toolbar "Toggle Breadcrumb" button will not be visible for any other type of editors.

alt text

That is a ToggleBreadcrumbAction, declared in plugin.xml as

  <actionSet
        label="%javaEditorPresentationActionSet.label"
        visible="false"
        id="org.eclipse.jdt.ui.text.java.actionSet.presentation">
     <action
           allowLabelUpdate="true"
           style="toggle"
           toolbarPath="org.eclipse.ui.edit.text.actionSet.presentation/Presentation"
           id="org.eclipse.jdt.ui.edit.text.java.toggleMarkOccurrences"
           definitionId="org.eclipse.jdt.ui.edit.text.java.toggleMarkOccurrences"
           disabledIcon="$nl$/icons/full/dtool16/mark_occurrences.gif"
           icon="$nl$/icons/full/etool16/mark_occurrences.gif"
           helpContextId="toggle_mark_occurrences_action_context"
           label="%toggleMarkOccurrences.label"
           retarget="true"
           tooltip="%toggleMarkOccurrences.tooltip">
     </action>
     <action
           allowLabelUpdate="true"
           style="toggle"
           toolbarPath="org.eclipse.ui.edit.text.actionSet.presentation/Presentation"
           id="org.eclipse.jdt.ui.edit.text.java.toggleBreadcrumb"
           definitionId="org.eclipse.jdt.ui.edit.text.java.toggleBreadcrumb"
           disabledIcon="$nl$/icons/full/dtool16/toggle_breadcrumb.gif"
           icon="$nl$/icons/full/etool16/toggle_breadcrumb.gif"
           helpContextId="toggle_mini_browser_action_context"
           label="%toggleBreadcrumb.label"
           retarget="true"
           tooltip="%toggleBreadcrumb.tooltip">
     </action>
  </actionSet>

You can try the same kind of definition.

VonC
+1  A: 

Thank you for your response, I found a simple way to do this, simply add the following extension point if the buttons are ActionSet:

<extension
     point="org.eclipse.ui.actionSetPartAssociations">
  <actionSetPartAssociation
        targetID="myActionSetId">
     <part
           id="myEditorId">
     </part>
  </actionSetPartAssociation>

Imen