Sunday, July 8, 2007

Adding Go Into and Go Up to CNF view

I have always liked the Go Into and Up features of the navigation view in the Eclipse IDE. Here is a simple way of adding it to your CNF (Common Navigator Framework) view. I'm assuming you are familiar with CNF, if not check out Common Navigator Framework.
You will need to add the Go Into action to your CNF view toolbar and the Up feature to the CNF view actions.
public class GoIntoAction extends Action {
    private IWorkbenchPage page;
    private CommonViewer commonViewer;

    public GoIntoAction(IWorkbenchPage p, ISelectionProvider selectionProvider) {
        page = p;
        commonViewer = (CommonViewer) selectionProvider;
    }

    @SuppressWarnings("unchecked")
    protected List getSelectedResources() {
        ISelection selection = commonViewer.getSelection();
        List resources = null;
        if (selection.isEmpty()) {
            resources = new ArrayList();
            resources.add(ResourcesPlugin.getWorkspace().getRoot());
        } else {
            resources = ((IStructuredSelection) selection).toList();
        }
        return resources;
    }

    @Override
    @SuppressWarnings("unchecked")
    public boolean isEnabled() {
        List resources = getSelectedResources();
        return (resources.size() == 1) && (resources.get(0) instanceof IContainer);
    }

    @Override
    @SuppressWarnings("unchecked")
    public void run() {
        ((ProjectExplorerView)page.getActivePart()).setInput(getSelectedResources().get(0));
    }
}
The GoIntoAction is added to the CNF view actionProvider extension. See here for Michael Elder's article on defining the viewer.
public class GoUpAction extends Action implements IViewActionDelegate {
    private ProjectExplorerView view;
    private CommonViewer commonViewer;

    public void init(IViewPart aView) {
        view = (ProjectExplorerView) aView;
        commonViewer = ((ProjectExplorerView) aView).getCommonViewer();
    }

    @SuppressWarnings("unchecked")
    @Override
    public boolean isEnabled() {
        return !(commonViewer.getInput() instanceof IWorkspaceRoot);
    }

    public void run(IAction action) {
        view.setInput(((IResource) commonViewer.getInput()).getParent());
        action.setEnabled(isEnabled());
    }

    public void selectionChanged(IAction action, ISelection aSelection) {
        action.setEnabled(isEnabled());
    }
}
Now add the GoUpAction to the CNF view toolbar.
<extension point="org.eclipse.ui.viewActions">
    <viewContribution
        id="com.kelman.navigator.ui.view.projectexplorercontribution"
        targetID="com.kelman.navigator.ui.view.ProjectExplorer">
       <action
            class="com.kelman.navigator.ui.actions.GoUpAction"
            icon="icons/elcl16/up_nav.gif"
            id="com.kelman.navigator.ui.actions.goupaction"
            label="%goUp.label"
            style="push"
            toolbarPath="Normal/additions"
            tooltip="%goUp.tooltip">
       </action>
    </viewContribution>
</extension>
I extracted the icons from the org.eclipse.ui.ide_3.*.jar file and dropped them into my plugin. You will need the following method in your CommonNavigator implementation.
    public void setInput(Object aObject) {
        IResource resource = (IResource) aObject;
        getCommonViewer().setInput(resource);
        setContentDescription(resource.getName());
    };

No comments: