aboutsummaryrefslogtreecommitdiffstats
path: root/client
diff options
context:
space:
mode:
Diffstat (limited to 'client')
-rw-r--r--client/ivy.xml3
-rw-r--r--client/src/com/vaadin/client/data/AbstractRemoteDataSource.java570
-rw-r--r--client/src/com/vaadin/client/data/CacheStrategy.java183
-rw-r--r--client/src/com/vaadin/client/data/DataChangeHandler.java70
-rw-r--r--client/src/com/vaadin/client/data/DataSource.java188
-rw-r--r--client/src/com/vaadin/client/data/RpcDataSourceConnector.java153
-rw-r--r--client/src/com/vaadin/client/metadata/TypeDataStore.java11
-rw-r--r--client/src/com/vaadin/client/ui/grid/Cell.java85
-rw-r--r--client/src/com/vaadin/client/ui/grid/ColumnConfiguration.java145
-rw-r--r--client/src/com/vaadin/client/ui/grid/DataAvailableEvent.java55
-rw-r--r--client/src/com/vaadin/client/ui/grid/DataAvailableHandler.java37
-rw-r--r--client/src/com/vaadin/client/ui/grid/Escalator.java4626
-rw-r--r--client/src/com/vaadin/client/ui/grid/EscalatorUpdater.java155
-rw-r--r--client/src/com/vaadin/client/ui/grid/FlyweightCell.java194
-rw-r--r--client/src/com/vaadin/client/ui/grid/FlyweightRow.java293
-rw-r--r--client/src/com/vaadin/client/ui/grid/Grid.java2562
-rw-r--r--client/src/com/vaadin/client/ui/grid/GridColumn.java47
-rw-r--r--client/src/com/vaadin/client/ui/grid/GridConnector.java610
-rw-r--r--client/src/com/vaadin/client/ui/grid/GridFooter.java74
-rw-r--r--client/src/com/vaadin/client/ui/grid/GridHeader.java150
-rw-r--r--client/src/com/vaadin/client/ui/grid/GridStaticSection.java532
-rw-r--r--client/src/com/vaadin/client/ui/grid/PositionFunction.java118
-rw-r--r--client/src/com/vaadin/client/ui/grid/Renderer.java45
-rw-r--r--client/src/com/vaadin/client/ui/grid/Row.java48
-rw-r--r--client/src/com/vaadin/client/ui/grid/RowContainer.java194
-rw-r--r--client/src/com/vaadin/client/ui/grid/RowVisibilityChangeEvent.java90
-rw-r--r--client/src/com/vaadin/client/ui/grid/RowVisibilityChangeHandler.java38
-rw-r--r--client/src/com/vaadin/client/ui/grid/ScrollbarBundle.java639
-rw-r--r--client/src/com/vaadin/client/ui/grid/datasources/ListDataSource.java438
-rw-r--r--client/src/com/vaadin/client/ui/grid/datasources/ListSorter.java177
-rw-r--r--client/src/com/vaadin/client/ui/grid/keyevents/AbstractGridKeyEventHandler.java43
-rw-r--r--client/src/com/vaadin/client/ui/grid/keyevents/BodyKeyDownHandler.java28
-rw-r--r--client/src/com/vaadin/client/ui/grid/keyevents/BodyKeyPressHandler.java28
-rw-r--r--client/src/com/vaadin/client/ui/grid/keyevents/BodyKeyUpHandler.java28
-rw-r--r--client/src/com/vaadin/client/ui/grid/keyevents/FooterKeyDownHandler.java28
-rw-r--r--client/src/com/vaadin/client/ui/grid/keyevents/FooterKeyPressHandler.java28
-rw-r--r--client/src/com/vaadin/client/ui/grid/keyevents/FooterKeyUpHandler.java28
-rw-r--r--client/src/com/vaadin/client/ui/grid/keyevents/GridKeyDownEvent.java51
-rw-r--r--client/src/com/vaadin/client/ui/grid/keyevents/GridKeyPressEvent.java51
-rw-r--r--client/src/com/vaadin/client/ui/grid/keyevents/GridKeyUpEvent.java51
-rw-r--r--client/src/com/vaadin/client/ui/grid/keyevents/HeaderKeyDownHandler.java28
-rw-r--r--client/src/com/vaadin/client/ui/grid/keyevents/HeaderKeyPressHandler.java28
-rw-r--r--client/src/com/vaadin/client/ui/grid/keyevents/HeaderKeyUpHandler.java28
-rw-r--r--client/src/com/vaadin/client/ui/grid/renderers/AbstractRendererConnector.java156
-rw-r--r--client/src/com/vaadin/client/ui/grid/renderers/ComplexRenderer.java153
-rw-r--r--client/src/com/vaadin/client/ui/grid/renderers/DateRenderer.java98
-rw-r--r--client/src/com/vaadin/client/ui/grid/renderers/DateRendererConnector.java34
-rw-r--r--client/src/com/vaadin/client/ui/grid/renderers/HtmlRenderer.java42
-rw-r--r--client/src/com/vaadin/client/ui/grid/renderers/NumberRenderer.java64
-rw-r--r--client/src/com/vaadin/client/ui/grid/renderers/NumberRendererConnector.java35
-rw-r--r--client/src/com/vaadin/client/ui/grid/renderers/TextRenderer.java33
-rw-r--r--client/src/com/vaadin/client/ui/grid/renderers/TextRendererConnector.java33
-rw-r--r--client/src/com/vaadin/client/ui/grid/renderers/UnsafeHtmlRendererConnector.java43
-rw-r--r--client/src/com/vaadin/client/ui/grid/renderers/WidgetRenderer.java67
-rw-r--r--client/src/com/vaadin/client/ui/grid/selection/AbstractRowHandleSelectionModel.java65
-rw-r--r--client/src/com/vaadin/client/ui/grid/selection/HasSelectionChangeHandlers.java43
-rw-r--r--client/src/com/vaadin/client/ui/grid/selection/MultiSelectionRenderer.java764
-rw-r--r--client/src/com/vaadin/client/ui/grid/selection/SelectionChangeEvent.java170
-rw-r--r--client/src/com/vaadin/client/ui/grid/selection/SelectionChangeHandler.java39
-rw-r--r--client/src/com/vaadin/client/ui/grid/selection/SelectionModel.java237
-rw-r--r--client/src/com/vaadin/client/ui/grid/selection/SelectionModelMulti.java258
-rw-r--r--client/src/com/vaadin/client/ui/grid/selection/SelectionModelNone.java73
-rw-r--r--client/src/com/vaadin/client/ui/grid/selection/SelectionModelSingle.java139
-rw-r--r--client/src/com/vaadin/client/ui/grid/sort/Sort.java155
-rw-r--r--client/src/com/vaadin/client/ui/grid/sort/SortEvent.java128
-rw-r--r--client/src/com/vaadin/client/ui/grid/sort/SortEventHandler.java38
-rw-r--r--client/src/com/vaadin/client/ui/grid/sort/SortOrder.java72
-rw-r--r--client/tests/src/com/vaadin/client/ui/grid/ListDataSourceTest.java192
-rw-r--r--client/tests/src/com/vaadin/client/ui/grid/PartitioningTest.java104
69 files changed, 16213 insertions, 0 deletions
diff --git a/client/ivy.xml b/client/ivy.xml
index 3abdcf9ba5..6b941af818 100644
--- a/client/ivy.xml
+++ b/client/ivy.xml
@@ -42,6 +42,9 @@
<dependency org="javax.validation" name="validation-api"
rev="1.0.0.GA" conf="build->default,sources" />
+ <!-- Testing dependencies -->
+ <dependency org="org.easymock" name="easymock" rev="3.0"
+ conf="test,ide-> default" />
</dependencies>
diff --git a/client/src/com/vaadin/client/data/AbstractRemoteDataSource.java b/client/src/com/vaadin/client/data/AbstractRemoteDataSource.java
new file mode 100644
index 0000000000..ca2d2bc83e
--- /dev/null
+++ b/client/src/com/vaadin/client/data/AbstractRemoteDataSource.java
@@ -0,0 +1,570 @@
+/*
+ * Copyright 2000-2014 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.client.data;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import com.google.gwt.core.client.Duration;
+import com.google.gwt.core.client.Scheduler;
+import com.google.gwt.core.client.Scheduler.ScheduledCommand;
+import com.vaadin.client.Profiler;
+import com.vaadin.shared.ui.grid.Range;
+
+/**
+ * Base implementation for data sources that fetch data from a remote system.
+ * This class takes care of caching data and communicating with the data source
+ * user. An implementation of this class should override
+ * {@link #requestRows(int, int)} to trigger asynchronously loading of data.
+ * When data is received from the server, new row data should be passed to
+ * {@link #setRowData(int, List)}. {@link #setEstimatedSize(int)} should be used
+ * based on estimations of how many rows are available.
+ *
+ * @since
+ * @author Vaadin Ltd
+ * @param <T>
+ * the row type
+ */
+public abstract class AbstractRemoteDataSource<T> implements DataSource<T> {
+
+ protected class RowHandleImpl extends RowHandle<T> {
+ private T row;
+ private final Object key;
+
+ public RowHandleImpl(final T row, final Object key) {
+ this.row = row;
+ this.key = key;
+ }
+
+ /**
+ * A method for the data source to update the row data.
+ *
+ * @param row
+ * the updated row object
+ */
+ public void setRow(final T row) {
+ this.row = row;
+ assert getRowKey(row).equals(key) : "The old key does not "
+ + "equal the new key for the given row (old: " + key
+ + ", new :" + getRowKey(row) + ")";
+ }
+
+ @Override
+ public T getRow() throws IllegalStateException {
+ if (isPinned()) {
+ return row;
+ } else {
+ throw new IllegalStateException("The row handle for key " + key
+ + " was not pinned");
+ }
+ }
+
+ private boolean isPinned() {
+ return pinnedRows.containsKey(key);
+ }
+
+ @Override
+ public void pin() {
+ Integer count = pinnedCounts.get(key);
+ if (count == null) {
+ count = Integer.valueOf(0);
+ pinnedRows.put(key, this);
+ }
+ pinnedCounts.put(key, Integer.valueOf(count.intValue() + 1));
+ }
+
+ @Override
+ public void unpin() throws IllegalStateException {
+ final Integer count = pinnedCounts.get(key);
+ if (count == null) {
+ throw new IllegalStateException("Row " + row + " with key "
+ + key + " was not pinned to begin with");
+ } else if (count.equals(Integer.valueOf(1))) {
+ pinnedRows.remove(key);
+ pinnedCounts.remove(key);
+ } else {
+ pinnedCounts.put(key, Integer.valueOf(count.intValue() - 1));
+ }
+ }
+
+ @Override
+ protected boolean equalsExplicit(final Object obj) {
+ if (obj instanceof AbstractRemoteDataSource.RowHandleImpl) {
+ /*
+ * Java prefers AbstractRemoteDataSource<?>.RowHandleImpl. I
+ * like the @SuppressWarnings more (keeps the line length in
+ * check.)
+ */
+ @SuppressWarnings("unchecked")
+ final RowHandleImpl rhi = (RowHandleImpl) obj;
+ return key.equals(rhi.key);
+ } else {
+ return false;
+ }
+ }
+
+ @Override
+ protected int hashCodeExplicit() {
+ return key.hashCode();
+ }
+ }
+
+ /**
+ * Records the start of the previously requested range. This is used when
+ * tracking request timings to distinguish between explicit responses and
+ * arbitrary updates pushed from the server.
+ */
+ private int lastRequestStart = -1;
+ private double pendingRequestTime;
+
+ private boolean coverageCheckPending = false;
+
+ private Range requestedAvailability = Range.between(0, 0);
+
+ private Range cached = Range.between(0, 0);
+
+ private final HashMap<Integer, T> rowCache = new HashMap<Integer, T>();
+
+ private DataChangeHandler dataChangeHandler;
+
+ private Range estimatedAvailableRange = Range.between(0, 0);
+
+ private CacheStrategy cacheStrategy = new CacheStrategy.DefaultCacheStrategy();
+
+ private final ScheduledCommand coverageChecker = new ScheduledCommand() {
+ @Override
+ public void execute() {
+ coverageCheckPending = false;
+ checkCacheCoverage();
+ }
+ };
+
+ private Map<Object, Integer> pinnedCounts = new HashMap<Object, Integer>();
+ private Map<Object, RowHandleImpl> pinnedRows = new HashMap<Object, RowHandleImpl>();
+ protected Collection<T> temporarilyPinnedRows = Collections.emptySet();
+
+ /**
+ * Sets the estimated number of rows in the data source.
+ *
+ * @param estimatedSize
+ * the estimated number of available rows
+ */
+ protected void setEstimatedSize(int estimatedSize) {
+ // TODO update dataChangeHandler if size changes
+ estimatedAvailableRange = Range.withLength(0, estimatedSize);
+ }
+
+ private void ensureCoverageCheck() {
+ if (!coverageCheckPending) {
+ coverageCheckPending = true;
+ Scheduler.get().scheduleDeferred(coverageChecker);
+ }
+ }
+
+ @Override
+ public void ensureAvailability(int firstRowIndex, int numberOfRows) {
+ requestedAvailability = Range.withLength(firstRowIndex, numberOfRows);
+
+ /*
+ * Don't request any data right away since the data might be included in
+ * a message that has been received but not yet fully processed.
+ */
+ ensureCoverageCheck();
+ }
+
+ private void checkCacheCoverage() {
+ if (lastRequestStart != -1) {
+ // Anyone clearing lastRequestStart should run this method again
+ return;
+ }
+
+ Profiler.enter("AbstractRemoteDataSource.checkCacheCoverage");
+
+ Range minCacheRange = getMinCacheRange();
+
+ if (!minCacheRange.intersects(cached) || cached.isEmpty()) {
+ /*
+ * Simple case: no overlap between cached data and needed data.
+ * Clear the cache and request new data
+ */
+ rowCache.clear();
+ cached = Range.between(0, 0);
+
+ handleMissingRows(getMaxCacheRange());
+ } else {
+ discardStaleCacheEntries();
+
+ // Might need more rows -> request them
+ if (!minCacheRange.isSubsetOf(cached)) {
+ Range[] missingCachePartition = getMaxCacheRange()
+ .partitionWith(cached);
+ handleMissingRows(missingCachePartition[0]);
+ handleMissingRows(missingCachePartition[2]);
+ }
+ }
+
+ Profiler.leave("AbstractRemoteDataSource.checkCacheCoverage");
+ }
+
+ private void discardStaleCacheEntries() {
+ Range[] cacheParition = cached.partitionWith(getMaxCacheRange());
+ dropFromCache(cacheParition[0]);
+ cached = cacheParition[1];
+ dropFromCache(cacheParition[2]);
+ }
+
+ private void dropFromCache(Range range) {
+ for (int i = range.getStart(); i < range.getEnd(); i++) {
+ rowCache.remove(Integer.valueOf(i));
+ }
+ }
+
+ private void handleMissingRows(Range range) {
+ if (range.isEmpty()) {
+ return;
+ }
+ lastRequestStart = range.getStart();
+ pendingRequestTime = Duration.currentTimeMillis();
+ requestRows(range.getStart(), range.length());
+ }
+
+ /**
+ * Triggers fetching rows from the remote data source.
+ * {@link #setRowData(int, List)} should be invoked with data for the
+ * requested rows when they have been received.
+ *
+ * @param firstRowIndex
+ * the index of the first row to fetch
+ * @param numberOfRows
+ * the number of rows to fetch
+ */
+ protected abstract void requestRows(int firstRowIndex, int numberOfRows);
+
+ @Override
+ public int getEstimatedSize() {
+ return estimatedAvailableRange.length();
+ }
+
+ @Override
+ public T getRow(int rowIndex) {
+ return rowCache.get(Integer.valueOf(rowIndex));
+ }
+
+ @Override
+ public void setDataChangeHandler(DataChangeHandler dataChangeHandler) {
+ this.dataChangeHandler = dataChangeHandler;
+
+ if (dataChangeHandler != null && !cached.isEmpty()) {
+ // Push currently cached data to the implementation
+ dataChangeHandler.dataUpdated(cached.getStart(), cached.length());
+ dataChangeHandler.dataAvailable(cached.getStart(), cached.length());
+ }
+ }
+
+ /**
+ * Informs this data source that updated data has been sent from the server.
+ *
+ * @param firstRowIndex
+ * the index of the first received row
+ * @param rowData
+ * a list of rows, starting from <code>firstRowIndex</code>
+ */
+ protected void setRowData(int firstRowIndex, List<T> rowData) {
+
+ Profiler.enter("AbstractRemoteDataSource.setRowData");
+
+ Range received = Range.withLength(firstRowIndex, rowData.size());
+
+ if (firstRowIndex == lastRequestStart) {
+ // Provide timing information if we know when we asked for this data
+ cacheStrategy.onDataArrive(Duration.currentTimeMillis()
+ - pendingRequestTime, received.length());
+ }
+ lastRequestStart = -1;
+
+ Range maxCacheRange = getMaxCacheRange();
+
+ Range[] partition = received.partitionWith(maxCacheRange);
+
+ Range newUsefulData = partition[1];
+ if (!newUsefulData.isEmpty()) {
+ // Update the parts that are actually inside
+ for (int i = newUsefulData.getStart(); i < newUsefulData.getEnd(); i++) {
+ rowCache.put(Integer.valueOf(i), rowData.get(i - firstRowIndex));
+ }
+
+ if (dataChangeHandler != null) {
+ Profiler.enter("AbstractRemoteDataSource.setRowData notify dataChangeHandler");
+ dataChangeHandler.dataUpdated(newUsefulData.getStart(),
+ newUsefulData.length());
+ Profiler.leave("AbstractRemoteDataSource.setRowData notify dataChangeHandler");
+ }
+
+ // Potentially extend the range
+ if (cached.isEmpty()) {
+ cached = newUsefulData;
+ } else {
+ discardStaleCacheEntries();
+
+ /*
+ * everything might've become stale so we need to re-check for
+ * emptiness.
+ */
+ if (!cached.isEmpty()) {
+ cached = cached.combineWith(newUsefulData);
+ } else {
+ cached = newUsefulData;
+ }
+ }
+ dataChangeHandler.dataAvailable(cached.getStart(), cached.length());
+
+ updatePinnedRows(rowData);
+ }
+
+ if (!partition[0].isEmpty() || !partition[2].isEmpty()) {
+ /*
+ * FIXME
+ *
+ * Got data that we might need in a moment if the container is
+ * updated before the widget settings. Support for this will be
+ * implemented later on.
+ */
+ }
+
+ // Eventually check whether all needed rows are now available
+ ensureCoverageCheck();
+
+ Profiler.leave("AbstractRemoteDataSource.setRowData");
+ }
+
+ private void updatePinnedRows(final List<T> rowData) {
+ for (final T row : rowData) {
+ final Object key = getRowKey(row);
+ final RowHandleImpl handle = pinnedRows.get(key);
+ if (handle != null) {
+ handle.setRow(row);
+ }
+ }
+ }
+
+ /**
+ * Informs this data source that the server has removed data.
+ *
+ * @param firstRowIndex
+ * the index of the first removed row
+ * @param count
+ * the number of removed rows, starting from
+ * <code>firstRowIndex</code>
+ */
+ protected void removeRowData(int firstRowIndex, int count) {
+ Profiler.enter("AbstractRemoteDataSource.removeRowData");
+
+ // pack the cached data
+ for (int i = 0; i < count; i++) {
+ Integer oldIndex = Integer.valueOf(firstRowIndex + count + i);
+ if (rowCache.containsKey(oldIndex)) {
+ Integer newIndex = Integer.valueOf(firstRowIndex + i);
+ rowCache.put(newIndex, rowCache.remove(oldIndex));
+ }
+ }
+
+ Range removedRange = Range.withLength(firstRowIndex, count);
+ if (cached.isSubsetOf(removedRange)) {
+ cached = Range.withLength(0, 0);
+ } else if (removedRange.intersects(cached)) {
+ Range[] partitions = cached.partitionWith(removedRange);
+ Range remainsBefore = partitions[0];
+ Range transposedRemainsAfter = partitions[2].offsetBy(-removedRange
+ .length());
+ cached = remainsBefore.combineWith(transposedRemainsAfter);
+ }
+ setEstimatedSize(getEstimatedSize() - count);
+ dataChangeHandler.dataRemoved(firstRowIndex, count);
+ checkCacheCoverage();
+
+ Profiler.leave("AbstractRemoteDataSource.removeRowData");
+ }
+
+ /**
+ * Informs this data source that new data has been inserted from the server.
+ *
+ * @param firstRowIndex
+ * the destination index of the new row data
+ * @param count
+ * the number of rows inserted
+ */
+ protected void insertRowData(int firstRowIndex, int count) {
+ Profiler.enter("AbstractRemoteDataSource.insertRowData");
+
+ if (cached.contains(firstRowIndex)) {
+ int oldCacheEnd = cached.getEnd();
+ /*
+ * We need to invalidate the cache from the inserted row onwards,
+ * since the cache wants to be a contiguous range. It doesn't
+ * support holes.
+ *
+ * If holes were supported, we could shift the higher part of
+ * "cached" and leave a hole the size of "count" in the middle.
+ */
+ cached = cached.splitAt(firstRowIndex)[0];
+
+ for (int i = firstRowIndex; i < oldCacheEnd; i++) {
+ rowCache.remove(Integer.valueOf(i));
+ }
+ }
+
+ else if (firstRowIndex < cached.getStart()) {
+ Range oldCached = cached;
+ cached = cached.offsetBy(count);
+
+ for (int i = 0; i < rowCache.size(); i++) {
+ Integer oldIndex = Integer.valueOf(oldCached.getEnd() - i);
+ Integer newIndex = Integer.valueOf(cached.getEnd() - i);
+ rowCache.put(newIndex, rowCache.remove(oldIndex));
+ }
+ }
+
+ setEstimatedSize(getEstimatedSize() + count);
+ dataChangeHandler.dataAdded(firstRowIndex, count);
+ checkCacheCoverage();
+
+ Profiler.leave("AbstractRemoteDataSource.insertRowData");
+ }
+
+ /**
+ * Gets the current range of cached rows
+ *
+ * @return the range of currently cached rows
+ */
+ public Range getCachedRange() {
+ return cached;
+ }
+
+ /**
+ * Sets the cache strategy that is used to determine how much data is
+ * fetched and cached.
+ * <p>
+ * The new strategy is immediately used to evaluate whether currently cached
+ * rows should be discarded or new rows should be fetched.
+ *
+ * @param cacheStrategy
+ * a cache strategy implementation, not <code>null</code>
+ */
+ public void setCacheStrategy(CacheStrategy cacheStrategy) {
+ if (cacheStrategy == null) {
+ throw new IllegalArgumentException();
+ }
+
+ if (this.cacheStrategy != cacheStrategy) {
+ this.cacheStrategy = cacheStrategy;
+
+ checkCacheCoverage();
+ }
+ }
+
+ private Range getMinCacheRange() {
+ Range minCacheRange = cacheStrategy.getMinCacheRange(
+ requestedAvailability, cached, estimatedAvailableRange);
+
+ assert minCacheRange.isSubsetOf(estimatedAvailableRange);
+
+ return minCacheRange;
+ }
+
+ private Range getMaxCacheRange() {
+ Range maxCacheRange = cacheStrategy.getMaxCacheRange(
+ requestedAvailability, cached, estimatedAvailableRange);
+
+ assert maxCacheRange.isSubsetOf(estimatedAvailableRange);
+
+ return maxCacheRange;
+ }
+
+ @Override
+ public RowHandle<T> getHandle(T row) throws IllegalStateException {
+ Object key = getRowKey(row);
+
+ if (key == null) {
+ throw new NullPointerException("key may not be null (row: " + row
+ + ")");
+ }
+
+ if (pinnedRows.containsKey(key)) {
+ return pinnedRows.get(key);
+ } else if (rowCache.containsValue(row)) {
+ return new RowHandleImpl(row, key);
+ } else {
+ throw new IllegalStateException("The cache of this DataSource "
+ + "does not currently contain the row " + row);
+ }
+ }
+
+ /**
+ * Gets a stable key for the row object.
+ * <p>
+ * This method is a workaround for the fact that there is no means to force
+ * proper implementations for {@link #hashCode()} and
+ * {@link #equals(Object)} methods.
+ * <p>
+ * Since the same row object will be created several times for the same
+ * logical data, the DataSource needs a mechanism to be able to compare two
+ * objects, and figure out whether or not they represent the same data. Even
+ * if all the fields of an entity would be changed, it still could represent
+ * the very same thing (say, a person changes all of her names.)
+ * <p>
+ * A very usual and simple example what this could be, is an unique ID for
+ * this object that would also be stored in a database.
+ *
+ * @param row
+ * the row object for which to get the key
+ * @return a non-null object that uniquely and consistently represents the
+ * row object
+ */
+ abstract public Object getRowKey(T row);
+
+ /**
+ * Marks rows as pinned when fetching new rows.
+ * <p>
+ * This collection of rows are intended to remain pinned if new rows are
+ * fetched from the data source, even if some of the pinned rows would fall
+ * off the cache and become inactive.
+ * <p>
+ * This method does nothing by itself, other than it stores the rows into a
+ * field. The implementation needs to make all the adjustments for itself.
+ * Check {@link RpcDataSourceConnector.RpcDataSource#requestRows(int, int)}
+ * for an implementation example.
+ *
+ * @param keys
+ * a collection of rows to keep pinned
+ *
+ * @see #temporarilyPinnedRows
+ * @see RpcDataSourceConnector.RpcDataSource#requestRows(int, int)
+ * @deprecated You probably don't want to call this method unless you're
+ * writing a Renderer for a selection model. Even if you are, be
+ * very aware what this method does and how it behaves.
+ */
+ @Deprecated
+ public void transactionPin(Collection<T> rows) {
+ if (rows == null) {
+ throw new IllegalArgumentException("argument may not be null");
+ }
+ temporarilyPinnedRows = rows;
+ }
+}
diff --git a/client/src/com/vaadin/client/data/CacheStrategy.java b/client/src/com/vaadin/client/data/CacheStrategy.java
new file mode 100644
index 0000000000..3448659e61
--- /dev/null
+++ b/client/src/com/vaadin/client/data/CacheStrategy.java
@@ -0,0 +1,183 @@
+/*
+ * Copyright 2000-2014 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.client.data;
+
+import com.vaadin.shared.ui.grid.Range;
+
+/**
+ * Determines what data an {@link AbstractRemoteDataSource} should fetch and
+ * keep cached.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public interface CacheStrategy {
+ /**
+ * A helper class for creating a simple symmetric cache strategy that uses
+ * the same logic for both rows before and after the currently cached range.
+ * <p>
+ * This simple approach rules out more advanced heuristics that would take
+ * the current scrolling direction or past scrolling behavior into account.
+ */
+ public static abstract class AbstractBasicSymmetricalCacheStrategy
+ implements CacheStrategy {
+
+ @Override
+ public void onDataArrive(double roundTripTime, int rowCount) {
+ // NOP
+ }
+
+ @Override
+ public Range getMinCacheRange(Range displayedRange, Range cachedRange,
+ Range estimatedAvailableRange) {
+ int cacheSize = getMinimumCacheSize(displayedRange.length());
+
+ return displayedRange.expand(cacheSize, cacheSize).restrictTo(
+ estimatedAvailableRange);
+ }
+
+ @Override
+ public Range getMaxCacheRange(Range displayedRange, Range cachedRange,
+ Range estimatedAvailableRange) {
+ int cacheSize = getMaximumCacheSize(displayedRange.length());
+
+ return displayedRange.expand(cacheSize, cacheSize).restrictTo(
+ estimatedAvailableRange);
+ }
+
+ /**
+ * Gets the maximum number of extra items to cache in one direction.
+ *
+ * @param pageSize
+ * the current number of items used at once
+ * @return maximum of items to cache
+ */
+ public abstract int getMaximumCacheSize(int pageSize);
+
+ /**
+ * Gets the the minimum number of extra items to cache in one direction.
+ *
+ * @param pageSize
+ * the current number of items used at once
+ * @return minimum number of items to cache
+ */
+ public abstract int getMinimumCacheSize(int pageSize);
+ }
+
+ /**
+ * The default cache strategy used by {@link AbstractRemoteDataSource},
+ * using multiples of the page size for determining the minimum and maximum
+ * number of items to keep in the cache. By default, at least three times
+ * the page size both before and after the currently used range are kept in
+ * the cache and items are discarded if there's yet another page size worth
+ * of items cached in either direction.
+ */
+ public static class DefaultCacheStrategy extends
+ AbstractBasicSymmetricalCacheStrategy {
+ private final int minimumRatio;
+ private final int maximumRatio;
+
+ /**
+ * Creates a DefaultCacheStrategy keeping between 3 and 4 pages worth of
+ * data cached both before and after the active range.
+ */
+ public DefaultCacheStrategy() {
+ this(3, 4);
+ }
+
+ /**
+ * Creates a DefaultCacheStrategy with custom ratios for how much data
+ * to cache. The ratios denote how many multiples of the currently used
+ * page size are kept in the cache in each direction.
+ *
+ * @param minimumRatio
+ * the minimum number of pages to keep in the cache in each
+ * direction
+ * @param maximumRatio
+ * the maximum number of pages to keep in the cache in each
+ * direction
+ */
+ public DefaultCacheStrategy(int minimumRatio, int maximumRatio) {
+ this.minimumRatio = minimumRatio;
+ this.maximumRatio = maximumRatio;
+ }
+
+ @Override
+ public int getMinimumCacheSize(int pageSize) {
+ return pageSize * minimumRatio;
+ }
+
+ @Override
+ public int getMaximumCacheSize(int pageSize) {
+ return pageSize * maximumRatio;
+ }
+ }
+
+ /**
+ * Called whenever data requested by the data source has arrived. This
+ * information can e.g. be used for measuring how long it takes to fetch
+ * different number of rows from the server.
+ * <p>
+ * A cache strategy implementation cannot use this information to keep track
+ * of which items are in the cache since the data source might discard items
+ * without notifying the cache strategy.
+ *
+ * @param roundTripTime
+ * the total number of milliseconds elapsed from requesting the
+ * data until the response was passed to the data source
+ * @param rowCount
+ * the number of received rows
+ */
+ public void onDataArrive(double roundTripTime, int rowCount);
+
+ /**
+ * Gets the minimum row range that should be cached. The data source will
+ * fetch new data if the currently cached range does not fill the entire
+ * minimum cache range.
+ *
+ * @param displayedRange
+ * the range of currently displayed rows
+ * @param cachedRange
+ * the range of currently cached rows
+ * @param estimatedAvailableRange
+ * the estimated range of rows available for the data source
+ *
+ * @return the minimum range of rows that should be cached, should at least
+ * include the displayed range and should not exceed the total
+ * estimated available range
+ */
+ public Range getMinCacheRange(Range displayedRange, Range cachedRange,
+ Range estimatedAvailableRange);
+
+ /**
+ * Gets the maximum row range that should be cached. The data source will
+ * discard cached rows that are outside the maximum range.
+ *
+ * @param displayedRange
+ * the range of currently displayed rows
+ * @param cachedRange
+ * the range of currently cached rows
+ * @param estimatedAvailableRange
+ * the estimated range of rows available for the data source
+ *
+ * @return the maximum range of rows that should be cached, should at least
+ * include the displayed range and should not exceed the total
+ * estimated available range
+ */
+ public Range getMaxCacheRange(Range displayedRange, Range cachedRange,
+ Range estimatedAvailableRange);
+}
diff --git a/client/src/com/vaadin/client/data/DataChangeHandler.java b/client/src/com/vaadin/client/data/DataChangeHandler.java
new file mode 100644
index 0000000000..fe72fe673a
--- /dev/null
+++ b/client/src/com/vaadin/client/data/DataChangeHandler.java
@@ -0,0 +1,70 @@
+/*
+ * Copyright 2000-2014 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.client.data;
+
+/**
+ * Callback interface used by {@link DataSource} to inform its user about
+ * updates to the data.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public interface DataChangeHandler {
+ /**
+ * Called when the contents of the data source has changed. If the number of
+ * rows has changed or if rows have been moved around,
+ * {@link #dataAdded(int, int)} or {@link #dataRemoved(int, int)} should
+ * ideally be used instead.
+ *
+ * @param firstRowIndex
+ * the index of the first changed row
+ * @param numberOfRows
+ * the number of changed rows
+ */
+ public void dataUpdated(int firstRowIndex, int numberOfRows);
+
+ /**
+ * Called when rows have been removed from the data source.
+ *
+ * @param firstRowIndex
+ * the index that the first removed row had prior to removal
+ * @param numberOfRows
+ * the number of removed rows
+ */
+ public void dataRemoved(int firstRowIndex, int numberOfRows);
+
+ /**
+ * Called when the new rows have been added to the container.
+ *
+ * @param firstRowIndex
+ * the index of the first added row
+ * @param numberOfRows
+ * the number of added rows
+ */
+ public void dataAdded(int firstRowIndex, int numberOfRows);
+
+ /**
+ * Called when rows requested with
+ * {@link DataSource#ensureAvailability(int, int)} rows are available.
+ *
+ * @param firstRowIndex
+ * the index of the first available row
+ * @param numberOfRows
+ * the number of available rows
+ */
+ public void dataAvailable(int firstRowIndex, int numberOfRows);
+}
diff --git a/client/src/com/vaadin/client/data/DataSource.java b/client/src/com/vaadin/client/data/DataSource.java
new file mode 100644
index 0000000000..24cef548c5
--- /dev/null
+++ b/client/src/com/vaadin/client/data/DataSource.java
@@ -0,0 +1,188 @@
+/*
+ * Copyright 2000-2014 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.client.data;
+
+/**
+ * Source of data for widgets showing lazily loaded data based on indexable
+ * items (e.g. rows) of a specified type. The data source is a lazy view into a
+ * larger data set.
+ *
+ * @since
+ * @author Vaadin Ltd
+ * @param <T>
+ * the row type
+ */
+public interface DataSource<T> {
+
+ /**
+ * A handle that contains information on whether a row should be
+ * {@link #pin() pinned} or {@link #unpin() unpinned}, and also always the
+ * most recent representation for that particular row.
+ *
+ * @param <T>
+ * the row type
+ */
+ public abstract class RowHandle<T> {
+ /**
+ * Gets the most recent representation for the row this handle
+ * represents.
+ *
+ * @return the most recent representation for the row this handle
+ * represents
+ * @throws IllegalStateException
+ * if this row handle isn't currently pinned
+ * @see #pin()
+ */
+ public abstract T getRow() throws IllegalStateException;
+
+ /**
+ * Marks this row as pinned.
+ * <p>
+ * <em>Note:</em> Pinning a row multiple times requires an equal amount
+ * of unpins to free the row from the "pinned" status.
+ * <p>
+ * <em>Technical Note:</em> Pinning a row makes sure that the row object
+ * for a particular set of data is always kept as up to date as the data
+ * source is able to. Since the DataSource might create a new instance
+ * of an object, object references aren't necessarily kept up-to-date.
+ * This is a technical work-around for that.
+ *
+ * @see #unpin()
+ */
+ public abstract void pin();
+
+ /**
+ * Marks this row as unpinned.
+ * <p>
+ * <em>Note:</em> Pinning a row multiple times requires an equal amount
+ * of unpins to free the row from the "pinned" status.
+ * <p>
+ * <em>Technical Note:</em> Pinning a row makes sure that the row object
+ * for a particular set of data is always kept as up to date as the data
+ * source is able to. Since the DataSource might create a new instance
+ * of an object, object references aren't necessarily kept up-to-date.
+ * This is a technical work-around for that.
+ *
+ * @throws IllegalStateException
+ * if this row handle has not been pinned before
+ * @see #pin()
+ */
+ public abstract void unpin() throws IllegalStateException;
+
+ /**
+ * An explicit override for {@link Object#equals(Object)}. This method
+ * should be functionally equivalent to a properly implemented equals
+ * method.
+ * <p>
+ * Having a properly implemented equals method is imperative for
+ * RowHandle to function. Because Java has no mechanism to force an
+ * override of an existing method, we're defining a new method for that
+ * instead.
+ *
+ * @param rowHandle
+ * the reference object with which to compare
+ * @return {@code true} if this object is the same as the obj argument;
+ * {@code false} otherwise.
+ */
+ protected abstract boolean equalsExplicit(Object obj);
+
+ /**
+ * An explicit override for {@link Object#hashCode()}. This method
+ * should be functionally equivalent to a properly implemented hashCode
+ * method.
+ * <p>
+ * Having a properly implemented hashCode method is imperative for
+ * RowHandle to function. Because Java has no mechanism to force an
+ * override of an existing method, we're defining a new method for that
+ * instead.
+ *
+ * @return a hash code value for this object
+ */
+ protected abstract int hashCodeExplicit();
+
+ @Override
+ public int hashCode() {
+ return hashCodeExplicit();
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ return equalsExplicit(obj);
+ }
+ }
+
+ /**
+ * Informs the data source that data for the given range is needed. A data
+ * source only has one active region at a time, so calling this method
+ * discards the previously set range.
+ * <p>
+ * This method triggers lazy loading of data if necessary. The change
+ * handler registered using {@link #setDataChangeHandler(DataChangeHandler)}
+ * is informed when new data has been loaded.
+ * <p>
+ * After any possible lazy loading and updates are done, the change handler
+ * is informed that new data is available.
+ *
+ * @param firstRowIndex
+ * the index of the first needed row
+ * @param numberOfRows
+ * the number of needed rows
+ */
+ public void ensureAvailability(int firstRowIndex, int numberOfRows);
+
+ /**
+ * Retrieves the data for the row at the given index. If the row data is not
+ * available, returns <code>null</code>.
+ * <p>
+ * This method does not trigger loading of unavailable data.
+ * {@link #ensureAvailability(int, int)} should be used to signal what data
+ * will be needed.
+ *
+ * @param rowIndex
+ * the index of the row to retrieve data for
+ * @return data for the row; or <code>null</code> if no data is available
+ */
+ public T getRow(int rowIndex);
+
+ /**
+ * Returns the current best guess for the number of rows in the container.
+ *
+ * @return the current estimation of the container size
+ */
+ public int getEstimatedSize();
+
+ /**
+ * Sets a data change handler to inform when data is updated, added or
+ * removed.
+ *
+ * @param dataChangeHandler
+ * the data change handler
+ */
+ public void setDataChangeHandler(DataChangeHandler dataChangeHandler);
+
+ /**
+ * Gets a {@link RowHandle} of a row object in the cache.
+ *
+ * @param row
+ * the row object for which to retrieve a row handle
+ * @return a non-<code>null</code> row handle of the given row object
+ * @throw IllegalStateException if this data source cannot be sure whether
+ * or not the given row exists. <em>In practice</em> this usually
+ * means that the row is not currently in this data source's cache.
+ */
+ public RowHandle<T> getHandle(T row);
+}
diff --git a/client/src/com/vaadin/client/data/RpcDataSourceConnector.java b/client/src/com/vaadin/client/data/RpcDataSourceConnector.java
new file mode 100644
index 0000000000..2819837504
--- /dev/null
+++ b/client/src/com/vaadin/client/data/RpcDataSourceConnector.java
@@ -0,0 +1,153 @@
+/*
+ * Copyright 2000-2014 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.client.data;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+import com.google.gwt.json.client.JSONArray;
+import com.google.gwt.json.client.JSONObject;
+import com.google.gwt.json.client.JSONParser;
+import com.google.gwt.json.client.JSONString;
+import com.google.gwt.json.client.JSONValue;
+import com.vaadin.client.ServerConnector;
+import com.vaadin.client.extensions.AbstractExtensionConnector;
+import com.vaadin.client.ui.grid.GridConnector;
+import com.vaadin.shared.data.DataProviderRpc;
+import com.vaadin.shared.data.DataProviderState;
+import com.vaadin.shared.data.DataRequestRpc;
+import com.vaadin.shared.ui.Connect;
+import com.vaadin.shared.ui.grid.GridState;
+import com.vaadin.shared.ui.grid.Range;
+
+/**
+ * Connects a Vaadin server-side container data source to a Grid. This is
+ * currently implemented as an Extension hardcoded to support a specific
+ * connector type. This will be changed once framework support for something
+ * more flexible has been implemented.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+@Connect(com.vaadin.data.RpcDataProviderExtension.class)
+public class RpcDataSourceConnector extends AbstractExtensionConnector {
+
+ public class RpcDataSource extends AbstractRemoteDataSource<JSONObject> {
+
+ private Collection<JSONObject> prevRows = Collections.emptySet();
+
+ @Override
+ protected void requestRows(int firstRowIndex, int numberOfRows) {
+ Range cached = getCachedRange();
+
+ Collection<JSONObject> newRows = new ArrayList<JSONObject>(
+ temporarilyPinnedRows);
+ newRows.removeAll(prevRows);
+
+ List<String> temporarilyPinnedKeys = new ArrayList<String>(
+ newRows.size());
+ for (JSONObject row : newRows) {
+ temporarilyPinnedKeys.add((String) getRowKey(row));
+ }
+
+ getRpcProxy(DataRequestRpc.class).requestRows(firstRowIndex,
+ numberOfRows, cached.getStart(), cached.length(),
+ temporarilyPinnedKeys);
+
+ prevRows = temporarilyPinnedRows;
+ }
+
+ @Override
+ public Object getRowKey(JSONObject row) {
+ JSONString string = row.get(GridState.JSONKEY_ROWKEY).isString();
+ if (string != null) {
+ return string.stringValue();
+ } else {
+ return null;
+ }
+ }
+
+ public RowHandle<JSONObject> getHandleByKey(Object key) {
+ JSONObject row = new JSONObject();
+ row.put(GridState.JSONKEY_ROWKEY, new JSONString((String) key));
+ return new RowHandleImpl(row, key);
+ }
+
+ @Override
+ @SuppressWarnings("deprecation")
+ public void transactionPin(Collection<JSONObject> keys) {
+ super.transactionPin(keys);
+ if (keys.isEmpty() && !prevRows.isEmpty()) {
+ prevRows = Collections.emptySet();
+ getRpcProxy(DataRequestRpc.class)
+ .releaseTemporarilyPinnedKeys();
+ }
+ }
+ }
+
+ private final RpcDataSource dataSource = new RpcDataSource();
+
+ @Override
+ protected void extend(ServerConnector target) {
+ dataSource.setEstimatedSize(getState().containerSize);
+ ((GridConnector) target).setDataSource(dataSource);
+
+ registerRpc(DataProviderRpc.class, new DataProviderRpc() {
+ @Override
+ public void setRowData(int firstRow, String rowsJson) {
+ JSONValue parsedJson = JSONParser.parseStrict(rowsJson);
+ JSONArray rowArray = parsedJson.isArray();
+ assert rowArray != null : "Was unable to parse JSON into an array: "
+ + parsedJson;
+
+ ArrayList<JSONObject> rows = new ArrayList<JSONObject>(rowArray
+ .size());
+ for (int i = 0; i < rowArray.size(); i++) {
+ JSONValue rowValue = rowArray.get(i);
+ JSONObject rowObject = rowValue.isObject();
+ assert rowObject != null : "Was unable to parse JSON into an object: "
+ + rowValue;
+ rows.add(rowObject);
+ }
+
+ dataSource.setRowData(firstRow, rows);
+ }
+
+ @Override
+ public void removeRowData(int firstRow, int count) {
+ dataSource.removeRowData(firstRow, count);
+ }
+
+ @Override
+ public void insertRowData(int firstRow, int count) {
+ dataSource.insertRowData(firstRow, count);
+ }
+ });
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see com.vaadin.client.ui.AbstractConnector#getState()
+ */
+ @Override
+ public DataProviderState getState() {
+ return (DataProviderState) super.getState();
+ }
+}
diff --git a/client/src/com/vaadin/client/metadata/TypeDataStore.java b/client/src/com/vaadin/client/metadata/TypeDataStore.java
index 7aa952d0f2..9b1fd7d45c 100644
--- a/client/src/com/vaadin/client/metadata/TypeDataStore.java
+++ b/client/src/com/vaadin/client/metadata/TypeDataStore.java
@@ -37,6 +37,8 @@ public class TypeDataStore {
.create();
private final FastStringMap<JsArrayString> delegateToWidgetProperties = FastStringMap
.create();
+ private final FastStringMap<Type> presentationTypes = FastStringMap
+ .create();
/**
* Maps connector class -> state property name -> hander method data
@@ -135,6 +137,10 @@ public class TypeDataStore {
return get().delegateToWidgetProperties.get(type.getBaseTypeName());
}
+ public static Type getPresentationType(Class<?> type) {
+ return get().presentationTypes.get(getType(type).getBaseTypeName());
+ }
+
public void setDelegateToWidget(Class<?> clazz, String propertyName,
String delegateValue) {
Type type = getType(clazz);
@@ -150,6 +156,11 @@ public class TypeDataStore {
typeProperties.push(propertyName);
}
+ public void setPresentationType(Class<?> type, Class<?> presentationType) {
+ presentationTypes.put(getType(type).getBaseTypeName(),
+ getType(presentationType));
+ }
+
public void setReturnType(Class<?> type, String methodName, Type returnType) {
returnTypes.put(new Method(getType(type), methodName).getLookupKey(),
returnType);
diff --git a/client/src/com/vaadin/client/ui/grid/Cell.java b/client/src/com/vaadin/client/ui/grid/Cell.java
new file mode 100644
index 0000000000..ede8bb22d0
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/Cell.java
@@ -0,0 +1,85 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid;
+
+import com.google.gwt.dom.client.TableCellElement;
+
+/**
+ * Describes a cell
+ * <p>
+ * It's a representation of the element in a grid cell, and its row and column
+ * indices.
+ * <p>
+ * Unlike the {@link FlyweightRow}, an instance of {@link Cell} can be stored in
+ * a field.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public class Cell {
+
+ private final int row;
+
+ private final int column;
+
+ private final TableCellElement element;
+
+ /**
+ * Constructs a new {@link Cell}.
+ *
+ * @param row
+ * The index of the row
+ * @param column
+ * The index of the column
+ * @param element
+ * The cell element
+ */
+ public Cell(int row, int column, TableCellElement element) {
+ super();
+ this.row = row;
+ this.column = column;
+ this.element = element;
+ }
+
+ /**
+ * Returns the index of the row the cell resides in.
+ *
+ * @return the row index
+ *
+ */
+ public int getRow() {
+ return row;
+ }
+
+ /**
+ * Returns the index of the column the cell resides in.
+ *
+ * @return the column index
+ */
+ public int getColumn() {
+ return column;
+ }
+
+ /**
+ * Returns the element of the cell.
+ *
+ * @return the cell element
+ */
+ public TableCellElement getElement() {
+ return element;
+ }
+
+}
diff --git a/client/src/com/vaadin/client/ui/grid/ColumnConfiguration.java b/client/src/com/vaadin/client/ui/grid/ColumnConfiguration.java
new file mode 100644
index 0000000000..f523fdbbd4
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/ColumnConfiguration.java
@@ -0,0 +1,145 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid;
+
+/**
+ * A representation of the columns in an instance of {@link Escalator}.
+ *
+ * @since
+ * @author Vaadin Ltd
+ * @see Escalator#getColumnConfiguration()
+ */
+public interface ColumnConfiguration {
+
+ /**
+ * Removes columns at certain indices.
+ * <p>
+ * If any of the removed columns were frozen, the number of frozen columns
+ * will be reduced by the number of the removed columns that were frozen.
+ *
+ * @param index
+ * the index of the first column to be removed
+ * @param numberOfColumns
+ * the number of rows to remove, starting from {@code index}
+ * @throws IndexOutOfBoundsException
+ * if the entire range of removed columns is not currently
+ * present in the escalator
+ * @throws IllegalArgumentException
+ * if <code>numberOfColumns</code> is less than 1.
+ */
+ public void removeColumns(int index, int numberOfColumns)
+ throws IndexOutOfBoundsException, IllegalArgumentException;
+
+ /**
+ * Adds columns at a certain index.
+ * <p>
+ * The new columns will be inserted between the column at the index, and the
+ * column before (an index of 0 means that the columns are inserted at the
+ * beginning). Therefore, the columns at the index and afterwards will be
+ * moved to the right.
+ * <p>
+ * The contents of the inserted columns will be queried from the respective
+ * cell renderers in the header, body and footer.
+ * <p>
+ * If there are frozen columns and the first added column is to the left of
+ * the last frozen column, the number of frozen columns will be increased by
+ * the number of inserted columns.
+ * <p>
+ * <em>Note:</em> Only the contents of the inserted columns will be
+ * rendered. If inserting new columns affects the contents of existing
+ * columns, {@link RowContainer#refreshRows(int, int)} needs to be called as
+ * appropriate.
+ *
+ * @param index
+ * the index of the column before which new columns are inserted,
+ * or {@link #getColumnCount()} to add new columns at the end
+ * @param numberOfColumns
+ * the number of columns to insert after the <code>index</code>
+ * @throws IndexOutOfBoundsException
+ * if <code>index</code> is not an integer in the range
+ * <code>[0..{@link #getColumnCount()}]</code>
+ * @throws IllegalArgumentException
+ * if {@code numberOfColumns} is less than 1.
+ */
+ public void insertColumns(int index, int numberOfColumns)
+ throws IndexOutOfBoundsException, IllegalArgumentException;
+
+ /**
+ * Returns the number of columns in the escalator.
+ *
+ * @return the number of columns in the escalator
+ */
+ public int getColumnCount();
+
+ /**
+ * Sets the number of leftmost columns that are not affected by horizontal
+ * scrolling.
+ *
+ * @param count
+ * the number of columns to freeze
+ *
+ * @throws IllegalArgumentException
+ * if the column count is &lt; 0 or &gt; the number of columns
+ *
+ */
+ public void setFrozenColumnCount(int count) throws IllegalArgumentException;
+
+ /**
+ * Get the number of leftmost columns that are not affected by horizontal
+ * scrolling.
+ *
+ * @return the number of frozen columns
+ */
+ public int getFrozenColumnCount();
+
+ /**
+ * Sets (or unsets) an explicit width for a column.
+ *
+ * @param index
+ * the index of the column for which to set a width
+ * @param px
+ * the number of pixels the indicated column should be, or a
+ * negative number to let the escalator decide
+ * @throws IllegalArgumentException
+ * if <code>index</code> is not a valid column index
+ */
+ public void setColumnWidth(int index, int px)
+ throws IllegalArgumentException;
+
+ /**
+ * Returns the user-defined width of a column.
+ *
+ * @param index
+ * the index of the column for which to retrieve the width
+ * @return the column's width in pixels, or a negative number if the width
+ * is implicitly decided by the escalator
+ * @throws IllegalArgumentException
+ * if <code>index</code> is not a valid column index
+ */
+ public int getColumnWidth(int index) throws IllegalArgumentException;
+
+ /**
+ * Returns the actual width of a column.
+ *
+ * @param index
+ * the index of the column for which to retrieve the width
+ * @return the column's actual width in pixels
+ * @throws IllegalArgumentException
+ * if <code>index</code> is not a valid column index
+ */
+ public int getColumnWidthActual(int index) throws IllegalArgumentException;
+} \ No newline at end of file
diff --git a/client/src/com/vaadin/client/ui/grid/DataAvailableEvent.java b/client/src/com/vaadin/client/ui/grid/DataAvailableEvent.java
new file mode 100644
index 0000000000..62b188c0ea
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/DataAvailableEvent.java
@@ -0,0 +1,55 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid;
+
+import com.google.gwt.event.shared.GwtEvent;
+import com.vaadin.shared.ui.grid.Range;
+
+/**
+ * Event object describing a change of row availability in DataSource of a Grid.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public class DataAvailableEvent extends GwtEvent<DataAvailableHandler> {
+
+ private Range rowsAvailable;
+ public static final Type<DataAvailableHandler> TYPE = new Type<DataAvailableHandler>();
+
+ public DataAvailableEvent(Range rowsAvailable) {
+ this.rowsAvailable = rowsAvailable;
+ }
+
+ /**
+ * Returns the range of available rows in {@link DataSource} for this event.
+ *
+ * @return range of available rows
+ */
+ public Range getAvailableRows() {
+ return rowsAvailable;
+ }
+
+ @Override
+ public Type<DataAvailableHandler> getAssociatedType() {
+ return TYPE;
+ }
+
+ @Override
+ protected void dispatch(DataAvailableHandler handler) {
+ handler.onDataAvailable(this);
+ }
+
+}
diff --git a/client/src/com/vaadin/client/ui/grid/DataAvailableHandler.java b/client/src/com/vaadin/client/ui/grid/DataAvailableHandler.java
new file mode 100644
index 0000000000..06ea08a17e
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/DataAvailableHandler.java
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid;
+
+import com.google.gwt.event.shared.EventHandler;
+
+/**
+ * Handler for {@link DataAvailableEvent}s.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public interface DataAvailableHandler extends EventHandler {
+
+ /**
+ * Called when DataSource has data available. Supplied with row range.
+ *
+ * @param availableRows
+ * Range of rows available in the DataSource
+ * @return true if the command was successfully completed, false to call
+ * again the next time new data is available
+ */
+ public void onDataAvailable(DataAvailableEvent event);
+}
diff --git a/client/src/com/vaadin/client/ui/grid/Escalator.java b/client/src/com/vaadin/client/ui/grid/Escalator.java
new file mode 100644
index 0000000000..bf62805034
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/Escalator.java
@@ -0,0 +1,4626 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.ListIterator;
+import java.util.Map;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import com.google.gwt.animation.client.AnimationScheduler;
+import com.google.gwt.animation.client.AnimationScheduler.AnimationCallback;
+import com.google.gwt.animation.client.AnimationScheduler.AnimationHandle;
+import com.google.gwt.core.client.Duration;
+import com.google.gwt.core.client.JavaScriptObject;
+import com.google.gwt.core.client.Scheduler;
+import com.google.gwt.dom.client.Document;
+import com.google.gwt.dom.client.Element;
+import com.google.gwt.dom.client.NativeEvent;
+import com.google.gwt.dom.client.Node;
+import com.google.gwt.dom.client.NodeList;
+import com.google.gwt.dom.client.Style;
+import com.google.gwt.dom.client.Style.Display;
+import com.google.gwt.dom.client.Style.Unit;
+import com.google.gwt.dom.client.TableCellElement;
+import com.google.gwt.dom.client.TableRowElement;
+import com.google.gwt.dom.client.TableSectionElement;
+import com.google.gwt.event.shared.HandlerRegistration;
+import com.google.gwt.logging.client.LogConfiguration;
+import com.google.gwt.user.client.DOM;
+import com.google.gwt.user.client.Window;
+import com.google.gwt.user.client.ui.UIObject;
+import com.google.gwt.user.client.ui.Widget;
+import com.vaadin.client.Profiler;
+import com.vaadin.client.Util;
+import com.vaadin.client.ui.grid.Escalator.JsniUtil.TouchHandlerBundle;
+import com.vaadin.client.ui.grid.PositionFunction.AbsolutePosition;
+import com.vaadin.client.ui.grid.PositionFunction.Translate3DPosition;
+import com.vaadin.client.ui.grid.PositionFunction.TranslatePosition;
+import com.vaadin.client.ui.grid.PositionFunction.WebkitTranslate3DPosition;
+import com.vaadin.client.ui.grid.ScrollbarBundle.HorizontalScrollbarBundle;
+import com.vaadin.client.ui.grid.ScrollbarBundle.VerticalScrollbarBundle;
+import com.vaadin.shared.ui.grid.GridState;
+import com.vaadin.shared.ui.grid.HeightMode;
+import com.vaadin.shared.ui.grid.Range;
+import com.vaadin.shared.ui.grid.ScrollDestination;
+import com.vaadin.shared.util.SharedUtil;
+
+/*-
+
+ Maintenance Notes! Reading these might save your day.
+ (note for editors: line width is 80 chars, including the
+ one-space indentation)
+
+
+ == Row Container Structure
+
+ AbstractRowContainer
+ |-- AbstractStaticRowContainer
+ | |-- HeaderRowContainer
+ | `-- FooterContainer
+ `---- BodyRowContainer
+
+ AbstractRowContainer is intended to contain all common logic
+ between RowContainers. It manages the bookkeeping of row
+ count, makes sure that all individual cells are rendered
+ the same way, and so on.
+
+ AbstractStaticRowContainer has some special logic that is
+ required by all RowContainers that don't scroll (hence the
+ word "static"). HeaderRowContainer and FooterRowContainer
+ are pretty thin special cases of a StaticRowContainer
+ (mostly relating to positioning of the root element).
+
+ BodyRowContainer could also be split into an additional
+ "AbstractScrollingRowContainer", but I felt that no more
+ inner classes were needed. So it contains both logic
+ required for making things scroll about, and equivalent
+ special cases for layouting, as are found in
+ Header/FooterRowContainers.
+
+
+ == The Three Indices
+
+ Each RowContainer can be thought to have three levels of
+ indices for any given displayed row (but the distinction
+ matters primarily for the BodyRowContainer, because of the
+ way it scrolls through data):
+
+ - Logical index
+ - Physical (or DOM) index
+ - Visual index
+
+ LOGICAL INDEX is the index that is linked to the data
+ source. If you want your data source to represent a SQL
+ database with 10 000 rows, the 7 000:th row in the SQL has a
+ logical index of 6 999, since the index is 0-based (unless
+ that data source does some funky logic).
+
+ PHYSICAL INDEX is the index for a row that you see in a
+ browser's DOM inspector. If your row is the second <tr>
+ element within a <tbody> tag, it has a physical index of 1
+ (because of 0-based indices). In Header and
+ FooterRowContainers, you are safe to assume that the logical
+ index is the same as the physical index. But because the
+ BodyRowContainer never displays large data sources entirely
+ in the DOM, a physical index usually has no apparent direct
+ relationship with its logical index.
+
+ VISUAL INDEX is the index relating to the order that you
+ see a row in, in the browser, as it is rendered. The
+ topmost row is 0, the second is 1, and so on. The visual
+ index is similar to the physical index in the sense that
+ Header and FooterRowContainers can assume a 1:1
+ relationship between visual index and logical index. And
+ again, BodyRowContainer has no such relationship. The
+ body's visual index has additionally no apparent
+ relationship with its physical index. Because the <tr> tags
+ are reused in the body and visually repositioned with CSS
+ as the user scrolls, the relationship between physical
+ index and visual index is quickly broken. You can get an
+ element's visual index via the field
+ BodyRowContainer.visualRowOrder.
+
+ Currently, the physical and visual indices are kept in sync
+ _most of the time_ by a deferred rearrangement of rows.
+ They become desynced when scrolling. This is to help screen
+ readers to read the contents from the DOM in a natural
+ order. See BodyRowContainer.DeferredDomSorter for more
+ about that.
+
+ */
+
+/**
+ * A workaround-class for GWT and JSNI.
+ * <p>
+ * GWT is unable to handle some method calls to Java methods in inner-classes
+ * from within JSNI blocks. Having that inner class extend a non-inner-class (or
+ * implement such an interface), makes it possible for JSNI to indirectly refer
+ * to the inner class, by invoking methods and fields in the non-inner-class
+ * API.
+ *
+ * @see Escalator.Scroller
+ */
+abstract class JsniWorkaround {
+ /**
+ * A JavaScript function that handles the scroll DOM event, and passes it on
+ * to Java code.
+ *
+ * @see #createScrollListenerFunction(Escalator)
+ * @see Escalator#onScroll()
+ * @see Escalator.Scroller#onScroll()
+ */
+ protected final JavaScriptObject scrollListenerFunction;
+
+ /**
+ * A JavaScript function that handles the mousewheel DOM event, and passes
+ * it on to Java code.
+ *
+ * @see #createMousewheelListenerFunction(Escalator)
+ * @see Escalator#onScroll()
+ * @see Escalator.Scroller#onScroll()
+ */
+ protected final JavaScriptObject mousewheelListenerFunction;
+
+ /**
+ * A JavaScript function that handles the touch start DOM event, and passes
+ * it on to Java code.
+ *
+ * @see TouchHandlerBundle#touchStart(Escalator.JsniUtil.TouchHandlerBundle.CustomTouchEvent)
+ */
+ protected JavaScriptObject touchStartFunction;
+
+ /**
+ * A JavaScript function that handles the touch move DOM event, and passes
+ * it on to Java code.
+ *
+ * @see TouchHandlerBundle#touchMove(Escalator.JsniUtil.TouchHandlerBundle.CustomTouchEvent)
+ */
+ protected JavaScriptObject touchMoveFunction;
+
+ /**
+ * A JavaScript function that handles the touch end and cancel DOM events,
+ * and passes them on to Java code.
+ *
+ * @see TouchHandlerBundle#touchEnd(Escalator.JsniUtil.TouchHandlerBundle.CustomTouchEvent)
+ */
+ protected JavaScriptObject touchEndFunction;
+
+ protected TouchHandlerBundle touchHandlerBundle;
+
+ protected JsniWorkaround(final Escalator escalator) {
+ scrollListenerFunction = createScrollListenerFunction(escalator);
+ mousewheelListenerFunction = createMousewheelListenerFunction(escalator);
+
+ touchHandlerBundle = new TouchHandlerBundle(escalator);
+ touchStartFunction = touchHandlerBundle.getTouchStartHandler();
+ touchMoveFunction = touchHandlerBundle.getTouchMoveHandler();
+ touchEndFunction = touchHandlerBundle.getTouchEndHandler();
+ }
+
+ /**
+ * A method that constructs the JavaScript function that will be stored into
+ * {@link #scrollListenerFunction}.
+ *
+ * @param esc
+ * a reference to the current instance of {@link Escalator}
+ * @see Escalator#onScroll()
+ */
+ protected abstract JavaScriptObject createScrollListenerFunction(
+ Escalator esc);
+
+ /**
+ * A method that constructs the JavaScript function that will be stored into
+ * {@link #mousewheelListenerFunction}.
+ *
+ * @param esc
+ * a reference to the current instance of {@link Escalator}
+ * @see Escalator#onScroll()
+ */
+ protected abstract JavaScriptObject createMousewheelListenerFunction(
+ Escalator esc);
+}
+
+/**
+ * A low-level table-like widget that features a scrolling virtual viewport and
+ * lazily generated rows.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public class Escalator extends Widget {
+
+ // todo comments legend
+ /*
+ * [[optimize]]: There's an opportunity to rewrite the code in such a way
+ * that it _might_ perform better (rememeber to measure, implement,
+ * re-measure)
+ */
+ /*
+ * [[rowheight]]: This code will require alterations that are relevant for
+ * being able to support variable row heights. NOTE: these bits can most
+ * often also be identified by searching for code reading the ROW_HEIGHT_PX
+ * constant.
+ */
+ /*
+ * [[mpixscroll]]: This code will require alterations that are relevant for
+ * supporting the scrolling through more pixels than some browsers normally
+ * would support. (i.e. when we support more than "a million" pixels in the
+ * escalator DOM). NOTE: these bits can most often also be identified by
+ * searching for code that call scrollElem.getScrollTop();.
+ */
+
+ /**
+ * A utility class that contains utility methods that are usually called
+ * from JSNI.
+ * <p>
+ * The methods are moved in this class to minimize the amount of JSNI code
+ * as much as feasible.
+ */
+ static class JsniUtil {
+ public static class TouchHandlerBundle {
+
+ /**
+ * A <a href=
+ * "http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsOverlay.html"
+ * >JavaScriptObject overlay</a> for the <a
+ * href="http://www.w3.org/TR/touch-events/">JavaScript
+ * TouchEvent</a> object.
+ * <p>
+ * This needs to be used in the touch event handlers, since GWT's
+ * {@link com.google.gwt.event.dom.client.TouchEvent TouchEvent}
+ * can't be cast from the JSNI call, and the
+ * {@link com.google.gwt.dom.client.NativeEvent NativeEvent} isn't
+ * properly populated with the correct values.
+ */
+ private final static class CustomTouchEvent extends
+ JavaScriptObject {
+ protected CustomTouchEvent() {
+ }
+
+ public native NativeEvent getNativeEvent()
+ /*-{
+ return this;
+ }-*/;
+
+ public native int getPageX()
+ /*-{
+ return this.targetTouches[0].pageX;
+ }-*/;
+
+ public native int getPageY()
+ /*-{
+ return this.targetTouches[0].pageY;
+ }-*/;
+ }
+
+ private double touches = 0;
+ private int lastX = 0;
+ private int lastY = 0;
+ private double lastTime = 0;
+ private boolean snappedScrollEnabled = true;
+ private double deltaX = 0;
+ private double deltaY = 0;
+
+ private final Escalator escalator;
+ private CustomTouchEvent latestTouchMoveEvent;
+ private AnimationCallback mover = new AnimationCallback() {
+ @Override
+ public void execute(double doNotUseThisTimestamp) {
+ /*
+ * We can't use the timestamp parameter here, since it is
+ * not in any predetermined format; TouchEnd does not
+ * provide a compatible timestamp, and we need to be able to
+ * get a comparable timestamp to determine whether to
+ * trigger a flick scroll or not.
+ */
+
+ if (touches != 1) {
+ return;
+ }
+
+ final int x = latestTouchMoveEvent.getPageX();
+ final int y = latestTouchMoveEvent.getPageY();
+ deltaX = x - lastX;
+ deltaY = y - lastY;
+ lastX = x;
+ lastY = y;
+
+ /*
+ * Instead of using the provided arbitrary timestamp, let's
+ * use a known-format and reproducible timestamp.
+ */
+ lastTime = Duration.currentTimeMillis();
+
+ // snap the scroll to the major axes, at first.
+ if (snappedScrollEnabled) {
+ final double oldDeltaX = deltaX;
+ final double oldDeltaY = deltaY;
+
+ /*
+ * Scrolling snaps to 40 degrees vs. flick scroll's 30
+ * degrees, since slow movements have poor resolution -
+ * it's easy to interpret a slight angle as a steep
+ * angle, since the sample rate is "unnecessarily" high.
+ * 40 simply felt better than 30.
+ */
+ final double[] snapped = Escalator.snapDeltas(deltaX,
+ deltaY, RATIO_OF_40_DEGREES);
+ deltaX = snapped[0];
+ deltaY = snapped[1];
+
+ /*
+ * if the snap failed once, let's follow the pointer
+ * from now on.
+ */
+ if (oldDeltaX != 0 && deltaX == oldDeltaX
+ && oldDeltaY != 0 && deltaY == oldDeltaY) {
+ snappedScrollEnabled = false;
+ }
+ }
+
+ moveScrollFromEvent(escalator, -deltaX, -deltaY,
+ latestTouchMoveEvent.getNativeEvent());
+ }
+ };
+ private AnimationHandle animationHandle;
+
+ public TouchHandlerBundle(final Escalator escalator) {
+ this.escalator = escalator;
+ }
+
+ public native JavaScriptObject getTouchStartHandler()
+ /*-{
+ // we need to store "this", since it won't be preserved on call.
+ var self = this;
+ return $entry(function (e) {
+ self.@com.vaadin.client.ui.grid.Escalator.JsniUtil.TouchHandlerBundle::touchStart(*)(e);
+ });
+ }-*/;
+
+ public native JavaScriptObject getTouchMoveHandler()
+ /*-{
+ // we need to store "this", since it won't be preserved on call.
+ var self = this;
+ return $entry(function (e) {
+ self.@com.vaadin.client.ui.grid.Escalator.JsniUtil.TouchHandlerBundle::touchMove(*)(e);
+ });
+ }-*/;
+
+ public native JavaScriptObject getTouchEndHandler()
+ /*-{
+ // we need to store "this", since it won't be preserved on call.
+ var self = this;
+ return $entry(function (e) {
+ self.@com.vaadin.client.ui.grid.Escalator.JsniUtil.TouchHandlerBundle::touchEnd(*)(e);
+ });
+ }-*/;
+
+ public void touchStart(final CustomTouchEvent event) {
+ touches = event.getNativeEvent().getTouches().length();
+ if (touches != 1) {
+ return;
+ }
+
+ escalator.scroller.cancelFlickScroll();
+
+ lastX = event.getPageX();
+ lastY = event.getPageY();
+
+ snappedScrollEnabled = true;
+ }
+
+ public void touchMove(final CustomTouchEvent event) {
+ /*
+ * since we only use the getPageX/Y, and calculate the diff
+ * within the handler, we don't need to calculate any
+ * intermediate deltas.
+ */
+ latestTouchMoveEvent = event;
+
+ if (animationHandle != null) {
+ animationHandle.cancel();
+ }
+ animationHandle = AnimationScheduler.get()
+ .requestAnimationFrame(mover, escalator.bodyElem);
+ event.getNativeEvent().preventDefault();
+
+ /*
+ * this initializes a correct timestamp, and also renders the
+ * first frame for added responsiveness.
+ */
+ mover.execute(Duration.currentTimeMillis());
+ }
+
+ public void touchEnd(final CustomTouchEvent event) {
+ touches = event.getNativeEvent().getTouches().length();
+
+ if (touches == 0) {
+ escalator.scroller.handleFlickScroll(deltaX, deltaY,
+ lastTime);
+ escalator.body.domSorter.reschedule();
+ }
+ }
+ }
+
+ public static void moveScrollFromEvent(final Escalator escalator,
+ final double deltaX, final double deltaY,
+ final NativeEvent event) {
+
+ if (!Double.isNaN(deltaX)) {
+ escalator.horizontalScrollbar.setScrollPosByDelta(deltaX);
+ }
+
+ if (!Double.isNaN(deltaY)) {
+ escalator.verticalScrollbar.setScrollPosByDelta(deltaY);
+ }
+
+ /*
+ * TODO: only prevent if not scrolled to end/bottom. Or no? UX team
+ * needs to decide.
+ */
+ final boolean warrantedYScroll = deltaY != 0
+ && escalator.verticalScrollbar.showsScrollHandle();
+ final boolean warrantedXScroll = deltaX != 0
+ && escalator.horizontalScrollbar.showsScrollHandle();
+ if (warrantedYScroll || warrantedXScroll) {
+ event.preventDefault();
+ }
+ }
+ }
+
+ /**
+ * The animation callback that handles the animation of a touch-scrolling
+ * flick with inertia.
+ */
+ private class FlickScrollAnimator implements AnimationCallback {
+ private static final double MIN_MAGNITUDE = 0.005;
+ private static final double MAX_SPEED = 7;
+
+ private double velX;
+ private double velY;
+ private double prevTime = 0;
+ private int millisLeft;
+ private double xFric;
+ private double yFric;
+
+ private boolean cancelled = false;
+ private double lastLeft;
+ private double lastTop;
+
+ /**
+ * Creates a new animation callback to handle touch-scrolling flick with
+ * inertia.
+ *
+ * @param deltaX
+ * the last scrolling delta in the x-axis in a touchmove
+ * @param deltaY
+ * the last scrolling delta in the y-axis in a touchmove
+ * @param lastTime
+ * the timestamp of the last touchmove
+ */
+ public FlickScrollAnimator(final double deltaX, final double deltaY,
+ final double lastTime) {
+ final double currentTimeMillis = Duration.currentTimeMillis();
+ velX = Math.max(Math.min(deltaX / (currentTimeMillis - lastTime),
+ MAX_SPEED), -MAX_SPEED);
+ velY = Math.max(Math.min(deltaY / (currentTimeMillis - lastTime),
+ MAX_SPEED), -MAX_SPEED);
+
+ lastLeft = horizontalScrollbar.getScrollPos();
+ lastTop = verticalScrollbar.getScrollPos();
+
+ /*
+ * If we're scrolling mainly in one of the four major directions,
+ * and only a teeny bit to any other side, snap the scroll to that
+ * major direction instead.
+ */
+ final double[] snapDeltas = Escalator.snapDeltas(velX, velY,
+ RATIO_OF_30_DEGREES);
+ velX = snapDeltas[0];
+ velY = snapDeltas[1];
+
+ if (velX * velX + velY * velY > MIN_MAGNITUDE) {
+ millisLeft = 1500;
+ xFric = velX / millisLeft;
+ yFric = velY / millisLeft;
+ } else {
+ millisLeft = 0;
+ }
+
+ }
+
+ @Override
+ public void execute(final double doNotUseThisTimestamp) {
+ /*
+ * We cannot use the timestamp provided to this method since it is
+ * of a format that cannot be determined at will. Therefore, we need
+ * a timestamp format that we can handle, so our calculations are
+ * correct.
+ */
+
+ if (millisLeft <= 0 || cancelled) {
+ scroller.currentFlickScroller = null;
+ return;
+ }
+
+ final double timestamp = Duration.currentTimeMillis();
+ if (prevTime == 0) {
+ prevTime = timestamp;
+ AnimationScheduler.get().requestAnimationFrame(this);
+ return;
+ }
+
+ double currentLeft = horizontalScrollbar.getScrollPos();
+ double currentTop = verticalScrollbar.getScrollPos();
+
+ final double timeDiff = timestamp - prevTime;
+ double left = currentLeft - velX * timeDiff;
+ setScrollLeft(left);
+ velX -= xFric * timeDiff;
+
+ double top = currentTop - velY * timeDiff;
+ setScrollTop(top);
+ velY -= yFric * timeDiff;
+
+ cancelBecauseOfEdgeOrCornerMaybe();
+
+ prevTime = timestamp;
+ millisLeft -= timeDiff;
+ lastLeft = currentLeft;
+ lastTop = currentTop;
+ AnimationScheduler.get().requestAnimationFrame(this);
+ }
+
+ private void cancelBecauseOfEdgeOrCornerMaybe() {
+ if (lastLeft == horizontalScrollbar.getScrollPos()
+ && lastTop == verticalScrollbar.getScrollPos()) {
+ cancel();
+ }
+ }
+
+ public void cancel() {
+ cancelled = true;
+ }
+ }
+
+ /**
+ * ScrollDestination case-specific handling logic.
+ */
+ private static double getScrollPos(final ScrollDestination destination,
+ final double targetStartPx, final double targetEndPx,
+ final double viewportStartPx, final double viewportEndPx,
+ final int padding) {
+
+ final double viewportLength = viewportEndPx - viewportStartPx;
+
+ switch (destination) {
+
+ /*
+ * Scroll as little as possible to show the target element. If the
+ * element fits into view, this works as START or END depending on the
+ * current scroll position. If the element does not fit into view, this
+ * works as START.
+ */
+ case ANY: {
+ final double startScrollPos = targetStartPx - padding;
+ final double endScrollPos = targetEndPx + padding - viewportLength;
+
+ if (startScrollPos < viewportStartPx) {
+ return startScrollPos;
+ } else if (targetEndPx + padding > viewportEndPx) {
+ return endScrollPos;
+ } else {
+ // NOOP, it's already visible
+ return viewportStartPx;
+ }
+ }
+
+ /*
+ * Scrolls so that the element is shown at the end of the viewport. The
+ * viewport will, however, not scroll before its first element.
+ */
+ case END: {
+ return targetEndPx + padding - viewportLength;
+ }
+
+ /*
+ * Scrolls so that the element is shown in the middle of the viewport.
+ * The viewport will, however, not scroll beyond its contents, given
+ * more elements than what the viewport is able to show at once. Under
+ * no circumstances will the viewport scroll before its first element.
+ */
+ case MIDDLE: {
+ final double targetMiddle = targetStartPx
+ + (targetEndPx - targetStartPx) / 2;
+ return targetMiddle - viewportLength / 2;
+ }
+
+ /*
+ * Scrolls so that the element is shown at the start of the viewport.
+ * The viewport will, however, not scroll beyond its contents.
+ */
+ case START: {
+ return targetStartPx - padding;
+ }
+
+ /*
+ * Throw an error if we're here. This can only mean that
+ * ScrollDestination has been carelessly amended..
+ */
+ default: {
+ throw new IllegalArgumentException(
+ "Internal: ScrollDestination has been modified, "
+ + "but Escalator.getScrollPos has not been updated "
+ + "to match new values.");
+ }
+ }
+
+ }
+
+ /** An inner class that handles all logic related to scrolling. */
+ private class Scroller extends JsniWorkaround {
+ private double lastScrollTop = 0;
+ private double lastScrollLeft = 0;
+ /**
+ * The current flick scroll animator. This is <code>null</code> if the
+ * view isn't animating a flick scroll at the moment.
+ */
+ private FlickScrollAnimator currentFlickScroller;
+
+ public Scroller() {
+ super(Escalator.this);
+ }
+
+ @Override
+ protected native JavaScriptObject createScrollListenerFunction(
+ Escalator esc)
+ /*-{
+ var vScroll = esc.@com.vaadin.client.ui.grid.Escalator::verticalScrollbar;
+ var vScrollElem = vScroll.@com.vaadin.client.ui.grid.ScrollbarBundle::getElement()();
+
+ var hScroll = esc.@com.vaadin.client.ui.grid.Escalator::horizontalScrollbar;
+ var hScrollElem = hScroll.@com.vaadin.client.ui.grid.ScrollbarBundle::getElement()();
+
+ return $entry(function(e) {
+ var target = e.target || e.srcElement; // IE8 uses e.scrElement
+
+ // in case the scroll event was native (i.e. scrollbars were dragged, or
+ // the scrollTop/Left was manually modified), the bundles have old cache
+ // values. We need to make sure that the caches are kept up to date.
+ if (target === vScrollElem) {
+ vScroll.@com.vaadin.client.ui.grid.ScrollbarBundle::updateScrollPosFromDom()();
+ } else if (target === hScrollElem) {
+ hScroll.@com.vaadin.client.ui.grid.ScrollbarBundle::updateScrollPosFromDom()();
+ } else {
+ $wnd.console.error("unexpected scroll target: "+target);
+ }
+
+ esc.@com.vaadin.client.ui.grid.Escalator::onScroll()();
+ });
+ }-*/;
+
+ @Override
+ protected native JavaScriptObject createMousewheelListenerFunction(
+ Escalator esc)
+ /*-{
+ return $entry(function(e) {
+ var deltaX = e.deltaX ? e.deltaX : -0.5*e.wheelDeltaX;
+ var deltaY = e.deltaY ? e.deltaY : -0.5*e.wheelDeltaY;
+
+ // IE8 has only delta y
+ if (isNaN(deltaY)) {
+ deltaY = -0.5*e.wheelDelta;
+ }
+
+ @com.vaadin.client.ui.grid.Escalator.JsniUtil::moveScrollFromEvent(*)(esc, deltaX, deltaY, e);
+ });
+ }-*/;
+
+ /**
+ * Recalculates the virtual viewport represented by the scrollbars, so
+ * that the sizes of the scroll handles appear correct in the browser
+ */
+ public void recalculateScrollbarsForVirtualViewport() {
+ int scrollContentHeight = body.calculateEstimatedTotalRowHeight();
+ int scrollContentWidth = columnConfiguration.calculateRowWidth();
+
+ double tableWrapperHeight = heightOfEscalator;
+ double tableWrapperWidth = widthOfEscalator;
+
+ boolean verticalScrollNeeded = scrollContentHeight > tableWrapperHeight
+ - header.heightOfSection - footer.heightOfSection;
+ boolean horizontalScrollNeeded = scrollContentWidth > tableWrapperWidth;
+
+ // One dimension got scrollbars, but not the other. Recheck time!
+ if (verticalScrollNeeded != horizontalScrollNeeded) {
+ if (!verticalScrollNeeded && horizontalScrollNeeded) {
+ verticalScrollNeeded = scrollContentHeight > tableWrapperHeight
+ - header.heightOfSection
+ - footer.heightOfSection
+ - horizontalScrollbar.getScrollbarThickness();
+ } else {
+ horizontalScrollNeeded = scrollContentWidth > tableWrapperWidth
+ - verticalScrollbar.getScrollbarThickness();
+ }
+ }
+
+ // let's fix the table wrapper size, since it's now stable.
+ if (verticalScrollNeeded) {
+ tableWrapperWidth -= verticalScrollbar.getScrollbarThickness();
+ }
+ if (horizontalScrollNeeded) {
+ tableWrapperHeight -= horizontalScrollbar
+ .getScrollbarThickness();
+ }
+ tableWrapper.getStyle().setHeight(tableWrapperHeight, Unit.PX);
+ tableWrapper.getStyle().setWidth(tableWrapperWidth, Unit.PX);
+
+ verticalScrollbar.setOffsetSize(tableWrapperHeight
+ - footer.heightOfSection - header.heightOfSection);
+ verticalScrollbar.setScrollSize(scrollContentHeight);
+
+ /*
+ * If decreasing the amount of frozen columns, and scrolled to the
+ * right, the scroll position might reset. So we need to remember
+ * the scroll position, and re-apply it once the scrollbar size has
+ * been adjusted.
+ */
+ double prevScrollPos = horizontalScrollbar.getScrollPos();
+
+ int unfrozenPixels = columnConfiguration
+ .getCalculatedColumnsWidth(Range.between(
+ columnConfiguration.getFrozenColumnCount(),
+ columnConfiguration.getColumnCount()));
+ int frozenPixels = scrollContentWidth - unfrozenPixels;
+ double hScrollOffsetWidth = tableWrapperWidth - frozenPixels;
+ horizontalScrollbar.setOffsetSize(hScrollOffsetWidth);
+ horizontalScrollbar.setScrollSize(unfrozenPixels);
+ horizontalScrollbar.getElement().getStyle()
+ .setLeft(frozenPixels, Unit.PX);
+ horizontalScrollbar.setScrollPos(prevScrollPos);
+ }
+
+ /**
+ * Logical scrolling event handler for the entire widget.
+ */
+ public void onScroll() {
+ if (internalScrollEventCalls > 0) {
+ internalScrollEventCalls--;
+ return;
+ }
+
+ final double scrollTop = verticalScrollbar.getScrollPos();
+ final double scrollLeft = horizontalScrollbar.getScrollPos();
+ if (lastScrollLeft != scrollLeft) {
+ for (int i = 0; i < columnConfiguration.frozenColumns; i++) {
+ header.updateFreezePosition(i, scrollLeft);
+ body.updateFreezePosition(i, scrollLeft);
+ footer.updateFreezePosition(i, scrollLeft);
+ }
+
+ position.set(headElem, -scrollLeft, 0);
+
+ /*
+ * TODO [[optimize]]: cache this value in case the instanceof
+ * check has undesirable overhead. This could also be a
+ * candidate for some deferred binding magic so that e.g.
+ * AbsolutePosition is not even considered in permutations that
+ * we know support something better. That would let the compiler
+ * completely remove the entire condition since it knows that
+ * the if will never be true.
+ */
+ if (position instanceof AbsolutePosition) {
+ /*
+ * we don't want to put "top: 0" on the footer, since it'll
+ * render wrong, as we already have
+ * "bottom: $footer-height".
+ */
+ footElem.getStyle().setLeft(-scrollLeft, Unit.PX);
+ } else {
+ position.set(footElem, -scrollLeft, 0);
+ }
+
+ lastScrollLeft = scrollLeft;
+ }
+
+ body.setBodyScrollPosition(scrollLeft, scrollTop);
+
+ lastScrollTop = scrollTop;
+ body.updateEscalatorRowsOnScroll();
+ /*
+ * TODO [[optimize]]: Might avoid a reflow by first calculating new
+ * scrolltop and scrolleft, then doing the escalator magic based on
+ * those numbers and only updating the positions after that.
+ */
+ }
+
+ public native void attachScrollListener(Element element)
+ /*
+ * Attaching events with JSNI instead of the GWT event mechanism because
+ * GWT didn't provide enough details in events, or triggering the event
+ * handlers with GWT bindings was unsuccessful. Maybe, with more time
+ * and skill, it could be done with better success. JavaScript overlay
+ * types might work. This might also get rid of the JsniWorkaround
+ * class.
+ */
+ /*-{
+ if (element.addEventListener) {
+ element.addEventListener("scroll", this.@com.vaadin.client.ui.grid.JsniWorkaround::scrollListenerFunction);
+ } else {
+ element.attachEvent("onscroll", this.@com.vaadin.client.ui.grid.JsniWorkaround::scrollListenerFunction);
+ }
+ }-*/;
+
+ public native void detachScrollListener(Element element)
+ /*
+ * Attaching events with JSNI instead of the GWT event mechanism because
+ * GWT didn't provide enough details in events, or triggering the event
+ * handlers with GWT bindings was unsuccessful. Maybe, with more time
+ * and skill, it could be done with better success. JavaScript overlay
+ * types might work. This might also get rid of the JsniWorkaround
+ * class.
+ */
+ /*-{
+ if (element.addEventListener) {
+ element.removeEventListener("scroll", this.@com.vaadin.client.ui.grid.JsniWorkaround::scrollListenerFunction);
+ } else {
+ element.detachEvent("onscroll", this.@com.vaadin.client.ui.grid.JsniWorkaround::scrollListenerFunction);
+ }
+ }-*/;
+
+ public native void attachMousewheelListener(Element element)
+ /*
+ * Attaching events with JSNI instead of the GWT event mechanism because
+ * GWT didn't provide enough details in events, or triggering the event
+ * handlers with GWT bindings was unsuccessful. Maybe, with more time
+ * and skill, it could be done with better success. JavaScript overlay
+ * types might work. This might also get rid of the JsniWorkaround
+ * class.
+ */
+ /*-{
+ if (element.addEventListener) {
+ // firefox likes "wheel", while others use "mousewheel"
+ var eventName = element.onwheel===undefined?"mousewheel":"wheel";
+ element.addEventListener(eventName, this.@com.vaadin.client.ui.grid.JsniWorkaround::mousewheelListenerFunction);
+ } else {
+ // IE8
+ element.attachEvent("onmousewheel", this.@com.vaadin.client.ui.grid.JsniWorkaround::mousewheelListenerFunction);
+ }
+ }-*/;
+
+ public native void detachMousewheelListener(Element element)
+ /*
+ * Detaching events with JSNI instead of the GWT event mechanism because
+ * GWT didn't provide enough details in events, or triggering the event
+ * handlers with GWT bindings was unsuccessful. Maybe, with more time
+ * and skill, it could be done with better success. JavaScript overlay
+ * types might work. This might also get rid of the JsniWorkaround
+ * class.
+ */
+ /*-{
+ if (element.addEventListener) {
+ // firefox likes "wheel", while others use "mousewheel"
+ var eventName = element.onwheel===undefined?"mousewheel":"wheel";
+ element.removeEventListener(eventName, this.@com.vaadin.client.ui.grid.JsniWorkaround::mousewheelListenerFunction);
+ } else {
+ // IE8
+ element.detachEvent("onmousewheel", this.@com.vaadin.client.ui.grid.JsniWorkaround::mousewheelListenerFunction);
+ }
+ }-*/;
+
+ public native void attachTouchListeners(Element element)
+ /*
+ * Detaching events with JSNI instead of the GWT event mechanism because
+ * GWT didn't provide enough details in events, or triggering the event
+ * handlers with GWT bindings was unsuccessful. Maybe, with more time
+ * and skill, it could be done with better success. JavaScript overlay
+ * types might work. This might also get rid of the JsniWorkaround
+ * class.
+ */
+ /*-{
+ if (element.addEventListener) {
+ element.addEventListener("touchstart", this.@com.vaadin.client.ui.grid.JsniWorkaround::touchStartFunction);
+ element.addEventListener("touchmove", this.@com.vaadin.client.ui.grid.JsniWorkaround::touchMoveFunction);
+ element.addEventListener("touchend", this.@com.vaadin.client.ui.grid.JsniWorkaround::touchEndFunction);
+ element.addEventListener("touchcancel", this.@com.vaadin.client.ui.grid.JsniWorkaround::touchEndFunction);
+ } else {
+ // this would be IE8, but we don't support it with touch
+ }
+ }-*/;
+
+ public native void detachTouchListeners(Element element)
+ /*
+ * Detaching events with JSNI instead of the GWT event mechanism because
+ * GWT didn't provide enough details in events, or triggering the event
+ * handlers with GWT bindings was unsuccessful. Maybe, with more time
+ * and skill, it could be done with better success. JavaScript overlay
+ * types might work. This might also get rid of the JsniWorkaround
+ * class.
+ */
+ /*-{
+ if (element.removeEventListener) {
+ element.removeEventListener("touchstart", this.@com.vaadin.client.ui.grid.JsniWorkaround::touchStartFunction);
+ element.removeEventListener("touchmove", this.@com.vaadin.client.ui.grid.JsniWorkaround::touchMoveFunction);
+ element.removeEventListener("touchend", this.@com.vaadin.client.ui.grid.JsniWorkaround::touchEndFunction);
+ element.removeEventListener("touchcancel", this.@com.vaadin.client.ui.grid.JsniWorkaround::touchEndFunction);
+ } else {
+ // this would be IE8, but we don't support it with touch
+ }
+ }-*/;
+
+ private void cancelFlickScroll() {
+ if (currentFlickScroller != null) {
+ currentFlickScroller.cancel();
+ }
+ }
+
+ /**
+ * Handles a touch-based flick scroll.
+ *
+ * @param deltaX
+ * the last scrolling delta in the x-axis in a touchmove
+ * @param deltaY
+ * the last scrolling delta in the y-axis in a touchmove
+ * @param lastTime
+ * the timestamp of the last touchmove
+ */
+ public void handleFlickScroll(double deltaX, double deltaY,
+ double lastTime) {
+ currentFlickScroller = new FlickScrollAnimator(deltaX, deltaY,
+ lastTime);
+ AnimationScheduler.get()
+ .requestAnimationFrame(currentFlickScroller);
+ }
+
+ public void scrollToColumn(final int columnIndex,
+ final ScrollDestination destination, final int padding) {
+ assert columnIndex >= columnConfiguration.frozenColumns : "Can't scroll to a frozen column";
+
+ /*
+ * To cope with frozen columns, we just pretend those columns are
+ * not there at all when calculating the position of the target
+ * column and the boundaries of the viewport. The resulting
+ * scrollLeft will be correct without compensation since the DOM
+ * structure effectively means that scrollLeft also ignores the
+ * frozen columns.
+ */
+ final int frozenPixels = columnConfiguration
+ .getCalculatedColumnsWidth(Range.withLength(0,
+ columnConfiguration.frozenColumns));
+
+ final int targetStartPx = columnConfiguration
+ .getCalculatedColumnsWidth(Range.withLength(0, columnIndex))
+ - frozenPixels;
+ final int targetEndPx = targetStartPx
+ + columnConfiguration.getColumnWidthActual(columnIndex);
+
+ final double viewportStartPx = getScrollLeft();
+ double viewportEndPx = viewportStartPx
+ + getElement().getOffsetWidth() - frozenPixels;
+ if (verticalScrollbar.showsScrollHandle()) {
+ viewportEndPx -= Util.getNativeScrollbarSize();
+ }
+
+ final double scrollLeft = getScrollPos(destination, targetStartPx,
+ targetEndPx, viewportStartPx, viewportEndPx, padding);
+
+ /*
+ * note that it doesn't matter if the scroll would go beyond the
+ * content, since the browser will adjust for that, and everything
+ * fall into line accordingly.
+ */
+ setScrollLeft(scrollLeft);
+ }
+
+ public void scrollToRow(final int rowIndex,
+ final ScrollDestination destination, final int padding) {
+ /*
+ * FIXME [[rowheight]]: coded to work only with default row heights
+ * - will not work with variable row heights
+ */
+ final int targetStartPx = body.getDefaultRowHeight() * rowIndex;
+ final int targetEndPx = targetStartPx + body.getDefaultRowHeight();
+
+ final double viewportStartPx = getScrollTop();
+ final double viewportEndPx = viewportStartPx
+ + body.calculateHeight();
+
+ final double scrollTop = getScrollPos(destination, targetStartPx,
+ targetEndPx, viewportStartPx, viewportEndPx, padding);
+
+ /*
+ * note that it doesn't matter if the scroll would go beyond the
+ * content, since the browser will adjust for that, and everything
+ * falls into line accordingly.
+ */
+ setScrollTop(scrollTop);
+ }
+ }
+
+ private abstract class AbstractRowContainer implements RowContainer {
+
+ private EscalatorUpdater updater = EscalatorUpdater.NULL;
+
+ private int rows;
+
+ /**
+ * The table section element ({@code <thead>}, {@code <tbody>} or
+ * {@code <tfoot>}) the rows (i.e. {@code <tr>} tags) are contained in.
+ */
+ protected final TableSectionElement root;
+
+ /** The height of the combined rows in the DOM. */
+ protected double heightOfSection = -1;
+
+ /**
+ * The primary style name of the escalator. Most commonly provided by
+ * Escalator as "v-escalator".
+ */
+ private String primaryStyleName = null;
+
+ /**
+ * A map containing cached values of an element's current top position.
+ * <p>
+ * Don't use this field directly, because it will not take proper care
+ * of all the bookkeeping required.
+ *
+ * @deprecated Use {@link #setRowPosition(Element, int, int)},
+ * {@link #getRowTop(Element)} and
+ * {@link #removeRowPosition(Element)} instead.
+ */
+ @Deprecated
+ private final Map<TableRowElement, Integer> rowTopPositionMap = new HashMap<TableRowElement, Integer>();
+
+ private boolean defaultRowHeightShouldBeAutodetected = true;
+
+ private int defaultRowHeight = INITIAL_DEFAULT_ROW_HEIGHT;
+
+ public AbstractRowContainer(
+ final TableSectionElement rowContainerElement) {
+ root = rowContainerElement;
+ }
+
+ @Override
+ public Element getElement() {
+ return root;
+ }
+
+ /**
+ * Gets the tag name of an element to represent a cell in a row.
+ * <p>
+ * Usually {@code "th"} or {@code "td"}.
+ * <p>
+ * <em>Note:</em> To actually <em>create</em> such an element, use
+ * {@link #createCellElement(int, int)} instead.
+ *
+ * @return the tag name for the element to represent cells as
+ * @see #createCellElement(int, int)
+ */
+ protected abstract String getCellElementTagName();
+
+ @Override
+ public EscalatorUpdater getEscalatorUpdater() {
+ return updater;
+ }
+
+ /**
+ * {@inheritDoc}
+ * <p>
+ * <em>Implementation detail:</em> This method does no DOM modifications
+ * (i.e. is very cheap to call) if there is no data for rows or columns
+ * when this method is called.
+ *
+ * @see #hasColumnAndRowData()
+ */
+ @Override
+ public void setEscalatorUpdater(final EscalatorUpdater escalatorUpdater) {
+ if (escalatorUpdater == null) {
+ throw new IllegalArgumentException(
+ "escalator updater cannot be null");
+ }
+
+ updater = escalatorUpdater;
+
+ if (hasColumnAndRowData() && getRowCount() > 0) {
+ refreshRows(0, getRowCount());
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ * <p>
+ * <em>Implementation detail:</em> This method does no DOM modifications
+ * (i.e. is very cheap to call) if there are no rows in the DOM when
+ * this method is called.
+ *
+ * @see #hasSomethingInDom()
+ */
+ @Override
+ public void removeRows(final int index, final int numberOfRows) {
+ assertArgumentsAreValidAndWithinRange(index, numberOfRows);
+
+ rows -= numberOfRows;
+
+ if (!isAttached()) {
+ return;
+ }
+
+ if (hasSomethingInDom()) {
+ paintRemoveRows(index, numberOfRows);
+ }
+ }
+
+ /**
+ * Removes those row elements from the DOM that correspond to the given
+ * range of logical indices. This may be fewer than {@code numberOfRows}
+ * , even zero, if not all the removed rows are actually visible.
+ * <p>
+ * The implementation must call {@link #paintRemoveRow(Element, int)}
+ * for each row that is removed from the DOM.
+ *
+ * @param index
+ * the logical index of the first removed row
+ * @param numberOfRows
+ * number of logical rows to remove
+ */
+ protected abstract void paintRemoveRows(final int index,
+ final int numberOfRows);
+
+ /**
+ * Removes a row element from the DOM, invoking
+ * {@link #getEscalatorUpdater()}
+ * {@link EscalatorUpdater#preDetach(Row, Iterable) preDetach} and
+ * {@link EscalatorUpdater#postDetach(Row, Iterable) postDetach} before
+ * and after removing the row, respectively.
+ * <p>
+ * This method must be called for each removed DOM row by any
+ * {@link #paintRemoveRows(int, int)} implementation.
+ *
+ * @param tr
+ * the row element to remove.
+ */
+ protected void paintRemoveRow(final TableRowElement tr,
+ final int logicalRowIndex) {
+
+ flyweightRow.setup(tr, logicalRowIndex,
+ columnConfiguration.getCalculatedColumnWidths());
+
+ getEscalatorUpdater().preDetach(flyweightRow,
+ flyweightRow.getCells());
+
+ tr.removeFromParent();
+
+ getEscalatorUpdater().postDetach(flyweightRow,
+ flyweightRow.getCells());
+
+ /*
+ * the "assert" guarantees that this code is run only during
+ * development/debugging.
+ */
+ assert flyweightRow.teardown();
+
+ }
+
+ private void assertArgumentsAreValidAndWithinRange(final int index,
+ final int numberOfRows) throws IllegalArgumentException,
+ IndexOutOfBoundsException {
+ if (numberOfRows < 1) {
+ throw new IllegalArgumentException(
+ "Number of rows must be 1 or greater (was "
+ + numberOfRows + ")");
+ }
+
+ if (index < 0 || index + numberOfRows > getRowCount()) {
+ throw new IndexOutOfBoundsException("The given "
+ + "row range (" + index + ".." + (index + numberOfRows)
+ + ") was outside of the current number of rows ("
+ + getRowCount() + ")");
+ }
+ }
+
+ @Override
+ public int getRowCount() {
+ return rows;
+ }
+
+ /**
+ * {@inheritDoc}
+ * <p>
+ * <em>Implementation detail:</em> This method does no DOM modifications
+ * (i.e. is very cheap to call) if there is no data for columns when
+ * this method is called.
+ *
+ * @see #hasColumnAndRowData()
+ */
+ @Override
+ public void insertRows(final int index, final int numberOfRows) {
+ if (index < 0 || index > getRowCount()) {
+ throw new IndexOutOfBoundsException("The given index (" + index
+ + ") was outside of the current number of rows (0.."
+ + getRowCount() + ")");
+ }
+
+ if (numberOfRows < 1) {
+ throw new IllegalArgumentException(
+ "Number of rows must be 1 or greater (was "
+ + numberOfRows + ")");
+ }
+
+ rows += numberOfRows;
+
+ /*
+ * only add items in the DOM if the widget itself is attached to the
+ * DOM. We can't calculate sizes otherwise.
+ */
+ if (isAttached()) {
+ paintInsertRows(index, numberOfRows);
+ }
+ }
+
+ /**
+ * Actually add rows into the DOM, now that everything can be
+ * calculated.
+ *
+ * @param visualIndex
+ * the DOM index to add rows into
+ * @param numberOfRows
+ * the number of rows to insert
+ * @return a list of the added row elements
+ */
+ protected List<TableRowElement> paintInsertRows(final int visualIndex,
+ final int numberOfRows) {
+ assert isAttached() : "Can't paint rows if Escalator is not attached";
+
+ final List<TableRowElement> addedRows = new ArrayList<TableRowElement>();
+
+ if (numberOfRows < 1) {
+ return addedRows;
+ }
+
+ Node referenceRow;
+ if (root.getChildCount() != 0 && visualIndex != 0) {
+ // get the row node we're inserting stuff after
+ referenceRow = root.getChild(visualIndex - 1);
+ } else {
+ // index is 0, so just prepend.
+ referenceRow = null;
+ }
+
+ for (int row = visualIndex; row < visualIndex + numberOfRows; row++) {
+ final int rowHeight = getDefaultRowHeight();
+ final TableRowElement tr = TableRowElement.as(DOM.createTR());
+ addedRows.add(tr);
+ tr.addClassName(getStylePrimaryName() + "-row");
+
+ for (int col = 0; col < columnConfiguration.getColumnCount(); col++) {
+ final int colWidth = columnConfiguration
+ .getColumnWidthActual(col);
+ final TableCellElement cellElem = createCellElement(
+ rowHeight, colWidth);
+ tr.appendChild(cellElem);
+
+ // Set stylename and position if new cell is frozen
+ if (col < columnConfiguration.frozenColumns) {
+ cellElem.addClassName("frozen");
+ position.set(cellElem, scroller.lastScrollLeft, 0);
+ }
+ }
+
+ referenceRow = paintInsertRow(referenceRow, tr, row);
+ }
+ reapplyRowWidths();
+
+ recalculateSectionHeight();
+
+ return addedRows;
+ }
+
+ /**
+ * Inserts a single row into the DOM, invoking
+ * {@link #getEscalatorUpdater()}
+ * {@link EscalatorUpdater#preAttach(Row, Iterable) preAttach} and
+ * {@link EscalatorUpdater#postAttach(Row, Iterable) postAttach} before
+ * and after inserting the row, respectively. The row should have its
+ * cells already inserted.
+ *
+ * @param referenceRow
+ * the row after which to insert or null if insert as first
+ * @param tr
+ * the row to be inserted
+ * @param logicalRowIndex
+ * the logical index of the inserted row
+ * @return the inserted row to be used as the new reference
+ */
+ protected Node paintInsertRow(Node referenceRow,
+ final TableRowElement tr, int logicalRowIndex) {
+ flyweightRow.setup(tr, logicalRowIndex,
+ columnConfiguration.getCalculatedColumnWidths());
+
+ getEscalatorUpdater().preAttach(flyweightRow,
+ flyweightRow.getCells());
+
+ referenceRow = insertAfterReferenceAndUpdateIt(root, tr,
+ referenceRow);
+
+ getEscalatorUpdater().postAttach(flyweightRow,
+ flyweightRow.getCells());
+ updater.update(flyweightRow, flyweightRow.getCells());
+
+ /*
+ * the "assert" guarantees that this code is run only during
+ * development/debugging.
+ */
+ assert flyweightRow.teardown();
+ return referenceRow;
+ }
+
+ private Node insertAfterReferenceAndUpdateIt(final Element parent,
+ final Element elem, final Node referenceNode) {
+ if (referenceNode != null) {
+ parent.insertAfter(elem, referenceNode);
+ } else {
+ /*
+ * referencenode being null means we have offset 0, i.e. make it
+ * the first row
+ */
+ /*
+ * TODO [[optimize]]: Is insertFirst or append faster for an
+ * empty root?
+ */
+ parent.insertFirst(elem);
+ }
+ return elem;
+ }
+
+ abstract protected void recalculateSectionHeight();
+
+ /**
+ * Returns the estimated height of all rows in the row container.
+ * <p>
+ * The estimate is promised to be correct as long as there are no rows
+ * with calculated heights.
+ */
+ protected int calculateEstimatedTotalRowHeight() {
+ return getDefaultRowHeight() * getRowCount();
+ }
+
+ /**
+ * {@inheritDoc}
+ * <p>
+ * <em>Implementation detail:</em> This method does no DOM modifications
+ * (i.e. is very cheap to call) if there is no data for columns when
+ * this method is called.
+ *
+ * @see #hasColumnAndRowData()
+ */
+ @Override
+ public void refreshRows(final int index, final int numberOfRows) {
+ Profiler.enter("Escalator.AbstractRowContainer.refreshRows");
+
+ assertArgumentsAreValidAndWithinRange(index, numberOfRows);
+
+ if (!isAttached()) {
+ return;
+ }
+
+ /*
+ * TODO [[rowheight]]: even if no rows are evaluated in the current
+ * viewport, the heights of some unrendered rows might change in a
+ * refresh. This would cause the scrollbar to be adjusted (in
+ * scrollHeight and/or scrollTop). Do we want to take this into
+ * account?
+ */
+ if (hasColumnAndRowData()) {
+ /*
+ * TODO [[rowheight]]: nudge rows down with
+ * refreshRowPositions() as needed
+ */
+ for (int row = index; row < index + numberOfRows; row++) {
+ final TableRowElement tr = getTrByVisualIndex(row);
+ refreshRow(tr, row);
+ }
+ }
+
+ Profiler.leave("Escalator.AbstractRowContainer.refreshRows");
+ }
+
+ void refreshRow(final TableRowElement tr, final int logicalRowIndex) {
+ flyweightRow.setup(tr, logicalRowIndex,
+ columnConfiguration.getCalculatedColumnWidths());
+ updater.update(flyweightRow, flyweightRow.getCells());
+
+ /*
+ * the "assert" guarantees that this code is run only during
+ * development/debugging.
+ */
+ assert flyweightRow.teardown();
+ }
+
+ /**
+ * Create and setup an empty cell element.
+ *
+ * @param width
+ * the width of the cell, in pixels
+ * @param height
+ * the height of the cell, in pixels
+ *
+ * @return a set-up empty cell element
+ */
+ @SuppressWarnings("hiding")
+ public TableCellElement createCellElement(final int height,
+ final int width) {
+ final TableCellElement cellElem = TableCellElement.as(DOM
+ .createElement(getCellElementTagName()));
+ cellElem.getStyle().setHeight(height, Unit.PX);
+ cellElem.getStyle().setWidth(width, Unit.PX);
+ cellElem.addClassName(getStylePrimaryName() + "-cell");
+ return cellElem;
+ }
+
+ @Override
+ public TableRowElement getRowElement(int index) {
+ return getTrByVisualIndex(index);
+ }
+
+ /**
+ * Gets the child element that is visually at a certain index
+ *
+ * @param index
+ * the index of the element to retrieve
+ * @return the element at position {@code index}
+ * @throws IndexOutOfBoundsException
+ * if {@code index} is not valid within {@link #root}
+ */
+ protected abstract TableRowElement getTrByVisualIndex(int index)
+ throws IndexOutOfBoundsException;
+
+ protected void paintRemoveColumns(final int offset,
+ final int numberOfColumns,
+ final List<ColumnConfigurationImpl.Column> removedColumns) {
+ final NodeList<Node> childNodes = root.getChildNodes();
+ for (int visualRowIndex = 0; visualRowIndex < childNodes
+ .getLength(); visualRowIndex++) {
+ final TableRowElement tr = getTrByVisualIndex(visualRowIndex);
+
+ flyweightRow.setup(tr, visualRowIndex,
+ columnConfiguration.getCalculatedColumnWidths());
+
+ Iterable<FlyweightCell> cells = flyweightRow.getCells(offset,
+ numberOfColumns);
+
+ getEscalatorUpdater().preDetach(flyweightRow, cells);
+
+ for (FlyweightCell cell : cells) {
+ Element cellElement = cell.getElement();
+ cellElement.removeFromParent();
+ }
+
+ /**
+ * We need a new iterable that does not try to reset the cell
+ * elements from the tr as they're not attached anymore. Instead
+ * the cells simply retain the now-unattached elements that were
+ * assigned on the above iteration.
+ *
+ * TODO a cleaner solution, eg. an iterable that only associates
+ * the elements once
+ */
+ cells = flyweightRow
+ .getUnattachedCells(offset, numberOfColumns);
+ getEscalatorUpdater().postDetach(flyweightRow, cells);
+
+ assert flyweightRow.teardown();
+ }
+ reapplyRowWidths();
+
+ final int firstRemovedColumnLeft = columnConfiguration
+ .getCalculatedColumnsWidth(Range.withLength(0, offset));
+ final boolean columnsWereRemovedFromLeftOfTheViewport = scroller.lastScrollLeft > firstRemovedColumnLeft;
+
+ if (columnsWereRemovedFromLeftOfTheViewport) {
+ int removedColumnsPxAmount = 0;
+ for (ColumnConfigurationImpl.Column removedColumn : removedColumns) {
+ removedColumnsPxAmount += removedColumn
+ .getCalculatedWidth();
+ }
+ final int leftByDiff = (int) (scroller.lastScrollLeft - removedColumnsPxAmount);
+ final int newScrollLeft = Math.max(firstRemovedColumnLeft,
+ leftByDiff);
+ horizontalScrollbar.setScrollPos(newScrollLeft);
+ }
+
+ // this needs to be after the scroll position adjustment above.
+ scroller.recalculateScrollbarsForVirtualViewport();
+
+ /*
+ * Because we might remove columns where affected by colspans, it's
+ * easiest to simply redraw everything when columns are modified.
+ *
+ * Yes, this is a TODO [[optimize]].
+ */
+ if (getRowCount() > 0
+ && getColumnConfiguration().getColumnCount() > 0) {
+ refreshRows(0, getRowCount());
+ }
+ }
+
+ protected void paintInsertColumns(final int offset,
+ final int numberOfColumns, boolean frozen) {
+ final NodeList<Node> childNodes = root.getChildNodes();
+
+ for (int row = 0; row < childNodes.getLength(); row++) {
+ final TableRowElement tr = getTrByVisualIndex(row);
+ paintInsertCells(tr, row, offset, numberOfColumns);
+ }
+ reapplyRowWidths();
+
+ if (frozen) {
+ for (int col = offset; col < offset + numberOfColumns; col++) {
+ setColumnFrozen(col, true);
+ }
+ }
+
+ // this needs to be before the scrollbar adjustment.
+ scroller.recalculateScrollbarsForVirtualViewport();
+
+ int pixelsToInsertedColumn = columnConfiguration
+ .getCalculatedColumnsWidth(Range.withLength(0, offset));
+ final boolean columnsWereAddedToTheLeftOfViewport = scroller.lastScrollLeft > pixelsToInsertedColumn;
+
+ if (columnsWereAddedToTheLeftOfViewport) {
+ int insertedColumnsWidth = columnConfiguration
+ .getCalculatedColumnsWidth(Range.withLength(offset,
+ numberOfColumns));
+ horizontalScrollbar.setScrollPos(scroller.lastScrollLeft
+ + insertedColumnsWidth);
+ }
+
+ /*
+ * Because we might insert columns where affected by colspans, it's
+ * easiest to simply redraw everything when columns are modified.
+ *
+ * Yes, this is a TODO [[optimize]].
+ */
+ if (getRowCount() > 0
+ && getColumnConfiguration().getColumnCount() > 1) {
+ refreshRows(0, getRowCount());
+ }
+ }
+
+ /**
+ * Inserts new cell elements into a single row element, invoking
+ * {@link #getEscalatorUpdater()}
+ * {@link EscalatorUpdater#preAttach(Row, Iterable) preAttach} and
+ * {@link EscalatorUpdater#postAttach(Row, Iterable) postAttach} before
+ * and after inserting the cells, respectively.
+ * <p>
+ * Precondition: The row must be already attached to the DOM and the
+ * FlyweightCell instances corresponding to the new columns added to
+ * {@code flyweightRow}.
+ *
+ * @param tr
+ * the row in which to insert the cells
+ * @param logicalRowIndex
+ * the index of the row
+ * @param offset
+ * the index of the first cell
+ * @param numberOfCells
+ * the number of cells to insert
+ */
+ private void paintInsertCells(final TableRowElement tr,
+ int logicalRowIndex, final int offset, final int numberOfCells) {
+
+ assert Document.get().isOrHasChild(tr) : "The row must be attached to the document";
+
+ flyweightRow.setup(tr, logicalRowIndex,
+ columnConfiguration.getCalculatedColumnWidths());
+
+ Iterable<FlyweightCell> cells = flyweightRow.getUnattachedCells(
+ offset, numberOfCells);
+
+ final int rowHeight = getDefaultRowHeight();
+ for (FlyweightCell cell : cells) {
+ final int colWidth = columnConfiguration
+ .getColumnWidthActual(cell.getColumn());
+ final TableCellElement cellElem = createCellElement(rowHeight,
+ colWidth);
+ cell.setElement(cellElem);
+ }
+
+ getEscalatorUpdater().preAttach(flyweightRow, cells);
+
+ Node referenceCell;
+ if (offset != 0) {
+ referenceCell = tr.getChild(offset - 1);
+ } else {
+ referenceCell = null;
+ }
+ for (FlyweightCell cell : cells) {
+ referenceCell = insertAfterReferenceAndUpdateIt(tr,
+ cell.getElement(), referenceCell);
+ }
+
+ getEscalatorUpdater().postAttach(flyweightRow, cells);
+
+ assert flyweightRow.teardown();
+ }
+
+ public void setColumnFrozen(int column, boolean frozen) {
+ final NodeList<TableRowElement> childRows = root.getRows();
+
+ for (int row = 0; row < childRows.getLength(); row++) {
+ final TableRowElement tr = childRows.getItem(row);
+
+ TableCellElement cell = tr.getCells().getItem(column);
+ if (frozen) {
+ cell.addClassName("frozen");
+ } else {
+ cell.removeClassName("frozen");
+ position.reset(cell);
+ }
+ }
+
+ if (frozen) {
+ updateFreezePosition(column, scroller.lastScrollLeft);
+ }
+ }
+
+ public void updateFreezePosition(int column, double scrollLeft) {
+ final NodeList<TableRowElement> childRows = root.getRows();
+
+ for (int row = 0; row < childRows.getLength(); row++) {
+ final TableRowElement tr = childRows.getItem(row);
+
+ TableCellElement cell = tr.getCells().getItem(column);
+ position.set(cell, scrollLeft, 0);
+ }
+ }
+
+ /**
+ * Iterates through all the cells in a column and returns the width of
+ * the widest element in this RowContainer.
+ *
+ * @param index
+ * the index of the column to inspect
+ * @return the pixel width of the widest element in the indicated column
+ */
+ public int calculateMaxColWidth(int index) {
+ TableRowElement row = TableRowElement.as(root
+ .getFirstChildElement());
+ int maxWidth = 0;
+ while (row != null) {
+ final TableCellElement cell = row.getCells().getItem(index);
+ final boolean isVisible = !cell.getStyle().getDisplay()
+ .equals(Display.NONE.getCssName());
+ if (isVisible) {
+ maxWidth = Math.max(maxWidth, cell.getScrollWidth());
+ }
+ row = TableRowElement.as(row.getNextSiblingElement());
+ }
+ return maxWidth;
+ }
+
+ /**
+ * Reapplies all the cells' widths according to the calculated widths in
+ * the column configuration.
+ */
+ public void reapplyColumnWidths() {
+ Element row = root.getFirstChildElement();
+ while (row != null) {
+ Element cell = row.getFirstChildElement();
+ int columnIndex = 0;
+ while (cell != null) {
+ @SuppressWarnings("hiding")
+ final int width = getCalculatedColumnWidthWithColspan(cell,
+ columnIndex);
+
+ /*
+ * TODO Should Escalator implement ProvidesResize at some
+ * point, this is where we need to do that.
+ */
+ cell.getStyle().setWidth(width, Unit.PX);
+
+ cell = cell.getNextSiblingElement();
+ columnIndex++;
+ }
+ row = row.getNextSiblingElement();
+ }
+
+ reapplyRowWidths();
+ }
+
+ private int getCalculatedColumnWidthWithColspan(final Element cell,
+ final int columnIndex) {
+ final int colspan = cell.getPropertyInt(FlyweightCell.COLSPAN_ATTR);
+ Range spannedColumns = Range.withLength(columnIndex, colspan);
+
+ /*
+ * Since browsers don't explode with overflowing colspans, escalator
+ * shouldn't either.
+ */
+ if (spannedColumns.getEnd() > columnConfiguration.getColumnCount()) {
+ spannedColumns = Range.between(columnIndex,
+ columnConfiguration.getColumnCount());
+ }
+ return columnConfiguration
+ .getCalculatedColumnsWidth(spannedColumns);
+ }
+
+ /**
+ * Applies the total length of the columns to each row element.
+ * <p>
+ * <em>Note:</em> In contrast to {@link #reapplyColumnWidths()}, this
+ * method only modifies the width of the {@code <tr>} element, not the
+ * cells within.
+ */
+ protected void reapplyRowWidths() {
+ int rowWidth = columnConfiguration.calculateRowWidth();
+
+ com.google.gwt.dom.client.Element row = root.getFirstChildElement();
+ while (row != null) {
+ row.getStyle().setWidth(rowWidth, Unit.PX);
+ row = row.getNextSiblingElement();
+ }
+ }
+
+ /**
+ * The primary style name for the container.
+ *
+ * @param primaryStyleName
+ * the style name to use as prefix for all row and cell style
+ * names.
+ */
+ protected void setStylePrimaryName(String primaryStyleName) {
+ String oldStyle = getStylePrimaryName();
+ if (SharedUtil.equals(oldStyle, primaryStyleName)) {
+ return;
+ }
+
+ this.primaryStyleName = primaryStyleName;
+
+ // Update already rendered rows and cells
+ TableRowElement row = root.getRows().getItem(0);
+ while (row != null) {
+ UIObject.setStylePrimaryName(row, primaryStyleName + "-row");
+ TableCellElement cell = row.getCells().getItem(0);
+ while (cell != null) {
+ UIObject.setStylePrimaryName(cell, primaryStyleName
+ + "-cell");
+ cell = TableCellElement.as(cell.getNextSiblingElement());
+ }
+ row = TableRowElement.as(row.getNextSiblingElement());
+ }
+ }
+
+ /**
+ * Returns the primary style name of the container.
+ *
+ * @return The primary style name or <code>null</code> if not set.
+ */
+ protected String getStylePrimaryName() {
+ return primaryStyleName;
+ }
+
+ @Override
+ public void setDefaultRowHeight(int px) throws IllegalArgumentException {
+ if (px < 1) {
+ throw new IllegalArgumentException("Height must be positive. "
+ + px + " was given.");
+ }
+
+ defaultRowHeightShouldBeAutodetected = false;
+ defaultRowHeight = px;
+ reapplyDefaultRowHeights();
+ }
+
+ @Override
+ public int getDefaultRowHeight() {
+ return defaultRowHeight;
+ }
+
+ /**
+ * The default height of rows has (most probably) changed.
+ * <p>
+ * Make sure that the displayed rows with a default height are updated
+ * in height and top position.
+ * <p>
+ * <em>Note:</em>This implementation should not call
+ * {@link Escalator#recalculateElementSizes()} - it is done by the
+ * discretion of the caller of this method.
+ */
+ protected abstract void reapplyDefaultRowHeights();
+
+ protected void reapplyRowHeight(final TableRowElement tr,
+ final int heightPx) {
+ Element cellElem = tr.getFirstChildElement();
+ while (cellElem != null) {
+ cellElem.getStyle().setHeight(heightPx, Unit.PX);
+ cellElem = cellElem.getNextSiblingElement();
+ }
+
+ /*
+ * no need to apply height to tr-element, it'll be resized
+ * implicitly.
+ */
+ }
+
+ @SuppressWarnings("boxing")
+ protected void setRowPosition(final TableRowElement tr, final int x,
+ final int y) {
+ position.set(tr, x, y);
+ rowTopPositionMap.put(tr, y);
+ }
+
+ @SuppressWarnings("boxing")
+ protected int getRowTop(final TableRowElement tr) {
+ return rowTopPositionMap.get(tr);
+ }
+
+ protected void removeRowPosition(TableRowElement tr) {
+ rowTopPositionMap.remove(tr);
+ }
+
+ public void autodetectRowHeight() {
+ Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand() {
+
+ @Override
+ public void execute() {
+ if (defaultRowHeightShouldBeAutodetected && isAttached()) {
+ final Element detectionTr = DOM.createTR();
+ detectionTr
+ .setClassName(getStylePrimaryName() + "-row");
+
+ final Element cellElem = DOM
+ .createElement(getCellElementTagName());
+ cellElem.setClassName(getStylePrimaryName() + "-cell");
+ cellElem.setInnerHTML("foo");
+
+ detectionTr.appendChild(cellElem);
+ root.appendChild(detectionTr);
+ defaultRowHeight = Math.max(1,
+ cellElem.getOffsetHeight());
+ root.removeChild(detectionTr);
+
+ if (root.hasChildNodes()) {
+ reapplyDefaultRowHeights();
+ }
+
+ defaultRowHeightShouldBeAutodetected = false;
+ }
+ }
+ });
+ }
+
+ @Override
+ public Cell getCell(final Element element) {
+ if (element == null) {
+ throw new IllegalArgumentException("Element cannot be null");
+ }
+
+ /*
+ * Ensure that element is not root nor the direct descendant of root
+ * (a row) and ensure the element is inside the dom hierarchy of the
+ * root element. If not, return.
+ */
+ if (root == element || element.getParentElement() == root
+ || !root.isOrHasChild(element)) {
+ return null;
+ }
+
+ /*
+ * Ensure element is the cell element by iterating up the DOM
+ * hierarchy until reaching cell element.
+ */
+ Element cellElementCandidate = element;
+ while (cellElementCandidate.getParentElement().getParentElement() != root) {
+ cellElementCandidate = cellElementCandidate.getParentElement();
+ }
+ final TableCellElement cellElement = TableCellElement
+ .as(cellElementCandidate);
+
+ // Find dom column
+ int domColumnIndex = -1;
+ for (Element e = cellElement; e != null; e = e
+ .getPreviousSiblingElement()) {
+ domColumnIndex++;
+ }
+
+ // Find dom row
+ int domRowIndex = -1;
+ for (Element e = cellElement.getParentElement(); e != null; e = e
+ .getPreviousSiblingElement()) {
+ domRowIndex++;
+ }
+
+ return new Cell(domRowIndex, domColumnIndex, cellElement);
+ }
+ }
+
+ private abstract class AbstractStaticRowContainer extends
+ AbstractRowContainer {
+ public AbstractStaticRowContainer(final TableSectionElement headElement) {
+ super(headElement);
+ }
+
+ @Override
+ protected void paintRemoveRows(final int index, final int numberOfRows) {
+ for (int i = index; i < index + numberOfRows; i++) {
+ final TableRowElement tr = root.getRows().getItem(index);
+ paintRemoveRow(tr, index);
+ }
+ recalculateSectionHeight();
+ }
+
+ @Override
+ protected TableRowElement getTrByVisualIndex(final int index)
+ throws IndexOutOfBoundsException {
+ if (index >= 0 && index < root.getChildCount()) {
+ return root.getRows().getItem(index);
+ } else {
+ throw new IndexOutOfBoundsException("No such visual index: "
+ + index);
+ }
+ }
+
+ @Override
+ public void insertRows(int index, int numberOfRows) {
+ super.insertRows(index, numberOfRows);
+ recalculateElementSizes();
+ applyHeightByRows();
+ }
+
+ @Override
+ public void removeRows(int index, int numberOfRows) {
+ super.removeRows(index, numberOfRows);
+ recalculateElementSizes();
+ applyHeightByRows();
+ }
+
+ @Override
+ protected void reapplyDefaultRowHeights() {
+ if (root.getChildCount() == 0) {
+ return;
+ }
+
+ Profiler.enter("Escalator.AbstractStaticRowContainer.reapplyDefaultRowHeights");
+
+ TableRowElement tr = root.getRows().getItem(0);
+ while (tr != null) {
+ reapplyRowHeight(tr, getDefaultRowHeight());
+ tr = TableRowElement.as(tr.getNextSiblingElement());
+ }
+
+ /*
+ * Because all rows are immediately displayed in the static row
+ * containers, the section's overall height has most probably
+ * changed.
+ */
+ recalculateSectionHeight();
+
+ Profiler.leave("Escalator.AbstractStaticRowContainer.reapplyDefaultRowHeights");
+ }
+
+ @Override
+ protected void recalculateSectionHeight() {
+ Profiler.enter("Escalator.AbstractStaticRowContainer.recalculateSectionHeight");
+
+ int newHeight = calculateEstimatedTotalRowHeight();
+ if (newHeight != heightOfSection) {
+ heightOfSection = newHeight;
+ sectionHeightCalculated();
+ body.verifyEscalatorCount();
+ }
+
+ Profiler.leave("Escalator.AbstractStaticRowContainer.recalculateSectionHeight");
+ }
+
+ /**
+ * Informs the row container that the height of its respective table
+ * section has changed.
+ * <p>
+ * These calculations might affect some layouting logic, such as the
+ * body is being offset by the footer, the footer needs to be readjusted
+ * according to its height, and so on.
+ * <p>
+ * A table section is either header, body or footer.
+ */
+ protected abstract void sectionHeightCalculated();
+ }
+
+ private class HeaderRowContainer extends AbstractStaticRowContainer {
+ public HeaderRowContainer(final TableSectionElement headElement) {
+ super(headElement);
+ }
+
+ @Override
+ protected void sectionHeightCalculated() {
+ bodyElem.getStyle().setMarginTop(heightOfSection, Unit.PX);
+ verticalScrollbar.getElement().getStyle()
+ .setTop(heightOfSection, Unit.PX);
+ }
+
+ @Override
+ protected String getCellElementTagName() {
+ return "th";
+ }
+
+ @Override
+ public void setStylePrimaryName(String primaryStyleName) {
+ super.setStylePrimaryName(primaryStyleName);
+ UIObject.setStylePrimaryName(root, primaryStyleName + "-header");
+ }
+ }
+
+ private class FooterRowContainer extends AbstractStaticRowContainer {
+ public FooterRowContainer(final TableSectionElement footElement) {
+ super(footElement);
+ }
+
+ @Override
+ public void setStylePrimaryName(String primaryStyleName) {
+ super.setStylePrimaryName(primaryStyleName);
+ UIObject.setStylePrimaryName(root, primaryStyleName + "-footer");
+ }
+
+ @Override
+ protected String getCellElementTagName() {
+ return "td";
+ }
+
+ @Override
+ protected void sectionHeightCalculated() {
+ int vscrollHeight = (int) Math.floor(heightOfEscalator
+ - header.heightOfSection - footer.heightOfSection);
+
+ final boolean horizontalScrollbarNeeded = columnConfiguration
+ .calculateRowWidth() > widthOfEscalator;
+ if (horizontalScrollbarNeeded) {
+ vscrollHeight -= horizontalScrollbar.getScrollbarThickness();
+ }
+
+ verticalScrollbar.setOffsetSize(vscrollHeight);
+ }
+ }
+
+ private class BodyRowContainer extends AbstractRowContainer {
+ /*
+ * TODO [[optimize]]: check whether a native JsArray might be faster
+ * than LinkedList
+ */
+ /**
+ * The order in which row elements are rendered visually in the browser,
+ * with the help of CSS tricks. Usually has nothing to do with the DOM
+ * order.
+ *
+ * @see #sortDomElements()
+ */
+ private final LinkedList<TableRowElement> visualRowOrder = new LinkedList<TableRowElement>();
+
+ /**
+ * The logical index of the topmost row.
+ *
+ * @deprecated Use the accessors {@link #setTopRowLogicalIndex(int)},
+ * {@link #updateTopRowLogicalIndex(int)} and
+ * {@link #getTopRowLogicalIndex()} instead
+ */
+ @Deprecated
+ private int topRowLogicalIndex = 0;
+
+ private void setTopRowLogicalIndex(int topRowLogicalIndex) {
+ if (LogConfiguration.loggingIsEnabled(Level.INFO)) {
+ Logger.getLogger("Escalator.BodyRowContainer").fine(
+ "topRowLogicalIndex: " + this.topRowLogicalIndex
+ + " -> " + topRowLogicalIndex);
+ }
+ assert topRowLogicalIndex >= 0 : "topRowLogicalIndex became negative";
+ /*
+ * if there's a smart way of evaluating and asserting the max index,
+ * this would be a nice place to put it. I haven't found out an
+ * effective and generic solution.
+ */
+
+ this.topRowLogicalIndex = topRowLogicalIndex;
+ }
+
+ private int getTopRowLogicalIndex() {
+ return topRowLogicalIndex;
+ }
+
+ private void updateTopRowLogicalIndex(int diff) {
+ setTopRowLogicalIndex(topRowLogicalIndex + diff);
+ }
+
+ private class DeferredDomSorter {
+ private static final int SORT_DELAY_MILLIS = 50;
+
+ // as it happens, 3 frames = 50ms @ 60fps.
+ private static final int REQUIRED_FRAMES_PASSED = 3;
+
+ private final AnimationCallback frameCounter = new AnimationCallback() {
+ @Override
+ public void execute(double timestamp) {
+ framesPassed++;
+ boolean domWasSorted = sortIfConditionsMet();
+ if (!domWasSorted) {
+ animationHandle = AnimationScheduler.get()
+ .requestAnimationFrame(this);
+ }
+ }
+ };
+
+ private int framesPassed;
+ private double startTime;
+ private AnimationHandle animationHandle;
+
+ public void reschedule() {
+ resetConditions();
+ animationHandle = AnimationScheduler.get()
+ .requestAnimationFrame(frameCounter);
+ }
+
+ private boolean sortIfConditionsMet() {
+ boolean enoughFramesHavePassed = framesPassed >= REQUIRED_FRAMES_PASSED;
+ boolean enoughTimeHasPassed = (Duration.currentTimeMillis() - startTime) >= SORT_DELAY_MILLIS;
+ boolean conditionsMet = enoughFramesHavePassed
+ && enoughTimeHasPassed;
+
+ if (conditionsMet) {
+ resetConditions();
+ sortDomElements();
+ }
+
+ return conditionsMet;
+ }
+
+ private void resetConditions() {
+ if (animationHandle != null) {
+ animationHandle.cancel();
+ animationHandle = null;
+ }
+ startTime = Duration.currentTimeMillis();
+ framesPassed = 0;
+ }
+ }
+
+ private DeferredDomSorter domSorter = new DeferredDomSorter();
+
+ public BodyRowContainer(final TableSectionElement bodyElement) {
+ super(bodyElement);
+ }
+
+ @Override
+ public void setStylePrimaryName(String primaryStyleName) {
+ super.setStylePrimaryName(primaryStyleName);
+ UIObject.setStylePrimaryName(root, primaryStyleName + "-body");
+ }
+
+ public void updateEscalatorRowsOnScroll() {
+ if (visualRowOrder.isEmpty()) {
+ return;
+ }
+
+ boolean rowsWereMoved = false;
+
+ final double topRowPos = getRowTop(visualRowOrder.getFirst());
+ // TODO [[mpixscroll]]
+ final double scrollTop = tBodyScrollTop;
+ final double viewportOffset = topRowPos - scrollTop;
+
+ /*
+ * TODO [[optimize]] this if-else can most probably be refactored
+ * into a neater block of code
+ */
+
+ if (viewportOffset > 0) {
+ // there's empty room on top
+
+ /*
+ * FIXME [[rowheight]]: coded to work only with default row
+ * heights - will not work with variable row heights
+ */
+ int originalRowsToMove = (int) Math.ceil(viewportOffset
+ / getDefaultRowHeight());
+ int rowsToMove = Math.min(originalRowsToMove,
+ root.getChildCount());
+
+ final int end = root.getChildCount();
+ final int start = end - rowsToMove;
+ /*
+ * FIXME [[rowheight]]: coded to work only with default row
+ * heights - will not work with variable row heights
+ */
+ final int logicalRowIndex = (int) (scrollTop / getDefaultRowHeight());
+ moveAndUpdateEscalatorRows(Range.between(start, end), 0,
+ logicalRowIndex);
+
+ updateTopRowLogicalIndex(-originalRowsToMove);
+
+ rowsWereMoved = true;
+ }
+
+ else if (viewportOffset + getDefaultRowHeight() <= 0) {
+ /*
+ * FIXME [[rowheight]]: coded to work only with default row
+ * heights - will not work with variable row heights
+ */
+
+ /*
+ * the viewport has been scrolled more than the topmost visual
+ * row.
+ */
+
+ int originalRowsToMove = (int) Math.abs(viewportOffset
+ / getDefaultRowHeight());
+ int rowsToMove = Math.min(originalRowsToMove,
+ root.getChildCount());
+
+ int logicalRowIndex;
+ if (rowsToMove < root.getChildCount()) {
+ /*
+ * We scroll so little that we can just keep adding the rows
+ * below the current escalator
+ */
+ logicalRowIndex = getLogicalRowIndex(visualRowOrder
+ .getLast()) + 1;
+ } else {
+ /*
+ * FIXME [[rowheight]]: coded to work only with default row
+ * heights - will not work with variable row heights
+ */
+ /*
+ * Since we're moving all escalator rows, we need to
+ * calculate the first logical row index from the scroll
+ * position.
+ */
+ logicalRowIndex = (int) (scrollTop / getDefaultRowHeight());
+ }
+
+ /*
+ * Since we're moving the viewport downwards, the visual index
+ * is always at the bottom. Note: Due to how
+ * moveAndUpdateEscalatorRows works, this will work out even if
+ * we move all the rows, and try to place them "at the end".
+ */
+ final int targetVisualIndex = root.getChildCount();
+
+ // make sure that we don't move rows over the data boundary
+ boolean aRowWasLeftBehind = false;
+ if (logicalRowIndex + rowsToMove > getRowCount()) {
+ /*
+ * TODO [[rowheight]]: with constant row heights, there's
+ * always exactly one row that will be moved beyond the data
+ * source, when viewport is scrolled to the end. This,
+ * however, isn't guaranteed anymore once row heights start
+ * varying.
+ */
+ rowsToMove--;
+ aRowWasLeftBehind = true;
+ }
+
+ moveAndUpdateEscalatorRows(Range.between(0, rowsToMove),
+ targetVisualIndex, logicalRowIndex);
+
+ if (aRowWasLeftBehind) {
+ /*
+ * To keep visualRowOrder as a spatially contiguous block of
+ * rows, let's make sure that the one row we didn't move
+ * visually still stays with the pack.
+ */
+ final Range strayRow = Range.withOnly(0);
+
+ /*
+ * We cannot trust getLogicalRowIndex, because it hasn't yet
+ * been updated. But since we're leaving rows behind, it
+ * means we've scrolled to the bottom. So, instead, we
+ * simply count backwards from the end.
+ */
+ final int topLogicalIndex = getRowCount()
+ - visualRowOrder.size();
+ moveAndUpdateEscalatorRows(strayRow, 0, topLogicalIndex);
+ }
+
+ final int naiveNewLogicalIndex = getTopRowLogicalIndex()
+ + originalRowsToMove;
+ final int maxLogicalIndex = getRowCount()
+ - visualRowOrder.size();
+ setTopRowLogicalIndex(Math.min(naiveNewLogicalIndex,
+ maxLogicalIndex));
+
+ rowsWereMoved = true;
+ }
+
+ if (rowsWereMoved) {
+ fireRowVisibilityChangeEvent();
+
+ if (scroller.touchHandlerBundle.touches == 0) {
+ /*
+ * this will never be called on touch scrolling. That is
+ * handled separately and explicitly by
+ * TouchHandlerBundle.touchEnd();
+ */
+ domSorter.reschedule();
+ }
+ }
+ }
+
+ @Override
+ protected List<TableRowElement> paintInsertRows(final int index,
+ final int numberOfRows) {
+ if (numberOfRows == 0) {
+ return Collections.emptyList();
+ }
+
+ /*
+ * TODO: this method should probably only add physical rows, and not
+ * populate them - let everything be populated as appropriate by the
+ * logic that follows.
+ *
+ * This also would lead to the fact that paintInsertRows wouldn't
+ * need to return anything.
+ */
+ final List<TableRowElement> addedRows = fillAndPopulateEscalatorRowsIfNeeded(
+ index, numberOfRows);
+
+ /*
+ * insertRows will always change the number of rows - update the
+ * scrollbar sizes.
+ */
+ scroller.recalculateScrollbarsForVirtualViewport();
+
+ /*
+ * FIXME [[rowheight]]: coded to work only with default row heights
+ * - will not work with variable row heights
+ */
+ final boolean addedRowsAboveCurrentViewport = index
+ * getDefaultRowHeight() < getScrollTop();
+ final boolean addedRowsBelowCurrentViewport = index
+ * getDefaultRowHeight() > getScrollTop()
+ + calculateHeight();
+
+ if (addedRowsAboveCurrentViewport) {
+ /*
+ * We need to tweak the virtual viewport (scroll handle
+ * positions, table "scroll position" and row locations), but
+ * without re-evaluating any rows.
+ */
+
+ /*
+ * FIXME [[rowheight]]: coded to work only with default row
+ * heights - will not work with variable row heights
+ */
+ final int yDelta = numberOfRows * getDefaultRowHeight();
+ adjustScrollPosIgnoreEvents(yDelta);
+ updateTopRowLogicalIndex(numberOfRows);
+ }
+
+ else if (addedRowsBelowCurrentViewport) {
+ // NOOP, we already recalculated scrollbars.
+ }
+
+ else { // some rows were added inside the current viewport
+
+ final int unupdatedLogicalStart = index + addedRows.size();
+ final int visualOffset = getLogicalRowIndex(visualRowOrder
+ .getFirst());
+
+ /*
+ * At this point, we have added new escalator rows, if so
+ * needed.
+ *
+ * If more rows were added than the new escalator rows can
+ * account for, we need to start to spin the escalator to update
+ * the remaining rows aswell.
+ */
+ final int rowsStillNeeded = numberOfRows - addedRows.size();
+ final Range unupdatedVisual = convertToVisual(Range.withLength(
+ unupdatedLogicalStart, rowsStillNeeded));
+ final int end = root.getChildCount();
+ final int start = end - unupdatedVisual.length();
+ final int visualTargetIndex = unupdatedLogicalStart
+ - visualOffset;
+ moveAndUpdateEscalatorRows(Range.between(start, end),
+ visualTargetIndex, unupdatedLogicalStart);
+
+ /*
+ * FIXME [[rowheight]]: coded to work only with default row
+ * heights - will not work with variable row heights
+ */
+ // move the surrounding rows to their correct places.
+ int rowTop = (unupdatedLogicalStart + (end - start))
+ * getDefaultRowHeight();
+ final ListIterator<TableRowElement> i = visualRowOrder
+ .listIterator(visualTargetIndex + (end - start));
+ while (i.hasNext()) {
+ final TableRowElement tr = i.next();
+ setRowPosition(tr, 0, rowTop);
+ /*
+ * FIXME [[rowheight]]: coded to work only with default row
+ * heights - will not work with variable row heights
+ */
+ rowTop += getDefaultRowHeight();
+ }
+
+ fireRowVisibilityChangeEvent();
+ sortDomElements();
+ }
+ return addedRows;
+ }
+
+ /**
+ * Move escalator rows around, and make sure everything gets
+ * appropriately repositioned and repainted.
+ *
+ * @param visualSourceRange
+ * the range of rows to move to a new place
+ * @param visualTargetIndex
+ * the visual index where the rows will be placed to
+ * @param logicalTargetIndex
+ * the logical index to be assigned to the first moved row
+ * @throws IllegalArgumentException
+ * if any of <code>visualSourceRange.getStart()</code>,
+ * <code>visualTargetIndex</code> or
+ * <code>logicalTargetIndex</code> is a negative number; or
+ * if <code>visualTargetInfo</code> is greater than the
+ * number of escalator rows.
+ */
+ private void moveAndUpdateEscalatorRows(final Range visualSourceRange,
+ final int visualTargetIndex, final int logicalTargetIndex)
+ throws IllegalArgumentException {
+
+ if (visualSourceRange.isEmpty()) {
+ return;
+ }
+
+ if (visualSourceRange.getStart() < 0) {
+ throw new IllegalArgumentException(
+ "Logical source start must be 0 or greater (was "
+ + visualSourceRange.getStart() + ")");
+ } else if (logicalTargetIndex < 0) {
+ throw new IllegalArgumentException(
+ "Logical target must be 0 or greater");
+ } else if (visualTargetIndex < 0) {
+ throw new IllegalArgumentException(
+ "Visual target must be 0 or greater");
+ } else if (visualTargetIndex > root.getChildCount()) {
+ throw new IllegalArgumentException(
+ "Visual target must not be greater than the number of escalator rows");
+ } else if (logicalTargetIndex + visualSourceRange.length() > getRowCount()) {
+ final int logicalEndIndex = logicalTargetIndex
+ + visualSourceRange.length() - 1;
+ throw new IllegalArgumentException(
+ "Logical target leads to rows outside of the data range ("
+ + logicalTargetIndex + ".." + logicalEndIndex
+ + ")");
+ }
+
+ /*
+ * Since we move a range into another range, the indices might move
+ * about. Having 10 rows, if we move 0..1 to index 10 (to the end of
+ * the collection), the target range will end up being 8..9, instead
+ * of 10..11.
+ *
+ * This applies only if we move elements forward in the collection,
+ * not backward.
+ */
+ final int adjustedVisualTargetIndex;
+ if (visualSourceRange.getStart() < visualTargetIndex) {
+ adjustedVisualTargetIndex = visualTargetIndex
+ - visualSourceRange.length();
+ } else {
+ adjustedVisualTargetIndex = visualTargetIndex;
+ }
+
+ if (visualSourceRange.getStart() != adjustedVisualTargetIndex) {
+
+ /*
+ * Reorder the rows to their correct places within
+ * visualRowOrder (unless rows are moved back to their original
+ * places)
+ */
+
+ /*
+ * TODO [[optimize]]: move whichever set is smaller: the ones
+ * explicitly moved, or the others. So, with 10 escalator rows,
+ * if we are asked to move idx[0..8] to the end of the list,
+ * it's faster to just move idx[9] to the beginning.
+ */
+
+ final List<TableRowElement> removedRows = new ArrayList<TableRowElement>(
+ visualSourceRange.length());
+ for (int i = 0; i < visualSourceRange.length(); i++) {
+ final TableRowElement tr = visualRowOrder
+ .remove(visualSourceRange.getStart());
+ removedRows.add(tr);
+ }
+ visualRowOrder.addAll(adjustedVisualTargetIndex, removedRows);
+ }
+
+ { // Refresh the contents of the affected rows
+ final ListIterator<TableRowElement> iter = visualRowOrder
+ .listIterator(adjustedVisualTargetIndex);
+ for (int logicalIndex = logicalTargetIndex; logicalIndex < logicalTargetIndex
+ + visualSourceRange.length(); logicalIndex++) {
+ final TableRowElement tr = iter.next();
+ refreshRow(tr, logicalIndex);
+ }
+ }
+
+ { // Reposition the rows that were moved
+ /*
+ * FIXME [[rowheight]]: coded to work only with default row
+ * heights - will not work with variable row heights
+ */
+ int newRowTop = logicalTargetIndex * getDefaultRowHeight();
+
+ final ListIterator<TableRowElement> iter = visualRowOrder
+ .listIterator(adjustedVisualTargetIndex);
+ for (int i = 0; i < visualSourceRange.length(); i++) {
+ final TableRowElement tr = iter.next();
+ setRowPosition(tr, 0, newRowTop);
+ /*
+ * FIXME [[rowheight]]: coded to work only with default row
+ * heights - will not work with variable row heights
+ */
+ newRowTop += getDefaultRowHeight();
+ }
+ }
+ }
+
+ /**
+ * Adjust the scroll position without having the scroll handler have any
+ * side-effects.
+ * <p>
+ * <em>Note:</em> {@link Scroller#onScroll()} <em>will</em> be
+ * triggered, but it will not do anything, with the help of
+ * {@link Escalator#internalScrollEventCalls}.
+ *
+ * @param yDelta
+ * the delta of pixels to scrolls. A positive value moves the
+ * viewport downwards, while a negative value moves the
+ * viewport upwards
+ */
+ public void adjustScrollPosIgnoreEvents(final double yDelta) {
+ if (yDelta == 0) {
+ return;
+ }
+
+ internalScrollEventCalls++;
+ verticalScrollbar.setScrollPosByDelta(yDelta);
+
+ /*
+ * FIXME [[rowheight]]: coded to work only with default row heights
+ * - will not work with variable row heights
+ */
+ final int rowTopPos = (int) yDelta
+ - ((int) yDelta % getDefaultRowHeight());
+ for (final TableRowElement tr : visualRowOrder) {
+ setRowPosition(tr, 0, getRowTop(tr) + rowTopPos);
+ }
+ setBodyScrollPosition(tBodyScrollLeft, tBodyScrollTop + yDelta);
+ }
+
+ /**
+ * Adds new physical escalator rows to the DOM at the given index if
+ * there's still a need for more escalator rows.
+ * <p>
+ * If Escalator already is at (or beyond) max capacity, this method does
+ * nothing to the DOM.
+ *
+ * @param index
+ * the index at which to add new escalator rows.
+ * <em>Note:</em>It is assumed that the index is both the
+ * visual index and the logical index.
+ * @param numberOfRows
+ * the number of rows to add at <code>index</code>
+ * @return a list of the added rows
+ */
+ private List<TableRowElement> fillAndPopulateEscalatorRowsIfNeeded(
+ final int index, final int numberOfRows) {
+
+ final int escalatorRowsStillFit = getMaxEscalatorRowCapacity()
+ - root.getChildCount();
+ final int escalatorRowsNeeded = Math.min(numberOfRows,
+ escalatorRowsStillFit);
+
+ if (escalatorRowsNeeded > 0) {
+
+ final List<TableRowElement> addedRows = super.paintInsertRows(
+ index, escalatorRowsNeeded);
+ visualRowOrder.addAll(index, addedRows);
+
+ /*
+ * We need to figure out the top positions for the rows we just
+ * added.
+ */
+ for (int i = 0; i < addedRows.size(); i++) {
+ /*
+ * FIXME [[rowheight]]: coded to work only with default row
+ * heights - will not work with variable row heights
+ */
+ setRowPosition(addedRows.get(i), 0, (index + i)
+ * getDefaultRowHeight());
+ }
+
+ /* Move the other rows away from above the added escalator rows */
+ for (int i = index + addedRows.size(); i < visualRowOrder
+ .size(); i++) {
+ final TableRowElement tr = visualRowOrder.get(i);
+ /*
+ * FIXME [[rowheight]]: coded to work only with default row
+ * heights - will not work with variable row heights
+ */
+ setRowPosition(tr, 0, i * getDefaultRowHeight());
+ }
+
+ return addedRows;
+ } else {
+ return new ArrayList<TableRowElement>();
+ }
+ }
+
+ private int getMaxEscalatorRowCapacity() {
+ /*
+ * FIXME [[rowheight]]: coded to work only with default row heights
+ * - will not work with variable row heights
+ */
+ final int maxEscalatorRowCapacity = (int) Math
+ .ceil(calculateHeight() / getDefaultRowHeight()) + 1;
+
+ /*
+ * maxEscalatorRowCapacity can become negative if the headers and
+ * footers start to overlap. This is a crazy situation, but Vaadin
+ * blinks the components a lot, so it's feasible.
+ */
+ return Math.max(0, maxEscalatorRowCapacity);
+ }
+
+ @Override
+ protected void paintRemoveRows(final int index, final int numberOfRows) {
+ if (numberOfRows == 0) {
+ return;
+ }
+
+ final Range viewportRange = Range.withLength(
+ getLogicalRowIndex(visualRowOrder.getFirst()),
+ visualRowOrder.size());
+
+ final Range removedRowsRange = Range
+ .withLength(index, numberOfRows);
+
+ final Range[] partitions = removedRowsRange
+ .partitionWith(viewportRange);
+ final Range removedAbove = partitions[0];
+ final Range removedLogicalInside = partitions[1];
+ final Range removedVisualInside = convertToVisual(removedLogicalInside);
+
+ /*
+ * TODO: extract the following if-block to a separate method. I'll
+ * leave this be inlined for now, to make linediff-based code
+ * reviewing easier. Probably will be moved in the following patch
+ * set.
+ */
+
+ /*
+ * Adjust scroll position in one of two scenarios:
+ *
+ * 1) Rows were removed above. Then we just need to adjust the
+ * scrollbar by the height of the removed rows.
+ *
+ * 2) There are no logical rows above, and at least the first (if
+ * not more) visual row is removed. Then we need to snap the scroll
+ * position to the first visible row (i.e. reset scroll position to
+ * absolute 0)
+ *
+ * The logic is optimized in such a way that the
+ * adjustScrollPosIgnoreEvents is called only once, to avoid extra
+ * reflows, and thus the code might seem a bit obscure.
+ */
+ final boolean firstVisualRowIsRemoved = !removedVisualInside
+ .isEmpty() && removedVisualInside.getStart() == 0;
+
+ if (!removedAbove.isEmpty() || firstVisualRowIsRemoved) {
+ /*
+ * FIXME [[rowheight]]: coded to work only with default row
+ * heights - will not work with variable row heights
+ */
+ final int yDelta = removedAbove.length()
+ * getDefaultRowHeight();
+ final int firstLogicalRowHeight = getDefaultRowHeight();
+ final boolean removalScrollsToShowFirstLogicalRow = verticalScrollbar
+ .getScrollPos() - yDelta < firstLogicalRowHeight;
+
+ if (removedVisualInside.isEmpty()
+ && (!removalScrollsToShowFirstLogicalRow || !firstVisualRowIsRemoved)) {
+ /*
+ * rows were removed from above the viewport, so all we need
+ * to do is to adjust the scroll position to account for the
+ * removed rows
+ */
+ adjustScrollPosIgnoreEvents(-yDelta);
+ } else if (removalScrollsToShowFirstLogicalRow) {
+ /*
+ * It seems like we've removed all rows from above, and also
+ * into the current viewport. This means we'll need to even
+ * out the scroll position to exactly 0 (i.e. adjust by the
+ * current negative scrolltop, presto!), so that it isn't
+ * aligned funnily
+ */
+ adjustScrollPosIgnoreEvents(-verticalScrollbar
+ .getScrollPos());
+ }
+ }
+
+ // ranges evaluated, let's do things.
+ if (!removedVisualInside.isEmpty()) {
+ int escalatorRowCount = bodyElem.getChildCount();
+
+ /*
+ * If we're left with less rows than the number of escalators,
+ * remove the unused ones.
+ */
+ final int escalatorRowsToRemove = escalatorRowCount
+ - getRowCount();
+ if (escalatorRowsToRemove > 0) {
+ for (int i = 0; i < escalatorRowsToRemove; i++) {
+ final TableRowElement tr = visualRowOrder
+ .remove(removedVisualInside.getStart());
+
+ paintRemoveRow(tr, index);
+ removeRowPosition(tr);
+ }
+ escalatorRowCount -= escalatorRowsToRemove;
+
+ /*
+ * Because we're removing escalator rows, we don't have
+ * anything to scroll by. Let's make sure the viewport is
+ * scrolled to top, to render any rows possibly left above.
+ */
+ body.setBodyScrollPosition(tBodyScrollLeft, 0);
+
+ /*
+ * We might have removed some rows from the middle, so let's
+ * make sure we're not left with any holes. Also remember:
+ * visualIndex == logicalIndex applies now.
+ */
+ final int dirtyRowsStart = removedLogicalInside.getStart();
+ for (int i = dirtyRowsStart; i < escalatorRowCount; i++) {
+ final TableRowElement tr = visualRowOrder.get(i);
+ /*
+ * FIXME [[rowheight]]: coded to work only with default
+ * row heights - will not work with variable row heights
+ */
+ setRowPosition(tr, 0, i * getDefaultRowHeight());
+ }
+
+ /*
+ * this is how many rows appeared into the viewport from
+ * below
+ */
+ final int rowsToUpdateDataOn = numberOfRows
+ - escalatorRowsToRemove;
+ final int start = Math.max(0, escalatorRowCount
+ - rowsToUpdateDataOn);
+ final int end = escalatorRowCount;
+ for (int i = start; i < end; i++) {
+ final TableRowElement tr = visualRowOrder.get(i);
+ refreshRow(tr, i);
+ }
+ }
+
+ else {
+ // No escalator rows need to be removed.
+
+ /*
+ * Two things (or a combination thereof) can happen:
+ *
+ * 1) We're scrolled to the bottom, the last rows are
+ * removed. SOLUTION: moveAndUpdateEscalatorRows the
+ * bottommost rows, and place them at the top to be
+ * refreshed.
+ *
+ * 2) We're scrolled somewhere in the middle, arbitrary rows
+ * are removed. SOLUTION: moveAndUpdateEscalatorRows the
+ * removed rows, and place them at the bottom to be
+ * refreshed.
+ *
+ * Since a combination can also happen, we need to handle
+ * this in a smart way, all while avoiding
+ * double-refreshing.
+ */
+
+ /*
+ * FIXME [[rowheight]]: coded to work only with default row
+ * heights - will not work with variable row heights
+ */
+ final int contentBottom = getRowCount()
+ * getDefaultRowHeight();
+ final int viewportBottom = (int) (tBodyScrollTop + calculateHeight());
+ if (viewportBottom <= contentBottom) {
+ /*
+ * We're in the middle of the row container, everything
+ * is added to the bottom
+ */
+ paintRemoveRowsAtMiddle(removedLogicalInside,
+ removedVisualInside, 0);
+ }
+
+ else if (contentBottom
+ + (numberOfRows * getDefaultRowHeight())
+ - viewportBottom < getDefaultRowHeight()) {
+ /*
+ * FIXME [[rowheight]]: above coded to work only with
+ * default row heights - will not work with variable row
+ * heights
+ */
+
+ /*
+ * We're at the end of the row container, everything is
+ * added to the top.
+ */
+ paintRemoveRowsAtBottom(removedLogicalInside,
+ removedVisualInside);
+ updateTopRowLogicalIndex(-removedLogicalInside.length());
+ }
+
+ else {
+ /*
+ * We're in a combination, where we need to both scroll
+ * up AND show new rows at the bottom.
+ *
+ * Example: Scrolled down to show the second to last
+ * row. Remove two. Viewport scrolls up, revealing the
+ * row above row. The last element collapses up and into
+ * view.
+ *
+ * Reminder: this use case handles only the case when
+ * there are enough escalator rows to still render a
+ * full view. I.e. all escalator rows will _always_ be
+ * populated
+ */
+ /*-
+ * 1 1 |1| <- newly rendered
+ * |2| |2| |2|
+ * |3| ==> |*| ==> |5| <- newly rendered
+ * |4| |*|
+ * 5 5
+ *
+ * 1 1 |1| <- newly rendered
+ * |2| |*| |4|
+ * |3| ==> |*| ==> |5| <- newly rendered
+ * |4| |4|
+ * 5 5
+ */
+
+ /*
+ * STEP 1:
+ *
+ * reorganize deprecated escalator rows to bottom, but
+ * don't re-render anything yet
+ */
+ /*-
+ * 1 1 1
+ * |2| |*| |4|
+ * |3| ==> |*| ==> |*|
+ * |4| |4| |*|
+ * 5 5 5
+ */
+ double newTop = getRowTop(visualRowOrder
+ .get(removedVisualInside.getStart()));
+ for (int i = 0; i < removedVisualInside.length(); i++) {
+ final TableRowElement tr = visualRowOrder
+ .remove(removedVisualInside.getStart());
+ visualRowOrder.addLast(tr);
+ }
+
+ for (int i = removedVisualInside.getStart(); i < escalatorRowCount; i++) {
+ final TableRowElement tr = visualRowOrder.get(i);
+ setRowPosition(tr, 0, (int) newTop);
+
+ /*
+ * FIXME [[rowheight]]: coded to work only with
+ * default row heights - will not work with variable
+ * row heights
+ */
+ newTop += getDefaultRowHeight();
+ }
+
+ /*
+ * STEP 2:
+ *
+ * manually scroll
+ */
+ /*-
+ * 1 |1| <-- newly rendered (by scrolling)
+ * |4| |4|
+ * |*| ==> |*|
+ * |*|
+ * 5 5
+ */
+ final double newScrollTop = contentBottom
+ - calculateHeight();
+ setScrollTop(newScrollTop);
+ /*
+ * Manually call the scroll handler, so we get immediate
+ * effects in the escalator.
+ */
+ scroller.onScroll();
+ internalScrollEventCalls++;
+
+ /*
+ * Move the bottommost (n+1:th) escalator row to top,
+ * because scrolling up doesn't handle that for us
+ * automatically
+ */
+ moveAndUpdateEscalatorRows(
+ Range.withOnly(escalatorRowCount - 1),
+ 0,
+ getLogicalRowIndex(visualRowOrder.getFirst()) - 1);
+ updateTopRowLogicalIndex(-1);
+
+ /*
+ * STEP 3:
+ *
+ * update remaining escalator rows
+ */
+ /*-
+ * |1| |1|
+ * |4| ==> |4|
+ * |*| |5| <-- newly rendered
+ *
+ * 5
+ */
+
+ /*
+ * FIXME [[rowheight]]: coded to work only with default
+ * row heights - will not work with variable row heights
+ */
+ final int rowsScrolled = (int) (Math
+ .ceil((viewportBottom - (double) contentBottom)
+ / getDefaultRowHeight()));
+ final int start = escalatorRowCount
+ - (removedVisualInside.length() - rowsScrolled);
+ final Range visualRefreshRange = Range.between(start,
+ escalatorRowCount);
+ final int logicalTargetIndex = getLogicalRowIndex(visualRowOrder
+ .getFirst()) + start;
+ // in-place move simply re-renders the rows.
+ moveAndUpdateEscalatorRows(visualRefreshRange, start,
+ logicalTargetIndex);
+ }
+ }
+
+ fireRowVisibilityChangeEvent();
+ sortDomElements();
+ }
+
+ updateTopRowLogicalIndex(-removedAbove.length());
+
+ /*
+ * this needs to be done after the escalator has been shrunk down,
+ * or it won't work correctly (due to setScrollTop invocation)
+ */
+ scroller.recalculateScrollbarsForVirtualViewport();
+ }
+
+ private void paintRemoveRowsAtMiddle(final Range removedLogicalInside,
+ final Range removedVisualInside, final int logicalOffset) {
+ /*-
+ * : : :
+ * |2| |2| |2|
+ * |3| ==> |*| ==> |4|
+ * |4| |4| |6| <- newly rendered
+ * : : :
+ */
+
+ final int escalatorRowCount = visualRowOrder.size();
+
+ final int logicalTargetIndex = getLogicalRowIndex(visualRowOrder
+ .getLast())
+ - (removedVisualInside.length() - 1)
+ + logicalOffset;
+ moveAndUpdateEscalatorRows(removedVisualInside, escalatorRowCount,
+ logicalTargetIndex);
+
+ // move the surrounding rows to their correct places.
+ final ListIterator<TableRowElement> iterator = visualRowOrder
+ .listIterator(removedVisualInside.getStart());
+
+ /*
+ * FIXME [[rowheight]]: coded to work only with default row heights
+ * - will not work with variable row heights
+ */
+ int rowTop = (removedLogicalInside.getStart() + logicalOffset)
+ * getDefaultRowHeight();
+ for (int i = removedVisualInside.getStart(); i < escalatorRowCount
+ - removedVisualInside.length(); i++) {
+ final TableRowElement tr = iterator.next();
+ setRowPosition(tr, 0, rowTop);
+ /*
+ * FIXME [[rowheight]]: coded to work only with default row
+ * heights - will not work with variable row heights
+ */
+ rowTop += getDefaultRowHeight();
+ }
+ }
+
+ private void paintRemoveRowsAtBottom(final Range removedLogicalInside,
+ final Range removedVisualInside) {
+ /*-
+ * :
+ * : : |4| <- newly rendered
+ * |5| |5| |5|
+ * |6| ==> |*| ==> |7|
+ * |7| |7|
+ */
+
+ final int logicalTargetIndex = getLogicalRowIndex(visualRowOrder
+ .getFirst()) - removedVisualInside.length();
+ moveAndUpdateEscalatorRows(removedVisualInside, 0,
+ logicalTargetIndex);
+
+ // move the surrounding rows to their correct places.
+ final ListIterator<TableRowElement> iterator = visualRowOrder
+ .listIterator(removedVisualInside.getEnd());
+ /*
+ * FIXME [[rowheight]]: coded to work only with default row heights
+ * - will not work with variable row heights
+ */
+ int rowTop = removedLogicalInside.getStart()
+ * getDefaultRowHeight();
+ while (iterator.hasNext()) {
+ final TableRowElement tr = iterator.next();
+ setRowPosition(tr, 0, rowTop);
+ /*
+ * FIXME [[rowheight]]: coded to work only with default row
+ * heights - will not work with variable row heights
+ */
+ rowTop += getDefaultRowHeight();
+ }
+ }
+
+ private int getLogicalRowIndex(final Element tr) {
+ assert tr.getParentNode() == root : "The given element isn't a row element in the body";
+ int internalIndex = visualRowOrder.indexOf(tr);
+ return getTopRowLogicalIndex() + internalIndex;
+ }
+
+ @Override
+ protected void recalculateSectionHeight() {
+ // NOOP for body, since it doesn't make any sense.
+ }
+
+ /**
+ * Adjusts the row index and number to be relevant for the current
+ * virtual viewport.
+ * <p>
+ * It converts a logical range of rows index to the matching visual
+ * range, truncating the resulting range with the viewport.
+ * <p>
+ * <ul>
+ * <li>Escalator contains logical rows 0..100
+ * <li>Current viewport showing logical rows 20..29
+ * <li>convertToVisual([20..29]) &rarr; [0..9]
+ * <li>convertToVisual([15..24]) &rarr; [0..4]
+ * <li>convertToVisual([25..29]) &rarr; [5..9]
+ * <li>convertToVisual([26..39]) &rarr; [6..9]
+ * <li>convertToVisual([0..5]) &rarr; [0..-1] <em>(empty)</em>
+ * <li>convertToVisual([35..1]) &rarr; [0..-1] <em>(empty)</em>
+ * <li>convertToVisual([0..100]) &rarr; [0..9]
+ * </ul>
+ *
+ * @return a logical range converted to a visual range, truncated to the
+ * current viewport. The first visual row has the index 0.
+ */
+ private Range convertToVisual(final Range logicalRange) {
+ if (logicalRange.isEmpty()) {
+ return logicalRange;
+ } else if (visualRowOrder.isEmpty()) {
+ // empty range
+ return Range.withLength(0, 0);
+ }
+
+ /*
+ * TODO [[rowheight]]: these assumptions will be totally broken with
+ * variable row heights.
+ */
+ final int maxEscalatorRows = getMaxEscalatorRowCapacity();
+ final int currentTopRowIndex = getLogicalRowIndex(visualRowOrder
+ .getFirst());
+
+ final Range[] partitions = logicalRange.partitionWith(Range
+ .withLength(currentTopRowIndex, maxEscalatorRows));
+ final Range insideRange = partitions[1];
+ return insideRange.offsetBy(-currentTopRowIndex);
+ }
+
+ @Override
+ protected String getCellElementTagName() {
+ return "td";
+ }
+
+ /**
+ * Calculates the height of the {@code <tbody>} as it should be rendered
+ * in the DOM.
+ */
+ private double calculateHeight() {
+ final int tableHeight = tableWrapper.getOffsetHeight();
+ final double footerHeight = footer.heightOfSection;
+ final double headerHeight = header.heightOfSection;
+ return tableHeight - footerHeight - headerHeight;
+ }
+
+ @Override
+ public void refreshRows(final int index, final int numberOfRows) {
+ Profiler.enter("Escalator.BodyRowContainer.refreshRows");
+
+ final Range visualRange = convertToVisual(Range.withLength(index,
+ numberOfRows));
+
+ if (!visualRange.isEmpty()) {
+ final int firstLogicalRowIndex = getLogicalRowIndex(visualRowOrder
+ .getFirst());
+ for (int rowNumber = visualRange.getStart(); rowNumber < visualRange
+ .getEnd(); rowNumber++) {
+ refreshRow(visualRowOrder.get(rowNumber),
+ firstLogicalRowIndex + rowNumber);
+ }
+ }
+
+ Profiler.leave("Escalator.BodyRowContainer.refreshRows");
+ }
+
+ @Override
+ protected TableRowElement getTrByVisualIndex(final int index)
+ throws IndexOutOfBoundsException {
+ if (index >= 0 && index < visualRowOrder.size()) {
+ return visualRowOrder.get(index);
+ } else {
+ throw new IndexOutOfBoundsException("No such visual index: "
+ + index);
+ }
+ }
+
+ @Override
+ public TableRowElement getRowElement(int index) {
+ if (index < 0 || index >= getRowCount()) {
+ throw new IndexOutOfBoundsException("No such logical index: "
+ + index);
+ }
+ int visualIndex = index
+ - getLogicalRowIndex(visualRowOrder.getFirst());
+ if (visualIndex >= 0 && visualIndex < visualRowOrder.size()) {
+ return super.getRowElement(visualIndex);
+ } else {
+ throw new IllegalStateException("Row with logical index "
+ + index + " is currently not available in the DOM");
+ }
+ }
+
+ private void setBodyScrollPosition(final double scrollLeft,
+ final double scrollTop) {
+ tBodyScrollLeft = scrollLeft;
+ tBodyScrollTop = scrollTop;
+ position.set(bodyElem, -tBodyScrollLeft, -tBodyScrollTop);
+ }
+
+ /**
+ * Make sure that there is a correct amount of escalator rows: Add more
+ * if needed, or remove any superfluous ones.
+ * <p>
+ * This method should be called when e.g. the height of the Escalator
+ * changes.
+ * <p>
+ * <em>Note:</em> This method will make sure that the escalator rows are
+ * placed in the proper places. By default new rows are added below, but
+ * if the content is scrolled down, the rows are populated on top
+ * instead.
+ */
+ public void verifyEscalatorCount() {
+ /*
+ * This method indeed has a smell very similar to paintRemoveRows
+ * and paintInsertRows.
+ *
+ * Unfortunately, those the code can't trivially be shared, since
+ * there are some slight differences in the respective
+ * responsibilities. The "paint" methods fake the addition and
+ * removal of rows, and make sure to either push existing data out
+ * of view, or draw new data into view. Only in some special cases
+ * will the DOM element count change.
+ *
+ * This method, however, has the explicit responsibility to verify
+ * that when "something" happens, we still have the correct amount
+ * of escalator rows in the DOM, and if not, we make sure to modify
+ * that count. Only in some special cases do we need to take into
+ * account other things than simply modifying the DOM element count.
+ */
+
+ Profiler.enter("Escalator.BodyRowContainer.verifyEscalatorCount");
+
+ if (!isAttached()) {
+ return;
+ }
+
+ final int maxEscalatorRows = getMaxEscalatorRowCapacity();
+ final int neededEscalatorRows = Math.min(maxEscalatorRows,
+ body.getRowCount());
+ final int neededEscalatorRowsDiff = neededEscalatorRows
+ - visualRowOrder.size();
+
+ if (neededEscalatorRowsDiff > 0) {
+ // needs more
+
+ /*
+ * This is a workaround for the issue where we might be scrolled
+ * to the bottom, and the widget expands beyond the content
+ * range
+ */
+
+ final int index = visualRowOrder.size();
+ final int nextLastLogicalIndex;
+ if (!visualRowOrder.isEmpty()) {
+ nextLastLogicalIndex = getLogicalRowIndex(visualRowOrder
+ .getLast()) + 1;
+ } else {
+ nextLastLogicalIndex = 0;
+ }
+
+ final boolean contentWillFit = nextLastLogicalIndex < getRowCount()
+ - neededEscalatorRowsDiff;
+ if (contentWillFit) {
+ final List<TableRowElement> addedRows = fillAndPopulateEscalatorRowsIfNeeded(
+ index, neededEscalatorRowsDiff);
+
+ /*
+ * Since fillAndPopulateEscalatorRowsIfNeeded operates on
+ * the assumption that index == visual index == logical
+ * index, we thank for the added escalator rows, but since
+ * they're painted in the wrong CSS position, we need to
+ * move them to their actual locations.
+ *
+ * Note: this is the second (see body.paintInsertRows)
+ * occasion where fillAndPopulateEscalatorRowsIfNeeded would
+ * behave "more correctly" if it only would add escalator
+ * rows to the DOM and appropriate bookkeping, and not
+ * actually populate them :/
+ */
+ moveAndUpdateEscalatorRows(
+ Range.withLength(index, addedRows.size()), index,
+ nextLastLogicalIndex);
+ } else {
+ /*
+ * TODO [[optimize]]
+ *
+ * We're scrolled so far down that all rows can't be simply
+ * appended at the end, since we might start displaying
+ * escalator rows that don't exist. To avoid the mess that
+ * is body.paintRemoveRows, this is a dirty hack that dumbs
+ * the problem down to a more basic and already-solved
+ * problem:
+ *
+ * 1) scroll all the way up 2) add the missing escalator
+ * rows 3) scroll back to the original position.
+ *
+ * Letting the browser scroll back to our original position
+ * will automatically solve any possible overflow problems,
+ * since the browser will not allow us to scroll beyond the
+ * actual content.
+ */
+
+ final double oldScrollTop = getScrollTop();
+ setScrollTop(0);
+ scroller.onScroll();
+ fillAndPopulateEscalatorRowsIfNeeded(index,
+ neededEscalatorRowsDiff);
+ setScrollTop(oldScrollTop);
+ scroller.onScroll();
+ internalScrollEventCalls++;
+ }
+ }
+
+ else if (neededEscalatorRowsDiff < 0) {
+ // needs less
+
+ final ListIterator<TableRowElement> iter = visualRowOrder
+ .listIterator(visualRowOrder.size());
+ for (int i = 0; i < -neededEscalatorRowsDiff; i++) {
+ final Element last = iter.previous();
+ last.removeFromParent();
+ iter.remove();
+ }
+
+ /*
+ * If we were scrolled to the bottom so that we didn't have an
+ * extra escalator row at the bottom, we'll probably end up with
+ * blank space at the bottom of the escalator, and one extra row
+ * above the header.
+ *
+ * Experimentation idea #1: calculate "scrollbottom" vs content
+ * bottom and remove one row from top, rest from bottom. This
+ * FAILED, since setHeight has already happened, thus we never
+ * will detect ourselves having been scrolled all the way to the
+ * bottom.
+ */
+
+ if (!visualRowOrder.isEmpty()) {
+ final int firstRowTop = getRowTop(visualRowOrder.getFirst());
+ /*
+ * FIXME [[rowheight]]: coded to work only with default row
+ * heights - will not work with variable row heights
+ */
+ final double firstRowMinTop = tBodyScrollTop
+ - getDefaultRowHeight();
+ if (firstRowTop < firstRowMinTop) {
+ final int newLogicalIndex = getLogicalRowIndex(visualRowOrder
+ .getLast()) + 1;
+ moveAndUpdateEscalatorRows(Range.withOnly(0),
+ visualRowOrder.size(), newLogicalIndex);
+ }
+ }
+ }
+
+ if (neededEscalatorRowsDiff != 0) {
+ fireRowVisibilityChangeEvent();
+ }
+
+ Profiler.leave("Escalator.BodyRowContainer.verifyEscalatorCount");
+ }
+
+ @Override
+ protected void reapplyDefaultRowHeights() {
+ if (visualRowOrder.isEmpty()) {
+ return;
+ }
+
+ /*
+ * As an intermediate step between hard-coded row heights to crazily
+ * varying row heights, Escalator will support the modification of
+ * the default row height (which is applied to all rows).
+ *
+ * This allows us to do some assumptions and simplifications for
+ * now. This code is intended to be quite short-lived, but gives
+ * insight into what needs to be done when row heights change in the
+ * body, in a general sense.
+ *
+ * TODO [[rowheight]] remove this comment once row heights may
+ * genuinely vary.
+ */
+
+ Profiler.enter("Escalator.BodyRowContainer.reapplyDefaultRowHeights");
+
+ /* step 1: resize and reposition rows */
+ for (int i = 0; i < visualRowOrder.size(); i++) {
+ TableRowElement tr = visualRowOrder.get(i);
+ reapplyRowHeight(tr, getDefaultRowHeight());
+
+ final int logicalIndex = getTopRowLogicalIndex() + i;
+ setRowPosition(tr, 0, logicalIndex * getDefaultRowHeight());
+ }
+
+ /*
+ * step 2: move scrollbar so that it corresponds to its previous
+ * place
+ */
+
+ /*
+ * This ratio needs to be calculated with the scrollsize (not max
+ * scroll position) in order to align the top row with the new
+ * scroll position.
+ */
+ double scrollRatio = verticalScrollbar.getScrollPos()
+ / verticalScrollbar.getScrollSize();
+ scroller.recalculateScrollbarsForVirtualViewport();
+ internalScrollEventCalls++;
+ verticalScrollbar.setScrollPos((int) (getDefaultRowHeight()
+ * getRowCount() * scrollRatio));
+ setBodyScrollPosition(horizontalScrollbar.getScrollPos(),
+ verticalScrollbar.getScrollPos());
+ scroller.onScroll();
+
+ /* step 3: make sure we have the correct amount of escalator rows. */
+ verifyEscalatorCount();
+
+ /*
+ * TODO [[rowheight]] This simply doesn't work with variable rows
+ * heights.
+ */
+ setTopRowLogicalIndex(getRowTop(visualRowOrder.getFirst())
+ / getDefaultRowHeight());
+
+ Profiler.leave("Escalator.BodyRowContainer.reapplyDefaultRowHeights");
+ }
+
+ /**
+ * Sorts the rows in the DOM to correspond to the visual order.
+ *
+ * @see #visualRowOrder
+ */
+ private void sortDomElements() {
+ final String profilingName = "Escalator.BodyRowContainer.sortDomElements";
+ Profiler.enter(profilingName);
+
+ /*
+ * Focus is lost from an element if that DOM element is (or any of
+ * its parents are) removed from the document. Therefore, we sort
+ * everything around that row instead.
+ */
+ final TableRowElement activeRow = getEscalatorRowWithFocus();
+
+ if (activeRow != null) {
+ assert activeRow.getParentElement() == root : "Trying to sort around a row that doesn't exist in body";
+ assert visualRowOrder.contains(activeRow) : "Trying to sort around a row that doesn't exist in visualRowOrder.";
+ }
+
+ /*
+ * Two cases handled simultaneously:
+ *
+ * 1) No focus on rows. We iterate visualRowOrder backwards, and
+ * take the respective element in the DOM, and place it as the first
+ * child in the body element. Then we take the next-to-last from
+ * visualRowOrder, and put that first, pushing the previous row as
+ * the second child. And so on...
+ *
+ * 2) Focus on some row within Escalator body. Again, we iterate
+ * visualRowOrder backwards. This time, we use the focused row as a
+ * pivot: Instead of placing rows from the bottom of visualRowOrder
+ * and placing it first, we place it underneath the focused row.
+ * Once we hit the focused row, we don't move it (to not reset
+ * focus) but change sorting mode. After that, we place all rows as
+ * the first child.
+ */
+
+ /*
+ * If we have a focused row, start in the mode where we put
+ * everything underneath that row. Otherwise, all rows are placed as
+ * first child.
+ */
+ boolean insertFirst = (activeRow == null);
+
+ final ListIterator<TableRowElement> i = visualRowOrder
+ .listIterator(visualRowOrder.size());
+ while (i.hasPrevious()) {
+ TableRowElement tr = i.previous();
+
+ if (tr == activeRow) {
+ insertFirst = true;
+ } else if (insertFirst) {
+ root.insertFirst(tr);
+ } else {
+ root.insertAfter(tr, activeRow);
+ }
+ }
+
+ Profiler.leave(profilingName);
+ }
+
+ /**
+ * Get the escalator row that has focus.
+ *
+ * @return The escalator row that contains a focused DOM element, or
+ * <code>null</code> if focus is outside of a body row.
+ */
+ private TableRowElement getEscalatorRowWithFocus() {
+ TableRowElement activeRow = null;
+
+ final Element activeElement = Util.getFocusedElement();
+
+ if (root.isOrHasChild(activeElement)) {
+ Element e = activeElement;
+
+ while (e != null && e != root) {
+ /*
+ * You never know if there's several tables embedded in a
+ * cell... We'll take the deepest one.
+ */
+ if (TableRowElement.is(e)) {
+ activeRow = TableRowElement.as(e);
+ }
+ e = e.getParentElement();
+ }
+ }
+
+ return activeRow;
+ }
+
+ @Override
+ public Cell getCell(Element element) {
+ Cell cell = super.getCell(element);
+ if (cell == null) {
+ return null;
+ }
+
+ // Convert DOM coordinates to logical coordinates for rows
+ Element rowElement = cell.getElement().getParentElement();
+ return new Cell(getLogicalRowIndex(rowElement), cell.getColumn(),
+ cell.getElement());
+ }
+ }
+
+ private class ColumnConfigurationImpl implements ColumnConfiguration {
+ public class Column {
+ private static final int DEFAULT_COLUMN_WIDTH_PX = 100;
+
+ private int definedWidth = -1;
+ private int calculatedWidth = DEFAULT_COLUMN_WIDTH_PX;
+
+ public void setWidth(int px) {
+ definedWidth = px;
+ calculatedWidth = (px >= 0) ? px : DEFAULT_COLUMN_WIDTH_PX;
+ }
+
+ public int getDefinedWidth() {
+ return definedWidth;
+ }
+
+ public int getCalculatedWidth() {
+ return calculatedWidth;
+ }
+ }
+
+ private final List<Column> columns = new ArrayList<Column>();
+ private int frozenColumns = 0;
+
+ /**
+ * A cached array of all the calculated column widths.
+ *
+ * @see #getCalculatedColumnWidths()
+ */
+ private int[] widthsArray = null;
+
+ /**
+ * {@inheritDoc}
+ * <p>
+ * <em>Implementation detail:</em> This method does no DOM modifications
+ * (i.e. is very cheap to call) if there are no rows in the DOM when
+ * this method is called.
+ *
+ * @see #hasSomethingInDom()
+ */
+ @Override
+ public void removeColumns(final int index, final int numberOfColumns) {
+ assertArgumentsAreValidAndWithinRange(index, numberOfColumns);
+
+ flyweightRow.removeCells(index, numberOfColumns);
+
+ // Cope with removing frozen columns
+ if (index < frozenColumns) {
+ if (index + numberOfColumns < frozenColumns) {
+ /*
+ * Last removed column was frozen, meaning that all removed
+ * columns were frozen. Just decrement the number of frozen
+ * columns accordingly.
+ */
+ frozenColumns -= numberOfColumns;
+ } else {
+ /*
+ * If last removed column was not frozen, we have removed
+ * columns beyond the frozen range, so all remaining frozen
+ * columns are to the left of the removed columns.
+ */
+ frozenColumns = index;
+ }
+ }
+
+ List<Column> removedColumns = new ArrayList<Column>();
+ for (int i = 0; i < numberOfColumns; i++) {
+ removedColumns.add(columns.remove(index));
+ }
+
+ if (hasSomethingInDom()) {
+ for (final AbstractRowContainer rowContainer : rowContainers) {
+ rowContainer.paintRemoveColumns(index, numberOfColumns,
+ removedColumns);
+ }
+ }
+ }
+
+ /**
+ * Calculate the width of a row, as the sum of columns' widths.
+ *
+ * @return the width of a row, in pixels
+ */
+ public int calculateRowWidth() {
+ return getCalculatedColumnsWidth(Range.between(0, getColumnCount()));
+ }
+
+ private void assertArgumentsAreValidAndWithinRange(final int index,
+ final int numberOfColumns) {
+ if (numberOfColumns < 1) {
+ throw new IllegalArgumentException(
+ "Number of columns can't be less than 1 (was "
+ + numberOfColumns + ")");
+ }
+
+ if (index < 0 || index + numberOfColumns > getColumnCount()) {
+ throw new IndexOutOfBoundsException("The given "
+ + "column range (" + index + ".."
+ + (index + numberOfColumns)
+ + ") was outside of the current "
+ + "number of columns (" + getColumnCount() + ")");
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ * <p>
+ * <em>Implementation detail:</em> This method does no DOM modifications
+ * (i.e. is very cheap to call) if there is no data for rows when this
+ * method is called.
+ *
+ * @see #hasColumnAndRowData()
+ */
+ @Override
+ public void insertColumns(final int index, final int numberOfColumns) {
+ if (index < 0 || index > getColumnCount()) {
+ throw new IndexOutOfBoundsException("The given index(" + index
+ + ") was outside of the current number of columns (0.."
+ + getColumnCount() + ")");
+ }
+
+ if (numberOfColumns < 1) {
+ throw new IllegalArgumentException(
+ "Number of columns must be 1 or greater (was "
+ + numberOfColumns);
+ }
+
+ flyweightRow.addCells(index, numberOfColumns);
+
+ for (int i = 0; i < numberOfColumns; i++) {
+ columns.add(index, new Column());
+ }
+
+ // Either all or none of the new columns are frozen
+ boolean frozen = index < frozenColumns;
+ if (frozen) {
+ frozenColumns += numberOfColumns;
+ }
+
+ if (hasColumnAndRowData()) {
+ for (final AbstractRowContainer rowContainer : rowContainers) {
+ rowContainer.paintInsertColumns(index, numberOfColumns,
+ frozen);
+ }
+ }
+ }
+
+ @Override
+ public int getColumnCount() {
+ return columns.size();
+ }
+
+ @Override
+ public void setFrozenColumnCount(int count)
+ throws IllegalArgumentException {
+ if (count < 0 || count > getColumnCount()) {
+ throw new IllegalArgumentException(
+ "count must be between 0 and the current number of columns ("
+ + columns + ")");
+ }
+ int oldCount = frozenColumns;
+ if (count == oldCount) {
+ return;
+ }
+
+ frozenColumns = count;
+
+ if (hasSomethingInDom()) {
+ // Are we freezing or unfreezing?
+ boolean frozen = count > oldCount;
+
+ int firstAffectedCol;
+ int firstUnaffectedCol;
+
+ if (frozen) {
+ firstAffectedCol = oldCount;
+ firstUnaffectedCol = count;
+ } else {
+ firstAffectedCol = count;
+ firstUnaffectedCol = oldCount;
+ }
+
+ for (int col = firstAffectedCol; col < firstUnaffectedCol; col++) {
+ header.setColumnFrozen(col, frozen);
+ body.setColumnFrozen(col, frozen);
+ footer.setColumnFrozen(col, frozen);
+ }
+ }
+
+ scroller.recalculateScrollbarsForVirtualViewport();
+ }
+
+ @Override
+ public int getFrozenColumnCount() {
+ return frozenColumns;
+ }
+
+ @Override
+ public void setColumnWidth(int index, int px)
+ throws IllegalArgumentException {
+ checkValidColumnIndex(index);
+
+ columns.get(index).setWidth(px);
+ widthsArray = null;
+
+ /*
+ * TODO [[optimize]]: only modify the elements that are actually
+ * modified.
+ */
+ header.reapplyColumnWidths();
+ body.reapplyColumnWidths();
+ footer.reapplyColumnWidths();
+ recalculateElementSizes();
+ }
+
+ private void checkValidColumnIndex(int index)
+ throws IllegalArgumentException {
+ if (!Range.withLength(0, getColumnCount()).contains(index)) {
+ throw new IllegalArgumentException("The given column index ("
+ + index + ") does not exist");
+ }
+ }
+
+ @Override
+ public int getColumnWidth(int index) throws IllegalArgumentException {
+ checkValidColumnIndex(index);
+ return columns.get(index).getDefinedWidth();
+ }
+
+ @Override
+ public int getColumnWidthActual(int index) {
+ return columns.get(index).getCalculatedWidth();
+ }
+
+ /**
+ * Calculates the width of the columns in a given range.
+ *
+ * @param columns
+ * the columns to calculate
+ * @return the total width of the columns in the given
+ * <code>columns</code>
+ */
+ int getCalculatedColumnsWidth(@SuppressWarnings("hiding")
+ final Range columns) {
+ /*
+ * This is an assert instead of an exception, since this is an
+ * internal method.
+ */
+ assert columns.isSubsetOf(Range.between(0, getColumnCount())) : "Range "
+ + "was outside of current column range (i.e.: "
+ + Range.between(0, getColumnCount())
+ + ", but was given :"
+ + columns;
+
+ int sum = 0;
+ for (int i = columns.getStart(); i < columns.getEnd(); i++) {
+ sum += getColumnWidthActual(i);
+ }
+ return sum;
+ }
+
+ void setCalculatedColumnWidth(int index, int width) {
+ columns.get(index).calculatedWidth = width;
+ widthsArray = null;
+ }
+
+ int[] getCalculatedColumnWidths() {
+ if (widthsArray == null || widthsArray.length != getColumnCount()) {
+ widthsArray = new int[getColumnCount()];
+ for (int i = 0; i < columns.size(); i++) {
+ widthsArray[i] = columns.get(i).getCalculatedWidth();
+ }
+ }
+ return widthsArray;
+ }
+ }
+
+ // abs(atan(y/x))*(180/PI) = n deg, x = 1, solve y
+ /**
+ * The solution to
+ * <code>|tan<sup>-1</sup>(<i>x</i>)|&times;(180/&pi;)&nbsp;=&nbsp;30</code>
+ * .
+ * <p>
+ * This constant is placed in the Escalator class, instead of an inner
+ * class, since even mathematical expressions aren't allowed in non-static
+ * inner classes for constants.
+ */
+ private static final double RATIO_OF_30_DEGREES = 1 / Math.sqrt(3);
+ /**
+ * The solution to
+ * <code>|tan<sup>-1</sup>(<i>x</i>)|&times;(180/&pi;)&nbsp;=&nbsp;40</code>
+ * .
+ * <p>
+ * This constant is placed in the Escalator class, instead of an inner
+ * class, since even mathematical expressions aren't allowed in non-static
+ * inner classes for constants.
+ */
+ private static final double RATIO_OF_40_DEGREES = Math.tan(2 * Math.PI / 9);
+
+ private static final String DEFAULT_WIDTH = "500.0px";
+ private static final String DEFAULT_HEIGHT = "400.0px";
+
+ private FlyweightRow flyweightRow = new FlyweightRow();
+
+ /** The {@code <thead/>} tag. */
+ private final TableSectionElement headElem = TableSectionElement.as(DOM
+ .createTHead());
+ /** The {@code <tbody/>} tag. */
+ private final TableSectionElement bodyElem = TableSectionElement.as(DOM
+ .createTBody());
+ /** The {@code <tfoot/>} tag. */
+ private final TableSectionElement footElem = TableSectionElement.as(DOM
+ .createTFoot());
+
+ /**
+ * TODO: investigate whether this field is now unnecessary, as
+ * {@link ScrollbarBundle} now caches its values.
+ *
+ * @deprecated maybe...
+ */
+ @Deprecated
+ private double tBodyScrollTop = 0;
+
+ /**
+ * TODO: investigate whether this field is now unnecessary, as
+ * {@link ScrollbarBundle} now caches its values.
+ *
+ * @deprecated maybe...
+ */
+ @Deprecated
+ private double tBodyScrollLeft = 0;
+
+ private final VerticalScrollbarBundle verticalScrollbar = new VerticalScrollbarBundle();
+ private final HorizontalScrollbarBundle horizontalScrollbar = new HorizontalScrollbarBundle();
+
+ private final HeaderRowContainer header = new HeaderRowContainer(headElem);
+ private final BodyRowContainer body = new BodyRowContainer(bodyElem);
+ private final FooterRowContainer footer = new FooterRowContainer(footElem);
+
+ private final Scroller scroller = new Scroller();
+
+ private final AbstractRowContainer[] rowContainers = new AbstractRowContainer[] {
+ header, body, footer };
+
+ private final ColumnConfigurationImpl columnConfiguration = new ColumnConfigurationImpl();
+ private final Element tableWrapper;
+
+ private PositionFunction position;
+
+ private int internalScrollEventCalls = 0;
+
+ /** The cached width of the escalator, in pixels. */
+ private double widthOfEscalator;
+ /** The cached height of the escalator, in pixels. */
+ private double heightOfEscalator;
+
+ /** The height of Escalator in terms of body rows. */
+ private double heightByRows = GridState.DEFAULT_HEIGHT_BY_ROWS;
+
+ /** The height of Escalator, as defined by {@link #setHeight(String)} */
+ private String heightByCss = "";
+
+ private HeightMode heightMode = HeightMode.CSS;
+
+ private static native double getPreciseWidth(Element element)
+ /*-{
+ if (element.getBoundingClientRect) {
+ var rect = element.getBoundingClientRect();
+ return rect.right - rect.left;
+ } else {
+ return element.offsetWidth;
+ }
+ }-*/;
+
+ private static native double getPreciseHeight(Element element)
+ /*-{
+ if (element.getBoundingClientRect) {
+ var rect = element.getBoundingClientRect();
+ return rect.bottom - rect.top;
+ } else {
+ return element.offsetHeight;
+ }
+ }-*/;
+
+ /**
+ * Creates a new Escalator widget instance.
+ */
+ public Escalator() {
+
+ detectAndApplyPositionFunction();
+ getLogger().info(
+ "Using " + position.getClass().getSimpleName()
+ + " for position");
+
+ final Element root = DOM.createDiv();
+ setElement(root);
+
+ root.appendChild(verticalScrollbar.getElement());
+ verticalScrollbar.getElement().setTabIndex(-1);
+ verticalScrollbar.setScrollbarThickness(Util.getNativeScrollbarSize());
+
+ root.appendChild(horizontalScrollbar.getElement());
+ horizontalScrollbar.getElement().setTabIndex(-1);
+ horizontalScrollbar
+ .setScrollbarThickness(Util.getNativeScrollbarSize());
+ horizontalScrollbar
+ .addVisibilityHandler(new ScrollbarBundle.VisibilityHandler() {
+ @Override
+ public void visibilityChanged(
+ ScrollbarBundle.VisibilityChangeEvent event) {
+ /*
+ * We either lost or gained a scrollbar. In any case, we
+ * need to change the height, if it's defined by rows.
+ */
+ applyHeightByRows();
+ }
+ });
+
+ tableWrapper = DOM.createDiv();
+
+ root.appendChild(tableWrapper);
+
+ final Element table = DOM.createTable();
+ tableWrapper.appendChild(table);
+
+ table.appendChild(headElem);
+ table.appendChild(bodyElem);
+ table.appendChild(footElem);
+
+ setStylePrimaryName("v-escalator");
+
+ // init default dimensions
+ setHeight(null);
+ setWidth(null);
+ }
+
+ @Override
+ protected void onLoad() {
+ super.onLoad();
+
+ header.autodetectRowHeight();
+ body.autodetectRowHeight();
+ footer.autodetectRowHeight();
+
+ header.paintInsertRows(0, header.getRowCount());
+ footer.paintInsertRows(0, footer.getRowCount());
+ recalculateElementSizes();
+ /*
+ * Note: There's no need to explicitly insert rows into the body.
+ *
+ * recalculateElementSizes will recalculate the height of the body. This
+ * has the side-effect that as the body's size grows bigger (i.e. from 0
+ * to its actual height), more escalator rows are populated. Those
+ * escalator rows are then immediately rendered. This, in effect, is the
+ * same thing as inserting those rows.
+ *
+ * In fact, having an extra paintInsertRows here would lead to duplicate
+ * rows.
+ */
+
+ scroller.attachScrollListener(verticalScrollbar.getElement());
+ scroller.attachScrollListener(horizontalScrollbar.getElement());
+ scroller.attachMousewheelListener(getElement());
+ scroller.attachTouchListeners(getElement());
+ }
+
+ @Override
+ protected void onUnload() {
+
+ scroller.detachScrollListener(verticalScrollbar.getElement());
+ scroller.detachScrollListener(horizontalScrollbar.getElement());
+ scroller.detachMousewheelListener(getElement());
+ scroller.detachTouchListeners(getElement());
+
+ header.paintRemoveRows(0, header.getRowCount());
+ footer.paintRemoveRows(0, footer.getRowCount());
+ body.paintRemoveRows(0, body.getRowCount());
+
+ super.onUnload();
+ }
+
+ private void detectAndApplyPositionFunction() {
+ /*
+ * firefox has a bug in its translate operation, showing white space
+ * when adjusting the scrollbar in BodyRowContainer.paintInsertRows
+ */
+ if (Window.Navigator.getUserAgent().contains("Firefox")) {
+ position = new AbsolutePosition();
+ return;
+ }
+
+ final Style docStyle = Document.get().getBody().getStyle();
+ if (hasProperty(docStyle, "transform")) {
+ if (hasProperty(docStyle, "transformStyle")) {
+ position = new Translate3DPosition();
+ } else {
+ position = new TranslatePosition();
+ }
+ } else if (hasProperty(docStyle, "webkitTransform")) {
+ position = new WebkitTranslate3DPosition();
+ } else {
+ position = new AbsolutePosition();
+ }
+ }
+
+ private Logger getLogger() {
+ return Logger.getLogger(getClass().getName());
+ }
+
+ private static native boolean hasProperty(Style style, String name)
+ /*-{
+ return style[name] !== undefined;
+ }-*/;
+
+ /**
+ * Check whether there are both columns and any row data (for either
+ * headers, body or footer).
+ *
+ * @return <code>true</code> iff header, body or footer has rows && there
+ * are columns
+ */
+ private boolean hasColumnAndRowData() {
+ return (header.getRowCount() > 0 || body.getRowCount() > 0 || footer
+ .getRowCount() > 0) && columnConfiguration.getColumnCount() > 0;
+ }
+
+ /**
+ * Check whether there are any cells in the DOM.
+ *
+ * @return <code>true</code> iff header, body or footer has any child
+ * elements
+ */
+ private boolean hasSomethingInDom() {
+ return headElem.hasChildNodes() || bodyElem.hasChildNodes()
+ || footElem.hasChildNodes();
+ }
+
+ /**
+ * Returns the row container for the header in this Escalator.
+ *
+ * @return the header. Never <code>null</code>
+ */
+ public RowContainer getHeader() {
+ return header;
+ }
+
+ /**
+ * Returns the row container for the body in this Escalator.
+ *
+ * @return the body. Never <code>null</code>
+ */
+ public RowContainer getBody() {
+ return body;
+ }
+
+ /**
+ * Returns the row container for the footer in this Escalator.
+ *
+ * @return the footer. Never <code>null</code>
+ */
+ public RowContainer getFooter() {
+ return footer;
+ }
+
+ /**
+ * Returns the configuration object for the columns in this Escalator.
+ *
+ * @return the configuration object for the columns in this Escalator. Never
+ * <code>null</code>
+ */
+ public ColumnConfiguration getColumnConfiguration() {
+ return columnConfiguration;
+ }
+
+ @Override
+ public void setWidth(final String width) {
+ super.setWidth(width != null && !width.isEmpty() ? width
+ : DEFAULT_WIDTH);
+ recalculateElementSizes();
+ }
+
+ /**
+ * {@inheritDoc}
+ * <p>
+ * If Escalator is currently not in {@link HeightMode#CSS}, the given value
+ * is remembered, and applied once the mode is applied.
+ *
+ * @see #setHeightMode(HeightMode)
+ */
+ @Override
+ public void setHeight(String height) {
+ /*
+ * TODO remove method once RequiresResize and the Vaadin layoutmanager
+ * listening mechanisms are implemented
+ */
+
+ heightByCss = height;
+ if (getHeightMode() == HeightMode.CSS) {
+ setHeightInternal(height);
+ }
+ }
+
+ private void setHeightInternal(final String height) {
+ final int escalatorRowsBefore = body.visualRowOrder.size();
+
+ super.setHeight(height != null && !height.isEmpty() ? height
+ : DEFAULT_HEIGHT);
+ recalculateElementSizes();
+
+ if (escalatorRowsBefore != body.visualRowOrder.size()) {
+ fireRowVisibilityChangeEvent();
+ }
+ }
+
+ /**
+ * Returns the vertical scroll offset. Note that this is not necessarily the
+ * same as the {@code scrollTop} attribute in the DOM.
+ *
+ * @return the logical vertical scroll offset
+ */
+ public double getScrollTop() {
+ return verticalScrollbar.getScrollPos();
+ }
+
+ /**
+ * Sets the vertical scroll offset. Note that this will not necessarily
+ * become the same as the {@code scrollTop} attribute in the DOM.
+ *
+ * @param scrollTop
+ * the number of pixels to scroll vertically
+ */
+ public void setScrollTop(final double scrollTop) {
+ verticalScrollbar.setScrollPos(scrollTop);
+ }
+
+ /**
+ * Returns the logical horizontal scroll offset. Note that this is not
+ * necessarily the same as the {@code scrollLeft} attribute in the DOM.
+ *
+ * @return the logical horizontal scroll offset
+ */
+ public double getScrollLeft() {
+ return horizontalScrollbar.getScrollPos();
+ }
+
+ /**
+ * Sets the logical horizontal scroll offset. Note that will not necessarily
+ * become the same as the {@code scrollLeft} attribute in the DOM.
+ *
+ * @param scrollLeft
+ * the number of pixels to scroll horizontally
+ */
+ public void setScrollLeft(final double scrollLeft) {
+ horizontalScrollbar.setScrollPos(scrollLeft);
+ }
+
+ /**
+ * Scrolls the body horizontally so that the column at the given index is
+ * visible and there is at least {@code padding} pixels in the direction of
+ * the given scroll destination.
+ *
+ * @param columnIndex
+ * the index of the column to scroll to
+ * @param destination
+ * where the column should be aligned visually after scrolling
+ * @param padding
+ * the number pixels to place between the scrolled-to column and
+ * the viewport edge.
+ * @throws IndexOutOfBoundsException
+ * if {@code columnIndex} is not a valid index for an existing
+ * column
+ * @throws IllegalArgumentException
+ * if {@code destination} is {@link ScrollDestination#MIDDLE}
+ * and padding is nonzero, or if the indicated column is frozen
+ */
+ public void scrollToColumn(final int columnIndex,
+ final ScrollDestination destination, final int padding)
+ throws IndexOutOfBoundsException, IllegalArgumentException {
+ if (destination == ScrollDestination.MIDDLE && padding != 0) {
+ throw new IllegalArgumentException(
+ "You cannot have a padding with a MIDDLE destination");
+ }
+ verifyValidColumnIndex(columnIndex);
+
+ if (columnIndex < columnConfiguration.frozenColumns) {
+ throw new IllegalArgumentException("The given column index "
+ + columnIndex + " is frozen.");
+ }
+
+ scroller.scrollToColumn(columnIndex, destination, padding);
+ }
+
+ private void verifyValidColumnIndex(final int columnIndex)
+ throws IndexOutOfBoundsException {
+ if (columnIndex < 0
+ || columnIndex >= columnConfiguration.getColumnCount()) {
+ throw new IndexOutOfBoundsException("The given column index "
+ + columnIndex + " does not exist.");
+ }
+ }
+
+ /**
+ * Scrolls the body vertically so that the row at the given index is visible
+ * and there is at least {@literal padding} pixels to the given scroll
+ * destination.
+ *
+ * @param rowIndex
+ * the index of the logical row to scroll to
+ * @param destination
+ * where the row should be aligned visually after scrolling
+ * @param padding
+ * the number pixels to place between the scrolled-to row and the
+ * viewport edge.
+ * @throws IndexOutOfBoundsException
+ * if {@code rowIndex} is not a valid index for an existing row
+ * @throws IllegalArgumentException
+ * if {@code destination} is {@link ScrollDestination#MIDDLE}
+ * and padding is nonzero
+ */
+ public void scrollToRow(final int rowIndex,
+ final ScrollDestination destination, final int padding)
+ throws IndexOutOfBoundsException, IllegalArgumentException {
+ if (destination == ScrollDestination.MIDDLE && padding != 0) {
+ throw new IllegalArgumentException(
+ "You cannot have a padding with a MIDDLE destination");
+ }
+ verifyValidRowIndex(rowIndex);
+
+ scroller.scrollToRow(rowIndex, destination, padding);
+ }
+
+ private void verifyValidRowIndex(final int rowIndex) {
+ if (rowIndex < 0 || rowIndex >= body.getRowCount()) {
+ throw new IndexOutOfBoundsException("The given row index "
+ + rowIndex + " does not exist.");
+ }
+ }
+
+ /**
+ * Recalculates the dimensions for all elements that require manual
+ * calculations. Also updates the dimension caches.
+ * <p>
+ * <em>Note:</em> This method has the <strong>side-effect</strong>
+ * automatically makes sure that an appropriate amount of escalator rows are
+ * present. So, if the body area grows, more <strong>escalator rows might be
+ * inserted</strong>. Conversely, if the body area shrinks,
+ * <strong>escalator rows might be removed</strong>.
+ */
+ private void recalculateElementSizes() {
+ if (!isAttached()) {
+ return;
+ }
+
+ Profiler.enter("Escalator.recalculateElementSizes");
+ widthOfEscalator = getPreciseWidth(getElement());
+ heightOfEscalator = getPreciseHeight(getElement());
+ for (final AbstractRowContainer rowContainer : rowContainers) {
+ rowContainer.recalculateSectionHeight();
+ }
+
+ scroller.recalculateScrollbarsForVirtualViewport();
+ body.verifyEscalatorCount();
+ Profiler.leave("Escalator.recalculateElementSizes");
+ }
+
+ /**
+ * A routing method for {@link Scroller#onScroll()}.
+ * <p>
+ * This is a workaround for GWT and JSNI unable to properly handle inner
+ * classes, so instead we call the outer class' method, which calls the
+ * inner class' respective method.
+ * <p>
+ * Ideally, this method would not exist, and {@link Scroller#onScroll()}
+ * would be called directly.
+ */
+ private void onScroll() {
+ scroller.onScroll();
+ }
+
+ /**
+ * Snap deltas of x and y to the major four axes (up, down, left, right)
+ * with a threshold of a number of degrees from those axes.
+ *
+ * @param deltaX
+ * the delta in the x axis
+ * @param deltaY
+ * the delta in the y axis
+ * @param thresholdRatio
+ * the threshold in ratio (0..1) between x and y for when to snap
+ * @return a two-element array: <code>[snappedX, snappedY]</code>
+ */
+ private static double[] snapDeltas(final double deltaX,
+ final double deltaY, final double thresholdRatio) {
+
+ final double[] array = new double[2];
+ if (deltaX != 0 && deltaY != 0) {
+ final double aDeltaX = Math.abs(deltaX);
+ final double aDeltaY = Math.abs(deltaY);
+ final double yRatio = aDeltaY / aDeltaX;
+ final double xRatio = aDeltaX / aDeltaY;
+
+ array[0] = (xRatio < thresholdRatio) ? 0 : deltaX;
+ array[1] = (yRatio < thresholdRatio) ? 0 : deltaY;
+ } else {
+ array[0] = deltaX;
+ array[1] = deltaY;
+ }
+
+ return array;
+ }
+
+ /**
+ * Adds an event handler that gets notified when the range of visible rows
+ * changes e.g. because of scrolling or row resizing.
+ *
+ * @param rowVisibilityChangeHandler
+ * the event handler
+ * @return a handler registration for the added handler
+ */
+ public HandlerRegistration addRowVisibilityChangeHandler(
+ RowVisibilityChangeHandler rowVisibilityChangeHandler) {
+ return addHandler(rowVisibilityChangeHandler,
+ RowVisibilityChangeEvent.TYPE);
+ }
+
+ private void fireRowVisibilityChangeEvent() {
+ if (!body.visualRowOrder.isEmpty()) {
+ int visibleRangeStart = body.getLogicalRowIndex(body.visualRowOrder
+ .getFirst());
+ int visibleRangeEnd = body.getLogicalRowIndex(body.visualRowOrder
+ .getLast()) + 1;
+
+ int visibleRowCount = visibleRangeEnd - visibleRangeStart;
+ fireEvent(new RowVisibilityChangeEvent(visibleRangeStart,
+ visibleRowCount));
+ } else {
+ fireEvent(new RowVisibilityChangeEvent(0, 0));
+ }
+ }
+
+ /**
+ * Gets the range of currently visible rows.
+ *
+ * @return range of visible rows
+ */
+ public Range getVisibleRowRange() {
+ return Range.withLength(
+ body.getLogicalRowIndex(body.visualRowOrder.getFirst()),
+ body.visualRowOrder.size());
+ }
+
+ /**
+ * Returns the widget from a cell node or <code>null</code> if there is no
+ * widget in the cell
+ *
+ * @param cellNode
+ * The cell node
+ */
+ static Widget getWidgetFromCell(Node cellNode) {
+ Node possibleWidgetNode = cellNode.getFirstChild();
+ if (possibleWidgetNode != null
+ && possibleWidgetNode.getNodeType() == Node.ELEMENT_NODE) {
+ @SuppressWarnings("deprecation")
+ com.google.gwt.user.client.Element castElement = (com.google.gwt.user.client.Element) possibleWidgetNode
+ .cast();
+ Widget w = Util.findWidget(castElement, null);
+
+ // Ensure findWidget did not traverse past the cell element in the
+ // DOM hierarchy
+ if (cellNode.isOrHasChild(w.getElement())) {
+ return w;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Forces the escalator to recalculate the widths of its columns.
+ * <p>
+ * All columns that haven't been assigned an explicit width will be resized
+ * to fit all currently visible contents.
+ *
+ * @see ColumnConfiguration#setColumnWidth(int, int)
+ */
+ public void calculateColumnWidths() {
+ boolean widthsHaveChanged = false;
+ for (int colIndex = 0; colIndex < columnConfiguration.getColumnCount(); colIndex++) {
+ if (columnConfiguration.getColumnWidth(colIndex) >= 0) {
+ continue;
+ }
+
+ final int oldColumnWidth = columnConfiguration
+ .getColumnWidthActual(colIndex);
+
+ int maxColumnWidth = 0;
+ maxColumnWidth = Math.max(maxColumnWidth,
+ header.calculateMaxColWidth(colIndex));
+ maxColumnWidth = Math.max(maxColumnWidth,
+ body.calculateMaxColWidth(colIndex));
+ maxColumnWidth = Math.max(maxColumnWidth,
+ footer.calculateMaxColWidth(colIndex));
+
+ Logger.getLogger("Escalator.calculateColumnWidths").info(
+ "#" + colIndex + ": " + maxColumnWidth + "px");
+
+ if (oldColumnWidth != maxColumnWidth) {
+ columnConfiguration.setCalculatedColumnWidth(colIndex,
+ maxColumnWidth);
+ widthsHaveChanged = true;
+ }
+ }
+
+ if (widthsHaveChanged) {
+ header.reapplyColumnWidths();
+ body.reapplyColumnWidths();
+ footer.reapplyColumnWidths();
+ recalculateElementSizes();
+ }
+ }
+
+ @Override
+ public void setStylePrimaryName(String style) {
+ super.setStylePrimaryName(style);
+
+ verticalScrollbar.setStylePrimaryName(style);
+ horizontalScrollbar.setStylePrimaryName(style);
+
+ UIObject.setStylePrimaryName(tableWrapper, style + "-tablewrapper");
+
+ header.setStylePrimaryName(style);
+ body.setStylePrimaryName(style);
+ footer.setStylePrimaryName(style);
+ }
+
+ /**
+ * Sets the number of rows that should be visible in Escalator's body, while
+ * {@link #getHeightMode()} is {@link HeightMode#ROW}.
+ * <p>
+ * If Escalator is currently not in {@link HeightMode#ROW}, the given value
+ * is remembered, and applied once the mode is applied.
+ *
+ * @param rows
+ * the number of rows that should be visible in Escalator's body
+ * @throws IllegalArgumentException
+ * if {@code rows} is &leq; 0,
+ * {@link Double#isInifinite(double) infinite} or
+ * {@link Double#isNaN(double) NaN}.
+ * @see #setHeightMode(HeightMode)
+ */
+ public void setHeightByRows(double rows) throws IllegalArgumentException {
+ if (rows <= 0) {
+ throw new IllegalArgumentException(
+ "The number of rows must be a positive number.");
+ } else if (Double.isInfinite(rows)) {
+ throw new IllegalArgumentException(
+ "The number of rows must be finite.");
+ } else if (Double.isNaN(rows)) {
+ throw new IllegalArgumentException("The number must not be NaN.");
+ }
+
+ heightByRows = rows;
+ applyHeightByRows();
+ }
+
+ /**
+ * Gets the amount of rows in Escalator's body that are shown, while
+ * {@link #getHeightMode()} is {@link HeightMode#ROW}.
+ * <p>
+ * By default, it is {@value GridState#DEFAULT_HEIGHT_BY_ROWS}.
+ *
+ * @return the amount of rows that are being shown in Escalator's body
+ * @see #setHeightByRows(double)
+ */
+ public double getHeightByRows() {
+ return heightByRows;
+ }
+
+ /**
+ * Reapplies the row-based height of the Grid, if Grid currently should
+ * define its height that way.
+ */
+ private void applyHeightByRows() {
+ if (heightMode != HeightMode.ROW) {
+ return;
+ }
+
+ double headerHeight = header.heightOfSection;
+ double footerHeight = footer.heightOfSection;
+ double bodyHeight = body.getDefaultRowHeight() * heightByRows;
+ double scrollbar = horizontalScrollbar.showsScrollHandle() ? horizontalScrollbar
+ .getScrollbarThickness() : 0;
+
+ double totalHeight = headerHeight + bodyHeight + scrollbar
+ + footerHeight;
+ setHeightInternal(totalHeight + "px");
+ }
+
+ /**
+ * Defines the mode in which the Escalator widget's height is calculated.
+ * <p>
+ * If {@link HeightMode#CSS} is given, Escalator will respect the values
+ * given via {@link #setHeight(String)}, and behave as a traditional Widget.
+ * <p>
+ * If {@link HeightMode#ROW} is given, Escalator will make sure that the
+ * {@link #getBody() body} will display as many rows as
+ * {@link #getHeightByRows()} defines. <em>Note:</em> If headers/footers are
+ * inserted or removed, the widget will resize itself to still display the
+ * required amount of rows in its body. It also takes the horizontal
+ * scrollbar into account.
+ *
+ * @param heightMode
+ * the mode in to which Escalator should be set
+ */
+ public void setHeightMode(HeightMode heightMode) {
+ /*
+ * This method is a workaround for the fact that Vaadin re-applies
+ * widget dimensions (height/width) on each state change event. The
+ * original design was to have setHeight an setHeightByRow be equals,
+ * and whichever was called the latest was considered in effect.
+ *
+ * But, because of Vaadin always calling setHeight on the widget, this
+ * approach doesn't work.
+ */
+
+ if (heightMode != this.heightMode) {
+ this.heightMode = heightMode;
+
+ switch (this.heightMode) {
+ case CSS:
+ setHeight(heightByCss);
+ break;
+ case ROW:
+ setHeightByRows(heightByRows);
+ break;
+ default:
+ throw new IllegalStateException("Unimplemented feature "
+ + "- unknown HeightMode: " + this.heightMode);
+ }
+ }
+ }
+
+ /**
+ * Returns the current {@link HeightMode} the Escalator is in.
+ * <p>
+ * Defaults to {@link HeightMode#CSS}.
+ *
+ * @return the current HeightMode
+ */
+ public HeightMode getHeightMode() {
+ return heightMode;
+ }
+
+ /**
+ * Returns the {@link RowContainer} which contains the element.
+ *
+ * @param element
+ * the element to check for
+ * @return the container the element is in or <code>null</code> if element
+ * is not present in any container.
+ */
+ public RowContainer findRowContainer(Element element) {
+ if (getHeader().getElement().isOrHasChild(element)) {
+ return getHeader();
+ } else if (getBody().getElement().isOrHasChild(element)) {
+ return getBody();
+ } else if (getFooter().getElement().isOrHasChild(element)) {
+ return getFooter();
+ }
+ return null;
+ }
+}
diff --git a/client/src/com/vaadin/client/ui/grid/EscalatorUpdater.java b/client/src/com/vaadin/client/ui/grid/EscalatorUpdater.java
new file mode 100644
index 0000000000..aae6b63d20
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/EscalatorUpdater.java
@@ -0,0 +1,155 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid;
+
+/**
+ * An interface that allows client code to define how a certain row in Escalator
+ * will be displayed. The contents of an escalator's header, body and footer are
+ * rendered by their respective updaters.
+ * <p>
+ * The updater is responsible for internally handling all remote communication,
+ * should the displayed data need to be fetched remotely.
+ * <p>
+ * This has a similar function to {@link Grid Grid's} {@link Renderer Renderers}
+ * , although they operate on different abstraction levels.
+ *
+ * @since
+ * @author Vaadin Ltd
+ * @see RowContainer#setEscalatorUpdater(EscalatorUpdater)
+ * @see Escalator#getHeader()
+ * @see Escalator#getBody()
+ * @see Escalator#getFooter()
+ * @see Renderer
+ */
+public interface EscalatorUpdater {
+
+ /**
+ * An {@link EscalatorUpdater} that doesn't render anything.
+ */
+ public static final EscalatorUpdater NULL = new EscalatorUpdater() {
+ @Override
+ public void update(final Row row,
+ final Iterable<FlyweightCell> cellsToUpdate) {
+ // NOOP
+ }
+
+ @Override
+ public void preAttach(final Row row,
+ final Iterable<FlyweightCell> cellsToAttach) {
+ // NOOP
+
+ }
+
+ @Override
+ public void postAttach(final Row row,
+ final Iterable<FlyweightCell> attachedCells) {
+ // NOOP
+ }
+
+ @Override
+ public void preDetach(final Row row,
+ final Iterable<FlyweightCell> cellsToDetach) {
+ // NOOP
+ }
+
+ @Override
+ public void postDetach(final Row row,
+ final Iterable<FlyweightCell> detachedCells) {
+ // NOOP
+ }
+ };
+
+ /**
+ * Renders a row contained in a row container.
+ * <p>
+ * <em>Note:</em> If rendering of cells is deferred (e.g. because
+ * asynchronous data retrieval), this method is responsible for explicitly
+ * displaying some placeholder data (empty content is valid). Because the
+ * cells (and rows) in an escalator are recycled, failing to reset a cell's
+ * presentation will lead to wrong data being displayed in the escalator.
+ * <p>
+ * For performance reasons, the escalator will never autonomously clear any
+ * data in a cell.
+ *
+ * @param row
+ * Information about the row that is being updated.
+ * <em>Note:</em> You should not store nor reuse this reference.
+ * @param cellsToUpdate
+ * A collection of cells that need to be updated. <em>Note:</em>
+ * You should neither store nor reuse the reference to the
+ * iterable, nor to the individual cells.
+ */
+ public void update(Row row, Iterable<FlyweightCell> cellsToUpdate);
+
+ /**
+ * Called before attaching new cells to the escalator.
+ *
+ * @param row
+ * Information about the row to which the cells will be added.
+ * <em>Note:</em> You should not store nor reuse this reference.
+ * @param cellsToAttach
+ * A collection of cells that are about to be attached.
+ * <em>Note:</em> You should neither store nor reuse the
+ * reference to the iterable, nor to the individual cells.
+ *
+ */
+ public void preAttach(Row row, Iterable<FlyweightCell> cellsToAttach);
+
+ /**
+ * Called after attaching new cells to the escalator.
+ *
+ * @param row
+ * Information about the row to which the cells were added.
+ * <em>Note:</em> You should not store nor reuse this reference.
+ * @param attachedCells
+ * A collection of cells that were attached. <em>Note:</em> You
+ * should neither store nor reuse the reference to the iterable,
+ * nor to the individual cells.
+ *
+ */
+ public void postAttach(Row row, Iterable<FlyweightCell> attachedCells);
+
+ /**
+ * Called before detaching cells from the escalator.
+ *
+ * @param row
+ * Information about the row from which the cells will be
+ * removed. <em>Note:</em> You should not store nor reuse this
+ * reference.
+ * @param cellsToAttach
+ * A collection of cells that are about to be detached.
+ * <em>Note:</em> You should neither store nor reuse the
+ * reference to the iterable, nor to the individual cells.
+ *
+ */
+ public void preDetach(Row row, Iterable<FlyweightCell> cellsToDetach);
+
+ /**
+ * Called after detaching cells from the escalator.
+ *
+ * @param row
+ * Information about the row from which the cells were removed.
+ * <em>Note:</em> You should not store nor reuse this reference.
+ * @param attachedCells
+ * A collection of cells that were detached. <em>Note:</em> You
+ * should neither store nor reuse the reference to the iterable,
+ * nor to the individual cells.
+ *
+ */
+ public void postDetach(Row row, Iterable<FlyweightCell> detachedCells);
+
+}
diff --git a/client/src/com/vaadin/client/ui/grid/FlyweightCell.java b/client/src/com/vaadin/client/ui/grid/FlyweightCell.java
new file mode 100644
index 0000000000..30cc4fc79e
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/FlyweightCell.java
@@ -0,0 +1,194 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid;
+
+import java.util.List;
+
+import com.google.gwt.dom.client.Style.Display;
+import com.google.gwt.dom.client.Style.Unit;
+import com.google.gwt.dom.client.TableCellElement;
+import com.vaadin.client.ui.grid.FlyweightRow.CellIterator;
+
+/**
+ * A {@link FlyweightCell} represents a cell in the {@link Grid} or
+ * {@link Escalator} at a certain point in time.
+ *
+ * <p>
+ * Since the {@link FlyweightCell} follows the <code>Flyweight</code>-pattern
+ * any instance of this object is subject to change without the user knowing it
+ * and so should not be stored anywhere outside of the method providing these
+ * instances.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public class FlyweightCell {
+ static final String COLSPAN_ATTR = "colSpan";
+
+ private final int column;
+ private final FlyweightRow row;
+
+ private TableCellElement element = null;
+ private CellIterator currentIterator = null;
+
+ public FlyweightCell(final FlyweightRow row, final int column) {
+ this.row = row;
+ this.column = column;
+ }
+
+ /**
+ * Returns the row index of the cell
+ */
+ public int getRow() {
+ assertSetup();
+ return row.getRow();
+ }
+
+ /**
+ * Returns the column index of the cell
+ */
+ public int getColumn() {
+ assertSetup();
+ return column;
+ }
+
+ /**
+ * Returns the element of the cell. Can be either a <code>TD</code> element
+ * or a <code>TH</code> element.
+ */
+ public TableCellElement getElement() {
+ assertSetup();
+ return element;
+ }
+
+ /**
+ * Return the colspan attribute of the element of the cell.
+ */
+ public int getColSpan() {
+ assertSetup();
+ return element.getPropertyInt(COLSPAN_ATTR);
+ }
+
+ /**
+ * Sets the DOM element for this FlyweightCell, either a <code>TD</code> or
+ * a <code>TH</code>. It is the caller's responsibility to actually insert
+ * the given element to the document when needed.
+ *
+ * @param element
+ * the element corresponding to this cell, cannot be null
+ */
+ void setElement(TableCellElement element) {
+ assert element != null;
+ assertSetup();
+ this.element = element;
+ }
+
+ void setup(final CellIterator iterator) {
+ currentIterator = iterator;
+
+ if (iterator.areCellsAttached()) {
+ final TableCellElement e = row.getElement().getCells()
+ .getItem(column);
+ e.setPropertyInt(COLSPAN_ATTR, 1);
+ e.getStyle().setWidth(row.getColumnWidth(column), Unit.PX);
+ e.getStyle().clearDisplay();
+ setElement(e);
+ }
+ }
+
+ /**
+ * Tear down the state of the Cell.
+ * <p>
+ * This is an internal check method, to prevent retrieving uninitialized
+ * data by calling {@link #getRow()}, {@link #getColumn()} or
+ * {@link #getElement()} at an improper time.
+ * <p>
+ * This should only be used with asserts ("
+ * <code>assert flyweightCell.teardown()</code> ") so that the code is never
+ * run when asserts aren't enabled.
+ *
+ * @return always <code>true</code>
+ * @see FlyweightRow#teardown()
+ */
+ boolean teardown() {
+ currentIterator = null;
+ element = null;
+ return true;
+ }
+
+ /**
+ * Asserts that the flyweight cell has properly been set up before trying to
+ * access any of its data.
+ */
+ private void assertSetup() {
+ assert currentIterator != null : "FlyweightCell was not properly "
+ + "initialized. This is either a bug in Grid/Escalator "
+ + "or a Cell reference has been stored and reused "
+ + "inappropriately.";
+ }
+
+ public void setColSpan(final int numberOfCells) {
+ if (numberOfCells < 1) {
+ throw new IllegalArgumentException(
+ "Number of cells should be more than 0");
+ }
+
+ /*-
+ * This will default to 1 if unset, as per DOM specifications:
+ * http://www.w3.org/TR/html5/tabular-data.html#attributes-common-to-td-and-th-elements
+ */
+ final int prevColSpan = getElement().getPropertyInt(COLSPAN_ATTR);
+ if (numberOfCells == 1 && prevColSpan == 1) {
+ return;
+ }
+
+ getElement().setPropertyInt(COLSPAN_ATTR, numberOfCells);
+ adjustCellWidthForSpan(numberOfCells);
+ hideOrRevealAdjacentCellElements(numberOfCells, prevColSpan);
+ currentIterator.setSkipNext(numberOfCells - 1);
+ }
+
+ private void adjustCellWidthForSpan(final int numberOfCells) {
+ final int cellsToTheRight = currentIterator.rawPeekNext(
+ numberOfCells - 1).size();
+
+ final int selfWidth = row.getColumnWidth(column);
+ int widthsOfColumnsToTheRight = 0;
+ for (int i = 0; i < cellsToTheRight; i++) {
+ widthsOfColumnsToTheRight += row.getColumnWidth(column + i + 1);
+ }
+ getElement().getStyle().setWidth(selfWidth + widthsOfColumnsToTheRight,
+ Unit.PX);
+ }
+
+ private void hideOrRevealAdjacentCellElements(final int numberOfCells,
+ final int prevColSpan) {
+ final int affectedCellsNumber = Math.max(prevColSpan, numberOfCells);
+ final List<FlyweightCell> affectedCells = currentIterator
+ .rawPeekNext(affectedCellsNumber - 1);
+ if (prevColSpan < numberOfCells) {
+ for (int i = 0; i < affectedCells.size(); i++) {
+ affectedCells.get(prevColSpan + i - 1).getElement().getStyle()
+ .setDisplay(Display.NONE);
+ }
+ } else if (prevColSpan > numberOfCells) {
+ for (int i = 0; i < affectedCells.size(); i++) {
+ affectedCells.get(numberOfCells + i - 1).getElement()
+ .getStyle().clearDisplay();
+ }
+ }
+ }
+}
diff --git a/client/src/com/vaadin/client/ui/grid/FlyweightRow.java b/client/src/com/vaadin/client/ui/grid/FlyweightRow.java
new file mode 100644
index 0000000000..08f4f1d33c
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/FlyweightRow.java
@@ -0,0 +1,293 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+
+import com.google.gwt.dom.client.TableRowElement;
+
+/**
+ * An internal implementation of the {@link Row} interface.
+ * <p>
+ * There is only one instance per Escalator. This is designed to be re-used when
+ * rendering rows.
+ *
+ * @since
+ * @author Vaadin Ltd
+ * @see Escalator.AbstractRowContainer#refreshRow(Node, int)
+ */
+class FlyweightRow implements Row {
+
+ static class CellIterator implements Iterator<FlyweightCell> {
+ /** A defensive copy of the cells in the current row. */
+ private final ArrayList<FlyweightCell> cells;
+ private final boolean cellsAttached;
+ private int cursor = 0;
+ private int skipNext = 0;
+
+ /**
+ * Creates a new iterator of attached flyweight cells. A cell is
+ * attached if it has a corresponding {@link FlyweightCell#getElement()
+ * DOM element} attached to the row element.
+ *
+ * @param cells
+ * the collection of cells to iterate
+ */
+ public static CellIterator attached(
+ final Collection<FlyweightCell> cells) {
+ return new CellIterator(cells, true);
+ }
+
+ /**
+ * Creates a new iterator of unattached flyweight cells. A cell is
+ * unattached if it does not have a corresponding
+ * {@link FlyweightCell#getElement() DOM element} attached to the row
+ * element.
+ *
+ * @param cells
+ * the collection of cells to iterate
+ */
+ public static CellIterator unattached(
+ final Collection<FlyweightCell> cells) {
+ return new CellIterator(cells, false);
+ }
+
+ private CellIterator(final Collection<FlyweightCell> cells,
+ final boolean attached) {
+ this.cells = new ArrayList<FlyweightCell>(cells);
+ cellsAttached = attached;
+ }
+
+ @Override
+ public boolean hasNext() {
+ return cursor + skipNext < cells.size();
+ }
+
+ @Override
+ public FlyweightCell next() {
+ // if we needed to skip some cells since the last invocation.
+ for (int i = 0; i < skipNext; i++) {
+ cells.remove(cursor);
+ }
+ skipNext = 0;
+
+ final FlyweightCell cell = cells.get(cursor++);
+ cell.setup(this);
+ return cell;
+ }
+
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException(
+ "Cannot remove cells via iterator");
+ }
+
+ /**
+ * Sets the number of cells to skip when {@link #next()} is called the
+ * next time. Cell hiding is also handled eagerly in this method.
+ *
+ * @param colspan
+ * the number of cells to skip on next invocation of
+ * {@link #next()}
+ */
+ public void setSkipNext(final int colspan) {
+ assert colspan > 0 : "Number of cells didn't make sense: "
+ + colspan;
+ skipNext = colspan;
+ }
+
+ /**
+ * Gets the next <code>n</code> cells in the iterator, ignoring any
+ * possibly spanned cells.
+ *
+ * @param n
+ * the number of next cells to retrieve
+ * @return A list of next <code>n</code> cells, or less if there aren't
+ * enough cells to retrieve
+ */
+ public List<FlyweightCell> rawPeekNext(final int n) {
+ final int from = Math.min(cursor, cells.size());
+ final int to = Math.min(cursor + n, cells.size());
+ List<FlyweightCell> nextCells = cells.subList(from, to);
+ for (FlyweightCell cell : nextCells) {
+ cell.setup(this);
+ }
+ return nextCells;
+ }
+
+ public boolean areCellsAttached() {
+ return cellsAttached;
+ }
+ }
+
+ private static final int BLANK = Integer.MIN_VALUE;
+
+ private int row;
+ private TableRowElement element;
+ private int[] columnWidths = null;
+ private final List<FlyweightCell> cells = new ArrayList<FlyweightCell>();
+
+ void setup(final TableRowElement e, final int row, int[] columnWidths) {
+ element = e;
+ this.row = row;
+ this.columnWidths = columnWidths;
+ }
+
+ /**
+ * Tear down the state of the Row.
+ * <p>
+ * This is an internal check method, to prevent retrieving uninitialized
+ * data by calling {@link #getRow()}, {@link #getElement()} or
+ * {@link #getCells()} at an improper time.
+ * <p>
+ * This should only be used with asserts ("
+ * <code>assert flyweightRow.teardown()</code> ") so that the code is never
+ * run when asserts aren't enabled.
+ *
+ * @return always <code>true</code>
+ */
+ boolean teardown() {
+ element = null;
+ row = BLANK;
+ columnWidths = null;
+ for (final FlyweightCell cell : cells) {
+ assert cell.teardown();
+ }
+ return true;
+ }
+
+ @Override
+ public int getRow() {
+ assertSetup();
+ return row;
+ }
+
+ @Override
+ public TableRowElement getElement() {
+ assertSetup();
+ return element;
+ }
+
+ void addCells(final int index, final int numberOfColumns) {
+ for (int i = 0; i < numberOfColumns; i++) {
+ final int col = index + i;
+ cells.add(col, new FlyweightCell(this, col));
+ }
+ updateRestOfCells(index + numberOfColumns);
+ }
+
+ void removeCells(final int index, final int numberOfColumns) {
+ for (int i = 0; i < numberOfColumns; i++) {
+ cells.remove(index);
+ }
+ updateRestOfCells(index);
+ }
+
+ private void updateRestOfCells(final int startPos) {
+ // update the column number for the cells to the right
+ for (int col = startPos; col < cells.size(); col++) {
+ cells.set(col, new FlyweightCell(this, col));
+ }
+ }
+
+ /**
+ * Returns flyweight cells for the client code to render. The cells get
+ * their associated {@link FlyweightCell#getElement() elements} from the row
+ * element.
+ * <p>
+ * Precondition: each cell has a corresponding element in the row
+ *
+ * @return an iterable of flyweight cells
+ *
+ * @see #setup(Element, int, int[])
+ * @see #teardown()
+ */
+ Iterable<FlyweightCell> getCells() {
+ return getCells(0, cells.size());
+ }
+
+ /**
+ * Returns a subrange of flyweight cells for the client code to render. The
+ * cells get their associated {@link FlyweightCell#getElement() elements}
+ * from the row element.
+ * <p>
+ * Precondition: each cell has a corresponding element in the row
+ *
+ * @param offset
+ * the index of the first cell to return
+ * @param numberOfCells
+ * the number of cells to return
+ * @return an iterable of flyweight cells
+ */
+ Iterable<FlyweightCell> getCells(final int offset, final int numberOfCells) {
+ assertSetup();
+ return new Iterable<FlyweightCell>() {
+ @Override
+ public Iterator<FlyweightCell> iterator() {
+ return CellIterator.attached(cells.subList(offset, offset
+ + numberOfCells));
+ }
+ };
+ }
+
+ /**
+ * Returns a subrange of unattached flyweight cells. Unattached cells do not
+ * have {@link FlyweightCell#getElement() elements} associated. Note that
+ * FlyweightRow does not keep track of whether cells in actuality have
+ * corresponding DOM elements or not; it is the caller's responsibility to
+ * invoke this method with correct parameters.
+ * <p>
+ * Precondition: the range [offset, offset + numberOfCells) must be valid
+ *
+ * @param offset
+ * the index of the first cell to return
+ * @param numberOfCells
+ * the number of cells to return
+ * @return an iterable of flyweight cells
+ */
+ Iterable<FlyweightCell> getUnattachedCells(final int offset,
+ final int numberOfCells) {
+ assertSetup();
+ assert offset >= 0 && offset + numberOfCells <= cells.size() : "Invalid range of cells";
+ return new Iterable<FlyweightCell>() {
+ @Override
+ public Iterator<FlyweightCell> iterator() {
+ return CellIterator.unattached(cells.subList(offset, offset
+ + numberOfCells));
+ }
+ };
+ }
+
+ /**
+ * Asserts that the flyweight row has properly been set up before trying to
+ * access any of its data.
+ */
+ private void assertSetup() {
+ assert element != null && row != BLANK && columnWidths != null : "Flyweight row was not "
+ + "properly initialized. Make sure the setup-method is "
+ + "called before retrieving data. This is either a bug "
+ + "in Escalator, or the instance of the flyweight row "
+ + "has been stored and accessed.";
+ }
+
+ int getColumnWidth(int column) {
+ assertSetup();
+ return columnWidths[column];
+ }
+}
diff --git a/client/src/com/vaadin/client/ui/grid/Grid.java b/client/src/com/vaadin/client/ui/grid/Grid.java
new file mode 100644
index 0000000000..70d8286c90
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/Grid.java
@@ -0,0 +1,2562 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import com.google.gwt.core.client.Scheduler;
+import com.google.gwt.core.client.Scheduler.ScheduledCommand;
+import com.google.gwt.core.shared.GWT;
+import com.google.gwt.dom.client.BrowserEvents;
+import com.google.gwt.dom.client.Element;
+import com.google.gwt.dom.client.EventTarget;
+import com.google.gwt.dom.client.NativeEvent;
+import com.google.gwt.dom.client.TableCellElement;
+import com.google.gwt.dom.client.TableRowElement;
+import com.google.gwt.dom.client.Touch;
+import com.google.gwt.event.dom.client.KeyCodeEvent;
+import com.google.gwt.event.dom.client.KeyCodes;
+import com.google.gwt.event.shared.HandlerRegistration;
+import com.google.gwt.touch.client.Point;
+import com.google.gwt.user.client.DOM;
+import com.google.gwt.user.client.Event;
+import com.google.gwt.user.client.Timer;
+import com.google.gwt.user.client.ui.Composite;
+import com.google.gwt.user.client.ui.HasVisibility;
+import com.google.gwt.user.client.ui.Widget;
+import com.vaadin.client.Util;
+import com.vaadin.client.data.DataChangeHandler;
+import com.vaadin.client.data.DataSource;
+import com.vaadin.client.ui.SubPartAware;
+import com.vaadin.client.ui.grid.GridFooter.FooterRow;
+import com.vaadin.client.ui.grid.GridHeader.HeaderRow;
+import com.vaadin.client.ui.grid.GridStaticSection.StaticCell;
+import com.vaadin.client.ui.grid.keyevents.AbstractGridKeyEventHandler;
+import com.vaadin.client.ui.grid.keyevents.AbstractGridKeyEventHandler.GridKeyDownHandler;
+import com.vaadin.client.ui.grid.keyevents.AbstractGridKeyEventHandler.GridKeyPressHandler;
+import com.vaadin.client.ui.grid.keyevents.AbstractGridKeyEventHandler.GridKeyUpHandler;
+import com.vaadin.client.ui.grid.keyevents.BodyKeyDownHandler;
+import com.vaadin.client.ui.grid.keyevents.BodyKeyPressHandler;
+import com.vaadin.client.ui.grid.keyevents.BodyKeyUpHandler;
+import com.vaadin.client.ui.grid.keyevents.FooterKeyDownHandler;
+import com.vaadin.client.ui.grid.keyevents.FooterKeyPressHandler;
+import com.vaadin.client.ui.grid.keyevents.FooterKeyUpHandler;
+import com.vaadin.client.ui.grid.keyevents.GridKeyDownEvent;
+import com.vaadin.client.ui.grid.keyevents.GridKeyPressEvent;
+import com.vaadin.client.ui.grid.keyevents.GridKeyUpEvent;
+import com.vaadin.client.ui.grid.keyevents.HeaderKeyDownHandler;
+import com.vaadin.client.ui.grid.keyevents.HeaderKeyPressHandler;
+import com.vaadin.client.ui.grid.keyevents.HeaderKeyUpHandler;
+import com.vaadin.client.ui.grid.renderers.ComplexRenderer;
+import com.vaadin.client.ui.grid.renderers.WidgetRenderer;
+import com.vaadin.client.ui.grid.selection.HasSelectionChangeHandlers;
+import com.vaadin.client.ui.grid.selection.SelectionChangeEvent;
+import com.vaadin.client.ui.grid.selection.SelectionChangeHandler;
+import com.vaadin.client.ui.grid.selection.SelectionModel;
+import com.vaadin.client.ui.grid.selection.SelectionModelMulti;
+import com.vaadin.client.ui.grid.selection.SelectionModelNone;
+import com.vaadin.client.ui.grid.selection.SelectionModelSingle;
+import com.vaadin.client.ui.grid.sort.Sort;
+import com.vaadin.client.ui.grid.sort.SortEvent;
+import com.vaadin.client.ui.grid.sort.SortEventHandler;
+import com.vaadin.client.ui.grid.sort.SortOrder;
+import com.vaadin.shared.ui.grid.GridConstants;
+import com.vaadin.shared.ui.grid.GridStaticCellType;
+import com.vaadin.shared.ui.grid.HeightMode;
+import com.vaadin.shared.ui.grid.Range;
+import com.vaadin.shared.ui.grid.ScrollDestination;
+import com.vaadin.shared.ui.grid.SortDirection;
+import com.vaadin.shared.ui.grid.SortEventOriginator;
+
+/**
+ * A data grid view that supports columns and lazy loading of data rows from a
+ * data source.
+ *
+ * <h3>Columns</h3>
+ * <p>
+ * The {@link GridColumn} class defines the renderer used to render a cell in
+ * the grid. Implement {@link GridColumn#getValue(Object)} to retrieve the cell
+ * value from the row object and return the cell renderer to render that cell.
+ * </p>
+ * <p>
+ * {@link GridColumn}s contain other properties like the width of the column and
+ * the visiblity of the column. If you want to change a column's properties
+ * after it has been added to the grid you can get a column object for a
+ * specific column index using {@link Grid#getColumn(int)}.
+ * </p>
+ * <p>
+ *
+ * TODO Explain about headers/footers once the multiple header/footer api has
+ * been implemented
+ *
+ * <h3>Data sources</h3>
+ * <p>
+ * TODO Explain about what a data source is and how it should be implemented.
+ * </p>
+ *
+ * @param <T>
+ * The row type of the grid. The row type is the POJO type from where
+ * the data is retrieved into the column cells.
+ * @since
+ * @author Vaadin Ltd
+ */
+public class Grid<T> extends Composite implements
+ HasSelectionChangeHandlers<T>, SubPartAware {
+
+ public static abstract class AbstractGridKeyEvent<T, HANDLER extends AbstractGridKeyEventHandler>
+ extends KeyCodeEvent<HANDLER> {
+
+ /**
+ * Enum describing different section of Grid.
+ */
+ public enum GridSection {
+ HEADER, BODY, FOOTER
+ }
+
+ private Grid<T> grid;
+ protected Cell activeCell;
+ protected GridSection activeSection;
+ private final Type<HANDLER> associatedType = new Type<HANDLER>(
+ getBrowserEventType(), this);
+
+ public AbstractGridKeyEvent(Grid<T> grid) {
+ this.grid = grid;
+ }
+
+ protected abstract String getBrowserEventType();
+
+ /**
+ * Gets the Grid instance for this event.
+ *
+ * @return grid
+ */
+ public Grid<T> getGrid() {
+ return grid;
+ }
+
+ /**
+ * Gets the active cell for this event.
+ *
+ * @return active cell
+ */
+ public Cell getActiveCell() {
+ return activeCell;
+ }
+
+ @Override
+ protected void dispatch(HANDLER handler) {
+ activeCell = grid.activeCellHandler.getActiveCell();
+ activeSection = GridSection.FOOTER;
+ final RowContainer container = grid.activeCellHandler.container;
+ if (container == grid.escalator.getHeader()) {
+ activeSection = GridSection.HEADER;
+ } else if (container == grid.escalator.getBody()) {
+ activeSection = GridSection.BODY;
+ }
+ }
+
+ @Override
+ public Type<HANDLER> getAssociatedType() {
+ return associatedType;
+ }
+ }
+
+ private GridKeyDownEvent<T> keyDown = new GridKeyDownEvent<T>(this);
+ private GridKeyUpEvent<T> keyUp = new GridKeyUpEvent<T>(this);
+ private GridKeyPressEvent<T> keyPress = new GridKeyPressEvent<T>(this);
+
+ private class ActiveCellHandler {
+
+ private RowContainer container = escalator.getBody();
+ private int activeRow = 0;
+ private int activeColumn = 0;
+ private int lastActiveBodyRow = 0;
+ private int lastActiveHeaderRow = 0;
+ private int lastActiveFooterRow = 0;
+ private TableCellElement cellWithActiveStyle = null;
+ private TableRowElement rowWithActiveStyle = null;
+
+ public ActiveCellHandler() {
+ sinkEvents(getNavigationEvents());
+ }
+
+ private Cell getActiveCell() {
+ return new Cell(activeRow, activeColumn, cellWithActiveStyle);
+ }
+
+ /**
+ * Sets style names for given cell when needed.
+ */
+ public void updateActiveCellStyle(FlyweightCell cell,
+ RowContainer cellContainer) {
+ int cellRow = cell.getRow();
+ int cellColumn = cell.getColumn();
+ int colSpan = cell.getColSpan();
+ boolean columnActive = Range.withLength(cellColumn, colSpan)
+ .contains(activeColumn);
+
+ if (cellContainer == container) {
+ // Cell is in the current container
+ if (cellRow == activeRow && columnActive) {
+ if (cellWithActiveStyle != cell.getElement()) {
+ // Cell is correct but it does not have active style
+ if (cellWithActiveStyle != null) {
+ // Remove old active style
+ setStyleName(cellWithActiveStyle,
+ cellActiveStyleName, false);
+ }
+ cellWithActiveStyle = cell.getElement();
+ // Add active style to correct cell.
+ setStyleName(cellWithActiveStyle, cellActiveStyleName,
+ true);
+ }
+ } else if (cellWithActiveStyle == cell.getElement()) {
+ // Due to escalator reusing cells, a new cell has the same
+ // element but is not the active cell.
+ setStyleName(cellWithActiveStyle, cellActiveStyleName,
+ false);
+ cellWithActiveStyle = null;
+ }
+ }
+
+ if (cellContainer == escalator.getHeader()
+ || cellContainer == escalator.getFooter()) {
+ // Correct header and footer column also needs highlighting
+ setStyleName(cell.getElement(), headerFooterActiveStyleName,
+ columnActive);
+ }
+ }
+
+ /**
+ * Sets active row style name for given row if needed.
+ *
+ * @param row
+ * a row object
+ */
+ public void updateActiveRowStyle(Row row) {
+ if (activeRow == row.getRow() && container == escalator.getBody()) {
+ if (row.getElement() != rowWithActiveStyle) {
+ // Row should have active style but does not have it.
+ if (rowWithActiveStyle != null) {
+ setStyleName(rowWithActiveStyle, rowActiveStyleName,
+ false);
+ }
+ rowWithActiveStyle = row.getElement();
+ setStyleName(rowWithActiveStyle, rowActiveStyleName, true);
+ }
+ } else if (rowWithActiveStyle == row.getElement()
+ || (container != escalator.getBody() && rowWithActiveStyle != null)) {
+ // Remove active style.
+ setStyleName(rowWithActiveStyle, rowActiveStyleName, false);
+ rowWithActiveStyle = null;
+ }
+ }
+
+ /**
+ * Sets currently active cell to a cell in given container with given
+ * indices.
+ *
+ * @param row
+ * new active row
+ * @param column
+ * new active column
+ * @param container
+ * new container
+ */
+ private void setActiveCell(int row, int column, RowContainer container) {
+ if (row == activeRow && column == activeColumn
+ && container == this.container) {
+ return;
+ }
+
+ int oldRow = activeRow;
+ int oldColumn = activeColumn;
+ activeRow = row;
+ activeColumn = column;
+
+ if (container == escalator.getBody()) {
+ scrollToRow(activeRow);
+ }
+ escalator.scrollToColumn(activeColumn, ScrollDestination.ANY, 10);
+
+ if (this.container == container) {
+ if (container != escalator.getBody()) {
+ if (oldColumn == activeColumn && oldRow != activeRow) {
+ refreshRow(oldRow);
+ } else if (oldColumn != activeColumn) {
+ refreshHeader();
+ refreshFooter();
+ }
+ } else {
+ if (oldRow != activeRow) {
+ refreshRow(oldRow);
+ }
+
+ if (oldColumn != activeColumn) {
+ refreshHeader();
+ refreshFooter();
+ }
+ }
+ } else {
+ RowContainer oldContainer = this.container;
+ this.container = container;
+
+ if (oldContainer == escalator.getBody()) {
+ lastActiveBodyRow = oldRow;
+ } else if (oldContainer == escalator.getHeader()) {
+ lastActiveHeaderRow = oldRow;
+ } else {
+ lastActiveFooterRow = oldRow;
+ }
+
+ if (oldColumn != activeColumn) {
+ refreshHeader();
+ refreshFooter();
+ if (oldContainer == escalator.getBody()) {
+ oldContainer.refreshRows(oldRow, 1);
+ }
+ } else {
+ oldContainer.refreshRows(oldRow, 1);
+ }
+ }
+ refreshRow(activeRow);
+ }
+
+ /**
+ * Sets currently active cell used for keyboard navigation. Note that
+ * active cell is not JavaScript {@code document.activeElement}.
+ *
+ * @param cell
+ * a cell object
+ */
+ public void setActiveCell(Cell cell) {
+ setActiveCell(cell.getRow(), cell.getColumn(),
+ escalator.findRowContainer(cell.getElement()));
+ }
+
+ /**
+ * Gets list of events that can be used for active cell navigation.
+ *
+ * @return list of navigation related event types
+ */
+ public Collection<String> getNavigationEvents() {
+ return Arrays.asList(BrowserEvents.KEYDOWN, BrowserEvents.CLICK);
+ }
+
+ /**
+ * Handle events that can change the currently active cell.
+ */
+ public void handleNavigationEvent(Event event, Cell cell) {
+ if (event.getType().equals(BrowserEvents.CLICK) && cell != null) {
+ setActiveCell(cell);
+ getElement().focus();
+ } else if (event.getType().equals(BrowserEvents.KEYDOWN)) {
+ int newRow = activeRow;
+ int newColumn = activeColumn;
+ RowContainer newContainer = container;
+
+ switch (event.getKeyCode()) {
+ case KeyCodes.KEY_DOWN:
+ newRow += 1;
+ break;
+ case KeyCodes.KEY_UP:
+ newRow -= 1;
+ break;
+ case KeyCodes.KEY_RIGHT:
+ newColumn += 1;
+ break;
+ case KeyCodes.KEY_LEFT:
+ newColumn -= 1;
+ break;
+ case KeyCodes.KEY_TAB:
+ if (event.getShiftKey()) {
+ newContainer = getPreviousContainer(container);
+ } else {
+ newContainer = getNextContainer(container);
+ }
+
+ if (newContainer == container) {
+ return;
+ }
+ break;
+ default:
+ return;
+ }
+
+ if (newContainer != container) {
+ if (newContainer == escalator.getBody()) {
+ newRow = lastActiveBodyRow;
+ } else if (newContainer == escalator.getHeader()) {
+ newRow = lastActiveHeaderRow;
+ } else {
+ newRow = lastActiveFooterRow;
+ }
+ } else if (newRow < 0) {
+ newContainer = getPreviousContainer(newContainer);
+
+ if (newContainer == container) {
+ newRow = 0;
+ } else if (newContainer == escalator.getBody()) {
+ newRow = getLastVisibleRowIndex();
+ } else {
+ newRow = newContainer.getRowCount() - 1;
+ }
+ } else if (newRow >= container.getRowCount()) {
+ newContainer = getNextContainer(newContainer);
+
+ if (newContainer == container) {
+ newRow = container.getRowCount() - 1;
+ } else if (newContainer == escalator.getBody()) {
+ newRow = getFirstVisibleRowIndex();
+ } else {
+ newRow = 0;
+ }
+ }
+
+ if (newContainer.getRowCount() == 0) {
+ // There are no rows in the container. Can't change the
+ // active cell.
+ return;
+ }
+
+ if (newColumn < 0) {
+ newColumn = 0;
+ } else if (newColumn >= getColumnCount()) {
+ newColumn = getColumnCount() - 1;
+ }
+
+ event.preventDefault();
+ event.stopPropagation();
+
+ setActiveCell(newRow, newColumn, newContainer);
+ }
+
+ }
+
+ private RowContainer getPreviousContainer(RowContainer current) {
+ if (current == escalator.getFooter()) {
+ current = escalator.getBody();
+ } else if (current == escalator.getBody()) {
+ current = escalator.getHeader();
+ } else {
+ return current;
+ }
+
+ if (current.getRowCount() == 0) {
+ return getPreviousContainer(current);
+ }
+ return current;
+ }
+
+ private RowContainer getNextContainer(RowContainer current) {
+ if (current == escalator.getHeader()) {
+ current = escalator.getBody();
+ } else if (current == escalator.getBody()) {
+ current = escalator.getFooter();
+ } else {
+ return current;
+ }
+
+ if (current.getRowCount() == 0) {
+ return getNextContainer(current);
+ }
+ return current;
+ }
+
+ private void refreshRow(int row) {
+ container.refreshRows(row, 1);
+ }
+ }
+
+ private class SelectionColumn extends GridColumn<Boolean, T> {
+ private boolean initDone = false;
+
+ public SelectionColumn(final Renderer<Boolean> selectColumnRenderer) {
+ super(selectColumnRenderer);
+ }
+
+ public void initDone() {
+ initDone = true;
+ }
+
+ @Override
+ public void setVisible(boolean visible) {
+ if (!visible && initDone) {
+ throw new UnsupportedOperationException("The selection "
+ + "column cannot be modified after init");
+ } else {
+ super.setVisible(visible);
+ }
+ }
+
+ @Override
+ public void setWidth(int pixels) {
+ if (pixels != getWidth() && initDone) {
+ throw new UnsupportedOperationException("The selection "
+ + "column cannot be modified after init");
+ } else {
+ super.setWidth(pixels);
+ }
+ }
+
+ @Override
+ public Boolean getValue(T row) {
+ return Boolean.valueOf(isSelected(row));
+ }
+ }
+
+ /**
+ * Class for sorting at a later time
+ */
+ private class LazySorter extends Timer {
+
+ private Cell cell;
+
+ private boolean multisort;
+
+ @Override
+ public void run() {
+ SortOrder sortingOrder = getSortOrder(getColumnFromVisibleIndex(cell
+ .getColumn()));
+ if (sortingOrder == null) {
+ /*
+ * No previous sorting, sort Ascending
+ */
+ sort(cell, SortDirection.ASCENDING, multisort);
+
+ } else {
+ // Toggle sorting
+ SortDirection direction = sortingOrder.getDirection();
+ if (direction == SortDirection.ASCENDING) {
+ sort(cell, SortDirection.DESCENDING, multisort);
+ } else {
+ sort(cell, SortDirection.ASCENDING, multisort);
+ }
+ }
+ }
+
+ /**
+ * Set the cell reference to the primary cell that sorting should be
+ * done for.
+ *
+ * @param cell
+ *
+ */
+ public void setCellReference(Cell cell) {
+ this.cell = cell;
+ }
+
+ /**
+ * Is multiple column sorting is enabled/disabled
+ *
+ * @param multisort
+ * true if multiple column sorting is enabled
+ */
+ public void setMultisort(boolean multisort) {
+ this.multisort = multisort;
+ }
+
+ /**
+ * Sorts the column in a direction
+ */
+ private void sort(Cell cell, SortDirection direction, boolean multisort) {
+ TableCellElement th = TableCellElement.as(cell.getElement());
+
+ // Apply primary sorting on clicked column
+ GridColumn<?, ?> columnInstance = getColumnFromVisibleIndex(cell
+ .getColumn());
+ Sort sorting = Sort.by(columnInstance, direction);
+
+ // Re-apply old sorting to the sort order
+ if (multisort) {
+ for (SortOrder order : getSortOrder()) {
+ if (order.getColumn() != columnInstance) {
+ sorting = sorting.then(order.getColumn(),
+ order.getDirection());
+ }
+ }
+ }
+
+ // Perform sorting; indicate originator as user
+ Grid.this.setSortOrder(sorting.build(), SortEventOriginator.USER);
+ }
+ }
+
+ /**
+ * Escalator used internally by grid to render the rows
+ */
+ private Escalator escalator = GWT.create(Escalator.class);
+
+ private final GridHeader header = GWT.create(GridHeader.class);
+
+ private final GridFooter footer = GWT.create(GridFooter.class);
+
+ /**
+ * List of columns in the grid. Order defines the visible order.
+ */
+ private final List<GridColumn<?, T>> columns = new ArrayList<GridColumn<?, T>>();
+
+ /**
+ * The datasource currently in use. <em>Note:</em> it is <code>null</code>
+ * on initialization, but not after that.
+ */
+ private DataSource<T> dataSource;
+
+ /**
+ * Currently available row range in DataSource.
+ */
+ private Range currentDataAvailable = Range.withLength(0, 0);
+
+ /**
+ * The last column frozen counter from the left
+ */
+ private GridColumn<?, T> lastFrozenColumn;
+
+ /**
+ * Current sort order. The (private) sort() method reads this list to
+ * determine the order in which to present rows.
+ */
+ private List<SortOrder> sortOrder = new ArrayList<SortOrder>();
+
+ private Renderer<Boolean> selectColumnRenderer = null;
+
+ private SelectionColumn selectionColumn;
+
+ private String rowHasDataStyleName;
+ private String rowSelectedStyleName;
+ private String cellActiveStyleName;
+ private String rowActiveStyleName;
+ private String headerFooterActiveStyleName;
+
+ /**
+ * Current selection model.
+ */
+ private SelectionModel<T> selectionModel;
+
+ protected final ActiveCellHandler activeCellHandler;
+
+ private final LazySorter lazySorter = new LazySorter();
+
+ /**
+ * Enumeration for easy setting of selection mode.
+ */
+ public enum SelectionMode {
+
+ /**
+ * Shortcut for {@link SelectionModelSingle}.
+ */
+ SINGLE {
+
+ @Override
+ protected <T> SelectionModel<T> createModel() {
+ return new SelectionModelSingle<T>();
+ }
+ },
+
+ /**
+ * Shortcut for {@link SelectionModelMulti}.
+ */
+ MULTI {
+
+ @Override
+ protected <T> SelectionModel<T> createModel() {
+ return new SelectionModelMulti<T>();
+ }
+ },
+
+ /**
+ * Shortcut for {@link SelectionModelNone}.
+ */
+ NONE {
+
+ @Override
+ protected <T> SelectionModel<T> createModel() {
+ return new SelectionModelNone<T>();
+ }
+ };
+
+ protected abstract <T> SelectionModel<T> createModel();
+ }
+
+ /**
+ * Base class for grid columns internally used by the Grid. The user should
+ * use {@link GridColumn} when creating new columns.
+ *
+ * @param <C>
+ * the column type
+ *
+ * @param <T>
+ * the row type
+ */
+ static abstract class AbstractGridColumn<C, T> implements HasVisibility {
+
+ /**
+ * the column is associated with
+ */
+ private Grid<T> grid;
+
+ /**
+ * Should the column be visible in the grid
+ */
+ private boolean visible = true;
+
+ /**
+ * Width of column in pixels
+ */
+ private int width = 100;
+
+ /**
+ * Renderer for rendering a value into the cell
+ */
+ private Renderer<? super C> bodyRenderer;
+
+ private boolean sortable = false;
+
+ /**
+ * Constructs a new column with a custom renderer.
+ *
+ * @param renderer
+ * The renderer to use for rendering the cells
+ */
+ public AbstractGridColumn(Renderer<? super C> renderer) {
+ if (renderer == null) {
+ throw new IllegalArgumentException("Renderer cannot be null.");
+ }
+ bodyRenderer = renderer;
+ }
+
+ /**
+ * Internally used by the grid to set itself
+ *
+ * @param grid
+ */
+ private void setGrid(Grid<T> grid) {
+ if (this.grid != null && grid != null) {
+ // Trying to replace grid
+ throw new IllegalStateException(
+ "Column already is attached to grid. Remove the column first from the grid and then add it.");
+ }
+
+ this.grid = grid;
+ }
+
+ /**
+ * Is the column visible. By default all columns are visible.
+ *
+ * @return <code>true</code> if the column is visible
+ */
+ @Override
+ public boolean isVisible() {
+ return visible;
+ }
+
+ /**
+ * Sets a column as visible in the grid.
+ *
+ * @param visible
+ * <code>true</code> if the column should be displayed in the
+ * grid
+ */
+ @Override
+ public void setVisible(boolean visible) {
+ if (this.visible == visible) {
+ return;
+ }
+
+ this.visible = visible;
+
+ if (grid != null) {
+ int index = findIndexOfColumn();
+ ColumnConfiguration conf = grid.escalator
+ .getColumnConfiguration();
+
+ if (visible) {
+ conf.insertColumns(index, 1);
+ } else {
+ conf.removeColumns(index, 1);
+ }
+
+ for (HeaderRow row : grid.getHeader().getRows()) {
+ row.calculateColspans();
+ }
+
+ for (FooterRow row : grid.getFooter().getRows()) {
+ row.calculateColspans();
+ }
+ }
+ }
+
+ /**
+ * Returns the data that should be rendered into the cell. By default
+ * returning Strings and Widgets are supported. If the return type is a
+ * String then it will be treated as preformatted text.
+ * <p>
+ * To support other types you will need to pass a custom renderer to the
+ * column via the column constructor.
+ *
+ * @param row
+ * The row object that provides the cell content.
+ *
+ * @return The cell content
+ */
+ public abstract C getValue(T row);
+
+ /**
+ * The renderer to render the cell width. By default renders the data as
+ * a String or adds the widget into the cell if the column type is of
+ * widget type.
+ *
+ * @return The renderer to render the cell content with
+ */
+ public Renderer<? super C> getRenderer() {
+ return bodyRenderer;
+ }
+
+ /**
+ * Finds the index of this column instance
+ *
+ */
+ private int findIndexOfColumn() {
+ return grid.findVisibleColumnIndex((GridColumn<?, T>) this);
+ }
+
+ /**
+ * Sets the pixel width of the column. Use a negative value for the grid
+ * to autosize column based on content and available space
+ *
+ * @param pixels
+ * the width in pixels or negative for auto sizing
+ */
+ public void setWidth(int pixels) {
+ width = pixels;
+
+ if (grid != null && isVisible()) {
+ int index = findIndexOfColumn();
+ ColumnConfiguration conf = grid.escalator
+ .getColumnConfiguration();
+ conf.setColumnWidth(index, pixels);
+ }
+ }
+
+ /**
+ * Returns the pixel width of the column
+ *
+ * @return pixel width of the column
+ */
+ public int getWidth() {
+ return width;
+ }
+
+ /**
+ * Enables sort indicators for the grid.
+ * <p>
+ * <b>Note:</b>The API can still sort the column even if this is set to
+ * <code>false</code>.
+ *
+ * @param sortable
+ * <code>true</code> when column sort indicators are visible.
+ */
+ public void setSortable(boolean sortable) {
+ if (this.sortable != sortable) {
+ this.sortable = sortable;
+ grid.refreshHeader();
+ }
+ }
+
+ /**
+ * Are sort indicators shown for the column.
+ *
+ * @return <code>true</code> if the column is sortable
+ */
+ public boolean isSortable() {
+ return sortable;
+ }
+ }
+
+ protected class BodyUpdater implements EscalatorUpdater {
+
+ @Override
+ public void preAttach(Row row, Iterable<FlyweightCell> cellsToAttach) {
+ for (FlyweightCell cell : cellsToAttach) {
+ Renderer<?> renderer = findRenderer(cell);
+ if (renderer instanceof ComplexRenderer) {
+ ((ComplexRenderer<?>) renderer).init(cell);
+ }
+ }
+ }
+
+ @Override
+ public void postAttach(Row row, Iterable<FlyweightCell> attachedCells) {
+ for (FlyweightCell cell : attachedCells) {
+ Renderer<?> renderer = findRenderer(cell);
+ if (renderer instanceof WidgetRenderer) {
+ WidgetRenderer<?, ?> widgetRenderer = (WidgetRenderer<?, ?>) renderer;
+
+ Widget widget = widgetRenderer.createWidget();
+ assert widget != null : "WidgetRenderer.createWidget() returned null. It should return a widget.";
+ assert widget.getParent() == null : "WidgetRenderer.createWidget() returned a widget which already is attached.";
+ assert cell.getElement().getChildCount() == 0 : "Cell content should be empty when adding Widget";
+
+ // Physical attach
+ cell.getElement().appendChild(widget.getElement());
+
+ // Logical attach
+ setParent(widget, Grid.this);
+ }
+ }
+ }
+
+ @Override
+ public void update(Row row, Iterable<FlyweightCell> cellsToUpdate) {
+ int rowIndex = row.getRow();
+ Element rowElement = row.getElement();
+ T rowData = dataSource.getRow(rowIndex);
+
+ boolean hasData = rowData != null;
+
+ // Assign stylename for rows with data
+ boolean usedToHaveData = rowElement
+ .hasClassName(rowHasDataStyleName);
+
+ if (usedToHaveData != hasData) {
+ setStyleName(rowElement, rowHasDataStyleName, hasData);
+ }
+
+ // Assign stylename for selected rows
+ if (hasData) {
+ setStyleName(rowElement, rowSelectedStyleName,
+ isSelected(rowData));
+ } else if (usedToHaveData) {
+ setStyleName(rowElement, rowSelectedStyleName, false);
+ }
+
+ activeCellHandler.updateActiveRowStyle(row);
+
+ for (FlyweightCell cell : cellsToUpdate) {
+ GridColumn<?, T> column = getColumnFromVisibleIndex(cell
+ .getColumn());
+
+ assert column != null : "Column was not found from cell ("
+ + cell.getColumn() + "," + cell.getRow() + ")";
+
+ activeCellHandler.updateActiveCellStyle(cell,
+ escalator.getBody());
+
+ Renderer renderer = column.getRenderer();
+
+ // Hide cell content if needed
+ if (renderer instanceof ComplexRenderer) {
+ ComplexRenderer clxRenderer = (ComplexRenderer) renderer;
+ if (hasData) {
+ if (!usedToHaveData) {
+ // Prepare cell for rendering
+ clxRenderer.setContentVisible(cell, true);
+ }
+
+ Object value = column.getValue(rowData);
+ clxRenderer.render(cell, value);
+
+ } else {
+ // Prepare cell for no data
+ clxRenderer.setContentVisible(cell, false);
+ }
+
+ } else if (hasData) {
+ // Simple renderers just render
+ Object value = column.getValue(rowData);
+ renderer.render(cell, value);
+
+ } else {
+ // Clear cell if there is no data
+ cell.getElement().removeAllChildren();
+ }
+ }
+ }
+
+ @Override
+ public void preDetach(Row row, Iterable<FlyweightCell> cellsToDetach) {
+ for (FlyweightCell cell : cellsToDetach) {
+ Renderer renderer = findRenderer(cell);
+ if (renderer instanceof WidgetRenderer) {
+ Widget w = Util.findWidget(cell.getElement()
+ .getFirstChildElement(), Widget.class);
+ if (w != null) {
+
+ // Logical detach
+ setParent(w, null);
+
+ // Physical detach
+ cell.getElement().removeChild(w.getElement());
+ }
+ }
+ }
+ }
+
+ @Override
+ public void postDetach(Row row, Iterable<FlyweightCell> detachedCells) {
+ for (FlyweightCell cell : detachedCells) {
+ Renderer renderer = findRenderer(cell);
+ if (renderer instanceof ComplexRenderer) {
+ ((ComplexRenderer) renderer).destroy(cell);
+ }
+ }
+ }
+ }
+
+ protected class StaticSectionUpdater implements EscalatorUpdater {
+
+ private GridStaticSection<?> section;
+ private RowContainer container;
+
+ public StaticSectionUpdater(GridStaticSection<?> section,
+ RowContainer container) {
+ super();
+ this.section = section;
+ this.container = container;
+ }
+
+ @Override
+ public void update(Row row, Iterable<FlyweightCell> cellsToUpdate) {
+ GridStaticSection.StaticRow<?> staticRow = section.getRow(row
+ .getRow());
+
+ final List<Integer> columnIndices = getVisibleColumnIndices();
+
+ for (FlyweightCell cell : cellsToUpdate) {
+
+ int index = columnIndices.get(cell.getColumn());
+ final StaticCell metadata = staticRow.getCell(index);
+
+ // Decorate default row with sorting indicators
+ if (staticRow instanceof HeaderRow) {
+ addSortingIndicatorsToHeaderRow((HeaderRow) staticRow, cell);
+ }
+
+ // Assign colspan to cell before rendering
+ cell.setColSpan(metadata.getColspan());
+
+ switch (metadata.getType()) {
+ case TEXT:
+ cell.getElement().setInnerText(metadata.getText());
+ break;
+ case HTML:
+ cell.getElement().setInnerHTML(metadata.getHtml());
+ break;
+ case WIDGET:
+ preDetach(row, Arrays.asList(cell));
+ cell.getElement().setInnerHTML("");
+ postAttach(row, Arrays.asList(cell));
+ break;
+ }
+
+ activeCellHandler.updateActiveCellStyle(cell, container);
+ }
+ }
+
+ private void addSortingIndicatorsToHeaderRow(HeaderRow headerRow,
+ FlyweightCell cell) {
+
+ cleanup(cell);
+
+ GridColumn<?, ?> column = getColumnFromVisibleIndex(cell
+ .getColumn());
+ SortOrder sortingOrder = getSortOrder(column);
+ if (!headerRow.isDefault() || !column.isSortable()
+ || sortingOrder == null) {
+ // Only apply sorting indicators to sortable header columns in
+ // the default header row
+ return;
+ }
+
+ Element cellElement = cell.getElement();
+
+ if (SortDirection.ASCENDING == sortingOrder.getDirection()) {
+ cellElement.addClassName("sort-asc");
+ } else {
+ cellElement.addClassName("sort-desc");
+ }
+
+ int sortIndex = Grid.this.getSortOrder().indexOf(sortingOrder);
+ if (sortIndex > -1 && Grid.this.getSortOrder().size() > 1) {
+ // Show sort order indicator if column is
+ // sorted and other sorted columns also exists.
+ cellElement.setAttribute("sort-order",
+ String.valueOf(sortIndex + 1));
+ }
+ }
+
+ /**
+ * Finds the sort order for this column
+ */
+ private SortOrder getSortOrder(GridColumn<?, ?> column) {
+ for (SortOrder order : Grid.this.getSortOrder()) {
+ if (order.getColumn() == column) {
+ return order;
+ }
+ }
+ return null;
+ }
+
+ private void cleanup(FlyweightCell cell) {
+ Element cellElement = cell.getElement();
+ cellElement.removeAttribute("sort-order");
+ cellElement.removeClassName("sort-desc");
+ cellElement.removeClassName("sort-asc");
+ }
+
+ @Override
+ public void preAttach(Row row, Iterable<FlyweightCell> cellsToAttach) {
+ }
+
+ @Override
+ public void postAttach(Row row, Iterable<FlyweightCell> attachedCells) {
+ GridStaticSection.StaticRow<?> gridRow = section.getRow(row
+ .getRow());
+ List<Integer> columnIndices = getVisibleColumnIndices();
+
+ for (FlyweightCell cell : attachedCells) {
+ int index = columnIndices.get(cell.getColumn());
+ StaticCell metadata = gridRow.getCell(index);
+ /*
+ * If the cell contains widgets that are not currently attach
+ * then attach them now.
+ */
+ if (GridStaticCellType.WIDGET.equals(metadata.getType())) {
+ final Widget widget = metadata.getWidget();
+ final Element cellElement = cell.getElement();
+
+ if (!widget.isAttached()) {
+
+ // Physical attach
+ cellElement.appendChild(widget.getElement());
+
+ // Logical attach
+ setParent(widget, Grid.this);
+
+ getLogger().info("Attached widget " + widget);
+ }
+ }
+ }
+ }
+
+ @Override
+ public void preDetach(Row row, Iterable<FlyweightCell> cellsToDetach) {
+ if (section.getRowCount() > row.getRow()) {
+ GridStaticSection.StaticRow<?> gridRow = section.getRow(row
+ .getRow());
+ List<Integer> columnIndices = getVisibleColumnIndices();
+ for (FlyweightCell cell : cellsToDetach) {
+ int index = columnIndices.get(cell.getColumn());
+ StaticCell metadata = gridRow.getCell(index);
+
+ if (GridStaticCellType.WIDGET.equals(metadata.getType())
+ && metadata.getWidget().isAttached()) {
+
+ Widget widget = metadata.getWidget();
+
+ // Logical detach
+ setParent(widget, null);
+
+ // Physical detach
+ widget.getElement().removeFromParent();
+
+ getLogger().info("Detached widget " + widget);
+ }
+ }
+ }
+ }
+
+ @Override
+ public void postDetach(Row row, Iterable<FlyweightCell> detachedCells) {
+ }
+
+ private List<Integer> getVisibleColumnIndices() {
+ List<Integer> indices = new ArrayList<Integer>(getColumnCount());
+ for (int i = 0; i < getColumnCount(); i++) {
+ if (getColumn(i).isVisible()) {
+ indices.add(i);
+ }
+ }
+ return indices;
+ }
+ };
+
+ /**
+ * Creates a new instance.
+ */
+ public Grid() {
+ initWidget(escalator);
+ getElement().setTabIndex(0);
+ activeCellHandler = new ActiveCellHandler();
+
+ setStylePrimaryName("v-grid");
+
+ escalator.getHeader().setEscalatorUpdater(createHeaderUpdater());
+ escalator.getBody().setEscalatorUpdater(createBodyUpdater());
+ escalator.getFooter().setEscalatorUpdater(createFooterUpdater());
+
+ header.setGrid(this);
+ HeaderRow defaultRow = header.appendRow();
+ header.setDefaultRow(defaultRow);
+
+ footer.setGrid(this);
+
+ setSelectionMode(SelectionMode.SINGLE);
+
+ escalator
+ .addRowVisibilityChangeHandler(new RowVisibilityChangeHandler() {
+ @Override
+ public void onRowVisibilityChange(
+ RowVisibilityChangeEvent event) {
+ if (dataSource != null) {
+ dataSource.ensureAvailability(
+ event.getFirstVisibleRow(),
+ event.getVisibleRowCount());
+ }
+ }
+ });
+
+ // Default action on SelectionChangeEvents. Refresh the body so changed
+ // become visible.
+ addSelectionChangeHandler(new SelectionChangeHandler<T>() {
+
+ @Override
+ public void onSelectionChange(SelectionChangeEvent<T> event) {
+ refreshBody();
+ }
+ });
+
+ // Sink header events and key events
+ sinkEvents(getHeader().getConsumedEvents());
+ sinkEvents(Arrays.asList(BrowserEvents.KEYDOWN, BrowserEvents.KEYUP,
+ BrowserEvents.KEYPRESS));
+ }
+
+ @Override
+ public void setStylePrimaryName(String style) {
+ super.setStylePrimaryName(style);
+ escalator.setStylePrimaryName(style);
+ rowHasDataStyleName = getStylePrimaryName() + "-row-has-data";
+ rowSelectedStyleName = getStylePrimaryName() + "-row-selected";
+ cellActiveStyleName = getStylePrimaryName() + "-cell-active";
+ headerFooterActiveStyleName = getStylePrimaryName() + "-header-active";
+ rowActiveStyleName = getStylePrimaryName() + "-row-active";
+
+ if (isAttached()) {
+ refreshHeader();
+ refreshBody();
+ refreshFooter();
+ }
+ }
+
+ /**
+ * Creates the escalator updater used to update the header rows in this
+ * grid. The updater is invoked when header rows or columns are added or
+ * removed, or the content of existing header cells is changed.
+ *
+ * @return the new header updater instance
+ *
+ * @see GridHeader
+ * @see Grid#getHeader()
+ */
+ protected EscalatorUpdater createHeaderUpdater() {
+ return new StaticSectionUpdater(header, escalator.getHeader());
+ }
+
+ /**
+ * Creates the escalator updater used to update the body rows in this grid.
+ * The updater is invoked when body rows or columns are added or removed,
+ * the content of body cells is changed, or the body is scrolled to expose
+ * previously hidden content.
+ *
+ * @return the new body updater instance
+ */
+ protected EscalatorUpdater createBodyUpdater() {
+ return new BodyUpdater();
+ }
+
+ /**
+ * Creates the escalator updater used to update the footer rows in this
+ * grid. The updater is invoked when header rows or columns are added or
+ * removed, or the content of existing header cells is changed.
+ *
+ * @return the new footer updater instance
+ *
+ * @see GridFooter
+ * @see #getFooter()
+ */
+ protected EscalatorUpdater createFooterUpdater() {
+ return new StaticSectionUpdater(footer, escalator.getFooter());
+ }
+
+ /**
+ * Refreshes header or footer rows on demand
+ *
+ * @param rows
+ * The row container
+ * @param firstRowIsVisible
+ * is the first row visible
+ * @param isHeader
+ * <code>true</code> if we refreshing the header, else assumed
+ * the footer
+ */
+ private void refreshRowContainer(RowContainer rows,
+ GridStaticSection<?> section) {
+
+ // Add or Remove rows on demand
+ int rowDiff = section.getVisibleRowCount() - rows.getRowCount();
+ if (rowDiff > 0) {
+ rows.insertRows(0, rowDiff);
+ } else if (rowDiff < 0) {
+ rows.removeRows(0, -rowDiff);
+ }
+
+ // Refresh all the rows
+ if (rows.getRowCount() > 0) {
+ rows.refreshRows(0, rows.getRowCount());
+ }
+ }
+
+ /**
+ * Refreshes all header rows
+ */
+ void refreshHeader() {
+ refreshRowContainer(escalator.getHeader(), header);
+ }
+
+ /**
+ * Refreshes all body rows
+ */
+ private void refreshBody() {
+ escalator.getBody().refreshRows(0, escalator.getBody().getRowCount());
+ }
+
+ /**
+ * Refreshes all footer rows
+ */
+ void refreshFooter() {
+ refreshRowContainer(escalator.getFooter(), footer);
+ }
+
+ /**
+ * Adds a column as the last column in the grid.
+ *
+ * @param column
+ * the column to add
+ */
+ public void addColumn(GridColumn<?, T> column) {
+ addColumn(column, getColumnCount());
+ }
+
+ /**
+ * Inserts a column into a specific position in the grid.
+ *
+ * @param index
+ * the index where the column should be inserted into
+ * @param column
+ * the column to add
+ * @throws IllegalStateException
+ * if Grid's current selection model renders a selection column,
+ * and {@code index} is 0.
+ */
+ public void addColumn(GridColumn<?, T> column, int index) {
+ if (column == selectionColumn) {
+ throw new IllegalArgumentException("The selection column many "
+ + "not be added manually");
+ } else if (selectionColumn != null && index == 0) {
+ throw new IllegalStateException("A column cannot be inserted "
+ + "before the selection column");
+ }
+
+ addColumnSkipSelectionColumnCheck(column, index);
+ }
+
+ private void addColumnSkipSelectionColumnCheck(GridColumn<?, T> column,
+ int index) {
+ // Register column with grid
+ columns.add(index, column);
+
+ header.addColumn(column, index);
+ footer.addColumn(column, index);
+
+ // Register this grid instance with the column
+ ((AbstractGridColumn<?, T>) column).setGrid(this);
+
+ // Insert column into escalator
+ if (column.isVisible()) {
+ int visibleIndex = findVisibleColumnIndex(column);
+ ColumnConfiguration conf = escalator.getColumnConfiguration();
+
+ // Insert column
+ conf.insertColumns(visibleIndex, 1);
+
+ // Transfer column width from column object to escalator
+ conf.setColumnWidth(visibleIndex, column.getWidth());
+ }
+
+ if (lastFrozenColumn != null
+ && ((AbstractGridColumn<?, T>) lastFrozenColumn)
+ .findIndexOfColumn() < index) {
+ refreshFrozenColumns();
+ }
+
+ // Sink all renderer events
+ Set<String> events = new HashSet<String>();
+ events.addAll(getConsumedEventsForRenderer(column.getRenderer()));
+
+ sinkEvents(events);
+ }
+
+ private void sinkEvents(Collection<String> events) {
+ assert events != null;
+
+ int eventsToSink = 0;
+ for (String typeName : events) {
+ int typeInt = Event.getTypeInt(typeName);
+ if (typeInt < 0) {
+ // Type not recognized by typeInt
+ sinkBitlessEvent(typeName);
+ } else {
+ eventsToSink |= typeInt;
+ }
+ }
+
+ if (eventsToSink > 0) {
+ sinkEvents(eventsToSink);
+ }
+ }
+
+ private int findVisibleColumnIndex(GridColumn<?, T> column) {
+ int idx = 0;
+ for (GridColumn<?, T> c : columns) {
+ if (c == column) {
+ return idx;
+ } else if (c.isVisible()) {
+ idx++;
+ }
+ }
+ return -1;
+ }
+
+ private GridColumn<?, T> getColumnFromVisibleIndex(int index) {
+ int idx = -1;
+ for (GridColumn<?, T> c : columns) {
+ if (c.isVisible()) {
+ idx++;
+ }
+ if (index == idx) {
+ return c;
+ }
+ }
+ return null;
+ }
+
+ private Renderer<?> findRenderer(FlyweightCell cell) {
+ GridColumn<?, T> column = getColumnFromVisibleIndex(cell.getColumn());
+ assert column != null : "Could not find column at index:"
+ + cell.getColumn();
+ return column.getRenderer();
+ }
+
+ /**
+ * Removes a column from the grid.
+ *
+ * @param column
+ * the column to remove
+ */
+ public void removeColumn(GridColumn<?, T> column) {
+ if (column != null && column.equals(selectionColumn)) {
+ throw new IllegalArgumentException(
+ "The selection column may not be removed manually.");
+ }
+
+ removeColumnSkipSelectionColumnCheck(column);
+ }
+
+ private void removeColumnSkipSelectionColumnCheck(GridColumn<?, T> column) {
+ int columnIndex = columns.indexOf(column);
+ int visibleIndex = findVisibleColumnIndex(column);
+ columns.remove(columnIndex);
+
+ header.removeColumn(columnIndex);
+ footer.removeColumn(columnIndex);
+
+ // de-register column with grid
+ ((AbstractGridColumn<?, T>) column).setGrid(null);
+
+ if (column.isVisible()) {
+ ColumnConfiguration conf = escalator.getColumnConfiguration();
+ conf.removeColumns(visibleIndex, 1);
+ }
+
+ if (column.equals(lastFrozenColumn)) {
+ setLastFrozenColumn(null);
+ } else {
+ refreshFrozenColumns();
+ }
+ }
+
+ /**
+ * Returns the amount of columns in the grid.
+ *
+ * @return The number of columns in the grid
+ */
+ public int getColumnCount() {
+ return columns.size();
+ }
+
+ /**
+ * Returns a list of columns in the grid.
+ *
+ * @return A unmodifiable list of the columns in the grid
+ */
+ public List<GridColumn<?, T>> getColumns() {
+ return Collections.unmodifiableList(new ArrayList<GridColumn<?, T>>(
+ columns));
+ }
+
+ /**
+ * Returns a column by its index in the grid.
+ *
+ * @param index
+ * the index of the column
+ * @return The column in the given index
+ * @throws IllegalArgumentException
+ * if the column index does not exist in the grid
+ */
+ public GridColumn<?, T> getColumn(int index)
+ throws IllegalArgumentException {
+ if (index < 0 || index >= columns.size()) {
+ throw new IllegalStateException("Column not found.");
+ }
+ return columns.get(index);
+ }
+
+ /**
+ * Returns the header section of this grid. The default header contains a
+ * single row displaying the column captions.
+ *
+ * @return the header
+ */
+ public GridHeader getHeader() {
+ return header;
+ }
+
+ /**
+ * Returns the footer section of this grid. The default footer is empty.
+ *
+ * @return the footer
+ */
+ public GridFooter getFooter() {
+ return footer;
+ }
+
+ /**
+ * {@inheritDoc}
+ * <p>
+ * <em>Note:</em> This method will change the widget's size in the browser
+ * only if {@link #getHeightMode()} returns {@link HeightMode#CSS}.
+ *
+ * @see #setHeightMode(HeightMode)
+ */
+ @Override
+ public void setHeight(String height) {
+ escalator.setHeight(height);
+ }
+
+ @Override
+ public void setWidth(String width) {
+ escalator.setWidth(width);
+ }
+
+ /**
+ * Sets the data source used by this grid.
+ *
+ * @param dataSource
+ * the data source to use, not null
+ * @throws IllegalArgumentException
+ * if <code>dataSource</code> is <code>null</code>
+ */
+ public void setDataSource(DataSource<T> dataSource)
+ throws IllegalArgumentException {
+ if (dataSource == null) {
+ throw new IllegalArgumentException("dataSource can't be null.");
+ }
+
+ selectionModel.reset();
+
+ if (this.dataSource != null) {
+ this.dataSource.setDataChangeHandler(null);
+ }
+
+ this.dataSource = dataSource;
+ dataSource.setDataChangeHandler(new DataChangeHandler() {
+ @Override
+ public void dataUpdated(int firstIndex, int numberOfItems) {
+ escalator.getBody().refreshRows(firstIndex, numberOfItems);
+ }
+
+ @Override
+ public void dataRemoved(int firstIndex, int numberOfItems) {
+ escalator.getBody().removeRows(firstIndex, numberOfItems);
+ }
+
+ @Override
+ public void dataAdded(int firstIndex, int numberOfItems) {
+ escalator.getBody().insertRows(firstIndex, numberOfItems);
+ }
+
+ @Override
+ public void dataAvailable(int firstIndex, int numberOfItems) {
+ currentDataAvailable = Range.withLength(firstIndex,
+ numberOfItems);
+ fireEvent(new DataAvailableEvent(currentDataAvailable));
+ }
+ });
+
+ int previousRowCount = escalator.getBody().getRowCount();
+ if (previousRowCount != 0) {
+ escalator.getBody().removeRows(0, previousRowCount);
+ }
+
+ int estimatedSize = dataSource.getEstimatedSize();
+ if (estimatedSize > 0) {
+ escalator.getBody().insertRows(0, estimatedSize);
+ }
+
+ }
+
+ /**
+ * Gets the {@Link DataSource} for this Grid.
+ *
+ * @return the data source used by this grid
+ */
+ public DataSource<T> getDataSource() {
+ return dataSource;
+ }
+
+ /**
+ * Sets the rightmost frozen column in the grid.
+ * <p>
+ * All columns up to and including the given column will be frozen in place
+ * when the grid is scrolled sideways.
+ *
+ * @param lastFrozenColumn
+ * the rightmost column to freeze, or <code>null</code> to not
+ * have any columns frozen
+ * @throws IllegalArgumentException
+ * if {@code lastFrozenColumn} is not a column from this grid
+ */
+ public void setLastFrozenColumn(GridColumn<?, T> lastFrozenColumn) {
+ this.lastFrozenColumn = lastFrozenColumn;
+ refreshFrozenColumns();
+ }
+
+ private void refreshFrozenColumns() {
+ final int frozenCount;
+ if (lastFrozenColumn != null) {
+ frozenCount = columns.indexOf(lastFrozenColumn) + 1;
+ if (frozenCount == 0) {
+ throw new IllegalArgumentException(
+ "The given column isn't attached to this grid");
+ }
+ } else {
+ frozenCount = 0;
+ }
+
+ escalator.getColumnConfiguration().setFrozenColumnCount(frozenCount);
+ }
+
+ /**
+ * Gets the rightmost frozen column in the grid.
+ * <p>
+ * <em>Note:</em> Most usually, this method returns the very value set with
+ * {@link #setLastFrozenColumn(GridColumn)}. This value, however, can be
+ * reset to <code>null</code> if the column is removed from this grid.
+ *
+ * @return the rightmost frozen column in the grid, or <code>null</code> if
+ * no columns are frozen.
+ */
+ public GridColumn<?, T> getLastFrozenColumn() {
+ return lastFrozenColumn;
+ }
+
+ public HandlerRegistration addRowVisibilityChangeHandler(
+ RowVisibilityChangeHandler handler) {
+ /*
+ * Reusing Escalator's RowVisibilityChangeHandler, since a scroll
+ * concept is too abstract. e.g. the event needs to be re-sent when the
+ * widget is resized.
+ */
+ return escalator.addRowVisibilityChangeHandler(handler);
+ }
+
+ /**
+ * Scrolls to a certain row, using {@link ScrollDestination#ANY}.
+ *
+ * @param rowIndex
+ * zero-based index of the row to scroll to.
+ * @throws IllegalArgumentException
+ * if rowIndex is below zero, or above the maximum value
+ * supported by the data source.
+ */
+ public void scrollToRow(int rowIndex) throws IllegalArgumentException {
+ scrollToRow(rowIndex, ScrollDestination.ANY,
+ GridConstants.DEFAULT_PADDING);
+ }
+
+ /**
+ * Scrolls to a certain row, using user-specified scroll destination.
+ *
+ * @param rowIndex
+ * zero-based index of the row to scroll to.
+ * @param destination
+ * desired destination placement of scrolled-to-row. See
+ * {@link ScrollDestination} for more information.
+ * @throws IllegalArgumentException
+ * if rowIndex is below zero, or above the maximum value
+ * supported by the data source.
+ */
+ public void scrollToRow(int rowIndex, ScrollDestination destination)
+ throws IllegalArgumentException {
+ scrollToRow(rowIndex, destination,
+ destination == ScrollDestination.MIDDLE ? 0
+ : GridConstants.DEFAULT_PADDING);
+ }
+
+ /**
+ * Scrolls to a certain row using only user-specified parameters.
+ *
+ * @param rowIndex
+ * zero-based index of the row to scroll to.
+ * @param destination
+ * desired destination placement of scrolled-to-row. See
+ * {@link ScrollDestination} for more information.
+ * @param paddingPx
+ * number of pixels to overscroll. Behavior depends on
+ * destination.
+ * @throws IllegalArgumentException
+ * if {@code destination} is {@link ScrollDestination#MIDDLE}
+ * and padding is nonzero, because having a padding on a
+ * centered row is undefined behavior, or if rowIndex is below
+ * zero or above the row count of the data source.
+ */
+ private void scrollToRow(int rowIndex, ScrollDestination destination,
+ int paddingPx) throws IllegalArgumentException {
+ int maxsize = escalator.getBody().getRowCount() - 1;
+
+ if (rowIndex < 0) {
+ throw new IllegalArgumentException("Row index (" + rowIndex
+ + ") is below zero!");
+ }
+
+ if (rowIndex > maxsize) {
+ throw new IllegalArgumentException("Row index (" + rowIndex
+ + ") is above maximum (" + maxsize + ")!");
+ }
+
+ escalator.scrollToRow(rowIndex, destination, paddingPx);
+ }
+
+ /**
+ * Scrolls to the beginning of the very first row.
+ */
+ public void scrollToStart() {
+ scrollToRow(0, ScrollDestination.START);
+ }
+
+ /**
+ * Scrolls to the end of the very last row.
+ */
+ public void scrollToEnd() {
+ scrollToRow(escalator.getBody().getRowCount() - 1,
+ ScrollDestination.END);
+ }
+
+ /**
+ * Sets the vertical scroll offset.
+ *
+ * @param px
+ * the number of pixels this grid should be scrolled down
+ */
+ public void setScrollTop(double px) {
+ escalator.setScrollTop(px);
+ }
+
+ /**
+ * Gets the vertical scroll offset
+ *
+ * @return the number of pixels this grid is scrolled down
+ */
+ public double getScrollTop() {
+ return escalator.getScrollTop();
+ }
+
+ private static final Logger getLogger() {
+ return Logger.getLogger(Grid.class.getName());
+ }
+
+ /**
+ * Sets the number of rows that should be visible in Grid's body, while
+ * {@link #getHeightMode()} is {@link HeightMode#ROW}.
+ * <p>
+ * If Grid is currently not in {@link HeightMode#ROW}, the given value is
+ * remembered, and applied once the mode is applied.
+ *
+ * @param rows
+ * The height in terms of number of rows displayed in Grid's
+ * body. If Grid doesn't contain enough rows, white space is
+ * displayed instead.
+ * @throws IllegalArgumentException
+ * if {@code rows} is zero or less
+ * @throws IllegalArgumentException
+ * if {@code rows} is {@link Double#isInifinite(double)
+ * infinite}
+ * @throws IllegalArgumentException
+ * if {@code rows} is {@link Double#isNaN(double) NaN}
+ *
+ * @see #setHeightMode(HeightMode)
+ */
+ public void setHeightByRows(double rows) throws IllegalArgumentException {
+ escalator.setHeightByRows(rows);
+ }
+
+ /**
+ * Gets the amount of rows in Grid's body that are shown, while
+ * {@link #getHeightMode()} is {@link HeightMode#ROW}.
+ * <p>
+ * By default, it is {@value Escalator#DEFAULT_HEIGHT_BY_ROWS}.
+ *
+ * @return the amount of rows that should be shown in Grid's body, while in
+ * {@link HeightMode#ROW}.
+ * @see #setHeightByRows(double)
+ */
+ public double getHeightByRows() {
+ return escalator.getHeightByRows();
+ }
+
+ /**
+ * Defines the mode in which the Grid widget's height is calculated.
+ * <p>
+ * If {@link HeightMode#CSS} is given, Grid will respect the values given
+ * via {@link #setHeight(String)}, and behave as a traditional Widget.
+ * <p>
+ * If {@link HeightMode#ROW} is given, Grid will make sure that the body
+ * will display as many rows as {@link #getHeightByRows()} defines.
+ * <em>Note:</em> If headers/footers are inserted or removed, the widget
+ * will resize itself to still display the required amount of rows in its
+ * body. It also takes the horizontal scrollbar into account.
+ *
+ * @param heightMode
+ * the mode in to which Grid should be set
+ */
+ public void setHeightMode(HeightMode heightMode) {
+ /*
+ * This method is a workaround for the fact that Vaadin re-applies
+ * widget dimensions (height/width) on each state change event. The
+ * original design was to have setHeight an setHeightByRow be equals,
+ * and whichever was called the latest was considered in effect.
+ *
+ * But, because of Vaadin always calling setHeight on the widget, this
+ * approach doesn't work.
+ */
+
+ escalator.setHeightMode(heightMode);
+ }
+
+ /**
+ * Returns the current {@link HeightMode} the Grid is in.
+ * <p>
+ * Defaults to {@link HeightMode#CSS}.
+ *
+ * @return the current HeightMode
+ */
+ public HeightMode getHeightMode() {
+ return escalator.getHeightMode();
+ }
+
+ private Set<String> getConsumedEventsForRenderer(Renderer<?> renderer) {
+ Set<String> events = new HashSet<String>();
+ if (renderer instanceof ComplexRenderer) {
+ Collection<String> consumedEvents = ((ComplexRenderer<?>) renderer)
+ .getConsumedEvents();
+ if (consumedEvents != null) {
+ events.addAll(consumedEvents);
+ }
+ }
+ return events;
+ }
+
+ @Override
+ public void onBrowserEvent(Event event) {
+ super.onBrowserEvent(event);
+ EventTarget target = event.getEventTarget();
+ if (Element.is(target)) {
+ Element e = Element.as(target);
+ RowContainer container = escalator.findRowContainer(e);
+ Cell cell = null;
+ if (container != null) {
+ cell = container.getCell(e);
+ if (cell != null) {
+ // FIXME getFromVisibleIndex???
+ GridColumn<?, T> gridColumn = columns.get(cell.getColumn());
+
+ if (container == escalator.getHeader()) {
+ if (getHeader().getRow(cell.getRow()).isDefault()) {
+ handleDefaultRowEvent(cell, event);
+ }
+ } else if (container == escalator.getFooter()) {
+ // NOP
+ } else if (gridColumn.getRenderer() instanceof ComplexRenderer) {
+ ComplexRenderer<?> cplxRenderer = (ComplexRenderer<?>) gridColumn
+ .getRenderer();
+ if (cplxRenderer.getConsumedEvents().contains(
+ event.getType())) {
+ if (cplxRenderer.onBrowserEvent(cell, event)) {
+ return;
+ }
+ }
+ }
+ }
+ }
+
+ Collection<String> navigation = activeCellHandler
+ .getNavigationEvents();
+ if (navigation.contains(event.getType())
+ && (Util.getFocusedElement() == getElement() || cell != null)) {
+ activeCellHandler.handleNavigationEvent(event, cell);
+ }
+
+ handleGridNavigation(event, cell);
+ }
+ }
+
+ private void handleGridNavigation(Event event, Cell cell) {
+ if (!event.getType().equals(BrowserEvents.KEYDOWN)) {
+ // Only handle key downs
+ return;
+ }
+
+ int newRow = -1;
+ RowContainer container = escalator.getBody();
+ switch (event.getKeyCode()) {
+ case KeyCodes.KEY_HOME:
+ if (container.getRowCount() > 0) {
+ newRow = 0;
+ }
+ break;
+ case KeyCodes.KEY_END:
+ if (container.getRowCount() > 0) {
+ newRow = container.getRowCount() - 1;
+ }
+ break;
+ case KeyCodes.KEY_PAGEUP: {
+ Range range = escalator.getVisibleRowRange();
+ if (!range.isEmpty()) {
+ int firstIndex = getFirstVisibleRowIndex();
+ newRow = firstIndex - range.length();
+ if (newRow < 0) {
+ newRow = 0;
+ }
+ }
+ break;
+ }
+ case KeyCodes.KEY_PAGEDOWN: {
+ Range range = escalator.getVisibleRowRange();
+ if (!range.isEmpty()) {
+ int lastIndex = getLastVisibleRowIndex();
+ newRow = lastIndex + range.length();
+ if (newRow >= container.getRowCount()) {
+ newRow = container.getRowCount() - 1;
+ }
+ }
+ break;
+ }
+ default:
+ return;
+ }
+
+ scrollToRow(newRow);
+ }
+
+ private Point rowEventTouchStartingPoint;
+
+ private boolean handleDefaultRowEvent(final Cell cell, NativeEvent event) {
+ if (!getColumn(cell.getColumn()).isSortable()) {
+ // Only handle sorting events if the column is sortable
+ return false;
+ }
+
+ if (BrowserEvents.TOUCHSTART.equals(event.getType())) {
+ if (event.getTouches().length() > 1) {
+ return false;
+ }
+
+ event.preventDefault();
+
+ Touch touch = event.getChangedTouches().get(0);
+ rowEventTouchStartingPoint = new Point(touch.getClientX(),
+ touch.getClientY());
+
+ lazySorter.setCellReference(cell);
+ lazySorter.setMultisort(true);
+ lazySorter.schedule(GridConstants.LONG_TAP_DELAY);
+
+ } else if (BrowserEvents.TOUCHMOVE.equals(event.getType())) {
+ if (event.getTouches().length() > 1) {
+ return false;
+ }
+
+ event.preventDefault();
+
+ Touch touch = event.getChangedTouches().get(0);
+ double diffX = Math.abs(touch.getClientX()
+ - rowEventTouchStartingPoint.getX());
+ double diffY = Math.abs(touch.getClientY()
+ - rowEventTouchStartingPoint.getY());
+
+ // Cancel long tap if finger strays too far from
+ // starting point
+ if (diffX > GridConstants.LONG_TAP_THRESHOLD
+ || diffY > GridConstants.LONG_TAP_THRESHOLD) {
+ lazySorter.cancel();
+ }
+
+ } else if (BrowserEvents.TOUCHEND.equals(event.getType())) {
+ if (event.getTouches().length() > 0) {
+ return false;
+ }
+
+ if (lazySorter.isRunning()) {
+ // Not a long tap yet, perform single sort
+ lazySorter.cancel();
+ lazySorter.setMultisort(false);
+ lazySorter.run();
+ }
+
+ } else if (BrowserEvents.TOUCHCANCEL.equals(event.getType())) {
+ if (event.getChangedTouches().length() > 1) {
+ return false;
+ }
+
+ lazySorter.cancel();
+
+ } else if (BrowserEvents.CLICK.equals(event.getType())) {
+ lazySorter.setCellReference(cell);
+ lazySorter.setMultisort(event.getShiftKey());
+ lazySorter.run();
+
+ // Active cell handling is also monitoring the click
+ // event so we allow event to propagate for it
+ return false;
+ }
+
+ return true;
+ }
+
+ @Override
+ public com.google.gwt.user.client.Element getSubPartElement(String subPart) {
+ // Parse SubPart string to type and indices
+ String[] splitArgs = subPart.split("\\[");
+
+ String type = splitArgs[0];
+ int[] indices = new int[splitArgs.length - 1];
+ for (int i = 0; i < indices.length; ++i) {
+ String tmp = splitArgs[i + 1];
+ indices[i] = Integer.parseInt(tmp.substring(0, tmp.length() - 1));
+ }
+
+ // Get correct RowContainer for type from Escalator
+ RowContainer container = null;
+ if (type.equalsIgnoreCase("header")) {
+ container = escalator.getHeader();
+ } else if (type.equalsIgnoreCase("cell")) {
+ // If wanted row is not visible, we need to scroll there.
+ Range visibleRowRange = escalator.getVisibleRowRange();
+ if (indices.length > 0 && !visibleRowRange.contains(indices[0])) {
+ try {
+ scrollToRow(indices[0]);
+ } catch (IllegalArgumentException e) {
+ getLogger().log(Level.SEVERE, e.getMessage());
+ }
+ // Scrolling causes a lazy loading event. No element can
+ // currently be retrieved.
+ return null;
+ }
+ container = escalator.getBody();
+ } else if (type.equalsIgnoreCase("footer")) {
+ container = escalator.getFooter();
+ }
+
+ if (null != container) {
+ if (indices.length == 0) {
+ // No indexing. Just return the wanted container element
+ return DOM.asOld(container.getElement());
+ } else {
+ try {
+ return DOM.asOld(getSubPart(container, indices));
+ } catch (Exception e) {
+ getLogger().log(Level.SEVERE, e.getMessage());
+ }
+ }
+ }
+ return null;
+ }
+
+ private Element getSubPart(RowContainer container, int[] indices) {
+ // Scroll wanted column to view if able
+ if (indices.length > 1
+ && escalator.getColumnConfiguration().getFrozenColumnCount() <= indices[1]) {
+ escalator.scrollToColumn(indices[1], ScrollDestination.ANY, 0);
+ }
+
+ Element targetElement = container.getRowElement(indices[0]);
+ for (int i = 1; i < indices.length && targetElement != null; ++i) {
+ targetElement = (Element) targetElement.getChild(indices[i]);
+ }
+ return targetElement;
+ }
+
+ @Override
+ public String getSubPartName(com.google.gwt.user.client.Element subElement) {
+ // Containers and matching SubPart types
+ List<RowContainer> containers = Arrays.asList(escalator.getHeader(),
+ escalator.getBody(), escalator.getFooter());
+ List<String> containerType = Arrays.asList("header", "cell", "footer");
+
+ for (int i = 0; i < containers.size(); ++i) {
+ RowContainer container = containers.get(i);
+ boolean containerRow = (subElement.getTagName().equalsIgnoreCase(
+ "tr") && subElement.getParentElement() == container
+ .getElement());
+ if (containerRow) {
+ // Wanted SubPart is row that is a child of containers root
+ // To get indices, we use a cell that is a child of this row
+ subElement = DOM.asOld(subElement.getFirstChildElement());
+ }
+
+ Cell cell = container.getCell(subElement);
+ if (cell != null) {
+ // Skip the column index if subElement was a child of root
+ return containerType.get(i) + "[" + cell.getRow()
+ + (containerRow ? "]" : "][" + cell.getColumn() + "]");
+ }
+ }
+ return null;
+ }
+
+ private void setSelectColumnRenderer(
+ final Renderer<Boolean> selectColumnRenderer) {
+ if (this.selectColumnRenderer == selectColumnRenderer) {
+ return;
+ }
+
+ if (this.selectColumnRenderer != null) {
+ removeColumnSkipSelectionColumnCheck(selectionColumn);
+ --activeCellHandler.activeColumn;
+ }
+
+ this.selectColumnRenderer = selectColumnRenderer;
+
+ if (selectColumnRenderer != null) {
+ ++activeCellHandler.activeColumn;
+ selectionColumn = new SelectionColumn(selectColumnRenderer);
+
+ // FIXME: this needs to be done elsewhere, requires design...
+ selectionColumn.setWidth(25);
+ addColumnSkipSelectionColumnCheck(selectionColumn, 0);
+ selectionColumn.initDone();
+ } else {
+ selectionColumn = null;
+ refreshBody();
+ }
+ }
+
+ /**
+ * Accesses the package private method Widget#setParent()
+ *
+ * @param widget
+ * The widget to access
+ * @param parent
+ * The parent to set
+ */
+ private static native final void setParent(Widget widget, Widget parent)
+ /*-{
+ widget.@com.google.gwt.user.client.ui.Widget::setParent(Lcom/google/gwt/user/client/ui/Widget;)(parent);
+ }-*/;
+
+ /**
+ * Sets the current selection model.
+ * <p>
+ * This function will call {@link SelectionModel#setGrid(Grid)}.
+ *
+ * @param selectionModel
+ * a selection model implementation.
+ * @throws IllegalArgumentException
+ * if selection model argument is null
+ */
+ public void setSelectionModel(SelectionModel<T> selectionModel) {
+
+ if (selectionModel == null) {
+ throw new IllegalArgumentException("Selection model can't be null");
+ }
+
+ if (selectColumnRenderer != null
+ && selectColumnRenderer instanceof ComplexRenderer) {
+ ((ComplexRenderer<?>) selectColumnRenderer).destroy();
+ }
+
+ this.selectionModel = selectionModel;
+ selectionModel.setGrid(this);
+ setSelectColumnRenderer(this.selectionModel
+ .getSelectionColumnRenderer());
+ }
+
+ /**
+ * Gets a reference to the current selection model.
+ *
+ * @return the currently used SelectionModel instance.
+ */
+ public SelectionModel<T> getSelectionModel() {
+ return selectionModel;
+ }
+
+ /**
+ * Sets current selection mode.
+ * <p>
+ * This is a shorthand method for {@link Grid#setSelectionModel}.
+ *
+ * @param mode
+ * a selection mode value
+ * @see {@link SelectionMode}.
+ */
+ public void setSelectionMode(SelectionMode mode) {
+ SelectionModel<T> model = mode.createModel();
+ setSelectionModel(model);
+ }
+
+ /**
+ * Test if a row is selected.
+ *
+ * @param row
+ * a row object
+ * @return true, if the current selection model considers the provided row
+ * object selected.
+ */
+ public boolean isSelected(T row) {
+ return selectionModel.isSelected(row);
+ }
+
+ /**
+ * Select a row using the current selection model.
+ * <p>
+ * Only selection models implementing {@link SelectionModel.Single} and
+ * {@link SelectionModel.Multi} are supported; for anything else, an
+ * exception will be thrown.
+ *
+ * @param row
+ * a row object
+ * @return <code>true</code> iff the current selection changed
+ * @throws IllegalStateException
+ * if the current selection model is not an instance of
+ * {@link SelectionModel.Single} or {@link SelectionModel.Multi}
+ */
+ @SuppressWarnings("unchecked")
+ public boolean select(T row) {
+ if (selectionModel instanceof SelectionModel.Single<?>) {
+ return ((SelectionModel.Single<T>) selectionModel).select(row);
+ } else if (selectionModel instanceof SelectionModel.Multi<?>) {
+ return ((SelectionModel.Multi<T>) selectionModel).select(row);
+ } else {
+ throw new IllegalStateException("Unsupported selection model");
+ }
+ }
+
+ /**
+ * Deselect a row using the current selection model.
+ * <p>
+ * Only selection models implementing {@link SelectionModel.Single} and
+ * {@link SelectionModel.Multi} are supported; for anything else, an
+ * exception will be thrown.
+ *
+ * @param row
+ * a row object
+ * @return <code>true</code> iff the current selection changed
+ * @throws IllegalStateException
+ * if the current selection model is not an instance of
+ * {@link SelectionModel.Single} or {@link SelectionModel.Multi}
+ */
+ @SuppressWarnings("unchecked")
+ public boolean deselect(T row) {
+ if (selectionModel instanceof SelectionModel.Single<?>) {
+ return ((SelectionModel.Single<T>) selectionModel).deselect(row);
+ } else if (selectionModel instanceof SelectionModel.Multi<?>) {
+ return ((SelectionModel.Multi<T>) selectionModel).deselect(row);
+ } else {
+ throw new IllegalStateException("Unsupported selection model");
+ }
+ }
+
+ /**
+ * Gets last selected row from the current SelectionModel.
+ * <p>
+ * Only selection models implementing {@link SelectionModel.Single} are
+ * valid for this method; for anything else, use the
+ * {@link Grid#getSelectedRows()} method.
+ *
+ * @return a selected row reference, or null, if no row is selected
+ * @throws IllegalStateException
+ * if the current selection model is not an instance of
+ * {@link SelectionModel.Single}
+ */
+ public T getSelectedRow() {
+ if (selectionModel instanceof SelectionModel.Single<?>) {
+ return ((SelectionModel.Single<T>) selectionModel).getSelectedRow();
+ } else {
+ throw new IllegalStateException(
+ "Unsupported selection model; can not get single selected row");
+ }
+ }
+
+ /**
+ * Gets currently selected rows from the current selection model.
+ *
+ * @return a non-null collection containing all currently selected rows.
+ */
+ public Collection<T> getSelectedRows() {
+ return selectionModel.getSelectedRows();
+ }
+
+ @Override
+ public HandlerRegistration addSelectionChangeHandler(
+ final SelectionChangeHandler<T> handler) {
+ return addHandler(handler, SelectionChangeEvent.getType());
+ }
+
+ /**
+ * Sets the current sort order using the fluid Sort API. Read the
+ * documentation for {@link Sort} for more information.
+ *
+ * @param s
+ * a sort instance
+ */
+ public void sort(Sort s) {
+ setSortOrder(s.build());
+ }
+
+ /**
+ * Sorts the Grid data in ascending order along one column.
+ *
+ * @param column
+ * a grid column reference
+ */
+ public <C> void sort(GridColumn<C, T> column) {
+ sort(column, SortDirection.ASCENDING);
+ }
+
+ /**
+ * Sorts the Grid data along one column.
+ *
+ * @param column
+ * a grid column reference
+ * @param direction
+ * a sort direction value
+ */
+ public <C> void sort(GridColumn<C, T> column, SortDirection direction) {
+ sort(Sort.by(column, direction));
+ }
+
+ /**
+ * Sets the sort order to use. Setting this causes the Grid to re-sort
+ * itself.
+ *
+ * @param order
+ * a sort order list. If set to null, the sort order is cleared.
+ */
+ public void setSortOrder(List<SortOrder> order) {
+ setSortOrder(order, SortEventOriginator.API);
+ }
+
+ private void setSortOrder(List<SortOrder> order,
+ SortEventOriginator originator) {
+ sortOrder.clear();
+ if (order != null) {
+ sortOrder.addAll(order);
+ }
+ sort(originator);
+ }
+
+ /**
+ * Get a copy of the current sort order array.
+ *
+ * @return a copy of the current sort order array
+ */
+ public List<SortOrder> getSortOrder() {
+ return Collections.unmodifiableList(sortOrder);
+ }
+
+ /**
+ * Finds the sorting order for this column
+ */
+ private SortOrder getSortOrder(GridColumn<?, ?> column) {
+ for (SortOrder order : getSortOrder()) {
+ if (order.getColumn() == column) {
+ return order;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Register a GWT event handler for a sorting event. This handler gets
+ * called whenever this Grid needs its data source to provide data sorted in
+ * a specific order.
+ *
+ * @param handler
+ * a sort event handler
+ * @return the registration for the event
+ */
+ public HandlerRegistration addSortHandler(SortEventHandler<T> handler) {
+ return addHandler(handler, SortEvent.getType());
+ }
+
+ /**
+ * Register a GWT event handler for a data available event. This handler
+ * gets called whenever the {@link DataSource} for this Grid has new data
+ * available.
+ * <p>
+ * This handle will be fired with the current available data after
+ * registration is done.
+ *
+ * @param handler
+ * a data available event handler
+ * @return the registartion for the event
+ */
+ public HandlerRegistration addDataAvailableHandler(
+ final DataAvailableHandler handler) {
+ // Deferred call to handler with current row range
+ Scheduler.get().scheduleFinally(new ScheduledCommand() {
+ @Override
+ public void execute() {
+ handler.onDataAvailable(new DataAvailableEvent(
+ currentDataAvailable));
+ }
+ });
+ return addHandler(handler, DataAvailableEvent.TYPE);
+ }
+
+ /**
+ * Register a KeyDown handler to this Grid. If the handler is a
+ * HeaderKeyDownHandler, it will be fired only when a header cell is active.
+ * The same goes for body and footer with their respective handlers.
+ *
+ * @param handler
+ * the key handler to register
+ * @return the registration for the event
+ */
+ public <HANDLER extends GridKeyDownHandler<T>> HandlerRegistration addKeyDownHandler(
+ HANDLER handler) {
+ if (handler instanceof BodyKeyDownHandler
+ || handler instanceof HeaderKeyDownHandler
+ || handler instanceof FooterKeyDownHandler) {
+ return addHandler(handler, keyDown.getAssociatedType());
+ }
+ throw new IllegalArgumentException(
+ "Handler not a valid extension of GridKeyDownHandler");
+ }
+
+ /**
+ * Register a KeyUp handler to this Grid. If the handler is a
+ * HeaderKeyUpHandler, it will be fired only when a header cell is active.
+ * The same goes for body and footer with their respective handlers.
+ *
+ * @param handler
+ * the key handler to register
+ * @return the registration for the event
+ */
+ public <HANDLER extends GridKeyUpHandler<T>> HandlerRegistration addKeyUpHandler(
+ HANDLER handler) {
+ if (handler instanceof BodyKeyUpHandler
+ || handler instanceof HeaderKeyUpHandler
+ || handler instanceof FooterKeyUpHandler) {
+ return addHandler(handler, keyUp.getAssociatedType());
+ }
+ throw new IllegalArgumentException(
+ "Handler not a valid extension of GridKeyUpHandler");
+ }
+
+ /**
+ * Register a KeyPress handler to this Grid. If the handler is a
+ * HeaderKeyPressHandler, it will be fired only when a header cell is
+ * active. The same goes for body and footer with their respective handlers.
+ *
+ * @param handler
+ * the key handler to register
+ * @return the registration for the event
+ */
+ public <HANDLER extends GridKeyPressHandler<T>> HandlerRegistration addKeyPressHandler(
+ HANDLER handler) {
+ if (handler instanceof BodyKeyPressHandler
+ || handler instanceof HeaderKeyPressHandler
+ || handler instanceof FooterKeyPressHandler) {
+ return addHandler(handler, keyPress.getAssociatedType());
+ }
+ throw new IllegalArgumentException(
+ "Handler not a valid extension of GridKeyPressHandler");
+ }
+
+ /**
+ * Apply sorting to data source.
+ */
+ private void sort(SortEventOriginator originator) {
+ refreshHeader();
+ fireEvent(new SortEvent<T>(this,
+ Collections.unmodifiableList(sortOrder), originator));
+ }
+
+ private int getLastVisibleRowIndex() {
+ int lastRowIndex = escalator.getVisibleRowRange().getEnd();
+ int footerTop = escalator.getFooter().getElement().getAbsoluteTop();
+ Element lastRow;
+
+ do {
+ lastRow = escalator.getBody().getRowElement(--lastRowIndex);
+ } while (lastRow.getAbsoluteBottom() > footerTop);
+
+ return lastRowIndex;
+ }
+
+ private int getFirstVisibleRowIndex() {
+ int firstRowIndex = escalator.getVisibleRowRange().getStart();
+ int headerBottom = escalator.getHeader().getElement()
+ .getAbsoluteBottom();
+ Element firstRow = escalator.getBody().getRowElement(firstRowIndex);
+
+ while (firstRow.getAbsoluteTop() < headerBottom) {
+ firstRow = escalator.getBody().getRowElement(++firstRowIndex);
+ }
+
+ return firstRowIndex;
+ }
+}
diff --git a/client/src/com/vaadin/client/ui/grid/GridColumn.java b/client/src/com/vaadin/client/ui/grid/GridColumn.java
new file mode 100644
index 0000000000..69be2d5532
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/GridColumn.java
@@ -0,0 +1,47 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid;
+
+/**
+ * Represents a column in the {@link Grid}.
+ *
+ * @param <C>
+ * The column type
+ *
+ * @param <T>
+ * The row type
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public abstract class GridColumn<C, T> extends Grid.AbstractGridColumn<C, T> {
+
+ /*
+ * This class is a convenience class so you do not have to reference
+ * Grid.AbstractGridColumn in your production code. The real implementation
+ * should be in the abstract class.
+ */
+
+ /**
+ * Constructs a new column with a custom renderer.
+ *
+ * @param renderer
+ * The renderer to use for rendering the cells
+ */
+ public GridColumn(Renderer<? super C> renderer) {
+ super(renderer);
+ }
+}
diff --git a/client/src/com/vaadin/client/ui/grid/GridConnector.java b/client/src/com/vaadin/client/ui/grid/GridConnector.java
new file mode 100644
index 0000000000..73440232b1
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/GridConnector.java
@@ -0,0 +1,610 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.logging.Logger;
+
+import com.google.gwt.json.client.JSONArray;
+import com.google.gwt.json.client.JSONObject;
+import com.google.gwt.json.client.JSONValue;
+import com.vaadin.client.ComponentConnector;
+import com.vaadin.client.ConnectorHierarchyChangeEvent;
+import com.vaadin.client.annotations.OnStateChange;
+import com.vaadin.client.communication.StateChangeEvent;
+import com.vaadin.client.data.DataSource.RowHandle;
+import com.vaadin.client.data.RpcDataSourceConnector.RpcDataSource;
+import com.vaadin.client.ui.AbstractHasComponentsConnector;
+import com.vaadin.client.ui.grid.GridHeader.HeaderRow;
+import com.vaadin.client.ui.grid.GridStaticSection.StaticCell;
+import com.vaadin.client.ui.grid.GridStaticSection.StaticRow;
+import com.vaadin.client.ui.grid.renderers.AbstractRendererConnector;
+import com.vaadin.client.ui.grid.selection.AbstractRowHandleSelectionModel;
+import com.vaadin.client.ui.grid.selection.SelectionChangeEvent;
+import com.vaadin.client.ui.grid.selection.SelectionChangeHandler;
+import com.vaadin.client.ui.grid.selection.SelectionModel;
+import com.vaadin.client.ui.grid.selection.SelectionModelMulti;
+import com.vaadin.client.ui.grid.selection.SelectionModelNone;
+import com.vaadin.client.ui.grid.selection.SelectionModelSingle;
+import com.vaadin.client.ui.grid.sort.SortEvent;
+import com.vaadin.client.ui.grid.sort.SortEventHandler;
+import com.vaadin.client.ui.grid.sort.SortOrder;
+import com.vaadin.shared.ui.Connect;
+import com.vaadin.shared.ui.grid.GridClientRpc;
+import com.vaadin.shared.ui.grid.GridColumnState;
+import com.vaadin.shared.ui.grid.GridServerRpc;
+import com.vaadin.shared.ui.grid.GridState;
+import com.vaadin.shared.ui.grid.GridState.SharedSelectionMode;
+import com.vaadin.shared.ui.grid.GridStaticSectionState;
+import com.vaadin.shared.ui.grid.GridStaticSectionState.CellState;
+import com.vaadin.shared.ui.grid.GridStaticSectionState.RowState;
+import com.vaadin.shared.ui.grid.ScrollDestination;
+import com.vaadin.shared.ui.grid.SortDirection;
+
+/**
+ * Connects the client side {@link Grid} widget with the server side
+ * {@link com.vaadin.ui.components.grid.Grid} component.
+ * <p>
+ * The Grid is typed to JSONObject. The structure of the JSONObject is described
+ * at {@link com.vaadin.shared.data.DataProviderRpc#setRowData(int, List)
+ * DataProviderRpc.setRowData(int, List)}.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+@Connect(com.vaadin.ui.components.grid.Grid.class)
+public class GridConnector extends AbstractHasComponentsConnector {
+
+ /**
+ * Custom implementation of the custom grid column using a JSONObject to
+ * represent the cell value and String as a column type.
+ */
+ private class CustomGridColumn extends GridColumn<Object, JSONObject> {
+
+ private final String id;
+
+ private AbstractRendererConnector<Object> rendererConnector;
+
+ public CustomGridColumn(String id,
+ AbstractRendererConnector<Object> rendererConnector) {
+ super(rendererConnector.getRenderer());
+ this.rendererConnector = rendererConnector;
+ this.id = id;
+ }
+
+ @Override
+ public Object getValue(final JSONObject obj) {
+ final JSONValue rowData = obj.get(GridState.JSONKEY_DATA);
+ final JSONArray rowDataArray = rowData.isArray();
+ assert rowDataArray != null : "Was unable to parse JSON into an array: "
+ + rowData;
+
+ final int columnIndex = resolveCurrentIndexFromState();
+ final JSONValue columnValue = rowDataArray.get(columnIndex);
+ return rendererConnector.decode(columnValue);
+ }
+
+ /*
+ * Only used to check that the renderer connector will not change during
+ * the column lifetime.
+ *
+ * TODO remove once support for changing renderers is implemented
+ */
+ private AbstractRendererConnector<Object> getRendererConnector() {
+ return rendererConnector;
+ }
+
+ private int resolveCurrentIndexFromState() {
+ List<GridColumnState> columns = getState().columns;
+ int numColumns = columns.size();
+ for (int index = 0; index < numColumns; index++) {
+ if (columns.get(index).id.equals(id)) {
+ return index;
+ }
+ }
+ return -1;
+ }
+ }
+
+ /**
+ * Maps a generated column id to a grid column instance
+ */
+ private Map<String, CustomGridColumn> columnIdToColumn = new HashMap<String, CustomGridColumn>();
+
+ private AbstractRowHandleSelectionModel<JSONObject> selectionModel = createSelectionModel(SharedSelectionMode.NONE);
+ private Set<String> selectedKeys = new LinkedHashSet<String>();
+
+ /**
+ * updateFromState is set to true when {@link #updateSelectionFromState()}
+ * makes changes to selection. This flag tells the
+ * {@code internalSelectionChangeHandler} to not send same data straight
+ * back to server. Said listener sets it back to false when handling that
+ * event.
+ */
+ private boolean updatedFromState = false;
+
+ private RpcDataSource dataSource;
+
+ private SelectionChangeHandler<JSONObject> internalSelectionChangeHandler = new SelectionChangeHandler<JSONObject>() {
+ @Override
+ public void onSelectionChange(SelectionChangeEvent<JSONObject> event) {
+ if (event.isBatchedSelection()) {
+ return;
+ }
+ if (!updatedFromState) {
+ for (JSONObject row : event.getRemoved()) {
+ selectedKeys.remove(dataSource.getRowKey(row));
+ }
+
+ for (JSONObject row : event.getAdded()) {
+ selectedKeys.add((String) dataSource.getRowKey(row));
+ }
+
+ getRpcProxy(GridServerRpc.class).selectionChange(
+ new ArrayList<String>(selectedKeys));
+ } else {
+ updatedFromState = false;
+ }
+ }
+ };
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public Grid<JSONObject> getWidget() {
+ return (Grid<JSONObject>) super.getWidget();
+ }
+
+ @Override
+ public GridState getState() {
+ return (GridState) super.getState();
+ }
+
+ @Override
+ protected void init() {
+ super.init();
+
+ registerRpc(GridClientRpc.class, new GridClientRpc() {
+ @Override
+ public void scrollToStart() {
+ getWidget().scrollToStart();
+ }
+
+ @Override
+ public void scrollToEnd() {
+ getWidget().scrollToEnd();
+ }
+
+ @Override
+ public void scrollToRow(int row, ScrollDestination destination) {
+ getWidget().scrollToRow(row, destination);
+ }
+ });
+
+ getWidget().setSelectionModel(selectionModel);
+
+ getWidget().addSelectionChangeHandler(internalSelectionChangeHandler);
+
+ getWidget().addSortHandler(new SortEventHandler<JSONObject>() {
+ @Override
+ public void sort(SortEvent<JSONObject> event) {
+ List<SortOrder> order = event.getOrder();
+ String[] columnIds = new String[order.size()];
+ SortDirection[] directions = new SortDirection[order.size()];
+ for (int i = 0; i < order.size(); i++) {
+ SortOrder sortOrder = order.get(i);
+ CustomGridColumn column = (CustomGridColumn) sortOrder
+ .getColumn();
+ columnIds[i] = column.id;
+
+ directions[i] = sortOrder.getDirection();
+ }
+
+ if (!Arrays.equals(columnIds, getState().sortColumns)
+ || !Arrays.equals(directions, getState().sortDirs)) {
+ // Report back to server if changed
+ getRpcProxy(GridServerRpc.class).sort(columnIds,
+ directions, event.getOriginator());
+ }
+ }
+ });
+ }
+
+ @Override
+ public void onStateChanged(StateChangeEvent stateChangeEvent) {
+ super.onStateChanged(stateChangeEvent);
+
+ // Column updates
+ if (stateChangeEvent.hasPropertyChanged("columns")) {
+
+ int totalColumns = getState().columns.size();
+
+ // Remove old columns
+ purgeRemovedColumns();
+
+ int currentColumns = getWidget().getColumnCount();
+ if (getWidget().getSelectionModel().getSelectionColumnRenderer() != null) {
+ currentColumns--;
+ }
+
+ // Add new columns
+ for (int columnIndex = currentColumns; columnIndex < totalColumns; columnIndex++) {
+ addColumnFromStateChangeEvent(columnIndex);
+ }
+
+ // Update old columns
+ for (int columnIndex = 0; columnIndex < currentColumns; columnIndex++) {
+ // FIXME Currently updating all column header / footers when a
+ // change in made in one column. When the framework supports
+ // quering a specific item in a list then it should do so here.
+ updateColumnFromStateChangeEvent(columnIndex);
+ }
+ }
+
+ if (stateChangeEvent.hasPropertyChanged("header")) {
+ updateSectionFromState(getWidget().getHeader(), getState().header);
+ }
+
+ if (stateChangeEvent.hasPropertyChanged("footer")) {
+ updateSectionFromState(getWidget().getFooter(), getState().footer);
+ }
+
+ if (stateChangeEvent.hasPropertyChanged("lastFrozenColumnId")) {
+ String frozenColId = getState().lastFrozenColumnId;
+ if (frozenColId != null) {
+ CustomGridColumn column = columnIdToColumn.get(frozenColId);
+ assert column != null : "Column to be frozen could not be found (id:"
+ + frozenColId + ")";
+ getWidget().setLastFrozenColumn(column);
+ } else {
+ getWidget().setLastFrozenColumn(null);
+ }
+ }
+ }
+
+ private void updateSectionFromState(GridStaticSection<?> section,
+ GridStaticSectionState state) {
+
+ while (section.getRowCount() != 0) {
+ section.removeRow(0);
+ }
+
+ for (RowState rowState : state.rows) {
+ StaticRow<?> row = section.appendRow();
+
+ int selectionOffset = 1;
+ if (getWidget().getSelectionModel() instanceof SelectionModel.None) {
+ selectionOffset = 0;
+ }
+
+ assert rowState.cells.size() == getWidget().getColumnCount() - selectionOffset;
+
+ int i = 0 + selectionOffset;
+ for (CellState cellState : rowState.cells) {
+ StaticCell cell = row.getCell(i++);
+ switch (cellState.type) {
+ case TEXT:
+ cell.setText(cellState.text);
+ break;
+ case HTML:
+ cell.setHtml(cellState.text);
+ break;
+ case WIDGET:
+ ComponentConnector connector = (ComponentConnector) cellState.connector;
+ cell.setWidget(connector.getWidget());
+ break;
+ default:
+ throw new IllegalStateException("unexpected cell type: "
+ + cellState.type);
+ }
+ }
+
+ for (List<Integer> group : rowState.cellGroups) {
+ GridColumn<?, ?>[] columns = new GridColumn<?, ?>[group.size()];
+ i = 0;
+ for (Integer colIndex : group) {
+ columns[i++] = getWidget().getColumn(selectionOffset + colIndex);
+ }
+ row.join(columns);
+ }
+
+ if (section instanceof GridHeader && rowState.defaultRow) {
+ ((GridHeader) section).setDefaultRow((HeaderRow) row);
+ }
+ }
+
+ section.setVisible(state.visible);
+
+ section.requestSectionRefresh();
+ }
+
+ /**
+ * Updates a column from a state change event.
+ *
+ * @param columnIndex
+ * The index of the column to update
+ */
+ private void updateColumnFromStateChangeEvent(final int columnIndex) {
+ /*
+ * We use the widget column index here instead of the given column
+ * index. SharedState contains information only about the explicitly
+ * defined columns, while the widget counts the selection column as an
+ * explicit one.
+ */
+ GridColumn<?, JSONObject> column = getWidget().getColumn(
+ getWidgetColumnIndex(columnIndex));
+
+ GridColumnState columnState = getState().columns.get(columnIndex);
+ updateColumnFromState(column, columnState);
+
+ assert column instanceof CustomGridColumn : "column at index "
+ + columnIndex + " is not a "
+ + CustomGridColumn.class.getSimpleName() + ", but a "
+ + column.getClass().getSimpleName();
+
+ if (columnState.rendererConnector != ((CustomGridColumn) column)
+ .getRendererConnector()) {
+ throw new UnsupportedOperationException(
+ "Changing column renderer after initialization is currently unsupported");
+ }
+ }
+
+ /**
+ * Adds a new column to the grid widget from a state change event
+ *
+ * @param columnIndex
+ * The index of the column, according to how it
+ */
+ private void addColumnFromStateChangeEvent(int columnIndex) {
+ GridColumnState state = getState().columns.get(columnIndex);
+ @SuppressWarnings("unchecked")
+ CustomGridColumn column = new CustomGridColumn(state.id,
+ ((AbstractRendererConnector<Object>) state.rendererConnector));
+ columnIdToColumn.put(state.id, column);
+
+ /*
+ * Adds a column to grid, and registers Grid with the column.
+ *
+ * We use the widget column index here instead of the given column
+ * index. SharedState contains information only about the explicitly
+ * defined columns, while the widget counts the selection column as an
+ * explicit one.
+ */
+ getWidget().addColumn(column, getWidgetColumnIndex(columnIndex));
+
+ /*
+ * Have to update state _after_ the column has been added to the grid as
+ * then, and only then, the column will call the grid which in turn will
+ * call the escalator's refreshRow methods on header/footer/body and
+ * visually refresh the row. If this is done in the reverse order the
+ * first column state update will be lost as no grid instance is
+ * present.
+ */
+ updateColumnFromState(column, state);
+ }
+
+ /**
+ * If we have a selection column renderer, we need to offset the index by
+ * one when referring to the column index in the widget.
+ */
+ private int getWidgetColumnIndex(final int columnIndex) {
+ Renderer<Boolean> selectionColumnRenderer = getWidget()
+ .getSelectionModel().getSelectionColumnRenderer();
+ int widgetColumnIndex = columnIndex;
+ if (selectionColumnRenderer != null) {
+ widgetColumnIndex++;
+ }
+ return widgetColumnIndex;
+ }
+
+ /**
+ * Updates the column values from a state
+ *
+ * @param column
+ * The column to update
+ * @param state
+ * The state to get the data from
+ */
+ private static void updateColumnFromState(GridColumn<?, JSONObject> column,
+ GridColumnState state) {
+ column.setVisible(state.visible);
+ column.setWidth(state.width);
+ column.setSortable(state.sortable);
+ }
+
+ /**
+ * Removes any orphan columns that has been removed from the state from the
+ * grid
+ */
+ private void purgeRemovedColumns() {
+
+ // Get columns still registered in the state
+ Set<String> columnsInState = new HashSet<String>();
+ for (GridColumnState columnState : getState().columns) {
+ columnsInState.add(columnState.id);
+ }
+
+ // Remove column no longer in state
+ Iterator<String> columnIdIterator = columnIdToColumn.keySet()
+ .iterator();
+ while (columnIdIterator.hasNext()) {
+ String id = columnIdIterator.next();
+ if (!columnsInState.contains(id)) {
+ CustomGridColumn column = columnIdToColumn.get(id);
+ columnIdIterator.remove();
+ getWidget().removeColumn(column);
+ }
+ }
+ }
+
+ public void setDataSource(RpcDataSource dataSource) {
+ this.dataSource = dataSource;
+ getWidget().setDataSource(this.dataSource);
+ }
+
+ @OnStateChange("selectionMode")
+ private void onSelectionModeChange() {
+ SharedSelectionMode mode = getState().selectionMode;
+ if (mode == null) {
+ getLogger().fine("ignored mode change");
+ return;
+ }
+
+ AbstractRowHandleSelectionModel<JSONObject> model = createSelectionModel(mode);
+ if (!model.getClass().equals(selectionModel.getClass())) {
+ selectionModel = model;
+ getWidget().setSelectionModel(model);
+ selectedKeys.clear();
+ }
+ }
+
+ @OnStateChange("selectedKeys")
+ private void updateSelectionFromState() {
+ boolean changed = false;
+
+ List<String> stateKeys = getState().selectedKeys;
+
+ // find new deselections
+ for (String key : selectedKeys) {
+ if (!stateKeys.contains(key)) {
+ changed = true;
+ deselectByHandle(dataSource.getHandleByKey(key));
+ }
+ }
+
+ // find new selections
+ for (String key : stateKeys) {
+ if (!selectedKeys.contains(key)) {
+ changed = true;
+ selectByHandle(dataSource.getHandleByKey(key));
+ }
+ }
+
+ /*
+ * A defensive copy in case the collection in the state is mutated
+ * instead of re-assigned.
+ */
+ selectedKeys = new LinkedHashSet<String>(stateKeys);
+
+ /*
+ * We need to fire this event so that Grid is able to re-render the
+ * selection changes (if applicable).
+ */
+ if (changed) {
+ // At least for now there's no way to send the selected and/or
+ // deselected row data. Some data is only stored as keys
+ updatedFromState = true;
+ getWidget().fireEvent(
+ new SelectionChangeEvent<JSONObject>(getWidget(),
+ (List<JSONObject>) null, null, false));
+ }
+ }
+
+ @OnStateChange({ "sortColumns", "sortDirs" })
+ private void onSortStateChange() {
+ List<SortOrder> sortOrder = new ArrayList<SortOrder>();
+
+ String[] sortColumns = getState().sortColumns;
+ SortDirection[] sortDirs = getState().sortDirs;
+
+ for (int i = 0; i < sortColumns.length; i++) {
+ sortOrder.add(new SortOrder(columnIdToColumn.get(sortColumns[i]),
+ sortDirs[i]));
+ }
+
+ getWidget().setSortOrder(sortOrder);
+ }
+
+ private Logger getLogger() {
+ return Logger.getLogger(getClass().getName());
+ }
+
+ @SuppressWarnings("static-method")
+ private AbstractRowHandleSelectionModel<JSONObject> createSelectionModel(
+ SharedSelectionMode mode) {
+ switch (mode) {
+ case SINGLE:
+ return new SelectionModelSingle<JSONObject>();
+ case MULTI:
+ return new SelectionModelMulti<JSONObject>();
+ case NONE:
+ return new SelectionModelNone<JSONObject>();
+ default:
+ throw new IllegalStateException("unexpected mode value: " + mode);
+ }
+ }
+
+ /**
+ * A workaround method for accessing the protected method
+ * {@code AbstractRowHandleSelectionModel.selectByHandle}
+ */
+ private native void selectByHandle(RowHandle<JSONObject> handle)
+ /*-{
+ var model = this.@com.vaadin.client.ui.grid.GridConnector::selectionModel;
+ model.@com.vaadin.client.ui.grid.selection.AbstractRowHandleSelectionModel::selectByHandle(*)(handle);
+ }-*/;
+
+ /**
+ * A workaround method for accessing the protected method
+ * {@code AbstractRowHandleSelectionModel.deselectByHandle}
+ */
+ private native void deselectByHandle(RowHandle<JSONObject> handle)
+ /*-{
+ var model = this.@com.vaadin.client.ui.grid.GridConnector::selectionModel;
+ model.@com.vaadin.client.ui.grid.selection.AbstractRowHandleSelectionModel::deselectByHandle(*)(handle);
+ }-*/;
+
+ /**
+ * Gets the row key for a row by index.
+ *
+ * @param index
+ * the index of the row for which to get the key
+ * @return the key for the row at {@code index}
+ */
+ public String getRowKey(int index) {
+ final JSONObject row = dataSource.getRow(index);
+ final Object key = dataSource.getRowKey(row);
+ assert key instanceof String : "Internal key was not a String but a "
+ + key.getClass().getSimpleName() + " (" + key + ")";
+ return (String) key;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.vaadin.client.HasComponentsConnector#updateCaption(com.vaadin.client
+ * .ComponentConnector)
+ */
+ @Override
+ public void updateCaption(ComponentConnector connector) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public void onConnectorHierarchyChange(
+ ConnectorHierarchyChangeEvent connectorHierarchyChangeEvent) {
+ }
+}
diff --git a/client/src/com/vaadin/client/ui/grid/GridFooter.java b/client/src/com/vaadin/client/ui/grid/GridFooter.java
new file mode 100644
index 0000000000..e798139b9a
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/GridFooter.java
@@ -0,0 +1,74 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid;
+
+import com.google.gwt.core.client.Scheduler;
+
+/**
+ * Represents the footer section of a Grid. The footer is always empty.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public class GridFooter extends GridStaticSection<GridFooter.FooterRow> {
+
+ /**
+ * A single row in a grid Footer section.
+ *
+ */
+ public class FooterRow extends GridStaticSection.StaticRow<FooterCell> {
+
+ @Override
+ protected FooterCell createCell() {
+ return new FooterCell();
+ }
+ }
+
+ /**
+ * A single cell in a grid Footer row. Has a textual caption.
+ *
+ */
+ public class FooterCell extends GridStaticSection.StaticCell {
+ }
+
+ private boolean markAsDirty = false;
+
+ @Override
+ protected FooterRow createRow() {
+ return new FooterRow();
+ }
+
+ @Override
+ protected void requestSectionRefresh() {
+ markAsDirty = true;
+
+ /*
+ * Defer the refresh so if we multiple times call refreshSection() (for
+ * example when updating cell values) we only get one actual refresh in
+ * the end.
+ */
+ Scheduler.get().scheduleFinally(new Scheduler.ScheduledCommand() {
+
+ @Override
+ public void execute() {
+ if (markAsDirty) {
+ markAsDirty = false;
+ getGrid().refreshFooter();
+ }
+ }
+ });
+ }
+}
diff --git a/client/src/com/vaadin/client/ui/grid/GridHeader.java b/client/src/com/vaadin/client/ui/grid/GridHeader.java
new file mode 100644
index 0000000000..2867ae4d1c
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/GridHeader.java
@@ -0,0 +1,150 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid;
+
+import java.util.Arrays;
+import java.util.Collection;
+
+import com.google.gwt.core.client.Scheduler;
+import com.google.gwt.dom.client.BrowserEvents;
+
+/**
+ * Represents the header section of a Grid. A header consists of a single header
+ * row containing a header cell for each column. Each cell has a simple textual
+ * caption.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public class GridHeader extends GridStaticSection<GridHeader.HeaderRow> {
+
+ /**
+ * A single row in a grid header section.
+ *
+ */
+ public class HeaderRow extends GridStaticSection.StaticRow<HeaderCell> {
+
+ private boolean isDefault = false;
+
+ protected void setDefault(boolean isDefault) {
+ this.isDefault = isDefault;
+ }
+
+ public boolean isDefault() {
+ return isDefault;
+ }
+
+ @Override
+ protected HeaderCell createCell() {
+ return new HeaderCell();
+ }
+ }
+
+ /**
+ * A single cell in a grid header row. Has a textual caption.
+ *
+ */
+ public class HeaderCell extends GridStaticSection.StaticCell {
+ }
+
+ private HeaderRow defaultRow;
+
+ private boolean markAsDirty = false;
+
+ @Override
+ public void removeRow(int index) {
+ HeaderRow removedRow = getRow(index);
+ super.removeRow(index);
+ if (removedRow == defaultRow) {
+ setDefaultRow(null);
+ }
+ }
+
+ /**
+ * Sets the default row of this header. The default row is a special header
+ * row providing a user interface for sorting columns.
+ *
+ * @param row
+ * the new default row, or null for no default row
+ *
+ * @throws IllegalArgumentException
+ * this header does not contain the row
+ */
+ public void setDefaultRow(HeaderRow row) {
+ if (row == defaultRow) {
+ return;
+ }
+ if (row != null && !getRows().contains(row)) {
+ throw new IllegalArgumentException(
+ "Cannot set a default row that does not exist in the container");
+ }
+ if (defaultRow != null) {
+ defaultRow.setDefault(false);
+ }
+ if (row != null) {
+ row.setDefault(true);
+ }
+ defaultRow = row;
+ requestSectionRefresh();
+ }
+
+ /**
+ * Returns the current default row of this header. The default row is a
+ * special header row providing a user interface for sorting columns.
+ *
+ * @return the default row or null if no default row set
+ */
+ public HeaderRow getDefaultRow() {
+ return defaultRow;
+ }
+
+ @Override
+ protected HeaderRow createRow() {
+ return new HeaderRow();
+ }
+
+ @Override
+ protected void requestSectionRefresh() {
+ markAsDirty = true;
+
+ /*
+ * Defer the refresh so if we multiple times call refreshSection() (for
+ * example when updating cell values) we only get one actual refresh in
+ * the end.
+ */
+ Scheduler.get().scheduleFinally(new Scheduler.ScheduledCommand() {
+
+ @Override
+ public void execute() {
+ if (markAsDirty) {
+ markAsDirty = false;
+ getGrid().refreshHeader();
+ }
+ }
+ });
+ }
+
+ /**
+ * Returns the events consumed by the header
+ *
+ * @return a collection of BrowserEvents
+ */
+ public Collection<String> getConsumedEvents() {
+ return Arrays.asList(BrowserEvents.TOUCHSTART, BrowserEvents.TOUCHMOVE,
+ BrowserEvents.TOUCHEND, BrowserEvents.TOUCHCANCEL,
+ BrowserEvents.CLICK);
+ }
+}
diff --git a/client/src/com/vaadin/client/ui/grid/GridStaticSection.java b/client/src/com/vaadin/client/ui/grid/GridStaticSection.java
new file mode 100644
index 0000000000..8c9ada46d0
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/GridStaticSection.java
@@ -0,0 +1,532 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+
+import com.google.gwt.user.client.ui.Widget;
+import com.vaadin.shared.ui.grid.GridStaticCellType;
+
+/**
+ * Abstract base class for Grid header and footer sections.
+ *
+ * @since
+ * @author Vaadin Ltd
+ * @param <ROWTYPE>
+ * the type of the rows in the section
+ */
+abstract class GridStaticSection<ROWTYPE extends GridStaticSection.StaticRow<?>> {
+
+ /**
+ * A header or footer cell. Has a simple textual caption.
+ *
+ */
+ static class StaticCell {
+
+ private Object content = null;
+
+ private int colspan = 1;
+
+ private GridStaticSection<?> section;
+
+ private GridStaticCellType type = GridStaticCellType.TEXT;
+
+ /**
+ * Sets the text displayed in this cell.
+ *
+ * @param text
+ * a plain text caption
+ */
+ public void setText(String text) {
+ this.content = text;
+ this.type = GridStaticCellType.TEXT;
+ section.requestSectionRefresh();
+ }
+
+ /**
+ * Returns the text displayed in this cell.
+ *
+ * @return the plain text caption
+ */
+ public String getText() {
+ if (type != GridStaticCellType.TEXT) {
+ throw new IllegalStateException(
+ "Cannot fetch Text from a cell with type " + type);
+ }
+ return (String) content;
+ }
+
+ protected GridStaticSection<?> getSection() {
+ assert section != null;
+ return section;
+ }
+
+ protected void setSection(GridStaticSection<?> section) {
+ this.section = section;
+ }
+
+ /**
+ * Returns the amount of columns the cell spans. By default is 1.
+ *
+ * @return The amount of columns the cell spans.
+ */
+ public int getColspan() {
+ return colspan;
+ }
+
+ /**
+ * Sets the amount of columns the cell spans. Must be more or equal to
+ * 1. By default is 1.
+ *
+ * @param colspan
+ * the colspan to set
+ */
+ public void setColspan(int colspan) {
+ if (colspan < 1) {
+ throw new IllegalArgumentException(
+ "Colspan cannot be less than 1");
+ }
+
+ this.colspan = colspan;
+ section.requestSectionRefresh();
+ }
+
+ /**
+ * Returns the html inside the cell.
+ *
+ * @throws IllegalStateException
+ * if trying to retrive HTML from a cell with a type other
+ * than {@link Type#HTML}.
+ * @return the html content of the cell.
+ */
+ public String getHtml() {
+ if (type != GridStaticCellType.HTML) {
+ throw new IllegalStateException(
+ "Cannot fetch HTML from a cell with type " + type);
+ }
+ return (String) content;
+ }
+
+ /**
+ * Sets the content of the cell to the provided html. All previous
+ * content is discarded and the cell type is set to {@link Type#HTML}.
+ *
+ * @param html
+ * The html content of the cell
+ */
+ public void setHtml(String html) {
+ this.content = html;
+ this.type = GridStaticCellType.HTML;
+ section.requestSectionRefresh();
+ }
+
+ /**
+ * Returns the widget in the cell.
+ *
+ * @throws IllegalStateException
+ * if the cell is not {@link Type#WIDGET}
+ *
+ * @return the widget in the cell
+ */
+ public Widget getWidget() {
+ if (type != GridStaticCellType.WIDGET) {
+ throw new IllegalStateException(
+ "Cannot fetch Widget from a cell with type " + type);
+ }
+ return (Widget) content;
+ }
+
+ /**
+ * Set widget as the content of the cell. The type of the cell becomes
+ * {@link Type#WIDGET}. All previous content is discarded.
+ *
+ * @param widget
+ * The widget to add to the cell. Should not be previously
+ * attached anywhere (widget.getParent == null).
+ */
+ public void setWidget(Widget widget) {
+ this.content = widget;
+ this.type = GridStaticCellType.WIDGET;
+ section.requestSectionRefresh();
+ }
+
+ /**
+ * Returns the type of the cell.
+ *
+ * @return the type of content the cell contains.
+ */
+ public GridStaticCellType getType() {
+ return type;
+ }
+ }
+
+ /**
+ * Abstract base class for Grid header and footer rows.
+ *
+ * @param <CELLTYPE>
+ * the type of the cells in the row
+ */
+ abstract static class StaticRow<CELLTYPE extends StaticCell> {
+
+ private List<CELLTYPE> cells = new ArrayList<CELLTYPE>();
+
+ private GridStaticSection<?> section;
+
+ private Collection<List<CELLTYPE>> cellGroups = new HashSet<List<CELLTYPE>>();
+
+ /**
+ * Returns the cell at the given position in this row.
+ *
+ * @param index
+ * the position of the cell
+ * @return the cell at the index
+ * @throws IndexOutOfBoundsException
+ * if the index is out of bounds
+ */
+ public CELLTYPE getCell(int index) {
+ return cells.get(index);
+ }
+
+ /**
+ * Merges cells in a row
+ *
+ * @param cells
+ * The cells to be merged
+ * @return The first cell of the merged cells
+ */
+ protected CELLTYPE join(List<CELLTYPE> cells) {
+ assert cells.size() > 1 : "You cannot merge less than 2 cells together";
+
+ // Ensure no cell is already grouped
+ for (CELLTYPE cell : cells) {
+ if (getCellGroupForCell(cell) != null) {
+ throw new IllegalStateException("Cell " + cell.getText()
+ + " is already grouped.");
+ }
+ }
+
+ // Ensure continuous range
+ int firstCellIndex = this.cells.indexOf(cells.get(0));
+ for (int i = 0; i < cells.size(); i++) {
+ if (this.cells.get(firstCellIndex + i) != cells.get(i)) {
+ throw new IllegalStateException(
+ "Cell range must be a continous range");
+ }
+ }
+
+ // Create a new group
+ cellGroups.add(new ArrayList<CELLTYPE>(cells));
+
+ // Calculates colspans, triggers refresh on section implicitly
+ calculateColspans();
+
+ // Returns first cell of group
+ return cells.get(0);
+ }
+
+ /**
+ * Merges columns cells in a row
+ *
+ * @param columns
+ * The columns which header should be merged
+ * @return The remaining visible cell after the merge
+ */
+ public CELLTYPE join(GridColumn<?, ?>... columns) {
+ assert columns.length > 1 : "You cannot merge less than 2 columns together";
+
+ // Convert columns to cells
+ List<CELLTYPE> cells = new ArrayList<CELLTYPE>();
+ for (GridColumn<?, ?> c : columns) {
+ int index = getSection().getGrid().getColumns().indexOf(c);
+ cells.add(this.cells.get(index));
+ }
+
+ return join(cells);
+ }
+
+ /**
+ * Merges columns cells in a row
+ *
+ * @param cells
+ * The cells to merge. Must be from the same row.
+ * @return The remaining visible cell after the merge
+ */
+ public CELLTYPE join(CELLTYPE... cells) {
+ return join(Arrays.asList(cells));
+ }
+
+ private List<CELLTYPE> getCellGroupForCell(CELLTYPE cell) {
+ for (List<CELLTYPE> group : cellGroups) {
+ if (group.contains(cell)) {
+ return group;
+ }
+ }
+ return null;
+ }
+
+ void calculateColspans() {
+
+ // Reset all cells
+ for (CELLTYPE cell : cells) {
+ cell.setColspan(1);
+ }
+
+ // Set colspan for grouped cells
+ for (List<CELLTYPE> group : cellGroups) {
+
+ int firstVisibleColumnInGroup = -1;
+ int lastVisibleColumnInGroup = -1;
+ int hiddenInsideGroup = 0;
+
+ /*
+ * To be able to calculate the colspan correctly we need to two
+ * things; find the first visible cell in the group which will
+ * get the colspan assigned to and find the amount of columns
+ * which should be spanned.
+ *
+ * To do that we iterate through all cells, marking into memory
+ * when we find the first visible cell, when we find the last
+ * visible cell and how many cells are hidden in between.
+ */
+ for (int i = 0; i < group.size(); i++) {
+ CELLTYPE cell = group.get(i);
+ int cellIndex = this.cells.indexOf(cell);
+ boolean columnVisible = getSection().getGrid()
+ .getColumn(cellIndex).isVisible();
+ if (columnVisible) {
+ lastVisibleColumnInGroup = i;
+ if (firstVisibleColumnInGroup == -1) {
+ firstVisibleColumnInGroup = i;
+ }
+ } else if (firstVisibleColumnInGroup != -1) {
+ hiddenInsideGroup++;
+ }
+ }
+
+ if (firstVisibleColumnInGroup == -1
+ || lastVisibleColumnInGroup == -1
+ || firstVisibleColumnInGroup == lastVisibleColumnInGroup) {
+ // No cells in group
+ continue;
+ }
+
+ /*
+ * Assign colspan to first cell in group.
+ */
+ CELLTYPE firstVisibleCell = group
+ .get(firstVisibleColumnInGroup);
+ firstVisibleCell.setColspan(lastVisibleColumnInGroup
+ - firstVisibleColumnInGroup - hiddenInsideGroup + 1);
+ }
+
+ }
+
+ protected void addCell(int index) {
+ CELLTYPE cell = createCell();
+ cell.setSection(getSection());
+ cells.add(index, cell);
+ }
+
+ protected void removeCell(int index) {
+ cells.remove(index);
+ }
+
+ protected abstract CELLTYPE createCell();
+
+ protected GridStaticSection<?> getSection() {
+ return section;
+ }
+
+ protected void setSection(GridStaticSection<?> section) {
+ this.section = section;
+ }
+ }
+
+ private Grid<?> grid;
+
+ private List<ROWTYPE> rows = new ArrayList<ROWTYPE>();
+
+ private boolean visible = true;
+
+ /**
+ * Creates and returns a new instance of the row type.
+ *
+ * @return the created row
+ */
+ protected abstract ROWTYPE createRow();
+
+ /**
+ * Informs the grid that this section should be re-rendered.
+ * <p>
+ * <b>Note</b> that re-render means calling update() on each cell,
+ * preAttach()/postAttach()/preDetach()/postDetach() is not called as the
+ * cells are not removed from the DOM.
+ */
+ protected abstract void requestSectionRefresh();
+
+ /**
+ * Sets the visibility of the whole section.
+ *
+ * @param visible
+ * true to show this section, false to hide
+ */
+ public void setVisible(boolean visible) {
+ this.visible = visible;
+ requestSectionRefresh();
+ }
+
+ /**
+ * Returns the visibility of this section.
+ *
+ * @return true if visible, false otherwise.
+ */
+ public boolean isVisible() {
+ return visible;
+ }
+
+ /**
+ * Inserts a new row at the given position.
+ *
+ * @param index
+ * the position at which to insert the row
+ * @return the new row
+ *
+ * @throws IndexOutOfBoundsException
+ * if the index is out of bounds
+ */
+ public ROWTYPE addRow(int index) {
+ ROWTYPE row = createRow();
+ row.setSection(this);
+ for (int i = 0; i < getGrid().getColumnCount(); ++i) {
+ row.addCell(i);
+ }
+ rows.add(index, row);
+
+ requestSectionRefresh();
+ return row;
+ }
+
+ /**
+ * Adds a new row at the top of this section.
+ *
+ * @return the new row
+ */
+ public ROWTYPE prependRow() {
+ return addRow(0);
+ }
+
+ /**
+ * Adds a new row at the bottom of this section.
+ *
+ * @return the new row
+ */
+ public ROWTYPE appendRow() {
+ return addRow(rows.size());
+ }
+
+ /**
+ * Removes the row at the given position.
+ *
+ * @param index
+ * the position of the row
+ *
+ * @throws IndexOutOfBoundsException
+ * if the index is out of bounds
+ */
+ public void removeRow(int index) {
+ rows.remove(index);
+ requestSectionRefresh();
+ }
+
+ /**
+ * Removes the given row from the section.
+ *
+ * @param row
+ * the row to be removed
+ *
+ * @throws IllegalArgumentException
+ * if the row does not exist in this section
+ */
+ public void removeRow(ROWTYPE row) {
+ try {
+ removeRow(rows.indexOf(row));
+ } catch (IndexOutOfBoundsException e) {
+ throw new IllegalArgumentException(
+ "Section does not contain the given row");
+ }
+ }
+
+ /**
+ * Returns the row at the given position.
+ *
+ * @param index
+ * the position of the row
+ * @return the row with the given index
+ *
+ * @throws IndexOutOfBoundsException
+ * if the index is out of bounds
+ */
+ public ROWTYPE getRow(int index) {
+ try {
+ return rows.get(index);
+ } catch (IndexOutOfBoundsException e) {
+ throw new IllegalArgumentException("Row with index " + index
+ + " does not exist");
+ }
+ }
+
+ /**
+ * Returns the number of rows in this section.
+ *
+ * @return the number of rows
+ */
+ public int getRowCount() {
+ return rows.size();
+ }
+
+ protected List<ROWTYPE> getRows() {
+ return rows;
+ }
+
+ protected int getVisibleRowCount() {
+ return isVisible() ? getRowCount() : 0;
+ }
+
+ protected void addColumn(GridColumn<?, ?> column, int index) {
+ for (ROWTYPE row : rows) {
+ row.addCell(index);
+ }
+ }
+
+ protected void removeColumn(int index) {
+ for (ROWTYPE row : rows) {
+ row.removeCell(index);
+ }
+ }
+
+ protected void setGrid(Grid<?> grid) {
+ this.grid = grid;
+ }
+
+ protected Grid<?> getGrid() {
+ assert grid != null;
+ return grid;
+ }
+}
diff --git a/client/src/com/vaadin/client/ui/grid/PositionFunction.java b/client/src/com/vaadin/client/ui/grid/PositionFunction.java
new file mode 100644
index 0000000000..4db5efd0fc
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/PositionFunction.java
@@ -0,0 +1,118 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid;
+
+import com.google.gwt.dom.client.Element;
+import com.google.gwt.dom.client.Style.Unit;
+
+/**
+ * A functional interface that can be used for positioning elements in the DOM.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+interface PositionFunction {
+ /**
+ * A position function using "transform: translate3d(x,y,z)" to position
+ * elements in the DOM.
+ */
+ public static class Translate3DPosition implements PositionFunction {
+ @Override
+ public void set(Element e, double x, double y) {
+ e.getStyle().setProperty("transform",
+ "translate3d(" + x + "px, " + y + "px, 0)");
+ }
+
+ @Override
+ public void reset(Element e) {
+ e.getStyle().clearProperty("transform");
+ }
+ }
+
+ /**
+ * A position function using "transform: translate(x,y)" to position
+ * elements in the DOM.
+ */
+ public static class TranslatePosition implements PositionFunction {
+ @Override
+ public void set(Element e, double x, double y) {
+ e.getStyle().setProperty("transform",
+ "translate(" + x + "px," + y + "px)");
+ }
+
+ @Override
+ public void reset(Element e) {
+ e.getStyle().clearProperty("transform");
+ }
+ }
+
+ /**
+ * A position function using "-webkit-transform: translate3d(x,y,z)" to
+ * position elements in the DOM.
+ */
+ public static class WebkitTranslate3DPosition implements PositionFunction {
+ @Override
+ public void set(Element e, double x, double y) {
+ e.getStyle().setProperty("webkitTransform",
+ "translate3d(" + x + "px," + y + "px,0)");
+ }
+
+ @Override
+ public void reset(Element e) {
+ e.getStyle().clearProperty("webkitTransform");
+ }
+ }
+
+ /**
+ * A position function using "left: x" and "top: y" to position elements in
+ * the DOM.
+ */
+ public static class AbsolutePosition implements PositionFunction {
+ @Override
+ public void set(Element e, double x, double y) {
+ e.getStyle().setLeft(x, Unit.PX);
+ e.getStyle().setTop(y, Unit.PX);
+ }
+
+ @Override
+ public void reset(Element e) {
+ e.getStyle().clearLeft();
+ e.getStyle().clearTop();
+ }
+ }
+
+ /**
+ * Position an element in an (x,y) coordinate system in the DOM.
+ *
+ * @param e
+ * the element to position. Never <code>null</code>.
+ * @param x
+ * the x coordinate, in pixels
+ * @param y
+ * the y coordinate, in pixels
+ */
+ void set(Element e, double x, double y);
+
+ /**
+ * Resets any previously applied positioning, clearing the used style
+ * attributes.
+ *
+ * @param e
+ * the element for which to reset the positioning
+ */
+ void reset(Element e);
+} \ No newline at end of file
diff --git a/client/src/com/vaadin/client/ui/grid/Renderer.java b/client/src/com/vaadin/client/ui/grid/Renderer.java
new file mode 100644
index 0000000000..787a145326
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/Renderer.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid;
+
+
+/**
+ * Renderer for rending a value &lt;T&gt; into cell.
+ * <p>
+ * You can add a renderer to any column by overring the
+ * {@link GridColumn#getRenderer()} method and returning your own renderer. You
+ * can retrieve the cell element using {@link Cell#getElement()}.
+ *
+ * @param <T>
+ * The column type
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public interface Renderer<T> {
+
+ /**
+ * Called whenever the {@link Grid} updates a cell
+ *
+ * @param cell
+ * The cell. Note that the cell is a flyweight and should not be
+ * stored outside of the method as it will change.
+ *
+ * @param data
+ * The column data object
+ */
+ void render(FlyweightCell cell, T data);
+}
diff --git a/client/src/com/vaadin/client/ui/grid/Row.java b/client/src/com/vaadin/client/ui/grid/Row.java
new file mode 100644
index 0000000000..6419a98574
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/Row.java
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid;
+
+import com.google.gwt.dom.client.TableRowElement;
+
+/**
+ * A representation of a row in an {@link Escalator}.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public interface Row {
+ /**
+ * Gets the row index.
+ *
+ * @return the row index
+ */
+ public int getRow();
+
+ /**
+ * Gets the root element for this row.
+ * <p>
+ * The {@link EscalatorUpdater} may update the class names of the element
+ * and add inline styles, but may not modify the contained DOM structure.
+ * <p>
+ * If you wish to modify the cells within this row element, access them via
+ * the <code>List&lt;{@link Cell}&gt;</code> objects passed in to
+ * {@code EscalatorUpdater.updateCells(Row, List)}
+ *
+ * @return the root element of the row
+ */
+ public TableRowElement getElement();
+} \ No newline at end of file
diff --git a/client/src/com/vaadin/client/ui/grid/RowContainer.java b/client/src/com/vaadin/client/ui/grid/RowContainer.java
new file mode 100644
index 0000000000..d0fb0db103
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/RowContainer.java
@@ -0,0 +1,194 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid;
+
+import com.google.gwt.dom.client.Element;
+
+/**
+ * A representation of the rows in each of the sections (header, body and
+ * footer) in an {@link Escalator}.
+ *
+ * @since
+ * @author Vaadin Ltd
+ * @see Escalator#getHeader()
+ * @see Escalator#getBody()
+ * @see Escalator#getFooter()
+ */
+public interface RowContainer {
+
+ /**
+ * An arbitrary pixel height of a row, before any autodetection for the row
+ * height has been made.
+ * */
+ public static final int INITIAL_DEFAULT_ROW_HEIGHT = 20;
+
+ /**
+ * Returns the current {@link EscalatorUpdater} used to render cells.
+ *
+ * @return the current escalator updater
+ */
+ public EscalatorUpdater getEscalatorUpdater();
+
+ /**
+ * Sets the {@link EscalatorUpdater} to use when displaying data in the
+ * escalator.
+ *
+ * @param escalatorUpdater
+ * the escalator updater to use to render cells. May not be
+ * <code>null</code>
+ * @throws IllegalArgumentException
+ * if {@code cellRenderer} is <code>null</code>
+ * @see EscalatorUpdater#NULL
+ */
+ public void setEscalatorUpdater(EscalatorUpdater escalatorUpdater)
+ throws IllegalArgumentException;
+
+ /**
+ * Removes rows at a certain index in the current row container.
+ *
+ * @param index
+ * the index of the first row to be removed
+ * @param numberOfRows
+ * the number of rows to remove, starting from the index
+ * @throws IndexOutOfBoundsException
+ * if any integer number in the range
+ * <code>[index..(index+numberOfRows)]</code> is not an existing
+ * row index
+ * @throws IllegalArgumentException
+ * if {@code numberOfRows} is less than 1.
+ */
+ public void removeRows(int index, int numberOfRows)
+ throws IndexOutOfBoundsException, IllegalArgumentException;
+
+ /**
+ * Adds rows at a certain index in this row container.
+ * <p>
+ * The new rows will be inserted between the row at the index, and the row
+ * before (an index of 0 means that the rows are inserted at the beginning).
+ * Therefore, the rows currently at the index and afterwards will be moved
+ * downwards.
+ * <p>
+ * The contents of the inserted rows will subsequently be queried from the
+ * escalator updater.
+ * <p>
+ * <em>Note:</em> Only the contents of the inserted rows will be rendered.
+ * If inserting new rows affects the contents of existing rows,
+ * {@link #refreshRows(int, int)} needs to be called for those rows
+ * separately.
+ *
+ * @param index
+ * the index of the row before which new rows are inserted, or
+ * {@link #getRowCount()} to add rows at the end
+ * @param numberOfRows
+ * the number of rows to insert after the <code>index</code>
+ * @see #setEscalatorUpdater(EscalatorUpdater)
+ * @throws IndexOutOfBoundsException
+ * if <code>index</code> is not an integer in the range
+ * <code>[0..{@link #getRowCount()}]</code>
+ * @throws IllegalArgumentException
+ * if {@code numberOfRows} is less than 1.
+ */
+ public void insertRows(int index, int numberOfRows)
+ throws IndexOutOfBoundsException, IllegalArgumentException;
+
+ /**
+ * Refreshes a range of rows in the current row container.
+ * <p>
+ * The data for the refreshed rows are queried from the current cell
+ * renderer.
+ *
+ * @param index
+ * the index of the first row that will be updated
+ * @param numberOfRows
+ * the number of rows to update, starting from the index
+ * @see #setEscalatorUpdater(EscalatorUpdater)
+ * @throws IndexOutOfBoundsException
+ * if any integer number in the range
+ * <code>[index..(index+numberOfColumns)]</code> is not an
+ * existing column index.
+ * @throws IllegalArgumentException
+ * if {@code numberOfRows} is less than 1.
+ */
+ public void refreshRows(int index, int numberOfRows)
+ throws IndexOutOfBoundsException, IllegalArgumentException;
+
+ /**
+ * Gets the number of rows in the current row container.
+ *
+ * @return the number of rows in the current row container
+ */
+ public int getRowCount();
+
+ /**
+ * The default height of the rows in this RowContainer.
+ *
+ * @param px
+ * the default height in pixels of the rows in this RowContainer
+ * @throws IllegalArgumentException
+ * if <code>px &lt; 1</code>
+ * @see #getDefaultRowHeight()
+ */
+ public void setDefaultRowHeight(int px) throws IllegalArgumentException;
+
+ /**
+ * Returns the default height of the rows in this RowContainer.
+ * <p>
+ * This value will be equal to {@link #INITIAL_DEFAULT_ROW_HEIGHT} if the
+ * {@link Escalator} has not yet had a chance to autodetect the row height,
+ * or no explicit value has yet given via {@link #setDefaultRowHeight(int)}
+ *
+ * @return the default height of the rows in this RowContainer, in pixels
+ * @see #setDefaultRowHeight(int)
+ */
+ public int getDefaultRowHeight();
+
+ /**
+ * Returns the cell object which contains information about the cell the
+ * element is in.
+ *
+ * @param element
+ * The element to get the cell for. If element is not present in
+ * row container then <code>null</code> is returned.
+ *
+ * @return the cell of the element, or <code>null</code> if element is not
+ * present in the {@link RowContainer}.
+ */
+ public Cell getCell(Element element);
+
+ /**
+ * Gets the row element with given logical index. For lazy loaded containers
+ * such as Escalators BodyRowContainer visibility should be checked before
+ * calling this function. See {@link Escalator#getVisibleRowRange()}.
+ *
+ * @param index
+ * the logical index of the element to retrieve
+ * @return the element at position {@code index}
+ * @throws IndexOutOfBoundsException
+ * if {@code index} is not valid within container
+ * @throws IllegalStateException
+ * if {@code index} is currently not available in the DOM
+ */
+ public Element getRowElement(int index) throws IndexOutOfBoundsException,
+ IllegalStateException;
+
+ /**
+ * Returns the root element of RowContainer
+ *
+ * @return RowContainer root element
+ */
+ public Element getElement();
+}
diff --git a/client/src/com/vaadin/client/ui/grid/RowVisibilityChangeEvent.java b/client/src/com/vaadin/client/ui/grid/RowVisibilityChangeEvent.java
new file mode 100644
index 0000000000..c5c5e45ca8
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/RowVisibilityChangeEvent.java
@@ -0,0 +1,90 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid;
+
+import com.google.gwt.event.shared.GwtEvent;
+
+/**
+ * Event fired when the range of visible rows changes e.g. because of scrolling.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public class RowVisibilityChangeEvent extends
+ GwtEvent<RowVisibilityChangeHandler> {
+ /**
+ * The type of this event.
+ */
+ public static final Type<RowVisibilityChangeHandler> TYPE = new Type<RowVisibilityChangeHandler>();
+
+ private final int firstVisibleRow;
+ private final int visibleRowCount;
+
+ /**
+ * Creates a new row visibility change event
+ *
+ * @param firstVisibleRow
+ * the index of the first visible row
+ * @param visibleRowCount
+ * the number of visible rows
+ */
+ public RowVisibilityChangeEvent(int firstVisibleRow, int visibleRowCount) {
+ this.firstVisibleRow = firstVisibleRow;
+ this.visibleRowCount = visibleRowCount;
+ }
+
+ /**
+ * Gets the index of the first row that is at least partially visible.
+ *
+ * @return the index of the first visible row
+ */
+ public int getFirstVisibleRow() {
+ return firstVisibleRow;
+ }
+
+ /**
+ * Gets the number of at least partially visible rows.
+ *
+ * @return the number of visible rows
+ */
+ public int getVisibleRowCount() {
+ return visibleRowCount;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see com.google.gwt.event.shared.GwtEvent#getAssociatedType()
+ */
+ @Override
+ public Type<RowVisibilityChangeHandler> getAssociatedType() {
+ return TYPE;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * com.google.gwt.event.shared.GwtEvent#dispatch(com.google.gwt.event.shared
+ * .EventHandler)
+ */
+ @Override
+ protected void dispatch(RowVisibilityChangeHandler handler) {
+ handler.onRowVisibilityChange(this);
+ }
+
+}
diff --git a/client/src/com/vaadin/client/ui/grid/RowVisibilityChangeHandler.java b/client/src/com/vaadin/client/ui/grid/RowVisibilityChangeHandler.java
new file mode 100644
index 0000000000..6aa165fe04
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/RowVisibilityChangeHandler.java
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid;
+
+import com.google.gwt.event.shared.EventHandler;
+
+/**
+ * Event handler that gets notified when the range of visible rows changes e.g.
+ * because of scrolling.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public interface RowVisibilityChangeHandler extends EventHandler {
+
+ /**
+ * Called when the range of visible rows changes e.g. because of scrolling.
+ *
+ * @param event
+ * the row visibility change event describing the change
+ */
+ void onRowVisibilityChange(RowVisibilityChangeEvent event);
+
+}
diff --git a/client/src/com/vaadin/client/ui/grid/ScrollbarBundle.java b/client/src/com/vaadin/client/ui/grid/ScrollbarBundle.java
new file mode 100644
index 0000000000..5a54f5fc68
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/ScrollbarBundle.java
@@ -0,0 +1,639 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid;
+
+import com.google.gwt.dom.client.Element;
+import com.google.gwt.dom.client.Style.Overflow;
+import com.google.gwt.dom.client.Style.Unit;
+import com.google.gwt.event.shared.EventHandler;
+import com.google.gwt.event.shared.GwtEvent;
+import com.google.gwt.event.shared.HandlerManager;
+import com.google.gwt.event.shared.HandlerRegistration;
+import com.google.gwt.user.client.DOM;
+import com.google.gwt.user.client.Event;
+import com.google.gwt.user.client.EventListener;
+import com.google.gwt.user.client.Timer;
+
+/**
+ * An element-like bundle representing a configurable and visual scrollbar in
+ * one axis.
+ *
+ * @since
+ * @author Vaadin Ltd
+ * @see VerticalScrollbarBundle
+ * @see HorizontalScrollbarBundle
+ */
+abstract class ScrollbarBundle {
+
+ private class TemporaryResizer extends Object {
+ private static final int TEMPORARY_RESIZE_DELAY = 1000;
+
+ private final Timer timer = new Timer() {
+ @Override
+ public void run() {
+ internalSetScrollbarThickness(1);
+ }
+ };
+
+ public void show() {
+ internalSetScrollbarThickness(OSX_INVISIBLE_SCROLLBAR_FAKE_SIZE_PX);
+ timer.schedule(TEMPORARY_RESIZE_DELAY);
+ }
+ }
+
+ /**
+ * A means to listen to when the scrollbar handle in a
+ * {@link ScrollbarBundle} either appears or is removed.
+ */
+ public interface VisibilityHandler extends EventHandler {
+ /**
+ * This method is called whenever the scrollbar handle's visibility is
+ * changed in a {@link ScrollbarBundle}.
+ *
+ * @param event
+ * the {@link VisibilityChangeEvent}
+ */
+ void visibilityChanged(VisibilityChangeEvent event);
+ }
+
+ public static class VisibilityChangeEvent extends
+ GwtEvent<VisibilityHandler> {
+ public static final Type<VisibilityHandler> TYPE = new Type<ScrollbarBundle.VisibilityHandler>() {
+ @Override
+ public String toString() {
+ return "VisibilityChangeEvent";
+ }
+ };
+
+ private final boolean isScrollerVisible;
+
+ private VisibilityChangeEvent(boolean isScrollerVisible) {
+ this.isScrollerVisible = isScrollerVisible;
+ }
+
+ /**
+ * Checks whether the scroll handle is currently visible or not
+ *
+ * @return <code>true</code> if the scroll handle is currently visible.
+ * <code>false</code> if not.
+ */
+ public boolean isScrollerVisible() {
+ return isScrollerVisible;
+ }
+
+ @Override
+ public Type<VisibilityHandler> getAssociatedType() {
+ return TYPE;
+ }
+
+ @Override
+ protected void dispatch(VisibilityHandler handler) {
+ handler.visibilityChanged(this);
+ }
+ }
+
+ /**
+ * The pixel size for OSX's invisible scrollbars.
+ * <p>
+ * Touch devices don't show a scrollbar at all, so the scrollbar size is
+ * irrelevant in their case. There doesn't seem to be any other popular
+ * platforms that has scrollbars similar to OSX. Thus, this behavior is
+ * tailored for OSX only, until additional platforms start behaving this
+ * way.
+ */
+ private static final int OSX_INVISIBLE_SCROLLBAR_FAKE_SIZE_PX = 13;
+
+ /**
+ * The allowed value inaccuracy when comparing two double-typed pixel
+ * values.
+ * <p>
+ * Since we're comparing pixels on a screen, epsilon must be less than 1.
+ * 0.49 was deemed a perfectly fine and beautifully round number.
+ */
+ private static final double PIXEL_EPSILON = 0.49d;
+
+ /**
+ * A representation of a single vertical scrollbar.
+ *
+ * @see VerticalScrollbarBundle#getElement()
+ */
+ final static class VerticalScrollbarBundle extends ScrollbarBundle {
+
+ @Override
+ public void setStylePrimaryName(String primaryStyleName) {
+ super.setStylePrimaryName(primaryStyleName);
+ root.addClassName(primaryStyleName + "-scroller-vertical");
+ }
+
+ @Override
+ protected void internalSetScrollPos(int px) {
+ root.setScrollTop(px);
+ }
+
+ @Override
+ protected int internalGetScrollPos() {
+ return root.getScrollTop();
+ }
+
+ @Override
+ protected void internalSetScrollSize(int px) {
+ scrollSizeElement.getStyle().setHeight(px, Unit.PX);
+ }
+
+ @Override
+ protected int internalGetScrollSize() {
+ return scrollSizeElement.getOffsetHeight();
+ }
+
+ @Override
+ protected void internalSetOffsetSize(double px) {
+ root.getStyle().setHeight(px, Unit.PX);
+ }
+
+ @Override
+ public double getOffsetSize() {
+ return root.getOffsetHeight();
+ }
+
+ @Override
+ protected void internalSetScrollbarThickness(int px) {
+ root.getStyle().setWidth(px, Unit.PX);
+ scrollSizeElement.getStyle().setWidth(px, Unit.PX);
+ }
+
+ @Override
+ protected int internalGetScrollbarThickness() {
+ return root.getOffsetWidth();
+ }
+
+ @Override
+ protected void forceScrollbar(boolean enable) {
+ if (enable) {
+ root.getStyle().setOverflowY(Overflow.SCROLL);
+ } else {
+ root.getStyle().clearOverflowY();
+ }
+ }
+ }
+
+ /**
+ * A representation of a single horizontal scrollbar.
+ *
+ * @see HorizontalScrollbarBundle#getElement()
+ */
+ final static class HorizontalScrollbarBundle extends ScrollbarBundle {
+
+ @Override
+ public void setStylePrimaryName(String primaryStyleName) {
+ super.setStylePrimaryName(primaryStyleName);
+ root.addClassName(primaryStyleName + "-scroller-horizontal");
+ }
+
+ @Override
+ protected void internalSetScrollPos(int px) {
+ root.setScrollLeft(px);
+ }
+
+ @Override
+ protected int internalGetScrollPos() {
+ return root.getScrollLeft();
+ }
+
+ @Override
+ protected void internalSetScrollSize(int px) {
+ scrollSizeElement.getStyle().setWidth(px, Unit.PX);
+ }
+
+ @Override
+ protected int internalGetScrollSize() {
+ return scrollSizeElement.getOffsetWidth();
+ }
+
+ @Override
+ protected void internalSetOffsetSize(double px) {
+ root.getStyle().setWidth(px, Unit.PX);
+ }
+
+ @Override
+ public double getOffsetSize() {
+ return root.getOffsetWidth();
+ }
+
+ @Override
+ protected void internalSetScrollbarThickness(int px) {
+ root.getStyle().setHeight(px, Unit.PX);
+ scrollSizeElement.getStyle().setHeight(px, Unit.PX);
+ }
+
+ @Override
+ protected int internalGetScrollbarThickness() {
+ return root.getOffsetHeight();
+ }
+
+ @Override
+ protected void forceScrollbar(boolean enable) {
+ if (enable) {
+ root.getStyle().setOverflowX(Overflow.SCROLL);
+ } else {
+ root.getStyle().clearOverflowX();
+ }
+ }
+ }
+
+ protected final Element root = DOM.createDiv();
+ protected final Element scrollSizeElement = DOM.createDiv();
+ protected boolean isInvisibleScrollbar = false;
+
+ private double scrollPos = 0;
+ private double maxScrollPos = 0;
+
+ private boolean scrollHandleIsVisible = false;
+
+ /** @deprecarted access via {@link #getHandlerManager()} instead. */
+ @Deprecated
+ private HandlerManager handlerManager;
+
+ private TemporaryResizer invisibleScrollbarTemporaryResizer = new TemporaryResizer();
+
+ private ScrollbarBundle() {
+ root.appendChild(scrollSizeElement);
+ }
+
+ protected abstract int internalGetScrollSize();
+
+ /**
+ * Sets the primary style name
+ *
+ * @param primaryStyleName
+ * The primary style name to use
+ */
+ public void setStylePrimaryName(String primaryStyleName) {
+ root.setClassName(primaryStyleName + "-scroller");
+ }
+
+ /**
+ * Gets the root element of this scrollbar-composition.
+ *
+ * @return the root element
+ */
+ public final Element getElement() {
+ return root;
+ }
+
+ /**
+ * Modifies the scroll position of this scrollbar by a number of pixels.
+ * <p>
+ * <em>Note:</em> Even though {@code double} values are used, they are
+ * currently only used as integers as large {@code int} (or small but fast
+ * {@code long}). This means, all values are truncated to zero decimal
+ * places.
+ *
+ * @param delta
+ * the delta in pixels to change the scroll position by
+ */
+ public final void setScrollPosByDelta(double delta) {
+ if (delta != 0) {
+ setScrollPos(getScrollPos() + delta);
+ }
+ }
+
+ /**
+ * Modifies {@link #root root's} dimensions in the axis the scrollbar is
+ * representing.
+ *
+ * @param px
+ * the new size of {@link #root} in the dimension this scrollbar
+ * is representing
+ */
+ protected abstract void internalSetOffsetSize(double px);
+
+ /**
+ * Sets the length of the scrollbar.
+ * <p>
+ * <em>Note:</em> Even though {@code double} values are used, they are
+ * currently only used as integers as large {@code int} (or small but fast
+ * {@code long}). This means, all values are truncated to zero decimal
+ * places.
+ *
+ * @param px
+ * the length of the scrollbar in pixels
+ */
+ public final void setOffsetSize(double px) {
+ internalSetOffsetSize(Math.max(0, truncate(px)));
+ forceScrollbar(showsScrollHandle());
+ recalculateMaxScrollPos();
+ fireVisibilityChangeIfNeeded();
+ }
+
+ /**
+ * Force the scrollbar to be visible with CSS. In practice, this means to
+ * set either <code>overflow-x</code> or <code>overflow-y</code> to "
+ * <code>scroll</code>" in the scrollbar's direction.
+ * <p>
+ * This is an IE8 workaround, since it doesn't always show scrollbars with
+ * <code>overflow: auto</code> enabled.
+ */
+ protected abstract void forceScrollbar(boolean enable);
+
+ /**
+ * Gets the length of the scrollbar
+ *
+ * @return the length of the scrollbar in pixels
+ */
+ public abstract double getOffsetSize();
+
+ /**
+ * Sets the scroll position of the scrollbar in the axis the scrollbar is
+ * representing.
+ * <p>
+ * <em>Note:</em> Even though {@code double} values are used, they are
+ * currently only used as integers as large {@code int} (or small but fast
+ * {@code long}). This means, all values are truncated to zero decimal
+ * places.
+ *
+ * @param px
+ * the new scroll position in pixels
+ */
+ public final void setScrollPos(double px) {
+ double oldScrollPos = scrollPos;
+ scrollPos = Math.max(0, Math.min(maxScrollPos, truncate(px)));
+
+ if (!pixelValuesEqual(oldScrollPos, scrollPos)) {
+ if (isInvisibleScrollbar) {
+ invisibleScrollbarTemporaryResizer.show();
+ }
+
+ /*
+ * This is where the value needs to be converted into an integer no
+ * matter how we flip it, since GWT expects an integer value.
+ * There's no point making a JSNI method that accepts doubles as the
+ * scroll position, since the browsers themselves don't support such
+ * large numbers (as of today, 25.3.2014). This double-ranged is
+ * only facilitating future virtual scrollbars.
+ */
+ internalSetScrollPos(toInt32(scrollPos));
+ }
+ }
+
+ /**
+ * Truncates a double such that no decimal places are retained.
+ * <p>
+ * E.g. {@code trunc(2.3d) == 2.0d} and {@code trunc(-2.3d) == -2.0d}.
+ *
+ * @param num
+ * the double value to be truncated
+ * @return the {@code num} value without any decimal digits
+ */
+ private static double truncate(double num) {
+ if (num > 0) {
+ return Math.floor(num);
+ } else {
+ return Math.ceil(num);
+ }
+ }
+
+ /**
+ * Modifies the element's scroll position (scrollTop or scrollLeft).
+ * <p>
+ * <em>Note:</em> The parameter here is a type of integer (instead of a
+ * double) by design. The browsers internally convert all double values into
+ * an integer value. To make this fact explicit, this API has chosen to
+ * force integers already at this level.
+ *
+ * @param px
+ * integer pixel value to scroll to
+ */
+ protected abstract void internalSetScrollPos(int px);
+
+ /**
+ * Gets the scroll position of the scrollbar in the axis the scrollbar is
+ * representing.
+ *
+ * @return the new scroll position in pixels
+ */
+ public final double getScrollPos() {
+ assert internalGetScrollPos() == toInt32(scrollPos) : "calculated scroll position ("
+ + toInt32(scrollPos)
+ + ") did not match the DOM element scroll position ("
+ + internalGetScrollPos() + ")";
+ return scrollPos;
+ }
+
+ /**
+ * Retrieves the element's scroll position (scrollTop or scrollLeft).
+ * <p>
+ * <em>Note:</em> The parameter here is a type of integer (instead of a
+ * double) by design. The browsers internally convert all double values into
+ * an integer value. To make this fact explicit, this API has chosen to
+ * force integers already at this level.
+ *
+ * @return integer pixel value of the scroll position
+ */
+ protected abstract int internalGetScrollPos();
+
+ /**
+ * Modifies {@link #scrollSizeElement scrollSizeElement's} dimensions in
+ * such a way that the scrollbar is able to scroll a certain number of
+ * pixels in the axis it is representing.
+ *
+ * @param px
+ * the new size of {@link #scrollSizeElement} in the dimension
+ * this scrollbar is representing
+ */
+ protected abstract void internalSetScrollSize(int px);
+
+ /**
+ * Sets the amount of pixels the scrollbar needs to be able to scroll
+ * through.
+ * <p>
+ * <em>Note:</em> Even though {@code double} values are used, they are
+ * currently only used as integers as large {@code int} (or small but fast
+ * {@code long}). This means, all values are truncated to zero decimal
+ * places.
+ *
+ * @param px
+ * the number of pixels the scrollbar should be able to scroll
+ * through
+ */
+ public final void setScrollSize(double px) {
+ internalSetScrollSize(toInt32(Math.max(0, truncate(px))));
+ forceScrollbar(showsScrollHandle());
+ recalculateMaxScrollPos();
+ fireVisibilityChangeIfNeeded();
+ }
+
+ /**
+ * Gets the amount of pixels the scrollbar needs to be able to scroll
+ * through.
+ *
+ * @return the number of pixels the scrollbar should be able to scroll
+ * through
+ */
+ public double getScrollSize() {
+ return internalGetScrollSize();
+ }
+
+ /**
+ * Modifies {@link #scrollSizeElement scrollSizeElement's} dimensions in the
+ * opposite axis to what the scrollbar is representing.
+ *
+ * @param px
+ * the dimension that {@link #scrollSizeElement} should take in
+ * the opposite axis to what the scrollbar is representing
+ */
+ protected abstract void internalSetScrollbarThickness(int px);
+
+ /**
+ * Sets the scrollbar's thickness.
+ * <p>
+ * If the thickness is set to 0, the scrollbar will be treated as an
+ * "invisible" scrollbar. This means, the DOM structure will be given a
+ * non-zero size, but {@link #getScrollbarThickness()} will still return the
+ * value 0.
+ *
+ * @param px
+ * the scrollbar's thickness in pixels
+ */
+ public final void setScrollbarThickness(int px) {
+ isInvisibleScrollbar = (px == 0);
+
+ if (isInvisibleScrollbar) {
+ Event.sinkEvents(root, Event.ONSCROLL);
+ Event.setEventListener(root, new EventListener() {
+ @Override
+ public void onBrowserEvent(Event event) {
+ invisibleScrollbarTemporaryResizer.show();
+ }
+ });
+ } else {
+ Event.sinkEvents(root, 0);
+ Event.setEventListener(root, null);
+ }
+
+ internalSetScrollbarThickness(Math.max(1, px));
+ }
+
+ /**
+ * Gets the scrollbar's thickness as defined in the DOM.
+ *
+ * @return the scrollbar's thickness as defined in the DOM, in pixels
+ */
+ protected abstract int internalGetScrollbarThickness();
+
+ /**
+ * Gets the scrollbar's thickness.
+ * <p>
+ * This value will differ from the value in the DOM, if the thickness was
+ * set to 0 with {@link #setScrollbarThickness(int)}, as the scrollbar is
+ * then treated as "invisible."
+ *
+ * @return the scrollbar's thickness in pixels
+ */
+ public final int getScrollbarThickness() {
+ if (!isInvisibleScrollbar) {
+ return internalGetScrollbarThickness();
+ } else {
+ return 0;
+ }
+ }
+
+ /**
+ * Checks whether the scrollbar's handle is visible.
+ * <p>
+ * In other words, this method checks whether the contents is larger than
+ * can visually fit in the element.
+ *
+ * @return <code>true</code> iff the scrollbar's handle is visible
+ */
+ public boolean showsScrollHandle() {
+ return getOffsetSize() < getScrollSize();
+ }
+
+ public void recalculateMaxScrollPos() {
+ double scrollSize = getScrollSize();
+ double offsetSize = getOffsetSize();
+ maxScrollPos = Math.max(0, scrollSize - offsetSize);
+
+ // make sure that the correct max scroll position is maintained.
+ setScrollPos(scrollPos);
+ }
+
+ /**
+ * This is a method that JSNI can call to synchronize the object state from
+ * the DOM.
+ */
+ private final void updateScrollPosFromDom() {
+ scrollPos = internalGetScrollPos();
+ }
+
+ protected HandlerManager getHandlerManager() {
+ if (handlerManager == null) {
+ handlerManager = new HandlerManager(this);
+ }
+ return handlerManager;
+ }
+
+ /**
+ * Adds handler for the scrollbar handle visibility.
+ *
+ * @param handler
+ * the {@link VisibilityHandler} to add
+ * @return {@link HandlerRegistration} used to remove the handler
+ */
+ public HandlerRegistration addVisibilityHandler(
+ final VisibilityHandler handler) {
+ return getHandlerManager().addHandler(VisibilityChangeEvent.TYPE,
+ handler);
+ }
+
+ private void fireVisibilityChangeIfNeeded() {
+ final boolean oldHandleIsVisible = scrollHandleIsVisible;
+ scrollHandleIsVisible = showsScrollHandle();
+ if (oldHandleIsVisible != scrollHandleIsVisible) {
+ final VisibilityChangeEvent event = new VisibilityChangeEvent(
+ scrollHandleIsVisible);
+ getHandlerManager().fireEvent(event);
+ }
+ }
+
+ /**
+ * Converts a double into an integer by JavaScript's terms.
+ * <p>
+ * Implementation copied from {@link Element#toInt32(double)}.
+ *
+ * @param val
+ * the double value to convert into an integer
+ * @return the double value converted to an integer
+ */
+ private static native int toInt32(double val)
+ /*-{
+ return val | 0;
+ }-*/;
+
+ /**
+ * Compares two double values with the error margin of
+ * {@link #PIXEL_EPSILON} (i.e. {@value #PIXEL_EPSILON})
+ *
+ * @param num1
+ * the first value for which to compare equality
+ * @param num2
+ * the second value for which to compare equality
+ */
+ private static boolean pixelValuesEqual(final double num1, final double num2) {
+ return Math.abs(num1 - num2) <= PIXEL_EPSILON;
+ }
+}
diff --git a/client/src/com/vaadin/client/ui/grid/datasources/ListDataSource.java b/client/src/com/vaadin/client/ui/grid/datasources/ListDataSource.java
new file mode 100644
index 0000000000..ef021a496a
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/datasources/ListDataSource.java
@@ -0,0 +1,438 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid.datasources;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.List;
+import java.util.ListIterator;
+
+import com.vaadin.client.data.DataChangeHandler;
+import com.vaadin.client.data.DataSource;
+import com.vaadin.shared.util.SharedUtil;
+
+/**
+ * A simple list based on an in-memory data source for simply adding a list of
+ * row pojos to the grid. Based on a wrapped list instance which supports adding
+ * and removing of items.
+ *
+ * <p>
+ * Usage:
+ *
+ * <pre>
+ * ListDataSource&lt;Integer&gt; ds = new ListDataSource&lt;Integer&gt;(1, 2, 3, 4);
+ *
+ * // Add item to the data source
+ * ds.asList().add(5);
+ *
+ * // Remove item from the data source
+ * ds.asList().remove(3);
+ *
+ * // Add multiple items
+ * ds.asList().addAll(Arrays.asList(5, 6, 7));
+ * </pre>
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public class ListDataSource<T> implements DataSource<T> {
+
+ private class RowHandleImpl extends RowHandle<T> {
+
+ private final T row;
+
+ public RowHandleImpl(T row) {
+ this.row = row;
+ }
+
+ @Override
+ public T getRow() {
+ /*
+ * We'll cheat here and don't throw an IllegalStateException even if
+ * this isn't pinned, because we know that the reference never gets
+ * stale.
+ */
+ return row;
+ }
+
+ @Override
+ public void pin() {
+ // NOOP, really
+ }
+
+ @Override
+ public void unpin() throws IllegalStateException {
+ /*
+ * Just to make things easier for everyone, we won't throw the
+ * exception, even in illegal situations.
+ */
+ }
+
+ @Override
+ protected boolean equalsExplicit(Object obj) {
+ if (obj instanceof ListDataSource.RowHandleImpl) {
+ /*
+ * Java prefers AbstractRemoteDataSource<?>.RowHandleImpl. I
+ * like the @SuppressWarnings more (keeps the line length in
+ * check.)
+ */
+ @SuppressWarnings("unchecked")
+ RowHandleImpl rhi = (RowHandleImpl) obj;
+ return SharedUtil.equals(row, rhi.row);
+ } else {
+ return false;
+ }
+ }
+
+ @Override
+ protected int hashCodeExplicit() {
+ return row.hashCode();
+ }
+ }
+
+ /**
+ * Wraps the datasource list and notifies the change handler of changing to
+ * the list
+ */
+ private class ListWrapper implements List<T> {
+
+ @Override
+ public int size() {
+ return ds.size();
+ }
+
+ @Override
+ public boolean isEmpty() {
+ return ds.isEmpty();
+ }
+
+ @Override
+ public boolean contains(Object o) {
+ return contains(o);
+ }
+
+ @Override
+ public Iterator<T> iterator() {
+ return new ListWrapperIterator(ds.iterator());
+ }
+
+ @Override
+ public Object[] toArray() {
+ return ds.toArray();
+ }
+
+ @Override
+ @SuppressWarnings("hiding")
+ public <T> T[] toArray(T[] a) {
+ return toArray(a);
+ }
+
+ @Override
+ public boolean add(T e) {
+ if (ds.add(e)) {
+ if (changeHandler != null) {
+ changeHandler.dataAdded(ds.size() - 1, 1);
+ }
+ return true;
+ }
+ return false;
+ }
+
+ @Override
+ public boolean remove(Object o) {
+ int index = ds.indexOf(o);
+ if (ds.remove(o)) {
+ if (changeHandler != null) {
+ changeHandler.dataRemoved(index, 1);
+ }
+ return true;
+ }
+ return false;
+ }
+
+ @Override
+ public boolean containsAll(Collection<?> c) {
+ return ds.containsAll(c);
+ }
+
+ @Override
+ public boolean addAll(Collection<? extends T> c) {
+ int idx = ds.size();
+ if (ds.addAll(c)) {
+ if (changeHandler != null) {
+ changeHandler.dataAdded(idx, c.size());
+ }
+ return true;
+ }
+ return false;
+ }
+
+ @Override
+ public boolean addAll(int index, Collection<? extends T> c) {
+ if (ds.addAll(index, c)) {
+ if (changeHandler != null) {
+ changeHandler.dataAdded(index, c.size());
+ }
+ return true;
+ }
+ return false;
+ }
+
+ @Override
+ public boolean removeAll(Collection<?> c) {
+ if (ds.removeAll(c)) {
+ if (changeHandler != null) {
+ // Have to update the whole list as the removal does not
+ // have to be a continuous range
+ changeHandler.dataUpdated(0, ds.size());
+ changeHandler.dataAvailable(0, ds.size());
+ }
+ return true;
+ }
+ return false;
+ }
+
+ @Override
+ public boolean retainAll(Collection<?> c) {
+ if (ds.retainAll(c)) {
+ if (changeHandler != null) {
+ // Have to update the whole list as the retain does not
+ // have to be a continuous range
+ changeHandler.dataUpdated(0, ds.size());
+ changeHandler.dataAvailable(0, ds.size());
+ }
+ return true;
+ }
+ return false;
+ }
+
+ @Override
+ public void clear() {
+ int size = ds.size();
+ ds.clear();
+ if (changeHandler != null) {
+ changeHandler.dataRemoved(0, size);
+ }
+ }
+
+ @Override
+ public T get(int index) {
+ return ds.get(index);
+ }
+
+ @Override
+ public T set(int index, T element) {
+ T prev = ds.set(index, element);
+ if (changeHandler != null) {
+ changeHandler.dataUpdated(index, 1);
+ }
+ return prev;
+ }
+
+ @Override
+ public void add(int index, T element) {
+ ds.add(index, element);
+ if (changeHandler != null) {
+ changeHandler.dataAdded(index, 1);
+ }
+ }
+
+ @Override
+ public T remove(int index) {
+ T removed = ds.remove(index);
+ if (changeHandler != null) {
+ changeHandler.dataRemoved(index, 1);
+ }
+ return removed;
+ }
+
+ @Override
+ public int indexOf(Object o) {
+ return ds.indexOf(o);
+ }
+
+ @Override
+ public int lastIndexOf(Object o) {
+ return ds.lastIndexOf(o);
+ }
+
+ @Override
+ public ListIterator<T> listIterator() {
+ // TODO could be implemented by a custom iterator.
+ throw new UnsupportedOperationException(
+ "List iterators not supported at this time.");
+ }
+
+ @Override
+ public ListIterator<T> listIterator(int index) {
+ // TODO could be implemented by a custom iterator.
+ throw new UnsupportedOperationException(
+ "List iterators not supported at this time.");
+ }
+
+ @Override
+ public List<T> subList(int fromIndex, int toIndex) {
+ throw new UnsupportedOperationException("Sub lists not supported.");
+ }
+ }
+
+ /**
+ * Iterator returned by {@link ListWrapper}
+ */
+ private class ListWrapperIterator implements Iterator<T> {
+
+ private final Iterator<T> iterator;
+
+ /**
+ * Constructs a new iterator
+ */
+ public ListWrapperIterator(Iterator<T> iterator) {
+ this.iterator = iterator;
+ }
+
+ @Override
+ public boolean hasNext() {
+ return iterator.hasNext();
+ }
+
+ @Override
+ public T next() {
+ return iterator.next();
+ }
+
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException(
+ "Iterator.remove() is not supported by this iterator.");
+ }
+ }
+
+ /**
+ * Datasource for providing row pojo's
+ */
+ private final List<T> ds;
+
+ /**
+ * Wrapper that wraps the data source
+ */
+ private final ListWrapper wrapper;
+
+ /**
+ * Handler for listening to changes in the underlying list.
+ */
+ private DataChangeHandler changeHandler;
+
+ /**
+ * Constructs a new list data source.
+ * <p>
+ * Note: Modifications to the original list will not be reflected in the
+ * data source after the data source has been constructed. To add or remove
+ * items to the data source after it has been constructed use
+ * {@link ListDataSource#asList()}.
+ *
+ *
+ * @param datasource
+ * The list to use for providing the data to the grid
+ */
+ public ListDataSource(List<T> datasource) {
+ if (datasource == null) {
+ throw new IllegalArgumentException("datasource cannot be null");
+ }
+ ds = new ArrayList<T>(datasource);
+ wrapper = new ListWrapper();
+ }
+
+ /**
+ * Constructs a data source with a set of rows. You can dynamically add and
+ * remove rows from the data source via the list you get from
+ * {@link ListDataSource#asList()}
+ *
+ * @param rows
+ * The rows to initially add to the data source
+ */
+ public ListDataSource(T... rows) {
+ if (rows == null) {
+ ds = new ArrayList<T>();
+ } else {
+ ds = new ArrayList<T>(Arrays.asList(rows));
+ }
+ wrapper = new ListWrapper();
+ }
+
+ @Override
+ public void ensureAvailability(int firstRowIndex, int numberOfRows) {
+ if (firstRowIndex >= ds.size()) {
+ throw new IllegalStateException(
+ "Trying to fetch rows outside of array");
+ }
+ changeHandler.dataAvailable(firstRowIndex, numberOfRows);
+ }
+
+ @Override
+ public T getRow(int rowIndex) {
+ return ds.get(rowIndex);
+ }
+
+ @Override
+ public int getEstimatedSize() {
+ return ds.size();
+ }
+
+ @Override
+ public void setDataChangeHandler(DataChangeHandler dataChangeHandler) {
+ this.changeHandler = dataChangeHandler;
+ }
+
+ /**
+ * Gets the list that backs this datasource. Any changes made to this list
+ * will be reflected in the datasource.
+ * <p>
+ * Note: The list is not the same list as passed into the data source via
+ * the constructor.
+ *
+ * @return Returns a list implementation that wraps the real list that backs
+ * the data source and provides events for the data source
+ * listeners.
+ */
+ public List<T> asList() {
+ return wrapper;
+ }
+
+ @Override
+ public RowHandle<T> getHandle(T row) throws IllegalStateException {
+ assert ds.contains(row) : "This data source doesn't contain the row "
+ + row;
+ return new RowHandleImpl(row);
+ }
+
+ /**
+ * Sort entire container according to a {@link Comparator}.
+ *
+ * @param comparator
+ * a comparator object, which compares two data source entries
+ * (beans/pojos)
+ */
+ public void sort(Comparator<T> comparator) {
+ Collections.sort(ds, comparator);
+ if (changeHandler != null) {
+ changeHandler.dataUpdated(0, ds.size());
+ }
+ }
+}
diff --git a/client/src/com/vaadin/client/ui/grid/datasources/ListSorter.java b/client/src/com/vaadin/client/ui/grid/datasources/ListSorter.java
new file mode 100644
index 0000000000..9e643825e9
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/datasources/ListSorter.java
@@ -0,0 +1,177 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid.datasources;
+
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import com.google.gwt.event.shared.HandlerRegistration;
+import com.vaadin.client.data.DataSource;
+import com.vaadin.client.ui.grid.Grid;
+import com.vaadin.client.ui.grid.GridColumn;
+import com.vaadin.client.ui.grid.sort.SortEvent;
+import com.vaadin.client.ui.grid.sort.SortEventHandler;
+import com.vaadin.client.ui.grid.sort.SortOrder;
+import com.vaadin.shared.ui.grid.SortDirection;
+
+/**
+ * Provides sorting facility from Grid for the {@link ListDataSource} in-memory
+ * data source.
+ *
+ * @since
+ * @author Vaadin Ltd
+ * @param <T>
+ * Grid row data type
+ */
+public class ListSorter<T> {
+
+ private Grid<T> grid;
+ private Map<GridColumn<?, T>, Comparator<?>> comparators;
+ private HandlerRegistration sortHandlerRegistration;
+
+ public ListSorter(Grid<T> grid) {
+
+ if (grid == null) {
+ throw new IllegalArgumentException("Grid can not be null");
+ }
+
+ this.grid = grid;
+ comparators = new HashMap<GridColumn<?, T>, Comparator<?>>();
+
+ sortHandlerRegistration = grid
+ .addSortHandler(new SortEventHandler<T>() {
+ @Override
+ public void sort(SortEvent<T> event) {
+ ListSorter.this.sort(event.getOrder());
+ }
+ });
+ }
+
+ /**
+ * Detach this Sorter from the Grid. This unregisters the sort event handler
+ * which was used to apply sorting to the ListDataSource.
+ */
+ public void removeFromGrid() {
+ sortHandlerRegistration.removeHandler();
+ }
+
+ /**
+ * Assign or remove a comparator for a column. This comparator method, if
+ * defined, is always used in favour of 'natural' comparison of objects
+ * (i.e. the compareTo of objects implementing the Comparable interface,
+ * which includes all standard data classes like String, Number derivatives
+ * and Dates). Any existing comparator can be removed by passing in a
+ * non-null GridColumn and a null Comparator.
+ *
+ * @param column
+ * a grid column. May not be null.
+ * @param comparator
+ * comparator method for the values returned by the grid column.
+ * If null, any existing comparator is removed.
+ */
+ public <C> void setComparator(GridColumn<C, T> column,
+ Comparator<C> comparator) {
+ if (column == null) {
+ throw new IllegalArgumentException(
+ "Column reference can not be null");
+ }
+ if (comparator == null) {
+ comparators.remove(column);
+ } else {
+ comparators.put(column, comparator);
+ }
+ }
+
+ /**
+ * Retrieve the comparator assigned for a specific grid column.
+ *
+ * @param column
+ * a grid column. May not be null.
+ * @return a comparator, or null if no comparator for the specified grid
+ * column has been set.
+ */
+ @SuppressWarnings("unchecked")
+ public <C> Comparator<C> getComparator(GridColumn<C, T> column) {
+ if (column == null) {
+ throw new IllegalArgumentException(
+ "Column reference can not be null");
+ }
+ return (Comparator<C>) comparators.get(column);
+ }
+
+ /**
+ * Remove all comparator mappings. Useful if the data source has changed but
+ * this Sorter is being re-used.
+ */
+ public void clearComparators() {
+ comparators.clear();
+ }
+
+ /**
+ * Apply sorting to the current ListDataSource.
+ *
+ * @param order
+ * the sort order list provided by the grid sort event
+ */
+ private void sort(final List<SortOrder> order) {
+ DataSource<T> ds = grid.getDataSource();
+ if (!(ds instanceof ListDataSource)) {
+ throw new IllegalStateException("Grid " + grid
+ + " data source is not a ListDataSource!");
+ }
+
+ ((ListDataSource<T>) ds).sort(new Comparator<T>() {
+
+ @Override
+ @SuppressWarnings({ "rawtypes", "unchecked" })
+ public int compare(T a, T b) {
+
+ for (SortOrder o : order) {
+
+ GridColumn column = o.getColumn();
+ Comparator cmp = ListSorter.this.comparators.get(column);
+ int result = 0;
+ Object value_a = column.getValue(a);
+ Object value_b = column.getValue(b);
+ if (cmp != null) {
+ result = cmp.compare(value_a, value_b);
+ } else {
+ if (!(value_a instanceof Comparable)) {
+ throw new IllegalStateException("Column " + column
+ + " has no assigned comparator and value "
+ + value_a + " isn't naturally comparable");
+ }
+ result = ((Comparable) value_a).compareTo(value_b);
+ }
+
+ if (result != 0) {
+ return o.getDirection() == SortDirection.ASCENDING ? result
+ : -result;
+ }
+ }
+
+ if (order.size() > 0) {
+ return order.get(0).getDirection() == SortDirection.ASCENDING ? a
+ .hashCode() - b.hashCode()
+ : b.hashCode() - a.hashCode();
+ }
+ return a.hashCode() - b.hashCode();
+ }
+ });
+ }
+}
diff --git a/client/src/com/vaadin/client/ui/grid/keyevents/AbstractGridKeyEventHandler.java b/client/src/com/vaadin/client/ui/grid/keyevents/AbstractGridKeyEventHandler.java
new file mode 100644
index 0000000000..28a85924fa
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/keyevents/AbstractGridKeyEventHandler.java
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid.keyevents;
+
+import com.google.gwt.event.shared.EventHandler;
+
+/**
+ * Base interface of all handlers for {@link AbstractGridKeyEvent}s.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public abstract interface AbstractGridKeyEventHandler extends EventHandler {
+
+ public abstract interface GridKeyDownHandler<T> extends
+ AbstractGridKeyEventHandler {
+ public void onKeyDown(GridKeyDownEvent<T> event);
+ }
+
+ public abstract interface GridKeyUpHandler<T> extends
+ AbstractGridKeyEventHandler {
+ public void onKeyUp(GridKeyUpEvent<T> event);
+ }
+
+ public abstract interface GridKeyPressHandler<T> extends
+ AbstractGridKeyEventHandler {
+ public void onKeyPress(GridKeyPressEvent<T> event);
+ }
+
+}
diff --git a/client/src/com/vaadin/client/ui/grid/keyevents/BodyKeyDownHandler.java b/client/src/com/vaadin/client/ui/grid/keyevents/BodyKeyDownHandler.java
new file mode 100644
index 0000000000..a3b76ea5d7
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/keyevents/BodyKeyDownHandler.java
@@ -0,0 +1,28 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid.keyevents;
+
+import com.vaadin.client.ui.grid.keyevents.AbstractGridKeyEventHandler.GridKeyDownHandler;
+
+/**
+ * Handler for {@link GridKeyDownEvent}s that happen when active cell is in the
+ * body of the Grid.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public interface BodyKeyDownHandler<T> extends GridKeyDownHandler<T> {
+}
diff --git a/client/src/com/vaadin/client/ui/grid/keyevents/BodyKeyPressHandler.java b/client/src/com/vaadin/client/ui/grid/keyevents/BodyKeyPressHandler.java
new file mode 100644
index 0000000000..5548994cf9
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/keyevents/BodyKeyPressHandler.java
@@ -0,0 +1,28 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid.keyevents;
+
+import com.vaadin.client.ui.grid.keyevents.AbstractGridKeyEventHandler.GridKeyPressHandler;
+
+/**
+ * Handler for {@link GridKeyPressEvent}s that happen when active cell is in the
+ * body of the Grid.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public interface BodyKeyPressHandler<T> extends GridKeyPressHandler<T> {
+} \ No newline at end of file
diff --git a/client/src/com/vaadin/client/ui/grid/keyevents/BodyKeyUpHandler.java b/client/src/com/vaadin/client/ui/grid/keyevents/BodyKeyUpHandler.java
new file mode 100644
index 0000000000..33b4fc81fe
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/keyevents/BodyKeyUpHandler.java
@@ -0,0 +1,28 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid.keyevents;
+
+import com.vaadin.client.ui.grid.keyevents.AbstractGridKeyEventHandler.GridKeyUpHandler;
+
+/**
+ * Handler for {@link GridKeyUpEvent}s that happen when active cell is in the
+ * body of the Grid.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public interface BodyKeyUpHandler<T> extends GridKeyUpHandler<T> {
+} \ No newline at end of file
diff --git a/client/src/com/vaadin/client/ui/grid/keyevents/FooterKeyDownHandler.java b/client/src/com/vaadin/client/ui/grid/keyevents/FooterKeyDownHandler.java
new file mode 100644
index 0000000000..e90f52e736
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/keyevents/FooterKeyDownHandler.java
@@ -0,0 +1,28 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid.keyevents;
+
+import com.vaadin.client.ui.grid.keyevents.AbstractGridKeyEventHandler.GridKeyDownHandler;
+
+/**
+ * Handler for {@link GridKeyDownEvent}s that happen when active cell is in the
+ * footer of the Grid.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public interface FooterKeyDownHandler<T> extends GridKeyDownHandler<T> {
+}
diff --git a/client/src/com/vaadin/client/ui/grid/keyevents/FooterKeyPressHandler.java b/client/src/com/vaadin/client/ui/grid/keyevents/FooterKeyPressHandler.java
new file mode 100644
index 0000000000..58f48f36f5
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/keyevents/FooterKeyPressHandler.java
@@ -0,0 +1,28 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid.keyevents;
+
+import com.vaadin.client.ui.grid.keyevents.AbstractGridKeyEventHandler.GridKeyPressHandler;
+
+/**
+ * Handler for {@link GridKeyPressEvent}s that happen when active cell is in the
+ * footer of the Grid.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public interface FooterKeyPressHandler<T> extends GridKeyPressHandler<T> {
+} \ No newline at end of file
diff --git a/client/src/com/vaadin/client/ui/grid/keyevents/FooterKeyUpHandler.java b/client/src/com/vaadin/client/ui/grid/keyevents/FooterKeyUpHandler.java
new file mode 100644
index 0000000000..d6bcddf710
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/keyevents/FooterKeyUpHandler.java
@@ -0,0 +1,28 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid.keyevents;
+
+import com.vaadin.client.ui.grid.keyevents.AbstractGridKeyEventHandler.GridKeyUpHandler;
+
+/**
+ * Handler for {@link GridKeyUpEvent}s that happen when active cell is in the
+ * footer of the Grid.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public interface FooterKeyUpHandler<T> extends GridKeyUpHandler<T> {
+} \ No newline at end of file
diff --git a/client/src/com/vaadin/client/ui/grid/keyevents/GridKeyDownEvent.java b/client/src/com/vaadin/client/ui/grid/keyevents/GridKeyDownEvent.java
new file mode 100644
index 0000000000..8af65dbf49
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/keyevents/GridKeyDownEvent.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid.keyevents;
+
+import com.google.gwt.dom.client.BrowserEvents;
+import com.vaadin.client.ui.grid.Grid;
+import com.vaadin.client.ui.grid.Grid.AbstractGridKeyEvent;
+import com.vaadin.client.ui.grid.keyevents.AbstractGridKeyEventHandler.GridKeyDownHandler;
+
+/**
+ * Represents native key down event in Grid.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public class GridKeyDownEvent<T> extends
+ AbstractGridKeyEvent<T, GridKeyDownHandler<T>> {
+
+ public GridKeyDownEvent(Grid<T> grid) {
+ super(grid);
+ }
+
+ @Override
+ protected void dispatch(GridKeyDownHandler<T> handler) {
+ super.dispatch(handler);
+ if ((activeSection == GridSection.BODY && handler instanceof BodyKeyDownHandler)
+ || (activeSection == GridSection.HEADER && handler instanceof HeaderKeyDownHandler)
+ || (activeSection == GridSection.FOOTER && handler instanceof FooterKeyDownHandler)) {
+ handler.onKeyDown(this);
+ }
+ }
+
+ @Override
+ protected String getBrowserEventType() {
+ return BrowserEvents.KEYDOWN;
+ }
+
+}
diff --git a/client/src/com/vaadin/client/ui/grid/keyevents/GridKeyPressEvent.java b/client/src/com/vaadin/client/ui/grid/keyevents/GridKeyPressEvent.java
new file mode 100644
index 0000000000..6f06bc6674
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/keyevents/GridKeyPressEvent.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid.keyevents;
+
+import com.google.gwt.dom.client.BrowserEvents;
+import com.vaadin.client.ui.grid.Grid;
+import com.vaadin.client.ui.grid.Grid.AbstractGridKeyEvent;
+import com.vaadin.client.ui.grid.keyevents.AbstractGridKeyEventHandler.GridKeyPressHandler;
+
+/**
+ * Represents native key press event in Grid.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public class GridKeyPressEvent<T> extends
+ AbstractGridKeyEvent<T, GridKeyPressHandler<T>> {
+
+ public GridKeyPressEvent(Grid<T> grid) {
+ super(grid);
+ }
+
+ @Override
+ protected void dispatch(GridKeyPressHandler<T> handler) {
+ super.dispatch(handler);
+ if ((activeSection == GridSection.BODY && handler instanceof BodyKeyPressHandler)
+ || (activeSection == GridSection.HEADER && handler instanceof HeaderKeyPressHandler)
+ || (activeSection == GridSection.FOOTER && handler instanceof FooterKeyPressHandler)) {
+ handler.onKeyPress(this);
+ }
+ }
+
+ @Override
+ protected String getBrowserEventType() {
+ return BrowserEvents.KEYPRESS;
+ }
+
+} \ No newline at end of file
diff --git a/client/src/com/vaadin/client/ui/grid/keyevents/GridKeyUpEvent.java b/client/src/com/vaadin/client/ui/grid/keyevents/GridKeyUpEvent.java
new file mode 100644
index 0000000000..d289dbae98
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/keyevents/GridKeyUpEvent.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid.keyevents;
+
+import com.google.gwt.dom.client.BrowserEvents;
+import com.vaadin.client.ui.grid.Grid;
+import com.vaadin.client.ui.grid.Grid.AbstractGridKeyEvent;
+import com.vaadin.client.ui.grid.keyevents.AbstractGridKeyEventHandler.GridKeyUpHandler;
+
+/**
+ * Represents native key up event in Grid.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public class GridKeyUpEvent<T> extends
+ AbstractGridKeyEvent<T, GridKeyUpHandler<T>> {
+
+ public GridKeyUpEvent(Grid<T> grid) {
+ super(grid);
+ }
+
+ @Override
+ protected void dispatch(GridKeyUpHandler<T> handler) {
+ super.dispatch(handler);
+ if ((activeSection == GridSection.BODY && handler instanceof BodyKeyUpHandler)
+ || (activeSection == GridSection.HEADER && handler instanceof HeaderKeyUpHandler)
+ || (activeSection == GridSection.FOOTER && handler instanceof FooterKeyUpHandler)) {
+ handler.onKeyUp(this);
+ }
+ }
+
+ @Override
+ protected String getBrowserEventType() {
+ return BrowserEvents.KEYUP;
+ }
+
+}
diff --git a/client/src/com/vaadin/client/ui/grid/keyevents/HeaderKeyDownHandler.java b/client/src/com/vaadin/client/ui/grid/keyevents/HeaderKeyDownHandler.java
new file mode 100644
index 0000000000..28c9a8e056
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/keyevents/HeaderKeyDownHandler.java
@@ -0,0 +1,28 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid.keyevents;
+
+import com.vaadin.client.ui.grid.keyevents.AbstractGridKeyEventHandler.GridKeyDownHandler;
+
+/**
+ * Handler for {@link GridKeyDownEvent}s that happen when active cell is in the
+ * header of the Grid.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public interface HeaderKeyDownHandler<T> extends GridKeyDownHandler<T> {
+}
diff --git a/client/src/com/vaadin/client/ui/grid/keyevents/HeaderKeyPressHandler.java b/client/src/com/vaadin/client/ui/grid/keyevents/HeaderKeyPressHandler.java
new file mode 100644
index 0000000000..607c30493d
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/keyevents/HeaderKeyPressHandler.java
@@ -0,0 +1,28 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid.keyevents;
+
+import com.vaadin.client.ui.grid.keyevents.AbstractGridKeyEventHandler.GridKeyPressHandler;
+
+/**
+ * Handler for {@link GridKeyPressEvent}s that happen when active cell is in the
+ * header of the Grid.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public interface HeaderKeyPressHandler<T> extends GridKeyPressHandler<T> {
+} \ No newline at end of file
diff --git a/client/src/com/vaadin/client/ui/grid/keyevents/HeaderKeyUpHandler.java b/client/src/com/vaadin/client/ui/grid/keyevents/HeaderKeyUpHandler.java
new file mode 100644
index 0000000000..bfa3dde79b
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/keyevents/HeaderKeyUpHandler.java
@@ -0,0 +1,28 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid.keyevents;
+
+import com.vaadin.client.ui.grid.keyevents.AbstractGridKeyEventHandler.GridKeyUpHandler;
+
+/**
+ * Handler for {@link GridKeyUpEvent}s that happen when active cell is in the
+ * header of the Grid.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public interface HeaderKeyUpHandler<T> extends GridKeyUpHandler<T> {
+} \ No newline at end of file
diff --git a/client/src/com/vaadin/client/ui/grid/renderers/AbstractRendererConnector.java b/client/src/com/vaadin/client/ui/grid/renderers/AbstractRendererConnector.java
new file mode 100644
index 0000000000..cad5af97df
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/renderers/AbstractRendererConnector.java
@@ -0,0 +1,156 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid.renderers;
+
+import com.google.gwt.json.client.JSONValue;
+import com.vaadin.client.ServerConnector;
+import com.vaadin.client.Util;
+import com.vaadin.client.communication.JsonDecoder;
+import com.vaadin.client.extensions.AbstractExtensionConnector;
+import com.vaadin.client.metadata.NoDataException;
+import com.vaadin.client.metadata.Type;
+import com.vaadin.client.metadata.TypeData;
+import com.vaadin.client.metadata.TypeDataStore;
+import com.vaadin.client.ui.grid.GridConnector;
+import com.vaadin.client.ui.grid.Renderer;
+
+/**
+ * An abstract base class for renderer connectors. A renderer connector is used
+ * to link a client-side {@link Renderer} to a server-side
+ * {@link com.vaadin.ui.components.grid.Renderer Renderer}. As a connector, it
+ * can use the regular Vaadin RPC and shared state mechanism to pass additional
+ * state and information between the client and the server. This base class
+ * itself only uses the basic
+ * {@link com.vaadin.shared.communication.SharedState SharedState} and no RPC
+ * interfaces.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public abstract class AbstractRendererConnector<T> extends
+ AbstractExtensionConnector {
+
+ private Renderer<T> renderer = null;
+
+ private final Type presentationType = TypeDataStore
+ .getPresentationType(this.getClass());
+
+ protected AbstractRendererConnector() {
+ if (presentationType == null) {
+ throw new IllegalStateException(
+ "No presentation type found for "
+ + Util.getSimpleName(this)
+ + ". This may be caused by some unspecified problem in widgetset compilation.");
+ }
+ }
+
+ /**
+ * Returns the renderer associated with this renderer connector.
+ * <p>
+ * A subclass of AbstractRendererConnector should override this method as
+ * shown below. The framework uses
+ * {@link com.google.gwt.core.client.GWT#create(Class) GWT.create(Class)} to
+ * create a renderer based on the return type of the overridden method, but
+ * only if {@link #createRenderer()} is not overridden as well:
+ *
+ * <pre>
+ * public MyRenderer getRenderer() {
+ * return (MyRenderer) super.getRenderer();
+ * }
+ * </pre>
+ *
+ * @return the renderer bound to this connector
+ */
+ public Renderer<T> getRenderer() {
+ if (renderer == null) {
+ renderer = createRenderer();
+ }
+ return renderer;
+ }
+
+ /**
+ * Creates a new Renderer instance associated with this renderer connector.
+ * <p>
+ * You should typically not override this method since the framework by
+ * default generates an implementation that uses {@link GWT#create(Class)}
+ * to create a renderer of the same type as returned by the most specific
+ * override of {@link #getRenderer()}. If you do override the method, you
+ * can't call <code>super.createRenderer()</code> since the metadata needed
+ * for that implementation is not generated if there's an override of the
+ * method.
+ *
+ * @return a new renderer to be used with this connector
+ */
+ protected Renderer<T> createRenderer() {
+ // TODO generate type data
+ Type type = TypeData.getType(getClass());
+ try {
+ Type rendererType = type.getMethod("getRenderer").getReturnType();
+ @SuppressWarnings("unchecked")
+ Renderer<T> instance = (Renderer<T>) rendererType.createInstance();
+ return instance;
+ } catch (NoDataException e) {
+ throw new IllegalStateException(
+ "Default implementation of createRenderer() does not work for "
+ + Util.getSimpleName(this)
+ + ". This might be caused by explicitely using "
+ + "super.createRenderer() or some unspecified "
+ + "problem with the widgetset compilation.", e);
+ }
+ }
+
+ /**
+ * Decodes the given JSON value into a value of type T so it can be passed
+ * to the {@link #getRenderer() renderer}.
+ *
+ * @param value
+ * the value to decode
+ * @return the decoded value of {@code value}
+ */
+ public T decode(JSONValue value) {
+ @SuppressWarnings("unchecked")
+ T decodedValue = (T) JsonDecoder.decodeValue(presentationType, value,
+ null, getConnection());
+ return decodedValue;
+ }
+
+ @Override
+ @Deprecated
+ protected void extend(ServerConnector target) {
+ // NOOP
+ }
+
+ /**
+ * Gets the row key for a row index.
+ * <p>
+ * In case this renderer wants be able to identify a row in such a way that
+ * the server also understands it, the row key is used for that. Rows are
+ * identified by unified keys between the client and the server.
+ *
+ * @param index
+ * the row index for which to get the row key
+ * @return the row key for the row at {@code index}
+ */
+ protected String getRowKey(int index) {
+ final ServerConnector parent = getParent();
+ if (parent instanceof GridConnector) {
+ return ((GridConnector) parent).getRowKey(index);
+ } else {
+ throw new IllegalStateException("Renderers can only be used "
+ + "with a Grid.");
+ }
+ }
+}
diff --git a/client/src/com/vaadin/client/ui/grid/renderers/ComplexRenderer.java b/client/src/com/vaadin/client/ui/grid/renderers/ComplexRenderer.java
new file mode 100644
index 0000000000..f0c95e2ddf
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/renderers/ComplexRenderer.java
@@ -0,0 +1,153 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid.renderers;
+
+import java.util.Collection;
+
+import com.google.gwt.dom.client.Element;
+import com.google.gwt.dom.client.NativeEvent;
+import com.google.gwt.dom.client.Node;
+import com.google.gwt.dom.client.Style.Visibility;
+import com.vaadin.client.ui.grid.Cell;
+import com.vaadin.client.ui.grid.FlyweightCell;
+import com.vaadin.client.ui.grid.Renderer;
+
+/**
+ * Base class for renderers that needs initialization and destruction logic
+ * (override {@link #init(FlyweightCell) and #destroy(FlyweightCell) } and event
+ * handling (see {@link #onBrowserEvent(Cell, NativeEvent)},
+ * {@link #getConsumedEvents()} and {@link #onActivate()}.
+ *
+ * <p>
+ * Also provides a helper method for hiding the cell contents by overriding
+ * {@link #setContentVisible(FlyweightCell, boolean)}
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public abstract class ComplexRenderer<T> implements Renderer<T> {
+
+ /**
+ * Called at initialization stage. Perform any initialization here e.g.
+ * attach handlers, attach widgets etc.
+ *
+ * @param cell
+ * The cell. Note that the cell is not to be stored outside of
+ * the method as the cell install will change. See
+ * {@link FlyweightCell}
+ */
+ public void init(FlyweightCell cell) {
+ // Implement if needed
+ }
+
+ /**
+ * Called after the cell is deemed to be destroyed and no longer used by the
+ * Grid. Called after the cell element is detached from the DOM.
+ *
+ * @param cell
+ * The cell. Note that the cell is not to be stored outside of
+ * the method as the cell install will change. See
+ * {@link FlyweightCell}
+ */
+ public void destroy(FlyweightCell cell) {
+ // Implement if needed
+ }
+
+ /**
+ * Returns the events that the renderer should consume. These are also the
+ * events that the Grid will pass to
+ * {@link #onBrowserEvent(Cell, NativeEvent)} when they occur.
+ * <code>null</code> if no events are consumed
+ *
+ * @return the consumed events, or null if no events are consumed
+ *
+ * @see com.google.gwt.dom.client.BrowserEvents
+ */
+ public Collection<String> getConsumedEvents() {
+ return null;
+ }
+
+ /**
+ * Called whenever a registered event is triggered in the column the
+ * renderer renders.
+ * <p>
+ * The events that triggers this needs to be returned by the
+ * {@link #getConsumedEvents()} method.
+ * <p>
+ * Returns boolean telling if the event has been completely handled and
+ * should not cause any other actions.
+ *
+ * @param cell
+ * Object containing information about the cell the event was
+ * triggered on.
+ *
+ * @param event
+ * The original DOM event
+ * @return true if event should not be handled by grid
+ */
+ public boolean onBrowserEvent(Cell cell, NativeEvent event) {
+ return false;
+ }
+
+ /**
+ * Used by Grid to toggle whether to show actual data or just an empty
+ * placeholder while data is loading. This method is invoked whenever a cell
+ * changes between data being available and data missing.
+ * <p>
+ * Default implementation hides content by setting visibility: hidden to all
+ * elements inside the cell. Text nodes are left as is - renderers that add
+ * such to the root element need to implement explicit support hiding them.
+ *
+ * @param cell
+ * The cell
+ * @param hasData
+ * Has the cell content been loaded from the data source
+ *
+ */
+ public void setContentVisible(FlyweightCell cell, boolean hasData) {
+ Element cellElement = cell.getElement();
+ for (int n = 0; n < cellElement.getChildCount(); n++) {
+ Node node = cellElement.getChild(n);
+ if (Element.is(node)) {
+ Element e = Element.as(node);
+ if (hasData) {
+ e.getStyle().clearVisibility();
+ } else {
+ e.getStyle().setVisibility(Visibility.HIDDEN);
+ }
+ }
+ }
+ }
+
+ /**
+ * Called when the cell is "activated" by pressing <code>enter</code>,
+ * double clicking or performing a double tap on the cell.
+ *
+ * @return <code>true</code> if event was handled and should not be
+ * interpreted as a generic gesture by Grid.
+ */
+ public boolean onActivate() {
+ return false;
+ }
+
+ /**
+ * Called when the renderer is deemed to be destroyed and no longer used by
+ * the Grid.
+ */
+ public void destroy() {
+ // Implement if needed
+ }
+}
diff --git a/client/src/com/vaadin/client/ui/grid/renderers/DateRenderer.java b/client/src/com/vaadin/client/ui/grid/renderers/DateRenderer.java
new file mode 100644
index 0000000000..fc7d3ac833
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/renderers/DateRenderer.java
@@ -0,0 +1,98 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid.renderers;
+
+import java.util.Date;
+
+import com.google.gwt.i18n.client.DateTimeFormat;
+import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
+import com.google.gwt.i18n.client.TimeZone;
+import com.vaadin.client.ui.grid.FlyweightCell;
+import com.vaadin.client.ui.grid.Renderer;
+
+/**
+ * A renderer for rendering dates into cells
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public class DateRenderer implements Renderer<Date> {
+
+ private DateTimeFormat format = DateTimeFormat
+ .getFormat(PredefinedFormat.DATE_TIME_SHORT);
+
+ // Calendar is unavailable for GWT
+ @SuppressWarnings("deprecation")
+ private TimeZone timeZone = TimeZone.createTimeZone(new Date()
+ .getTimezoneOffset());
+
+ @Override
+ public void render(FlyweightCell cell, Date date) {
+ String dateStr = format.format(date, timeZone);
+ cell.getElement().setInnerText(dateStr);
+ }
+
+ /**
+ * Gets the format of how the date is formatted.
+ *
+ * @return the format
+ * @see <a
+ * href="http://www.gwtproject.org/javadoc/latest/com/google/gwt/i18n/client/DateTimeFormat.html">GWT
+ * documentation on DateTimeFormat</a>
+ */
+ public DateTimeFormat getFormat() {
+ return format;
+ }
+
+ /**
+ * Sets the format used for formatting the dates.
+ *
+ * @param format
+ * the format to set
+ * @see <a
+ * href="http://www.gwtproject.org/javadoc/latest/com/google/gwt/i18n/client/DateTimeFormat.html">GWT
+ * documentation on DateTimeFormat</a>
+ */
+ public void setFormat(DateTimeFormat format) {
+ if (format == null) {
+ throw new IllegalArgumentException("Format should not be null");
+ }
+ this.format = format;
+ }
+
+ /**
+ * Returns the time zone of the date.
+ *
+ * @return the time zone
+ */
+ public TimeZone getTimeZone() {
+ return timeZone;
+ }
+
+ /**
+ * Sets the time zone of the the date. By default uses the time zone of the
+ * browser.
+ *
+ * @param timeZone
+ * the timeZone to set
+ */
+ public void setTimeZone(TimeZone timeZone) {
+ if (timeZone == null) {
+ throw new IllegalArgumentException("Timezone should not be null");
+ }
+ this.timeZone = timeZone;
+ }
+}
diff --git a/client/src/com/vaadin/client/ui/grid/renderers/DateRendererConnector.java b/client/src/com/vaadin/client/ui/grid/renderers/DateRendererConnector.java
new file mode 100644
index 0000000000..52ae7d9b6b
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/renderers/DateRendererConnector.java
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid.renderers;
+
+import com.vaadin.shared.ui.Connect;
+
+/**
+ * A connector for {@link com.vaadin.ui.components.grid.renderers.DateRenderer
+ * DateRenderer}.
+ * <p>
+ * The server-side Renderer operates on dates, but the data is serialized as a
+ * string, and displayed as-is on the client side. This is to be able to support
+ * the server's locale.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+@Connect(com.vaadin.ui.components.grid.renderers.DateRenderer.class)
+public class DateRendererConnector extends TextRendererConnector {
+ // No implementation needed
+}
diff --git a/client/src/com/vaadin/client/ui/grid/renderers/HtmlRenderer.java b/client/src/com/vaadin/client/ui/grid/renderers/HtmlRenderer.java
new file mode 100644
index 0000000000..36c5d2bb0f
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/renderers/HtmlRenderer.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid.renderers;
+
+import com.google.gwt.safehtml.shared.SafeHtml;
+import com.google.gwt.safehtml.shared.SafeHtmlUtils;
+import com.vaadin.client.ui.grid.FlyweightCell;
+import com.vaadin.client.ui.grid.Renderer;
+
+/**
+ * Renders a string as HTML into a cell.
+ * <p>
+ * The html string is rendered as is without any escaping. It is up to the
+ * developer to ensure that the html string honors the {@link SafeHtml}
+ * contract. For more information see
+ * {@link SafeHtmlUtils#fromSafeConstant(String)}.
+ *
+ * @since
+ * @author Vaadin Ltd
+ * @see SafeHtmlUtils#fromSafeConstant(String)
+ */
+public class HtmlRenderer implements Renderer<String> {
+
+ @Override
+ public void render(FlyweightCell cell, String htmlString) {
+ cell.getElement().setInnerSafeHtml(
+ SafeHtmlUtils.fromSafeConstant(htmlString));
+ }
+}
diff --git a/client/src/com/vaadin/client/ui/grid/renderers/NumberRenderer.java b/client/src/com/vaadin/client/ui/grid/renderers/NumberRenderer.java
new file mode 100644
index 0000000000..aa23bc2370
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/renderers/NumberRenderer.java
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid.renderers;
+
+import com.google.gwt.i18n.client.NumberFormat;
+import com.vaadin.client.ui.grid.FlyweightCell;
+import com.vaadin.client.ui.grid.Renderer;
+
+/**
+ * Renders a number into a cell using a specific {@link NumberFormat}. By
+ * default uses the default number format returned by
+ * {@link NumberFormat#getDecimalFormat()}.
+ *
+ * @since
+ * @author Vaadin Ltd
+ * @param <T>
+ * The number type to render.
+ */
+public class NumberRenderer<T extends Number> implements Renderer<T> {
+
+ private NumberFormat format = NumberFormat.getDecimalFormat();
+
+ /**
+ * Gets the number format that the number should be formatted in.
+ *
+ * @return the number format used to render the number
+ */
+ public NumberFormat getFormat() {
+ return format;
+ }
+
+ /**
+ * Sets the number format to use for formatting the number.
+ *
+ * @param format
+ * the format to use
+ * @throws IllegalArgumentException
+ * when the format is null
+ */
+ public void setFormat(NumberFormat format) throws IllegalArgumentException {
+ if (format == null) {
+ throw new IllegalArgumentException("Format cannot be null");
+ }
+ this.format = format;
+ }
+
+ @Override
+ public void render(FlyweightCell cell, Number number) {
+ cell.getElement().setInnerText(format.format(number));
+ }
+}
diff --git a/client/src/com/vaadin/client/ui/grid/renderers/NumberRendererConnector.java b/client/src/com/vaadin/client/ui/grid/renderers/NumberRendererConnector.java
new file mode 100644
index 0000000000..cba29d0690
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/renderers/NumberRendererConnector.java
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid.renderers;
+
+import com.vaadin.shared.ui.Connect;
+
+/**
+ * A connector for
+ * {@link com.vaadin.ui.components.grid.renderers.NumberRenderer NumberRenderer}
+ * .
+ * <p>
+ * The server-side Renderer operates on numbers, but the data is serialized as a
+ * string, and displayed as-is on the client side. This is to be able to support
+ * the server's locale.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+@Connect(com.vaadin.ui.components.grid.renderers.NumberRenderer.class)
+public class NumberRendererConnector extends TextRendererConnector {
+ // no implementation needed
+}
diff --git a/client/src/com/vaadin/client/ui/grid/renderers/TextRenderer.java b/client/src/com/vaadin/client/ui/grid/renderers/TextRenderer.java
new file mode 100644
index 0000000000..d2f3520c43
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/renderers/TextRenderer.java
@@ -0,0 +1,33 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid.renderers;
+
+import com.vaadin.client.ui.grid.FlyweightCell;
+import com.vaadin.client.ui.grid.Renderer;
+
+/**
+ * Renderer that renders text into a cell.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public class TextRenderer implements Renderer<String> {
+
+ @Override
+ public void render(FlyweightCell cell, String text) {
+ cell.getElement().setInnerText(text);
+ }
+}
diff --git a/client/src/com/vaadin/client/ui/grid/renderers/TextRendererConnector.java b/client/src/com/vaadin/client/ui/grid/renderers/TextRendererConnector.java
new file mode 100644
index 0000000000..9ec609ae06
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/renderers/TextRendererConnector.java
@@ -0,0 +1,33 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid.renderers;
+
+import com.vaadin.shared.ui.Connect;
+
+/**
+ * A connector for {@link TextRenderer}.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+@Connect(com.vaadin.ui.components.grid.renderers.TextRenderer.class)
+public class TextRendererConnector extends AbstractRendererConnector<String> {
+
+ @Override
+ public TextRenderer getRenderer() {
+ return (TextRenderer) super.getRenderer();
+ }
+}
diff --git a/client/src/com/vaadin/client/ui/grid/renderers/UnsafeHtmlRendererConnector.java b/client/src/com/vaadin/client/ui/grid/renderers/UnsafeHtmlRendererConnector.java
new file mode 100644
index 0000000000..1d4a8c0384
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/renderers/UnsafeHtmlRendererConnector.java
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid.renderers;
+
+import com.vaadin.client.ui.grid.FlyweightCell;
+import com.vaadin.client.ui.grid.Renderer;
+import com.vaadin.shared.ui.Connect;
+
+/**
+ * A connector for {@link UnsafeHtmlRenderer}
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+@Connect(com.vaadin.ui.components.grid.renderers.HtmlRenderer.class)
+public class UnsafeHtmlRendererConnector extends
+ AbstractRendererConnector<String> {
+
+ public static class UnsafeHtmlRenderer implements Renderer<String> {
+ @Override
+ public void render(FlyweightCell cell, String data) {
+ cell.getElement().setInnerHTML(data);
+ }
+ }
+
+ @Override
+ public UnsafeHtmlRenderer getRenderer() {
+ return (UnsafeHtmlRenderer) super.getRenderer();
+ }
+}
diff --git a/client/src/com/vaadin/client/ui/grid/renderers/WidgetRenderer.java b/client/src/com/vaadin/client/ui/grid/renderers/WidgetRenderer.java
new file mode 100644
index 0000000000..b7cd72600a
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/renderers/WidgetRenderer.java
@@ -0,0 +1,67 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid.renderers;
+
+import com.google.gwt.user.client.ui.Widget;
+import com.vaadin.client.Util;
+import com.vaadin.client.ui.grid.FlyweightCell;
+
+/**
+ * A renderer for rendering widgets into cells.
+ *
+ * @since
+ * @author Vaadin Ltd
+ * @param <T>
+ * the row data type
+ * @param <W>
+ * the Widget type
+ */
+public abstract class WidgetRenderer<T, W extends Widget> extends
+ ComplexRenderer<T> {
+
+ /**
+ * Creates a widget to attach to a cell. The widgets will be attached to the
+ * cell after the cell element has been attached to DOM.
+ *
+ * @return widget to attach to a cell. All returned instances should be new
+ * widget instances without a parent.
+ */
+ public abstract W createWidget();
+
+ @Override
+ public void render(FlyweightCell cell, T data) {
+ W w = Util.findWidget(cell.getElement().getFirstChildElement(), null);
+ assert w != null : "Widget not found in cell (" + cell.getColumn()
+ + "," + cell.getRow() + ")";
+ render(cell, data, w);
+ }
+
+ /**
+ * Renders a cell with a widget. This provides a way to update any
+ * information in the widget that is cell specific. Do not detach the Widget
+ * here, it will be done automatically by the Grid when the widget is no
+ * longer needed.
+ *
+ * @param cell
+ * the cell to render
+ * @param data
+ * the data of the cell
+ * @param widget
+ * the widget embedded in the cell
+ */
+ public abstract void render(FlyweightCell cell, T data, W widget);
+
+}
diff --git a/client/src/com/vaadin/client/ui/grid/selection/AbstractRowHandleSelectionModel.java b/client/src/com/vaadin/client/ui/grid/selection/AbstractRowHandleSelectionModel.java
new file mode 100644
index 0000000000..f55229d86c
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/selection/AbstractRowHandleSelectionModel.java
@@ -0,0 +1,65 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid.selection;
+
+import com.vaadin.client.data.DataSource.RowHandle;
+
+/**
+ * An abstract class that adds a consistent API for common methods that's needed
+ * by Vaadin's server-based selection models to work.
+ * <p>
+ * <em>Note:</em> This should be an interface instead of an abstract class, if
+ * only we could define protected methods in an interface.
+ *
+ * @author Vaadin Ltd
+ * @param <T>
+ * The grid's row type
+ */
+public abstract class AbstractRowHandleSelectionModel<T> implements
+ SelectionModel<T> {
+ /**
+ * Select a row, based on its
+ * {@link com.vaadin.client.data.DataSource.RowHandle RowHandle}.
+ * <p>
+ * <em>Note:</em> this method may not fire selection change events.
+ *
+ * @param handle
+ * the handle to select by
+ * @return <code>true</code> iff the selection state was changed by this
+ * call
+ * @throws UnsupportedOperationException
+ * if the selection model does not support either handles or
+ * selection
+ */
+ protected abstract boolean selectByHandle(RowHandle<T> handle);
+
+ /**
+ * Deselect a row, based on its
+ * {@link com.vaadin.client.data.DataSource.RowHandle RowHandle}.
+ * <p>
+ * <em>Note:</em> this method may not fire selection change events.
+ *
+ * @param handle
+ * the handle to deselect by
+ * @return <code>true</code> iff the selection state was changed by this
+ * call
+ * @throws UnsupportedOperationException
+ * if the selection model does not support either handles or
+ * deselection
+ */
+ protected abstract boolean deselectByHandle(RowHandle<T> handle)
+ throws UnsupportedOperationException;
+}
diff --git a/client/src/com/vaadin/client/ui/grid/selection/HasSelectionChangeHandlers.java b/client/src/com/vaadin/client/ui/grid/selection/HasSelectionChangeHandlers.java
new file mode 100644
index 0000000000..342c426b55
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/selection/HasSelectionChangeHandlers.java
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid.selection;
+
+import com.google.gwt.event.shared.HandlerRegistration;
+
+/**
+ * Marker interface for widgets that fires selection change events.
+ *
+ * @author Vaadin Ltd
+ * @since
+ */
+public interface HasSelectionChangeHandlers<T> {
+
+ /**
+ * Register a selection change handler.
+ * <p>
+ * This handler is called whenever a
+ * {@link com.vaadin.ui.components.grid.selection.SelectionModel
+ * SelectionModel} detects a change in selection state.
+ *
+ * @param handler
+ * a {@link SelectionChangeHandler}
+ * @return a handler registration object, which can be used to remove the
+ * handler.
+ */
+ public HandlerRegistration addSelectionChangeHandler(
+ SelectionChangeHandler<T> handler);
+
+}
diff --git a/client/src/com/vaadin/client/ui/grid/selection/MultiSelectionRenderer.java b/client/src/com/vaadin/client/ui/grid/selection/MultiSelectionRenderer.java
new file mode 100644
index 0000000000..a3030e3a1f
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/selection/MultiSelectionRenderer.java
@@ -0,0 +1,764 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid.selection;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+
+import com.google.gwt.animation.client.AnimationScheduler;
+import com.google.gwt.animation.client.AnimationScheduler.AnimationCallback;
+import com.google.gwt.animation.client.AnimationScheduler.AnimationHandle;
+import com.google.gwt.dom.client.BrowserEvents;
+import com.google.gwt.dom.client.Element;
+import com.google.gwt.dom.client.InputElement;
+import com.google.gwt.dom.client.NativeEvent;
+import com.google.gwt.dom.client.TableElement;
+import com.google.gwt.event.dom.client.KeyCodes;
+import com.google.gwt.event.shared.HandlerRegistration;
+import com.google.gwt.user.client.DOM;
+import com.google.gwt.user.client.Event;
+import com.google.gwt.user.client.Event.NativePreviewEvent;
+import com.google.gwt.user.client.Event.NativePreviewHandler;
+import com.vaadin.client.Util;
+import com.vaadin.client.data.AbstractRemoteDataSource;
+import com.vaadin.client.data.DataSource;
+import com.vaadin.client.ui.grid.Cell;
+import com.vaadin.client.ui.grid.DataAvailableEvent;
+import com.vaadin.client.ui.grid.DataAvailableHandler;
+import com.vaadin.client.ui.grid.FlyweightCell;
+import com.vaadin.client.ui.grid.Grid;
+import com.vaadin.client.ui.grid.keyevents.BodyKeyDownHandler;
+import com.vaadin.client.ui.grid.keyevents.BodyKeyUpHandler;
+import com.vaadin.client.ui.grid.keyevents.GridKeyDownEvent;
+import com.vaadin.client.ui.grid.keyevents.GridKeyUpEvent;
+import com.vaadin.client.ui.grid.renderers.ComplexRenderer;
+import com.vaadin.client.ui.grid.selection.SelectionModel.Multi.Batched;
+import com.vaadin.shared.ui.grid.ScrollDestination;
+
+/* This class will probably not survive the final merge of all selection functionality. */
+public class MultiSelectionRenderer<T> extends ComplexRenderer<Boolean> {
+
+ /** The size of the autoscroll area, both top and bottom. */
+ private static final int SCROLL_AREA_GRADIENT_PX = 100;
+
+ /** The maximum number of pixels per second to autoscroll. */
+ private static final int SCROLL_TOP_SPEED_PX_SEC = 500;
+
+ /**
+ * The minimum area where the grid doesn't scroll while the pointer is
+ * pressed.
+ */
+ private static final int MIN_NO_AUTOSCROLL_AREA_PX = 50;
+
+ /**
+ * This class's main objective is to listen when to stop autoscrolling, and
+ * make sure everything stops accordingly.
+ */
+ private class TouchEventHandler implements NativePreviewHandler {
+ @Override
+ public void onPreviewNativeEvent(final NativePreviewEvent event) {
+ switch (event.getTypeInt()) {
+ case Event.ONTOUCHSTART: {
+ if (event.getNativeEvent().getTouches().length() == 1) {
+ /*
+ * Something has dropped a touchend/touchcancel and the
+ * scroller is most probably running amok. Let's cancel it
+ * and pretend that everything's going as expected
+ *
+ * Because this is a preview, this code is run before the
+ * event handler in MultiSelectionRenderer.onBrowserEvent.
+ * Therefore, we can simply kill everything and let that
+ * method restart things as they should.
+ */
+ autoScrollHandler.stop();
+
+ /*
+ * Related TODO: investigate why iOS seems to ignore a
+ * touchend/touchcancel when frames are dropped, and/or if
+ * something can be done about that.
+ */
+ }
+ break;
+ }
+
+ case Event.ONTOUCHMOVE:
+ event.cancel();
+ break;
+
+ case Event.ONTOUCHEND:
+ case Event.ONTOUCHCANCEL:
+ /*
+ * Remember: targetElement is always where touchstart started,
+ * not where the finger is pointing currently.
+ */
+ final Element targetElement = Element.as(event.getNativeEvent()
+ .getEventTarget());
+ if (isInFirstColumn(targetElement)) {
+ removeNativeHandler();
+ event.cancel();
+ }
+ break;
+ }
+ }
+
+ private boolean isInFirstColumn(final Element element) {
+ if (element == null) {
+ return false;
+ }
+ final Element tbody = getTbodyElement();
+
+ if (tbody == null || !tbody.isOrHasChild(element)) {
+ return false;
+ }
+
+ /*
+ * The null-parent in the while clause is in the case where element
+ * is an immediate tr child in the tbody. Should never happen in
+ * internal code, but hey...
+ */
+ Element cursor = element;
+ while (cursor.getParentElement() != null
+ && cursor.getParentElement().getParentElement() != tbody) {
+ cursor = cursor.getParentElement();
+ }
+
+ final Element tr = cursor.getParentElement();
+ return tr.getFirstChildElement().equals(cursor);
+ }
+ }
+
+ /**
+ * This class's responsibility is to
+ * <ul>
+ * <li>scroll the table while a pointer is kept in a scrolling zone and
+ * <li>select rows whenever a pointer is "activated" on a selection cell
+ * </ul>
+ * <p>
+ * <em>Techical note:</em> This class is an AnimationCallback because we
+ * need a timer: when the finger is kept in place while the grid scrolls, we
+ * still need to be able to make new selections. So, instead of relying on
+ * events (which won't be fired, since the pointer isn't necessarily
+ * moving), we do this check on each frame while the pointer is "active"
+ * (mouse is pressed, finger is on screen).
+ */
+ private class AutoScrollerAndSelector implements AnimationCallback {
+
+ /**
+ * If the acceleration gradient area is smaller than this, autoscrolling
+ * will be disabled (it becomes too quick to accelerate to be usable).
+ */
+ private static final int GRADIENT_MIN_THRESHOLD_PX = 10;
+
+ /**
+ * The speed at which the gradient area recovers, once scrolling in that
+ * direction has started.
+ */
+ private static final int SCROLL_AREA_REBOUND_PX_PER_SEC = 1;
+ private static final double SCROLL_AREA_REBOUND_PX_PER_MS = SCROLL_AREA_REBOUND_PX_PER_SEC / 1000.0d;
+
+ /**
+ * The lowest y-coordinate on the {@link Event#getClientY() client} from
+ * where we need to start scrolling towards the top.
+ */
+ private int topBound = -1;
+
+ /**
+ * The highest y-coordinate on the {@link Event#getClientY() client}
+ * from where we need to scrolling towards the bottom.
+ */
+ private int bottomBound = -1;
+
+ /**
+ * <code>true</code> if the pointer is selecting, <code>false</code> if
+ * the pointer is deselecting.
+ */
+ private final boolean selectionPaint;
+
+ /**
+ * The area where the selection acceleration takes place. If &lt;
+ * {@link #GRADIENT_MIN_THRESHOLD_PX}, autoscrolling is disabled
+ */
+ private final int gradientArea;
+
+ /**
+ * The number of pixels per seconds we currently are scrolling (negative
+ * is towards the top, positive is towards the bottom).
+ */
+ private double scrollSpeed = 0;
+
+ private double prevTimestamp = 0;
+
+ /**
+ * This field stores fractions of pixels to scroll, to make sure that
+ * we're able to scroll less than one px per frame.
+ */
+ private double pixelsToScroll = 0.0d;
+
+ /** Should this animator be running. */
+ private boolean running = false;
+
+ /** The handle in which this instance is running. */
+ private AnimationHandle handle;
+
+ /** The pointer's pageX coordinate. */
+ private int pageX;
+
+ /** The pointer's pageY coordinate. */
+ private int pageY;
+
+ /** The logical index of the row that was most recently modified. */
+ private int logicalRow = -1;
+
+ /** @see #doScrollAreaChecks(int) */
+ private int finalTopBound;
+
+ /** @see #doScrollAreaChecks(int) */
+ private int finalBottomBound;
+
+ private boolean scrollAreaShouldRebound = false;
+
+ private AbstractRemoteDataSource<T> remoteDataSource = null;
+ private Batched<T> batchedSelectionModel = null;
+
+ public AutoScrollerAndSelector(final int topBound,
+ final int bottomBound, final int gradientArea,
+ final boolean selectionPaint) {
+ finalTopBound = topBound;
+ finalBottomBound = bottomBound;
+ this.gradientArea = gradientArea;
+ this.selectionPaint = selectionPaint;
+ }
+
+ @Override
+ public void execute(final double timestamp) {
+ final double timeDiff = timestamp - prevTimestamp;
+ prevTimestamp = timestamp;
+
+ reboundScrollArea(timeDiff);
+
+ pixelsToScroll += scrollSpeed * (timeDiff / 1000.0d);
+ final int intPixelsToScroll = (int) pixelsToScroll;
+ pixelsToScroll -= intPixelsToScroll;
+
+ if (intPixelsToScroll != 0) {
+ grid.setScrollTop(grid.getScrollTop() + intPixelsToScroll);
+ }
+
+ @SuppressWarnings("hiding")
+ int logicalRow = getLogicalRowIndex(Util.getElementFromPoint(pageX,
+ pageY));
+ if (logicalRow != -1 && logicalRow != this.logicalRow) {
+ this.logicalRow = logicalRow;
+ setSelected(logicalRow, selectionPaint);
+
+ if (remoteDataSource != null && batchedSelectionModel != null) {
+ Collection<T> pinneds = batchedSelectionModel
+ .getSelectedRowsBatch();
+ pinneds.addAll(batchedSelectionModel
+ .getDeselectedRowsBatch());
+ remoteDataSource.transactionPin(pinneds);
+ }
+ }
+
+ reschedule();
+ }
+
+ /**
+ * If the scroll are has been offset by the pointer starting out there,
+ * move it back a bit
+ */
+ private void reboundScrollArea(double timeDiff) {
+ if (!scrollAreaShouldRebound) {
+ return;
+ }
+
+ int reboundPx = (int) Math.ceil(SCROLL_AREA_REBOUND_PX_PER_MS
+ * timeDiff);
+ if (topBound < finalTopBound) {
+ topBound += reboundPx;
+ topBound = Math.min(topBound, finalTopBound);
+ updateScrollSpeed(pageY);
+ } else if (bottomBound > finalBottomBound) {
+ bottomBound -= reboundPx;
+ bottomBound = Math.max(bottomBound, finalBottomBound);
+ updateScrollSpeed(pageY);
+ }
+ }
+
+ private void updateScrollSpeed(final int pointerPageY) {
+
+ final double ratio;
+ if (pointerPageY < topBound) {
+ final double distance = pointerPageY - topBound;
+ ratio = Math.max(-1, distance / gradientArea);
+ }
+
+ else if (pointerPageY > bottomBound) {
+ final double distance = pointerPageY - bottomBound;
+ ratio = Math.min(1, distance / gradientArea);
+ }
+
+ else {
+ ratio = 0;
+ }
+
+ scrollSpeed = ratio * SCROLL_TOP_SPEED_PX_SEC;
+ }
+
+ @SuppressWarnings("deprecation")
+ public void start(int logicalRowIndex) {
+ running = true;
+ setSelected(logicalRowIndex, selectionPaint);
+ logicalRow = logicalRowIndex;
+ reschedule();
+
+ DataSource<T> dataSource = grid.getDataSource();
+ SelectionModel<T> selectionModel = grid.getSelectionModel();
+ if (dataSource instanceof AbstractRemoteDataSource
+ && selectionModel instanceof Batched) {
+ this.remoteDataSource = (AbstractRemoteDataSource<T>) dataSource;
+ this.batchedSelectionModel = (Batched<T>) selectionModel;
+
+ Collection<T> pinneds = batchedSelectionModel
+ .getSelectedRowsBatch();
+ pinneds.addAll(batchedSelectionModel.getDeselectedRowsBatch());
+ remoteDataSource.transactionPin(pinneds);
+ }
+ }
+
+ @SuppressWarnings("deprecation")
+ public void stop() {
+ running = false;
+
+ // split into two lines because of Java generics not playing nice.
+ @SuppressWarnings("unchecked")
+ Collection<T> emptySet = (Collection<T>) Collections.emptySet();
+ remoteDataSource.transactionPin(emptySet);
+ remoteDataSource = null;
+ batchedSelectionModel = null;
+
+ if (handle != null) {
+ handle.cancel();
+ handle = null;
+ }
+ }
+
+ private void reschedule() {
+ if (running && gradientArea >= GRADIENT_MIN_THRESHOLD_PX) {
+ handle = AnimationScheduler.get().requestAnimationFrame(this,
+ grid.getElement());
+ }
+ }
+
+ @SuppressWarnings("hiding")
+ public void updatePointerCoords(int pageX, int pageY) {
+ doScrollAreaChecks(pageY);
+ updateScrollSpeed(pageY);
+ this.pageX = pageX;
+ this.pageY = pageY;
+ }
+
+ /**
+ * This method checks whether the first pointer event started in an area
+ * that would start scrolling immediately, and does some actions
+ * accordingly.
+ * <p>
+ * If it is, that scroll area will be offset "beyond" the pointer (above
+ * if pointer is towards the top, otherwise below).
+ * <p>
+ * <span style="font-size:smaller">*) This behavior will change in
+ * future patches (henrik paul 2.7.2014)</span>
+ */
+ private void doScrollAreaChecks(int pageY) {
+ /*
+ * The first run makes sure that neither scroll position is
+ * underneath the finger, but offset to either direction from
+ * underneath the pointer.
+ */
+ if (topBound == -1) {
+ topBound = Math.min(finalTopBound, pageY);
+ bottomBound = Math.max(finalBottomBound, pageY);
+ }
+
+ /*
+ * Subsequent runs make sure that the scroll area grows (but doesn't
+ * shrink) with the finger, but no further than the final bound.
+ */
+ else {
+ int oldTopBound = topBound;
+ if (topBound < finalTopBound) {
+ topBound = Math.max(topBound,
+ Math.min(finalTopBound, pageY));
+ }
+
+ int oldBottomBound = bottomBound;
+ if (bottomBound > finalBottomBound) {
+ bottomBound = Math.min(bottomBound,
+ Math.max(finalBottomBound, pageY));
+ }
+
+ final boolean topDidNotMove = oldTopBound == topBound;
+ final boolean bottomDidNotMove = oldBottomBound == bottomBound;
+ final boolean wasVerticalMovement = pageY != this.pageY;
+ scrollAreaShouldRebound = (topDidNotMove && bottomDidNotMove && wasVerticalMovement);
+ }
+ }
+ }
+
+ /**
+ * This class makes sure that pointer movemenets are registered and
+ * delegated to the autoscroller so that it can:
+ * <ul>
+ * <li>modify the speed in which we autoscroll.
+ * <li>"paint" a new row with the selection.
+ * </ul>
+ * Essentially, when a pointer is pressed on the selection column, a native
+ * preview handler is registered (so that selection gestures can happen
+ * outside of the selection column). The handler itself makes sure that it's
+ * detached when the pointer is "lifted".
+ */
+ private class AutoScrollHandler {
+ private AutoScrollerAndSelector autoScroller;
+
+ /** The registration info for {@link #scrollPreviewHandler} */
+ private HandlerRegistration handlerRegistration;
+
+ private final NativePreviewHandler scrollPreviewHandler = new NativePreviewHandler() {
+ @Override
+ public void onPreviewNativeEvent(final NativePreviewEvent event) {
+ if (autoScroller == null) {
+ stop();
+ return;
+ }
+
+ final NativeEvent nativeEvent = event.getNativeEvent();
+ int pageY = 0;
+ int pageX = 0;
+ switch (event.getTypeInt()) {
+ case Event.ONMOUSEMOVE:
+ case Event.ONTOUCHMOVE:
+ pageY = Util.getTouchOrMouseClientY(nativeEvent);
+ pageX = Util.getTouchOrMouseClientX(nativeEvent);
+ autoScroller.updatePointerCoords(pageX, pageY);
+ break;
+ case Event.ONMOUSEUP:
+ case Event.ONTOUCHEND:
+ case Event.ONTOUCHCANCEL:
+ stop();
+ break;
+ }
+ }
+ };
+
+ /**
+ * The top bound, as calculated from the {@link Event#getClientY()
+ * client} coordinates.
+ */
+ private int topBound = -1;
+
+ /**
+ * The bottom bound, as calculated from the {@link Event#getClientY()
+ * client} coordinates.
+ */
+ private int bottomBound = -1;
+
+ /** The size of the autoscroll acceleration area. */
+ private int gradientArea;
+
+ public void start(int logicalRowIndex) {
+
+ SelectionModel<T> model = grid.getSelectionModel();
+ if (model instanceof Batched) {
+ Batched<?> batchedModel = (Batched<?>) model;
+ batchedModel.startBatchSelect();
+ }
+
+ /*
+ * bounds are updated whenever the autoscroll cycle starts, to make
+ * sure that the widget hasn't changed in size, moved around, or
+ * whatnot.
+ */
+ updateScrollBounds();
+
+ assert handlerRegistration == null : "handlerRegistration was not null";
+ assert autoScroller == null : "autoScroller was not null";
+ handlerRegistration = Event
+ .addNativePreviewHandler(scrollPreviewHandler);
+
+ autoScroller = new AutoScrollerAndSelector(topBound, bottomBound,
+ gradientArea, !isSelected(logicalRowIndex));
+ autoScroller.start(logicalRowIndex);
+ }
+
+ private void updateScrollBounds() {
+ final Element root = Element.as(grid.getElement());
+ final Element tableWrapper = Element.as(root.getChild(2));
+ final TableElement table = TableElement.as(tableWrapper
+ .getFirstChildElement());
+ final Element thead = table.getTHead();
+ final Element tfoot = table.getTFoot();
+
+ /*
+ * GWT _does_ have an "Element.getAbsoluteTop()" that takes both the
+ * client top and scroll compensation into account, but they're
+ * calculated wrong for our purposes, so this does something
+ * similar, but only suitable for us.
+ *
+ * Also, this should be a bit faster, since the scroll compensation
+ * is calculated only once and used in two places.
+ */
+
+ final int topBorder = getClientTop(root) + thead.getOffsetHeight();
+ final int bottomBorder = getClientTop(tfoot);
+
+ final int scrollCompensation = getScrollCompensation();
+ topBound = scrollCompensation + topBorder + SCROLL_AREA_GRADIENT_PX;
+ bottomBound = scrollCompensation + bottomBorder
+ - SCROLL_AREA_GRADIENT_PX;
+ gradientArea = SCROLL_AREA_GRADIENT_PX;
+
+ // modify bounds if they're too tightly packed
+ if (bottomBound - topBound < MIN_NO_AUTOSCROLL_AREA_PX) {
+ int adjustment = MIN_NO_AUTOSCROLL_AREA_PX
+ - (bottomBound - topBound);
+ topBound -= adjustment / 2;
+ bottomBound += adjustment / 2;
+ gradientArea -= adjustment / 2;
+ }
+ }
+
+ /** Get the "top" of an element in relation to "client" coordinates. */
+ private int getClientTop(final Element e) {
+ Element cursor = e;
+ int top = 0;
+ while (cursor != null) {
+ top += cursor.getOffsetTop();
+ cursor = cursor.getOffsetParent();
+ }
+ return top;
+ }
+
+ private int getScrollCompensation() {
+ Element cursor = grid.getElement();
+ int scroll = 0;
+ while (cursor != null) {
+ scroll -= cursor.getScrollTop();
+ cursor = cursor.getParentElement();
+ }
+
+ return scroll;
+ }
+
+ public void stop() {
+ if (handlerRegistration != null) {
+ handlerRegistration.removeHandler();
+ handlerRegistration = null;
+ }
+
+ if (autoScroller != null) {
+ autoScroller.stop();
+ autoScroller = null;
+ }
+
+ SelectionModel<T> model = grid.getSelectionModel();
+ if (model instanceof Batched) {
+ Batched<?> batchedModel = (Batched<?>) model;
+ batchedModel.commitBatchSelect();
+ }
+
+ removeNativeHandler();
+ }
+ }
+
+ private class SpaceKeyDownSelectHandler implements BodyKeyDownHandler<T> {
+
+ private HandlerRegistration scrollHandler = null;
+ private boolean spaceDown = false;
+
+ @Override
+ public void onKeyDown(GridKeyDownEvent<T> event) {
+ if (event.getNativeKeyCode() != KeyCodes.KEY_SPACE || spaceDown) {
+ return;
+ }
+
+ spaceDown = true;
+ Cell active = event.getActiveCell();
+ final int rowIndex = active.getRow();
+
+ if (scrollHandler != null) {
+ scrollHandler.removeHandler();
+ scrollHandler = null;
+ }
+
+ scrollHandler = grid
+ .addDataAvailableHandler(new DataAvailableHandler() {
+
+ @Override
+ public void onDataAvailable(DataAvailableEvent event) {
+ if (event.getAvailableRows().contains(rowIndex)) {
+ setSelected(rowIndex, !isSelected(rowIndex));
+ scrollHandler.removeHandler();
+ scrollHandler = null;
+ }
+ }
+ });
+ grid.scrollToRow(rowIndex, ScrollDestination.ANY);
+ }
+
+ }
+
+ private static final String LOGICAL_ROW_PROPERTY_INT = "vEscalatorLogicalRow";
+
+ private final Grid<T> grid;
+ private HandlerRegistration nativePreviewHandlerRegistration;
+ private final SpaceKeyDownSelectHandler handler = new SpaceKeyDownSelectHandler();
+ private HandlerRegistration spaceDown;
+ private HandlerRegistration spaceUp;
+
+ private final AutoScrollHandler autoScrollHandler = new AutoScrollHandler();
+
+ public MultiSelectionRenderer(final Grid<T> grid) {
+ this.grid = grid;
+ spaceDown = grid.addKeyDownHandler(handler);
+ spaceUp = grid.addKeyUpHandler(new BodyKeyUpHandler<T>() {
+
+ @Override
+ public void onKeyUp(GridKeyUpEvent<T> event) {
+ if (event.getNativeKeyCode() == KeyCodes.KEY_SPACE) {
+ handler.spaceDown = false;
+ }
+ }
+ });
+ }
+
+ @Override
+ public void destroy() {
+ spaceDown.removeHandler();
+ spaceUp.removeHandler();
+ if (nativePreviewHandlerRegistration != null) {
+ removeNativeHandler();
+ }
+ }
+
+ @Override
+ public void render(final FlyweightCell cell, final Boolean data) {
+ /*
+ * FIXME: Once https://dev.vaadin.com/review/#/c/3670/ is merged
+ * (init/destroy), split this method. Also, remove all event preview
+ * handlers on detach, to avoid hanging events.
+ */
+
+ final InputElement checkbox = InputElement.as(DOM.createInputCheck());
+ checkbox.setChecked(data.booleanValue());
+ checkbox.setPropertyInt(LOGICAL_ROW_PROPERTY_INT, cell.getRow());
+ cell.getElement().removeAllChildren();
+ cell.getElement().appendChild(checkbox);
+ }
+
+ @Override
+ public Collection<String> getConsumedEvents() {
+ final HashSet<String> events = new HashSet<String>();
+
+ /*
+ * this column's first interest is only to attach a NativePreventHandler
+ * that does all the magic. These events are the beginning of that
+ * cycle.
+ */
+ events.add(BrowserEvents.MOUSEDOWN);
+ events.add(BrowserEvents.TOUCHSTART);
+
+ return events;
+ }
+
+ @Override
+ public boolean onBrowserEvent(final Cell cell, final NativeEvent event) {
+ if (BrowserEvents.TOUCHSTART.equals(event.getType())
+ || BrowserEvents.MOUSEDOWN.equals(event.getType())) {
+ injectNativeHandler();
+ int logicalRowIndex = getLogicalRowIndex(Element.as(event
+ .getEventTarget()));
+ autoScrollHandler.start(logicalRowIndex);
+ event.preventDefault();
+ event.stopPropagation();
+ return true;
+ } else {
+ throw new IllegalStateException("received unexpected event: "
+ + event.getType());
+ }
+ }
+
+ private void injectNativeHandler() {
+ removeNativeHandler();
+ nativePreviewHandlerRegistration = Event
+ .addNativePreviewHandler(new TouchEventHandler());
+ }
+
+ private void removeNativeHandler() {
+ if (nativePreviewHandlerRegistration != null) {
+ nativePreviewHandlerRegistration.removeHandler();
+ nativePreviewHandlerRegistration = null;
+ }
+ }
+
+ private int getLogicalRowIndex(final Element target) {
+ /*
+ * We can't simply go backwards until we find a <tr> first element,
+ * because of the table-in-table scenario. We need to, unfortunately, go
+ * up from our known root.
+ */
+ final Element tbody = getTbodyElement();
+ Element tr = tbody.getFirstChildElement();
+ while (tr != null) {
+ if (tr.isOrHasChild(target)) {
+ final Element td = tr.getFirstChildElement();
+ assert td != null : "Cell has disappeared";
+
+ final Element checkbox = td.getFirstChildElement();
+ assert checkbox != null : "Checkbox has disappeared";
+
+ return checkbox.getPropertyInt(LOGICAL_ROW_PROPERTY_INT);
+ }
+ tr = tr.getNextSiblingElement();
+ }
+ return -1;
+ }
+
+ private Element getTbodyElement() {
+ final Element root = grid.getElement();
+ final Element tablewrapper = Element.as(root.getChild(2));
+ if (tablewrapper != null) {
+ final TableElement table = TableElement.as(tablewrapper
+ .getFirstChildElement());
+ return table.getTBodies().getItem(0);
+ } else {
+ return null;
+ }
+ }
+
+ protected boolean isSelected(final int logicalRow) {
+ return grid.isSelected(grid.getDataSource().getRow(logicalRow));
+ }
+
+ protected void setSelected(final int logicalRow, final boolean select) {
+ T row = grid.getDataSource().getRow(logicalRow);
+ if (select) {
+ grid.select(row);
+ } else {
+ grid.deselect(row);
+ }
+ }
+}
diff --git a/client/src/com/vaadin/client/ui/grid/selection/SelectionChangeEvent.java b/client/src/com/vaadin/client/ui/grid/selection/SelectionChangeEvent.java
new file mode 100644
index 0000000000..4bebaf0fbb
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/selection/SelectionChangeEvent.java
@@ -0,0 +1,170 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid.selection;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+import com.google.gwt.event.shared.GwtEvent;
+import com.vaadin.client.ui.grid.Grid;
+
+/**
+ * Event object describing a change in Grid row selection state.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+@SuppressWarnings("rawtypes")
+public class SelectionChangeEvent<T> extends GwtEvent<SelectionChangeHandler> {
+
+ private static final Type<SelectionChangeHandler> eventType = new Type<SelectionChangeHandler>();
+
+ private final Grid<T> grid;
+ private final List<T> added;
+ private final List<T> removed;
+ private final boolean batched;
+
+ /**
+ * Creates an event with a single added or removed row.
+ *
+ * @param grid
+ * grid reference, used for getSource
+ * @param added
+ * the added row, or <code>null</code> if a row was not added
+ * @param removed
+ * the removed row, or <code>null</code> if a row was not removed
+ * @param batched
+ * whether or not this selection change event is triggered during
+ * a batched selection/deselection action
+ * @see SelectionModel.Multi.Batched
+ */
+ public SelectionChangeEvent(Grid<T> grid, T added, T removed,
+ boolean batched) {
+ this.grid = grid;
+ this.batched = batched;
+
+ if (added != null) {
+ this.added = Collections.singletonList(added);
+ } else {
+ this.added = Collections.emptyList();
+ }
+
+ if (removed != null) {
+ this.removed = Collections.singletonList(removed);
+ } else {
+ this.removed = Collections.emptyList();
+ }
+ }
+
+ /**
+ * Creates an event where several rows have been added or removed.
+ *
+ * @param grid
+ * Grid reference, used for getSource
+ * @param added
+ * a collection of added rows, or <code>null</code> if no rows
+ * were added
+ * @param removed
+ * a collection of removed rows, or <code>null</code> if no rows
+ * were removed
+ * @param batched
+ * whether or not this selection change event is triggered during
+ * a batched selection/deselection action
+ * @see SelectionModel.Multi.Batched
+ */
+ public SelectionChangeEvent(Grid<T> grid, Collection<T> added,
+ Collection<T> removed, boolean batched) {
+ this.grid = grid;
+ this.batched = batched;
+
+ if (added != null) {
+ this.added = new ArrayList<T>(added);
+ } else {
+ this.added = Collections.emptyList();
+ }
+
+ if (removed != null) {
+ this.removed = new ArrayList<T>(removed);
+ } else {
+ this.removed = Collections.emptyList();
+ }
+ }
+
+ /**
+ * Get a reference to the Grid object that fired this event.
+ *
+ * @return a grid reference
+ */
+ @Override
+ public Grid<T> getSource() {
+ return grid;
+ }
+
+ /**
+ * Get all rows added to the selection since the last
+ * {@link SelectionChangeEvent}.
+ *
+ * @return a collection of added rows. Empty collection if no rows were
+ * added.
+ */
+ public Collection<T> getAdded() {
+ return Collections.unmodifiableCollection(added);
+ }
+
+ /**
+ * Get all rows removed from the selection since the last
+ * {@link SelectionChangeEvent}.
+ *
+ * @return a collection of removed rows. Empty collection if no rows were
+ * removed.
+ */
+ public Collection<T> getRemoved() {
+ return Collections.unmodifiableCollection(removed);
+ }
+
+ /**
+ * Gets a type identifier for this event.
+ *
+ * @return a {@link Type} identifier.
+ */
+ public static Type<SelectionChangeHandler> getType() {
+ return eventType;
+ }
+
+ @Override
+ public Type<SelectionChangeHandler> getAssociatedType() {
+ return eventType;
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ protected void dispatch(SelectionChangeHandler handler) {
+ handler.onSelectionChange(this);
+ }
+
+ /**
+ * Checks if this selection change event is fired during a batched
+ * selection/deselection operation.
+ *
+ * @return <code>true</code> iff this event is fired during a batched
+ * selection/deselection operation
+ */
+ public boolean isBatchedSelection() {
+ return batched;
+ }
+}
diff --git a/client/src/com/vaadin/client/ui/grid/selection/SelectionChangeHandler.java b/client/src/com/vaadin/client/ui/grid/selection/SelectionChangeHandler.java
new file mode 100644
index 0000000000..a469f5af1f
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/selection/SelectionChangeHandler.java
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid.selection;
+
+import com.google.gwt.event.shared.EventHandler;
+
+/**
+ * Handler for {@link SelectionChangeEvent}s.
+ *
+ * @since
+ * @author Vaadin Ltd
+ * @param <T>
+ * The row data type
+ */
+public interface SelectionChangeHandler<T> extends EventHandler {
+
+ /**
+ * Called when a selection model's selection state is changed.
+ *
+ * @param event
+ * a selection change event, containing info about rows that have
+ * been added to or removed from the selection.
+ */
+ public void onSelectionChange(SelectionChangeEvent<T> event);
+
+}
diff --git a/client/src/com/vaadin/client/ui/grid/selection/SelectionModel.java b/client/src/com/vaadin/client/ui/grid/selection/SelectionModel.java
new file mode 100644
index 0000000000..55336ce3da
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/selection/SelectionModel.java
@@ -0,0 +1,237 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid.selection;
+
+import java.util.Collection;
+
+import com.vaadin.client.ui.grid.Grid;
+import com.vaadin.client.ui.grid.Renderer;
+
+/**
+ * Common interface for all selection models.
+ * <p>
+ * Selection models perform tracking of selected rows in the Grid, as well as
+ * dispatching events when the selection state changes.
+ *
+ * @author Vaadin Ltd
+ * @since
+ * @param <T>
+ * Grid's row type
+ */
+public interface SelectionModel<T> {
+
+ /**
+ * Return true if the provided row is considered selected under the
+ * implementing selection model.
+ *
+ * @param row
+ * row object instance
+ * @return <code>true</code>, if the row given as argument is considered
+ * selected.
+ */
+ public boolean isSelected(T row);
+
+ /**
+ * Return the {@link Renderer} responsible for rendering the selection
+ * column.
+ *
+ * @return a renderer instance. If null is returned, a selection column will
+ * not be drawn.
+ */
+ public Renderer<Boolean> getSelectionColumnRenderer();
+
+ /**
+ * Tells this SelectionModel which Grid it belongs to.
+ * <p>
+ * Implementations are free to have this be a no-op. This method is called
+ * internally by Grid.
+ *
+ * @param grid
+ * a {@link Grid} instance
+ */
+ public void setGrid(Grid<T> grid);
+
+ /**
+ * Resets the SelectionModel to the initial state.
+ * <p>
+ * This method can be called internally, for example, when the attached
+ * Grid's data source changes.
+ */
+ public void reset();
+
+ /**
+ * Returns a Collection containing all selected rows.
+ *
+ * @return a non-null collection.
+ */
+ public Collection<T> getSelectedRows();
+
+ /**
+ * Selection model that allows a maximum of one row to be selected at any
+ * one time.
+ *
+ * @param <T>
+ * type parameter corresponding with Grid row type
+ */
+ public interface Single<T> extends SelectionModel<T> {
+
+ /**
+ * Selects a row.
+ *
+ * @param row
+ * a {@link Grid} row object
+ * @return true, if this row as not previously selected.
+ */
+ public boolean select(T row);
+
+ /**
+ * Deselects a row.
+ * <p>
+ * This is a no-op unless {@link row} is the currently selected row.
+ *
+ * @param row
+ * a {@link Grid} row object
+ * @return true, if the currently selected row was deselected.
+ */
+ public boolean deselect(T row);
+
+ /**
+ * Returns the currently selected row.
+ *
+ * @return a {@link Grid} row object or null, if nothing is selected.
+ */
+ public T getSelectedRow();
+
+ }
+
+ /**
+ * Selection model that allows for several rows to be selected at once.
+ *
+ * @param <T>
+ * type parameter corresponding with Grid row type
+ */
+ public interface Multi<T> extends SelectionModel<T> {
+
+ /**
+ * A multi selection model that can send selections and deselections in
+ * a batch, instead of committing them one-by-one.
+ *
+ * @param <T>
+ * type parameter corresponding with Grid row type
+ */
+ public interface Batched<T> extends Multi<T> {
+ /**
+ * Starts a batch selection.
+ * <p>
+ * Any commands to any select or deselect method will be batched
+ * into one, and a final selection event will be fired when
+ * {@link #commitBatchSelect()} is called.
+ * <p>
+ * <em>Note:</em> {@link SelectionChangeEvent SelectionChangeEvents}
+ * will still be fired for each selection/deselection. You should
+ * check whether the event is a part of a batch or not with
+ * {@link SelectionChangeEvent#isBatchedSelection()}.
+ */
+ public void startBatchSelect();
+
+ /**
+ * Commits and ends a batch selection.
+ * <p>
+ * Any and all selections and deselections since the last invocation
+ * of {@link #startBatchSelect()} will be fired at once as one
+ * collated {@link SelectionChangeEvent}.
+ */
+ public void commitBatchSelect();
+
+ /**
+ * Checks whether or not a batch has been started.
+ *
+ * @return <code>true</code> iff a batch has been started
+ */
+ public boolean isBeingBatchSelected();
+
+ /**
+ * Gets all the rows that would become selected in this batch.
+ *
+ * @return a collection of the rows that would become selected
+ */
+ public Collection<T> getSelectedRowsBatch();
+
+ /**
+ * Gets all the rows that would become deselected in this batch.
+ *
+ * @return a collection of the rows that would become deselected
+ */
+ public Collection<T> getDeselectedRowsBatch();
+ }
+
+ /**
+ * Selects one or more rows.
+ *
+ * @param rows
+ * {@link Grid} row objects
+ * @return true, if the set of selected rows was changed.
+ */
+ public boolean select(T... rows);
+
+ /**
+ * Deselects one or more rows.
+ *
+ * @param rows
+ * Grid row objects
+ * @return true, if the set of selected rows was changed.
+ */
+ public boolean deselect(T... rows);
+
+ /**
+ * De-selects all rows.
+ *
+ * @return true, if any row was previously selected.
+ */
+ public boolean deselectAll();
+
+ /**
+ * Select all rows in a {@link Collection}.
+ *
+ * @param rows
+ * a collection of Grid row objects
+ * @return true, if the set of selected rows was changed.
+ */
+ public boolean select(Collection<T> rows);
+
+ /**
+ * Deselect all rows in a {@link Collection}.
+ *
+ * @param rows
+ * a collection of Grid row objects
+ * @return true, if the set of selected rows was changed.
+ */
+ public boolean deselect(Collection<T> rows);
+
+ }
+
+ /**
+ * Interface for a selection model that does not allow anything to be
+ * selected.
+ *
+ * @param <T>
+ * type parameter corresponding with Grid row type
+ */
+ public interface None<T> extends SelectionModel<T> {
+
+ }
+
+}
diff --git a/client/src/com/vaadin/client/ui/grid/selection/SelectionModelMulti.java b/client/src/com/vaadin/client/ui/grid/selection/SelectionModelMulti.java
new file mode 100644
index 0000000000..b6ecc945e2
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/selection/SelectionModelMulti.java
@@ -0,0 +1,258 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid.selection;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedHashSet;
+import java.util.Set;
+
+import com.vaadin.client.data.DataSource.RowHandle;
+import com.vaadin.client.ui.grid.Grid;
+import com.vaadin.client.ui.grid.Renderer;
+
+/**
+ * Multi-row selection model.
+ *
+ * @author Vaadin Ltd
+ * @since
+ */
+public class SelectionModelMulti<T> extends AbstractRowHandleSelectionModel<T>
+ implements SelectionModel.Multi.Batched<T> {
+
+ private final LinkedHashSet<RowHandle<T>> selectedRows;
+ private Renderer<Boolean> renderer;
+ private Grid<T> grid;
+
+ private boolean batchStarted = false;
+ private final LinkedHashSet<RowHandle<T>> selectionBatch = new LinkedHashSet<RowHandle<T>>();
+ private final LinkedHashSet<RowHandle<T>> deselectionBatch = new LinkedHashSet<RowHandle<T>>();
+
+ public SelectionModelMulti() {
+ grid = null;
+ renderer = null;
+ selectedRows = new LinkedHashSet<RowHandle<T>>();
+ }
+
+ @Override
+ public boolean isSelected(T row) {
+ return isSelectedByHandle(grid.getDataSource().getHandle(row));
+ }
+
+ @Override
+ public Renderer<Boolean> getSelectionColumnRenderer() {
+ return renderer;
+ }
+
+ @Override
+ public void setGrid(Grid<T> grid) {
+ if (grid == null) {
+ throw new IllegalArgumentException("Grid cannot be null");
+ }
+
+ if (this.grid == null) {
+ this.grid = grid;
+ } else {
+ throw new IllegalStateException(
+ "Grid reference cannot be reassigned");
+ }
+
+ this.renderer = new MultiSelectionRenderer<T>(grid);
+ }
+
+ @Override
+ public boolean select(T... rows) {
+ if (rows == null) {
+ throw new IllegalArgumentException("Rows cannot be null");
+ }
+ return select(Arrays.asList(rows));
+ }
+
+ @Override
+ public boolean deselect(T... rows) {
+ if (rows == null) {
+ throw new IllegalArgumentException("Rows cannot be null");
+ }
+ return deselect(Arrays.asList(rows));
+ }
+
+ @Override
+ public boolean deselectAll() {
+ if (selectedRows.size() > 0) {
+
+ @SuppressWarnings("unchecked")
+ final LinkedHashSet<RowHandle<T>> selectedRowsClone = (LinkedHashSet<RowHandle<T>>) selectedRows
+ .clone();
+ SelectionChangeEvent<T> event = new SelectionChangeEvent<T>(grid,
+ null, getSelectedRows(), isBeingBatchSelected());
+ selectedRows.clear();
+
+ if (isBeingBatchSelected()) {
+ selectionBatch.clear();
+ deselectionBatch.clear();
+ deselectionBatch.addAll(selectedRowsClone);
+ }
+
+ grid.fireEvent(event);
+ return true;
+ }
+ return false;
+ }
+
+ @Override
+ public boolean select(Collection<T> rows) {
+ if (rows == null) {
+ throw new IllegalArgumentException("Rows cannot be null");
+ }
+
+ Set<T> added = new LinkedHashSet<T>();
+
+ for (T row : rows) {
+ RowHandle<T> handle = grid.getDataSource().getHandle(row);
+ if (selectByHandle(handle)) {
+ added.add(row);
+ }
+ }
+
+ if (added.size() > 0) {
+ grid.fireEvent(new SelectionChangeEvent<T>(grid, added, null,
+ isBeingBatchSelected()));
+
+ return true;
+ }
+ return false;
+ }
+
+ @Override
+ public boolean deselect(Collection<T> rows) {
+ if (rows == null) {
+ throw new IllegalArgumentException("Rows cannot be null");
+ }
+
+ Set<T> removed = new LinkedHashSet<T>();
+
+ for (T row : rows) {
+ RowHandle<T> handle = grid.getDataSource().getHandle(row);
+ if (deselectByHandle(handle)) {
+ removed.add(row);
+ }
+ }
+
+ if (removed.size() > 0) {
+ grid.fireEvent(new SelectionChangeEvent<T>(grid, null, removed,
+ isBeingBatchSelected()));
+ return true;
+ }
+ return false;
+ }
+
+ protected boolean isSelectedByHandle(RowHandle<T> handle) {
+ return selectedRows.contains(handle);
+ }
+
+ @Override
+ protected boolean selectByHandle(RowHandle<T> handle) {
+ if (selectedRows.add(handle)) {
+ handle.pin();
+
+ if (isBeingBatchSelected()) {
+ deselectionBatch.remove(handle);
+ selectionBatch.add(handle);
+ }
+
+ return true;
+ }
+ return false;
+ }
+
+ @Override
+ protected boolean deselectByHandle(RowHandle<T> handle) {
+ if (selectedRows.remove(handle)) {
+ if (!isBeingBatchSelected()) {
+ handle.unpin();
+ } else {
+ selectionBatch.remove(handle);
+ deselectionBatch.add(handle);
+ }
+ return true;
+ }
+ return false;
+ }
+
+ @Override
+ public Collection<T> getSelectedRows() {
+ Set<T> selected = new LinkedHashSet<T>();
+ for (RowHandle<T> handle : selectedRows) {
+ selected.add(handle.getRow());
+ }
+ return Collections.unmodifiableSet(selected);
+ }
+
+ @Override
+ public void reset() {
+ deselectAll();
+ }
+
+ @Override
+ public void startBatchSelect() {
+ assert !isBeingBatchSelected() : "Batch has already been started";
+ batchStarted = true;
+ }
+
+ @Override
+ public void commitBatchSelect() {
+ assert isBeingBatchSelected() : "Batch was never started";
+ if (!isBeingBatchSelected()) {
+ return;
+ }
+
+ batchStarted = false;
+
+ final Collection<T> added = getSelectedRowsBatch();
+ selectionBatch.clear();
+
+ final Collection<T> removed = getDeselectedRowsBatch();
+ deselectionBatch.clear();
+
+ grid.fireEvent(new SelectionChangeEvent<T>(grid, added, removed,
+ isBeingBatchSelected()));
+ }
+
+ @Override
+ public boolean isBeingBatchSelected() {
+ return batchStarted;
+ }
+
+ @Override
+ public Collection<T> getSelectedRowsBatch() {
+ return rowHandlesToRows(selectionBatch);
+ }
+
+ @Override
+ public Collection<T> getDeselectedRowsBatch() {
+ return rowHandlesToRows(deselectionBatch);
+ }
+
+ private ArrayList<T> rowHandlesToRows(Collection<RowHandle<T>> rowHandles) {
+ ArrayList<T> rows = new ArrayList<T>(rowHandles.size());
+ for (RowHandle<T> handle : rowHandles) {
+ rows.add(handle.getRow());
+ }
+ return rows;
+ }
+}
diff --git a/client/src/com/vaadin/client/ui/grid/selection/SelectionModelNone.java b/client/src/com/vaadin/client/ui/grid/selection/SelectionModelNone.java
new file mode 100644
index 0000000000..8192237da0
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/selection/SelectionModelNone.java
@@ -0,0 +1,73 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid.selection;
+
+import java.util.Collection;
+import java.util.Collections;
+
+import com.vaadin.client.data.DataSource.RowHandle;
+import com.vaadin.client.ui.grid.Grid;
+import com.vaadin.client.ui.grid.Renderer;
+
+/**
+ * No-row selection model.
+ *
+ * @author Vaadin Ltd
+ * @since
+ */
+public class SelectionModelNone<T> extends AbstractRowHandleSelectionModel<T>
+ implements SelectionModel.None<T> {
+
+ @Override
+ public boolean isSelected(T row) {
+ return false;
+ }
+
+ @Override
+ public Renderer<Boolean> getSelectionColumnRenderer() {
+ return null;
+ }
+
+ @Override
+ public void setGrid(Grid<T> grid) {
+ // noop
+ }
+
+ @Override
+ public void reset() {
+ // noop
+ }
+
+ @Override
+ public Collection<T> getSelectedRows() {
+ return Collections.emptySet();
+ }
+
+ @Override
+ protected boolean selectByHandle(RowHandle<T> handle)
+ throws UnsupportedOperationException {
+ throw new UnsupportedOperationException("This selection model "
+ + "does not support selection");
+ }
+
+ @Override
+ protected boolean deselectByHandle(RowHandle<T> handle)
+ throws UnsupportedOperationException {
+ throw new UnsupportedOperationException("This selection model "
+ + "does not support deselection");
+ }
+
+}
diff --git a/client/src/com/vaadin/client/ui/grid/selection/SelectionModelSingle.java b/client/src/com/vaadin/client/ui/grid/selection/SelectionModelSingle.java
new file mode 100644
index 0000000000..d63b371c4d
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/selection/SelectionModelSingle.java
@@ -0,0 +1,139 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid.selection;
+
+import java.util.Collection;
+import java.util.Collections;
+
+import com.vaadin.client.data.DataSource.RowHandle;
+import com.vaadin.client.ui.grid.Grid;
+import com.vaadin.client.ui.grid.Renderer;
+
+/**
+ * Single-row selection model.
+ *
+ * @author Vaadin Ltd
+ * @since
+ */
+public class SelectionModelSingle<T> extends AbstractRowHandleSelectionModel<T>
+ implements SelectionModel.Single<T> {
+
+ private Grid<T> grid;
+ private RowHandle<T> selectedRow;
+ private Renderer<Boolean> renderer;
+
+ @Override
+ public boolean isSelected(T row) {
+ return selectedRow != null
+ && selectedRow.equals(grid.getDataSource().getHandle(row));
+ }
+
+ @Override
+ public Renderer<Boolean> getSelectionColumnRenderer() {
+ return renderer;
+ }
+
+ @Override
+ public void setGrid(Grid<T> grid) {
+ if (grid == null) {
+ throw new IllegalArgumentException("Grid cannot be null");
+ }
+
+ if (this.grid == null) {
+ this.grid = grid;
+ } else {
+ throw new IllegalStateException(
+ "Grid reference cannot be reassigned");
+ }
+ renderer = new MultiSelectionRenderer<T>(grid);
+ }
+
+ @Override
+ public boolean select(T row) {
+
+ if (row == null) {
+ throw new IllegalArgumentException("Row cannot be null");
+ }
+
+ T removed = getSelectedRow();
+ if (selectByHandle(grid.getDataSource().getHandle(row))) {
+ grid.fireEvent(new SelectionChangeEvent<T>(grid, row, removed,
+ false));
+
+ return true;
+ }
+ return false;
+ }
+
+ @Override
+ public boolean deselect(T row) {
+
+ if (row == null) {
+ throw new IllegalArgumentException("Row cannot be null");
+ }
+
+ if (isSelected(row)) {
+ deselectByHandle(selectedRow);
+ grid.fireEvent(new SelectionChangeEvent<T>(grid, null, row, false));
+ return true;
+ }
+
+ return false;
+ }
+
+ @Override
+ public T getSelectedRow() {
+ return (selectedRow != null ? selectedRow.getRow() : null);
+ }
+
+ @Override
+ public void reset() {
+ if (selectedRow != null) {
+ deselect(getSelectedRow());
+ }
+ }
+
+ @Override
+ public Collection<T> getSelectedRows() {
+ if (getSelectedRow() != null) {
+ return Collections.singleton(getSelectedRow());
+ }
+ return Collections.emptySet();
+ }
+
+ @Override
+ protected boolean selectByHandle(RowHandle<T> handle) {
+ if (handle != null && !handle.equals(selectedRow)) {
+ deselectByHandle(selectedRow);
+ selectedRow = handle;
+ selectedRow.pin();
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ @Override
+ protected boolean deselectByHandle(RowHandle<T> handle) {
+ if (handle != null && handle.equals(selectedRow)) {
+ selectedRow.unpin();
+ selectedRow = null;
+ return true;
+ } else {
+ return false;
+ }
+ }
+}
diff --git a/client/src/com/vaadin/client/ui/grid/sort/Sort.java b/client/src/com/vaadin/client/ui/grid/sort/Sort.java
new file mode 100644
index 0000000000..00658c4375
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/sort/Sort.java
@@ -0,0 +1,155 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid.sort;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import com.vaadin.client.ui.grid.GridColumn;
+import com.vaadin.shared.ui.grid.SortDirection;
+
+/**
+ * Fluid Sort descriptor object.
+ *
+ * @since
+ * @author Vaadin Ltd
+ * @param T
+ * grid data type
+ */
+public class Sort {
+
+ private final Sort previous;
+ private final SortOrder order;
+ private final int count;
+
+ /**
+ * Basic constructor, used by the {@link #by(GridColumn)} and
+ * {@link #by(GridColumn, SortDirection)} methods.
+ *
+ * @param column
+ * a grid column
+ * @param direction
+ * a sort direction
+ */
+ private Sort(GridColumn<?, ?> column, SortDirection direction) {
+ previous = null;
+ count = 1;
+ order = new SortOrder(column, direction);
+ }
+
+ /**
+ * Extension constructor. Performs object equality checks on all previous
+ * Sort objects in the chain to make sure that the column being passed in
+ * isn't already used earlier (which would indicate a bug). If the column
+ * has been used before, this constructor throws an
+ * {@link IllegalStateException}.
+ *
+ * @param previous
+ * the sort instance that the new sort instance is to extend
+ * @param column
+ * a (previously unused) grid column reference
+ * @param direction
+ * a sort direction
+ */
+ private Sort(Sort previous, GridColumn<?, ?> column, SortDirection direction) {
+ this.previous = previous;
+ count = previous.count + 1;
+ order = new SortOrder(column, direction);
+
+ Sort s = previous;
+ while (s != null) {
+ if (s.order.getColumn() == column) {
+ throw new IllegalStateException(
+ "Can not sort along the same column twice");
+ }
+ s = s.previous;
+ }
+ }
+
+ /**
+ * Start building a Sort order by sorting a provided column in ascending
+ * order.
+ *
+ * @param column
+ * a grid column object reference
+ * @return a sort instance, typed to the grid data type
+ */
+ public static Sort by(GridColumn<?, ?> column) {
+ return by(column, SortDirection.ASCENDING);
+ }
+
+ /**
+ * Start building a Sort order by sorting a provided column.
+ *
+ * @param column
+ * a grid column object reference
+ * @param direction
+ * indicator of sort direction - either ascending or descending
+ * @return a sort instance, typed to the grid data type
+ */
+ public static Sort by(GridColumn<?, ?> column, SortDirection direction) {
+ return new Sort(column, direction);
+ }
+
+ /**
+ * Continue building a Sort order. The provided column is sorted in
+ * ascending order if the previously added columns have been evaluated as
+ * equals.
+ *
+ * @param column
+ * a grid column object reference
+ * @return a sort instance, typed to the grid data type
+ */
+ public Sort then(GridColumn<?, ?> column) {
+ return then(column, SortDirection.ASCENDING);
+ }
+
+ /**
+ * Continue building a Sort order. The provided column is sorted in
+ * specified order if the previously added columns have been evaluated as
+ * equals.
+ *
+ * @param column
+ * a grid column object reference
+ * @param direction
+ * indicator of sort direction - either ascending or descending
+ * @return a sort instance, typed to the grid data type
+ */
+ public Sort then(GridColumn<?, ?> column, SortDirection direction) {
+ return new Sort(this, column, direction);
+ }
+
+ /**
+ * Build a sort order list. This method is called internally by Grid when
+ * calling {@link com.vaadin.client.ui.grid.Grid#sort(Sort)}, but can also
+ * be called manually to create a SortOrder list, which can also be provided
+ * directly to Grid.
+ *
+ * @return a sort order list.
+ */
+ public List<SortOrder> build() {
+
+ List<SortOrder> order = new ArrayList<SortOrder>(count);
+
+ Sort s = this;
+ for (int i = count - 1; i >= 0; --i) {
+ order.add(0, s.order);
+ s = s.previous;
+ }
+
+ return order;
+ }
+}
diff --git a/client/src/com/vaadin/client/ui/grid/sort/SortEvent.java b/client/src/com/vaadin/client/ui/grid/sort/SortEvent.java
new file mode 100644
index 0000000000..edbd84c4a5
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/sort/SortEvent.java
@@ -0,0 +1,128 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid.sort;
+
+import java.util.List;
+
+import com.google.gwt.event.shared.GwtEvent;
+import com.vaadin.client.data.DataSource;
+import com.vaadin.client.ui.grid.Grid;
+import com.vaadin.shared.ui.grid.SortEventOriginator;
+
+/**
+ * A sort event, fired by the Grid when it needs its data source to provide data
+ * sorted in a specific manner.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public class SortEvent<T> extends GwtEvent<SortEventHandler<?>> {
+
+ private static final Type<SortEventHandler<?>> TYPE = new Type<SortEventHandler<?>>();
+
+ private final Grid<T> grid;
+ private final List<SortOrder> order;
+ private final SortEventOriginator originator;
+
+ /**
+ * Creates a new Sort Event. All provided parameters are final, and passed
+ * on as-is.
+ *
+ * @param grid
+ * a grid reference
+ * @param order
+ * an array dictating the desired sort order of the data source
+ * @param originator
+ * a value indicating where this event originated from
+ */
+ public SortEvent(Grid<T> grid, List<SortOrder> order,
+ SortEventOriginator originator) {
+ this.grid = grid;
+ this.order = order;
+ this.originator = originator;
+ }
+
+ @Override
+ public Type<SortEventHandler<?>> getAssociatedType() {
+ return TYPE;
+ }
+
+ /**
+ * Static access to the GWT event type identifier associated with this Event
+ * class
+ *
+ * @return a type object, uniquely describing this event type.
+ */
+ public static Type<SortEventHandler<?>> getType() {
+ return TYPE;
+ }
+
+ /**
+ * Get access to the Grid that fired this event
+ *
+ * @return the grid instance
+ */
+ @Override
+ public Grid<T> getSource() {
+ return grid;
+ }
+
+ /**
+ * Get access to the Grid that fired this event
+ *
+ * @return the grid instance
+ */
+ public Grid<T> getGrid() {
+ return grid;
+ }
+
+ /**
+ * Access the data source of the Grid that fired this event
+ *
+ * @return a data source instance
+ */
+ public DataSource<T> getDataSource() {
+ return grid.getDataSource();
+ }
+
+ /**
+ * Get the sort ordering that is to be applied to the Grid
+ *
+ * @return a list of sort order objects
+ */
+ public List<SortOrder> getOrder() {
+ return order;
+ }
+
+ /**
+ * Gets a value describing the originator of this event, i.e. what actions
+ * resulted in this event being fired.
+ *
+ * @return a sort event originator value
+ *
+ * @see SortEventOriginator
+ */
+ public SortEventOriginator getOriginator() {
+ return originator;
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ protected void dispatch(SortEventHandler<?> handler) {
+ ((SortEventHandler<T>) handler).sort(this);
+ }
+
+}
diff --git a/client/src/com/vaadin/client/ui/grid/sort/SortEventHandler.java b/client/src/com/vaadin/client/ui/grid/sort/SortEventHandler.java
new file mode 100644
index 0000000000..57e7fc2ead
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/sort/SortEventHandler.java
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid.sort;
+
+import com.google.gwt.event.shared.EventHandler;
+
+/**
+ * Handler for a Grid sort event, called when the Grid needs its data source to
+ * provide data sorted in a specific manner.
+ *
+ * @since
+ * @author Vaadin Ltd
+ */
+public interface SortEventHandler<T> extends EventHandler {
+
+ /**
+ * Handle sorting of the Grid. This method is called when a re-sorting of
+ * the Grid's data is requested.
+ *
+ * @param event
+ * the sort event
+ */
+ public void sort(SortEvent<T> event);
+
+}
diff --git a/client/src/com/vaadin/client/ui/grid/sort/SortOrder.java b/client/src/com/vaadin/client/ui/grid/sort/SortOrder.java
new file mode 100644
index 0000000000..682beda793
--- /dev/null
+++ b/client/src/com/vaadin/client/ui/grid/sort/SortOrder.java
@@ -0,0 +1,72 @@
+/*
+ * Copyright 2000-2014 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.client.ui.grid.sort;
+
+import com.vaadin.client.ui.grid.GridColumn;
+import com.vaadin.shared.ui.grid.SortDirection;
+
+/**
+ * Sort order descriptor. Contains column and direction references.
+ *
+ * @since
+ * @author Vaadin Ltd
+ * @param T
+ * grid data type
+ */
+public class SortOrder {
+
+ private final GridColumn<?, ?> column;
+ private final SortDirection direction;
+
+ /**
+ * Create a sort order descriptor.
+ *
+ * @param column
+ * a grid column descriptor object
+ * @param direction
+ * a sorting direction value (ascending or descending)
+ */
+ public SortOrder(GridColumn<?, ?> column, SortDirection direction) {
+ if (column == null) {
+ throw new IllegalArgumentException(
+ "Grid column reference can not be null!");
+ }
+ if (direction == null) {
+ throw new IllegalArgumentException(
+ "Direction value can not be null!");
+ }
+ this.column = column;
+ this.direction = direction;
+ }
+
+ /**
+ * Returns the {@link GridColumn} reference given in the constructor.
+ *
+ * @return a grid column reference
+ */
+ public GridColumn<?, ?> getColumn() {
+ return column;
+ }
+
+ /**
+ * Returns the {@link SortDirection} value given in the constructor.
+ *
+ * @return a sort direction value
+ */
+ public SortDirection getDirection() {
+ return direction;
+ }
+}
diff --git a/client/tests/src/com/vaadin/client/ui/grid/ListDataSourceTest.java b/client/tests/src/com/vaadin/client/ui/grid/ListDataSourceTest.java
new file mode 100644
index 0000000000..55a2b56ee2
--- /dev/null
+++ b/client/tests/src/com/vaadin/client/ui/grid/ListDataSourceTest.java
@@ -0,0 +1,192 @@
+/*
+ * Copyright 2000-2013 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.client.ui.grid;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Arrays;
+import java.util.Comparator;
+
+import org.easymock.EasyMock;
+import org.junit.Test;
+
+import com.vaadin.client.data.DataChangeHandler;
+import com.vaadin.client.ui.grid.datasources.ListDataSource;
+
+public class ListDataSourceTest {
+
+ @Test
+ public void testDataSourceConstruction() throws Exception {
+
+ ListDataSource<Integer> ds = new ListDataSource<Integer>(0, 1, 2, 3);
+
+ assertEquals(4, ds.getEstimatedSize());
+ assertEquals(0, (int) ds.getRow(0));
+ assertEquals(1, (int) ds.getRow(1));
+ assertEquals(2, (int) ds.getRow(2));
+ assertEquals(3, (int) ds.getRow(3));
+
+ ds = new ListDataSource<Integer>(Arrays.asList(0, 1, 2, 3));
+
+ assertEquals(4, ds.getEstimatedSize());
+ assertEquals(0, (int) ds.getRow(0));
+ assertEquals(1, (int) ds.getRow(1));
+ assertEquals(2, (int) ds.getRow(2));
+ assertEquals(3, (int) ds.getRow(3));
+ }
+
+ @Test
+ public void testListAddOperation() throws Exception {
+
+ ListDataSource<Integer> ds = new ListDataSource<Integer>(0, 1, 2, 3);
+
+ DataChangeHandler handler = EasyMock
+ .createNiceMock(DataChangeHandler.class);
+ ds.setDataChangeHandler(handler);
+
+ handler.dataAdded(4, 1);
+ EasyMock.expectLastCall();
+
+ EasyMock.replay(handler);
+
+ ds.asList().add(4);
+
+ assertEquals(5, ds.getEstimatedSize());
+ assertEquals(0, (int) ds.getRow(0));
+ assertEquals(1, (int) ds.getRow(1));
+ assertEquals(2, (int) ds.getRow(2));
+ assertEquals(3, (int) ds.getRow(3));
+ assertEquals(4, (int) ds.getRow(4));
+ }
+
+ @Test
+ public void testListAddAllOperation() throws Exception {
+
+ ListDataSource<Integer> ds = new ListDataSource<Integer>(0, 1, 2, 3);
+
+ DataChangeHandler handler = EasyMock
+ .createNiceMock(DataChangeHandler.class);
+ ds.setDataChangeHandler(handler);
+
+ handler.dataAdded(4, 3);
+ EasyMock.expectLastCall();
+
+ EasyMock.replay(handler);
+
+ ds.asList().addAll(Arrays.asList(4, 5, 6));
+
+ assertEquals(7, ds.getEstimatedSize());
+ assertEquals(0, (int) ds.getRow(0));
+ assertEquals(1, (int) ds.getRow(1));
+ assertEquals(2, (int) ds.getRow(2));
+ assertEquals(3, (int) ds.getRow(3));
+ assertEquals(4, (int) ds.getRow(4));
+ assertEquals(5, (int) ds.getRow(5));
+ assertEquals(6, (int) ds.getRow(6));
+ }
+
+ @Test
+ public void testListRemoveOperation() throws Exception {
+
+ ListDataSource<Integer> ds = new ListDataSource<Integer>(0, 1, 2, 3);
+
+ DataChangeHandler handler = EasyMock
+ .createNiceMock(DataChangeHandler.class);
+ ds.setDataChangeHandler(handler);
+
+ handler.dataRemoved(3, 1);
+ EasyMock.expectLastCall();
+
+ EasyMock.replay(handler);
+
+ ds.asList().remove(2);
+
+ assertEquals(3, ds.getEstimatedSize());
+ assertEquals(0, (int) ds.getRow(0));
+ assertEquals(1, (int) ds.getRow(1));
+ assertEquals(3, (int) ds.getRow(2));
+ }
+
+ @Test
+ public void testListRemoveAllOperation() throws Exception {
+
+ ListDataSource<Integer> ds = new ListDataSource<Integer>(0, 1, 2, 3);
+
+ DataChangeHandler handler = EasyMock
+ .createNiceMock(DataChangeHandler.class);
+ ds.setDataChangeHandler(handler);
+
+ handler.dataRemoved(0, 3);
+ EasyMock.expectLastCall();
+
+ EasyMock.replay(handler);
+
+ ds.asList().removeAll(Arrays.asList(0, 2, 3));
+
+ assertEquals(1, ds.getEstimatedSize());
+ assertEquals(1, (int) ds.getRow(0));
+ }
+
+ @Test
+ public void testListClearOperation() throws Exception {
+
+ ListDataSource<Integer> ds = new ListDataSource<Integer>(0, 1, 2, 3);
+
+ DataChangeHandler handler = EasyMock
+ .createNiceMock(DataChangeHandler.class);
+ ds.setDataChangeHandler(handler);
+
+ handler.dataRemoved(0, 4);
+ EasyMock.expectLastCall();
+
+ EasyMock.replay(handler);
+
+ ds.asList().clear();
+
+ assertEquals(0, ds.getEstimatedSize());
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void testFetchingNonExistantItem() {
+ ListDataSource<Integer> ds = new ListDataSource<Integer>(0, 1, 2, 3);
+ ds.ensureAvailability(5, 1);
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void testUnsupportedIteratorRemove() {
+ ListDataSource<Integer> ds = new ListDataSource<Integer>(0, 1, 2, 3);
+ ds.asList().iterator().remove();
+ }
+
+ @Test
+ public void sortColumn() {
+ ListDataSource<Integer> ds = new ListDataSource<Integer>(3, 4, 2, 3, 1);
+
+ // TODO Should be simplified to sort(). No point in providing these
+ // trivial comparators.
+ ds.sort(new Comparator<Integer>() {
+ @Override
+ public int compare(Integer o1, Integer o2) {
+ return o1.compareTo(o2);
+ }
+ });
+
+ assertTrue(Arrays.equals(ds.asList().toArray(), new Integer[] { 1, 2,
+ 3, 3, 4 }));
+ }
+
+}
diff --git a/client/tests/src/com/vaadin/client/ui/grid/PartitioningTest.java b/client/tests/src/com/vaadin/client/ui/grid/PartitioningTest.java
new file mode 100644
index 0000000000..e97bb339e4
--- /dev/null
+++ b/client/tests/src/com/vaadin/client/ui/grid/PartitioningTest.java
@@ -0,0 +1,104 @@
+/*
+ * Copyright 2000-2013 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.client.ui.grid;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+import com.vaadin.shared.ui.grid.Range;
+
+@SuppressWarnings("static-method")
+public class PartitioningTest {
+
+ @Test
+ public void selfRangeTest() {
+ final Range range = Range.between(0, 10);
+ final Range[] partitioning = range.partitionWith(range);
+
+ assertTrue("before is empty", partitioning[0].isEmpty());
+ assertTrue("inside is self", partitioning[1].equals(range));
+ assertTrue("after is empty", partitioning[2].isEmpty());
+ }
+
+ @Test
+ public void beforeRangeTest() {
+ final Range beforeRange = Range.between(0, 10);
+ final Range afterRange = Range.between(10, 20);
+ final Range[] partitioning = beforeRange.partitionWith(afterRange);
+
+ assertTrue("before is self", partitioning[0].equals(beforeRange));
+ assertTrue("inside is empty", partitioning[1].isEmpty());
+ assertTrue("after is empty", partitioning[2].isEmpty());
+ }
+
+ @Test
+ public void afterRangeTest() {
+ final Range beforeRange = Range.between(0, 10);
+ final Range afterRange = Range.between(10, 20);
+ final Range[] partitioning = afterRange.partitionWith(beforeRange);
+
+ assertTrue("before is empty", partitioning[0].isEmpty());
+ assertTrue("inside is empty", partitioning[1].isEmpty());
+ assertTrue("after is self", partitioning[2].equals(afterRange));
+ }
+
+ @Test
+ public void beforeAndInsideRangeTest() {
+ final Range beforeRange = Range.between(0, 10);
+ final Range afterRange = Range.between(5, 15);
+ final Range[] partitioning = beforeRange.partitionWith(afterRange);
+
+ assertEquals("before", Range.between(0, 5), partitioning[0]);
+ assertEquals("inside", Range.between(5, 10), partitioning[1]);
+ assertTrue("after is empty", partitioning[2].isEmpty());
+ }
+
+ @Test
+ public void insideRangeTest() {
+ final Range fullRange = Range.between(0, 20);
+ final Range insideRange = Range.between(5, 15);
+ final Range[] partitioning = insideRange.partitionWith(fullRange);
+
+ assertTrue("before is empty", partitioning[0].isEmpty());
+ assertEquals("inside", Range.between(5, 15), partitioning[1]);
+ assertTrue("after is empty", partitioning[2].isEmpty());
+ }
+
+ @Test
+ public void insideAndBelowTest() {
+ final Range beforeRange = Range.between(0, 10);
+ final Range afterRange = Range.between(5, 15);
+ final Range[] partitioning = afterRange.partitionWith(beforeRange);
+
+ assertTrue("before is empty", partitioning[0].isEmpty());
+ assertEquals("inside", Range.between(5, 10), partitioning[1]);
+ assertEquals("after", Range.between(10, 15), partitioning[2]);
+ }
+
+ @Test
+ public void aboveAndBelowTest() {
+ final Range fullRange = Range.between(0, 20);
+ final Range insideRange = Range.between(5, 15);
+ final Range[] partitioning = fullRange.partitionWith(insideRange);
+
+ assertEquals("before", Range.between(0, 5), partitioning[0]);
+ assertEquals("inside", Range.between(5, 15), partitioning[1]);
+ assertEquals("after", Range.between(15, 20), partitioning[2]);
+ }
+}