gpt4 book ai didi

java - 即时更改 Eclipse 标题中按钮上的文本

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:20:21 26 4
gpt4 key购买 nike

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

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

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

我也愿意以任何其他方式在基于 Eclipse 构建的应用程序中实现这一点,只要文本发生变化并单击文本打开一个 View 即可。

最佳答案

我相信标题使用图标,不确定是否可以使用文本。您可以在上下文(弹出)菜单中添加一个条目,该菜单将显示迹线数量,并在单击该项目时启动您的 View 。

我基于 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();
}
}

现在我们需要将其连接到将启动您的 View 的命令和处理程序中。

这是我的 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>

关于java - 即时更改 Eclipse 标题中按钮上的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1035416/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com