views:

729

answers:

5

I am looking into the groovy-wicket integration and lack of anonymous inner classes seems to be a problem when writing the event handlers. Is there a groovier way of writing this code

import org.apache.wicket.PageParameters
import org.apache.wicket.markup.html.basic.Label
import org.apache.wicket.markup.html.link.Link
import org.apache.wicket.markup.html.WebPage


/**
 * Homepage
 */
class HomePage extends WebPage {


    public HomePage(final PageParameters parameters) {

        // Add the simplest type of label
        add(new Label("message", "Wicket running!"));   
        def link1 = new ClickHandler("link1") //in java, defined inline
        add(link1);
    }   
}

class ClickHandler extends Link{

    ClickHandler(String id) {
     super(id);
    }

    void onClick(){println "Hi"}
}
A: 

Ermh.. This doesn't look like a "good" alternative, but it seems to be the "official" Groovy alternative:

Groovy Alternatives to Inner Classes

OscarRyz
I am aware of the link, but had no success in following the instructions.
Dan
+1  A: 

I may be wrong but isn't this what the WickeBuilder tries to solve:

The Wicket Builder utility implements a Groovy Builder for constructing Wicket Component trees.

While using the builder makes building Component trees easier and more clear to the reader, the original driver was the fact that Groovy does not allow anonymous inner classes. Wicket relies on overriding methods to provide custom functionality for many Component types. Groovy can be used to code Wicket page classes, but each class that is overridden needs a named class definition. Possible, but clunky.

The WicketBuilder simulates these overrides with named Closures. Closures are, essentially, portable code blocks. Under the hood, the builder creates dynamic class overrides and runs the closures when the named method is called.

[...]

Pascal Thivent
This seems to be the correct answer. Alas, it seems that the projects hasn't seen a lot of activity as of late. Anyway, this problem surely affects other frameworks as well. There is a talk about implementing inner classes in future version of groovy.
Dan
Indeed, Groovy anonymous inner classes are on the Roadmap for 1.8 http://docs.codehaus.org/display/GroovyJSR/Groovy+Roadmap
Pascal Thivent
Anonymous inner classes are due in Groovy 1.7
Don
Indeed, 1.7 as written in the roadmap (1.8 is a typo).
Pascal Thivent
A: 

i actually do not use groovy often, but asked me the same question few month ago. i tried out different approaches

http://rschmid.wordpress.com/2009/05/03/anonymouse-inner-classes-in-groovy/

rschmid
A: 

Groovy 1.7 adds support for anonymous inner classes. You can try beta-1 today. Beta-2 will arrive soon.

Peter Niederwieser
I'm using grails and groovy 1.6.4 is still bundled with 1.2 M3 version of grials. Is there a way to make grails use new version of groovy?
Dan
+1  A: 

Comlete example for groovy 1.7.x and wicket 1.4.x http://wash-inside-out.blogspot.com/2010/08/wicket-and-groovy-integration.html

Igor