Archive for August, 2009

Reflections on Predictably Irrational

Monday, August 31st, 2009

I recently read the book Predictably Irrational by Dan Ariely. I really enjoyed his insights into motivation and personal judgement of values. If you don’t have the time to read the book you may want to listen to this podcast which contains the core of the book: Dan Brinklin’s interview with Dan Ariely.

What I personally found most interesting is the chapter about options. The book clains that people tend to leave to many options open.

Dan advices that we should leave options, which are of no value for us, behind us. Otherwise you spread yourself to thin over too many options.

I’m convinced by his argumentation and I see in myself a pattern that I like to leave options open. I’m playing with several programming languages, IDE’s and have lots of technical books about a wide-spread range of topics.

I should depart from a technology if I believe that another is more appropriated from my tasks. But of course I need to be open for new technologies and trends.

Therefore I decided to do the following:

  1. I will stop playing around with several IDE’s and focus on only one IDE
  2. I will get rid of technology books and articles if I have not read them in 2 years. Two years are long enough; if I have not read then in two year I propertly will never read them.
  3. I also have several currently unused projects which I was planning to “fix later”. I will just leave them as they are.
  4. I will not maintain Articles from www.vogella.de anymore if I’m not using that technology anymore and if there I no public interest in them.
 

Eclipse e4 – Using the modeled user interface editor

Wednesday, August 26th, 2009

Update This description has been updated in the following tutorial: Eclipse e4 tutorial.

In the Eclipse e4 webseminar I learned about the e4 UI editor which allow you to change the user interface on the fly.

As you most likely know the Eclipse UI is modelled as an Eclipse EMF model. This model can be changed during directly in Eclipse via the editor or via the EMF API (I have not yet tested this) and the UI will change immeadiately according to the change.

To following my example you need the E4 0.9 download from Eclipse E4 0.9 download.

Start Eclipse e4 tutorial. Select Window -> UI Editor

e4modelledui10

Open the WorkbenchWindow

e4modelledui20

You can now modify the model, e.g. you can drag the package explorer on the same level as the Java perspective.

e4modelledui30

I believe the modeled UI concept is really powerful as it exposes the internal state of the Eclipse based apllication via a well-defined model with a clear API.

Kudos to the Eclipse e4 team!

 

Eclipse e4 – Styling your UI with css

Tuesday, August 18th, 2009

Update This description has been updated in the following tutorial: Eclipse e4 tutorial.

Eclipse e4 allows to style your applications via css like stylesheets. The following gives a short example how to get started with css stylecheets.

Create a new e4 product “de.vogella.e4.css” similar to the description Your first Eclipse e4 application. Of course you can also copy “de.vogella.e4.first” and edit the package, Application.xmi and the product to fit the new project “de.vogella.e4.css” or just extend “de.vogella.e4.first” with the usage of stylesheets.

I like to have my examples separate therefore I use “de.vogella.e4.css”.

First lets extend our UI from last time a little bit. Change View1.java to the following.


package de.vogella.e4.css.views;

import org.eclipse.e4.core.services.context.IEclipseContext;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.services.IDisposable;

public class View1 implements IDisposable {

	public View1(Composite parent, final IEclipseContext outputContext) {
		GridLayout gridLayout = new GridLayout(2, true);
		Label label = new Label(parent, SWT.NONE);
		label.setText("E4 is new");
		GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
		gridData.horizontalIndent = 20;
		label.setLayoutData(gridData);
		Text text = new Text(parent, SWT.NONE);
		text.setText("and different");

		label = new Label(parent, SWT.NONE);
		label.setText("This is a button");
		new Button(parent, SWT.PUSH).setText("My button");

		GridLayoutFactory.createFrom(gridLayout).generateLayout(parent);
	}

	public void dispose() {

	}
}

Run your application and you get the following:

e10css10

This is obvious an application which does not yet use css styleing.

Add now the folder css to your application and create the following file myfirststylesheet.css:


Label {
	font: Verdana 20px;
	color:rgb(178,34,34);
	background-color:rgb(255,255,255) rgb(0,0,0) 60%;
}

Button {
	font: Verdana 15px;
	background-color:rgb(255,255,255) rgb(0,0,0);
}

Text {
	font: Verdana 15px;
	background-color:radial rgb(255,255,255) rgb(0,0,0);
}

Add the property “applicationCSS” which points to the css file to your product:

<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
   <extension
         point="org.eclipse.e4.workbench.parts">
          <part
            class="de.vogella.e4.first.views.View1"
            label="My first E4 View"
            parentId="org.eclipse.e4.ui.tags.navigation">
      </part>
   </extension>
   <extension
         id="product1"
         point="org.eclipse.core.runtime.products">
      <product
            application="org.eclipse.e4.ui.workbench.swt.application"
            name="vogella">
         <property
               name="appName"
               value="vogella">
         </property>
         <property
               name="applicationXMI"
               value="de.vogella.e4.css/Application.xmi">
         </property>
         <property
               name="applicationCSS"
               value="platform:/plugin/de.vogella.e4.css/css/myfirststylesheet.css">
         </property>
      </product>
   </extension>

</plugin>

Run your Eclipse e4 application and you should receive this amazing and beautiful UI .

e10css20

Arguably the beauty lies in the eye of the beholder. ;-)

I hope this get you started in playing around with the css styling. Check the existing cvs examples (org.eclipse.e4.demo.contacts and org.eclipse.e4.ui.examples.css) to learn more about. You find these in the Eclipse cvs trunk under E4->org.eclipse.e4.ui. The usage of cvs is described Source Code Guide of Eclipse 3.5 (Galileo) – Using cvs

I’m sure you can conjure a nicer UI then I did in my example. :-)

Good luck in your E4 journey!

 

Add the error log view to your Eclipse RCP application

Monday, August 17th, 2009

The Eclipse platform provide error message in case something goes wrong.

During development I use the launch parameter -consoleLog to see if any error has occured. I assume most plugin developer use -consoleLog that is why I opened Bug for making -consoleLog a default in a new launch configuration.

Obviously you don’t want your users to check the console for errors. To make errors visible to the user you can add the exiting error log to your RCP application.

Create a new Eclipse RCP project “de.vogella.rcp.intro.errorview” for this purpose. Use the “Hello RCP” as a template.

Select the plugin.xml and add the dependency to org.eclipse.ui.views.log.

Ddd the standard Eclipse command “org.eclipse.ui.views.showView” to your menu under the Entry “Admin”. See Eclipse Commands how to do this.

This will result in the following plugin.xml


<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>

   <extension
         id="application"
         point="org.eclipse.core.runtime.applications">
      <application>
         <run
               class="de.vogella.rcp.intro.errorview.Application">
         </run>
      </application>
   </extension>
   <extension
         point="org.eclipse.ui.perspectives">
      <perspective
            name="RCP Perspective"
            class="de.vogella.rcp.intro.errorview.Perspective"
            id="de.vogella.rcp.intro.errorview.perspective">
      </perspective>
   </extension>
   <extension
         point="org.eclipse.ui.menus">
      <menuContribution
            locationURI="menu:org.eclipse.ui.main.menu">
         <menu
               label="Admin">
            <command
                  commandId="org.eclipse.ui.views.showView"
                  label="Show Error View"
                  style="push">
               <parameter
                     name="org.eclipse.ui.views.showView.viewId"
                     value="org.eclipse.pde.runtime.LogView">
               </parameter>
            </command>
         </menu>
      </menuContribution>
   </extension>

</plugin>

Run your new application. You should have a menu entry “Admin” with an entry “Show Error View”. If you select it the error view should open.

errorLogView10

 

Create your first Eclipse e4 application

Wednesday, August 12th, 2009

Update This description has been updated in the following tutorial: Eclipse e4.

Eclipse released on the 29 July 2009 the tech preview version 0.9 of Eclipse e4, which can be downloaded here.

I wasn’t able to find a description how to create an new application based on E4. Starting from the CVS example is possible but I wanted to start from scratch. After several failed attempts to create an E4 application I believe I have found a way to create an E4 application. I assume others will have similar problems so I wanted to share my experience.

To follow this description you need to have knowledge how to develop Eclipse Plugins in Eclipse 3.5 or earlier. Check out this Eclipse Plugin Development Tutorial.

So lets get started with e4.

Download e4. Extract it and start it is similar to older Eclipse versions.

Currently there is no wizard which creates a working scaffold of an E4 application. We will therefore use the exsting plugin templates.

Create a new Plugin project “de.vogella.e4.first”. Do not select “Generate an activator”, nor “This plug-in will make contributions to the UI” nor “Would you like to create a rich client application”. Do not select a template. Press finish.

e410

This will open the Plugin Perspective, switch here to the Package Explorer to see your new project.

Create the package “de.vogella.e4.first”. The result should look like the following.

e412

Select your MANIFEST.MF and add the following dependencies to your plugin.

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: First
Bundle-SymbolicName: de.vogella.e4.first;singleton:=true
Bundle-Version: 1.0.0.qualifier
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Require-Bundle: org.eclipse.ui,
 org.eclipse.core.runtime,
 org.eclipse.e4.ui.workbench;bundle-version="0.9.0",
 org.eclipse.e4.ui.workbench.renderers.swt;bundle-version="0.9.0",
 org.eclipse.e4.ui.workbench.renderers.swt.contributions;bundle-version="0.9.0",
 org.eclipse.e4.ui.workbench.swt;bundle-version="0.9.0",
 org.eclipse.e4.ui.services;bundle-version="0.9.0",
 org.eclipse.e4.core.services;bundle-version="0.9.0"

You need (some of) these plugins for the usage of E4 features.

Add the following extension points. I noticed that for some of the features you have to modify the plugin.xml directly as the UI does not support them. But you have to create the plugin.xml via the MANIFEST.MF file; as I created it directly as a new file I received a strange system behavior.


<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
   <extension
         point="org.eclipse.e4.workbench.parts">
          <part
            class="de.vogella.e4.first.views.View1"
            label="My first E4 View"
            parentId="org.eclipse.e4.ui.tags.navigation">
      </part>
   </extension>
   <extension
         id="product"
         point="org.eclipse.core.runtime.products">
      <product
            application="org.eclipse.e4.ui.workbench.swt.application"
            name="vogella">
             <property
               name="appName"
               value="vogella">
         </property>
         <property
               name="applicationXMI"
               value="de.vogella.e4.first/Application.xmi">
         </property>
      </product>

   </extension>

</plugin>

“org.eclipse.e4.workbench.parts” is a new extension point by which you can define your view parts, formally known as views and editors.

The extension point “org.eclipse.core.runtime.products” has a new parameter “applicationXMI”. This is a parameter to the model of your UI. We came to this later. You also do not define your own application; we use “org.eclipse.e4.ui.workbench.swt.application”.

Create a product configuration called “vogella.product”. This is similar to Eclipse 3.5; see here for a description: Eclipse Product Configuration.

e420

Make sure you select “org.eclipse.e4.ui.workbench.swt.application”.

e430

Add as dependency to your product the plugin “org.eclipse.e4.ui.workbench”. Add also the plugin “de.vogella.e4.first”.
Press “add required plugins”.

Create the following two classes:


package de.vogella.e4.first.handlers;

import org.eclipse.e4.workbench.ui.IWorkbench;

public class ExitHandler {
	public void execute(IWorkbench workbench) {
		workbench.close();
	}
}

and

package de.vogella.e4.first.views;

import org.eclipse.e4.core.services.context.IEclipseContext;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.services.IDisposable;

public class View1 implements IDisposable {

	public View1(Composite parent, final IEclipseContext outputContext) {
		Label label = new Label(parent, SWT.NONE);
		label.setText("E4 is new");
		GridData gridData = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
		gridData.horizontalIndent = 20;
		label.setLayoutData(gridData);
		Text text = new Text(parent, SWT.NONE);
		text.setText("and different");
		GridLayoutFactory.fillDefaults().generateLayout(parent);
	}

	public void dispose() {

	}
}

You need a model for your application. In the parameter “applicationXMI” we earlier defined that this model will be “de.vogella.e4.first/Application.xmi”. Create the file Application.xmi with the following content.


<?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" x="100" y="100" width="800" height="600">
    <menu>
      <items xsi:type="application:MMenuItem" name="File">
        <menu>
          <items xsi:type="application:MMenuItem" id="" name="Exit" command="//@command.0"/>
        </menu>
      </items>
    </menu>
    <children policy="VerticalComposite">
      <children xsi:type="application:MSashForm" policy="HorizontalSash">
        <children xsi:type="application:MStack">
          <children xsi:type="application:MContributedPart" iconURI="" name="My View" tooltip="My first View" URI="platform:/plugin/de.vogella.e4.first/de.vogella.e4.first.views.View1"/>
        </children>
        <weights>30</weights>
        <weights>70</weights>
      </children>
    </children>
    <handlers id="" URI="platform:/plugin/de.vogella.e4.first/de.vogella.e4.first.handlers.ExitHandler" persistedState="" command="//@command.0"/>
  </windows>
  <command id="application.exit" name="Exit"/>
</application:MApplication>

This is the EMF model which defines your UI. Eclipse should display this model in a EMF editor so that you can investigate it. In this model you find your view and command definition. The content can be viewed via the properities view.

e450

Now start your application from your vogella.product. Starting it directly from the plugin.xml will not work (easily).

If you receive the error “org.eclipse.core.runtime.AssertionFailedException: null argument:-applicationXMI argument missing” check you plugin.xml. E4 created in several cases twice the products entry.

You should be rewarded ;-) with this user interface:

e460

With this first application I hope you can started using the E4 advanced features, e.g. CSS styling, etc.

My final thoughts: Eclipse e4 has already amazing capabilities for a tech review release. The new modeled UI removes the need for a lot of boilerplate code.

