}
protected void renderScreenletPaginateMenu(Appendable writer, Map<String, Object> context, ModelScreenWidget.Form form) throws IOException {
HttpServletResponse response = (HttpServletResponse) context.get("response");
HttpServletRequest request = (HttpServletRequest) context.get("request");
ModelForm modelForm = form.getModelForm(context);
modelForm.runFormActions(context);
modelForm.preparePager(context);
String targetService = modelForm.getPaginateTarget(context);
if (targetService == null) {
targetService = "${targetService}";
}
// get the parametrized pagination index and size fields
int paginatorNumber = modelForm.getPaginatorNumber(context);
String viewIndexParam = modelForm.getMultiPaginateIndexField(context);
String viewSizeParam = modelForm.getMultiPaginateSizeField(context);
int viewIndex = modelForm.getViewIndex(context);
int viewSize = modelForm.getViewSize(context);
int listSize = modelForm.getListSize(context);
int lowIndex = modelForm.getLowIndex(context);
int highIndex = modelForm.getHighIndex(context);
int actualPageSize = modelForm.getActualPageSize(context);
// if this is all there seems to be (if listSize < 0, then size is unknown)
if (actualPageSize >= listSize && listSize >= 0) return;
// needed for the "Page" and "rows" labels
Map<String, String> uiLabelMap = UtilGenerics.cast(context.get("uiLabelMap"));
String ofLabel = "";
if (uiLabelMap == null) {
Debug.logWarning("Could not find uiLabelMap in context", module);
} else {
ofLabel = (String) uiLabelMap.get("CommonOf");
ofLabel = ofLabel.toLowerCase();
}
// for legacy support, the viewSizeParam is VIEW_SIZE and viewIndexParam is VIEW_INDEX when the fields are "viewSize" and "viewIndex"
if (viewIndexParam.equals("viewIndex" + "_" + paginatorNumber)) viewIndexParam = "VIEW_INDEX" + "_" + paginatorNumber;
if (viewSizeParam.equals("viewSize" + "_" + paginatorNumber)) viewSizeParam = "VIEW_SIZE" + "_" + paginatorNumber;
ServletContext ctx = (ServletContext) request.getAttribute("servletContext");
RequestHandler rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_");
Map<String, Object> inputFields = UtilGenerics.toMap(context.get("requestParameters"));
// strip out any multi form fields if the form is of type multi
if (modelForm.getType().equals("multi")) {
inputFields = UtilHttp.removeMultiFormParameters(inputFields);
}
String queryString = UtilHttp.urlEncodeArgs(inputFields);
// strip legacy viewIndex/viewSize params from the query string
queryString = UtilHttp.stripViewParamsFromQueryString(queryString, "" + paginatorNumber);
// strip parametrized index/size params from the query string
HashSet<String> paramNames = new HashSet<String>();
paramNames.add(viewIndexParam);
paramNames.add(viewSizeParam);
queryString = UtilHttp.stripNamedParamsFromQueryString(queryString, paramNames);
String anchor = "";
String paginateAnchor = modelForm.getPaginateTargetAnchor();
if (paginateAnchor != null) anchor = "#" + paginateAnchor;
// preparing the link text, so that later in the code we can reuse this and just add the viewIndex
StringBuilder prepLinkTextBuffer = new StringBuilder(targetService);
if (prepLinkTextBuffer.indexOf("?") < 0) {
prepLinkTextBuffer.append("?");
} else if (prepLinkTextBuffer.indexOf("?", prepLinkTextBuffer.length() - 1) > 0) {
prepLinkTextBuffer.append("&");
}
if (!UtilValidate.isEmpty(queryString) && !queryString.equals("null")) {
prepLinkTextBuffer.append(queryString).append("&");
}
prepLinkTextBuffer.append(viewSizeParam).append("=").append(viewSize).append("&").append(viewIndexParam).append("=");
String prepLinkText = prepLinkTextBuffer.toString();
String linkText;
appendWhitespace(writer);
// The current screenlet title bar navigation syling requires rendering
// these links in reverse order
// Last button
writer.append("<li class=\"").append(modelForm.getPaginateLastStyle());
if (highIndex < listSize) {
writer.append("\"><a href=\"");
int page = (listSize / viewSize) - 1;
linkText = prepLinkText + page + anchor;
// - make the link
writer.append(rh.makeLink(request, response, linkText));
writer.append("\">").append(modelForm.getPaginateLastLabel(context)).append("</a>");
} else {
// disabled button
writer.append(" disabled\">").append(modelForm.getPaginateLastLabel(context));
}
writer.append("</li>");
appendWhitespace(writer);
// Next button
writer.append("<li class=\"").append(modelForm.getPaginateNextStyle());
if (highIndex < listSize) {
writer.append("\"><a href=\"");
linkText = prepLinkText + (viewIndex + 1) + anchor;
// - make the link
writer.append(rh.makeLink(request, response, linkText));
writer.append("\">").append(modelForm.getPaginateNextLabel(context)).append("</a>");
} else {
// disabled button
writer.append(" disabled\">").append(modelForm.getPaginateNextLabel(context));
}
writer.append("</li>");
appendWhitespace(writer);
if (listSize > 0) {
writer.append("<li>");
writer.append(Integer.toString(lowIndex + 1)).append(" - ").append(Integer.toString(lowIndex + actualPageSize)).append(" ").append(ofLabel).append(" ").append(Integer.toString(listSize));
writer.append("</li>");
appendWhitespace(writer);
}
// Previous button
writer.append("<li class=\"nav-previous");
if (viewIndex > 0) {
writer.append("\"><a href=\"");
linkText = prepLinkText + (viewIndex - 1) + anchor;
// - make the link
writer.append(rh.makeLink(request, response, linkText));
writer.append("\">").append(modelForm.getPaginatePreviousLabel(context)).append("</a>");
} else {
// disabled button
writer.append(" disabled\">").append(modelForm.getPaginatePreviousLabel(context));
}
writer.append("</li>");
appendWhitespace(writer);
// First button
writer.append("<li class=\"nav-first");
if (viewIndex > 0) {
writer.append("\"><a href=\"");
linkText = prepLinkText + 0 + anchor;
writer.append(rh.makeLink(request, response, linkText));
writer.append("\">").append(modelForm.getPaginateFirstLabel(context)).append("</a>");
} else {
writer.append(" disabled\">").append(modelForm.getPaginateFirstLabel(context));
}
writer.append("</li>");
appendWhitespace(writer);
}