Everyday Coder

The rants and raves of a programmer in the trenches.

The Real Transformers

I was checking out some videos on YouTube today and came across this one. I love this video. Basically, it's a stop-motion animation using transformer action figures. If you're a fan of the Transformers (from way back), you'll like this video.

I must say that the dialog was a bit lacking and the sound needs a little work too, but it's still worth it. Another great day online!

Side Note: Man, I really need to upgrade my Wordpress. I'd love to be able to embed the movie on this page. Anyway...

I am the type of developer that uses existing 3rd-party solutions whenever possible. Lately, I've been using XStream for object persistance in a standalone Java application. So far, I must say that I'm extremely impressed with how well this API is put together and also with its simplicity. Exporting an entire object tree is as simple as new XStream().toXML(myObjectTree);

One of the great features of XStream is its ability for customization of the XML. I began customizing my XML and ran into a slight snag (not a bug, but a snag). I couldn't figure out how to get an enum field to output as an attribute rather than a child element. Example Java class:

class Card {
int val;
Type type;

enum Type {
HEARTS, CLUBS, DIAMONDS, SPADES
}
}

Now let's export it:

void export(List<Card> cards) {
XStream xs = new XStream();
xs.alias("cards", List.class);
xs.alias("card", Card.class);
xs.useAttributeFor("val", int.class);
xs.useAttributeFor("type", Type.class);

System.out.print(xs.toXML(cards));
}

Executing the above code will produce the following XML:

<cards>
<card val="1">
<type>SPADES</type>
</card>
</cards>

As you can see, the type field is not properly used as an attribute like I instructed: xs.useAttributeFor("type", Type.class); The reason? Well, I wasn't sure, so, being the lazy developer I am, I trolled around Google, et. al. to find if someone else had this problem. Nothing. And after a few minutes of poking around in the XStream documentation, I was at a loss. I sent an email describing my problem to the users' mailing list, expecting to wait a day or so for an answer. Supprisingly, I recieved an answer within a few hours, from the project lead himself.

Jörg informed me that attribute converters must be of type SingleValueConverter. Attributes are single text values and don't need all of that other object (un)ravelling stuff. Jörg also pointed me to an issue that had been opened in the project's bug tracking system just a few days ago that also described this.

The solution, at the moment, is to simply create a specific converter for my Enum types that is a SingleValueConverter. The code for this is below:

public class SingleValueEnumConverter extends AbstractSingleValueConverter {

private final Class enumType;

public SingleValueEnumConverter(Class type) {
this.enumType = type;
}

public boolean canConvert(Class c) {
return c.equals(enumType);
}

public Object fromString(String value) {
return Enum.valueOf(enumType, value);
}
}

And to modify the XStream example above:

XStream xs = new XStream();
xs.alias("cards", List.class);
xs.alias("card", Card.class);
xs.useAttributeFor("val", int.class);
xs.useAttributeFor("type", Type.class);

xs.registerConverter(new SingleValueEnumConverter(Card.Type.class));

System.out.print(xs.toXML(cards));

And now the XML looks like this:

<cards>
<card val="1" type="SPADES"/>
</cards>

So as you can see, this was exactly what I needed. I love coding in a world where we have such a large community of people willing to take a few minutes of their time to guide you in the right direction. Great job XStream!

Soft Clipping with Java 2D

The current application that I'm coding uses an interface composed mostly of rounded rectangles. Initially it looked really cool. I later added transparency to some of the panels so that the parent panel's background would show through. On the Mac, it looked really great. I had since ran the application on Windows and a few of my decisions regarding how I made the rounded rectangle backgrounds came back to haunt me.

With a little more testing, I discovered that the issues lied with anti-aliasing and clipping using Graphics2D. Since this issue was purely asthetic, I moved on to more pressing issues. Then, about a week later I got my monthly Tech Tips from Sun. Talk about reading my mind. The Java 2D Trickery - Core Java Technologies Technical Tips contains 2 excellent Java 2D tips. The first one titled Java 2D Soft Clipping was the answer I needed.

After about 5 minutes, the problem was resolved and it now looks great on both platforms. Excellent tip! Well done Chris Campbell! I suggest you check it out and subscribe if you haven't already done so.

Our Old Friend: GridBagLayout

I stumbled across this article about my old friend the GridBagLayout today. I for one thought I was in a very small minority when it comes to actually using and liking the GridBagLayout. Well, Jeff Friesen proved that he is also on my side when he wrote Harness the Power of Java's GridBagLayout. The article is a very good demystifier for those non-believers out there.

Since this is the first article in a series, it really only covers the basics, but it promises even more. Jeff does say that in case you're still not impressed with GridBagLayout, part 3 presents the JGoodies FormLayout alternative, and compares this layout manager with GridBagLayout so you can choose the layout manager that works best for you. I can't wait. People are always telling me to switch to FormLayout, but I've used it in the past and have not looked back. I just did not like dealing with those cryptic configuration strings ("3dlu p d:fill 4f 5u") all the time. I look forward to the 3rd part of the series.

If your scratching your head over which LayoutManager to use for forms, I would recommend the GridBagLayout. Our old friend.

All Grown Up

Today, I bit the bullet and bought the everydaycoder.com domain. I also setup my own blog so that I have more control over the look and feel as well as standards compliance. I feel like I just graduated from kindergarten to first grade. It does feel really nice to put my own shinny cool header and customize this site to my heart's content.

My speech (as valedictorian of my recent graduation): I'd first like to thank the little people without which I would not be here today. I'd also like to thank my new school. I love what you've done with the place and I can't wait to learn more.

Ok. I think I've carried the metaphor far enough. Seriously, Blogger was a great home for getting started. But I do like, so far, WordPress and love the new found freedom.

So what do you think? Can anyone guess what computer has the text "Everyday Coder" printed on it in the header? Good luck.

Next »