【问题标题】:Use Navigation History in Eclipse RCP在 Eclipse RCP 中使用导航历史
【发布时间】:2012-05-12 02:52:34
【问题描述】:

我喜欢在我的 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 );

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

【问题讨论】:

    标签: java eclipse eclipse-plugin eclipse-rcp


    【解决方案1】:

    我找到了解决方案。为了在您的 Eclipse RCP 应用程序中使用 Navigation History,您必须将以下代码行添加到您的 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 );
    }
    

    【讨论】:

    • 我知道我在挖掘旧帖子,但你拯救了我的一天 :) 这也适用于 RAP!
    【解决方案2】:

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

    您可以在他们的AbstractTextEditor 中看到 Eclipse 小组是如何实现该接口的。

    【讨论】:

      【解决方案3】:

      谢谢,我已经研究了 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 为空。这就是为什么我的 UI 图标一直显示为灰色的原因。有人可以将指定导航命令的 plugin.xml 部分发送给我吗?然后我可以将它与我的配置进行比较。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-04-24
        • 2023-04-10
        • 1970-01-01
        • 2018-01-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多