当vaadin网格进入编辑模式时,我想启用/禁用一些复选框。
不幸的是我找不到正确的钩子。我得到的最接近的是SelectionListener,但它与编辑模式不匹配。
grid.addSelectionListener(new SelectionListener() { ... } );
当网格进入编辑模式时,是否有任何方法可以获取事件?
答案 0 :(得分:1)
对于 Vaadin 8 ,您可以扩展Grid
并返回自定义Editor
(据我所知,当前没有实现用于捕获启动事件的侦听器) 。
此外,您可以添加取消&保存监听器(已由框架实现)以查看编辑器是否已关闭。
<强> 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 ,您可以覆盖方法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));
}
}