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

我喜欢在我的RCP应用程序中使用Eclipse提供的导航历史记录。 不幸的是,这个function没有很好的记录 事实上我只找到了这个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 ); 

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

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

 /** * Fills the cool bar with the main toolbars for the window. * 

* The default implementation does nothing. Subclasses may override. *

* * @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 ); }

您必须在编辑器中实现INavigationLocationProvider接口。

您可以看到Eclipse组如何在其AbstractTextEditor中实现该接口。

谢谢,我已经看了一下AbstractTextEditor实现。 因为我只想在编辑器之间导航而不是在编辑器内的不同位置之间导航,所以我发现最简单的实现应该如下所示:

 public class MyEditor extends EditorPart implements INavigationLocationProvider { public static final String ID = "MyEditor"; ... @Override public INavigationLocation createEmptyNavigationLocation() { return new MyNavigationLocation( this ); } @Override public INavigationLocation createNavigationLocation() { return new MyNavigationLocation( this ); } } 

 public class MyNavigationLocation extends NavigationLocation { public MyNavigationLocation( IEditorPart part ) { super( part ); } @Override public boolean mergeInto( INavigationLocation location ) { return false; } @Override public void restoreLocation() { } @Override public void restoreState( IMemento memento ) { } @Override public void saveState( IMemento memento ) { } @Override public void update() { } } 

我的问题是它仍然无效。 我希望失败必定在其他地方。 也许我在Eclipse命令配置中遗漏了一些东西。 有任何想法吗?

编辑:

问题在于NavigationHistory类的markLocation()方法。 它调用私有方法addEntry()。 在我的情况下,私有变量ignoreEntries设置为1.这就是我无法在历史记录中标记位置的原因。 不幸的是我还没想到,为什么将ignoreEntries设置为1. Eclipse文档没有说明它: http ://help.eclipse.org/indigo/index.jsp?topic =%2Forg.eclipse.platform.doc .isv%2Freference%2Fapi%2Forg%2Feclipse%2Fui%2FINavigationHistory.html

 /* * Adds a location to the history. */ private void addEntry(IEditorPart part) { if (ignoreEntries > 0 || part == null) { return; } ... } 

第二编辑:

我发现每次打开一个新的编辑器时,都会通过NavigationHistory的markEditor()方法添加一个历史记录条目。 标记在显示线程中完成,在标记过程完成之前,您无法再添加标记。 如果要在打开编辑器后直接标记位置,则必须在显示线程中调用markLocation()。 不过我的问题仍然存在。 NavigationHistory中的后退和前进NavigationHistoryAction为null。 这就是我的UI图标保持灰色的原因。 有人可以给我发送指定导航命令的plugin.xml部分吗? 然后我可以将它与我的配置进行比较。