How to Create a PageBookView
Join the DZone community and get the full member experience.
Join For FreeThink of Properties View. It displays the properties of the selected
element in the active part. Whenever the selection changes or the
active part changes, it tracks them and displays the properties (unless
you used the 'Pin to selection' feature available from 3.5)
There are many views like this, which update themselves when the active
part changes. Outline view, Templates view, GEF Palette view, etc. If
you want to create such view, its not a tough job - Eclipse provides
you all the basic features in the class PageBookView. All you need is
to extend this class and fill the void by implementing the abstract
methods. Thats what we are going to see in this tip. The use case is to
create a ActivePartTrackerView, which will display the name of the
current active workbench part.
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");
}
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;From http://blog.eclipse-tips.com/
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");
}
}
Opinions expressed by DZone contributors are their own.
Comments