在Eclipse RCP中使用导航历史记录

Tob*_*lig 5 java eclipse eclipse-plugin eclipse-rcp

我喜欢在我的RCP应用程序中使用Eclipse提供的导航历史记录.不幸的是,这个功能没有很好的记录 事实上我只找到了这个Wiki条目:http://wiki.eclipse.org/FAQ_How_do_I_hook_my_editor_to_the_Back_and_Forward_buttons%3F

它提到可以在导航历史记录中标记每个编辑器,而无需指定位置.这正是我想要的.

无论特定编辑器是否支持导航历史记录,markLocation都可以使用.如果编辑器未实现INavigationLocationProvider,则将添加历史记录条目,允许用户跳回该编辑器但不返回任何特定位置.

我在应用程序中添加了以下代码行,以便每次打开新的编辑器时添加导航条目.

IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IEditorPart editor = page.openEditor( input, MyEditor.ID );
page.getNavigationHistory().markLocation( editor );
Run Code Online (Sandbox Code Playgroud)

我的问题是代码不起作用.这些命令的工具栏图标org.eclipse.ui.navigate.backwardHistoryorg.eclipse.ui.navigate.forwardHistory保持变灰.

Tob*_*lig 3

我已经找到了解决方案。为了在 Eclipse RCP 应用程序中使用导航历史记录,您必须将以下代码行添加到您的ApplicationActionBarAdvisor.

/**
 * Fills the cool bar with the main toolbars for the window.
 * <p>
 * The default implementation does nothing. Subclasses may override.
 * </p>
 * 
 * @param coolBar
 *            the cool bar manager
 */
protected void fillCoolBar( ICoolBarManager coolBar ) {
    IToolBarManager navigation = new ToolBarManager( SWT.FLAT );

    IAction backward = getAction( IWorkbenchCommandConstants.NAVIGATE_BACKWARD_HISTORY );
    IAction forward = getAction( IWorkbenchCommandConstants.NAVIGATE_FORWARD_HISTORY );

    navigation.add( backward );
    navigation.add( forward );

    coolBar.add( navigation );
}

/**
 * Instantiates the actions used in the fill methods. Use
 * {@link #register(IAction)} to register the action with the key binding
 * service and add it to the list of actions to be disposed when the window
 * is closed.
 * 
 * @param window
 *            the window containing the action bars
 */
protected void makeActions( IWorkbenchWindow window ) {
    IAction backward = ActionFactory.BACKWARD_HISTORY.create( window );
    backward.setId( IWorkbenchCommandConstants.NAVIGATE_BACKWARD_HISTORY );
    IAction forward = ActionFactory.FORWARD_HISTORY.create( window );
    forward.setId( IWorkbenchCommandConstants.NAVIGATE_FORWARD_HISTORY );

    register( backward );
    register( forward );
}
Run Code Online (Sandbox Code Playgroud)