Archives for December 2007

WWIG - JFlubber

Let me first state that it is very possible that any of the applications in this series, if reimplemented today in the original implementation language, might see a reduction in code size/simplification due to libraries, methods, or techniques that weren’t available the first time around. That is probably the case with this first example as I am pretty sure that some of the implementation code eventually made its way into the SwingX components.


jFlubber, in its orginal version, was created by Dick Wall (of Java Posse fame) as a tool to mark flub spots during the recording of the podcast and as a way to learn Matisse. In its second iteration, Romain[Guy] injected some bling. That’s the version I’ve recreated in Groovy.

Dick’s Original Version Romain’s version

 

Creating the buttons

Instead of creating each button with a half a dozen method calls as Matisse did, I was able to refactor it into a single function that takes a string and a closure for the ActionListener. 

 

 def createButton(buttonString, action) {
  return swing.button(borderPainted: false, contentAreaFilled: false, focusPainted: false,
    icon: swing.imageIcon(getClass().getResource(baseIconString + buttonString + ’-button.png’)),
    pressedIcon: swing.imageIcon(getClass().getResource(baseIconString + buttonString + ’-button-pressed.png’)),
    actionPerformed: action)
     } 

 

TimeLabel and WoodenContentPane

Because Groovy doesn’t allow inner classes, these had to be broken out of the main form class file. For the time label, thanks to the existence of Painters, I was able to construct a component duplicating the effect using a CompoundPainter :

 

private TimeLabel() {
  compoundPainter = swing.compoundPainter() {
     imagePainter(image: ImageIO.read(getClass().getResource("images/metal.png")),scaleToFit: true)
                 textPainter(id: ’txtPainter’, font: new Font("Arial", Font.PLAIN, 36), fillPaint: Color.white) {
                         shadowPathEffect()
                 }
          }
           setBackgroundPainter(compoundPainter)
} 

 

Only cosmetic changes were made to the WoodenContentPane class file. registerBeanFactory registers the class with the builder so that we can reference it like any other built-in component.

 

swing = new SwingXBuilder()
swing.lookAndFeel(new org.jdesktop.swingx.plaf.nimbus.NimbusLookAndFeel())
swing.registerBeanFactory("woodenPane", WoodenContentPane)
swing.registerBeanFactory("timeLabel", TimeLabel)
... 
timeLabel = swing.timeLabel(text: ’0:00’) 

 

And now for the pièce de résistance, the code to build and layout the GUI. Warning: ymmv if your resolution is less than 1024x768. I’ve not yet found a reliable way to duplicate the efficient resizing from GroupLayout in MigLayout. The spacing is a little bit baked in. A sacrifice made for speed’s sake.

def pane = swing.woodenPane(layout: new MigLayout()) {
  def x = centerObject(261)
  widget(timeLabel, constraints: "id timeLabel, w 261!, h 81!, x " + x)
  widget(startButton, constraints: ’id startButton,x timeLabel.x-5, y timeLabel.y2+10’)
  widget(stopButton, constraints: ’id stopButton, x startButton.x2+45, y startButton.y, x2 timeLabel.x2’)
  flubX = centerObject(flubButton.getSize().getWidth())

  widget(flubButton, constraints: ’id flubButton, x 75, y 150,wrap’)
  scrollPane(constraints: ’id scrollPane,x timeLabel.x+10, x2 timeLabel.x2, y flubButton.y2+10’,                     horizontalScrollBarPolicy:    ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER) {
     widget(flubPointsTextArea)
  }
  widget(saveButton, constraints: ’x startButton.x, y scrollPane.y2+20’)
  widget(clearButton, constraints: ’x stopButton.x, y scrollPane.y2+20, x2 timeLabel.x2’)

}

 

And here’s how the Groovy version looks. Download the tar’d project archive here.

 

What would it look like in Groovy

For the last couple months, I’ve had to take a sabbatical of sorts from Groovy Swing development to focus on my blog redesign and Grails. To get back in the groove, I’ve decided to do (hopefully) a series of ports called "What would it look like in Groovy" or WWIG for short.

What I’m hoping to do is (1) resharpen my Swing skills after the long hiatus and (2) give the community functional applications using Groovy Swing.

I’m generally going to stick with smallish apps that are functional, somewhat easy to port, and preferably already look good.

New version of the Google Chart Plugin is out

The Google Chart API plugin has been updated to version 0.2. 

New in this release:

  • Chart title font size
  • Chart title color
  • Chart legends


The plugin now resides in the Grails plugin repository. Documentation coming soon.

Response to Java vs Ruby

The main problem I have with the blog entry Java vs Ruby other than seeming to be thinly veiled "Java is dying" FUD is that it conveniently oscillates between comparing Ruby to Java, the platform and Java the language wherever it seems to best further their cause.

"The Java Platform is Weakening"
The section states the assertion in the title yet only offers anecdotal evidence citing the rise and decline of C. Then it states that Java "should start to decline." According to TIOBE and the good availability of Java developers, it seems to not be the case.

