编辑器具有焦点时添加/删除操作工具栏

时间:2010-02-21 13:02:38

标签: eclipse-plugin toolbar

我正在使用Eclipse插件基础架构扩展Eclipse,我遇到了一个问题:

我创建了一个文本编辑器,当编辑器打开并具有焦点时,我想向Eclipse工具栏添加操作。例如:

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

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

});

以下示例:extensionPoint:ActionSet,将操作按钮永久添加到工具栏:

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

如何动态制作?

2 个答案:

答案 0 :(得分:2)

感谢您的回复,我找到了一种简单的方法,如果按钮是ActionSet,只需添加以下扩展点:

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

答案 1 :(得分:1)

您可以查看类似动态工具栏更新的Eclipse实现。

例如,只能为Java编辑器激活Breadcrumb bare,并且任何其他类型的编辑器都不会显示工具栏“Toggle Breadcrumb”按钮。

alt text http://img109.imageshack.us/img109/359/eclipsetoolbarupdate.png

这是ToggleBreadcrumbAction,在plugin.xml中声明为

  <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>

您可以尝试相同的定义。