简体   繁体   English

添加到ListView的删除链接-如何从内部匿名类引用外部匿名类

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

Wicket ListView provide removeLink to add with ListItem. Wicket ListView提供removeLink与ListItem添加。 The implementation of the removeLink method in the source of ListView class is: ListView类的源代码中的removeLink方法的实现是:

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 : 现在,如果我添加一个到ListItem的链接并按上述方法覆盖onClick()方法并为其添加更多功能,那么如何重新定义此代码段:

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

As instantiation of ListView object is done by Anonymous Class of ListView and same for the Link. 由于ListView对象的实例化是由ListView的匿名类完成的,因此对于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? 用Java是可能的吗?

You can use Component.findParent(class) : 您可以使用Component.findParent(class)

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

Or you could just call super.onClick() in your onClick() method. 或者,您可以只在onClick()方法中调用super.onClick()

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. 我相信下面的代码会起作用-只要声明的变量为final,就可以从内部类访问声明的变量,因此您可以声明一个包含ListView实例的变量,并从内部类使用它,如下面的示例中所做的那样。 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. 请记住,该变量必须声明为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. 根据经验,如果您的类在方法主体中具有两个以上的方法或五行以上的代码,则最好使其成为一个适当的命名类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM