Dan
Dan

Reputation: 11183

Anonymous inner class in groovy

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"}
}

Upvotes: 3

Views: 5165

Answers (5)

Peter Niederwieser
Peter Niederwieser

Reputation: 123910

Groovy 1.7 and above support anonymous inner classes. See groovy 1.7 release notes.

Upvotes: 0

Igor
Igor

Reputation: 41

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

Upvotes: 1

Raffael Schmid
Raffael Schmid

Reputation: 339

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/

Upvotes: 0

Pascal Thivent
Pascal Thivent

Reputation: 570385

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.

[...]

Upvotes: 1

OscarRyz
OscarRyz

Reputation: 199234

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

Groovy Alternatives to Inner Classes

Upvotes: 1

Related Questions