Search in sources :

Example 1 with IterationStatus

use of org.primefaces.component.api.IterationStatus in project primefaces by primefaces.

the class DataList method forEachRow.

public void forEachRow(Consumer<IterationStatus> callback) {
    FacesContext context = getFacesContext();
    Map<String, Object> requestMap = context.getExternalContext().getRequestMap();
    String varStatus = getVarStatus();
    String rowIndexVar = getRowIndexVar();
    Object varStatusBackup = varStatus == null ? null : requestMap.get(varStatus);
    Object rowIndexVarBackup = rowIndexVar == null ? null : requestMap.get(rowIndexVar);
    int first = getFirst();
    int rows = getRows();
    int pageSize = first + rows;
    int rowCount = getRowCount();
    int last = rows == 0 ? rowCount : (first + rows);
    for (int i = first; i < last; i++) {
        setRowIndex(i);
        if (!isRowAvailable()) {
            break;
        }
        IterationStatus status = new IterationStatus((i == 0), (i == (rowCount - 1)), i, i, first, (pageSize - 1), 1);
        if (varStatus != null) {
            requestMap.put(varStatus, status);
        }
        if (rowIndexVar != null) {
            requestMap.put(rowIndexVar, i);
        }
        callback.accept(status);
    }
    // cleanup
    setRowIndex(-1);
    if (varStatus != null) {
        if (varStatusBackup == null) {
            requestMap.remove(varStatus);
        } else {
            requestMap.put(varStatus, varStatusBackup);
        }
    }
    if (rowIndexVar != null) {
        if (rowIndexVarBackup == null) {
            requestMap.remove(rowIndexVar);
        } else {
            requestMap.put(rowIndexVar, rowIndexVarBackup);
        }
    }
}
Also used : FacesContext(javax.faces.context.FacesContext) IterationStatus(org.primefaces.component.api.IterationStatus)

Example 2 with IterationStatus

use of org.primefaces.component.api.IterationStatus in project primefaces by primefaces.

the class GalleriaRenderer method encodeIndicators.

public void encodeIndicators(FacesContext context, Galleria galleria) throws IOException {
    UIComponent facet = galleria.getFacet("indicator");
    boolean shouldRenderFacet = ComponentUtils.shouldRenderFacet(facet);
    if (galleria.isShowIndicators() && shouldRenderFacet) {
        ResponseWriter writer = context.getResponseWriter();
        writer.startElement("ul", null);
        writer.writeAttribute("class", Galleria.INDICATORS_CLASS, null);
        if (galleria.isRepeating()) {
            Map<String, Object> requestMap = context.getExternalContext().getRequestMap();
            int rowCount = galleria.getRowCount();
            String varStatus = galleria.getVarStatus();
            Object varStatusBackup = varStatus == null ? null : requestMap.get(varStatus);
            for (int i = 0; i < rowCount; i++) {
                galleria.setIndex(i);
                IterationStatus status = new IterationStatus((i == 0), (i == (rowCount - 1)), i, i, 0, rowCount - 1, 1);
                if (varStatus != null) {
                    requestMap.put(varStatus, status);
                }
                encodeIndicator(context, galleria, galleria.getFacet("indicator"), true);
            }
            galleria.setIndex(-1);
            if (varStatus != null) {
                if (varStatusBackup == null) {
                    requestMap.remove(varStatus);
                } else {
                    requestMap.put(varStatus, varStatusBackup);
                }
            }
        } else {
            if (facet instanceof UIPanel) {
                for (UIComponent kid : facet.getChildren()) {
                    if (kid.isRendered()) {
                        encodeIndicator(context, galleria, kid, false);
                    }
                }
            } else {
                encodeIndicator(context, galleria, facet, false);
            }
        }
        writer.endElement("ul");
    }
}
Also used : ResponseWriter(javax.faces.context.ResponseWriter) UIComponent(javax.faces.component.UIComponent) UIPanel(javax.faces.component.UIPanel) IterationStatus(org.primefaces.component.api.IterationStatus)

Example 3 with IterationStatus

use of org.primefaces.component.api.IterationStatus in project primefaces by primefaces.

the class GalleriaRenderer method encodeItems.

