Author: nbelaevski
Date: 2008-03-18 14:07:23 -0400 (Tue, 18 Mar 2008)
New Revision: 6929
Modified:
trunk/ui/datascroller/src/main/java/org/richfaces/component/DataScrollerViewPhaseListener.java
trunk/ui/datascroller/src/main/java/org/richfaces/component/UIDatascroller.java
trunk/ui/datascroller/src/main/java/org/richfaces/renderkit/html/DataScrollerRenderer.java
trunk/ui/datascroller/src/main/resources/org/richfaces/renderkit/html/scripts/datascroller.js
trunk/ui/datascroller/src/main/templates/org/richfaces/htmlDatascroller.jspx
trunk/ui/datascroller/src/test/java/org/richfaces/component/DatascrollerComponentTest.java
Log:
http://jira.jboss.com/jira/browse/RF-2182
Modified:
trunk/ui/datascroller/src/main/java/org/richfaces/component/DataScrollerViewPhaseListener.java
===================================================================
---
trunk/ui/datascroller/src/main/java/org/richfaces/component/DataScrollerViewPhaseListener.java 2008-03-18
18:07:09 UTC (rev 6928)
+++
trunk/ui/datascroller/src/main/java/org/richfaces/component/DataScrollerViewPhaseListener.java 2008-03-18
18:07:23 UTC (rev 6929)
@@ -24,7 +24,6 @@
import java.util.Iterator;
import javax.faces.component.UIComponent;
-import javax.faces.component.UIData;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
@@ -49,21 +48,17 @@
}
private void updateScrollers(UIComponent component) {
- if (component.isRendered()) {
- if (component instanceof UIDatascroller) {
- UIDatascroller datascroller = (UIDatascroller) component;
-
- UIData dataTable = datascroller.getDataTable();
- if (dataTable.isRendered()) {
- dataTable.setFirst(datascroller.setupFirstRowValue());
- }
- }
-
- Iterator<UIComponent> children = component.getFacetsAndChildren();
- while(children.hasNext()) {
- updateScrollers(children.next());
- }
+ if (component.isRendered()) {
+ if (component instanceof UIDatascroller) {
+ UIDatascroller datascroller = (UIDatascroller) component;
+ datascroller.setupFirstRowValue();
}
+
+ Iterator<UIComponent> children = component.getFacetsAndChildren();
+ while(children.hasNext()) {
+ updateScrollers(children.next());
+ }
+ }
}
public void beforePhase(PhaseEvent event) {
Modified: trunk/ui/datascroller/src/main/java/org/richfaces/component/UIDatascroller.java
===================================================================
---
trunk/ui/datascroller/src/main/java/org/richfaces/component/UIDatascroller.java 2008-03-18
18:07:09 UTC (rev 6928)
+++
trunk/ui/datascroller/src/main/java/org/richfaces/component/UIDatascroller.java 2008-03-18
18:07:23 UTC (rev 6929)
@@ -50,7 +50,7 @@
public abstract class UIDatascroller extends AjaxActionComponent
implements DataScrollerSource, ActionSource {
- private Integer pageNumber;
+ private Integer page;
public static final String COMPONENT_TYPE = "org.richfaces.Datascroller";
public static final String COMPONENT_FAMILY =
"org.richfaces.Datascroller";
@@ -225,26 +225,6 @@
return (UIData) forComp;
}
- /**
- * Gets the index of the current page
- *
- * @param data
- * @param pageCount
- * @return the page index
- */
- public int getPageIndex(UIData data, int pageCount) {
- int page = getPage();
- if (page < 0) {
- if (pageCount == 0) {
- page = 1;
- } else {
- page = pageCount + page + 1;
- }
- }
-
- return page;
- }
-
private int getFastStepOrDefault() {
int step = getFastStep();
if (step <= 0) {
@@ -254,54 +234,57 @@
}
}
- public int getPageForFacet(String facetName) {
- UIData dataTable = getDataTable();
- int pageCount = getPageCount(dataTable);
- int pageIndex = getPageIndex(dataTable, pageCount);
-
- return getPageForFacet(facetName, pageIndex, pageCount);
+ /**
+ * @param data
+ * @return
+ * @see #getPage()
+ * @deprecated
+ */
+ public int getPageIndex(UIData data) {
+ return getPageIndex();
}
- public int getPageForFacet(String facetName, int pageIndex, int pageCount) {
+ /**
+ * @return
+ * @see #getPage()
+ * @deprecated
+ */
+ public int getPageIndex() {
+ return getPageIndex();
+ }
+
+ public int getPageForFacet(String facetName) {
+ if (facetName == null) {
+ throw new NullPointerException();
+ }
+
int newPage = 1;
+ int pageCount = getPageCount();
if (FIRST_FACET_NAME.equals(facetName)) {
newPage = 1;
} else if (PREVIOUS_FACET_NAME.equals(facetName)) {
- newPage = pageIndex - 1;
+ newPage = getPage() - 1;
} else if (NEXT_FACET_NAME.equals(facetName)) {
- newPage = pageIndex + 1;
+ newPage = getPage() + 1;
} else if (LAST_FACET_NAME.equals(facetName)) {
newPage = pageCount > 0 ? pageCount : 1;
} else if (FAST_FORWARD_FACET_NAME.equals(facetName)) {
- newPage = pageIndex + getFastStepOrDefault();
+ newPage = getPage() + getFastStepOrDefault();
} else if (FAST_REWIND_FACET_NAME.equals(facetName)) {
- newPage = pageIndex - getFastStepOrDefault();
+ newPage = getPage() - getFastStepOrDefault();
} else {
- if (facetName != null) {
- try {
- newPage = Integer.parseInt(facetName.toString());
- } catch (NumberFormatException e) {
- throw new FacesException(e.getLocalizedMessage(), e);
- }
+ try {
+ newPage = Integer.parseInt(facetName.toString());
+ } catch (NumberFormatException e) {
+ throw new FacesException(e.getLocalizedMessage(), e);
}
-
- if (newPage < 0) {
- if (pageCount == 0) {
- newPage = 1;
- } else {
- //newPage = -1 should scroll to the last one
- newPage = pageCount + newPage + 1;
- }
- }
}
if (newPage >= 1 && newPage <= pageCount) {
return newPage;
- } else if (newPage == 0) {
- return 1;
} else {
- return -1;
+ return 0;
}
}
@@ -319,7 +302,7 @@
*/
public void setPage(String facetName) {
int newPage = getPageForFacet(facetName);
- if (newPage > 0) {
+ if (newPage != 0) {
setPage(newPage);
}
}
@@ -379,6 +362,25 @@
return row;
}
+ /**
+ * @param data
+ * @return
+ * @see #getPage()
+ * @deprecated
+ */
+ public int getFirstRow(UIData data) {
+ return data.getFirst();
+ }
+
+ /**
+ * @param rows
+ * @see #setPage(int)
+ * @deprecated
+ */
+ public void setFirstRow(int rows) {
+ getDataTable().setFirst(rows);
+ }
+
// facet getter methods
public UIComponent getFirst() {
return getFacet(FIRST_FACET_NAME);
@@ -404,52 +406,33 @@
return getFacet(PREVIOUS_FACET_NAME);
}
- private transient Integer currentPageIndex;
- private transient Integer currentPageCount;
-
- public Integer getCurrentPageCount(UIData dataTable) {
- if (currentPageCount == null) {
- currentPageCount = getPageCount(dataTable);
- }
-
- return currentPageCount;
- }
-
- public Integer getCurrentPageIndex(UIData dataTable) {
- if (currentPageIndex == null) {
- int pageCount = getCurrentPageCount(dataTable);
- int pageIndex = getPageIndex(dataTable, pageCount);
-
- if (pageCount < 1) {
- pageCount = 1;
+ public void setupFirstRowValue() {
+ UIData dataTable = getDataTable();
+ if (isRendered()) {
+ Integer pageCount = getPageCount(dataTable);
+
+ int page = getPage();
+ if (page < 1) {
+ page = 1;
+ setPage(page);
+ } else if (page > pageCount) {
+ page = pageCount != 0 ? pageCount : 1;
+ setPage(page);
}
- if (pageIndex < 1) {
- pageIndex = 1;
- setPage(pageIndex);
- } else if (pageIndex > pageCount) {
- pageIndex = pageCount;
- setPage(pageIndex);
+ if (dataTable.isRendered()) {
+ dataTable.setFirst((page - 1) * getRows(dataTable));
}
-
- currentPageIndex = pageIndex;
}
-
- return currentPageIndex;
}
-
- public int setupFirstRowValue() {
- UIData dataTable = getDataTable();
- return (getCurrentPageIndex(dataTable) - 1) * getRows(dataTable);
- }
public void setPage(int newPage) {
- this.pageNumber = newPage;
+ this.page = newPage;
}
public int getPage() {
- if (this.pageNumber != null) {
- return pageNumber;
+ if (this.page != null) {
+ return page;
}
ValueExpression ve = getValueExpression("page");
@@ -471,13 +454,13 @@
}
private void updateModel(int newPage) {
- this.pageNumber = newPage;
+ this.page = newPage;
FacesContext context = getFacesContext();
ValueExpression ve = getValueExpression("page");
if (ve != null) {
try {
- ve.setValue(context.getELContext(), this.pageNumber);
- this.pageNumber = null;
+ ve.setValue(context.getELContext(), this.page);
+ this.page = null;
} catch (ELException e) {
String messageStr = e.getMessage();
Throwable result = e.getCause();
@@ -566,7 +549,7 @@
public Object saveState(FacesContext context) {
return new Object[] {
super.saveState(context),
- pageNumber
+ page
};
}
@@ -575,7 +558,7 @@
Object[] state = (Object[]) object;
super.restoreState(context, state[0]);
- pageNumber = (Integer) state[1];
+ page = (Integer) state[1];
}
}
Modified:
trunk/ui/datascroller/src/main/java/org/richfaces/renderkit/html/DataScrollerRenderer.java
===================================================================
---
trunk/ui/datascroller/src/main/java/org/richfaces/renderkit/html/DataScrollerRenderer.java 2008-03-18
18:07:09 UTC (rev 6928)
+++
trunk/ui/datascroller/src/main/java/org/richfaces/renderkit/html/DataScrollerRenderer.java 2008-03-18
18:07:23 UTC (rev 6929)
@@ -28,7 +28,6 @@
import java.util.Map;
import javax.faces.component.UIComponent;
-import javax.faces.component.UIData;
import javax.faces.component.UIParameter;
import javax.faces.context.FacesContext;
import javax.faces.context.ResponseWriter;
@@ -36,6 +35,7 @@
import org.ajax4jsf.javascript.JSFunction;
import org.ajax4jsf.javascript.JSFunctionDefinition;
+import org.ajax4jsf.javascript.JSReference;
import org.ajax4jsf.renderkit.AjaxRendererUtils;
import org.ajax4jsf.renderkit.HeaderResourcesRendererBase;
import org.richfaces.component.UIDatascroller;
@@ -54,24 +54,18 @@
String param = (String) paramMap.get(clientId);
if (param != null) {
UIDatascroller scroller = (UIDatascroller) component;
- UIData data = scroller.getDataTable();
- int pageCount = scroller.getPageCount(data);
-
- if (pageCount > 1) {
- int pageIndex = scroller.getPageIndex(data, pageCount);
- int newPage = scroller.getPageForFacet(param, pageIndex, pageCount);
- int page = scroller.getPage();
- if (newPage > 0 && newPage != page) {
- DataScrollerEvent event = new DataScrollerEvent(scroller,
- String.valueOf(page), param, newPage);
- if (scroller.isImmediate()) {
- event.setPhaseId(PhaseId.APPLY_REQUEST_VALUES);
- } else {
- event.setPhaseId(PhaseId.INVOKE_APPLICATION);
- }
+ int newPage = scroller.getPageForFacet(param);
+ int page = scroller.getPage();
+ if (newPage != 0 && newPage != page) {
+ DataScrollerEvent event = new DataScrollerEvent(scroller,
+ String.valueOf(page), param, newPage);
+ if (scroller.isImmediate()) {
+ event.setPhaseId(PhaseId.APPLY_REQUEST_VALUES);
+ } else {
+ event.setPhaseId(PhaseId.INVOKE_APPLICATION);
+ }
- component.queueEvent(event);
- }
+ component.queueEvent(event);
}
}
}
@@ -282,11 +276,10 @@
public String getSubmitFunction(FacesContext context, UIComponent component) {
- JSFunctionDefinition definition = new JSFunctionDefinition("event");
+ JSFunctionDefinition definition = new JSFunctionDefinition("event",
"value");
JSFunction function = AjaxRendererUtils.buildAjaxFunction(component,
context);
- UIDatascroller scroller = (UIDatascroller) component;
Map eventOptions = AjaxRendererUtils.buildEventOptions(context,
component);
Map parameters = (Map) eventOptions.get("parameters");
@@ -296,6 +289,8 @@
parameters.putAll(params);
}
+ parameters.put(component.getClientId(context), new
JSReference("value"));
+
function.addParameter(eventOptions);
StringBuffer buffer = new StringBuffer();
function.appendScript(buffer);
Modified:
trunk/ui/datascroller/src/main/resources/org/richfaces/renderkit/html/scripts/datascroller.js
===================================================================
---
trunk/ui/datascroller/src/main/resources/org/richfaces/renderkit/html/scripts/datascroller.js 2008-03-18
18:07:09 UTC (rev 6928)
+++
trunk/ui/datascroller/src/main/resources/org/richfaces/renderkit/html/scripts/datascroller.js 2008-03-18
18:07:23 UTC (rev 6929)
@@ -18,20 +18,5 @@
e = e.parentNode;
}
- var id = e.id;
- var inputId = id + "PageIndex";
-
- var input = e.lastChild;
- while (!input.id || input.id != inputId) {
- input = input.previousSibling;
- }
-
- var value = input.value;
-
- input.value = newPage;
- try {
- e.submitFunction(event);
- } finally {
- input.value = value;
- }
+ e.submitFunction(event, newPage);
};
\ No newline at end of file
Modified: trunk/ui/datascroller/src/main/templates/org/richfaces/htmlDatascroller.jspx
===================================================================
---
trunk/ui/datascroller/src/main/templates/org/richfaces/htmlDatascroller.jspx 2008-03-18
18:07:09 UTC (rev 6928)
+++
trunk/ui/datascroller/src/main/templates/org/richfaces/htmlDatascroller.jspx 2008-03-18
18:07:23 UTC (rev 6929)
@@ -23,19 +23,16 @@
</jsp:scriptlet>
<c:object var="singlePageRenderStyle" type="java.lang.String"
/>
- <c:object var="pageIndex" type="int" />
<jsp:scriptlet>
<![CDATA[
- javax.faces.component.UIData dataTable = component.getDataTable();
-
int maxPages = component.getMaxPages();
if (maxPages <= 1) {
maxPages = 1;
}
- int pageCount = component.getCurrentPageCount(dataTable);
- pageIndex = component.getCurrentPageIndex(dataTable);
+ int pageCount = component.getPageCount();
+ int pageIndex = component.getPage();
boolean singlePageRender = true;
@@ -356,6 +353,5 @@
<script type="text/javascript">
RichFaces.Datascroller.initialize('#{clientId}',
#{this:getSubmitFunction(context,component)});
</script>
- <input type="hidden" name="#{clientId}"
id="#{clientId}PageIndex" value="#{pageIndex}" />
</div>
</f:root>
\ No newline at end of file
Modified:
trunk/ui/datascroller/src/test/java/org/richfaces/component/DatascrollerComponentTest.java
===================================================================
---
trunk/ui/datascroller/src/test/java/org/richfaces/component/DatascrollerComponentTest.java 2008-03-18
18:07:09 UTC (rev 6928)
+++
trunk/ui/datascroller/src/test/java/org/richfaces/component/DatascrollerComponentTest.java 2008-03-18
18:07:23 UTC (rev 6929)
@@ -426,31 +426,56 @@
assertEquals(4,scroller.getPageCount());
scroller.setPage("2");
scroller.setFastStep(2);
- assertEquals(5,scroller.setupFirstRowValue());
+ scroller.setupFirstRowValue();
+ assertEquals(5, scroller.getFirstRow(data));
+
scroller.setPage("next");
- assertEquals(10,scroller.setupFirstRowValue());
+ scroller.setupFirstRowValue();
+ assertEquals(10,scroller.getFirstRow(data));
+
scroller.setPage("previous");
- assertEquals(5,scroller.setupFirstRowValue());
+ scroller.setupFirstRowValue();
+ assertEquals(5,scroller.getFirstRow(data));
+
scroller.setPage("fastforward");
- assertEquals(15,scroller.setupFirstRowValue());
+ scroller.setupFirstRowValue();
+ assertEquals(15,scroller.getFirstRow(data));
+
scroller.setPage("fastrewind");
- assertEquals(5,scroller.setupFirstRowValue());
+ scroller.setupFirstRowValue();
+ assertEquals(5,scroller.getFirstRow(data));
+
scroller.setPage("first");
- assertEquals(0,scroller.setupFirstRowValue());
+ scroller.setupFirstRowValue();
+ assertEquals(0,scroller.getFirstRow(data));
+
scroller.setPage("previous");
- assertEquals(0,scroller.setupFirstRowValue());
+ scroller.setupFirstRowValue();
+ assertEquals(0,scroller.getFirstRow(data));
+
scroller.setPage("fastrewind");
- assertEquals(0,scroller.setupFirstRowValue());
+ scroller.setupFirstRowValue();
+ assertEquals(0,scroller.getFirstRow(data));
+
scroller.setPage("last");
- assertEquals(15,scroller.setupFirstRowValue());
+ scroller.setupFirstRowValue();
+ assertEquals(15,scroller.getFirstRow(data));
+
scroller.setPage("next");
- assertEquals(15,scroller.setupFirstRowValue());
+ scroller.setupFirstRowValue();
+ assertEquals(15,scroller.getFirstRow(data));
+
scroller.setPage("fastforward");
- assertEquals(15,scroller.setupFirstRowValue());
+ scroller.setupFirstRowValue();
+ assertEquals(15,scroller.getFirstRow(data));
+
scroller.setPage("5");
- assertEquals(15,scroller.setupFirstRowValue());
+ scroller.setupFirstRowValue();
+ assertEquals(15,scroller.getFirstRow(data));
+
scroller.setPage("0");
- assertEquals(0,scroller.setupFirstRowValue());
+ scroller.setupFirstRowValue();
+ assertEquals(15,scroller.getFirstRow(data));
}
public void testListener() throws Exception{