10.1. Creating a PerspectiveTo 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 pointStart 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:
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 factoriesWhen 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. IPageLayoutAs 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:
|