summaryrefslogtreecommitdiffstats
path: root/server/src/com/vaadin/data/util
diff options
context:
space:
mode:
authorTeemu Suo-Anttila <teemusa@vaadin.com>2014-09-15 17:22:12 +0300
committerHenrik Paul <henrik@vaadin.com>2014-09-17 07:05:07 +0000
commit3b24655d80aaaffedfc18747674b29faf2b4d0e5 (patch)
treecf774f454c043c1afd5ab80ede088951d9c50fe9 /server/src/com/vaadin/data/util
parentb17ec1ca3f6bebc51fe206e01b4331730d23dc0d (diff)
downloadvaadin-framework-3b24655d80aaaffedfc18747674b29faf2b4d0e5.tar.gz
vaadin-framework-3b24655d80aaaffedfc18747674b29faf2b4d0e5.zip
Add Container.Sortable interface to GeneratedPropertyContainer (#13334)
Change-Id: Ib6be69b1ede0d3129d9812c78fe961412be7d145
Diffstat (limited to 'server/src/com/vaadin/data/util')
-rw-r--r--server/src/com/vaadin/data/util/GeneratedPropertyContainer.java84
1 files changed, 82 insertions, 2 deletions
diff --git a/server/src/com/vaadin/data/util/GeneratedPropertyContainer.java b/server/src/com/vaadin/data/util/GeneratedPropertyContainer.java
index 63432428c3..6d008667f7 100644
--- a/server/src/com/vaadin/data/util/GeneratedPropertyContainer.java
+++ b/server/src/com/vaadin/data/util/GeneratedPropertyContainer.java
@@ -15,17 +15,21 @@
*/
package com.vaadin.data.util;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
+import java.util.Map.Entry;
import java.util.Set;
import com.google.gwt.thirdparty.guava.common.collect.Sets;
import com.vaadin.data.Container;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
+import com.vaadin.shared.ui.grid.SortDirection;
+import com.vaadin.ui.components.grid.sort.SortOrder;
/**
* Container supporting generated properties.
@@ -33,10 +37,12 @@ import com.vaadin.data.Property;
* @since
* @author Vaadin Ltd
*/
-public class GeneratedPropertyContainer implements Container.Indexed {
+public class GeneratedPropertyContainer implements Container.Indexed,
+ Container.Sortable {
- private Container.Indexed wrappedContainer;
+ private final Container.Indexed wrappedContainer;
private final Map<Object, PropertyValueGenerator<?>> propertyGenerators;
+ private Sortable sortableContainer = null;
/**
* Property implementation for generated properties
@@ -138,6 +144,9 @@ public class GeneratedPropertyContainer implements Container.Indexed {
public GeneratedPropertyContainer(Container.Indexed container) {
wrappedContainer = container;
propertyGenerators = new HashMap<Object, PropertyValueGenerator<?>>();
+ if (wrappedContainer instanceof Sortable) {
+ sortableContainer = (Sortable) wrappedContainer;
+ }
}
/* Functions related to generated properties */
@@ -199,6 +208,77 @@ public class GeneratedPropertyContainer implements Container.Indexed {
}
}
+ /* Sorting functionality */
+
+ @Override
+ public void sort(Object[] propertyId, boolean[] ascending) {
+ if (sortableContainer == null) {
+ new UnsupportedOperationException(
+ "Wrapped container is not Sortable");
+ }
+
+ if (propertyId.length == 0) {
+ sortableContainer.sort(propertyId, ascending);
+ return;
+ }
+
+ List<Object> actualSortProperties = new ArrayList<Object>();
+ List<Boolean> actualSortDirections = new ArrayList<Boolean>();
+
+ for (int i = 0; i < propertyId.length; ++i) {
+ Object property = propertyId[i];
+ SortDirection direction;
+ boolean isAscending = i < ascending.length ? ascending[i] : true;
+ if (isAscending) {
+ direction = SortDirection.ASCENDING;
+ } else {
+ direction = SortDirection.DESCENDING;
+ }
+
+ if (propertyGenerators.containsKey(property)) {
+ for (SortOrder s : propertyGenerators.get(property)
+ .getSortProperties(new SortOrder(property, direction))) {
+ actualSortProperties.add(s.getPropertyId());
+ actualSortDirections
+ .add(s.getDirection() == SortDirection.ASCENDING);
+ }
+ } else {
+ actualSortProperties.add(property);
+ actualSortDirections.add(isAscending);
+ }
+ }
+
+ boolean[] actualAscending = new boolean[actualSortDirections.size()];
+ for (int i = 0; i < actualAscending.length; ++i) {
+ actualAscending[i] = actualSortDirections.get(i);
+ }
+
+ sortableContainer.sort(actualSortProperties.toArray(), actualAscending);
+ }
+
+ @Override
+ public Collection<?> getSortableContainerPropertyIds() {
+ if (sortableContainer == null) {
+ new UnsupportedOperationException(
+ "Wrapped container is not Sortable");
+ }
+
+ Set<Object> sortablePropertySet = new HashSet<Object>(
+ sortableContainer.getSortableContainerPropertyIds());
+ for (Entry<?, PropertyValueGenerator<?>> entry : propertyGenerators
+ .entrySet()) {
+ Object property = entry.getKey();
+ SortOrder order = new SortOrder(property, SortDirection.ASCENDING);
+ if (entry.getValue().getSortProperties(order).length > 0) {
+ sortablePropertySet.add(property);
+ } else {
+ sortablePropertySet.remove(property);
+ }
+ }
+
+ return sortablePropertySet;
+ }
+
/* Item related overrides */
@Override