Previous Page
Next Page

10.1. Creating a Perspective

To create a new perspective, extend the org.eclipse.ui.perspectives extension point and then define the layout of the perspective by creating a perspective factory class implementing the IPerspectiveFactory interface (see Figure 10-1).

Figure 10-1. Perspective declaration and behavior.


10.1.1. Perspective extension point

Start by opening the Favorites plug-in manifest editor, selecting the Extensions tab, and clicking the Add button. When the New Extension wizard opens, select org.eclipse.ui.perspectives from the list of all available extension points (see Figure 10-2). Click the Finish button to add this extension to the plug-in manifest.

Figure 10-2. The New Extension wizard showing the org.eclipse.ui.perspectives extension point selected.


Now, back in the Extensions page of the plug-in manifest editor, right-click on the org.eclipse.ui.perspectives extension and select New > perspective. This immediately adds a perspective named com.quality-eclipse.favorites.perspective1 in the plug-in manifest. Clicking on this new perspective displays its properties on the right side of the editor (see Figure 10-3). Modify them as follows:

id "com.qualityeclipse.favorites.FavoritesPerspective"

The unique identifier used to reference the perspective.

name "Favorites"

The text label associated with the perspective.

class "com.qualityeclipse.favorites.perspectives.FavoritesPerspectiveFactory"

The class describing the layout of the perspective. The class is instantiated using its no argument constructor, but can be parameterized using the IExecutableExtension interface (see Section 20.5, Types Specified in an Extension Point, on page 723).

icon "icons/sample.gif"

The icon associated with the perspective.

Figure 10-3. The extension element details for the Favorites perspective.


If you switch to the plugin.xml page of the plug-in manifest editor, you will see the following new section of XML defining the new perspective:

<extension point="org.eclipse.ui.perspectives">
   <perspective
      class="com.qualityeclipse.favorites.perspectives.
             FavoritesPerspectiveFactory"
      icon="icons/sample.gif"
      id="com.qualityeclipse.favorites.FavoritesPerspective"/>
      name="Favorites"
</extension>

10.1.2. Perspective factories

When specifying the name of a perspective factory class, clicking on the Browse... button next to the class field will open a class selection editor, in which an existing class can be selected. Clicking on the class: label to the right of the class field will open a Java Attribute Editor dialog in which a new class (conforming to the IPerspectiveFactory interface) can be created (see Figure 10-4).

Figure 10-4. The Java Class Selection wizard.


The IPerspectiveFactory interface defines a single method, createInitialLayout(), which specifies the initial page layout and visible action sets for the perspective. The factory is only used to define the initial layout of the perspective and is then discarded. By default, the layout area contains space for the editors, but no views. The factory can add additional views, which are placed relative to the editor area or to another view.

Open the newly created FavoritesPerspectiveFactory class and modify it as follows so that the Favorites view will appear below the editor area and the standard Outline view will be shown to its left.

package com.qualityeclipse.favorites.perspectives;

import org.eclipse.ui.*;

public class FavoritesPerspectiveFactory
   implements IPerspectiveFactory
{
   private static final String FAVORITES_VIEW_ID =
      "com.qualityeclipse.favorites.views.FavoritesView";
   private static final String FAVORITES_ACTION_ID =
      "com.qualityeclipse.favorites.workbenchActionSet";

   public void createInitialLayout(IPageLayout layout) {
      // Get the editor area.
      String editorArea = layout.getEditorArea();

      // Put the Outline view on the left.
      layout.addView(
         IPageLayout.ID_OUTLINE,
         IPageLayout.LEFT,
         0.25f,
         editorArea);

      // Put the Favorites view on the bottom with
      // the Tasks view.
      IFolderLayout bottom =
         layout.createFolder(
            "bottom",
            IPageLayout.BOTTOM,
            0.66f,
            editorArea);
      bottom.addView(FAVORITES_VIEW_ID);
      bottom.addView(IPageLayout.ID_TASK_LIST);
      bottom.addPlaceholder(IPageLayout.ID_PROBLEM_VIEW);

      // Add the Favorites action set.
      layout.addActionSet(FAVORITES_ACTION_ID);
   }
}

Within the createInitialLayout() method, the addView() method is used to add the standard Outline view to the left of the editor area such that it takes up 25 percent of the horizontal area within the window. Using the createFolder() method, a folder layout is created to occupy the bottom third of the layout below the editor area. The Favorites view and standard Tasks view are added to the folder layout so that each will appear stacked with a tab inside the folder.

Next, a placeholder for the standard Problems view is added to the folder. If a user opened the Problems view, it would open in the location specified by the placeholder. Finally, the Favorites action set is made visible by default within the perspective.

When opened, the new Favorites perspective will look something like what's shown Figure 10-5.

Figure 10-5. The Favorites perspective.


10.1.3. IPageLayout

As seen in the previous section, the IPageLayout interface defines the protocol necessary to support just about any possible perspective layout. It defines many useful APIs, including the following:

addActionSet(String) Adds an action set with the given ID to the page layout.

addFastView(String) Adds the view with the given ID to the page layout as a fast view.

addFastView(String, float) Adds the view with the given ID to the page layout as a fast view with the given width ratio.

addNewWizardShortcut(String) Adds a creation wizard to the File New menu.

addPerspectiveShortcut(String) Adds a perspective shortcut to the Perspective menu.

addPlaceholder(String, int, float, String) Adds a placeholder for a view with the given ID to the page layout.

addShowInPart(String) Adds an item to the Show In prompter.

addShowViewShortcut(String) Adds a view to the Show View menu.

addStandaloneView(String, boolean, int, float, String) Adds a stand-alone view with the given ID to this page layout. A stand-alone view cannot be docked together with other views.

addView(String, int, float, String) Adds a view with the given ID to the page layout.

createFolder(String, int, float, String) Creates and adds a folder with the given ID to the page layout.

createPlaceholderFolder(String, int, float, String) Creates and adds a placeholder for a new folder with the given ID to the page layout.

getEditorArea() Returns the special ID for the editor area in the page layout.

getViewLayout(String) Returns the layout for the view or placeholder with the given compound ID in this page layout.

setEditorAreaVisible(boolean) Shows or hides the editor area for the page layout.

setFixed(boolean) Sets whether this layout is fixed. In a fixed layout, layout parts cannot be moved or zoomed, and the initial set of views cannot be closed.


Previous Page
Next Page