Use navigation history in Eclipse RCP

I like to use the navigation history provided by Eclipse in my RCP application. Unfortunately, this feature is poorly documented. In fact, I found this entry on the wiki: http://wiki.eclipse.org/FAQ_How_do_I_hook_my_editor_to_the_Back_and_Forward_buttons%3F

It mentions that each editor can be flagged in the navigation history without indicating a location. This is exactly what I want.

Regardless of whether a particular editor supports navigation history, markLocation will work. If the editor does not implement INavigationLocationProvider, a history record will be added, allowing the user to return to this editor, but without returning to any particular place.

I added the following lines of code to my application to add a navigation entry each time a new editor opens.

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

My problem is that the code is not working. The toolbar icons for commands org.eclipse.ui.navigate.backwardHistoryand org.eclipse.ui.navigate.forwardHistoryremain grayed out.

+5
source share
3 answers

I have found a solution. To use navigation history in your RCP Eclipse application, you must add the following lines of code to yours 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 );
}
+3
source

You need to implement the InavigationLocationProvider interface in your editor.

You can see how the Eclipse group implemented the interface in AbstractTextEditor .

+1
source

, 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. ?

Edit:

markLocation() NavigationHistory. 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;
    }

    ...
}

:

, , , markEditor() NavigationHistory. , , . , markLocation() . . NavigationHistoryAction NavigationHistory . . - plugin.xml, ? .

0

All Articles