Wednesday, March 12, 2008

 

GroupLayout converting Java 6 to Java 5

OK, so I'm writing Swing code again (which I'm not very good at), and have been using an odd combination of Netbeans 6 to do form design via Matisse (since the Eclipse version is MyEclipse only?) and the rest of the code in Eclipse (since I can't get comfortable in NB yet). Unfortunately, and perhaps because I'm running on Java 6, Matisse defaults to using the Java 6 javax.swing.GroupLayout, but my client is running Java 5 (late discovery). While I could install a new version of Java for them, the lack of Java 6 on my G5 Mac at home tipped me back to 5.

While there is an option in NB to generate using the org.jdesktop packages (enabled now), it won't retrospectively rejig an existing form. Now the APIs while essentially the same code, and different versions so a bunch of things make the process more than changing the imports.

The script below did 95% of the changes my forms needed (in fact just one line in two forms needed a parameter removing -- addComponent had an extra param on a vertical group). Just thought I'd put it out there since I saw a few forum posts asking about this. It would be nice if NB could do this for you, but I suspect it's a low priority item..
$ cat swing6tolayout.sed
s/javax.swing.GroupLayout/org.jdesktop.layout.GroupLayout/g
s/GroupLayout.Alignment/GroupLayout/g
s/javax.swing.LayoutStyle.ComponentPlacement./org.jdesktop.layout.LayoutStyle./g
s/\.addGroup/\.add/g
s/\.addComponent/\.add/g
s/\.addGap/\.add/g

$ sed -f swing6tolayout.sed MyForm.java > tmp.java
$ mv tmp.java MyForm.java
It does make me smile that despite the size / complexity / ability of Eclipse & Netbeans, sed was the easiest way to do this. Wasn't Eclipse supposed to be providing refactoring batches? Or James Gosling's project to manipulate code? Dunno. Sed was already in the toolkit (cygwin), so it won.

Update:
Annoyingly, when you do this, the form reverts to the previous layout type next time you edit in NetBeans. By the power of diff, you can fix this too by changing the MyForm.form file thus:
$ diff JDesk.form Java6.form
9c9
< <auxvalue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="2" />
---
> <auxvalue name="FormSettings_layoutCodeTarget" type="java.lang.Integer" value="1"/>
ie '2' is for jdesktop.org and '1' is for Java 6.



Labels: , ,


This page is powered by Blogger. Isn't yours?