Monday, August 7, 2006

Swing Date Pickers

Back in April I looked around for a good open source Swing date picker for a big Swing project. I found 6 and I actually tried them all. But in the end we simply stayed with JSpinner components.

These were the must-have requirements:
1. support for typing dates directly (this is, without opening a pop-up)
2. ability to limit the selectable dates with a lower and/or upper boundary
3. good event mechanism

And some nice-to have requirements:
4. visual mouse selection
5. support for multiple date formats
6. look good
7. further limitation of selectable days, for example no weekend days

These were the date pickers I tried:
- JCommon
- MDateSelector
- SwingX's date picker
- UICollection's date picker
- JCalendar
- And there was a sixth, but I can not find it back anymore.

None of these met the must-haves. In particular requirement #1, keyboard support, is only present in the the SwingX component. In contrast good old JSpinner supports #1-#3 splendidly, but fails miserably on #4-#7.

However, my search did seem to have some result.

It started with comment on an introduction on the SwingX JXDatePicker by R.J. Lorimer on JavaLobby. This guy writes good stuff. Then I thought, why not ask on the SwingX forum directly. So I wrote this message in search of more features. The message was followed by a lively discussion in search of requirements.

It seems that the SwingX guys picked it up and are now working on the final killer Swing date picker component!

Sunday, August 6, 2006

The Pragmatic Programmer - Book review

A colleague from the company that currently hires me, proudly told that they have a copy of the The Pragmatic Programmer for every 3 employees. Having just finished the Spring MVC book, I thought lets see what this is about. So I read the book and here I will convince you to read it as well.

After only a few chapters 2 strong thoughts came up and remained there for most of the rest:
1. What a dull book, I am already doing this stuff. But more importantly:
2. This is Good Stuff, I wish more people would read this book.

Though the book is old (2000) it is still very relevant. It contains a large set of well described programming practices and attitudes. You can read the entire book in any order. From experience I can say that what this book says, will really help you build better code more quickly.

One point I loved, is when the authors write off the construction metaphor (which is so previous century) in favor of the gardening metaphor. 'Gardening' is much better for explaining to business people why you sometimes need to spend time with no visible improvements.

I wondered why I had never seen iContract before, which is I think a hidden gem of the book. Apparently it is only recently resurrected.

As a totally unrelated bonus, I finally understand the use of Linda like technologies (Rinda/JavaSpaces).

Some minor points:
- I only noticed one trend away from this book: things like Active Record makes the section "Code generators" a bit redundant. Of course it can still be useful. Just watch out for software in which you don't need it.
- Exercise 23 states that it is a good idea to set java references to null after you are finished with the object. I disagree. You can consider doing this when you are writing code for a particular (old) JVM version that you happen to know intimately. And even then I would say that your methods are too long.

Anyways, the book does justice to its title; all tips are practical and executable. The more people will use ideas from this book, the less bad software I will have to work with. So here is my request to all colleague programmers: please go and read this book!

Author blogs: Andrew Hunt and David Thomas

Sunday, July 23, 2006

Java 7 improvements

