如何检查是否在 Eclipse 插件开发中单击了保存按钮?

How to check if save button is clicked in Eclipse Plugin development?

可能与 this question 重复,但我正在专门寻找一种方法来检查 Eclipse 中的保存按钮是否被单击,而不是编辑器是否正在保存。

您可以使用 IExecutionListener 来收听文件保存命令。类似于:

ICommandService commandSvc = PlatformUI.getWorkbench().getAdapter(ICommandService.class);
Command saveCommand = commandSvc.getCommand(IWorkbenchCommandConstants.FILE_SAVE);
saveCommand.addExecutionListener(new IExecutionListener()
  {
    @Override
    public void preExecute(final String commandId, final ExecutionEvent event)
    {
    }

    @Override
    public void postExecuteSuccess(final String commandId, final Object returnValue)
    {
    }

    @Override
    public void postExecuteFailure(final String commandId, final ExecutionException exception)
    {
    }

    @Override
    public void notHandled(final String commandId, final NotHandledException exception)
    {
    }
  });

IWorkbenchCommandConstants.FILE_SAVE 是包含文件保存命令 id 的标准常量。

ExecutionEvent 参数有一个 getTrigger 方法,该方法 returns 反对导致命令 运行。看起来这将是一个 MenuItem 如果 'File > Save' 菜单导致命令为 运行。