4
我想在vaadin網格進入編輯模式時啓用/禁用某些複選框。Vaadin Grid的進入/離開編輯模式的監聽器?
不幸的是我找不到正確的鉤子。我得到的最接近的是SelectionListener,但它與編輯模式不匹配。
grid.addSelectionListener(new SelectionListener() { ... });
當網格進入編輯模式時,有沒有什麼辦法讓事件發生?
我想在vaadin網格進入編輯模式時啓用/禁用某些複選框。Vaadin Grid的進入/離開編輯模式的監聽器?
不幸的是我找不到正確的鉤子。我得到的最接近的是SelectionListener,但它與編輯模式不匹配。
grid.addSelectionListener(new SelectionListener() { ... });
當網格進入編輯模式時,有沒有什麼辦法讓事件發生?
對於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));
}
}