在eclipse标题中即时更改按钮上的文本

时间:2009-06-23 21:42:30

标签: java eclipse eclipse-plugin swt

我在eclipse中创建一个视图来显示堆栈跟踪,我希望可以通过标题中的按钮访问该视图。所以我使用plugin.xml框架创建扩展,基本上就是这样:

<extension
    point="org.eclipse.ui.commands">
  <command
        categoryId="com.commands.category"
        id="commands.tracing"
        name="0 Traces">
  </command>
 </extension>

这将创建一个“0 trace”作为文本的按钮。但是,我希望使用我在侦听器中收集的每条跟踪来增加按钮中的数字。基本上我只需要能够在Java文件中而不是在plugin.xml文件中编辑标题中的按钮。但我不知道该怎么做!我查看了eclipse SDK帮助,但没有找到任何东西。请帮忙!

我也愿意采用任何其他方式在eclipse上构建的应用程序中实现这一点,只要文本发生更改并单击文本即可打开视图。

1 个答案:

答案 0 :(得分:1)

我认为标题使用图标,不确定是否可以使用文字。您可以在上下文(弹出)菜单中添加一个条目,该菜单将显示跟踪数量,单击该项目时会启动您的视图。

我的基础是Eclipse附带的Hello,World Command模板,并添加了一个菜单贡献。

我将CompoundContributionItem扩展为菜单贡献,如下所示。


/**
 * Menu contribution which will update it's label to show the number of current
 * traces.
 */
public class TraceWindowContributionItem extends CompoundContributionItem {
    private static int demoTraceCount = 0;
    /**
     * Implemented to create a label which displays the number of traces.
     */
    @Override
    protected IContributionItem[] getContributionItems() {

        //Here is your dynamic label.
        final String label = getTraceCount() + " traces";

        //Create other items needed for creation of contribution.
        final String id = "test.dynamicContribution";
        final IServiceLocator serviceLocator = findServiceLocator();
        final String commandId = "test.commands.sampleCommand";
        final Map parameters = new HashMap();

        //Here is the contribution which will be displayed in the menu.
        final CommandContributionItem contribution = new CommandContributionItem(
                serviceLocator, id, commandId, parameters, null, null, null,
                label, null, null, SWT.NONE);

        return new IContributionItem[] { contribution };
    }

    /**
     * Demo method simply increments by 1 each time the popup is shown. Your
     * code would store or query for the actual trace count each time.
     * @return
     */
    private int getTraceCount() {
        return demoTraceCount++;
    }

    /**
     * Find the service locator to give to the contribution.
     */
    private IServiceLocator findServiceLocator() {
        return PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    }
}

现在我们需要将它连接到将启动视图的命令和处理程序。

这是我的plugin.xml。


<!-- Wire up our dynamic menu contribution which will show the number of traces -->
<extension
     point="org.eclipse.ui.menus">
  <menuContribution
        locationURI="popup:org.eclipse.ui.popup.any?after=additions">
     <dynamic
           class="test.TraceWindowContributionItem"
           id="test.dynamicContribution">
     </dynamic>
  </menuContribution>
</extension>
<!-- Command and handler for launching the traces view -->
<extension
     point="org.eclipse.ui.commands">
  <category
        name="Sample Category"
        id="test.commands.category">
  </category>
  <command
        categoryId="test.commands.category"
        defaultHandler="test.handlers.SampleHandler"
        id="test.commands.sampleCommand"
        name="Sample Command">
  </command>
</extension>
<extension
     point="org.eclipse.ui.handlers">
  <handler
        commandId="test.commands.sampleCommand"
        class="test.handlers.SampleHandler">
  </handler>
</extension>