使用GWT嵌套编辑器会导致类型不匹配错误

时间:2014-07-11 12:02:09

标签: gwt gwt-platform

我正在尝试让嵌套编辑器在GWT / GWTP中工作 我有一个没有嵌套的例子(感谢你们) 当我尝试在此视图中添加子编辑器时,出现此错误:

  

... ContainerEditorDialogView_BinderImpl.java”   第270行:类型不匹配:无法从Element转换为GroupListEditor

当我尝试添加这样的子编辑器时会发生这种情况:

public class ContainerEditorDialogView extends
        PopupViewWithUiHandlers<ContainerEditorDialogUiHandlers> implements
        ContainerEditorDialogPresenterWidget.MyView, Editor<ContainerDto> {
    interface Binder extends UiBinder<PopupPanel, ContainerEditorDialogView> {
    }
    public interface Driver extends SimpleBeanEditorDriver<ContainerDto, ContainerEditorDialogView> {
    }

    @UiField
    TextBox uuid;   
    @UiField
    TextBox name;   
    @UiField
    TextBox groups;
    @UiField
    GroupListEditor groupListEditor;
 ...

这是我的GroupListEditor:

public class GroupListEditor extends Composite implements IsEditor<ListEditor<String, GroupItemEditor>> {

  private static StringListEditorUiBinder uiBinder = GWT.create(StringListEditorUiBinder.class);

  interface StringListEditorUiBinder extends UiBinder<Widget, GroupListEditor> {}

  private class StringItemEditorSource extends EditorSource<GroupItemEditor> {
    @Override
    public GroupItemEditor create(final int index) {
      GroupItemEditor subEditor = new GroupItemEditor();
      groups.insert(subEditor, index);
      subEditor.addDeleteHandler(new EditorDeleteEvent.EditorDeleteHandler() {
        public void onEditorDeleteEvent(EditorDeleteEvent event) {
          remove(index);
        }
      });
      return subEditor;
    }    
    @Override
    public void dispose(GroupItemEditor subEditor) {
      subEditor.removeFromParent(); 
    }
    @Override
    public void setIndex(GroupItemEditor editor, int index) {
      groups.insert(editor, index);
    }
  }  

  private ListEditor<String, GroupItemEditor> editor = ListEditor.of(new StringItemEditorSource());

  @UiField 
  FlowPanel pWidget;

  @UiField 
  PushButton bAdd;

  @UiField 
  FlowPanel groups;

  @UiField 
  HTML listName;

  public GroupListEditor() {
    initWidget(uiBinder.createAndBindUi(this));
  }

  public void setName(String s) {
    if (s == null) {
      listName.setHTML("");
    } else {
      listName.setHTML(s);
    }
  }

  @UiHandler("bAdd")
  void onBAddClick(ClickEvent event) {
    add();
  }

  private void add() {
    String s = "";
    editor.getList().add(s);
  }

  @Override
  public ListEditor<String, GroupItemEditor> asEditor() {
    return editor;
  }

  private void remove(final int index) {
    editor.getList().remove(index);
  }

}

及其布局:

<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
        xmlns:g="urn:import:com.google.gwt.user.client.ui">
        <g:FlowPanel ui:field="pWidget">
                <g:HorizontalPanel>
                        <g:PushButton text="Add" ui:field="bAdd"/>
                        <g:Cell verticalAlignment="ALIGN_MIDDLE">
                                <g:HTML wordWrap="true">&nbsp;Todos</g:HTML>
                        </g:Cell>
                </g:HorizontalPanel>
                <g:HTML ui:field="listName"/>
                <g:FlowPanel ui:field="groups"/>
        </g:FlowPanel>
</ui:UiBinder> 

项目编辑:

public class GroupItemEditor extends Composite implements LeafValueEditor<String> {

  interface StringItemEditorUiBinder extends UiBinder<Widget, GroupItemEditor> {}

  private static StringItemEditorUiBinder uiBinder = GWT.create(StringItemEditorUiBinder.class);

  @UiField 
  TextBox tbvalue;

  @UiField 
  PushButton bDelete;

  public GroupItemEditor() {
    initWidget(uiBinder.createAndBindUi(this));
  }

  @UiHandler("bDelete")
  void onBDeleteClick(ClickEvent event) {
    fireDeleteEvent();
  }

  private void fireDeleteEvent() {
    //fireEvent(new EditorDeleteEvent());
  }

  public final HandlerRegistration addDeleteHandler(EditorDeleteEvent.EditorDeleteHandler handler) {
    return addHandler(handler, EditorDeleteEvent.TYPE);
  }

  @Override
  public void setValue(String value) {
    tbvalue.setValue(value);
  }

  @Override
  public String getValue() {
    return tbvalue.getValue().trim();
  }

其布局:

<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
        xmlns:g="urn:import:com.google.gwt.user.client.ui" xmlns:p1="urn:import:com.google.gwt.editor.ui.client">
        <ui:style>
                .important {
                        font-weight: bold;
                }
        </ui:style>
        <g:FlowPanel>
                <g:HorizontalPanel>
                        <g:TextBox ui:field="tbvalue"/>
                        <g:HTML wordWrap="true">&nbsp;</g:HTML>
                        <g:Cell verticalAlignment="ALIGN_MIDDLE">
                                <g:PushButton text="X" ui:field="bDelete"/>
                        </g:Cell>
                </g:HorizontalPanel>

        </g:FlowPanel>
</ui:UiBinder> 

编辑:这是ContainerEditorDialogView.ui.xml

<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent">
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
    xmlns:g="urn:import:com.google.gwt.user.client.ui">

    <g:DialogBox modal="true">
        <g:caption>
            <b>Local dialog box example</b>
        </g:caption>
        <g:HTMLPanel>
            Container Settings:
            <table>
                <tr>
                    <td>
                        UUID:
                    </td>
                    <td>
                        <g:TextBox ui:field="uuid" />
                    </td>
                </tr>
                <tr>
                    <td>
                        Container Name:
                    </td>
                    <td>
                        <g:TextBox ui:field="name" />
                    </td>
                </tr>

                <tr>
                    <td>
                        Groups (Editor):
                    </td>
                    <td>
                        <GroupListEditor ui:field="groupListEditor" />
                    </td>
                </tr>

                <tr>
                    <td>
                        Groups:
                    </td>
                    <td>
                        <g:TextBox ui:field="groups" />
                    </td>
                </tr>
                <tr>
                    <td>
                        IMEI:
                    </td>
                    <td>
                        <g:TextBox ui:field="imei" />
                    </td>
                </tr>
                <tr>
                    <td>
                        Type:
                    </td>
                    <td>
                        <g:TextBox ui:field="type" />
                    </td>
                </tr>
                <tr>
                    <td>
                        <g:Button ui:field="okButton">Ok</g:Button>
                    </td>
                    <td>
                        <g:Button ui:field="cancelButton">Cancel</g:Button>
                    </td>
                </tr>               
            </table>            

        </g:HTMLPanel>
    </g:DialogBox>
</ui:UiBinder>

您是否知道可能导致此错误的原因是什么? 我能找到的所有信息都是三年左右......

谢谢:)

1 个答案:

答案 0 :(得分:2)

<GroupListEditor ui:field="groupListEditor" />

当你打算将它作为一个小部件时,它应该在它前面有一个命名空间,就像其他小部件一样。该名称空间应该在GroupListEditor所在的包的顶部定义。例如,如果它是pack.age.to.my.client.widgets.GroupListEditor,那么这将是:

<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder"
    xmlns:g="urn:import:com.google.gwt.user.client.ui"
    xmlns:my="urn:import:pack.age.to.my.client.widgets">

...

    <my:GroupListEditor ui:field="groupListEditor" />

你可以根据自己的意愿制作命名空间,为了举个例子,我使用了'my'。