Opening a View
The following code will open a view. For this to work, there are two prerequisites:
1) In the plugin.xml the view should allow multiple views.
2) The Perspective must define the placeholder where the views should open.
String id = "1";
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
try {
page.showView(CompanyView.ID, id, IWorkbenchPage.VIEW_ACTIVATE);
}
catch (PartInitException e) {
e.printStackTrace();
}
The id String in the code above is the secondary id that will get passed to the view itself when it is created. This secondary ID is usually a database primary key or an EJB identity property. Eclipse will only open one CompanyView for any given secondary ID. If there is an open view for a given secondary ID and you attempt to re-open the view (by triggering the execution of the code shown above), Eclipse will simply set the focus on the existing open view without reopening. The view will not refresh.
From within the view, somewhere in the createPart method, you should make a call to a method that will run the initialization sequence that is dependent upon the secondary ID.
The following code shows how to access the secondary ID from within the view:
private void initCompany() {
String companyId = this.getViewSite().getSecondaryId(); // Access secondary ID
int id = Integer.parseInt(companyId);
CompanyOrgService service = CompanyOrgService.getInstance();
Company company = service.findCompany(id);
if (company != null) {
// Do something useful with the company object
this.setCompany(company);
this.setPartName(this.company.getShortName());
detailGroup.setOrg(company);
locationGroup.setLocationMap(company.getLocationMap());
idGroup.setIdMap(company.getIdMap());
subOrgGroup.setOrgSet(company.getSubOrgSet());
}
}
Accessing a View before the dispose() method gets called
An IPerspectiveListener2 can be used to detect when a View is about to be closed. The listener should be removed from within the View’s dispose() method.
getSite().getWorkbenchWindow().addPerspectiveListener(new IPerspectiveListener2() { ... });
To close a View programatically
You can close a view programatically by hiding the view, which is equivallent to closing it.
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
page.hideView(viewRef);
Disable view close
The [x] close button for a view is enabled or disabled through the IViewLayout. You access this layout through the PageLayout in your Perspective class (which implements IPerspectiveLayout). Once you have access to the IViewLayout, you can call the setCloseable() method:
public class MyPerspective implements IPerspectiveFactory {
public final static String ID = "com.domain.client.perspective.MyPerspective";/**
* Creates the initial layout for a page.
*/
public void createInitialLayout(IPageLayout layout) {
...
IFolderLayout leftFolder = layout.createFolder("leftFolder", IPageLayout.BOTTOM, 0.2f,
IdentifierView.ID);
leftFolder.addView(DoNotCloseView.ID);
layout.getViewLayout(DoNotCloseView.ID).setCloseable(false);
...
}