public UIData getUIData() { String forStr = getFor(); UIData forComp = (UIData) CoreComponentUtils.findComponent(forStr, this); if (forComp == null) { throw new IllegalArgumentException( "could not find UIData referenced by attribute @for = '" + forStr + "'"); } else if (!(forComp instanceof UIData)) { throw new IllegalArgumentException( "uiComponent referenced by attribute @for = '" + forStr + "' must be of type " + UIData.class.getName() + ", not type " + forComp.getClass().getName()); } // compare with cached DataModel to check for updates if (_origDataModelHash != 0 && _origDataModelHash != forComp.getValue().hashCode()) { reset(); } if (!isIgnorePagination() && ((first != Integer.MIN_VALUE && first != forComp.getFirst()) || (rows != Integer.MIN_VALUE && rows != forComp.getRows()))) { reset(); } Object value = forComp.getValue(); if (null != value) { _origDataModelHash = forComp.getValue().hashCode(); } return forComp; }
/** * 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) getUIData(context, forValue); if (uiData == null) { return 0; } return uiData.getRows(); }
@Override public void encodeChildren(FacesContext context, UIComponent component) throws IOException { rendererParamsNotNull(context, component); if (!shouldEncodeChildren(component)) { return; } UIData data = (UIData) component; ResponseWriter writer = context.getResponseWriter(); // Iterate over the rows of data that are provided int processed = 0; int rowIndex = data.getFirst() - 1; int rows = data.getRows(); List<Integer> bodyRows = getBodyRows(data); boolean hasBodyRows = (bodyRows != null && !bodyRows.isEmpty()); boolean wroteTableBody = false; if (!hasBodyRows) { renderTableBodyStart(component, writer); } while (true) { // Have we displayed the requested number of rows? if ((rows > 0) && (++processed > rows)) { break; } // Select the current row data.setRowIndex(++rowIndex); if (!data.isRowAvailable()) { break; // Scrolled past the last row } // render any table body rows if (hasBodyRows && bodyRows.contains(data.getRowIndex())) { if (wroteTableBody) { writer.endElement("tbody"); } writer.startElement("tbody", data); wroteTableBody = true; } // Render the beginning of this row renderRowStart(component, writer); // Render the row content renderRow(context, component, null, writer); // Render the ending of this row renderRowEnd(component, writer); } renderTableBodyEnd(component, writer); // Clean up after ourselves data.setRowIndex(-1); }
private void renderToHandler(OutputTypeHandler outputHandler, UIData uiData, FacesContext fc) { try { int rowIndex = 0; int numberOfRowsToDisplay = 0; if (!isIgnorePagination()) { rowIndex = uiData.getFirst(); numberOfRowsToDisplay = uiData.getRows(); first = rowIndex; rows = numberOfRowsToDisplay; } int countOfRowsDisplayed = 0; uiData.setRowIndex(rowIndex); String[] includeColumnsArray = null; String includeColumns = getIncludeColumns(); if (includeColumns != null) includeColumnsArray = includeColumns.split(","); List columns = getRenderedChildColumnsList(uiData); // System.out.println("DataExporter.renderToHandler() columns.size: " + columns.size()); // write header // System.out.println("DataExporter.renderToHandler() HEADERS"); processAllColumns(fc, outputHandler, columns, includeColumnsArray, -1, false); // System.out.println("DataExporter.renderToHandler() ROWS"); while (uiData.isRowAvailable()) { if (numberOfRowsToDisplay > 0 && countOfRowsDisplayed >= numberOfRowsToDisplay) { break; } // render the child columns; each one in a td processAllColumns( fc, outputHandler, columns, includeColumnsArray, countOfRowsDisplayed, false); // keep track of rows displayed countOfRowsDisplayed++; // maintain the row index property on the underlying UIData // component rowIndex++; uiData.setRowIndex(rowIndex); } // reset the underlying UIData component uiData.setRowIndex(-1); // write footer // System.out.println("DataExporter.renderToHandler() FOOTERS"); processAllColumns( fc, outputHandler, columns, includeColumnsArray, countOfRowsDisplayed, true); outputHandler.flushFile(); } catch (Exception e) { log.error("renderToHandler()", e); } }
/** * Scroll to the page that contains the specified row number. * * @param row Desired row number */ public void scroll(int row) { int rows = data.getRows(); if (rows < 1) { return; // Showing entire table already } if (row < 0) { data.setFirst(0); } else if (row >= data.getRowCount()) { data.setFirst(data.getRowCount() - 1); } else { data.setFirst(row - (row % rows)); } }
public void decode(FacesContext context, UIComponent component) { String id = component.getClientId(context); Map parameters = context.getExternalContext().getRequestParameterMap(); String response = (String) parameters.get(id); String dataTableId = (String) get(context, component, "dataTableId"); Integer a = (Integer) get(context, component, "showpages"); int showpages = a == null ? 0 : a.intValue(); UIData data = (UIData) findComponent(context.getViewRoot(), getId(dataTableId, id), context); int first = data.getFirst(); int itemcount = data.getRowCount(); int pagesize = data.getRows(); if (pagesize <= 0) { pagesize = itemcount; } if (response == null) { first = 0; } else if ("<".equals(response)) { first -= pagesize; } else if (">".equals(response)) { first += pagesize; } else if ("<<".equals(response)) { first -= pagesize * showpages; } else if (">>".equals(response)) { first += pagesize * showpages; } else { int page = 0; // default if cannot be parsed try { page = Integer.parseInt(response); } catch (NumberFormatException ex) { // do nothing, leave at zero log.debug("do nothing, leave at zero"); } first = (page - 1) * pagesize; } if (first + pagesize > itemcount) { first = itemcount - pagesize; } if (first < 0) { first = 0; } data.setFirst(first); }
public int getCurrentPage(FacesContext context) { String forValue = (String) getAttributes().get("for"); UIData uiData = (UIData) getUIData(context, forValue); if (uiData == null) { return 0; } int rowsPerPage = uiData.getRows(); // occasionally this object is invalided, and it longer has the current // row. In these cases, get current row from ScrollerBean Map sessionMap = FacesContext.getCurrentInstance().getExternalContext().getSessionMap(); RegistryObjectCollectionBean searchPanelBean = (RegistryObjectCollectionBean) sessionMap.get("roCollection"); int currentRow = searchPanelBean.getScrollerBean().getCurrentRow(); uiData.setFirst(currentRow); int result = currentRow / rowsPerPage + 1; return result; }
/** * 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) getUIData(context, forValue); if (uiData == null) { return 0; } int rowsPerPage = uiData.getRows(); int totalRows = 0; int result = 0; /* replace with count from scrollerbean totalRows = uiData.getRowCount(); */ ScrollerBean sbean = RegistryObjectCollectionBean.getInstance().getScrollerBean(); totalRows = sbean.getTotalResultCount(); result = totalRows / rowsPerPage; if (0 != (totalRows % rowsPerPage)) { result++; } return result; }
/** Scroll backwards to the previous page. */ public String previous() { int first = data.getFirst(); scroll(first - data.getRows()); return (null); }
/** Scroll forwards to the next page. */ public String next() { int first = data.getFirst(); scroll(first + data.getRows()); return (null); }
public void encodeChildren(FacesContext context, UIComponent component) throws IOException { if ((context == null) || (component == null)) { throw new NullPointerException( Util.getExceptionMessageString(Util.NULL_PARAMETERS_ERROR_MESSAGE_ID)); } if (log.isTraceEnabled()) { log.trace("Begin encoding children " + component.getId()); } if (!component.isRendered()) { if (log.isTraceEnabled()) { log.trace( "No encoding necessary " + component.getId() + " since " + "rendered attribute is set to false "); } return; } UIData data = (UIData) component; ValueBinding msgsBinding = component.getValueBinding("value"); List msgBeanList = (List) msgsBinding.getValue(context); // Set up variables we will need String columnClasses[] = getColumnClasses(data); int columnStyle = 0; int columnStyles = columnClasses.length; String rowClasses[] = getRowClasses(data); int rowStyles = rowClasses.length; ResponseWriter writer = context.getResponseWriter(); Iterator kids = null; Iterator grandkids = null; // Iterate over the rows of data that are provided int processed = 0; int rowIndex = data.getFirst() - 1; int rows = data.getRows(); int rowStyle = 0; writer.startElement("tbody", component); writer.writeText("\n", null); int hideDivNo = 0; while (true) { // PrivateMessageDecoratedBean dmb = null; // if(msgBeanList !=null && msgBeanList.size()>(rowIndex+1) && // rowIndex>=-1) // { // dmb = (PrivateMessageDecoratedBean)msgBeanList.get(rowIndex+1); // } // boolean hasChildBoolean = false; // if(dmb != null) // { // for(int i=0; i<msgBeanList.size(); i++) // { // PrivateMessageDecoratedBean tempDmb = // (PrivateMessageDecoratedBean)msgBeanList.get(i); // if(tempDmb.getUiInReply() != null && // tempDmb.getUiInReply().getId().equals(dmb.getMsg().getId())) // { // hasChildBoolean = true; // break; // } // } // } // Have we displayed the requested number of rows? if ((rows > 0) && (++processed > rows)) { break; } // Select the current row data.setRowIndex(++rowIndex); if (!data.isRowAvailable()) { break; // Scrolled past the last row } PrivateMessageDecoratedBean dmb = null; dmb = (PrivateMessageDecoratedBean) data.getRowData(); boolean hasChildBoolean = false; if (dmb != null) { // if dmb has depth = 0, check for children if (dmb.getDepth() == 0) { // first, get the index of the dmb int index = -1; for (int i = 0; i < msgBeanList.size(); i++) { PrivateMessageDecoratedBean tempDmb = (PrivateMessageDecoratedBean) msgBeanList.get(i); if (dmb.getMsg().getId().equals(tempDmb.getMsg().getId())) { index = i; break; } } if (index < (msgBeanList.size() - 1) && index >= 0) { PrivateMessageDecoratedBean nextDmb = (PrivateMessageDecoratedBean) msgBeanList.get(index + 1); if (nextDmb.getDepth() > 0) { hasChildBoolean = true; } } } } if (dmb != null && dmb.getDepth() > 0) { writer.write( "<tr style=\"display:none\" id=\"_id_" + new Integer(hideDivNo).toString() + "__hide_division_" + "\">"); } else { writer.write("<tr>"); } if (rowStyles > 0) { writer.writeAttribute("class", rowClasses[rowStyle++], "rowClasses"); if (rowStyle >= rowStyles) { rowStyle = 0; } } writer.writeText("\n", null); // Iterate over the child UIColumn components for each row columnStyle = 0; kids = getColumns(data); while (kids.hasNext()) { // Identify the next renderable column UIColumn column = (UIColumn) kids.next(); // Render the beginning of this cell writer.startElement("td", column); if (columnStyles > 0) { writer.writeAttribute("class", columnClasses[columnStyle++], "columnClasses"); if (columnStyle >= columnStyles) { columnStyle = 0; } } if (dmb != null && dmb.getDepth() > 0) { if (column.getId().endsWith("_msg_subject")) { StringBuilder indent = new StringBuilder(); int indentInt = dmb.getDepth() * 4; for (int i = 0; i < indentInt; i++) { indent.append(" "); } writer.write(indent.toString()); } } else { if (column.getId().endsWith("_msg_subject")) { if (hasChildBoolean && dmb.getDepth() == 0) { writer.write( " <img src=\"" + BARIMG + "\" style=\"" + CURSOR + "\" id=\"_id_" + Integer.valueOf(hideDivNo).toString() + "__img_hide_division_\"" + " onclick=\""); int childNo = getTotalChildNo(dmb, msgBeanList); String hideTr = ""; for (int i = 0; i < childNo; i++) { hideTr += "javascript:showHideDiv('_id_" + (hideDivNo + i) + "', '" + RESOURCE_PATH + "');"; } writer.write(hideTr); writer.write("\" />"); } } } // Render the contents of this cell by iterating over // the kids of our kids grandkids = getChildren(column); while (grandkids.hasNext()) { encodeRecursive(context, (UIComponent) grandkids.next()); } // Render the ending of this cell writer.endElement("td"); writer.writeText("\n", null); } // Render the ending of this row writer.endElement("tr"); writer.writeText("\n", null); if (dmb != null && dmb.getDepth() > 0) { //// /*ValueBinding expandedBinding = component.getValueBinding("expanded"); String expanded = ""; if(expandedBinding != null) expanded = (String)expandedBinding.getValue(context); if(expanded.equalsIgnoreCase("true")) {*/ writer.write("<script type=\"text/javascript\">"); writer.write(" showHideDiv('_id_" + hideDivNo + "', '" + RESOURCE_PATH + "');"); writer.write("</script>"); ////// } hideDivNo++; } } writer.endElement("tbody"); writer.writeText("\n", null); // Clean up after ourselves data.setRowIndex(-1); if (log.isTraceEnabled()) { log.trace("End encoding children " + component.getId()); } }
public void encodeBegin(FacesContext context, UIComponent component) throws IOException { String id = component.getClientId(context); UIComponent parent = component; while (!(parent instanceof UIForm)) { parent = parent.getParent(); } String formId = parent.getClientId(context); ResponseWriter writer = context.getResponseWriter(); String styleClass = (String) get(context, component, "styleClass"); String selectedStyleClass = (String) get(context, component, "selectedStyleClass"); String dataTableId = (String) get(context, component, "dataTableId"); String controlId = (String) get(context, component, "controlId"); Integer a = (Integer) get(context, component, "showpages"); int showpages = a == null ? 0 : a.intValue(); // find the component with the given ID UIData data = (UIData) findComponent(context.getViewRoot(), getId(dataTableId, id), context); int first = data.getFirst(); int itemcount = data.getRowCount(); int pagesize = data.getRows(); if (pagesize <= 0) { pagesize = itemcount; } int pages; int currentPage; if (pagesize != 0) { pages = itemcount / pagesize; currentPage = first / pagesize; if (itemcount % pagesize != 0) { pages++; } } else { pages = 1; currentPage = 1; } if (first >= itemcount - pagesize) { currentPage = pages - 1; } int startPage = 0; int endPage = pages; if (showpages > 0) { startPage = (currentPage / showpages) * showpages; endPage = Math.min(startPage + showpages, pages); } boolean showLinks = true; Boolean showThem = (Boolean) get(context, component, "showLinks"); if (showThem != null) { showLinks = showThem.booleanValue(); } // links << < # # # > >> if (showLinks) { if (currentPage > 0) { writeLink(writer, component, formId, controlId, "<", styleClass); } if (startPage > 0) { writeLink(writer, component, formId, controlId, "<<", styleClass); } for (int i = startPage; i < endPage; i++) { writeLink( writer, component, formId, controlId, "" + (i + 1), i == currentPage ? selectedStyleClass : styleClass); } if (endPage < pages) { writeLink(writer, component, formId, controlId, ">>", styleClass); } if (first < itemcount - pagesize) { writeLink(writer, component, formId, controlId, ">", styleClass); } } // hidden field to hold result writeHiddenField(writer, component, controlId, id); }