Thursday, February 17, 2011

Defining a new perspective/extend existing / or instantiate a new perspective at runtime within the workbench.

Reference Link : http://www.eclipse.org/articles/using-perspectives/PerspectiveArticle.html

Introduction:

A perspective is a visual container for a set of views and editors (parts). Each perspective has an input and a type. The input attribute is used to define which resources are visible in the workspace and the type attribute is used to define which actions and views are visible in the user interface.

The root of the user interface is accessed by invoking PlatformUI.getWorkbench( ). returns an object of type IWorkbench. A workbench has one or more windows of type IWorkbenchWindow. And each window has a collection of pages of type IWorkbenchPage.In the user interface a page is known as a "perspective". Within each window there is at most one active and visible page.

So,
PlatformUI.getWorkbench( )
|
IWorkbench
|
IWorkbenchWindow
|
IWorkbenchPage (also know as perspective)

A page can also be created programmatically using public API on IWorkbenchWindow. For instance, the following code demonstrates the creation of a new page.

>>Opening a Resource perspective programatically
// fWindow is an IWorkbenchWindow.
fWindow.openPage("org.eclipse.ui.resourcePerspective", ResourcesPlugin.getWorkspace());
here,
input = resource perspective( ie..org.eclipse.ui.resourcePerspective)
type = may be any object of type IAdaptable. In this case the input is the entire workspace.

>> Given a page, you can get the input and perspective type by using the following methods.
public IAdaptable getInput();
public IPerspectiveDescriptor getPerspective();

>> To retrieve Perspective info on(id, label, icon) :
The getPerspective method on IWorkbenchPage returns an object of type IPerspectiveDescriptor. This object has accessor methods for the perspective id, label and icon.

---------------------
Adding a New Perspective

____________________________

The definition of a new perspective is a three step process.

  1. Create a plug-in.
  2. Add a perspective extension to the plugin.xml file.
  3. Define a perspective class for the extension within the plug-in.
Consider teh creation of a new perspective with the below layotu:

so, the first step :

1. create a plug-in

2. Add a perspective to your plug-in through the extensions tab of the plug-in.xml as below


            name="Test"
class="org.eclipse.ui.articles.perspective.TestPerspective"
id="org.eclipse.ui.articles.perspective.Test">

3. Define perspective class by clicking at the 'class' hypertext' in the extensions tab

The createInitialLayout method is called on the IPerspectiveFactory. This method defines the initial layout for a page. Implementors may add views, folders, actions and action sets to the page layout.

In the example below you can see how createInitialLayout is implemented in the TestPerspective class. For clarity the algorithm has been split into two parts which define

3.(i)the actions - defineActions : This method adds a number of items and action sets to the window.

3.(ii)layout: - defineLayout.

public void createInitialLayout(IPageLayout layout) {
defineActions(layout);
defineLayout(layout);
}
----
>> add items to the File > New, Show View, or Perspective > Open menus of the window. You can also add complete action sets to the menu or toolbar of the window. In this example a few File > New and Show View items are added.
public void defineActions(IPageLayout layout) {
// Add "new wizards".
layout.addNewWizardShortcut("org.eclipse.ui.wizards.new.folder");
layout.addNewWizardShortcut("org.eclipse.ui.wizards.new.file");

// Add "show views".
layout.addShowViewShortcut(IPageLayout.ID_RES_NAV);
layout.addShowViewShortcut(IPageLayout.ID_BOOKMARKS);
layout.addShowViewShortcut(IPageLayout.ID_OUTLINE);
layout.addShowViewShortcut(IPageLayout.ID_PROP_SHEET);
layout.addShowViewShortcut(IPageLayout.ID_TASK_LIST);
}
>>add views to the layout. When createInitialLayout is called the page layout consists of an editor area with no additional views. Additional views are added using the editor area as the initial point of reference. In defineLayout the factory gets the id of the editor area. Then it creates a folder to the left of this area and adds the Navigator view and Outline view to this folder.

public void defineLayout(IPageLayout layout) {
// Editors are placed for free.
String editorArea = layout.getEditorArea();

// Place navigator and outline to left of
// editor area.
IFolderLayout left =
layout.createFolder("left", IPageLayout.LEFT, (float) 0.26, editorArea);
left.addView(IPageLayout.ID_RES_NAV); -- deprecated
left.addView(IPageLayout.ID_OUTLINE); - adds the default eclipse views to your perspective
}

createFolder - adds a new folder to the page layout.
The positioning constants are defined on IPageLayout, and include TOP, BOTTOM, LEFT and RIGHT.

------------------------

>>you can invoke IPageLayout.setEditorAreaVisible(boolean) to hide or show the editor area.




No comments:

Post a Comment