Thursday, March 1, 2007

Show View Shortcuts

Show View Shortcuts:
In Eclipse, you can click the 'Window->Show View' menu, and you see a bunch of views available for quick showing within the current perspective.

Where I started:
From my research in how to get the 'Window->Show View' menu, I knew that org.eclipse.ui.internal.ShowViewMenu was used by the IContributionItem created by the VIEWS_SHORTLIST ContributionFactory in the org.eclipse.ui.workbench plug-in. Thus, my search began there.

Inside the ShowViewMenu#fillMenu(IMenuManager) method, the Menu is populated with view ids retrieved from the active org.eclipse.ui.IWorkbenchPage. Tracing references to this method should aid us. Unfortunately, ShowViewMenu#fillMenu(IMenuManager) is the only reference. Thus, we must dig into the implementation class, org.eclipse.ui.internal.WorkbenchPage.

org.eclipse.ui.internal.WorkbenchPage#getShowViewShortcuts() retrieves its data from the active org.eclipse.ui.internal.Perspective with the org.eclipse.ui.internal.Perspective#getShowViewShortcuts() method. Inside this method, we see a java.util.ArrayList of view ids stored internally. Searching for references within the class turns up the org.eclipse.ui.internal.Perspective#loadPredefinedPesp(PerspectiveDescriptor) method. It sets the view ids based on the value given by the org.eclipse.ui.internal.PageLayout#getShowViewShortcuts() method. This method maintains its own java.util.ArrayList of view ids. Searching for its references, we find we can add view ids with the org.eclipse.ui.internal.PageLayout#addShowViewShortcut(String) method. Encouragingly, this method is also part of the org.eclipse.ui.IPageLayout public API.

Now, we have the method to call to add a view to the top-level "Show View" menu. We just need to find where it is used. Searching for references to org.eclipse.ui.IPageLayout#addShowViewShortcut(String) yields the org.eclipse.ui.internal.ide.ResourcePerspective#defineActions(IPageLayout) method in the org.eclipse.ui.ide plug-in. Again, this method is not part of the public API, but it is only used inside the org.eclipse.ui.internal.ide.ResourcePerspective#createInitialLayout(IPageLayout) method. This method implements the org.eclipse.ui.IPerspectiveFactory#createInitialLayout(IPageLayout) method, and provides a good example for how to exercise the public API.

Most Likely Public API:
To define which views should be available as shortcuts under the 'Show View' Menu, add the view ids with the org.eclipse.ui.IPageLayout#addShowViewShortcut(String) method on the org.eclipse.ui.IPageLayout that is passed to the org.eclipse.ui.IPerspectiveFactory#createInitialLayout(IPageLayout) method. The IPerspectiveFactory is responsible for defining the layout of your perspective.

No comments: