Packagesorter Sources

Here are the sources to the package sorter Eclipse plugin. The plugin is released under the EPL license and is packed as Eclipse Project. Unzip and import the directory into your Eclipse workspace.

Download

Tuesday, October 14th, 2008 Other No Comments

Install Java 6 Update 10 on Ubuntu 8.04

I had to install Java 6 Update 10 on Ubuntu 8.04. The thing is straight forward if you do not want to integrate the new JVM into Ubuntus alternatives system (and do not need teh new Java Plugin in your browser). Download, install, modify your PATH, JAVA_HOME and JDK_HOME, done. Google helped to find this really good description of the process, including moving the JVM to /usr/lib/jvm, where Ubuntu expects the JVMs.

The tricky part is the integration into Ubuntus alternatives system. It provides an easy way to switch between VMs and is usable even by “non geek” users and update-java-alternatives will also switch your java browser plugin.

We have to have Java 6 installed from the Ubuntu repositories:

sudo apt-get install sun-java6-jdk

Now, follow Caspers first two steps to install the VM. Download Update 10 and execute the script

sh jdk-6u10-rc2-bin-b32-linux-amd64-12_sep_2008.bin

Move the directory to /usr/lib/jvm, where Ubuntu expects JVMs

sudo mv jdk1.6.0_10 java-6-sun-1.6.0.10
sudo mv jdk1.6.0_10/ /usr/lib/jvm

And create a symlink

cd /usr/lib/jvm
sudo ln -s java-6-sun-1.6.0.10 java-6.10-sun

The directory should look more or less like this

lrwxrwxrwx   1 root   root      23 2008-10-07 19:01 java-1.5.0-sun -> java-1.5.0-sun-1.5.0.15
drwxr-xr-x  10 root   root    4096 2008-10-08 11:34 java-1.5.0-sun-1.5.0.15
-rw-r--r--   1 root   root    2165 2008-03-26 02:28 .java-1.5.0-sun.jinfo
lrwxrwxrwx   1 root   root      19 2008-10-08 12:21 java-6.10-sun -> java-6-sun-1.6.0.10
lrwxrwxrwx   1 root   root      19 2008-10-07 19:00 java-6-sun -> java-6-sun-1.6.0.06
drwxr-xr-x   8 root   root    4096 2008-10-08 11:35 java-6-sun-1.6.0.06
drwxr-xr-x  10 root   root    4096 2008-10-08 11:40 java-6-sun-1.6.0.10
-rw-r--r--   1 root   root    2341 2008-10-08 11:47 .java-6-sun.jinfo

To integrate the new VM into the alternatives system, we use update-alternatives --install to get new alternatives and provide a .jinfo file used by update-java-alternatives. For simplicity, I (and the script) assume that you have Java 6 installed. We need the Java 6 .jinfo file as a template. Basically, we h clone and modify the file, such that all the links point to the appropriate directory. Well, that can be done with your favourite text editor. The ugly part is, you have to call udpate-alternatives --install for ALL the commands. That calls for a script
.

#!/usr/bin/perl
use strict;

# use this as template - we assume that you have java 6 installed
# its in the repo !
my $jvm_template="java-6-sun";
my $jvm_template_path="/usr/lib/jvm/java-6-sun";

# this is the new jvm
my $jvm_name="java-6-sun-1.6.0.10";
my $jvm_alias="java-6.10-sun";
my $jvm_path="/usr/lib/jvm/java-6.10-sun";
my $jvm_priority=64;
my $jvm_section="non-free";

open(JINFO_OUT, ">/usr/lib/jvm/.$jvm_alias.jinfo") or die "Can not write jinfo file !";

#write header
print JINFO_OUT "name=$jvm_name\n";
print JINFO_OUT "alias=$jvm_alias\n";
print JINFO_OUT "priority=$jvm_priority\n";
print JINFO_OUT "section=$jvm_section\n\n";

my @lines = ();
open(IN, "/usr/lib/jvm/.$jvm_template.jinfo") or die "Can note read jinfo template!";
@lines = <IN>;
close(IN);

my @new_config = ();
foreach(@lines){
    if($_=~ /$jvm_template_path/){
        chomp($_);
        $_ =~ s/$jvm_template_path/$jvm_path/;
        push(@new_config, $_);
        print JINFO_OUT "$_\n";
    }
}
close(JINFO_OUT);

foreach(@new_config){
    my @split = split(' ', $_);
    system("update-alternatives --install /usr/bin/@split[1]  @split[1]  @split[2] $jvm_priority");
}

