Holy crap! I was just perusing the bin directory of my JDK installation and stumbled on a tool called VisualVM. It is a free lightweight profiling tool for the JVM. I can easily find which Java process maps to a specific Java application. Better yet the tool tells me what the PIDs are so that I can easily terminate “rogue” Java processes. VisualVM also provides visualisation tools for monitoring Java Classes, Threads, PermGen, and the Heap. It saves me from having to buy JProfiler or setting up Netbeans to profile my Java applications.
Monthly Archives: May 2009
Building Twitter with Grails in 40 minutes
Last night at the Groovy Group we had Graeme Roche from SpringSource and lead developer of Grails give a webinar titled “Building Twitter with Grails in 40 minutes“. We were all impressed, not only with how Grails leverages Spring and third party plugins but also with the Twitter application that Graeme was building. He gave a new meaning to TDD — Twitter Driven Development. Each new feature that he added allowed him to tweet about it in the application that he was building. It was evident that he enjoys talking about Grails and connecting with user groups, even the flu wasn’t going to keep him away from giving the talk!
May Sydney Groovy Group
I am amazed with how many quality presenters we have had at the Sydney Groovy Group, and the number of project leads that have presented on their Groovy projects.
At the last meeting we got to hear about Griffon from James Williams, one of the project’s lead developers. Griffon is a Grails like framework for developing Swing based desktop applications. I was extremely impressed with Griffon, and how easy it was to create components without having to implement a bunch of event listeners or interfaces that you don’t need or care about. It was also interesting to see how the MVC pattern was applied to a Swing application. It made me wish Griffon was around 10 years ago when I was building Swing applications for Xylogy.
The May Sydney Groovy group will be meeting this Wednesday. Graeme Rocher will be giving a webinar on Grails, plus a run down on the recent GR8 Conference. So you’ll get to hear first hand on where Spring intends to take Groovy and Grails.
If you intend to make it to the meeting then please express your interest in the forums.
IR pen goes commercial
It had to happen, someone has finally mass produced the IR pen so that you can use your Wiimote as an interactive whiteboard. You can buy an IR pen from http://penteractive.us/ for about $8. I sure hope Johnny Chung Lee is getting a cut of this action. Of course if you want to make your own then read my blog posting about making your own IR pen.
Penteractive also sell stands, which makes setting up the Wiimote whiteboard so much easier! Speaking of which, check out IDEO’s multitouch system (at code.google.com) which also supports the Wiimote and IR pen interaction.
I am excited about all this because it makes a Mingle projected story wall so much easier to setup and interact with.
QTip JQuery Plugin
I’ve been using a JQuery tooltip plugin called QTip on my current project. It has that wow factor that blows users away. I highly recommend it.
Improving Django Comments’ User Experience with AJAX
I really like Django. It is not bloated like a lot of other frameworks, and it has a healthy balance between convention and configuration. As a developer I want to be able to use the tools that I want to use, and not be forced into a specific way of doing things. An example of this in Django is the comments framework that is part of django.contrib. The comments framework provides the infrastructure for attaching comments to any domain model in your Django project through content types. It also provides a few spam prevention features that you should consider leveraging, such as a security hash or a hidden honeypot field. However, the default comments form is rather bare, and the workflow for posting a comment requires too many clicks and redirects.

There are some solutions that try to improve the user experience, and do a good job of it, but require hacking the comments framework. I personally don’t like hacking the internals of any framework. Not because I’m scared to, but because I don’t want to inherit any unnecessary maintenance overheads.
Besides, I believe the comments framework does the right thing. It doesn’t try to do too much, and by that I mean the HTML that it generates for the default rendered form and corresponding responses can easily be mashed up as part of the submitting pages DOM. So with a bit of JQuery and AJAX knowhow you can post comments without refreshing or being redirected away from the submitting page.
First I use the utility methods from the comments framework to list all comments for a particular discussion content type, and to display the default comment form immediately after. Note that the comment form is wrapped in div tags with the id “comment_form”. The div will be used to override the block with responses from the server after an ajax post.
<h3>Comment</h3>
{% load comments %}
{% get_comment_form for discussion as form %}
<div id="comment_form">
{% render_comment_form for discussion %}
</div>
In my template (discussion.html) in which I want to allow people to comment on something I add the following to a script block that will insert the JavaScript into the head element of the base template (base.html).
<script type="text/javascript" charset="utf-8">
function bindPostCommentHandler() {
$('#comment_form form input.submit-preview').remove();
$('#comment_form form').submit(function() {
$.ajax({
type: "POST",
data: $('#comment_form form').serialize(),
url: "{% comment_form_target %}",
cache: false,
dataType: "html",
success: function(html, textStatus) {
$('#comment_form form').replaceWith(html);
bindPostCommentHandler();
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
$('#comment_form form').replaceWith('Your comment was unable to be posted at this time. We apologise for the inconvenience.');
}
});
return false;
});
}
$(document).ready(function() {
bindPostCommentHandler();
});
</script>
In line 2 I define a method called bindPostCommentHandler() which performs the ajax call to post a comment and to handle the response when the form submit event is triggered.
In line 3 I remove the preview button from the form as I do not want this functionality. Rich text plugins generally have a preview mode, so I don’t see the need for the server to process a preview request.
In line 4 I bind the ajax post call to the submit event of the comments form. This means when I click on the Post button to submit the form I will trigger an ajax post instead of a normal post to the server.
Line 7 serialises the data from the form input fields using the JQuery serialize() function.
Line 8 specifies the URL to post to. I use the utility method from the comments framework to retrieve this for me, so I don’t need to hard code it to a specific URL. The comment_form_target should retrieve the correct URL for the comments application that you should have configured in your urls.py file. If not then add the following URL route to your urls.py file.
(r'^comments/', include('django.contrib.comments.urls')),
Line 10 specifies that the response will be HTML. This is necessary so that JQuery knows how to parse and handle the response.
Line 11 details the success callback, which is the function that calls when the response is successful. The callback simply replaces the form in the div wrapper with the response. A successful response could either be a “thank you” message for posting a comment, or a form displaying fields with invalid fields. Line 13 is necessary to rebind the bindPostCommentHandler() function to any new form that appears in the div wrapper. If you omit this rebinding, then you will lose ajax posts on successive submits.
Line 15 defines the callback that gets called when the server responds with an http error code. I just override the div wrapper block with an apologetic message.
Finally, line 23 binds the bindPostCommentHandler() function when the page is ready.
As you can see from the above, all you need is some JavaScript to improve the user experience for posting comments using Django’s comments framework. No framework hacking necessary. Note that if you want to further improve this solution, then you can add sliders, fade ins, and fade outs for displaying the responses from the server. I left these out for brevity. I also left out appending the submitted comment to the list of comments, as the above solution will only display the submitted comment after the user performs a GET request after the submit.
