Tutorial: an Application for showing SWT Examples

In this tutorial we will create an application in order to test the features of the Standard Widget Tootlkit (SWT) library. The figure below shows what the result will look like.

INTRODUCTION

First we will create a minimal RCP application, using the Plug-in Project creation Eclipse Wizard. The next step will consist into adding a third-party view (SWT Examples, from the Eclipse download site) to our minimal RCP application just created. The result will be an application that will allow us to test deeply all of the SWT library features.

The main goal of this tutorial is show how easy is integrate a third-party view into a brand-new RCP application, without having to write a single line of code, or, to be honest, writing just one line.

STEP 1: Create application

First we have to setup the environment, downloading the Eclipse version RCP/Plug-in Developers from http://www.eclipse.org/downloads/ (see figure below).

This version contains all we need in order to develop Rich Client application or Eclipse Plug-ins.Then we need to download the SWT Examples from Eclipse site. If you downloaded Eclipse 3.4.1 as described above you can go directly here. This file has to be extracted into the Eclipse installation parent folder (e.g. if your Eclipse installation folder is C:\eclipse, then extract the file into C:\).

To verify the success of this operation restart Eclipse and select the menu item Window->Show View, select other. In the available views list you should find the following (see figure).Now we know we have all we need in the right place.

The next step is to create a new Plug-in Project. To do this, follow the steps showed in the figures below. From menu File select New then Plug-in ProjectAs Plug-in name enter “Test SWT Samples”.In the next wizard step ensure to select Yes in the option “Would you like to create a rich client application?”.In the next wizard step select the “Hello RCP” template and press Finish.

Now will be asked you to switch to the Plug-in development perspective, select Yes.

You should now see the project MANIFEST.MF editor. It contains mainly the information about the dependencies from other projects and the exporting options.

From here you can manage the project and launch it. Just click “Launch” as showed below.The restult should be this:

STEP 2: Adding a third-party view

Now we want to open one of the views of the SWT Examples from our test application.

In order to do this our Plug-in needs a dependence from the SWT Examples one: open the MANIFEST.MF editor page and select the tab “Dependencies” in the lower part of the editor.

In the upper-left area of the tab there are all the dependencies from other projects (the figure shows the two default dependencies). Click the Add button and enter org.eclipse.swt, now the filter will allow you to select the plugin org.eclipse.swt.examples.controls which contains the view we want to show. Press OK.As a result you should see the plugin just selected into the Dependencies list.

Now we want our perspective to open the SWT Examples view. To do this switch to Extension tab and press the button Add. In the pop-up enter the text “*per” into the filter in order to select the extension org.eclipse.ui.perspectiveExtensions.On the item just created, do a right-click, New->View in order to insert a reference to a view.On the right side of the editor you should see the detail of the properties of the extension just created, where we have to provide the ID of the view we want to open. Enter org.eclipse.swt.examples.controls.view.In the field relative enter “org.eclipse.ui.editorss”; this is needed in order to choose in which perspective area the view has to be shown (in this case we want to show it in the editor area, the central one).

In the end we need to write some source code (just one single line to be put manually!) in order to prevent the perspective to open the default editor.

Open the class Perspective.java, into which the only implemented method is createInitialLayout(…) . Into this method let’s put the following instruction:

[java]
public class Perspective implements IPerspectiveFactory {
public void createInitialLayout(IPageLayout layout) {
layout.setEditorAreaVisible(false);
}
}
[/java]

As you can see below, we just added a third-party view to our application, with one line of code!

After having saved (CTRL-S), just re-launch the application in order to see the view that will allow us to test nearly all the feauters of the SWT library.

Bookmark the permalink.

One Response to Tutorial: an Application for showing SWT Examples

  1. abhijit chakra says:

    Nice tutorial ,give more example on views.it will better .

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>