com.vaadin.ui.dnd.
Class DropTargetExtension<T extends AbstractComponent>
- java.lang.Object
-
- com.vaadin.server.AbstractClientConnector
-
- com.vaadin.server.AbstractExtension
-
- com.vaadin.ui.dnd.DropTargetExtension<T>
-
Type Parameters:
T
- Type of the component to be extended.All Implemented Interfaces:
MethodEventSource
,ClientConnector
,Extension
,Connector
,Serializable
Direct Known Subclasses:
public class DropTargetExtension<T extends AbstractComponent> extends AbstractExtension
Extension to make a component a drop target for HTML5 drag and drop functionality.
Since:
8.1
Author:
Vaadin Ltd
See Also:
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface com.vaadin.server.ClientConnector
ClientConnector.AttachEvent, ClientConnector.AttachListener, ClientConnector.ConnectorErrorEvent, ClientConnector.DetachEvent, ClientConnector.DetachListener
-
-
Constructor Summary
Constructors Constructor Description DropTargetExtension(T target)
Extends
target
component and makes it a drop target.
-
Method Summary
All Methods Modifier and Type Method Description Registration
addDropListener(DropListener<T> listener)
Attaches drop listener for the current drop target.
void
attach()
Notifies the connector that it is connected to a VaadinSession (and therefore also to a UI).
String
getDropCriteriaScript()
Gets the criteria script that determines whether a drop is allowed.
DropEffect
getDropEffect()
Returns the drop effect for the current drop target.
T
getParent()
Returns the component this extension is attached to.
protected DropTargetState
getState()
Returns the shared state for this connector.
protected DropTargetState
getState(boolean markAsDirty)
Returns the shared state for this connector.
protected void
onDrop(List<String> types, Map<String,String> data, DropEffect dropEffect, MouseEventDetails mouseEventDetails)
Invoked when a
drop
has been received from client side.protected void
registerDropTargetRpc()
Registers the server side RPC methods invoked from client side on
drop
event.void
setDropCriteria(Criterion.Match match, Criterion... criteria)
Sets multiple drop criteria to allow drop on this drop target.
void
setDropCriteriaScript(String criteriaScript)
Sets a criteria script in JavaScript to allow drop on this drop target.
void
setDropCriterion(String key, ComparisonOperator operator, double value)
Set a drop criterion to allow drop on this drop target.
void
setDropCriterion(String key, ComparisonOperator operator, int value)
Set a drop criterion to allow drop on this drop target.
void
setDropCriterion(String key, String value)
Set a drop criterion to allow drop on this drop target.
void
setDropEffect(DropEffect dropEffect)
Sets the drop effect for the current drop target.
-
Methods inherited from class com.vaadin.server.AbstractExtension
extend, getSupportedParentType, remove, setParent
-
Methods inherited from class com.vaadin.server.AbstractClientConnector
addAttachListener, addDetachListener, addExtension, addListener, addListener, addListener, addListener, addListener, addListener, addMethodInvocationToQueue, beforeClientResponse, createState, detach, encodeState, equals, fireEvent, getAllChildrenIterable, getConnectorId, getErrorHandler, getExtensions, getListeners, getResource, getRpcManager, getRpcProxy, getSession, getStateType, getUI, handleConnectorRequest, hashCode, hasListeners, isAttached, isConnectorEnabled, isThis, markAsDirty, markAsDirtyRecursive, registerRpc, registerRpc, removeAttachListener, removeDetachListener, removeExtension, removeListener, removeListener, removeListener, removeListener, removeListener, requestRepaint, requestRepaintAll, retrievePendingRpcCalls, setErrorHandler, setResource, updateDiffstate
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.vaadin.server.ClientConnector
addAttachListener, addDetachListener, beforeClientResponse, detach, encodeState, getErrorHandler, getExtensions, getRpcManager, getStateType, getUI, handleConnectorRequest, isAttached, isConnectorEnabled, markAsDirty, markAsDirtyRecursive, removeAttachListener, removeDetachListener, removeExtension, requestRepaint, requestRepaintAll, retrievePendingRpcCalls, setErrorHandler
-
Methods inherited from interface com.vaadin.shared.Connector
getConnectorId
-
-
-
-
Constructor Detail
-
DropTargetExtension
public DropTargetExtension(T target)
Extends
target
component and makes it a drop target.Parameters:
target
- Component to be extended.
-
-
Method Detail
-
attach
public void attach()
Description copied from interface:
ClientConnector
Notifies the connector that it is connected to a VaadinSession (and therefore also to a UI).
The caller of this method is
Component.setParent(HasComponents)
if the parent is itself already attached to the session. If not, the parent will call theClientConnector.attach()
for all its children when it is attached to the session. This method is always called before the connector's data is sent to the client-side for the first time.The attachment logic is implemented in
AbstractClientConnector
.Specified by:
attach
in interfaceClientConnector
Overrides:
attach
in classAbstractClientConnector
-
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 theDropEvent
.Parameters:
types
- List of data types fromDataTransfer.types
object.data
- Map containing all types and corresponding data from theDataTransfer
object.dropEffect
- the drop effectmouseEventDetails
- 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
anddragover
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 ornull
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 setSee Also:
-
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()
orsetDropCriteria()
methods. If no criteria is set, then the drop is always accepted, if the setdropEffect
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 ornull
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:
-
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 comparedvalue
- value to be compared to the payload's valueSee Also:
-
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 comparedoperator
- comparison operator to be usedvalue
- value to be compared to the payload's valueSee Also:
-
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 comparedoperator
- comparison operator to be usedvalue
- value to be compared to the payload's valueSee Also:
-
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 targetcriteria
- 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 classAbstractClientConnector
Returns:
The shared state for this connector. Never null.
-
getState
protected DropTargetState getState(boolean markAsDirty)
Description copied from class:
AbstractClientConnector
Returns the shared state for this connector.
Overrides:
getState
in classAbstractClientConnector
Parameters:
markAsDirty
- true if the connector should automatically be marked dirty, false otherwiseReturns:
The shared state for this connector. Never null.
See Also:
-
getParent
public T getParent()
Returns the component this extension is attached to.
Specified by:
getParent
in interfaceClientConnector
Specified by:
getParent
in interfaceConnector
Overrides:
getParent
in classAbstractExtension
Returns:
Extended component.
-
-