gpt4 book ai didi

gridview - Vaadin Grid 的监听器进入/离开编辑模式?

转载 作者:行者123 更新时间:2023-12-01 04:58:54 26 4
gpt4 key购买 nike

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

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

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

当网格进入编辑模式时,有什么方法可以获取事件?

最佳答案

对于 Vaadin 8

对于 瓦丁 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

对于 瓦丁 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));
}
}

关于gridview - Vaadin Grid 的监听器进入/离开编辑模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34633588/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com