import com.vaadin.shared.Range; | import com.vaadin.shared.Range; | ||||
import com.vaadin.shared.ui.Connect; | import com.vaadin.shared.ui.Connect; | ||||
import com.vaadin.shared.ui.dnd.DropEffect; | import com.vaadin.shared.ui.dnd.DropEffect; | ||||
import com.vaadin.shared.ui.grid.GridDragSourceExtensionRpc; | |||||
import com.vaadin.shared.ui.grid.GridDragSourceExtensionState; | |||||
import com.vaadin.shared.ui.grid.GridDragSourceRpc; | |||||
import com.vaadin.shared.ui.grid.GridDragSourceState; | |||||
import com.vaadin.shared.ui.grid.GridState; | import com.vaadin.shared.ui.grid.GridState; | ||||
import com.vaadin.ui.GridDragSourceExtension; | |||||
import com.vaadin.ui.GridDragSource; | |||||
import elemental.events.Event; | import elemental.events.Event; | ||||
import elemental.json.Json; | import elemental.json.Json; | ||||
/** | /** | ||||
* Adds HTML5 drag and drop functionality to a {@link com.vaadin.client.widgets.Grid | * Adds HTML5 drag and drop functionality to a {@link com.vaadin.client.widgets.Grid | ||||
* Grid}'s rows. This is the client side counterpart of {@link | |||||
* GridDragSourceExtension}. | |||||
* Grid}'s rows. This is the client side counterpart of {@link GridDragSource}. | |||||
* | * | ||||
* @author Vaadin Ltd | * @author Vaadin Ltd | ||||
* @since | * @since | ||||
*/ | */ | ||||
@Connect(GridDragSourceExtension.class) | |||||
public class GridDragSourceExtensionConnector extends | |||||
@Connect(GridDragSource.class) | |||||
public class GridDragSourceConnector extends | |||||
DragSourceExtensionConnector { | DragSourceExtensionConnector { | ||||
private GridConnector gridConnector; | private GridConnector gridConnector; | ||||
protected void sendDragStartEventToServer(Event dragStartEvent) { | protected void sendDragStartEventToServer(Event dragStartEvent) { | ||||
// Start server RPC with dragged item keys | // Start server RPC with dragged item keys | ||||
getRpcProxy(GridDragSourceExtensionRpc.class) | |||||
.dragStart(draggedItemKeys); | |||||
getRpcProxy(GridDragSourceRpc.class).dragStart(draggedItemKeys); | |||||
} | } | ||||
private List<JsonObject> getDraggedRows(Event dragStartEvent) { | private List<JsonObject> getDraggedRows(Event dragStartEvent) { | ||||
DropEffect dropEffect) { | DropEffect dropEffect) { | ||||
// Send server RPC with dragged item keys | // Send server RPC with dragged item keys | ||||
getRpcProxy(GridDragSourceExtensionRpc.class) | |||||
getRpcProxy(GridDragSourceRpc.class) | |||||
.dragEnd(dropEffect, draggedItemKeys); | .dragEnd(dropEffect, draggedItemKeys); | ||||
} | } | ||||
* @return Drag data if present or row data otherwise. | * @return Drag data if present or row data otherwise. | ||||
*/ | */ | ||||
private JsonObject getDragData(JsonObject row) { | private JsonObject getDragData(JsonObject row) { | ||||
return row.hasKey(GridDragSourceExtensionState.JSONKEY_DRAG_DATA) | |||||
? row.getObject(GridDragSourceExtensionState.JSONKEY_DRAG_DATA) | |||||
: row; | |||||
return row.hasKey(GridDragSourceState.JSONKEY_DRAG_DATA) | |||||
? row.getObject(GridDragSourceState.JSONKEY_DRAG_DATA) : row; | |||||
} | } | ||||
@Override | @Override | ||||
} | } | ||||
@Override | @Override | ||||
public GridDragSourceExtensionState getState() { | |||||
return (GridDragSourceExtensionState) super.getState(); | |||||
public GridDragSourceState getState() { | |||||
return (GridDragSourceState) super.getState(); | |||||
} | } | ||||
} | } |
import com.vaadin.shared.ui.Connect; | import com.vaadin.shared.ui.Connect; | ||||
import com.vaadin.shared.ui.grid.DropLocation; | import com.vaadin.shared.ui.grid.DropLocation; | ||||
import com.vaadin.shared.ui.grid.DropMode; | import com.vaadin.shared.ui.grid.DropMode; | ||||
import com.vaadin.shared.ui.grid.GridDropTargetExtensionRpc; | |||||
import com.vaadin.shared.ui.grid.GridDropTargetExtensionState; | |||||
import com.vaadin.shared.ui.grid.GridDropTargetRpc; | |||||
import com.vaadin.shared.ui.grid.GridDropTargetState; | |||||
import com.vaadin.shared.ui.grid.GridState; | import com.vaadin.shared.ui.grid.GridState; | ||||
import com.vaadin.ui.GridDropTargetExtension; | |||||
import com.vaadin.ui.GridDropTarget; | |||||
import elemental.events.Event; | import elemental.events.Event; | ||||
import elemental.json.JsonObject; | import elemental.json.JsonObject; | ||||
/** | /** | ||||
* Makes Grid an HTML5 drop target. This is the client side counterpart of | * Makes Grid an HTML5 drop target. This is the client side counterpart of | ||||
* {@link GridDropTargetExtension}. | |||||
* {@link GridDropTarget}. | |||||
* | * | ||||
* @author Vaadin Ltd | * @author Vaadin Ltd | ||||
* @since | * @since | ||||
*/ | */ | ||||
@Connect(GridDropTargetExtension.class) | |||||
public class GridDropTargetExtensionConnector extends | |||||
@Connect(GridDropTarget.class) | |||||
public class GridDropTargetConnector extends | |||||
DropTargetExtensionConnector { | DropTargetExtensionConnector { | ||||
// Drag over class name suffixes | // Drag over class name suffixes | ||||
(NativeEvent) dropEvent); | (NativeEvent) dropEvent); | ||||
} | } | ||||
getRpcProxy(GridDropTargetExtensionRpc.class) | |||||
getRpcProxy(GridDropTargetRpc.class) | |||||
.drop(dataTransferText, rowKey, dropLocation); | .drop(dataTransferText, rowKey, dropLocation); | ||||
} | } | ||||
} | } | ||||
@Override | @Override | ||||
public GridDropTargetExtensionState getState() { | |||||
return (GridDropTargetExtensionState) super.getState(); | |||||
public GridDropTargetState getState() { | |||||
return (GridDropTargetState) super.getState(); | |||||
} | } | ||||
} | } |
import com.vaadin.event.dnd.DragEndEvent; | import com.vaadin.event.dnd.DragEndEvent; | ||||
import com.vaadin.shared.ui.dnd.DropEffect; | import com.vaadin.shared.ui.dnd.DropEffect; | ||||
import com.vaadin.ui.Grid; | import com.vaadin.ui.Grid; | ||||
import com.vaadin.ui.GridDragSource; | |||||
/** | /** | ||||
* Drop event on an HTML5 drop target {@link Grid} row. | * Drop event on an HTML5 drop target {@link Grid} row. | ||||
* @param <T> | * @param <T> | ||||
* The Grid bean type. | * The Grid bean type. | ||||
* @author Vaadin Ltd. | * @author Vaadin Ltd. | ||||
* @see com.vaadin.ui.GridDragSourceExtension#addGridDragStartListener(GridDragStartListener) | |||||
* @see GridDragSource#addGridDragStartListener(GridDragStartListener) | |||||
* @since | * @since | ||||
*/ | */ | ||||
public class GridDragEndEvent<T> extends DragEndEvent<Grid<T>> { | public class GridDragEndEvent<T> extends DragEndEvent<Grid<T>> { |
import java.lang.reflect.Method; | import java.lang.reflect.Method; | ||||
import com.vaadin.event.ConnectorEventListener; | import com.vaadin.event.ConnectorEventListener; | ||||
import com.vaadin.ui.GridDragSource; | |||||
/** | /** | ||||
* Drop listener for HTML5 drop on a Grid row. | * Drop listener for HTML5 drop on a Grid row. | ||||
* @param <T> | * @param <T> | ||||
* The Grid bean type. | * The Grid bean type. | ||||
* @author Vaadin Ltd. | * @author Vaadin Ltd. | ||||
* @see com.vaadin.ui.GridDragSourceExtension#addGridDragEndListener(GridDragEndListener) | |||||
* @see GridDragSource#addGridDragEndListener(GridDragEndListener) | |||||
* @since | * @since | ||||
*/ | */ | ||||
@FunctionalInterface | @FunctionalInterface |
import com.vaadin.event.dnd.DragStartEvent; | import com.vaadin.event.dnd.DragStartEvent; | ||||
import com.vaadin.shared.ui.dnd.EffectAllowed; | import com.vaadin.shared.ui.dnd.EffectAllowed; | ||||
import com.vaadin.ui.Grid; | import com.vaadin.ui.Grid; | ||||
import com.vaadin.ui.GridDragSource; | |||||
/** | /** | ||||
* Drag start event on an HTML5 drag source {@link Grid} row. | * Drag start event on an HTML5 drag source {@link Grid} row. | ||||
* @param <T> | * @param <T> | ||||
* The Grid bean type. | * The Grid bean type. | ||||
* @author Vaadin Ltd. | * @author Vaadin Ltd. | ||||
* @see com.vaadin.ui.GridDragSourceExtension#addGridDragStartListener(GridDragStartListener) | |||||
* @see GridDragSource#addGridDragStartListener(GridDragStartListener) | |||||
* @since | * @since | ||||
*/ | */ | ||||
public class GridDragStartEvent<T> extends DragStartEvent<Grid<T>> { | public class GridDragStartEvent<T> extends DragStartEvent<Grid<T>> { |
import java.lang.reflect.Method; | import java.lang.reflect.Method; | ||||
import com.vaadin.event.ConnectorEventListener; | import com.vaadin.event.ConnectorEventListener; | ||||
import com.vaadin.ui.GridDragSource; | |||||
/** | /** | ||||
* Drag start listener for HTML5 drag start on a Grid row. | * Drag start listener for HTML5 drag start on a Grid row. | ||||
* @param <T> | * @param <T> | ||||
* The Grid bean type. | * The Grid bean type. | ||||
* @author Vaadin Ltd. | * @author Vaadin Ltd. | ||||
* @see com.vaadin.ui.GridDragSourceExtension#addGridDragStartListener(GridDragStartListener) | |||||
* @see GridDragSource#addGridDragStartListener(GridDragStartListener) | |||||
* @since | * @since | ||||
*/ | */ | ||||
@FunctionalInterface | @FunctionalInterface |
import com.vaadin.shared.ui.grid.DropLocation; | import com.vaadin.shared.ui.grid.DropLocation; | ||||
import com.vaadin.ui.AbstractComponent; | import com.vaadin.ui.AbstractComponent; | ||||
import com.vaadin.ui.Grid; | import com.vaadin.ui.Grid; | ||||
import com.vaadin.ui.GridDropTarget; | |||||
/** | /** | ||||
* Drop event on an HTML5 drop target {@link Grid} row. | * Drop event on an HTML5 drop target {@link Grid} row. | ||||
* @param <T> | * @param <T> | ||||
* The Grid bean type. | * The Grid bean type. | ||||
* @author Vaadin Ltd. | * @author Vaadin Ltd. | ||||
* @see com.vaadin.ui.GridDropTargetExtension#addGridDropListener(GridDropListener) | |||||
* @see GridDropTarget#addGridDropListener(GridDropListener) | |||||
* @since | * @since | ||||
*/ | */ | ||||
public class GridDropEvent<T> extends DropEvent<Grid<T>> { | public class GridDropEvent<T> extends DropEvent<Grid<T>> { |
import java.lang.reflect.Method; | import java.lang.reflect.Method; | ||||
import com.vaadin.event.ConnectorEventListener; | import com.vaadin.event.ConnectorEventListener; | ||||
import com.vaadin.ui.GridDropTarget; | |||||
/** | /** | ||||
* Drop listener for HTML5 drop on a Grid row. | * Drop listener for HTML5 drop on a Grid row. | ||||
* @param <T> | * @param <T> | ||||
* The Grid bean type. | * The Grid bean type. | ||||
* @author Vaadin Ltd. | * @author Vaadin Ltd. | ||||
* @see com.vaadin.ui.GridDropTargetExtension#addGridDropListener(GridDropListener) | |||||
* @see GridDropTarget#addGridDropListener(GridDropListener) | |||||
* @since | * @since | ||||
*/ | */ | ||||
@FunctionalInterface | @FunctionalInterface |
import com.vaadin.shared.Registration; | import com.vaadin.shared.Registration; | ||||
import com.vaadin.shared.ui.dnd.DragSourceState; | import com.vaadin.shared.ui.dnd.DragSourceState; | ||||
import com.vaadin.shared.ui.dnd.DropEffect; | import com.vaadin.shared.ui.dnd.DropEffect; | ||||
import com.vaadin.shared.ui.grid.GridDragSourceExtensionRpc; | |||||
import com.vaadin.shared.ui.grid.GridDragSourceExtensionState; | |||||
import com.vaadin.shared.ui.grid.GridDragSourceRpc; | |||||
import com.vaadin.shared.ui.grid.GridDragSourceState; | |||||
import elemental.json.JsonObject; | import elemental.json.JsonObject; | ||||
* @author Vaadin Ltd. | * @author Vaadin Ltd. | ||||
* @since | * @since | ||||
*/ | */ | ||||
public class GridDragSourceExtension<T> extends DragSourceExtension<Grid<T>> { | |||||
public class GridDragSource<T> extends DragSourceExtension<Grid<T>> { | |||||
/** | /** | ||||
* Drag data generator that appends drag data to each row. | * Drag data generator that appends drag data to each row. | ||||
* @param target | * @param target | ||||
* Grid to be extended. | * Grid to be extended. | ||||
*/ | */ | ||||
public GridDragSourceExtension(Grid<T> target) { | |||||
public GridDragSource(Grid<T> target) { | |||||
super(target); | super(target); | ||||
// Create drag data generator | // Create drag data generator | ||||
@Override | @Override | ||||
protected void registerDragSourceRpc(Grid<T> target) { | protected void registerDragSourceRpc(Grid<T> target) { | ||||
registerRpc(new GridDragSourceExtensionRpc() { | |||||
registerRpc(new GridDragSourceRpc() { | |||||
@Override | @Override | ||||
public void dragStart(List<String> draggedItemKeys) { | public void dragStart(List<String> draggedItemKeys) { | ||||
*/ | */ | ||||
private void generateDragData(T item, JsonObject jsonObject) { | private void generateDragData(T item, JsonObject jsonObject) { | ||||
Optional.ofNullable(generatorFunction).ifPresent(generator -> jsonObject | Optional.ofNullable(generatorFunction).ifPresent(generator -> jsonObject | ||||
.put(GridDragSourceExtensionState.JSONKEY_DRAG_DATA, | |||||
.put(GridDragSourceState.JSONKEY_DRAG_DATA, | |||||
generator.apply(item))); | generator.apply(item))); | ||||
} | } | ||||
} | } | ||||
@Override | @Override | ||||
protected GridDragSourceExtensionState getState() { | |||||
return (GridDragSourceExtensionState) super.getState(); | |||||
protected GridDragSourceState getState() { | |||||
return (GridDragSourceState) super.getState(); | |||||
} | } | ||||
@Override | @Override | ||||
protected GridDragSourceExtensionState getState(boolean markAsDirty) { | |||||
return (GridDragSourceExtensionState) super.getState(markAsDirty); | |||||
protected GridDragSourceState getState(boolean markAsDirty) { | |||||
return (GridDragSourceState) super.getState(markAsDirty); | |||||
} | } | ||||
} | } |
import com.vaadin.event.dnd.grid.GridDropListener; | import com.vaadin.event.dnd.grid.GridDropListener; | ||||
import com.vaadin.shared.Registration; | import com.vaadin.shared.Registration; | ||||
import com.vaadin.shared.ui.grid.DropMode; | import com.vaadin.shared.ui.grid.DropMode; | ||||
import com.vaadin.shared.ui.grid.GridDropTargetExtensionRpc; | |||||
import com.vaadin.shared.ui.grid.GridDropTargetExtensionState; | |||||
import com.vaadin.shared.ui.grid.GridDropTargetRpc; | |||||
import com.vaadin.shared.ui.grid.GridDropTargetState; | |||||
/** | /** | ||||
* Makes the rows of a Grid HTML5 drop targets. This is the server side | * Makes the rows of a Grid HTML5 drop targets. This is the server side | ||||
* @author Vaadin Ltd | * @author Vaadin Ltd | ||||
* @since | * @since | ||||
*/ | */ | ||||
public class GridDropTargetExtension<T> extends DropTargetExtension<Grid<T>> { | |||||
public class GridDropTarget<T> extends DropTargetExtension<Grid<T>> { | |||||
/** | /** | ||||
* Extends a Grid and makes it's rows drop targets for HTML5 drag and drop. | * Extends a Grid and makes it's rows drop targets for HTML5 drag and drop. | ||||
* Grid's row. | * Grid's row. | ||||
* @see GridDropEvent#getDropLocation() | * @see GridDropEvent#getDropLocation() | ||||
*/ | */ | ||||
public GridDropTargetExtension(Grid<T> target, DropMode dropMode) { | |||||
public GridDropTarget(Grid<T> target, DropMode dropMode) { | |||||
super(target); | super(target); | ||||
setDropMode(dropMode); | setDropMode(dropMode); | ||||
@Override | @Override | ||||
protected void registerDropTargetRpc(Grid<T> target) { | protected void registerDropTargetRpc(Grid<T> target) { | ||||
registerRpc( | registerRpc( | ||||
(GridDropTargetExtensionRpc) (dataTransferText, rowKey, dropLocation) -> { | |||||
(GridDropTargetRpc) (dataTransferText, rowKey, dropLocation) -> { | |||||
T dropTargetRow = target.getDataCommunicator() | T dropTargetRow = target.getDataCommunicator() | ||||
.getKeyMapper().get(rowKey); | .getKeyMapper().get(rowKey); | ||||
} | } | ||||
@Override | @Override | ||||
protected GridDropTargetExtensionState getState() { | |||||
return (GridDropTargetExtensionState) super.getState(); | |||||
protected GridDropTargetState getState() { | |||||
return (GridDropTargetState) super.getState(); | |||||
} | } | ||||
@Override | @Override | ||||
protected GridDropTargetExtensionState getState(boolean markAsDirty) { | |||||
return (GridDropTargetExtensionState) super.getState(markAsDirty); | |||||
protected GridDropTargetState getState(boolean markAsDirty) { | |||||
return (GridDropTargetState) super.getState(markAsDirty); | |||||
} | } | ||||
} | } |
* @author Vaadin Ltd. | * @author Vaadin Ltd. | ||||
* @since | * @since | ||||
*/ | */ | ||||
public interface GridDragSourceExtensionRpc extends ServerRpc { | |||||
public interface GridDragSourceRpc extends ServerRpc { | |||||
/** | /** | ||||
* Called when dragstart event happens on client side. | * Called when dragstart event happens on client side. |
import com.vaadin.shared.ui.dnd.DragSourceState; | import com.vaadin.shared.ui.dnd.DragSourceState; | ||||
/** | /** | ||||
* State class containing parameters for GridDragSourceExtension. | |||||
* State class containing parameters for GridDragSource. | |||||
* | * | ||||
* @author Vaadin Ltd | * @author Vaadin Ltd | ||||
* @since | * @since | ||||
*/ | */ | ||||
public class GridDragSourceExtensionState extends DragSourceState { | |||||
public class GridDragSourceState extends DragSourceState { | |||||
/** | /** | ||||
* Json key for storing data for a dragged row. | * Json key for storing data for a dragged row. |
* @author Vaadin Ltd. | * @author Vaadin Ltd. | ||||
* @since | * @since | ||||
*/ | */ | ||||
public interface GridDropTargetExtensionRpc extends ServerRpc { | |||||
public interface GridDropTargetRpc extends ServerRpc { | |||||
/** | /** | ||||
* Called when drop event happens on client side. | * Called when drop event happens on client side. |
import com.vaadin.shared.ui.dnd.DropTargetState; | import com.vaadin.shared.ui.dnd.DropTargetState; | ||||
/** | /** | ||||
* State class containing parameters for GridDropTargetExtension. | |||||
* State class containing parameters for GridDropTarget. | |||||
* | * | ||||
* @author Vaadin Ltd | * @author Vaadin Ltd | ||||
* @since | * @since | ||||
*/ | */ | ||||
public class GridDropTargetExtensionState extends DropTargetState { | |||||
public class GridDropTargetState extends DropTargetState { | |||||
/** | /** | ||||
* Stores the drop mode of the drop target Grid. | * Stores the drop mode of the drop target Grid. |
import com.vaadin.shared.ui.grid.DropMode; | import com.vaadin.shared.ui.grid.DropMode; | ||||
import com.vaadin.tests.components.AbstractTestUIWithLog; | import com.vaadin.tests.components.AbstractTestUIWithLog; | ||||
import com.vaadin.ui.Grid; | import com.vaadin.ui.Grid; | ||||
import com.vaadin.ui.GridDragSourceExtension; | |||||
import com.vaadin.ui.GridDropTargetExtension; | |||||
import com.vaadin.ui.GridDragSource; | |||||
import com.vaadin.ui.GridDropTarget; | |||||
import com.vaadin.ui.HorizontalLayout; | import com.vaadin.ui.HorizontalLayout; | ||||
import com.vaadin.ui.Layout; | import com.vaadin.ui.Layout; | ||||
import com.vaadin.ui.RadioButtonGroup; | import com.vaadin.ui.RadioButtonGroup; | ||||
dragSourceComponent.addColumn(Bean::getId).setCaption("ID"); | dragSourceComponent.addColumn(Bean::getId).setCaption("ID"); | ||||
dragSourceComponent.addColumn(Bean::getValue).setCaption("Value"); | dragSourceComponent.addColumn(Bean::getValue).setCaption("Value"); | ||||
GridDragSourceExtension<Bean> dragSource = new GridDragSourceExtension<>( | |||||
GridDragSource<Bean> dragSource = new GridDragSource<>( | |||||
dragSourceComponent); | dragSourceComponent); | ||||
dragSource.setDragDataGenerator(bean -> { | dragSource.setDragDataGenerator(bean -> { | ||||
JsonObject ret = Json.createObject(); | JsonObject ret = Json.createObject(); | ||||
dropTargetComponent.addColumn(Bean::getId).setCaption("ID"); | dropTargetComponent.addColumn(Bean::getId).setCaption("ID"); | ||||
dropTargetComponent.addColumn(Bean::getValue).setCaption("Value"); | dropTargetComponent.addColumn(Bean::getValue).setCaption("Value"); | ||||
GridDropTargetExtension<Bean> dropTarget = new GridDropTargetExtension<>( | |||||
GridDropTarget<Bean> dropTarget = new GridDropTarget<>( | |||||
dropTargetComponent, DropMode.ON_TOP); | dropTargetComponent, DropMode.ON_TOP); | ||||
dropTarget.addGridDropListener(event -> { | dropTarget.addGridDropListener(event -> { | ||||
log(event.getDataTransferText() + ", targetId=" + event | log(event.getDataTransferText() + ", targetId=" + event |