JsArrayString detachedArray = detachedConnectors.dump(); | JsArrayString detachedArray = detachedConnectors.dump(); | ||||
for (int i = 0; i < detachedArray.length(); i++) { | for (int i = 0; i < detachedArray.length(); i++) { | ||||
ServerConnector connector = getConnectorMap().getConnector( | |||||
detachedArray.get(i)); | |||||
ServerConnector connector = getConnectorMap() | |||||
.getConnector(detachedArray.get(i)); | |||||
Profiler.enter( | Profiler.enter( | ||||
"unregisterRemovedConnectors unregisterConnector"); | "unregisterRemovedConnectors unregisterConnector"); | ||||
* establishing a push connection with the client. | * establishing a push connection with the client. | ||||
* | * | ||||
* @return the push connection identifier string | * @return the push connection identifier string | ||||
* | |||||
* @since 8.1 | |||||
*/ | */ | ||||
public String getPushId() { | public String getPushId() { | ||||
return pushId; | return pushId; |
import com.vaadin.client.widget.grid.selection.SelectionModel; | import com.vaadin.client.widget.grid.selection.SelectionModel; | ||||
import com.vaadin.client.widgets.Escalator; | import com.vaadin.client.widgets.Escalator; | ||||
import com.vaadin.client.widgets.Grid; | import com.vaadin.client.widgets.Grid; | ||||
import com.vaadin.server.Page; | |||||
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 elemental.json.JsonObject; | import elemental.json.JsonObject; | ||||
/** | /** | ||||
* 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 GridDragSource}. | |||||
* 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 GridDragSource}. | |||||
* | * | ||||
* @author Vaadin Ltd | * @author Vaadin Ltd | ||||
* @since | |||||
* @since 8.1 | |||||
*/ | */ | ||||
@Connect(GridDragSource.class) | @Connect(GridDragSource.class) | ||||
public class GridDragSourceConnector extends | |||||
DragSourceExtensionConnector { | |||||
public class GridDragSourceConnector extends DragSourceExtensionConnector { | |||||
private static final String STYLE_SUFFIX_DRAG_BADGE = "-drag-badge"; | private static final String STYLE_SUFFIX_DRAG_BADGE = "-drag-badge"; | ||||
@Override | @Override | ||||
protected void extend(ServerConnector target) { | protected void extend(ServerConnector target) { | ||||
this.gridConnector = (GridConnector) target; | |||||
gridConnector = (GridConnector) target; | |||||
// Set newly added rows draggable | // Set newly added rows draggable | ||||
getGridBody().setNewEscalatorRowCallback( | getGridBody().setNewEscalatorRowCallback( | ||||
Element draggedRowElement = (Element) event.getTarget(); | Element draggedRowElement = (Element) event.getTarget(); | ||||
Element badge = DOM.createSpan(); | Element badge = DOM.createSpan(); | ||||
badge.setClassName( | |||||
gridConnector.getWidget().getStylePrimaryName() + "-row" | |||||
+ STYLE_SUFFIX_DRAG_BADGE); | |||||
badge.setClassName(gridConnector.getWidget().getStylePrimaryName() | |||||
+ "-row" + STYLE_SUFFIX_DRAG_BADGE); | |||||
badge.setInnerHTML(draggedItemKeys.size() + ""); | badge.setInnerHTML(draggedItemKeys.size() + ""); | ||||
badge.getStyle().setMarginLeft( | badge.getStyle().setMarginLeft( | ||||
draggedRowElement.appendChild(badge); | draggedRowElement.appendChild(badge); | ||||
// Remove badge on the next animation frame. Drag image will still contain the badge. | |||||
// Remove badge on the next animation frame. Drag image will still | |||||
// contain the badge. | |||||
AnimationScheduler.get().requestAnimationFrame(timestamp -> { | AnimationScheduler.get().requestAnimationFrame(timestamp -> { | ||||
badge.removeFromParent(); | badge.removeFromParent(); | ||||
}, (Element) event.getTarget()); | }, (Element) event.getTarget()); | ||||
@Override | @Override | ||||
protected String createDataTransferText(Event dragStartEvent) { | protected String createDataTransferText(Event dragStartEvent) { | ||||
JsonArray dragData = toJsonArray( | |||||
getDraggedRows(dragStartEvent).stream().map(this::getDragData) | |||||
.collect(Collectors.toList())); | |||||
JsonArray dragData = toJsonArray(getDraggedRows(dragStartEvent).stream() | |||||
.map(this::getDragData).collect(Collectors.toList())); | |||||
return dragData.toJson(); | return dragData.toJson(); | ||||
} | } | ||||
DropEffect dropEffect) { | DropEffect dropEffect) { | ||||
// Send server RPC with dragged item keys | // Send server RPC with dragged item keys | ||||
getRpcProxy(GridDragSourceRpc.class) | |||||
.dragEnd(dropEffect, draggedItemKeys); | |||||
getRpcProxy(GridDragSourceRpc.class).dragEnd(dropEffect, | |||||
draggedItemKeys); | |||||
} | } | ||||
/** | /** | ||||
* allowed and a selected row is dragged. | * allowed and a selected row is dragged. | ||||
* | * | ||||
* @param draggedRow | * @param draggedRow | ||||
* Data of dragged row. | |||||
* Data of dragged row. | |||||
* @return {@code true} if multiple rows are dragged, {@code false} | * @return {@code true} if multiple rows are dragged, {@code false} | ||||
* otherwise. | |||||
* otherwise. | |||||
*/ | */ | ||||
private boolean dragMultipleRows(JsonObject draggedRow) { | private boolean dragMultipleRows(JsonObject draggedRow) { | ||||
SelectionModel<JsonObject> selectionModel = getGrid() | SelectionModel<JsonObject> selectionModel = getGrid() | ||||
* Get all selected rows from a subset of rows defined by {@code range}. | * Get all selected rows from a subset of rows defined by {@code range}. | ||||
* | * | ||||
* @param range | * @param range | ||||
* Range of indexes. | |||||
* Range of indexes. | |||||
* @return List of data of all selected rows in the given range. | * @return List of data of all selected rows in the given range. | ||||
*/ | */ | ||||
private List<JsonObject> getSelectedRowsInRange(Range range) { | private List<JsonObject> getSelectedRowsInRange(Range range) { | ||||
* Converts a list of {@link JsonObject}s to a {@link JsonArray}. | * Converts a list of {@link JsonObject}s to a {@link JsonArray}. | ||||
* | * | ||||
* @param objects | * @param objects | ||||
* List of json objects. | |||||
* List of json objects. | |||||
* @return Json array containing all json objects. | * @return Json array containing all json objects. | ||||
*/ | */ | ||||
private JsonArray toJsonArray(List<JsonObject> objects) { | private JsonArray toJsonArray(List<JsonObject> objects) { | ||||
* otherwise. | * otherwise. | ||||
* | * | ||||
* @param row | * @param row | ||||
* Row data. | |||||
* Row data. | |||||
* @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) { |
* {@link GridDropTarget}. | * {@link GridDropTarget}. | ||||
* | * | ||||
* @author Vaadin Ltd | * @author Vaadin Ltd | ||||
* @since | |||||
* @since 8.1 | |||||
*/ | */ | ||||
@Connect(GridDropTarget.class) | @Connect(GridDropTarget.class) | ||||
public class GridDropTargetConnector extends | |||||
DropTargetExtensionConnector { | |||||
public class GridDropTargetConnector extends DropTargetExtensionConnector { | |||||
/** | /** | ||||
* Current style name | * Current style name | ||||
(NativeEvent) dropEvent); | (NativeEvent) dropEvent); | ||||
} | } | ||||
getRpcProxy(GridDropTargetRpc.class) | |||||
.drop(dataTransferText, rowKey, dropLocation); | |||||
getRpcProxy(GridDropTargetRpc.class).drop(dataTransferText, rowKey, | |||||
dropLocation); | |||||
} | } | ||||
private JsonObject getRowData(TableRowElement row) { | private JsonObject getRowData(TableRowElement row) { | ||||
} else if (getState().dropMode == DropMode.ON_TOP_OR_BETWEEN) { | } else if (getState().dropMode == DropMode.ON_TOP_OR_BETWEEN) { | ||||
if (getRelativeY(target, event) < getState().dropThreshold) { | if (getRelativeY(target, event) < getState().dropThreshold) { | ||||
return DropLocation.ABOVE; | return DropLocation.ABOVE; | ||||
} else if (target.getOffsetHeight() - getRelativeY(target, event) | |||||
< getState().dropThreshold) { | |||||
} else if (target.getOffsetHeight() | |||||
- getRelativeY(target, event) < getState().dropThreshold) { | |||||
return DropLocation.BELOW; | return DropLocation.BELOW; | ||||
} else { | } else { | ||||
return DropLocation.ON_TOP; | return DropLocation.ON_TOP; | ||||
@Override | @Override | ||||
protected void onDragEnter(Event event) { | protected void onDragEnter(Event event) { | ||||
// Generate style names for the drop target | // Generate style names for the drop target | ||||
String styleRow = | |||||
gridConnector.getWidget().getStylePrimaryName() + "-row"; | |||||
String styleRow = gridConnector.getWidget().getStylePrimaryName() | |||||
+ "-row"; | |||||
styleDragCenter = styleRow + STYLE_SUFFIX_DRAG_CENTER; | styleDragCenter = styleRow + STYLE_SUFFIX_DRAG_CENTER; | ||||
styleDragTop = styleRow + STYLE_SUFFIX_DRAG_TOP; | styleDragTop = styleRow + STYLE_SUFFIX_DRAG_TOP; | ||||
styleDragBottom = styleRow + STYLE_SUFFIX_DRAG_BOTTOM; | styleDragBottom = styleRow + STYLE_SUFFIX_DRAG_BOTTOM; |
* held down. | * held down. | ||||
* | * | ||||
* @param enabled | * @param enabled | ||||
* {@literal true} if capturing enabled, {@literal false} otherwise | |||||
* {@literal true} if capturing enabled, {@literal false} | |||||
* otherwise | |||||
* | |||||
* @since 8.1 | |||||
*/ | */ | ||||
public void setCapturingEnabled(boolean enabled) { | public void setCapturingEnabled(boolean enabled) { | ||||
capturingEnabled = enabled; | capturingEnabled = enabled; | ||||
* Returns if the widget's capturing of mouse events are enabled. | * Returns if the widget's capturing of mouse events are enabled. | ||||
* | * | ||||
* @return {@literal true} if mouse capturing is enabled, {@literal false} | * @return {@literal true} if mouse capturing is enabled, {@literal false} | ||||
* otherwise | |||||
* otherwise | |||||
* | |||||
* @since 8.1 | |||||
*/ | */ | ||||
public boolean isCapturingEnabled() { | public boolean isCapturingEnabled() { | ||||
return capturingEnabled; | return capturingEnabled; | ||||
private static native int getHorizontalBorderAndPaddingWidth(Element elem) | private static native int getHorizontalBorderAndPaddingWidth(Element elem) | ||||
/*-{ | /*-{ | ||||
// THIS METHOD IS ONLY USED FOR INTERNET EXPLORER, IT DOESN'T WORK WITH OTHERS | // THIS METHOD IS ONLY USED FOR INTERNET EXPLORER, IT DOESN'T WORK WITH OTHERS | ||||
var convertToPixel = function(elem, value) { | var convertToPixel = function(elem, value) { | ||||
// From the awesome hack by Dean Edwards | // From the awesome hack by Dean Edwards | ||||
// http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291 | // http://erik.eae.net/archives/2007/07/27/18.54.15/#comment-102291 | ||||
// Remember the original values | // Remember the original values | ||||
var left = elem.style.left, rsLeft = elem.runtimeStyle.left; | var left = elem.style.left, rsLeft = elem.runtimeStyle.left; | ||||
// Put in the new values to get a computed value out | // Put in the new values to get a computed value out | ||||
elem.runtimeStyle.left = elem.currentStyle.left; | elem.runtimeStyle.left = elem.currentStyle.left; | ||||
elem.style.left = value || 0; | elem.style.left = value || 0; | ||||
var ret = elem.style.pixelLeft; | var ret = elem.style.pixelLeft; | ||||
// Revert the changed values | // Revert the changed values | ||||
elem.style.left = left; | elem.style.left = left; | ||||
elem.runtimeStyle.left = rsLeft; | elem.runtimeStyle.left = rsLeft; | ||||
return ret; | return ret; | ||||
} | } | ||||
var ret = 0; | var ret = 0; | ||||
var sides = ["Right","Left"]; | var sides = ["Right","Left"]; | ||||
for(var i=0; i<2; i++) { | for(var i=0; i<2; i++) { | ||||
var side = sides[i]; | var side = sides[i]; | ||||
ret += parseInt(value.substr(0, value.length-2)); | ret += parseInt(value.substr(0, value.length-2)); | ||||
} | } | ||||
} | } | ||||
// Padding ------------------------------------------------------- | // Padding ------------------------------------------------------- | ||||
value = elem.currentStyle["padding"+side]; | value = elem.currentStyle["padding"+side]; | ||||
if ( !/^\d+(px)?$/i.test( value ) && /^\d/.test( value ) ) { | if ( !/^\d+(px)?$/i.test( value ) && /^\d/.test( value ) ) { | ||||
ret += parseInt(value.substr(0, value.length-2)); | ret += parseInt(value.substr(0, value.length-2)); | ||||
} | } | ||||
} | } | ||||
return ret; | return ret; | ||||
}-*/; | }-*/; | ||||
* the escalator. | * the escalator. | ||||
* | * | ||||
* @param consumer | * @param consumer | ||||
* A Consumer function that receives the newly added table row | |||||
* elements. | |||||
* A Consumer function that receives the newly added table | |||||
* row elements. | |||||
* @since 8.1 | |||||
*/ | */ | ||||
public void setNewEscalatorRowCallback( | public void setNewEscalatorRowCallback( | ||||
Consumer<List<TableRowElement>> consumer); | Consumer<List<TableRowElement>> consumer); |
* 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 GridDragSource#addGridDragStartListener(GridDragStartListener) | * @see GridDragSource#addGridDragStartListener(GridDragStartListener) | ||||
* @since | |||||
* @since 8.1 | |||||
*/ | */ | ||||
public class GridDragEndEvent<T> extends DragEndEvent<Grid<T>> { | public class GridDragEndEvent<T> extends DragEndEvent<Grid<T>> { | ||||
* Creates a drag end event. | * Creates a drag end event. | ||||
* | * | ||||
* @param source | * @param source | ||||
* Grid component in which the items were dragged. | |||||
* Grid component in which the items were dragged. | |||||
* @param dropEffect | * @param dropEffect | ||||
* Drop effect from {@code DataTransfer.dropEffect} object. | |||||
* Drop effect from {@code DataTransfer.dropEffect} object. | |||||
* @param draggedItems | * @param draggedItems | ||||
* Set of items having been dragged. | |||||
* Set of items having been dragged. | |||||
*/ | */ | ||||
public GridDragEndEvent(Grid<T> source, DropEffect dropEffect, | public GridDragEndEvent(Grid<T> source, DropEffect dropEffect, | ||||
Set<T> draggedItems) { | Set<T> draggedItems) { |
* 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 GridDragSource#addGridDragEndListener(GridDragEndListener) | * @see GridDragSource#addGridDragEndListener(GridDragEndListener) | ||||
* @since | |||||
* @since 8.1 | |||||
*/ | */ | ||||
@FunctionalInterface | @FunctionalInterface | ||||
public interface GridDragEndListener<T> extends ConnectorEventListener { | public interface GridDragEndListener<T> extends ConnectorEventListener { | ||||
* drag. | * drag. | ||||
* | * | ||||
* @param event | * @param event | ||||
* The drag end event. | |||||
* The drag end event. | |||||
*/ | */ | ||||
void dragEnd(GridDragEndEvent<T> event); | void dragEnd(GridDragEndEvent<T> event); | ||||
} | } |
* 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 GridDragSource#addGridDragStartListener(GridDragStartListener) | * @see GridDragSource#addGridDragStartListener(GridDragStartListener) | ||||
* @since | |||||
* @since 8.1 | |||||
*/ | */ | ||||
public class GridDragStartEvent<T> extends DragStartEvent<Grid<T>> { | public class GridDragStartEvent<T> extends DragStartEvent<Grid<T>> { | ||||
* Creates a drag start event. | * Creates a drag start event. | ||||
* | * | ||||
* @param source | * @param source | ||||
* The source grid where the rows are being dragged from. | |||||
* The source grid where the rows are being dragged from. | |||||
* @param effectAllowed | * @param effectAllowed | ||||
* Allowed effect from {@code DataTransfer.effectAllowed} object. | |||||
* Allowed effect from {@code DataTransfer.effectAllowed} object. | |||||
* @param draggedItems | * @param draggedItems | ||||
* Set of items being dragged. | |||||
* Set of items being dragged. | |||||
*/ | */ | ||||
public GridDragStartEvent(Grid<T> source, EffectAllowed effectAllowed, | public GridDragStartEvent(Grid<T> source, EffectAllowed effectAllowed, | ||||
Set<T> draggedItems) { | Set<T> draggedItems) { |
* 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 GridDragSource#addGridDragStartListener(GridDragStartListener) | * @see GridDragSource#addGridDragStartListener(GridDragStartListener) | ||||
* @since | |||||
* @since 8.1 | |||||
*/ | */ | ||||
@FunctionalInterface | @FunctionalInterface | ||||
public interface GridDragStartListener<T> extends ConnectorEventListener { | public interface GridDragStartListener<T> extends ConnectorEventListener { | ||||
* Invoked when the user has started dragging grid's rows. | * Invoked when the user has started dragging grid's rows. | ||||
* | * | ||||
* @param event | * @param event | ||||
* The drag start event. | |||||
* The drag start event. | |||||
*/ | */ | ||||
void dragStart(GridDragStartEvent<T> event); | void dragStart(GridDragStartEvent<T> event); | ||||
} | } |
* 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 GridDropTarget#addGridDropListener(GridDropListener) | * @see GridDropTarget#addGridDropListener(GridDropListener) | ||||
* @since | |||||
* @since 8.1 | |||||
*/ | */ | ||||
public class GridDropEvent<T> extends DropEvent<Grid<T>> { | public class GridDropEvent<T> extends DropEvent<Grid<T>> { | ||||
* Creates a Grid row drop event. | * Creates a Grid row drop event. | ||||
* | * | ||||
* @param target | * @param target | ||||
* Grid that received the drop. | |||||
* Grid that received the drop. | |||||
* @param dataTransferText | * @param dataTransferText | ||||
* Data of type {@code "text"} from the {@code DataTransfer} | |||||
* object. | |||||
* Data of type {@code "text"} from the {@code DataTransfer} | |||||
* object. | |||||
* @param dragSourceExtension | * @param dragSourceExtension | ||||
* Drag source extension of the component that initiated the drop | |||||
* event. | |||||
* Drag source extension of the component that initiated the drop | |||||
* event. | |||||
* @param dropTargetRow | * @param dropTargetRow | ||||
* Target row that received the drop. | |||||
* Target row that received the drop. | |||||
* @param dropLocation | * @param dropLocation | ||||
* Location of the drop within the target row. | |||||
* Location of the drop within the target row. | |||||
*/ | */ | ||||
public GridDropEvent(Grid<T> target, String dataTransferText, | public GridDropEvent(Grid<T> target, String dataTransferText, | ||||
DragSourceExtension<? extends AbstractComponent> dragSourceExtension, | DragSourceExtension<? extends AbstractComponent> dragSourceExtension, |
* 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 GridDropTarget#addGridDropListener(GridDropListener) | * @see GridDropTarget#addGridDropListener(GridDropListener) | ||||
* @since | |||||
* @since 8.1 | |||||
*/ | */ | ||||
@FunctionalInterface | @FunctionalInterface | ||||
public interface GridDropListener<T> extends ConnectorEventListener { | public interface GridDropListener<T> extends ConnectorEventListener { | ||||
* Called when drop event is fired on a Grid row. | * Called when drop event is fired on a Grid row. | ||||
* | * | ||||
* @param event | * @param event | ||||
* Server side drop event. | |||||
* Server side drop event. | |||||
*/ | */ | ||||
void drop(GridDropEvent<T> event); | void drop(GridDropEvent<T> event); | ||||
} | } |
* establishing a push connection with the client. | * establishing a push connection with the client. | ||||
* | * | ||||
* @return the push connection identifier string | * @return the push connection identifier string | ||||
* | |||||
* @since 8.1 | |||||
*/ | */ | ||||
public String getPushId() { | public String getPushId() { | ||||
assert hasLock(); | assert hasLock(); |
/** | /** | ||||
* Internal utility class. | * Internal utility class. | ||||
* | * | ||||
* @since | |||||
* @since 8.1 | |||||
* @author Vaadin Ltd | * @author Vaadin Ltd | ||||
*/ | */ | ||||
public class ComponentRootSetter implements Serializable { | public class ComponentRootSetter implements Serializable { |
* </p> | * </p> | ||||
* | * | ||||
* @author Vaadin Ltd. | * @author Vaadin Ltd. | ||||
* @since | |||||
* @since 8.1 | |||||
*/ | */ | ||||
public class Composite extends AbstractComponent implements HasComponents { | public class Composite extends AbstractComponent implements HasComponents { | ||||
* that ONLY visible rows are taken into account. | * that ONLY visible rows are taken into account. | ||||
* | * | ||||
* @param <T> | * @param <T> | ||||
* The Grid bean type. | |||||
* The Grid bean type. | |||||
* @author Vaadin Ltd. | * @author Vaadin Ltd. | ||||
* @since | |||||
* @since 8.1 | |||||
*/ | */ | ||||
public class GridDragSource<T> extends DragSourceExtension<Grid<T>> { | public class GridDragSource<T> extends DragSourceExtension<Grid<T>> { | ||||
* Extends a Grid and makes it's rows draggable. | * Extends a Grid and makes it's rows draggable. | ||||
* | * | ||||
* @param target | * @param target | ||||
* Grid to be extended. | |||||
* Grid to be extended. | |||||
*/ | */ | ||||
public GridDragSource(Grid<T> target) { | public GridDragSource(Grid<T> target) { | ||||
super(target); | super(target); | ||||
* function is set by the user of this extension. | * function is set by the user of this extension. | ||||
* | * | ||||
* @param item | * @param item | ||||
* Row item for data generation. | |||||
* Row item for data generation. | |||||
* @param jsonObject | * @param jsonObject | ||||
* Row data in json format. | |||||
* Row data in json format. | |||||
*/ | */ | ||||
private void generateDragData(T item, JsonObject jsonObject) { | private void generateDragData(T item, JsonObject jsonObject) { | ||||
Optional.ofNullable(generatorFunction).ifPresent(generator -> jsonObject | |||||
.put(GridDragSourceState.JSONKEY_DRAG_DATA, | |||||
Optional.ofNullable(generatorFunction) | |||||
.ifPresent(generator -> jsonObject.put( | |||||
GridDragSourceState.JSONKEY_DRAG_DATA, | |||||
generator.apply(item))); | generator.apply(item))); | ||||
} | } | ||||
* {@link JsonObject} to be appended to the row data. | * {@link JsonObject} to be appended to the row data. | ||||
* <p> | * <p> | ||||
* Example: | * Example: | ||||
* | |||||
* <pre> | * <pre> | ||||
* dragSourceExtension.setDragDataGenerator(item -> { | |||||
* JsonObject dragData = Json.createObject(); | |||||
* dragData.put("someKey", item.getValue()); | |||||
* return dragData; | |||||
* }); | |||||
* dragSourceExtension.setDragDataGenerator(item -> { | |||||
* JsonObject dragData = Json.createObject(); | |||||
* dragData.put("someKey", item.getValue()); | |||||
* return dragData; | |||||
* }); | |||||
* </pre> | * </pre> | ||||
* | * | ||||
* @param generator | * @param generator | ||||
* Function to be executed on row data generation. | |||||
* Function to be executed on row data generation. | |||||
*/ | */ | ||||
public void setDragDataGenerator( | public void setDragDataGenerator( | ||||
SerializableFunction<T, JsonObject> generator) { | SerializableFunction<T, JsonObject> generator) { | ||||
* Setting the data transfer text for this drag source is not supported. | * Setting the data transfer text for this drag source is not supported. | ||||
* | * | ||||
* @throws UnsupportedOperationException | * @throws UnsupportedOperationException | ||||
* Setting dataTransferText is not supported, since the drag data is | |||||
* set for each row based on the data provided by the generator. | |||||
* Setting dataTransferText is not supported, since the drag | |||||
* data is set for each row based on the data provided by the | |||||
* generator. | |||||
* @see #setDragDataGenerator(SerializableFunction) | * @see #setDragDataGenerator(SerializableFunction) | ||||
*/ | */ | ||||
@Override | @Override | ||||
public void setDataTransferText(String data) throws | |||||
UnsupportedOperationException { | |||||
public void setDataTransferText(String data) | |||||
throws UnsupportedOperationException { | |||||
throw new UnsupportedOperationException( | throw new UnsupportedOperationException( | ||||
"Setting dataTransferText is not supported"); | "Setting dataTransferText is not supported"); | ||||
} | } | ||||
* Attaches dragstart listener for the current drag source grid. | * Attaches dragstart listener for the current drag source grid. | ||||
* | * | ||||
* @param listener | * @param listener | ||||
* Listener to handle the dragstart event. | |||||
* Listener to handle the dragstart event. | |||||
* @return Handle to be used to remove this listener. | * @return Handle to be used to remove this listener. | ||||
* @see GridDragStartEvent | * @see GridDragStartEvent | ||||
*/ | */ | ||||
* Attaches dragend listener for the current drag source grid. | * Attaches dragend listener for the current drag source grid. | ||||
* | * | ||||
* @param listener | * @param listener | ||||
* Listener to handle the dragend event. | |||||
* Listener to handle the dragend event. | |||||
* @return Handle to be used to remove this listener. | * @return Handle to be used to remove this listener. | ||||
* @see GridDragEndEvent | * @see GridDragEndEvent | ||||
*/ | */ |
* counterpart of GridDropTargetExtensionConnector. | * counterpart of GridDropTargetExtensionConnector. | ||||
* | * | ||||
* @param <T> | * @param <T> | ||||
* Type of the Grid bean. | |||||
* Type of the Grid bean. | |||||
* @author Vaadin Ltd | * @author Vaadin Ltd | ||||
* @since | |||||
* @since 8.1 | |||||
*/ | */ | ||||
public class GridDropTarget<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. | ||||
* | * | ||||
* @param target | * @param target | ||||
* Grid to be extended. | |||||
* Grid to be extended. | |||||
* @param dropMode | * @param dropMode | ||||
* Drop mode that describes the allowed drop locations within the | |||||
* Grid's row. | |||||
* Drop mode that describes the allowed drop locations within the | |||||
* Grid's row. | |||||
* @see GridDropEvent#getDropLocation() | * @see GridDropEvent#getDropLocation() | ||||
*/ | */ | ||||
public GridDropTarget(Grid<T> target, DropMode dropMode) { | public GridDropTarget(Grid<T> target, DropMode dropMode) { | ||||
* Sets the drop mode of this drop target. | * Sets the drop mode of this drop target. | ||||
* | * | ||||
* @param dropMode | * @param dropMode | ||||
* Drop mode that describes the allowed drop locations within the | |||||
* Grid's row. | |||||
* Drop mode that describes the allowed drop locations within the | |||||
* Grid's row. | |||||
* @see GridDropEvent#getDropLocation() | * @see GridDropEvent#getDropLocation() | ||||
*/ | */ | ||||
public void setDropMode(DropMode dropMode) { | public void setDropMode(DropMode dropMode) { | ||||
} | } | ||||
/** | /** | ||||
* Attaches drop listener for the current drop target. {@link | |||||
* GridDropListener#drop(GridDropEvent)} is called when drop event happens | |||||
* on the client side. | |||||
* Attaches drop listener for the current drop target. | |||||
* {@link GridDropListener#drop(GridDropEvent)} is called when drop event | |||||
* happens on the client side. | |||||
* | * | ||||
* @param listener | * @param listener | ||||
* Listener to handle drop event. | |||||
* Listener to handle drop event. | |||||
* @return Handle to be used to remove this listener. | * @return Handle to be used to remove this listener. | ||||
*/ | */ | ||||
public Registration addGridDropListener(GridDropListener<T> listener) { | public Registration addGridDropListener(GridDropListener<T> listener) { | ||||
* <li>within {@code threshold} pixels from the top of a row results in a | * <li>within {@code threshold} pixels from the top of a row results in a | ||||
* drop event with {@link com.vaadin.shared.ui.grid.DropLocation#ABOVE | * drop event with {@link com.vaadin.shared.ui.grid.DropLocation#ABOVE | ||||
* DropLocation.ABOVE}</li> | * DropLocation.ABOVE}</li> | ||||
* <li>within {@code threshold} pixels from the bottom of a row results in | |||||
* a drop event with {@link com.vaadin.shared.ui.grid.DropLocation#BELOW | |||||
* <li>within {@code threshold} pixels from the bottom of a row results in a | |||||
* drop event with {@link com.vaadin.shared.ui.grid.DropLocation#BELOW | |||||
* DropLocation.BELOW}</li> | * DropLocation.BELOW}</li> | ||||
* <li>anywhere else within the row results in a drop event with {@link | |||||
* com.vaadin.shared.ui.grid.DropLocation#ON_TOP DropLocation.ON_TOP}</li> | |||||
* <li>anywhere else within the row results in a drop event with | |||||
* {@link com.vaadin.shared.ui.grid.DropLocation#ON_TOP | |||||
* DropLocation.ON_TOP}</li> | |||||
* </ul> | * </ul> | ||||
* The value only has an effect when drop mode is set to {@link | |||||
* DropMode#ON_TOP_OR_BETWEEN}. | |||||
* The value only has an effect when drop mode is set to | |||||
* {@link DropMode#ON_TOP_OR_BETWEEN}. | |||||
* <p> | * <p> | ||||
* Default is 5 pixels. | * Default is 5 pixels. | ||||
* | * | ||||
* @param threshold | * @param threshold | ||||
* The threshold from the top and bottom of the row in pixels. | |||||
* The threshold from the top and bottom of the row in pixels. | |||||
*/ | */ | ||||
public void setDropThreshold(int threshold) { | public void setDropThreshold(int threshold) { | ||||
getState().dropThreshold = threshold; | getState().dropThreshold = threshold; | ||||
@Override | @Override | ||||
protected void registerDropTargetRpc(Grid<T> target) { | protected void registerDropTargetRpc(Grid<T> target) { | ||||
registerRpc( | |||||
(GridDropTargetRpc) (dataTransferText, rowKey, dropLocation) -> { | |||||
registerRpc((GridDropTargetRpc) (dataTransferText, rowKey, | |||||
dropLocation) -> { | |||||
T dropTargetRow = target.getDataCommunicator() | |||||
.getKeyMapper().get(rowKey); | |||||
T dropTargetRow = target.getDataCommunicator().getKeyMapper() | |||||
.get(rowKey); | |||||
GridDropEvent<T> event = new GridDropEvent<>(target, | |||||
dataTransferText, getUI().getActiveDragSource(), | |||||
dropTargetRow, dropLocation); | |||||
GridDropEvent<T> event = new GridDropEvent<>(target, | |||||
dataTransferText, getUI().getActiveDragSource(), | |||||
dropTargetRow, dropLocation); | |||||
fireEvent(event); | |||||
}); | |||||
fireEvent(event); | |||||
}); | |||||
} | } | ||||
@Override | @Override |
* Shared state for Composite. | * Shared state for Composite. | ||||
* | * | ||||
* @author Vaadin Ltd | * @author Vaadin Ltd | ||||
* @since | |||||
* @since 8.1 | |||||
*/ | */ | ||||
public class CompositeState extends AbstractComponentState { | public class CompositeState extends AbstractComponentState { | ||||
} | } |
* Defines drop locations within a Grid row. | * Defines drop locations within a Grid row. | ||||
* | * | ||||
* @author Vaadin Ltd. | * @author Vaadin Ltd. | ||||
* @since | |||||
* @since 8.1 | |||||
*/ | */ | ||||
public enum DropLocation { | public enum DropLocation { | ||||
* Defines the locations within the Grid row where an element can be dropped. | * Defines the locations within the Grid row where an element can be dropped. | ||||
* | * | ||||
* @author Vaadin Ltd. | * @author Vaadin Ltd. | ||||
* @since | |||||
* @since 8.1 | |||||
*/ | */ | ||||
public enum DropMode { | public enum DropMode { | ||||
/** | /** | ||||
* The drop event can happen between Grid rows. The drop is above a row | |||||
* when the cursor is over the top 50% of a row, otherwise below the | |||||
* row. | |||||
* The drop event can happen between Grid rows. The drop is above a row when | |||||
* the cursor is over the top 50% of a row, otherwise below the row. | |||||
*/ | */ | ||||
BETWEEN, | BETWEEN, | ||||
/** | /** | ||||
* The drop event can happen on top of Grid rows. The target of the drop | |||||
* is the row under the cursor at the time of the drop event. | |||||
* The drop event can happen on top of Grid rows. The target of the drop is | |||||
* the row under the cursor at the time of the drop event. | |||||
*/ | */ | ||||
ON_TOP, | ON_TOP, | ||||
* The drop event can happen either on top of or between Grid rows. The drop | * The drop event can happen either on top of or between Grid rows. The drop | ||||
* is either | * is either | ||||
* <ul> | * <ul> | ||||
* <li><i>above</i> a row when the cursor is over a specified portion of | |||||
* the top part of the row,</li> | |||||
* <li><i>above</i> a row when the cursor is over a specified portion of the | |||||
* top part of the row,</li> | |||||
* <li><i>below</i> when the cursor is over a specified portion of the | * <li><i>below</i> when the cursor is over a specified portion of the | ||||
* bottom part of the row, or</li> | * bottom part of the row, or</li> | ||||
* <li><i>on top</i> when the cursor is over the row but doesn't match the | * <li><i>on top</i> when the cursor is over the row but doesn't match the |
* corresponding client side events happen on the drag source Grid. | * corresponding client side events happen on the drag source Grid. | ||||
* | * | ||||
* @author Vaadin Ltd. | * @author Vaadin Ltd. | ||||
* @since | |||||
* @since 8.1 | |||||
*/ | */ | ||||
public interface GridDragSourceRpc extends ServerRpc { | public interface GridDragSourceRpc extends ServerRpc { | ||||
* Called when dragstart event happens on client side. | * Called when dragstart event happens on client side. | ||||
* | * | ||||
* @param draggedItemKeys | * @param draggedItemKeys | ||||
* Keys of the items in Grid being dragged. | |||||
* Keys of the items in Grid being dragged. | |||||
*/ | */ | ||||
public void dragStart(List<String> draggedItemKeys); | public void dragStart(List<String> draggedItemKeys); | ||||
* Called when dragend event happens on client side. | * Called when dragend event happens on client side. | ||||
* | * | ||||
* @param dropEffect | * @param dropEffect | ||||
* Drop effect of the dragend event, extracted from {@code | |||||
* Drop effect of the dragend event, extracted from {@code | |||||
* DataTransfer.dropEffect} parameter. | * DataTransfer.dropEffect} parameter. | ||||
* @param draggedItemKeys | * @param draggedItemKeys | ||||
* Keys of the items in Grid having been dragged. | |||||
* Keys of the items in Grid having been dragged. | |||||
*/ | */ | ||||
public void dragEnd(DropEffect dropEffect, List<String> draggedItemKeys); | public void dragEnd(DropEffect dropEffect, List<String> draggedItemKeys); | ||||
} | } |
* State class containing parameters for GridDragSource. | * State class containing parameters for GridDragSource. | ||||
* | * | ||||
* @author Vaadin Ltd | * @author Vaadin Ltd | ||||
* @since | |||||
* @since 8.1 | |||||
*/ | */ | ||||
public class GridDragSourceState extends DragSourceState { | public class GridDragSourceState extends DragSourceState { | ||||
* drop target Grid. | * drop target Grid. | ||||
* | * | ||||
* @author Vaadin Ltd. | * @author Vaadin Ltd. | ||||
* @since | |||||
* @since 8.1 | |||||
*/ | */ | ||||
public interface GridDropTargetRpc extends ServerRpc { | public interface GridDropTargetRpc extends ServerRpc { | ||||
* Called when drop event happens on client side. | * Called when drop event happens on client side. | ||||
* | * | ||||
* @param dataTransferText | * @param dataTransferText | ||||
* Data of type {@code "text"} from the {@code DataTransfer} | |||||
* object. | |||||
* Data of type {@code "text"} from the {@code DataTransfer} | |||||
* object. | |||||
* @param rowKey | * @param rowKey | ||||
* Key of the row on which the drop event occured. | |||||
* Key of the row on which the drop event occured. | |||||
* @param dropLocation | * @param dropLocation | ||||
* Location of the drop within the row. | |||||
* Location of the drop within the row. | |||||
*/ | */ | ||||
public void drop(String dataTransferText, String rowKey, | public void drop(String dataTransferText, String rowKey, | ||||
DropLocation dropLocation); | DropLocation dropLocation); |
* State class containing parameters for GridDropTarget. | * State class containing parameters for GridDropTarget. | ||||
* | * | ||||
* @author Vaadin Ltd | * @author Vaadin Ltd | ||||
* @since | |||||
* @since 8.1 | |||||
*/ | */ | ||||
public class GridDropTargetState extends DropTargetState { | public class GridDropTargetState extends DropTargetState { | ||||