public class KeywordListEditor extends KeywordListEditor_Roo_Gwt {

  @UiField FlowPanel container;

  @UiField(provided = true)
  @Ignore
  ValueListBox<KeywordProxy> picker =
      new ValueListBox<KeywordProxy>(
          medizin.client.managed.ui.KeywordProxyRenderer.instance(),
          new com.google.gwt.requestfactory.ui.client.EntityProxyKeyProvider<KeywordProxy>());

  @UiField Button add;

  @UiField HTMLPanel editorPanel;

  @UiField Button clickToEdit;

  @UiField HTMLPanel viewPanel;

  @UiField Label viewLabel;

  @UiField Style style;

  boolean editing = false;

  private List<KeywordProxy> values;

  private final List<KeywordProxy> displayedList;

  public KeywordListEditor() {
    initWidget(GWT.<Binder>create(Binder.class).createAndBindUi(this));
    Driver driver = GWT.<Driver>create(Driver.class);
    ListEditor<KeywordProxy, NameLabel> editor = ListEditor.of(new NameLabelSource());
    ListEditor<KeywordProxy, NameLabel> listEditor = editor;
    driver.initialize(listEditor);
    driver.display(new ArrayList<KeywordProxy>());
    displayedList = listEditor.getList();
    editing = false;
  }

  @UiHandler("add")
  public void addClicked(ClickEvent e) {
    displayedList.add(picker.getValue());
    viewLabel.setText(makeFlatList(displayedList));
  }

  @UiHandler("clickToEdit")
  public void clickToEditClicked(ClickEvent e) {
    toggleEditorMode();
  }

  @Override
  public void flush() {}

  @Override
  public List<medizin.client.managed.request.KeywordProxy> getValue() {
    if (values == null && displayedList.size() == 0) {
      return null;
    }
    return new ArrayList<KeywordProxy>(displayedList);
  }

  public void onLoad() {
    makeEditable(false);
  }

  @Override
  public void onPropertyChange(String... strings) {}

  public void setAcceptableValues(Collection<KeywordProxy> proxies) {
    picker.setAcceptableValues(proxies);
  }

  @Override
  public void setDelegate(EditorDelegate<List<KeywordProxy>> editorDelegate) {}

  @Override
  public void setValue(List<KeywordProxy> values) {
    this.values = values;
    makeEditable(editing = false);
    if (displayedList != null) {
      displayedList.clear();
    }
    if (values != null) {
      for (KeywordProxy e : values) {
        displayedList.add(e);
      }
    }
    viewLabel.setText(makeFlatList(values));
  }

  private void makeEditable(boolean editable) {
    if (editable) {
      editorPanel.setStylePrimaryName(style.editorPanelVisible());
      viewPanel.setStylePrimaryName(style.viewPanelHidden());
      clickToEdit.setText("Done");
    } else {
      editorPanel.setStylePrimaryName(style.editorPanelHidden());
      viewPanel.setStylePrimaryName(style.viewPanelVisible());
      clickToEdit.setText("Edit");
    }
  }

  private String makeFlatList(Collection<KeywordProxy> values) {
    return CollectionRenderer.of(medizin.client.managed.ui.KeywordProxyRenderer.instance())
        .render(values);
  }

  private void toggleEditorMode() {
    editing = !editing;
    makeEditable(editing);
  }

  interface Binder extends UiBinder<Widget, KeywordListEditor> {}

  interface Driver
      extends RequestFactoryEditorDriver<List<KeywordProxy>, ListEditor<KeywordProxy, NameLabel>> {}

  class NameLabel extends Composite implements ValueAwareEditor<KeywordProxy> {

    final Label nameEditor = new Label();

    public NameLabel() {
      this(null);
    }

    public NameLabel(EventBus eventBus) {
      initWidget(nameEditor);
    }

    public void flush() {}

    @Override
    public void onPropertyChange(String... strings) {}

    @Override
    public void setDelegate(EditorDelegate<KeywordProxy> editorDelegate) {}

    @Override
    public void setValue(KeywordProxy proxy) {}
  }

  interface Style extends CssResource {

    String editorPanelHidden();

    String editorPanelVisible();

    String viewPanelHidden();

    String viewPanelVisible();
  }

  private class NameLabelSource extends EditorSource<NameLabel> {

    @Override
    public NameLabel create(int index) {
      NameLabel label = new NameLabel();
      container.insert(label, index);
      return label;
    }

    @Override
    public void dispose(NameLabel subEditor) {
      subEditor.removeFromParent();
    }

    @Override
    public void setIndex(NameLabel editor, int index) {
      container.insert(editor, index);
    }
  }
}
 private String makeFlatList(Collection<KeywordProxy> values) {
   return CollectionRenderer.of(medizin.client.managed.ui.KeywordProxyRenderer.instance())
       .render(values);
 }