First lets create a View:
<extension
point="org.eclipse.ui.views">
<view
class="com.eclipse_tips.views.SelectionView"
icon="icons/sample.gif"
id="com.eclipse-tips.views.pagebookview"
name="Selection Provider View">
</view>
</extension>
The contents of the PageBookView is arranged in pages (IPage). There can be multiple pages, each of them is associated with a corresponding IWorkbenchPart. When the associated part becomes active, the PageBookView automatically switches to the respective page. When a PageBookView is created, it asks for a bootstrap part by calling getBootstrapPart() method. When no bootstrap part is found, it uses a default page. That default page is also shown when there are no pages for the currently active part. Lets start by creating this default page. To do that we need to implement the createDefaultPage() method, which returns the default page. Lets use the MessagePage which simply displays a string.
@Override
protected IPage createDefaultPage(PageBook book) {
MessagePage messagePage = new MessagePage();
initPage(messagePage);
messagePage.setMessage("No interested in this part");
messagePage.createControl(book);
return messagePage;
} Now our SelectionView is up and running, except that its showing a static content not creating any IPages for the active parts. Before we create an IPage, how to determine whether to create an IPage for a given IWorkbenchPart or ignore it? Its by the isImportant() method. Lets say, we want to respond only to the parts that are contributed by the Platform UI.
@Override
protected boolean isImportant(IWorkbenchPart part) {
return part.getSite().getPluginId().startsWith("org.eclipse.ui");
}
So if a Package Explorer(contributed by JDT) or the Manifest Editor(contributed by PDE) is the active part, then our SelectionView will not create any page and use the default page. If its a TextEditor or Project Explorer, then it will create the page and show it.
To create an IPage for a given part, we need to override the doCreatePage() method. Unlike the createDefaultPage() method this doesn't return a IPage, rather a PageRec. Why? This Page Record stores additional information - the associated workbench part and action bars:
@Override
protected PageRec doCreatePage(IWorkbenchPart part) {
MessagePage messagePage = new MessagePage();
initPage(messagePage);
messagePage.setMessage("Page for "+part.getTitle());
messagePage.createControl(getPageBook());
return new PageRec(part, messagePage);
}
When you are switching between the TextEditor and the ProjectExplore view, the PageBookView will track and find the page for the active part and automatically show it. When the page is no longer needed (the associated part is closed), it would call the doDestroyPage(). This is where you would ideally remove any listeners and dispose of the resources.
So the whole class goes here:
package com.eclipse_tips.views;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.part.IPage;
import org.eclipse.ui.part.MessagePage;
import org.eclipse.ui.part.PageBook;
import org.eclipse.ui.part.PageBookView;
/**
* @author Prakash G.R.
*
*/
public class ActivePartTrackerView extends PageBookView {
@Override
protected IPage createDefaultPage(PageBook book) {
MessagePage messagePage = new MessagePage();
initPage(messagePage);
messagePage.setMessage("No interested in this part");
messagePage.createControl(book);
return messagePage;
}
@Override
protected PageRec doCreatePage(IWorkbenchPart part) {
MessagePage messagePage = new MessagePage();
initPage(messagePage);
messagePage.setMessage("Page for "+part.getTitle());
messagePage.createControl(getPageBook());
return new PageRec(part, messagePage);
}
@Override
protected void doDestroyPage(IWorkbenchPart part, PageRec pageRecord) {
pageRecord.page.dispose();
}
@Override
protected IWorkbenchPart getBootstrapPart() {
IWorkbenchPage page = getSite().getPage();
if(page != null) {
// check whether the active part is important to us
IWorkbenchPart activePart = page.getActivePart();
return isImportant(activePart)?activePart:null;
}
return null;
}
@Override
protected boolean isImportant(IWorkbenchPart part) {
return part.getSite().getPluginId().startsWith("org.eclipse.ui");
}
}



1 comments: