📄 scrollercomponent.java
字号:
case ACTION_NEXT:
facetName = "next";
linkText = "Next";
break;
case ACTION_PREVIOUS:
facetName = "previous";
linkText = "Previous";
break;
default:
facetName = "number";
linkText = "" + navActionType;
isPageNumber = true;
// heuristic: if navActionType is number, and we are not
// enabled, this must be the current page.
if (!enabled) {
facetName = "current";
isCurrentPage = true;
}
break;
}
// leverage any navigation facets we have
writer.write("\n ");
if (enabled) {
writer.write("<a " + getAnchorAttrs(context, clientId,
navActionType) + ">");
}
facet = getFacet(facetName);
// render the facet pertaining to this widget type in the NORTH
// and WEST cases.
if (facet != null) {
// If we're rendering a "go to the Nth page" link
if (isPageNumber) {
// See if the user specified an orientation
String facetO = (String) getAttributes().get(
FACET_MARKUP_ORIENTATION_ATTR);
if (facet != null) {
facetOrientation = facetO;
// verify that the orientation is valid
if (!(facetOrientation.equalsIgnoreCase(NORTH) ||
facetOrientation.equalsIgnoreCase(SOUTH) ||
facetOrientation.equalsIgnoreCase(EAST) ||
facetOrientation.equalsIgnoreCase(WEST))) {
facetOrientation = NORTH;
}
}
}
// output the facet as specified in facetOrientation
if (facetOrientation.equalsIgnoreCase(NORTH) ||
facetOrientation.equalsIgnoreCase(EAST)) {
facet.encodeBegin(context);
if (facet.getRendersChildren()) {
facet.encodeChildren(context);
}
facet.encodeEnd(context);
}
// The difference between NORTH and EAST is that NORTH
// requires a <br>.
if (facetOrientation.equalsIgnoreCase(NORTH)) {
writer.startElement("br", null); // PENDING(craigmcc)
writer.endElement("br");
}
}
// if we have a facet, only output the link text if
// navActionType is number
if (null != facet) {
if (navActionType != ACTION_NEXT &&
navActionType != ACTION_PREVIOUS) {
writer.write(linkText);
}
} else {
writer.write(linkText);
}
// output the facet in the EAST and SOUTH cases
if (null != facet) {
if (facetOrientation.equalsIgnoreCase(SOUTH)) {
writer.startElement("br", null); // PENDING(craigmcc)
writer.endElement("br");
}
// The difference between SOUTH and WEST is that SOUTH
// requires a <br>.
if (facetOrientation.equalsIgnoreCase(SOUTH) ||
facetOrientation.equalsIgnoreCase(WEST)) {
facet.encodeBegin(context);
if (facet.getRendersChildren()) {
facet.encodeChildren(context);
}
facet.encodeEnd(context);
}
}
if (enabled) {
writer.write("</a>");
}
}
/**
* <p>Build and return the string consisting of the attibutes for a
* result set navigation link anchor.</p>
*
* @param context the FacesContext
* @param clientId the clientId of the enclosing UIComponent
* @param action the value for the rhs of the =
*
* @return a String suitable for setting as the value of a navigation
* href.
*/
private String getAnchorAttrs(FacesContext context, String clientId,
int action) {
int currentPage = 1;
int formNumber = getFormNumber(context);
Integer curPage = (Integer) getAttributes().get("currentPage");
if (curPage != null) {
currentPage = curPage.intValue();
}
String result =
"href=\"#\" " +
"onmousedown=\"" +
"document.forms[" + formNumber + "]['" + clientId +
"_action'].value='" +
action +
"'; " +
"document.forms[" + formNumber + "]['" + clientId +
"_curPage'].value='" +
currentPage +
"'; " +
"document.forms[" + formNumber + "].submit()\"";
return result;
}
private String getHiddenFields(String clientId) {
String result =
"<input type=\"hidden\" name=\"" + clientId + "_action\"/>\n" +
"<input type=\"hidden\" name=\"" + clientId + "_curPage\"/>";
return result;
}
// PENDING: avoid doing this each time called. Perhaps
// store in our own attr?
protected UIForm getForm(FacesContext context) {
UIComponent parent = this.getParent();
while (parent != null) {
if (parent instanceof UIForm) {
break;
}
parent = parent.getParent();
}
return (UIForm) parent;
}
protected int getFormNumber(FacesContext context) {
Map requestMap = context.getExternalContext().getRequestMap();
int numForms = 0;
Integer formsInt = null;
// find out the current number of forms in the page.
if (null != (formsInt = (Integer)
requestMap.get(FORM_NUMBER_ATTR))) {
numForms = formsInt.intValue();
// since the form index in the document starts from 0.
numForms--;
}
return numForms;
}
/**
* Returns the total number of pages in the result set based on
* <code>rows</code> and <code>rowCount</code> of <code>UIData</code>
* component that this scroller is associated with.
* For the purposes of this demo, we are assuming the <code>UIData</code> to
* be child of <code>UIForm</code> component and not nested inside a custom
* NamingContainer.
*/
protected int getTotalPages(FacesContext context) {
String forValue = (String) getAttributes().get("for");
UIData uiData = (UIData) getForm(context).findComponent(forValue);
if (uiData == null) {
return 0;
}
int rowsPerPage = uiData.getRows();
int totalRows = 0;
int result = 0;
totalRows = uiData.getRowCount();
if (rowsPerPage > 0){
result = totalRows / rowsPerPage;
if (0 != (totalRows % rowsPerPage)) {
result++;
}
} else
result = 1;
return result;
}
/**
* Returns the number of rows to display by looking up the
* <code>UIData</code> component that this scroller is associated with.
* For the purposes of this demo, we are assuming the <code>UIData</code> to
* be child of <code>UIForm</code> component and not nested inside a custom
* NamingContainer.
*/
protected int getRowsPerPage(FacesContext context) {
String forValue = (String) getAttributes().get("for");
UIData uiData = (UIData) getForm(context).findComponent(forValue);
if (uiData == null) {
return 0;
}
return uiData.getRows();
}
public static MethodBinding createConstantMethodBinding(String outcome) {
return new ConstantMethodBinding(outcome);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -