RSS

Search Engine

Monday, June 14, 2010

Views

8.1. Overview

Views provide information for a given task. A view is typically used to navigate a hierarchy of information, open an editor, or display properties for the active editor.

The following will explain how to add views to your application.For this example create a new RCP project "de.vogella.rcp.intro.view". Use the "Hello RCP" as a template.

8.2. Create a view

Select the file "plugin.xml" and the tab "Extensions". Press the "add" button and add "org.eclipse.ui.views" as an extension.

Right mouse-click on your new view extension and select New -> View

Maintain the id "de.vogella.rcp.intro.view.MyView" and the class "de.vogella.rcp.intro.view.MyView".

Create the class of the view by clicking on the class hyperlink.

Maintain the following code in your new class.

    
package de.vogella.rcp.intro.view;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.ViewPart;

public class MyView extends ViewPart {

@Override
public void createPartControl(Composite parent) {
Text text = new Text(parent, SWT.BORDER);
text.setText("Imagine a fantastic user interface here");
}

@Override
public void setFocus() {
}
}

8.3. Add the view to your perspective.

After creating the view you have to add this view to your perspective. We will do this by using another extension point.

Select again "plugin.xml" and the tab "Extensions". Press add and add the extension "org.eclipse.ui.perspectiveExtensions".

Right click it and select view.

Maintain your view id "de.vogella.rcp.intro.view.MyView". Make the view relative to "org.eclipse.ui.editorss" which is the currently invisible editor area and make the view use all the space by selecting the maximum ratio of "0.95f".

8.4. Result

Run your application to see the result.

8.5. Add view to perspective via code

Alternatively to the usage of the extension point "org.eclipse.ui.perspectiveExtensions" you could have add the view also via coding to your perspective. To to this you could modify "Perspective.java" to the following.

    
package de.vogella.rcp.intro.view;

import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.IPerspectiveFactory;

public class Perspective implements IPerspectiveFactory {

public void createInitialLayout(IPageLayout layout) {
layout.addView("de.vogella.rcp.intro.view.MyView", IPageLayout.TOP,
IPageLayout.RATIO_MAX, IPageLayout.ID_EDITOR_AREA);
}
}

Tip

If possible use extension points over code.

0 comments:

Post a Comment