Try it out!

 

TweetHub – Eclipse ECF Twitter client

Monday, August 10th, 2009

Eclipse ECF is a framework for supporting the development of distributed Eclipse-based tools and applications.

With the Galileo release Eclipse ECF provides also a nice Twitter client TweetHub. TweetHub is an Eclipse RCP application for communicating with Twitter.

It took me a while to gather the right information to make TwitterHub work. So I thought it might be helpful to blog about it so that this might be easier for others.

First you need to install the ECF SDK via the update manager from http://download.eclipse.org/rt/ecf/3.0/3.5/repo.

To install TwitterHub you need to download the code from cvs. The cvs connection string is: :pserver:anonymous@ecf1.osuosl.org:/ecf

To learn how to use cvs see Eclipse cvs access.

Check-out the following plugins from the cvs location:

org.eclipse.ecf.provider.twitter.ui.hub.product
org.eclipse.ecf.provider.twitter.ui.hub
org.eclipse.ecf.provider.twitter

TweetHub08

This should be sufficient. You should now be able to run the product.

TweetHub10

So now you can use TwitterHub to follow me on Twitter. ;-)

I would like to thank Marcelo Mayworm who helped on in the eclipse.technology.ecf newsgroup.

Additional links:
ECL New and Noteworthy
TweetHub wiki .

 

Collection of Java related Podcasts

Thursday, August 6th, 2009

The following is a list of my favorite Java and Java related Podcasts. Please feel free to suggest additional Java podcasts.

While this blog entry will not be edited I’m planning to keep an updated list of these Java Podcasts on Java Podcasts Overview.

Java Podcasts:

Javaposse

Most likely the best known Java podcast is Javaposse which can be found on http://javaposse.com/.In this podcast Tor Norbye, Carl Quinn, Dick Wall and Joe Nuxoll discuss the news around Java and the Java Virtual Machine and perform interviews with the key player in the Java world. They also cover news about Scala.

This podcast is in english.

Eclipse live

Eclipse related web seminars and podcasts can be found on http://live.eclipse.org/ .

Theses podcasts are (mainly) in english.

Javahispano

Spanish podcast around Java technology and Java news can be found http://feeds.javahispano.org/JHPodcasts

This podcast is in spanish.

Les cast codeurs

French podcast around Java technology and Java news can be found at http://lescastcodeurs.com/.

This podcast is in french.

Java related (covering also other topics)

Software Engineering Radio

Software Engineering Radio performs interesting interviews with the technical stars from different programming areas (including Java technology). They also have sessions there they explain on a starter level technology for example “what is database”.

http://www.se-radio.net/

This podcast is in english.

IBM Developerworks

IBM developerworks features mainly interviews about all kinds of topics including lots of Java and JVM related informations.

http://www.ibm.com/developerworks/podcast/

This podcast is in english.

 

Using Collections.sort and Comparator in Java

Tuesday, August 4th, 2009

Sorting a collection in Java is easy, just use Collections.sort(Collection) to sort your values. For example:

package de.vogella.algorithms.sort.standardjava;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Simple {
	public static void main(String[] args) {
		List list = new ArrayList();
		list.add(5);
		list.add(4);
		list.add(3);
		list.add(7);
		list.add(2);
		list.add(1);
		Collections.sort(list);
		for (Integer integer : list) {
			System.out.println(integer);
		}
	}
}

This is possible because Integer implements the Comparable interface. This interface defines the method compare which performs pairwise comparison of the elements and returns -1 if the element is smaller then the compared element, 0 if it is equal and 1 if it is larger.

But what if what to sort differently, e.g. for example in different order. Well, you could just use Collection.reverse(). Or you define your own class with implements the interface Comparator.

package de.vogella.algorithms.sort.standardjava;

import java.util.Comparator;

public class MyIntComparable implements Comparator<Integer>{

	@Override
	public int compare(Integer o1, Integer o2) {
		return (o1>o2 ? -1 : (o1==o2 ? 0 : 1));
	}
}
package de.vogella.algorithms.sort.standardjava;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Simple2 {
	public static void main(String[] args) {
		List<Integer> list = new ArrayList<Integer>();
		list.add(5);
		list.add(4);
		list.add(3);
		list.add(7);
		list.add(2);
		list.add(1);
		Collections.sort(list, new MyIntComparable());
		for (Integer integer : list) {
			System.out.println(integer);
		}
	}
}

The nice thing about this approach is that you then sort any object by any attribute or even a combination of attributes. For example if you have objects of type Person with an attribute income and dataOfBirth you could define different implementations of Comparator and sort the objects according to your needs.