Download the script and execute it (with sudo/as root – we have to write to /usr/lib/jvm) after you have moved the update 10 installation to /usr/lib/jvm/java-6-sun-1.6.0.10 and created the symlink to java-6.10-sun as described above. (The script is quick and dirty. The names are defined in the first lines. If you have used other directory/alias names, you have to modify the script).

sudo perl create-jvm-alternative-6.10.pl

This generates a .jinfo file in /usr/lib/jvm and it calls update-alternatives --install for all the things mentioned in the template .jinfo. The update-alternatives takes a generic name as first argument (thanks Jonathan – I should read man pages more often). This is created as a symlink pointing to the actual name in /etc/alternatives. /etc/alternatives then points to the currently chosen alternative. SO, for example, at some point the script calls:

update-alternatives --install /usr/bin/java java /usr/lib/jvm/java-6.10/bin/java

This creates two symlinks

/usr/bin/java -> /etc/alternatives/java
/etc/alternatives/java -> /usr/lib/jvm/java-6.10/bin/java

The latter might already exist and point to some java alternative (i.e. java v1.5). As far as I’ve seen this is not overwritten. The latter symlink is the one that is actually changed when you switch alternatives.

When everything is done, you should get something like:

thasso@antef:~> update-java-alternatives -l
java-1.5.0-sun 53 /usr/lib/jvm/java-1.5.0-sun
java-6.10-sun 64 /usr/lib/jvm/java-6.10-sun
java-6-sun 63 /usr/lib/jvm/java-6-sun
Thursday, October 9th, 2008 Other 10 Comments

A simple AbstractBean

Just in case, here is a simple example of an AbstractBean class that can be used as a base class for JavaBeans if you want to use the Eclipse bouded setter plugin.

public abstract class AbstractBean {
    /**
     * Create a new change support
     */
    protected PropertyChangeSupport change = new EDTSafePropertyChangeSupport(this);

    /**
     * @param listener
     * @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.beans.PropertyChangeListener)
     */
    public void addPropertyChangeListener(PropertyChangeListener listener) {
        change.addPropertyChangeListener(listener);
    }

    /**
     * @param propertyName
     * @param listener
     * @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.lang.String, java.beans.PropertyChangeListener)
     */
    public void addPropertyChangeListener(String propertyName,
            PropertyChangeListener listener) {
        change.addPropertyChangeListener(propertyName, listener);
    }

    /**
     * @param propertyName
     * @param oldValue
     * @param newValue
     * @see java.beans.PropertyChangeSupport#firePropertyChange(java.lang.String, java.lang.Object, java.lang.Object)
     */
    public void firePropertyChange(String propertyName, Object oldValue,
            Object newValue) {
        change.firePropertyChange(propertyName, oldValue, newValue);
    }

    /**
     * @return
     * @see java.beans.PropertyChangeSupport#getPropertyChangeListeners()
     */
    public PropertyChangeListener[] getPropertyChangeListeners() {
        return change.getPropertyChangeListeners();
    }

    /**
     * @param propertyName
     * @return
     * @see java.beans.PropertyChangeSupport#getPropertyChangeListeners(java.lang.String)
     */
    public PropertyChangeListener[] getPropertyChangeListeners(
            String propertyName) {
        return change.getPropertyChangeListeners(propertyName);
    }

    /**
     * @param propertyName
     * @return
     * @see java.beans.PropertyChangeSupport#hasListeners(java.lang.String)
     */
    public boolean hasListeners(String propertyName) {
        return change.hasListeners(propertyName);
    }

    /**
     * @param listener
     * @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.beans.PropertyChangeListener)
     */
    public void removePropertyChangeListener(PropertyChangeListener listener) {
        change.removePropertyChangeListener(listener);
    }

    /**
     * @param propertyName
     * @param listener
     * @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.lang.String, java.beans.PropertyChangeListener)
     */
    public void removePropertyChangeListener(String propertyName,
            PropertyChangeListener listener) {
        change.removePropertyChangeListener(propertyName, listener);
    }

    /**
     * Simply ensures that events are delivered through the event dispatching thread
     *
     */
    class EDTSafePropertyChangeSupport extends PropertyChangeSupport{
        public EDTSafePropertyChangeSupport(Object sourceBean) {
            super(sourceBean);
        }
        @Override
        public void firePropertyChange(final PropertyChangeEvent evt) {
            if(SwingUtilities.isEventDispatchThread()){
                super.firePropertyChange(evt);
            }else{
                SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        firePropertyChange(evt);
                    }
                });
            }
        }
    }
}