public void encodeItems(FacesContext context, Galleria galleria) throws IOException {
    ResponseWriter writer = context.getResponseWriter();
    writer.startElement("ul", null);
    writer.writeAttribute("class", Galleria.ITEMS_CLASS, null);
    if (galleria.isRepeating()) {
        Map<String, Object> requestMap = context.getExternalContext().getRequestMap();
        int rowCount = galleria.getRowCount();
        String varStatus = galleria.getVarStatus();
        Object varStatusBackup = varStatus == null ? null : requestMap.get(varStatus);
        for (int i = 0; i < rowCount; i++) {
            galleria.setIndex(i);
            IterationStatus status = new IterationStatus((i == 0), (i == (rowCount - 1)), i, i, 0, rowCount - 1, 1);
            if (varStatus != null) {
                requestMap.put(varStatus, status);
            }
            writer.startElement("li", null);
            writer.writeAttribute("class", Galleria.ITEM_CLASS, null);
            renderChildren(context, galleria);
            writer.endElement("li");
        }
        galleria.setIndex(-1);
        if (varStatus != null) {
            if (varStatusBackup == null) {
                requestMap.remove(varStatus);
            } else {
                requestMap.put(varStatus, varStatusBackup);
            }
        }
    } else {
        for (UIComponent kid : galleria.getChildren()) {
            if (kid.isRendered()) {
                writer.startElement("li", null);
                writer.writeAttribute("class", Galleria.ITEM_CLASS, null);
                renderChild(context, kid);
                writer.endElement("li");
            }
        }
    }
    writer.endElement("ul");
}
Also used : ResponseWriter(javax.faces.context.ResponseWriter) UIComponent(javax.faces.component.UIComponent) IterationStatus(org.primefaces.component.api.IterationStatus)

Example 4 with IterationStatus

use of org.primefaces.component.api.IterationStatus in project primefaces by primefaces.

the class UIRepeat method visitChildren.

private boolean visitChildren(VisitContext context, VisitCallback callback) {
    Integer begin = this.getBegin();
    Integer end = this.getEnd();
    Integer step = this.getStep();
    int rowCount = getDataModel().getRowCount();
    int i = ((begin != null) ? begin : 0);
    int e = ((end != null) ? end : rowCount);
    int s = ((step != null) ? step : 1);
    validateIterationControlValues(rowCount, i, e);
    FacesContext faces = context.getFacesContext();
    this.setIndex(faces, i);
    this.updateIterationStatus(faces, new IterationStatus(true, (i + s > e || rowCount == 1), i, begin, end, step));
    while (i < e && this.isIndexAvailable()) {
        this.setIndex(faces, i);
        this.updateIterationStatus(faces, new IterationStatus(false, i + s >= e, i, begin, end, step));
        for (UIComponent kid : getChildren()) {
            if (kid.visitTree(context, callback)) {
                return true;
            }
        }
        i += s;
    }
    return false;
}
Also used : FacesContext(javax.faces.context.FacesContext) UIComponent(javax.faces.component.UIComponent) IterationStatus(org.primefaces.component.api.IterationStatus)

Example 5 with IterationStatus

use of org.primefaces.component.api.IterationStatus in project primefaces by primefaces.

the class UIRepeat method broadcast.

@Override
public void broadcast(FacesEvent event) throws AbortProcessingException {
    if (event instanceof IndexedEvent) {
        IndexedEvent idxEvent = (IndexedEvent) event;
        this.resetDataModel();
        int prevIndex = this.index;
        FacesEvent target = idxEvent.getTarget();
        FacesContext ctx = getFacesContext();
        UIComponent source = target.getComponent();
        UIComponent compositeParent = null;
        try {
            int rowCount = getDataModel().getRowCount();
            int idx = idxEvent.getIndex();
            this.setIndex(ctx, idx);
            Integer begin = this.getBegin();
            Integer end = this.getEnd();
            Integer step = this.getStep();
            int b = ((begin != null) ? begin : 0);
            int e = ((end != null) ? end : rowCount);
            int s = ((step != null) ? step : 1);
            this.updateIterationStatus(ctx, new IterationStatus(idx == b, (idx + s >= e || rowCount == 1), idx, begin, end, step));
            if (this.isIndexAvailable()) {
                if (!UIComponent.isCompositeComponent(source)) {
                    compositeParent = UIComponent.getCompositeComponentParent(source);
                }
                if (compositeParent != null) {
                    compositeParent.pushComponentToEL(ctx, null);
                }
                source.pushComponentToEL(ctx, null);
                source.broadcast(target);
            }
        } finally {
            source.popComponentFromEL(ctx);
            if (compositeParent != null) {
                compositeParent.popComponentFromEL(ctx);
            }
            this.updateIterationStatus(ctx, null);
            this.setIndex(ctx, prevIndex);
        }
    } else {
        super.broadcast(event);
    }
}
Also used : FacesContext(javax.faces.context.FacesContext) UIComponent(javax.faces.component.UIComponent) IterationStatus(org.primefaces.component.api.IterationStatus)

Aggregations

IterationStatus (org.primefaces.component.api.IterationStatus)6 UIComponent (javax.faces.component.UIComponent)5 FacesContext (javax.faces.context.FacesContext)3 ResponseWriter (javax.faces.context.ResponseWriter)2 IOException (java.io.IOException)1 FacesException (javax.faces.FacesException)1 UIPanel (javax.faces.component.UIPanel)1 Renderer (javax.faces.render.Renderer)1 PrimeEnvironment (org.primefaces.config.PrimeEnvironment)1