Columns

Extending Eclipse -- Part 2

In my April column, I talked about extending Eclipse -- "an IDE for anything and for nothing in particular" -- by describing how to add a menu button to its Workbench toolbar. I will now describe how easy it is to add a new view and how to create your own perspective.

An Eclipse view is a visual component within the Workbench. Examples of views include editors, navigators, and other methods of presentation or ways to navigate information in your Workbench. Views have their own menus and toolbars. The actions represented by buttons on a view’s toolbar affect only the items within that view. A view can appear by itself, or it can be stacked with other views in a tabbed notebook.

To add a view to Eclipse, one needs to perform the following:

* Create or update a plugin.xml file with the view’s extension point information;
* Create an icon for the view; or
* Create the corresponding Java class for the view.

Every Eclipse plug-in has a corresponding plugin.xml file. In my April column, we created a new plug-in and corresponding plugin.xml file to add a menu. To add a new view, we can simply update that plugin.xml file with the following:


<>
id=”carleton.examples.
helloworld.ui.labelview”
name=”Label View”
class=”carleton.examples.helloworld.ui.LabelView”
icon=”icons\view.gif”/>

The id identifies the name you will use to refer to the view in your code. Eclipse maintains a list of associations mapping ids to class names. When you want to create a new instance of the view, you don’t execute MyClassView new(). Rather, you get Eclipse to do it for you by passing the id (we’ll discuss this in another column). The name identifies what text will appear across the top of the view when it opens. The icon identifies the directory and file containing the icon for the view. Finally, the class identifies the name of the view’s Java class. By choosing the extension point “org.eclipse.ui.views,” I am indicating that this view will be one that is selectable from the Window-> Show Views menu and that can be opened in any perspective. How do I know this? Look in your Eclipse install directory for the file ..\eclipse\plugins\org.eclipse.ui_2.1.0\ doc\org_eclipse_ui.html, which describes the different extension points available to you and the information each requires.

The Java class for the simple LabelView is:

packagecarleton.examples.helloworld.ui;

import org.eclipse.swt.widgets.*;
import org.eclipse.ui.part.ViewPart;

public class LabelView extends ViewPart {
private Label label;
public LabelView() {
super(); }
public void setFocus() {
label.setFocus();}
public void createPartControl
(Composite parent) {
label = new Label(parent, 0);
label.setText(“Hello XML Web Services One”);}
}

The important part of the class definition is that the class extends ViewPart and implements the default constructor, the setFocus and the createPartControl methods. The createPartControl method is where you can add all of the other widgets that make sense for your view. Beyond that, the rest of the view’s control behavior is up to you. Therefore, if you are building more than just a LabelView, use all the widgets you can find and connect them together in method createPartControl.

Each Workbench window contains one or more perspectives. A perspective defines the initial set and layout of views in the Workbench. The idea behind a perspective is that it provides a set of capabilities to accomplish a specific task. For example, Eclipse’s Java perspective provides a set of views to edit Java source files. Perspectives also control what appears in menus and toolbars. They define visible action sets that you can change to customize a perspective.

The steps for adding a perspective are similar to those for adding a view:

* Create or update a plugin.xml file with the perspective’s extension point information;
* Create an icon for the perspective; and
* Create the corresponding class for the perspective.

As before, to add a perspective, we update or create the plugin.xml file with the following:


<>
name=”XMLOne Test Perspective”
class= “carleton.examples.helloworld.ui.TestPerspective”
id=”carleton.examples.helloworld.ui.TestPerspective”
icon=”icons\101view.gif”>


The id identifies the name you use to refer to the perspective in your code. The icon identifies the directory and file containing the icon for the perspective. The name identifies the string that will be used in the menu to select the perspective. Finally, the class identifies the name of the perspective’s Java class. By choosing the extension point “org.eclipse.ui.perspectives,” we are indicating that this perspective will be one that is selectable from the Window-> Show Perspectives menu.

The Java class for the TestPerspective is:

public class TestPerspective implements
IPerspectiveFactory {

private static String ICON_FOLDER_NAME = “icons/”;

public void createInitialLayout
(IPageLayout layout) {
defineActions(layout);
defineLayout(layout);}

public void defineLayout(IpageLayout
layout) { IFolderLayout left, middle;
String editorArea = layout.getEditorArea();
left = layout.createFolder(“left”,IPageLayout.LEFT, 0.25f, editorArea);
left.addView(IPageLayout.ID_RES_NAV);
middle = layout.createFolder(“middle”, IPageLayout.LEFT, 0.25f, editorArea); middle.addView(“carleton.examples.helloworld.ui.TextEditor”);}

public void defineActions(IPageLayout layout) { /* Adds a view to the Show View menu. The id must name a view extension contributed to the workbench’s extension point */
layout.addShowViewShortcut (“carleton.
examples.helloworld.ui.TextEditor”);} }

The important part of the class file is that the class extends IPerspectiveFactory and implements the method createInitialLayout. You use this method to define the views that will open when the perspective is selected and where. In the above code, I asked for two views to open up on the left and in the middle of the window. Once you have established the location of the views, you must refer to the view you want by its id, such as IPageLayout.ID_RES_NAV and“carleton.examples.helloworld.ui.TextEditor,” and not by its class name.

Changing the Workbench is easy. Add a menu, view or perspective by creating a plug-in and creating your plugin.xml file with your extension points duly noted. The hard part is thinking about what you want to integrate with Eclipse. I suggest you think outside of the box. You can always build a bigger, better IDE, but why not use Eclipse as a starting point? You will be glad you did.

About the Author

Dwight Deugo is a professor of computer science at Carleton University in Ottawa, Ontario. Dwight has been an editor for SIGS and 101communications publications, and serves as chair of the Java Programming track at the SIGS Conference for Java Development. He can be reached via e-mail at [email protected].