开发者

Add remove link to ListView - how to refer outer anonymous class from inner anonymous class

Wicket ListView provide removeLink to add with ListItem. The implementation of the removeLink method in the source of ListView class is:

public final Link removeLink(final String id, final ListItem item)
{
    return new Link(id)
    {
        private static final long serialVersionUID = 1L;

        /**
         * @see org.apache.wicket.markup.html.link.Link#onClick()
         */
        public void onClick()
        {
            addStateChange(new Change()
            {
                private static final long serialVersionUID = 1L;

                final int oldIndex = getList().indexOf(item.getModelObject());
                final Object removedObject = item.getModelObject();

                public void undo()
                {
                    getList().add(oldIndex, removedObject);
                }

            });

            item.modelChanging();

            // Remove item and invalidate listView
            getList().remove(item.getModelObject());

            ListView.this.modelChanged();
            ListView.this.removeAll();
        }
    };
}

Now if I add a Link to the ListItem and Override the onClick() method as above and add some more functionality to it, how can I redefine this snippet :

ListView.this.modelChanged();
ListView.this.removeAll();

As instantiation of ListView object is done by Anonymous Class of ListView and same for the Link.

add(new ListView("listId", list) {
    protected void populateItem(ListItem item) {
         开发者_如何学编程item.add(new Link("linkId") {
             public void onClick() {
                 // how can I define
                 // ListView.this.modelChanged();
                 // ListView.this.removeAll();
                 // here?
             }
         });
    }
});

That is how to refer outer anonymous class from inner anonymous class? Is calling method of anonymous outer class (although it is inner) from anonymous inner class of that outer class in general? Is is possible in Java?


You can use Component.findParent(class):

ListView<?> listView = findParent(ListView.class);
listView.modelChanged();
listView.removeAll();

Or you could just call super.onClick() in your onClick() method.


I believe the code below will work - declared variables are accessible from the inner class as long as they are declared final, so you can declare a variable that holds the ListView instance and use it from your inner class, as I did in the example below. Please note that I didn't compile this code, but I see no reason for it not to work. Just remember that the variable must be declared final.

add(new ListView("listId", list) {
    protected void populateItem(ListItem item) {
         final ListView lv = this;
         item.add(new Link("linkId") {
             public void onClick() {
                 lv.modelChange();
                 lv.removeAll();
             }
         });
    }
});


The simple answer is: don't use nested anonymous classes. They're an absolute nightmare to read and to maintain anyway. As a rule of thumb, if your class is going to have more than two methods or more than five lines of code in the method bodies, it is better to make it a proper, named class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