You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2014/08/27 20:52:07 UTC
git commit: TAP5-1689: Grid pagination details should be overridable
via a Grid parameter to support Grids that render in loops
Repository: tapestry-5
Updated Branches:
refs/heads/master 9a3705bc8 -> 018d3c424
TAP5-1689: Grid pagination details should be overridable via a Grid parameter to support Grids that render in loops
Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo
Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/018d3c42
Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/018d3c42
Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/018d3c42
Branch: refs/heads/master
Commit: 018d3c424fb78f74034a4e2fdbf762cf0ee771b8
Parents: 9a3705b
Author: Howard M. Lewis Ship <hl...@apache.org>
Authored: Wed Aug 27 11:51:27 2014 -0700
Committer: Howard M. Lewis Ship <hl...@apache.org>
Committed: Wed Aug 27 11:51:27 2014 -0700
----------------------------------------------------------------------
.../tapestry5/corelib/components/Grid.java | 68 ++++++++++++-----
.../tapestry5/grid/GridPaginationModel.java | 34 +++++++++
.../tapestry5/grid/GridPaginationModelImpl.java | 80 ++++++++++++++++++++
3 files changed, 163 insertions(+), 19 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/018d3c42/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Grid.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Grid.java b/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Grid.java
index dd3fda4..c69a95d 100644
--- a/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Grid.java
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Grid.java
@@ -1,5 +1,3 @@
-// Copyright 2007, 2008, 2009, 2010, 2011, 2012, 2014 The Apache Software Foundation
-//
// 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
@@ -193,7 +191,7 @@ public class Grid implements GridModel, ClientElement
*/
@Parameter
private boolean inPlace;
-
+
/**
* If true, then the Grid will also render a table element complete with headers if the data source is empty.
* If set to true, a model parameter will have to be specified. A default model for a specific class can be
@@ -201,7 +199,7 @@ public class Grid implements GridModel, ClientElement
*/
@Parameter
private boolean renderTableIfEmpty = false;
-
+
/**
* The name of the pseudo-zone that encloses the Grid. Starting in 5.4, this is always either
@@ -214,14 +212,21 @@ public class Grid implements GridModel, ClientElement
private boolean didRenderZoneDiv;
- @Persist
- private Integer currentPage;
- @Persist
- private String sortColumnId;
+ /**
+ * The pagination model for the Grid, which encapsulates current page, sort column id,
+ * and sort ascending/descending. If not bound, a persistent property of the Grid is used.
+ * When rendering the Grid in a loop, this should be bound in some way to keep successive instances
+ * of the Grid configured individually.
+ *
+ * @since 5.4
+ */
+ @Parameter(value = "defaultPaginationModel")
+ private GridPaginationModel paginationModel;
+ @Property
@Persist
- private Boolean sortAscending;
+ private GridPaginationModel defaultPaginationModel;
@Inject
private ComponentResources resources;
@@ -330,8 +335,10 @@ public class Grid implements GridModel, ClientElement
{
public ColumnSort getColumnSort(String columnId)
{
- if (!TapestryInternalUtils.isEqual(columnId, sortColumnId))
+ if (!TapestryInternalUtils.isEqual(columnId, paginationModel.getSortColumnId()))
+ {
return ColumnSort.UNSORTED;
+ }
return getColumnSort();
}
@@ -344,22 +351,28 @@ public class Grid implements GridModel, ClientElement
public void updateSort(String columnId)
{
assert InternalUtils.isNonBlank(columnId);
- if (columnId.equals(sortColumnId))
+
+ setupPaginationModel();
+
+ if (columnId.equals(paginationModel.getSortColumnId()))
{
setSortAscending(!getSortAscending());
return;
}
- sortColumnId = columnId;
+ paginationModel.setSortColumnId(columnId);
setSortAscending(true);
}
public List<SortConstraint> getSortConstraints()
{
- if (sortColumnId == null)
+ // In a few limited cases we may not have yet hit the SetupRender phase, and the model may be null.
+ if (paginationModel == null || paginationModel.getSortColumnId() == null)
+ {
return Collections.emptyList();
+ }
- PropertyModel sortModel = getDataModel().getById(sortColumnId);
+ PropertyModel sortModel = getDataModel().getById(paginationModel.getSortColumnId());
SortConstraint constraint = new SortConstraint(sortModel, getColumnSort());
@@ -368,7 +381,8 @@ public class Grid implements GridModel, ClientElement
public void clear()
{
- sortColumnId = null;
+ paginationModel.setSortColumnId(null);
+ paginationModel.setSortAscending(null);
}
}
@@ -386,7 +400,7 @@ public class Grid implements GridModel, ClientElement
*/
protected Binding defaultModel()
{
-
+
return new AbstractBinding()
{
public Object get()
@@ -442,8 +456,12 @@ public class Grid implements GridModel, ClientElement
{
zone = null;
+ setupPaginationModel();
+
if (formSupport != null)
+ {
formSupport.store(this, SETUP_DATA_SOURCE);
+ }
setupDataSource();
@@ -452,6 +470,14 @@ public class Grid implements GridModel, ClientElement
return !renderTableIfEmpty && cachingSource.getAvailableRows() == 0 ? empty : null;
}
+ private void setupPaginationModel()
+ {
+ if (paginationModel == null)
+ {
+ paginationModel = new GridPaginationModelImpl();
+ }
+ }
+
void setupDataSource()
{
// TAP5-34: We pass the source into the CachingDataSource now; previously
@@ -529,7 +555,7 @@ public class Grid implements GridModel, ClientElement
return dataModel;
}
-
+
public int getNumberOfProperties()
{
return getDataModel().getPropertyNames().size();
@@ -557,22 +583,26 @@ public class Grid implements GridModel, ClientElement
public int getCurrentPage()
{
+ Integer currentPage = paginationModel.getCurrentPage();
+
return currentPage == null ? 1 : currentPage;
}
public void setCurrentPage(int currentPage)
{
- this.currentPage = currentPage;
+ paginationModel.setCurrentPage(currentPage);
}
private boolean getSortAscending()
{
+ Boolean sortAscending = paginationModel.getSortAscending();
+
return sortAscending != null && sortAscending.booleanValue();
}
private void setSortAscending(boolean sortAscending)
{
- this.sortAscending = sortAscending;
+ paginationModel.setSortAscending(sortAscending);
}
public int getRowsPerPage()
http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/018d3c42/tapestry-core/src/main/java/org/apache/tapestry5/grid/GridPaginationModel.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/grid/GridPaginationModel.java b/tapestry-core/src/main/java/org/apache/tapestry5/grid/GridPaginationModel.java
new file mode 100644
index 0000000..04475de
--- /dev/null
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/grid/GridPaginationModel.java
@@ -0,0 +1,34 @@
+// 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 org.apache.tapestry5.grid;
+
+/**
+ * Store pagination information, such as the current page, sort column, and
+ * sort ascending vs. descending.
+ *
+ * @since 5.4
+ */
+public interface GridPaginationModel
+{
+ String getSortColumnId();
+
+ void setSortColumnId(String sortColumnId);
+
+ Boolean getSortAscending();
+
+ void setSortAscending(Boolean sortAscending);
+
+ Integer getCurrentPage();
+
+ void setCurrentPage(Integer currentPage);
+}
http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/018d3c42/tapestry-core/src/main/java/org/apache/tapestry5/grid/GridPaginationModelImpl.java
----------------------------------------------------------------------
diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/grid/GridPaginationModelImpl.java b/tapestry-core/src/main/java/org/apache/tapestry5/grid/GridPaginationModelImpl.java
new file mode 100644
index 0000000..4b76869
--- /dev/null
+++ b/tapestry-core/src/main/java/org/apache/tapestry5/grid/GridPaginationModelImpl.java
@@ -0,0 +1,80 @@
+// 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 org.apache.tapestry5.grid;
+
+import org.apache.tapestry5.BaseOptimizedSessionPersistedObject;
+
+/**
+ * Standard implementation of {@link org.apache.tapestry5.grid.GridPaginationModel}.
+ *
+ * @since 5.4
+ */
+public class GridPaginationModelImpl extends BaseOptimizedSessionPersistedObject implements GridPaginationModel
+{
+ private static final long serialVersionUID = -5532310466213300537L;
+
+ private String sortColumnId;
+
+ private Boolean sortAscending;
+
+ private Integer currentPage;
+
+ @Override
+ public String getSortColumnId()
+ {
+ return sortColumnId;
+ }
+
+ @Override
+ public void setSortColumnId(String sortColumnId)
+ {
+ this.sortColumnId = sortColumnId;
+
+ markDirty();
+ }
+
+ @Override
+ public Boolean getSortAscending()
+ {
+ return sortAscending;
+ }
+
+ @Override
+ public void setSortAscending(Boolean sortAscending)
+ {
+ this.sortAscending = sortAscending;
+
+ markDirty();
+ }
+
+ @Override
+ public Integer getCurrentPage()
+ {
+ return currentPage;
+ }
+
+ @Override
+ public void setCurrentPage(Integer currentPage)
+ {
+ this.currentPage = currentPage;
+
+ markDirty();
+ }
+
+ @Override
+ public String toString()
+ {
+ return String.format("GridPaginationModelImpl[currentPage=%s, sortColumnId=%s, sortAscending=%s]",
+ currentPage, sortColumnId, sortAscending);
+ }
+}