com.vaadin.ui.dnd.

Class DropTargetExtension<T extends AbstractComponent>

    • Constructor Detail

      • DropTargetExtension

        public DropTargetExtension​(T target)

        Extends target component and makes it a drop target.

        Parameters:

        target - Component to be extended.

    • Method Detail

      • registerDropTargetRpc

        protected void registerDropTargetRpc()

        Registers the server side RPC methods invoked from client side on drop event.

        Override this method if you need to have a custom RPC interface for transmitting the drop event with more data. If just need to do additional things before firing the drop event, then you should override onDrop(List, Map, DropEffect, MouseEventDetails) instead.

      • onDrop

        protected void onDrop​(List<String> types,
                              Map<String,​String> data,
                              DropEffect dropEffect,
                              MouseEventDetails mouseEventDetails)

        Invoked when a drop has been received from client side. Fires the DropEvent.

        Parameters:

        types - List of data types from DataTransfer.types object.

        data - Map containing all types and corresponding data from the DataTransfer object.

        dropEffect - the drop effect

        mouseEventDetails - mouse event details object containing information about the drop event

      • setDropEffect

        public void setDropEffect​(DropEffect dropEffect)

        Sets the drop effect for the current drop target. This is set to the dropEffect on dragenter and dragover events.

        NOTE: If the drop effect that doesn't match the dropEffect / effectAllowed of the drag source, it DOES NOT prevent drop on IE and Safari! For FireFox and Chrome the drop is prevented if there they don't match.

        Default value is browser dependent and can depend on e.g. modifier keys.

        From Moz Foundation: "You can modify the dropEffect property during the dragenter or dragover events, if for example, a particular drop target only supports certain operations. You can modify the dropEffect property to override the user effect, and enforce a specific drop operation to occur. Note that this effect must be one listed within the effectAllowed property. Otherwise, it will be set to an alternate value that is allowed."

        Parameters:

        dropEffect - the drop effect to be set or null to not modify

      • getDropEffect

        public DropEffect getDropEffect()

        Returns the drop effect for the current drop target.

        Returns:

        The drop effect of this drop target or null if none set

        See Also:

        setDropEffect(DropEffect)

      • setDropCriteriaScript

        public void setDropCriteriaScript​(String criteriaScript)

        Sets a criteria script in JavaScript to allow drop on this drop target. The script is executed when something is dragged on top of the target, and the drop is not allowed in case the script returns false.

        Drop will be allowed if it passes both this criteria script and the criteria set via any of setDropCriterion() or setDropCriteria() methods. If no criteria is set, then the drop is always accepted, if the set dropEffect matches the drag source.

        IMPORTANT: Construct the criteria script carefully and do not include untrusted sources such as user input. Always keep in mind that the script is executed on the client as is.

        Example:

         target.setDropCriterion(
                 // If dragged source contains a URL, allow it to be dropped
                 "if (event.dataTransfer.types.includes('text/uri-list')) {"
                         + "    return true;" + "}" +
        
                         // Otherwise cancel the event
                         "return false;");
         

        Parameters:

        criteriaScript - JavaScript to be executed when drop event happens or null to clear.

      • getDropCriteriaScript

        public String getDropCriteriaScript()

        Gets the criteria script that determines whether a drop is allowed. If the script returns false, then it is determined the drop is not allowed.

        Returns:

        JavaScript that executes when drop event happens.

        See Also:

        setDropCriteriaScript(String)

      • setDropCriterion

        public void setDropCriterion​(String key,
                                     String value)

        Set a drop criterion to allow drop on this drop target. When data is dragged on top of the drop target, the given value is compared to the drag source's payload with the same key. The drag passes this criterion if the value of the payload and the value given here are equal.

        Note that calling this method will overwrite the previously set criteria. To set multiple criteria, call the setDropCriteria(Criterion.Match, Criterion...) method.

        To handle more complex criteria, define a custom script with setDropCriteriaScript(String). Drop will be allowed if both this criterion and the criteria script are passed.

        Parameters:

        key - key of the payload to be compared

        value - value to be compared to the payload's value

        See Also:

        DragSourceExtension.setPayload(String, String)

      • setDropCriterion

        public void setDropCriterion​(String key,
                                     ComparisonOperator operator,
                                     int value)

        Set a drop criterion to allow drop on this drop target. When data is dragged on top of the drop target, the given value is compared to the drag source's payload with the same key. The drag passes this criterion if the value of the payload compared to the given value using the given operator holds.

        Note that calling this method will overwrite the previously set criteria. To set multiple criteria, call the setDropCriteria(Criterion.Match, Criterion...) method.

        To handle more complex criteria, define a custom script with setDropCriteriaScript(String). Drop will be allowed if both this criterion and the criteria script are passed.

        Parameters:

        key - key of the payload to be compared

        operator - comparison operator to be used

        value - value to be compared to the payload's value

        See Also:

        DragSourceExtension.setPayload(String, int)

      • setDropCriterion

        public void setDropCriterion​(String key,
                                     ComparisonOperator operator,
                                     double value)

        Set a drop criterion to allow drop on this drop target. When data is dragged on top of the drop target, the given value is compared to the drag source's payload with the same key. The drag passes this criterion if the value of the payload compared to the given value using the given operator holds.

        Note that calling this method will overwrite the previously set criteria. To set multiple criteria, call the setDropCriteria(Criterion.Match, Criterion...) method.

        To handle more complex criteria, define a custom script with setDropCriteriaScript(String). Drop will be allowed if both this criterion and the criteria script are passed.

        Parameters:

        key - key of the payload to be compared

        operator - comparison operator to be used

        value - value to be compared to the payload's value

        See Also:

        DragSourceExtension.setPayload(String, double)

      • setDropCriteria

        public void setDropCriteria​(Criterion.Match match,
                                    Criterion... criteria)

        Sets multiple drop criteria to allow drop on this drop target. When data is dragged on top of the drop target, the value of the given criteria is compared to the drag source's payload with the same key.

        The drag passes these criteria if, depending on match, any or all of the criteria matches the payload, that is the value of the payload compared to the value of the criterion using the criterion's operator holds.

        Note that calling this method will overwrite the previously set criteria.

        To handle more complex criteria, define a custom script with setDropCriteriaScript(String). Drop will be allowed if both this criterion and the criteria script are passed.

        Parameters:

        match - defines whether any or all of the given criteria should match to allow drop on this drop target

        criteria - criteria to be compared to the payload

      • addDropListener

        public Registration addDropListener​(DropListener<T> listener)

        Attaches drop listener for the current drop target. DropListener.drop(DropEvent) is called when drop event happens on the client side.

        Parameters:

        listener - Listener to handle drop event.

        Returns:

        Handle to be used to remove this listener.

      • getState

        protected DropTargetState getState()

        Description copied from class: AbstractClientConnector

        Returns the shared state for this connector. The shared state object is shared between the server connector and the client connector. Changes are only communicated from the server to the client and not in the other direction.

        As a side effect, marks the connector dirty so any changes done to the state will be sent to the client. Use getState(false) to avoid marking the connector as dirty.

        Overrides:

        getState in class AbstractClientConnector

        Returns:

        The shared state for this connector. Never null.