gridview – Vaadin Grid的监听器进入/退出编辑模式?

当vaadin网格进入编辑模式时,我想启用/禁用一些复选框.

不幸的是我找不到正确的钩子.我得到的最接近的是SelectionListener,但它与编辑模式不匹配.

grid.addSelectionListener(new SelectionListener() { ... } );

当网格进入编辑模式时,有没有办法获得事件?

最佳答案 对于Vaadin 8

对于Vaadin 8,您可以扩展Grid并返回一个自定义编辑器(据我所知,目前还没有实现用于捕获start事件的侦听器).

此外,您可以添加取消&保存监听器(已由框架实现)以查看编辑器是否已关闭.

CustomEditor.java

public class CustomEditor<T> extends EditorImpl<T> {

  private EventRouter eventRouter = new EventRouter();

  public CustomEditor(PropertySet<T> propertySet) {
    super(propertySet);
  }

  public Registration addStartListener(EditorStartListener<T> listener) {
    return eventRouter.addListener(EditorStartEvent.class, listener,
                                   EditorStartListener.class.getDeclaredMethods()[0]);
  }

  @Override
  protected void doEdit(T bean) {
    super.doEdit(bean);
    fireToggleEvent(bean);
  }

  private void fireToggleEvent(T bean) {
    eventRouter.fireEvent(new EditorStartEvent<>(this, bean));
  }
}

EditorStartListener.java

@FunctionalInterface
public interface EditorStartListener<T> extends Serializable {

  void onEditorStart(EditorStartEvent<T> event);
}

EditorStartEvent.java

public class EditorStartEvent<T> extends EventObject {

  private T bean;

  public EditorStartEvent(Editor<T> editor, T bean) {
    super(editor);
    this.bean = bean;
  }

  @SuppressWarnings("unchecked")
  @Override
  public Editor<T> getSource() {
    return (Editor<T>) super.getSource();
  }

  public Grid<T> getGrid() {
    return getSource().getGrid();
  }

  public T getBean() {
    return bean;
  }
}

CustomGrid.java

public class CustomGrid<T> extends Grid<T> {

  private PropertySet<T> propertySet;

  public CustomGrid() {
    super();
  }

  // Other constructors omitted for clarity

  @Override
  protected void setPropertySet(PropertySet<T> propertySet) {
    this.propertySet = propertySet;
    super.setPropertySet(propertySet);
  }

  @Override
  protected Editor<T> createEditor() {
    return new CustomEditor<T>(propertySet);
  }
}

用法:

Grid<Person> grid = new CustomGrid<>();
CustomEditor<Person> editor = (CustomEditor<Person>) grid.getEditor();
editor.setEnabled(true);
editor.addStartListener(event -> System.out.println("Editor started!"));

对于Vaadin 7

对于Vaadin 7,您可以覆盖方法doEditItem()并使用与上面相同的方法在此处添加侦听器:

EditStartListener.java

public interface EditStartListener extends Serializable {

    public static final Method EDIT_START_METHOD = ReflectTools.findMethod(EditStartListener.class, "editStart", EditStartEvent.class);

    public void editStart(EditStartEvent event);

}

EditStartEvent.java

public class EditStartEvent extends Component.Event {

    public EditStartEvent(Component source) {
      super(source);
    }

    public Grid getGrid() {
      return (Grid) getSource();
    }
}

CustomGrid.java

public class CustomGrid extends Grid {

    public void addEditStartListener(EditStartListener listener) {
      addListener(EditStartEvent.class, listener, EditStartListener.EDIT_START_METHOD);
    }

    @Override
    protected void doEditItem() {
      super.doEditItem();
      fireEvent(new EditStartEvent(this));
    }
}
点赞