Listener for Vaadin Grid to enter / exit edit mode?

I would like to enable / disable some checkboxes when the vaadin grid goes into edit mode.

Unfortunately, I could not find the right hook. The closest I got is a SelectionListener, but it does not match the editing rule correctly.

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

Is there a way to get an event when the grid goes into edit mode?

+4
source share
1 answer

For Vaadin 8

For Vaadin 8, you can expand Gridand return a custom one Editor(as far as I saw, there is currently no listener to capture the launch event).

, ( ), , .


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));
    }
}
+1

Source: https://habr.com/ru/post/1623088/


All Articles