Author: abelevich
Date: 2010-03-31 11:21:40 -0400 (Wed, 31 Mar 2010)
New Revision: 16695
Added:
root/ui-sandbox/trunk/components/datascroller/ui/src/main/java/org/richfaces/event/
root/ui-sandbox/trunk/components/datascroller/ui/src/main/java/org/richfaces/event/DataTablePreRenderListener.java
Log:
add dataTable's PreRenderEvent listener.
Added:
root/ui-sandbox/trunk/components/datascroller/ui/src/main/java/org/richfaces/event/DataTablePreRenderListener.java
===================================================================
---
root/ui-sandbox/trunk/components/datascroller/ui/src/main/java/org/richfaces/event/DataTablePreRenderListener.java
(rev 0)
+++
root/ui-sandbox/trunk/components/datascroller/ui/src/main/java/org/richfaces/event/DataTablePreRenderListener.java 2010-03-31
15:21:40 UTC (rev 16695)
@@ -0,0 +1,182 @@
+package org.richfaces.event;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.Map.Entry;
+
+import javax.el.ValueExpression;
+import javax.faces.component.NamingContainer;
+import javax.faces.component.UIComponent;
+import javax.faces.component.UIData;
+import javax.faces.context.FacesContext;
+import javax.faces.event.AbortProcessingException;
+import javax.faces.event.SystemEvent;
+import javax.faces.event.SystemEventListener;
+
+import org.ajax4jsf.Messages;
+import org.richfaces.DataScrollerUtils;
+import org.richfaces.component.UIDataAdaptor;
+import org.richfaces.component.UIDataScroller;
+import org.richfaces.component.UIDataScroller.PageMode;
+import org.richfaces.component.util.MessageUtil;
+import org.richfaces.log.RichfacesLogger;
+import org.slf4j.Logger;
+
+public class DataTablePreRenderListener implements SystemEventListener {
+
+ private static final Logger log = RichfacesLogger.COMPONENTS.getLogger();
+
+ @Override
+ public boolean isListenerForSource(Object source) {
+ return ((source instanceof UIDataAdaptor) || (source instanceof UIData));
+ }
+
+
+ public UIDataScroller processActiveDatascroller(FacesContext facesContext,
List<UIDataScroller> dataScrollers, UIComponent dataTable) {
+
+ UIDataScroller activeComponent = null;
+ List<Object> values = new ArrayList<Object>(dataScrollers.size());
+
+ String stateKey = dataTable.getClientId(facesContext) +
UIDataScroller.SCROLLER_STATE_ATTRIBUTE;
+ Map<String, Object> attributes = dataTable.getAttributes();
+ Object pageValue = attributes.get(stateKey);
+
+ boolean valid = true;
+
+ if(pageValue == null) {
+
+ for (UIDataScroller datascroller : dataScrollers) {
+ Object nextPageValue = null;
+
+ if (datascroller.isLocalPageSet()) {
+ nextPageValue = datascroller.getPage();
+ attributes.put(stateKey, nextPageValue);
+ datascroller.resetLocalPage();
+ } else {
+ nextPageValue = datascroller.getValueExpression("page");
+ }
+
+ if (!values.isEmpty() && !same(values.get(values.size() - 1),
nextPageValue)) {
+ valid = false;
+ }
+
+ values.add(nextPageValue);
+
+ if (nextPageValue != null) {
+ activeComponent = datascroller;
+ }
+ }
+
+ }
+
+ if(activeComponent == null) {
+ activeComponent = dataScrollers.get(dataScrollers.size()-1);
+ }
+
+ if (!valid) {
+ //TODO: anton - test debug
+ String formattedMessage = getPageDifferentMessage(facesContext,
activeComponent, dataScrollers, values);
+ log.error(formattedMessage);
+ }
+
+ return activeComponent;
+ }
+
+ @Override
+ public void processEvent(SystemEvent event) throws AbortProcessingException {
+ UIComponent dataTable = (UIComponent)event.getSource();
+ List<UIDataScroller> dataScrollers =
DataScrollerUtils.findDataScrollers(dataTable);
+
+ if(!dataScrollers.isEmpty()) {
+
+ FacesContext facesContext = FacesContext.getCurrentInstance();
+
+ UIDataScroller activeComponent = processActiveDatascroller(facesContext,
dataScrollers, dataTable);
+
+ int rowCount = DataScrollerUtils.getRowCount(dataTable);
+ int rows = DataScrollerUtils.getRows(dataTable);
+
+ Integer pageCount = DataScrollerUtils.getPageCount(dataTable, rowCount,
rows);
+
+ int page = activeComponent.getPage();
+ int newPage = -1;
+
+ if (page < 1) {
+ newPage = 1;
+ } else if (page > pageCount) {
+ newPage = (pageCount != 0 ? pageCount : 1);
+ }
+
+ if (newPage != -1) {
+ Object label = MessageUtil.getLabel(facesContext, activeComponent);
+ String formattedMessage =
Messages.getMessage(Messages.DATASCROLLER_PAGE_MISSING, new Object[] {label, page,
pageCount, newPage});
+ //TODO: anton - test debug
+ log.warn(formattedMessage);
+
+ page = newPage;
+ dataTable.getAttributes().put(dataTable.getClientId(facesContext) +
UIDataScroller.SCROLLER_STATE_ATTRIBUTE, page);
+ }
+
+
+ int first;
+
+ String lastPageMode = activeComponent.getLastPageMode();
+
+ if (lastPageMode == null) {
+ lastPageMode = PageMode.part.toString();
+ } else if (!PageMode.part.equals(lastPageMode) &&
!PageMode.full.equals(lastPageMode)) {
+ throw new IllegalArgumentException("Illegal value of
'lastPageMode' attribute: '" + lastPageMode + "'");
+ }
+
+ if (page != pageCount || PageMode.part.equals(lastPageMode)) {
+ first = (page - 1) * rows;
+ } else {
+ first = rowCount - rows;
+ if (first < 0) {
+ first = 0;
+ }
+ }
+ dataTable.getAttributes().put("first", first);
+ }
+ }
+
+ private String getPageDifferentMessage(FacesContext facesContext, UIDataScroller
activeComponent, List<UIDataScroller> dataScrollers, List<Object> values) {
+ StringBuilder builder = new StringBuilder("\n[");
+ Iterator<UIDataScroller> scrollerItr = dataScrollers.iterator();
+ Iterator<Object> valueItr = values.iterator();
+
+ while (scrollerItr.hasNext()) {
+ UIDataScroller next = scrollerItr.next();
+ builder.append(MessageUtil.getLabel(facesContext, next));
+ builder.append(": ");
+
+ Object value = valueItr.next();
+ if (value instanceof ValueExpression) {
+ builder.append(((ValueExpression) value).getExpressionString());
+ } else {
+ builder.append(value);
+ }
+
+ builder.append(scrollerItr.hasNext() ? ",\n" : "]");
+ }
+
+ return Messages.getMessage(Messages.DATASCROLLER_PAGES_DIFFERENT, new Object[]
{MessageUtil.getLabel(facesContext, activeComponent), builder});
+
+ }
+
+ private static boolean same(Object o1, Object o2) {
+ if (o1 instanceof ValueExpression && o2 instanceof ValueExpression) {
+ ValueExpression ve1 = (ValueExpression) o1;
+ ValueExpression ve2 = (ValueExpression) o2;
+
+ if (same(ve1.getExpressionString(), ve2.getExpressionString()) &&
same(ve1.getExpectedType(), ve2.getExpectedType())) {
+ return true;
+ }
+ }
+
+ return (o1 != null && o1.equals(o2)) || (o1 == null && o2 ==
null);
+ }
+}
Show replies by date