Author: abelevich
Date: 2010-03-31 11:20:50 -0400 (Wed, 31 Mar 2010)
New Revision: 16694
Modified:
root/ui-sandbox/trunk/components/datascroller/ui/src/main/java/org/richfaces/component/UIDataScroller.java
Log:
add setter/getter for the lastPageMode, move methods getRows, getRowCount to the util
class
Modified:
root/ui-sandbox/trunk/components/datascroller/ui/src/main/java/org/richfaces/component/UIDataScroller.java
===================================================================
---
root/ui-sandbox/trunk/components/datascroller/ui/src/main/java/org/richfaces/component/UIDataScroller.java 2010-03-31
15:19:29 UTC (rev 16693)
+++
root/ui-sandbox/trunk/components/datascroller/ui/src/main/java/org/richfaces/component/UIDataScroller.java 2010-03-31
15:20:50 UTC (rev 16694)
@@ -1,5 +1,6 @@
package org.richfaces.component;
+import java.util.List;
import java.util.Map;
import javax.el.ELException;
@@ -13,19 +14,17 @@
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
-import javax.faces.event.ComponentSystemEvent;
import javax.faces.event.FacesEvent;
-import org.ajax4jsf.Messages;
import org.ajax4jsf.component.IterationStateHolder;
import org.ajax4jsf.renderkit.RendererUtils;
+import org.richfaces.DataScrollerUtils;
import org.richfaces.component.util.MessageUtil;
import org.richfaces.event.DataScrollerEvent;
import org.richfaces.event.DataScrollerListener;
import org.richfaces.event.DataScrollerSource;
-import org.richfaces.log.RichfacesLogger;
-import org.slf4j.Logger;
+
public class UIDataScroller extends UIComponentBase implements DataScrollerSource,
IterationStateHolder {
public static final String COMPONENT_TYPE = "org.richfaces.DataScroller";
@@ -34,10 +33,6 @@
public static final String SCROLLER_STATE_ATTRIBUTE = COMPONENT_TYPE +
":page";
- private static final Logger log = RichfacesLogger.COMPONENTS.getLogger();
-
- private Integer page;
-
public static final String FIRST_FACET_NAME = "first";
public static final String LAST_FACET_NAME = "last";
@@ -49,18 +44,27 @@
public static final String FIRST_DISABLED_FACET_NAME = "first_disabled";
public static final String LAST_DISABLED_FACET_NAME = "last_disabled";
-
+ private Integer page;
+
protected enum PropertyKeys {
boundaryControls, data, fastControls, fastStep, forComponent, handleValue,
inactiveStyle, selectStyle,
inactiveStyleClass, selectStyleClass, scrollerListener, lastPageMode, maxPages,
pageIndexVar, pagesVar, renderIfSinglePage,
style, styleClass, stepControls
}
- protected enum PageMode {
- full, part //part - prev short
+ public enum PageMode {
+ full, part
}
+ public String getLastPageMode() {
+ return (String)getStateHelper().eval(PropertyKeys.lastPageMode);
+ }
+
+ public void setLastPageMode(String lastPageMode) {
+ getStateHelper().put(PropertyKeys.lastPageMode, lastPageMode);
+ }
+
public void addScrollerListener(DataScrollerListener listener) {
addFacesListener(listener);
}
@@ -89,9 +93,13 @@
scrollerListener.invoke(facesContext.getELContext(), new Object[]{event});
}
- facesContext.getPartialViewContext().getRenderIds().add(dataScrollerEvent.getComponent().getClientId(facesContext));
+ UIComponent dataTable = getDataTable();
- UIComponent dataTable = getDataTable();
+ List <UIDataScroller> dataScrollers =
DataScrollerUtils.findDataScrollers(dataTable);
+ for(UIDataScroller dataScroller: dataScrollers) {
+
facesContext.getPartialViewContext().getRenderIds().add(dataScroller.getClientId(facesContext));
+ }
+
String dataTableId = dataTable.getClientId(facesContext);
if(dataTable instanceof PartiallyEncodedComponent) {
dataTableId += "@body";
@@ -150,8 +158,7 @@
int newPage = 1;
int pageCount = getPageCount();
-
-
+
if (FIRST_FACET_NAME.equals(facetName)) {
newPage = 1;
} else if (LAST_FACET_NAME.equals(facetName)) {
@@ -182,20 +189,7 @@
}
public int getPageCount(UIComponent data, int rowCount, int rows) {
- int pageCount;
- if (rows > 0) {
- pageCount = rows <= 0 ? 1 : rowCount / rows;
- if (rowCount % rows > 0) {
- pageCount++;
- }
- if (pageCount == 0) {
- pageCount = 1;
- }
- } else {
- rows = 1;
- pageCount = 1;
- }
- return pageCount;
+ return DataScrollerUtils.getPageCount(data, rowCount, rows);
}
/** @return the page count of the uidata */
@@ -204,33 +198,16 @@
}
public int getRowCount(UIComponent data) {
- int rowCount = (Integer)data.getAttributes().get("rowCount");
- if (rowCount >= 0) {
- return rowCount;
- }
-
- return BinarySearch.search(data);
+ return (Integer)data.getAttributes().get("rowCount");
}
/** @return int */
public int getRowCount() {
- //xxx nick -> alex - scrollable models can return -1 here
- //let's implement "dychotomic" discovery
- // setPage(1)... if isPageAvailable() setPage(2) then 4, 8, etc.
- // setPage() { setRowIndex(pageIdx * rows); }
- // isPageAvailable() { return isRowAvailable() }
- //return getUIData().getRowCount();
return getRowCount(getDataTable());
}
public int getRows(UIComponent data) {
- int row = 0;
- row = (Integer)data.getAttributes().get("rows");
- if (row == 0) {
- row = getRowCount(data);
- }
-
- return row;
+ return DataScrollerUtils.getRows(data);
}
// facet getter methods
@@ -265,67 +242,15 @@
return true;
}
-
- public void setupFirstRowValue() {
- UIComponent dataTable = getDataTable();
- if (isRendered()) {
- int rowCount = getRowCount(dataTable);
- int rows = getRows(dataTable);
-
- Integer pageCount = getPageCount(dataTable, rowCount, rows);
-
- int page = getPage();
- int newPage = -1;
- if (page < 1) {
- newPage = 1;
- } else if (page > pageCount) {
- newPage = (pageCount != 0 ? pageCount : 1);
- }
-
- if (newPage != -1) {
- FacesContext facesContext = getFacesContext();
- Object label = MessageUtil.getLabel(facesContext, this);
- String formattedMessage = Messages.getMessage(Messages.DATASCROLLER_PAGE_MISSING,
- new Object[] {label, page, pageCount, newPage});
-
- log.warn(formattedMessage);
-
- page = newPage;
- dataTable.getAttributes().put(dataTable.getClientId(facesContext)+
SCROLLER_STATE_ATTRIBUTE, page);
- }
-
- if (isRendered(dataTable)) {
- int first;
-
- String lastPageMode = (String)getStateHelper().get(PropertyKeys.lastPageMode);
-
- 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);
- }
- }
- }
-
+
public void setPage(int newPage) {
this.page = newPage;
}
public int getPage() {
+
UIComponent dataTable = getDataTable();
- Map<String, Object> attributes = getDataTable().getAttributes();
+ Map<String, Object> attributes = dataTable.getAttributes();
FacesContext facesContext = getFacesContext();
Integer state = (Integer) attributes.get(dataTable.getClientId(facesContext)+
SCROLLER_STATE_ATTRIBUTE);
@@ -336,12 +261,12 @@
if (this.page != null) {
return page;
- }
-
+ }
+
ValueExpression ve = getValueExpression("page");
if (ve != null) {
try {
- Integer pageObject = (Integer) ve.getValue(facesContext.getELContext());
+ Integer pageObject = (Integer) ve.getValue(getFacesContext().getELContext());
if (pageObject != null) {
return pageObject;
@@ -413,55 +338,6 @@
}
}
- static class BinarySearch {
-
- public static int search(UIComponent data) {
- int rowIndex = (Integer)data.getAttributes().get("rowIndex");
- try {
- int n = 1;
- int k = 2;
- for (; ;) {
- data.getAttributes().put("rowIndex", k-1);
- if ((Boolean)data.getAttributes().get("rowAvailable")) {
- n = k;
- k = k * 2;
- } else {
- break;
- }
- }
-
- while (n < k) {
- int kk = Math.round((n + k) / 2) + 1;
- data.getAttributes().put("rowIndex", kk-1);
- if ((Boolean)data.getAttributes().get("rowAvailable")) {
- n = kk;
- } else {
- k = kk - 1;
- }
- }
-
- data.getAttributes().put("rowIndex", k-1);
- if ((Boolean)data.getAttributes().get("rowAvailable")) {
- return k;
- } else {
- return 0;
- }
- } finally {
- data.getAttributes().put("rowIndex",rowIndex);
- }
- }
- }
-
- public Object saveState(FacesContext facesContext) {
- return new Object[] { super.saveState(facesContext), page };
- }
-
- public void restoreState(FacesContext facesContext, Object object) {
- Object[] state = (Object[]) object;
- super.restoreState(facesContext, state[0]);
- page = (Integer) state[1];
- }
-
public boolean isLocalPageSet() {
return page != null;
}
@@ -475,28 +351,19 @@
return COMPONENT_FAMILY;
}
- @Override
- public void processEvent(ComponentSystemEvent event) throws AbortProcessingException {
- super.processEvent(event);
- }
-
- @Override
- public Object getIterationState() {
- FacesContext facesContext = getFacesContext();
- UIComponent dataTable = getDataTable();
- Integer page =
(Integer)dataTable.getAttributes().get(dataTable.getClientId(facesContext) +
SCROLLER_STATE_ATTRIBUTE);
- Object [] stateObject = page != null ? new Object[]{page} : null;
- return stateObject;
+ @Override
+ public Object getIterationState(FacesContext context) {
+ return new Object[] {this.page};
}
@Override
- public void setIterationState(Object stateObject) {
- if (stateObject != null) {
- Object[] state = (Object[]) stateObject;
- FacesContext facesContext = getFacesContext();
- UIComponent dataTable = getDataTable();
- dataTable.getAttributes().put(dataTable.getClientId(facesContext) +
SCROLLER_STATE_ATTRIBUTE, state[0]);
- }
+ public void setIterationState(FacesContext context, Object state) {
+ if(state != null) {
+ Object [] stateObject = (Object[])state;
+ this.page = (Integer)stateObject[0];
+ } else {
+ this.page = null;
+ }
}
}