在我的eclipse应用程序中,如果激活了某个切换菜单,我需要添加监听器。我将它们添加到我的命令的execute方法中。我希望这个状态是持久的,所以我使用RegistryToggleState。现在我遇到了一个问题,如果在启动应用程序时启用了状态,则永远不会单击链接到我的命令的菜单。在这种情况下,我的听众将不会被添加。 我想我可以让我的插件尽早启动并简单地运行HandlerUTils.togglecommandState两次。否则我需要在启动时获取teh命令的状态,以便我可以在启用命令时自己添加侦听器。但对于两者我都需要在早期启动时通过id获取命令。我试着用以下方法做到这一点:
@Override
public void earlyStartup() {
// TODO Auto-generated method stub
System.err.println("plugin started");
ICommandService commandService = (ICommandService) PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getService(
ICommandService.class);
Command command = commandService
.getCommand("jeff.synchronization.commands.synchronizeClients");
try {
HandlerUtil.toggleCommandState(command);
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("state toggled");
}
但是现在我得到了一个Exception,因为在早期启动时找不到ActiveWorkbenchWindow。有没有办法在启动时通过id获取命令?我最糟糕的解决方案是始终通过start将状态设置为false,以便用户必须自己设置标志。
!MESSAGE Unable to execute early startup code for an extension
!STACK 0
java.lang.NullPointerException
at jeff.synchronization.Activator.earlyStartup(Activator.java:89)
at org.eclipse.ui.internal.EarlyStartupRunnable.runEarlyStartup(EarlyStartupRunnable.java:87)
at org.eclipse.ui.internal.EarlyStartupRunnable.run(EarlyStartupRunnable.java:66)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
at org.eclipse.ui.internal.Workbench$55.run(Workbench.java:2555)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:53)
18145 [Worker-5] ERROR org.eclipse.logger - PLUGIN=jeff.synchronization: Unable to execute early startup code for an extension
java.lang.NullPointerException
at jeff.synchronization.Activator.earlyStartup(Activator.java:89)
at org.eclipse.ui.internal.EarlyStartupRunnable.runEarlyStartup(EarlyStartupRunnable.java:87)
at org.eclipse.ui.internal.EarlyStartupRunnable.run(EarlyStartupRunnable.java:66)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
at org.eclipse.ui.internal.Workbench$55.run(Workbench.java:2555)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:53)
2027890 [main] INFO jeff.database.jpa.core.factory.EclipseLinkLogAdapter - logout_successful