1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
/*
* Copyright 2000-2016 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.event.dnd;
import java.util.Objects;
import com.vaadin.server.AbstractExtension;
import com.vaadin.shared.Registration;
import com.vaadin.shared.ui.dnd.DropEffect;
import com.vaadin.shared.ui.dnd.DropTargetRpc;
import com.vaadin.shared.ui.dnd.DropTargetState;
import com.vaadin.ui.AbstractComponent;
/**
* Extension to add drop target functionality to a widget for using HTML5 drag
* and drop.
*
* @param <T>
* Type of the component to be extended.
* @author Vaadin Ltd
* @since 8.1
*/
public class DropTargetExtension<T extends AbstractComponent> extends
AbstractExtension {
/**
* Extends {@code target} component and makes it a drop target.
*
* @param target
* Component to be extended.
*/
public DropTargetExtension(T target) {
registerRpc((DropTargetRpc) (dataTransferText, dropEffect) -> {
DropEvent<T> event = new DropEvent<>(target, dataTransferText,
dropEffect, getUI().getActiveDragSource());
fireEvent(event);
});
super.extend(target);
}
/**
* Sets the drop effect for the current drop target. Used for the client
* side {@code DataTransfer.dropEffect} parameter.
* <p>
* Default value is browser dependent and can depend on e.g. modifier keys.
*
* @param dropEffect
* The drop effect to be set. Cannot be {@code null}.
*/
public void setDropEffect(DropEffect dropEffect) {
if (dropEffect == null) {
throw new IllegalArgumentException("Drop effect cannot be null.");
}
if (!Objects.equals(getState(false).dropEffect, dropEffect)) {
getState().dropEffect = dropEffect;
}
}
/**
* Returns the drop effect for the current drop target.
*
* @return The drop effect of this drop target.
*/
public DropEffect getDropEffect() {
return getState(false).dropEffect;
}
/**
* Sets criteria to allow dragover event on the current drop target. The
* script executes when dragover event happens and stops the event in case
* the script returns {@code false}.
* <p>
* <b>IMPORTANT:</b> 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.
* <p>
* Example:
* <pre>
* target.setDropCriteria(
* // If dragged source contains a URL, allow it to be dragged over
* "if (event.dataTransfer.types.includes('text/uri-list')) {" +
* " return true;" +
* "}" +
*
* // Otherwise cancel the event"
* "return false;");
* </pre>
*
* @param criteriaScript
* JavaScript to be executed when dragover event happens or {@code
* null} to clear.
*/
public void setDragOverCriteria(String criteriaScript) {
if (!Objects.equals(getState(false).dragOverCriteria, criteriaScript)) {
getState().dragOverCriteria = criteriaScript;
}
}
/**
* Returns the criteria for allowing dragover event on the current drop
* target.
*
* @return JavaScript that executes when dragover event happens.
*/
public String getDragOverCriteria() {
return getState(false).dragOverCriteria;
}
/**
* Sets criteria to allow drop event on the current drop target. The script
* executes when drop event happens and stops the event in case the script
* returns {@code false}.
* <p>
* <b>IMPORTANT:</b> 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.
* <p>
* Example:
* <pre>
* target.setDropCriteria(
* // 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;");
* </pre>
*
* @param criteriaScript
* JavaScript to be executed when drop event happens or {@code null}
* to clear.
*/
public void setDropCriteria(String criteriaScript) {
if (!Objects.equals(getState(false).dropCriteria, criteriaScript)) {
getState().dropCriteria = criteriaScript;
}
}
/**
* Returns the criteria for allowing drop event on the current drop target.
*
* @return JavaScript that executes when drop event happens.
*/
public String getDropCriteria() {
return getState(false).dropCriteria;
}
/**
* Attaches drop listener for the current drop target. {@link
* DropListener#drop(DropEvent)} is called when drop event happens on the
* client side.
*
* @param listener
* Listener to handle drop event.
* @return Handle to be used to remove this listener.
*/
public Registration addDropListener(DropListener<T> listener) {
return addListener(DropEvent.class, listener, DropListener.DROP_METHOD);
}
@Override
protected DropTargetState getState() {
return (DropTargetState) super.getState();
}
@Override
protected DropTargetState getState(boolean markAsDirty) {
return (DropTargetState) super.getState(markAsDirty);
}
/**
* Returns the component this extension is attached to.
*
* @return Extended component.
*/
@Override
@SuppressWarnings("unchecked")
public T getParent() {
return (T) super.getParent();
}
}
|