This implementation restricts itself to the firePropertyChange(String,Object,Object) method. It also provide a custom extension of PropertyChangeSupport that ensures that events are fired in the EventDispatchingThread.

Tags: , ,

Thursday, October 2nd, 2008 Eclipse, Swing No Comments

generate bounded setters plugin

In many cases you need to have bound properties while working with JavaBeans. It might be easy to add PropertyChangeSupport but this does not solve the hassle of modifying your setter methods to fire change events. Of course you can do that with a search and replace pattern, but this is not a very nice and intuitive solution. A better way to address the problem is changing the Eclipse template for setter methods. This works quit well and you can keep things short, but you will loose the ability to create unbound setter methods. Of course, you can modify the template on a per project bases, but in my case, I always end up in situations where I need both bound and unbound properties.

I searched the net to find an Eclipse plugin that does the job of creating bound properties, but I didn’t find anything. So….I wrote my own plugin. The plugin works with Eclipse Ganymede (>=3.4) and can be installed using this update site:

http://java.randgestalten.de/updatesite

At the moment, the the plugin assumes that your bean provides a firePropertyChange(String, Object, Object) method. Thats one drawback I will try to address in the next version. For the moment, I think a good solution is to provide an AbstractBean class that you extend when you write a JavaBean from scratch ( here is an example of such class).

As long as your class provides a firePropertyChange method, you can use the plugin to generate bounded getter/setter methods. The plugin uses the default template for getter methods and provides a new template for bounded setters. You find the template in the Java->Editor->Templates preferences, where you can change it according to your needs.

Using the plugins mechanism to create getter/setter is straight forward, integrated in the “Source” menu of the context popup in the java editor as well as the package explorer and outline views. Just start writing your bean, right-click in the editor and select Source->Generate bounded getters/setters. This will open the same dialog as the default getter/setter generation except that it uses the boundedsetter template to create the setter method.

Menu integration

Menu integration

Beside the integration into the context menu, the plugin also supports quickfix and quickassist. Move your cursor to a variable definition and hit ctrl-1 (or command-1 if you are on a mac) to open the quickfix menu. The plugin integrates a bounded getter/setter generation quickfix that follows the same rules as the default generation behavior. You will see the same dialog except that you will get a bounded setter when you hit the Ok button.

So, as said before, the plugin integrates nicely into the Eclipse mechanism of generating getter/setter methods. You have a custom template just for the bounded setters and all dialogs are basically the same.

Quickfix Dialog

Quickfix Dialog

The thing that is missing currently is the automatic generation of the property change support or some delegation methods. This involves more work, because we have to check for existing methods…create property change support….insert delegation methods etc. I will try to build that in the next version, but for the moment you have to provide that by yourself. Either let your Bean extend an AbstractBean implementation that already supports property changes or integrate it directly into the object using PropertyChangeSupport. Keep in mind that plugin currently expects a firePropertyChange(String,Object,Object) method.

EDIT
The plugins source code is available here.

UPDATE

I just checked, and it seems that the Plugin is also working with Eclipse 3.5 but you have to uncheck the “Group items by category” to get the plugins listed.

Tags: , , ,

Wednesday, October 1st, 2008 Eclipse 7 Comments

Package sorter plugin

 

Packagesorter in action

Packagesorter in action

While working on EPoS, I found it very confusing that Eclipse does not sort multiple source directories alphabetically. The little PackageSorter plugin helps here and can be used in Eclipse Ganymede (>=3.4). The plugin is available on our update site:

http://java.randgestalten.de/updatesite

Add this site in the Eclipse Software Update dialog and install the Packagesorter plugin. If the plugin is successfully installed, it provides a button in to top toolbar of the java package explorer. If activated, it will sort your source directories in alphabetic order instead of using the build-path order, which seems to be the default.

As long as you only deal with one source directory, you will see no use in this plugin, but when your project consists of multiple source directories, which are all included in the projects build path, this might be helpful.

Source code (Download)

Tags: , ,

Tuesday, September 30th, 2008 Eclipse 1 Comment

Eclipse Plugins update site

I just set up an update site for our tiny eclipse plugins. The update site URL is

http://java.randgestalten.de/updatesite

and it includes the package sorter plugin and the bounded setter generator – and might include more at some point – we’ll see.

Tags: ,

Tuesday, September 30th, 2008 Eclipse No Comments