Writing your first view
The ViewPart Abstract Class
ViewPart (part of the org.karora.moomba.ui.part package) is a very simple class to work with. If you are familiar at all with the Eclipse APIs then you will find this, and most of the workbench APIs follow much the same format. This class consists of the following methods:
public interface IViewPart { public void init(IViewSite site); public IWorkbenchPartSite getSite(); public String getId(); public String getPartName(); public void createPartControl(Component parent); }
init - This is called by the workbench when the ViewPart is first created.
getSite - This returns the ViewSite associated with this ViewPart. A ViewSite is a simple service which supports the operation of the ViewPart.
getId - Must return a unique Id for this view part. Note: IT MUST BE UNIQUE. The Xbio standard for this is the fully qualified class name, i.e. my.package.MyClass. It is also highly recommended that each Workbench Part declares a public final static string variable called ID which defines this unique id.
getPartName - Should return a name for the part. This does not need to be unique, but should be a user readable name as it may appear in a title bar for the view part.
createPartControl - Is the key method. When you implement this method you will add all your widgets/UI components and such on to the Echo2 component passed in. This method is called by the workbench and should not be called by the client.
Writing a hello world part
So lets create a new class, you'll notice the archetype created some hello world classes for you earlier, however these won't run out of the box without some configuration (hah!).
- Create a new class that extends ViewPart. Lets call it "HelloBigWorldPart"
- Complete the methods getId() and getPartName()
In createPartControl, add some form of component container, such as a column, row, whatever takes your fancy. (Check out the Echo2 api for more information about components and such). Throw a label on it stating "Hello World". Then add this component to the parent component passed in. Maybe it would look a bit like the following.
public void createPartControl(Component parent) { Column content = new Column(); Label myLabel = new Label("Hello World"); contentPane.add(myLabel); parent.add(content); }
Adding the view to a perspective
To see the view it needs to be added to a perspective. Let's add this view to the perspective created in the previous tutorial. Adding the following line to the HelloWorldPerspective.createInitialLayout method will do this for us.
layout.addView("helloworld.HelloBigWorldPart");
Note: There are 2 addView methods in the IPageLayout interface. This method adds the view directly under previous views. The second method allows detailed positioning of the view within the perspective.
Spring/OSGi configuration files
Edit the bundle-context.xml and bundle-context-osgi.xml files and declare the new service.
<b>bundle-context.xml</b> <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <b><bean name="helloworld.HelloBigWorldPart" class="helloworld.HelloBigWorldPart" singleton="false"/></b> <bean name="helloworld.HelloWorldPerspective" class="helloworld.HelloWorldPerspective" singleton="false"/> </beans>
<b>bundle-context-osgi.xml</b> <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:osgi="http://www.springframework.org/schema/osgi" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd"> <b><osgi:service id="helloworld.HelloBigWorldPartOsgi" ref="helloworld.HelloBigWorldPart" interface="com.gbv.platform.workbench.ui.part.IViewPart" lazy-init="false"/></b> <osgi:service id="helloworld.HelloWorldPerspectiveOsgi" ref="helloworld.HelloWorldPerspective" interface="com.gbv.platform.workbench.ui.IPerspectiveFactory" lazy-init="false"/> </beans>
Compile, Install, and Run
Build your project.
mvn clean package
Install your built jar into your Equinox Runtime and refresh the project.
Right click on your Equinox Runtime project, and select "Run On Server". Run it on Tomcat.
Once the server is up, type ss in the console to show all installed bundles and you should, as before, see both the Workbench and the HelloWorld Perspective bundles.
Start the Spring framework OSGi bundle, the OSGi extender bundle and both the Workbench and Hello World Perspective bundles.
Then go to the following url
http://localhost:8080/EquinoxRuntime/Workbench
You should now see your new view within your existing perspective.

Add Comment