Once could argue that the platform is strengthening. Grails, a web framework inspired by RoR, allows developers to have an agile, rapid web dev experience without having to leave their Java knowledge behind. Groovy, the language that powers Grails [mostly, with Java bits], has a Java-like syntax and integrates seamlessly in Java and most importantly use Java libraries. Several PepsiCo brands are using Grails for their sites(http://grails.org/PepsiCo+Grails+Sites+Post-Analysis).

The Groovy community has a good relationship with the JRuby team. Some might call it one of a symbiotic nature where JRuby mines Groovy for cool features to implement but I prefer to call it one of mutual respect and inspiration. Joking aside Charlie[Nutter] is active on our mailing lists. For all the hype on the ’Net, there is little if any animosity between our factions.

One of the things that Groovy has that JRuby doesn’t is full integration. A Java class can subclass a Groovy class that subclasses a Java class. In JRuby, a JRuby class can’t yet be used as in Java [though they are working on it].

The rise of Ruby, or some variant thereof, whether real or imagined, doesn’t exactly mean that the Java platform has to die. JRuby will not be a bridge to move CRuby as some has stated. It will stay firmly planted in the Java ecosystem and provide yet another way to target the platform.

Merry Christmas!/Happy Holidays!

Do you MOTU?

I’ve decided to take the plunge and begin the process of a becoming MOTU. For those not familiar with Ubuntu, a MOTU (or Master of the Universe) is someone who is helps make sure the components and packages of the Universe and Multiverse(basically everything that is not officially supported by the core Ubuntu developers) are as bug-free and up-to-date as possible.

MOTUs also work in collaboration with Debian maintainers to forward Ubuntu fixes and merges upstream to Debian and vice versa.

Your progress is determined by your participation and cred. It’s kinda like that episode of the Simpsons where Bart was in that advanced class that had no grades or set lesson plans. You evolve at your own pace. Once your build enough cred and the MOTU council accepts your application, you can create new packages for immediate inclusion in Ubuntu. Before that time, I’ll be able to submit packages but they will need to reviewed and approved by 2 MOTU’s before making it into the Universe repositories.

Why?

• At last count, there are about 4 different packaging efforts for Groovy targeting Debian and Ubuntu. There should be at most 2(Ubuntu and upstream).
• Better collaboration is needed moving forward.
• I’ve been using Ubuntu since Breezy and want a cool Ubuntu.com email addy.
• Changes in Groovy won’t be constrained to any OS’s release schedule.
• I’ve already been preparing Debian packages for the last couple releases. Now that I have the hang of it, it’s about time to make it official.

 

That’s my Chrismakwanzakah gift to the Groovy community. What’s yours?

Grails Google Chart API Plugin

Since it was announced several days ago, I’ve been messing around with Google Chart API and Grails plugins.

Here’s an example of the code to produce a pie chart:

    <% 
        def labels = [’First’,’Second’,’Third’] 
        def colors = [’FF0000’,’00ff00’,’0000ff’] 
        def values = [35,45,10] 
    %> 
       

It produces the image tag:

<img src="http://chart.apis.google.com/chart?chs=400x200&chd=s:v9O&chl=First|Second|Third
      &chco=FF0000,00ff00,0000ff&chf=bg,s,efefef&chtt=Sample Pie Chart&cht=p3" />

That code produces the following pie chart:

Sample Pie Chart Generated by Google Chart API

 

Download: grails-google-chart-0.1.zip Now available in the Grails Plugin Repository ...install with:

 

grails install-plugin google-chart

 

As with the social bookmarks plugin, there are more extensive examples in the plugin itself. Run it with grails run-app to see them. Some of the advanced features aren’t implemented yet like item markers and the like but you are able to use any of the three types of encoding, multiple datasets, all chart types and axis labeling.

Because of the 1,000 call threshold imposed by Google, unless you want to risk your charts not showing up, it wouldn’t be a good idea to use this on a high traffic site since it does a straight call. The next evolution will probably be some sort of caching mechanism based on the dataset, other attributes, or something.

Edit: It’s actually 50,000 calls per day. 

Let me know what y’all think..

 

And then there were three

There are three separate efforts to make blog software utilizing Grails. There is Glen Smith’s (of GroovyBlogs fame) Gravl, Bayu Adji’s Djiblog, and the software powering my blog.

I think I’ll call mine Skribi. It’s Esperanto for "to write."

I think it’s great that multiple people are approaching the same issue. Each will bring different perspectives and solutions to the table. Making blog software also pushes Grails more into the domain of the plebian user, something that other frameworks, that shall remain nameless, do a little bit better than Grails [mostly because they have been around longer].

Grails Social Bookmark Plugin

The problem with social bookmarks is that they oscillate between a desire to inform and neediness. Imagine the blogosphere is a giant supermarket, like a Kroger, Carrefour, or Sainsbury’s*. In the produce section, you have your time sensitive stuff like the news, walk over to the candy aisle and you have the jokes, NSFW, and fluff. Over in the junk food aisle, you have all the stuff that is gratifying for a fleeting moment, like Ruby is sooooo awesome!!!!, Java sucks!!!!, PHP is so l33t, etc.  

People can’t be expected to wander through the aisles to find your blog nestled in with organic peanut butter. The properly used social bookmark link can get you one of those coveted checkout counter slots for a short time, in the hopes that when your time as a featured product is over, folks will come to your aisle and look for you. Place fifteen links on every page, submit every post to every engine yourself, and you will look just like the trash mags.

To that end, the most recent addition to my blog is to add social bookmarking links. So that the effort doesn’t have to been duplicated among the other Grails Blog Software efforts, I’m releasing the functionality as a plugin. There are seven links built in, as you can see at the bottom of this entry. Feel free to modify the taglib to add your own favorites. The three options are text only, image only, or both. The format is basically this:

 <g:bookmark title="${entry.title} permalink="${entry.permalink}" type="image"/>



Run the plugin as a regular Grails app and you will see more taglib specifics on the index.gsp page.

Download: grails-social-bookmarks-0.1.zip

I went back and refactored the taglib to use addtoany.com since they have support for a lot of services and their widget is nice and AJAX-y.

Add this taglib to your application: BookmarkTagLib.groovy

* larger supermarket chains in America, France, and England respectively

Two projects that lived and died before their time

WebOnSwing
WebOnSwing is a project that allows you to migrate Swing applications to a web environment with few modifications. It would be good for Groovy because the Swing(X)Builder could target desktop as well as web applications freeing us from the stranglehold of HTML and CSS. But alas, it wasn’t meant to be. WebOnSwing is dependent mostly on Java 1.3 - 1.4 with a buggy patch for 1.5 and no release plan for 1.6 or higher.

JD4X
JD4X is a window manager for Linux implemented in Java. Back in the day, the idea was probably laughed at. With the upcoming release of Update N, this is seeming like more and more of a good idea. Preloading classes for faster app startup, shared or multiple JVMs, and Groovy UIs on top of that...it would be awesome. The good thing is that if someone decided to take up the task, there is ample documentation and the system is built with 1.4 - 1.5 in mind.

A couple days with Grails

No type of cutover from one system to another ever goes totally as planned. This certainly wasn’t different.

Let me first say that Eatj.com is a great service, I would like the option to not have to put everything in the war or in secondary storage* but I can deal with it if the tradeoff is a private JVM. A private JVM is both a blessing and a curse. It’s good because you can quickly fix small problems and not have to wait for 1A for the host to deploy the WAR file. It is bad for precisely the same reason. Here are the features of this as-yet unnamed software.

Fancy URLs (year/month/day/title and just the title)
RSS and Atom feeds
Comments moderation
Scheduled Posts
Tags
Archive links

The comments moderation mostly worked but needs some tweaking and finally thanks to the new feed Technorati is correctly aggregating my blog. It apparently didn’t like my last CSS change when I was using Nucleus CMS because it aggregated only the comment blocks from that point on. Moving forward, I’m going to work on the backend admin console, integrate S3 services better(maybe with an applet, yes I said an applet), and do some CSS tweaking.

Again, if you got broken links in the past day or so, I truly apologize, think of it as a artifact of using 0.1 software(my blog, not grails).

*One of the higher account levels does allow FTP

Handling Legacy URLs

Given that my blog had about 40 or so entries and dozens of links scattered out in the wild, it was fairly important that the existence of the new project didn’t suddenly break all those links. So URL mapping was a prerequisite from day one.
 
Importing the old data
To avoid having to deal with the pecularities of making MySQL statements runnable on Apache Derby, I chose to export the data from the old blog as XML. Also, operating on XML allows your import to be implementation agnostic. After the export, I ran grails console and wrote a quick script to parse the XML, create objects, and persist them. One word of caution: The setId function on domain classes is merely a convenience method to satisfy the JavaBean spec. The objects will use whatever is the next available value in the sequence and not what you set. To get my entries in the proper order, I did a sort on the publish date. You should also be mindful of the dependencies of the objects and start with the object with no dependencies and move up the line. For me, that meant importing categories first, then entries, then comments(I had separately recreated users).
 
Having my entries added, I opened grails-app/conf/UrlMappings.groovy and tried to add the following:
 

 

"/blog/index.php?itemid=$id" {
     controller = xxxx
     action = xxxx
}   

 

That failed miserably. The problem is that "?" has the context in Groovy to dereference if the variable is not null. After several tries, I figured out the correct solution is this:
 

 

"/blog/index.php" {
     controller = xxxxx
     action = xxxxx
}  

 

The query string, of which itemid is a part, is represented in params so for the accompanying show action, we could use something like:
 

 

def show = {  
     def entry
     if (params.itemid != null)
          entry = Entry.get(params.itemid)
     else entry = Entry.get(params.id)
     [entry : entry]   
}