GWT:处理复合单元格中的事件 [英] GWT: handling event in composite cell

查看:94
本文介绍了GWT:处理复合单元格中的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CompositeCell让我们使用Java自定义GWT中表格单元格的内容。我们可以将几乎任何其他组件放在表格的单元格中,并按照我们的需要进行布局。问题是,如果我们使用html标签将CompositeCell的布局定义为另一个表(请参阅CompositeCell匿名类实现,请参阅下文),我们将松散单元组件的事件处理:(。



运行下面的代码,当我们点击单元格的按钮时,会实现对事件处理响应的弹出式窗口IF我们对CompositeCell匿名实现进行评论。



我一直在调试CompositeCell.onBrowserEvent(Context,Element,C,NativeEvent,ValueUpdater),因为我认为使用HTML表格标签的单元格布局的定义打破了GWT窗口小部件层次结构中的事件链,但并没有成功至今。



备注:代码的注释和未注释版本都实现了相同的GUI布局,这个例子只是为了表明我们在定制单元格内容时放松事件处理。

  public class ActionCellTest实现EntryPoint {

private static final String SERVER_ERROR =+尝试联系服务器时发生错误。请检查您的网络
+连接,然后重试。;

private final GreetingServiceAsync greetingService = GWT.create(GreetingService.class);
$ b $ public void onModuleLoad(){
CellTable< Person>>>> cells = new LinkedList< HasCell< HasCell< Person>> cellTable< ActionCellTest.Person>();

; Person,?>>();
cells.add(new HasCellImpl(first name,new ActionCell.Delegate< Person>(){

@Override
public void execute(Person object){
Window.alert(object.getFirstName());
}
}));
cells.add(new HasCellImpl(last名称,新的ActionCell.Delegate< ActionCellTest.Person>(){

@Override
public void execute(Person object){
Window.alert(object.getLastName() );
}
}));

CompositeCell< Person> cell = new CompositeCell< Person>(cells){

@Override
public void render(Context context,Person value,SafeHtmlBuilder sb){
sb.appendHtmlConstant(<表>< TBODY>< TR>中); (HasCell< Person,?> hasCell:cells){
render(context,value,sb,hasCell);
}
sb.appendHtmlConstant(< / tr>< / tbody>< / table>);
}

@覆盖
保护< X> void render(Context context,Person value,SafeHtmlBuilder sb,HasCell< Person,X> hasCell){
Cell< X> cell = hasCell.getCell();
sb.appendHtmlConstant(< td>);
cell.render(context,hasCell.getValue(value),sb);
sb.appendHtmlConstant(< / td>);

$ b @Override
protected Element getContainerElement(Element parent){
return parent.getFirstChildElement()。getFirstChildElement()。getFirstChildElement();
}

};
table.addColumn(new TextColumn< ActionCellTest.Person>(){

@Override
public String getValue(ActionCellTest.Person object){
return object.getFirstName ()++ object.getLastName();
}
},name);

table.addColumn(new Column< Person,Person>(cell){

@Override
public Person getValue(ActionCellTest.Person object){
返回对象;
}
},composite);

LinkedList< Person> data = new LinkedList< ActionCellTest.Person>();
data.add(新人(Amy,Reed));
data.add(新人(Tim,Gardner));
table.setRowData(data);

RootPanel.get()。add(table);
}

private class HasCellImpl实现HasCell< Person,Person> {
private ActionCell< Person> FCELL;
$ b $ public HasCellImpl(String text,Delegate< Person> delegate){
fCell = new ActionCell< Person>(text,delegate);
}

@Override
public Cell< Person> getCell(){
return fCell;
}

@Override
public FieldUpdater< Person,Person> getFieldUpdater(){
return null;
}

@Override
public Person getValue(Person object){
return object;
}
}

私人类Person {
private String fFirstName;
private String fLastName;

public Person(String first,String last){
fFirstName = first;
fLastName = last;
}

public String getFirstName(){
return fFirstName;
}

public String getLastName(){
return fLastName;




解决方案

这是一个已知问题,将在即将到来的GWT 2.5中解决(大约几周): http://code.google.com/p/google-web-toolkit/issues/detail?id=5714

(in平均的时间,你可以跑掉干线或尝试backporting更改)

CompositeCell let us customize the content of a table cell's in GWT using Java. We can put almost any other group of widget within the table's cell and layout them as we want. Problem is that if we used the html tags to define the layout of the CompositeCell as yet another table (see CompositeCell anonymous class implementation bellow) we loose the event handling for the components of the cell :(.

Running the following code, when we click in the buttons of the cell will realize the popup in response of the event handling IF WE COMMENT the CompositeCell anonymous implementation.

I've been debugging CompositeCell.onBrowserEvent(Context, Element, C, NativeEvent, ValueUpdater) because i think that the definition of the cell layout using HTML table tags breaks the event chain within GWT widgets hierarchy but haven't been successful so far.

Remark: both commented and uncommented versions of the code realize the same GUI layout. This example just intent to show that we loose event handling when customizing cell's content.

public class ActionCellTest implements EntryPoint {

    private static final String SERVER_ERROR = "An error occurred while " + "attempting to contact the server. Please check your network "
            + "connection and try again.";

    private final GreetingServiceAsync greetingService = GWT.create(GreetingService.class);

    public void onModuleLoad() {
        CellTable<Person> table = new CellTable<ActionCellTest.Person>();

        final List<HasCell<Person, ?>> cells = new LinkedList<HasCell<Person, ?>>();
        cells.add(new HasCellImpl("first name", new ActionCell.Delegate<Person>() {

            @Override
            public void execute(Person object) {
                Window.alert(object.getFirstName());
            }
        }));
        cells.add(new HasCellImpl("last name", new ActionCell.Delegate<ActionCellTest.Person>() {

            @Override
            public void execute(Person object) {
                Window.alert(object.getLastName());
            }
        }));

        CompositeCell<Person> cell = new CompositeCell<Person>(cells) {

            @Override
            public void render(Context context, Person value, SafeHtmlBuilder sb) {
                sb.appendHtmlConstant("<table><tbody><tr>");
                for (HasCell<Person, ?> hasCell : cells) {
                    render(context, value, sb, hasCell);
                }
                sb.appendHtmlConstant("</tr></tbody></table>");
            }

            @Override
            protected <X> void render(Context context, Person value, SafeHtmlBuilder sb, HasCell<Person, X> hasCell) {
                Cell<X> cell = hasCell.getCell();
                sb.appendHtmlConstant("<td>");
                cell.render(context, hasCell.getValue(value), sb);
                sb.appendHtmlConstant("</td>");
            }

            @Override
            protected Element getContainerElement(Element parent) {
                return parent.getFirstChildElement().getFirstChildElement().getFirstChildElement();
            }

        };
        table.addColumn(new TextColumn<ActionCellTest.Person>() {

            @Override
            public String getValue(ActionCellTest.Person object) {
                return object.getFirstName() + " " + object.getLastName();
            }
        }, "name");

        table.addColumn(new Column<Person, Person>(cell) {

            @Override
            public Person getValue(ActionCellTest.Person object) {
                return object;
            }
        }, "composite");

        LinkedList<Person> data = new LinkedList<ActionCellTest.Person>();
        data.add(new Person("Amy", "Reed"));
        data.add(new Person("Tim", "Gardner"));
        table.setRowData(data);

        RootPanel.get().add(table);
    }

    private class HasCellImpl implements HasCell<Person, Person> {
        private ActionCell<Person> fCell;

        public HasCellImpl(String text, Delegate<Person> delegate) {
            fCell = new ActionCell<Person>(text, delegate);
        }

        @Override
        public Cell<Person> getCell() {
            return fCell;
        }

        @Override
        public FieldUpdater<Person, Person> getFieldUpdater() {
            return null;
        }

        @Override
        public Person getValue(Person object) {
            return object;
        }
    }

    private class Person {
        private String fFirstName;
        private String fLastName;

        public Person(String first, String last) {
            fFirstName = first;
            fLastName = last;
        }

        public String getFirstName() {
            return fFirstName;
        }

        public String getLastName() {
            return fLastName;
        }
    }
}

解决方案

This is a known issue which will be fixed in the upcoming GWT 2.5 (a matter of weeks): http://code.google.com/p/google-web-toolkit/issues/detail?id=5714
(in the mean time, you can run off trunk or try backporting the change)

这篇关于GWT:处理复合单元格中的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