Since now seems the time to request new Java 7 improvements (see for example Bag O' Tricks) here are my requests:
  • Type aliases I really hate having to type code like
    private Map<String, List<StringFormatter<DomainThing>>> map = new HashMap<String, List<StringFormatter<DomainThing>>>();
    It is so non-DRY that my fingers ache every time I see code like this.

    Here is an alternative:

    private class DomainFormatterList :     List<StringFormatter<DomainThing>>;
    private Map<String, DomainFormatterList> map =
        new HashMap<String, DomainFormatterList>();
    Still some repetition, but a lot better already. Any ideas for improvement?
  • A non-static String.format method.
    How often did I start with a literal String only to find out I had to move the cursor back to the left and put "String.format(" in front of it? It would have been easier if the format method were an instance method. Compare:
    String.format("Message for %d %s items", 5, "red");
    "Message for %d %s items".format(5, "red");
Please share you thoughts.

Saturday, July 22, 2006

Expert Spring MVC and Web Flow - Book review

I just finished another book: Expert Spring MVC and Web Flow. This time I really enjoyed reading the book. The tutorial and the example applications distributed with Spring are excellent, but to get a good feel for the complete Spring MVC this book is a gem.

Here are some of its strong points:
- The book has clear subjects per chapter (an obvious requirement, but not always met).
- Your time is not wasted; the language is concise without being to cryptic. Really, the books reads as if it has been reviewed and corrected by at least 30 knowledgeable persons.
- You're not coerced in how you must use Spring MVC, but you're told how to make the appropriate choices from the options Spring MVC provides.
- The book not only shows you how stuff works, but also why, learning you valuable design principles that stretch way beyond Spring MVC.
- The appendix on DWR integration is short but very sweet.

Some minor nitpicking:
- The books regularly refers to Spring 1.3, though by the time of writing it was already known that version 1.2 would be followed by 2.0 (the chapters on Spring Web Flow have this correct).
- There is no information on the new form tag libraries.
- There is no mention of Spring Portlet MVC, except for a short reference in the chapters on Spring Web Flow. (Note that you'll find switching from Spring MVC to Spring Portlet MVC quite easy.)

If you are considering learning another web application framework: buy this book.

Tuesday, July 11, 2006

Writing CSV from Java

I recently tried to find a open source Java CSV writer released under a commercially friendly license. To my surprise I could not find one.

CSVs are still the easy solution when you want to export to any spreadsheet program. The only big problem is getting it right for Excel as Microsoft has its own opinion about CSV.

Anyway, since I did not find any, I wrote my own. It has the following features:
- streaming output (big files are no problem)
- uses Java5 printf formatting for flexible column formats
- settable separation character, locale and encoding
- verifies numer of columns

I also wrote a View implementation for use in a Spring MVC environment.

Here are some possible improvements:
- more automatic support for different Excel versions (dependent on locale)
- introduce a CsvWriterFactory to enable easier Spring wiring

I don't think my employer would minds if this went opensource. For example with an Apache license. Anybody interested?

Update 2007-10-23: I think our troubles are finally over: meet Super CSV!

Thursday, June 29, 2006

Portlet anyone?

Today I have been trying to get a portlet to work. Without success. I could not believe it could be so difficult to deploy this stuff. How can anyone stand this? It's no wonder people run away to Ruby on Rails.

Example: The error message is "NullPointerException".
Translation: please remove log4j.jar from your WEB-INF/lib folder.

Another example: "The portlet.xml could not be found". I am quite sure the file is there! Cause is yet to be found. Some pointers I found told me to check 4 other files. I did, no idea yet what could be wrong there.

Please spare me from more JCP junk.

(For those interested: I tried to run the Spring MVC portlet example in Pluto 1.0.1. I made one change: the name of the application.)

Update I know, I know. It is not fair to criticize open source projects. There are actually people that put their heart in this kind of stuff, and they deserve appraisal for it. Unfortunately I am now in the situation where I have no choice and actually have to use an under documented Pluto version.

This morning I found I overlooked yet another Tomcat log file (localhost.log) in which there were clear class not found exceptions. These provided enough pointers to get things running pretty quickly.

Saturday, June 17, 2006

Spring One

The fast facts:
  • 50 presentations
  • 400+ visitors from over 24 countries
  • Spring 2.0 to be released very soon
    • excellent JPA support
    • AspectJ fully integrated (without special compiler)
    • finally a form tag library in Spring MVC
    • improved xml configuration
    • asynchronous JMS facilities
    • many many small fixes and improvements
  • word count
    • EJB3: 0 times
    • Java Persistence API (JPA): zillion times
  • BEA is building WebLogic 9 series on Spring
  • Spring in use in ultra-safety focused companies like Voca
The last 2 days I have been to the first ever Spring-One conference in Antwerp, Belgium. It was an exciting experience to see all the core-developers of this excellent piece of software in one location. Unlike at the JBoss World conference in Barcelona, Spain half a year ago, I felt a friendly and respectful atmosphere from all speakers. The enthusiasm was actually contagious and let me to believe there is still lots of hope for the Java environment.

Remarkable is that besides the obvious talks about upcoming Spring 2.0 there was also quite some focus on Domain Driven Design or Rich Domain Modeling. Perhaps Interface 21, the company that employs many of the important Spring developers, wants to emphasize that they are very capable in providing advanced architectural consulting services.

The best practical I have seen was the JPA testing demonstration by Rod Johnson. With some black class-loader magic Spring makes in possible to quickly test JPA based DAOs and repositories against a database. And by quick I mean quick: several hundreds of tests in only 5 seconds on a simple laptop.

Though not all presentations were great, most were. I think I got a lot of value being at the conference.

Update: Chris May has a nice and extensive blog on Spring One.

Presentation materials are available from the Spring one website.