Over a million developers have joined DZone.

e4: First e4 RCP Application

· Java Zone

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

The next big thing in Eclipse is Eclipse 4.0 dubbed as e4. It will be released in 2010. That doesn't mean that the 3.x stream will be deprecated or discontinued. The 3.x releases will go on for "few" years till everyone boards the 4.0. But clearly the future of Eclipse is e4 and its already here. I'll be writing a series of blog posts on e4 and this will be the first. You can follow this blog in RSS, or email or even in twitter. I'm still learning about e4, so feel free to correct me if you find any mistakes :-)

First set up your e4 Environment:

1) Download e4 1.0 M1
2) Click e4->Generate e4 Example Project



3) Right click e4-examples.psf and click 'Import Project Set'. This should import the contacts and the photo projects from the CVS to your workspace
4) Open the contacts.product file and click the "Launch an Eclipse Application" link in the Overview page
5) Thats it. You should be running the Contacts demo.

You can play around with the css theme or code and check out the effects. When you are done, lets start an app from the scratch. The first app will have nothing more than a view (something like the 'RCP Application with a View', created by PDE Wizard). Note: If you find it hard to work with e4, you can grab a latest 3.6 build and set the e4 as the target platform and proceed with the following steps)

Step 1:

Use the New Plug-in wizard and create a new plugin. (No RCP - just plain old plugin)


Step 2:

You don't need an Activator. In the Overview tab, remove the Activator class and also delete the Activator.java file from the sources.


Step 3:

The default dependencies of this would have o.e.core.runtime and o.e.ui. In e4, we still need the core.runtime in e4, but not the o.e.ui. Remove it. Now add the following dependencies:

  • org.eclipse.swt,
  • org.eclipse.jface,
  • org.eclipse.e4.ui.workbench
  • org.eclipse.e4.ui.workbench.swt

Step 4:

Lets create a view. Instead of o.e.ui.views, now we have to use org.eclipse.e4.workbench.parts extension (which should now serve both the editors and views). The extension point would look like


<extensio point="org.eclipse.e4.workbench.parts">
<part class="com.eclipse_tips.e4.firstapp.MyFirstView" />
</extension>

The implementation class would be:

public class MyFirstView {

public MyFirstView(Composite parent){

// this shouldn't be there ideally, investigating
if(parent.getLayout() == null)
parent.setLayout(new FillLayout());

Composite composite = new Composite(parent, SWT.NONE);
composite.setLayout(new FillLayout());

Button button = new Button(composite, SWT.PUSH);
button.setText("Hello World");
button.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
MessageDialog.openInformation(new Shell(), "e4", "Hello e4 World!");
}
});
}

}

Yes, what you are seeing it correct. It doesn't implement the IWorkbenchView or extend ViewPart. Further more, it appears that I've done one of the top 10 mistakes by not having a default constructor. Don't worry, e4 is smart. When it instantiates an object like this thru reflection, it identifies the parameters for the constructor and will pass them from the context. If more than one constructor is present, it can even identify the right one to use (more on this later)


Step 5:

Now, we have the view, lets create an RCP product that will use this view. Fire up the New Wizard and create a Product Configuration file with the basic settings. In the Product Configuration editor, give it some name and click the New for the Product. Specify some id and select 'org.eclipse.e4.ui.workbench.swt.application' for Application.



Step 6:

Go back to your plugin.xml. You should see the newly created application there. Now lets create the missing link between the product and the view - the UI model. In a typical 3.x RCP, you would have specified an application that has a run method, an initial perspective id, the layout for that perspective, the views, menus, actions thru the ActionFactory constants etc. This whole setup would require a little bit of both java coding and plugin.xml configuration. In e4, you can specify the entire UI in a model. Thats the applicationXMI. It can get as complex as your UI can be, but the simplest one looks like this:

<?xml version="1.0" encoding="ASCII"?>
<application:MApplication xmi:version="2.0"
xmlns:xmi="http://www.omg.org/XMI"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:application="http://www.eclipse.org/ui/2008/Application">

<windows name="Main Window" width="200" height="100">
<children xsi:type="application:MContributedPart"
id="MyView"
iconURI="platform:/plugin/com.eclipse-tips.e4.firstApp/icons/sample.gif"
URI="platform:/plugin/com.eclipse-tips.e4.firstApp/com.eclipse_tips.e4.firstapp.MyFirstView"/>
</windows>
</application:MApplication>

There is nothing much to explain. It has got nothing more than a window and a view in it. (no menus,no tool bars, no status lines and even no stacks to hold the view) Specify this file in the parameter of the product extension:

<extension
id="rcpProduct"
point="org.eclipse.core.runtime.products">
<product
application="org.eclipse.e4.ui.workbench.swt.application"
name="My First e4 RCP">
<property
name="applicationXMI"
value="com.eclipse-tips.e4.firstApp/App.xmi">
</property>
</product>
</extension>

Step 7:

You are all set to go. Switch to the Dependencies tab of the Product Editor and add your plugin. Click 'Add Required plugins' and launch. The launch will fail, because we have not included few more plugins that are required, but not specified in the dependencies (from databinding beans to renderers will be missing) The simplest way to get thru the error, is to edit the launch configuration and launch with 'all workspace and enabled target plugins'.





Now launch again, voila - there comes your first e4 RCP!

So what has happened till now? With the product & launch configuration, we are basically running the org.eclipse.e4.ui.workbench.swt.application. The application looks for the model of the UI in the applicationXMI parameter. It creates the Workbench, and asks it to render the UI from that model. Recursively, an appropriate renderer for every element in the XMI file is found, and its asked to create the UI. Finally you get the app:


From http://blog.eclipse-tips.com

Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}