DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more
  1. DZone
  2. Coding
  3. Frameworks
  4. How to Create a PageBookView

How to Create a PageBookView

Prakash  user avatar by
Prakash
·
May. 15, 09 · Interview
Like (0)
Save
Tweet
Share
15.41K Views

Join the DZone community and get the full member experience.

Join For Free

Think 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");
}




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");
}

}
 From http://blog.eclipse-tips.com/
Use case Property (programming) Bootstrap (front-end framework) Workbench (AmigaOS) Template career Strings Eclipse Manifest (transportation)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Fixing Bottlenecks in Your Microservices App Flows
  • Mission-Critical Cloud Modernization: Managing Coexistence With One-Way Data Sync
  • Automated Testing With Jasmine Framework and Selenium
  • Keep Your Application Secrets Secret

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: