summaryrefslogtreecommitdiffstats
path: root/shared
diff options
context:
space:
mode:
authorAdam Wagner <wbadam@users.noreply.github.com>2017-07-03 13:36:17 +0200
committerHenri Sara <henri.sara@gmail.com>2017-07-03 14:36:17 +0300
commit6ecef502864ed201b468a68c00676beb401f21c7 (patch)
tree2f96341ebcb02bced9092e4d3196266ee2eb2740 /shared
parent4d085fd8b3589db4fbbdcd52643cee4b20f70e31 (diff)
downloadvaadin-framework-6ecef502864ed201b468a68c00676beb401f21c7.tar.gz
vaadin-framework-6ecef502864ed201b468a68c00676beb401f21c7.zip
Add criteria API to make it easier to set acceptance criteria for drag and drop (#9605)
Fixes #9600
Diffstat (limited to 'shared')
-rw-r--r--shared/src/main/java/com/vaadin/shared/ui/dnd/DragSourceState.java7
-rw-r--r--shared/src/main/java/com/vaadin/shared/ui/dnd/DropTargetState.java17
-rw-r--r--shared/src/main/java/com/vaadin/shared/ui/dnd/criteria/ComparisonOperator.java55
-rw-r--r--shared/src/main/java/com/vaadin/shared/ui/dnd/criteria/Criterion.java215
-rw-r--r--shared/src/main/java/com/vaadin/shared/ui/dnd/criteria/Payload.java125
5 files changed, 418 insertions, 1 deletions
diff --git a/shared/src/main/java/com/vaadin/shared/ui/dnd/DragSourceState.java b/shared/src/main/java/com/vaadin/shared/ui/dnd/DragSourceState.java
index 0fefd05665..42e3bffc07 100644
--- a/shared/src/main/java/com/vaadin/shared/ui/dnd/DragSourceState.java
+++ b/shared/src/main/java/com/vaadin/shared/ui/dnd/DragSourceState.java
@@ -21,6 +21,7 @@ import java.util.List;
import java.util.Map;
import com.vaadin.shared.communication.SharedState;
+import com.vaadin.shared.ui.dnd.criteria.Payload;
/**
* State class containing parameters for DragSourceExtension.
@@ -75,4 +76,10 @@ public class DragSourceState extends SharedState {
* Used to store data in the {@code DataTransfer} object for the drag event.
*/
public Map<String, String> data = new HashMap<>();
+
+ /**
+ * Payload for comparing against acceptance criteria. Transferred in the
+ * {@code DataTransfer} object as data type.
+ */
+ public Map<String, Payload> payload = new HashMap<>();
}
diff --git a/shared/src/main/java/com/vaadin/shared/ui/dnd/DropTargetState.java b/shared/src/main/java/com/vaadin/shared/ui/dnd/DropTargetState.java
index c36d97f1dc..edd8beddbd 100644
--- a/shared/src/main/java/com/vaadin/shared/ui/dnd/DropTargetState.java
+++ b/shared/src/main/java/com/vaadin/shared/ui/dnd/DropTargetState.java
@@ -15,7 +15,11 @@
*/
package com.vaadin.shared.ui.dnd;
+import java.util.ArrayList;
+import java.util.List;
+
import com.vaadin.shared.communication.SharedState;
+import com.vaadin.shared.ui.dnd.criteria.Criterion;
/**
* State class containing parameters for DropTargetExtension.
@@ -32,5 +36,16 @@ public class DropTargetState extends SharedState {
/**
* Criteria script to allow drop event on the element
*/
- public String dropCriteria;
+ public String criteriaScript;
+
+ /**
+ * List of criteria to compare against the payload.
+ */
+ public List<Criterion> criteria = new ArrayList<>();
+
+ /**
+ * Declares whether any or all of the given criteria should match the
+ * payload
+ */
+ public Criterion.Match criteriaMatch = Criterion.Match.ANY;
}
diff --git a/shared/src/main/java/com/vaadin/shared/ui/dnd/criteria/ComparisonOperator.java b/shared/src/main/java/com/vaadin/shared/ui/dnd/criteria/ComparisonOperator.java
new file mode 100644
index 0000000000..d7ab6ff339
--- /dev/null
+++ b/shared/src/main/java/com/vaadin/shared/ui/dnd/criteria/ComparisonOperator.java
@@ -0,0 +1,55 @@
+/*
+ * 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.shared.ui.dnd.criteria;
+
+/**
+ * Comparison operator for drag and drop acceptance criterion.
+ *
+ * @author Vaadin Ltd.
+ * @since 8.1
+ */
+public enum ComparisonOperator {
+
+ /**
+ * Smaller than operator ("<").
+ */
+ SMALLER_THAN,
+
+ /**
+ * Smaller than or equals to operator ("<=").
+ */
+ SMALLER_THAN_OR_EQUALS,
+
+ /**
+ * Equals to operator ("==").
+ */
+ EQUALS,
+
+ /**
+ * Greater than or equals to operator (">=").
+ */
+ GREATER_THAN_OR_EQUALS,
+
+ /**
+ * Greater than operator (">").
+ */
+ GREATER_THAN,
+
+ /**
+ * Not equals operator ("!=").
+ */
+ NOT_EQUALS
+}
diff --git a/shared/src/main/java/com/vaadin/shared/ui/dnd/criteria/Criterion.java b/shared/src/main/java/com/vaadin/shared/ui/dnd/criteria/Criterion.java
new file mode 100644
index 0000000000..c90637a6e0
--- /dev/null
+++ b/shared/src/main/java/com/vaadin/shared/ui/dnd/criteria/Criterion.java
@@ -0,0 +1,215 @@
+/*
+ * 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.shared.ui.dnd.criteria;
+
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.Optional;
+
+/**
+ * Stores parameters for the drag and drop acceptance criterion defined using
+ * the criteria API.
+ * <p>
+ * When data is dragged over a drop target, the value here is compared to the
+ * payload added in DropTargetExtension with same key and value type.
+ *
+ * @author Vaadin Ltd
+ * @since 8.1
+ */
+public class Criterion implements Serializable {
+
+ /**
+ * Declares whether all or any of the given criteria should match when
+ * compared against the payload.
+ */
+ public enum Match {
+ /**
+ * When compared to the payload, the drop will be accepted if any of the
+ * criteria matches.
+ */
+ ANY,
+
+ /**
+ * When compared to the payload, the drop will be accepted only if all
+ * of the given criteria matches.
+ */
+ ALL
+ }
+
+ private String key;
+ private String value;
+ private Payload.ValueType valueType;
+ private ComparisonOperator operator;
+
+ /**
+ * Mandatory zero param constructor.
+ */
+ private Criterion() {
+
+ }
+
+ /**
+ * Creates a criterion object with the default comparison operator {@link
+ * ComparisonOperator#EQUALS}.
+ *
+ * @param key
+ * key of the payload to be compared
+ * @param value
+ * value of the payload to be compared
+ */
+ public Criterion(String key, String value) {
+ this(key, ComparisonOperator.EQUALS, value, Payload.ValueType.STRING);
+ }
+
+ /**
+ * Creates a criterion object.
+ *
+ * @param key
+ * key of the payload to be compared
+ * @param operator
+ * comparison operator
+ * @param value
+ * value of the payload to be compared
+ */
+ public Criterion(String key, ComparisonOperator operator, int value) {
+ this(key, operator, String.valueOf(value), Payload.ValueType.INTEGER);
+ }
+
+ /**
+ * Creates a criterion object.
+ *
+ * @param key
+ * key of the payload to be compared
+ * @param operator
+ * comparison operator
+ * @param value
+ * value of the payload to be compared
+ */
+ public Criterion(String key, ComparisonOperator operator, double value) {
+ this(key, operator, String.valueOf(value), Payload.ValueType.DOUBLE);
+ }
+
+ /**
+ * Creates a criterion object.
+ *
+ * @param key
+ * key of the payload to be compared
+ * @param operator
+ * comparison operator
+ * @param value
+ * value of the payload to be compared
+ * @param valueType
+ * type of the payload to be compared
+ */
+ private Criterion(String key, ComparisonOperator operator, String value,
+ Payload.ValueType valueType) {
+ this.key = key;
+ this.value = value;
+ this.valueType = valueType;
+ this.operator = operator;
+ }
+
+ /**
+ * Gets the key of the payload to be compared
+ *
+ * @return key of the payload to be compared
+ */
+ public String getKey() {
+ return key;
+ }
+
+ /**
+ * Gets the value of the payload to be compared
+ *
+ * @return value of the payload to be compared
+ */
+ public String getValue() {
+ return value;
+ }
+
+ /**
+ * Gets the type of the payload value to be compared
+ *
+ * @return type of the payload value to be compared
+ */
+ public Payload.ValueType getValueType() {
+ return valueType;
+ }
+
+ /**
+ * Gets the comparison operator.
+ *
+ * @return operator to be used when comparing payload value with criterion
+ */
+ public ComparisonOperator getOperator() {
+ return operator;
+ }
+
+ /**
+ * Compares this criterion's value to the given payload's value and returns
+ * whether the result matches the criterion's operator. The comparison is
+ * done with the payload whose key and value type match the criterion's key
+ * and value type.
+ *
+ * @param payloadCollection
+ * collection of payloads to compare the criterion against
+ * @return {@code false} if there exists a payload in the collection with
+ * the same key and value type and it doesn't match the criterion, {@code
+ * true} otherwise
+ */
+ public boolean resolve(Collection<Payload> payloadCollection) {
+ Optional<Payload> payload = payloadCollection.stream()
+ .filter(p -> p.getKey().equals(key) && p.getValueType()
+ .equals(valueType)).findAny();
+
+ return payload.map(this::compareCriterionValue).orElse(true);
+ }
+
+ private boolean compareCriterionValue(Payload payload) {
+ int result;
+
+ switch (valueType) {
+ case STRING:
+ default:
+ result = value.compareTo(payload.getValue());
+ break;
+ case INTEGER:
+ result = Integer.valueOf(value)
+ .compareTo(Integer.valueOf(payload.getValue()));
+ break;
+ case DOUBLE:
+ result = Double.valueOf(value)
+ .compareTo(Double.valueOf(payload.getValue()));
+ break;
+ }
+
+ switch (operator) {
+ case SMALLER_THAN:
+ return result < 0;
+ case SMALLER_THAN_OR_EQUALS:
+ return result <= 0;
+ case EQUALS:
+ default:
+ return result == 0;
+ case GREATER_THAN_OR_EQUALS:
+ return result >= 0;
+ case GREATER_THAN:
+ return result > 0;
+ case NOT_EQUALS:
+ return result != 0;
+ }
+ }
+}
diff --git a/shared/src/main/java/com/vaadin/shared/ui/dnd/criteria/Payload.java b/shared/src/main/java/com/vaadin/shared/ui/dnd/criteria/Payload.java
new file mode 100644
index 0000000000..ffa3d0a1e6
--- /dev/null
+++ b/shared/src/main/java/com/vaadin/shared/ui/dnd/criteria/Payload.java
@@ -0,0 +1,125 @@
+/*
+ * 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.shared.ui.dnd.criteria;
+
+import java.io.Serializable;
+
+/**
+ * Stores key/value pairs and the value type. Payload is set in
+ * DragSourceExtension and is transferred during drag operation. It is used for
+ * comparing values to acceptance criteria.
+ */
+public class Payload implements Serializable {
+
+ /**
+ * Type of the payload's value.
+ */
+ public enum ValueType {
+ STRING, INTEGER, DOUBLE;
+ }
+
+ /**
+ * Prefix of the payload data type.
+ */
+ public static final String ITEM_PREFIX = "v-item";
+
+ private String key;
+ private String value;
+ private ValueType valueType;
+
+ /**
+ * Mandatory zero arg constructor.
+ */
+ private Payload() {
+
+ }
+
+ /**
+ * Creates a payload object.
+ *
+ * @param key
+ * key of the payload
+ * @param value
+ * value of the payload
+ * @param valueType
+ * type of the payload value
+ */
+ public Payload(String key, String value, ValueType valueType) {
+ this.key = key;
+ this.value = value;
+ this.valueType = valueType;
+ }
+
+ /**
+ * Gets the key of this payload.
+ *
+ * @return key identifying this payload
+ */
+ public String getKey() {
+ return key;
+ }
+
+ /**
+ * Gets the value of this payload.
+ *
+ * @return value of this payload
+ */
+ public String getValue() {
+ return value;
+ }
+
+ /**
+ * Gets the value type of this payload.
+ *
+ * @return the type of the value of this payload
+ */
+ public ValueType getValueType() {
+ return valueType;
+ }
+
+ /**
+ * Returns the string representation of this payload. It is used as the data
+ * type in the {@code DataTransfer} object.
+ *
+ * @return the string representation of this payload
+ */
+ public String getPayloadString() {
+ return ITEM_PREFIX + ":" + valueType.name().toLowerCase() + ":" + key
+ + ":" + value;
+ }
+
+ /**
+ * Parses a payload string and returns a payload object represented by that
+ * string.
+ *
+ * @param payloadString
+ * string that represents a payload object
+ * @return a payload object represented by the given string
+ */
+ public static Payload parse(String payloadString) {
+ String[] parts = payloadString.split(":");
+
+ if (parts.length != 4 || !ITEM_PREFIX.equals(parts[0])) {
+ throw new IllegalArgumentException(
+ "Data type does not have a valid payload format");
+ }
+
+ // Create payload object of the given parts. Value type is converted to
+ // upper case to match the enum's case.
+ return new Payload(parts[2], parts[3],
+ ValueType.valueOf(parts[1].toUpperCase()));
+ }
+}