Search in sources :

Example 1 with LayoutElement

use of com.sun.jsftemplating.layout.descriptors.LayoutElement in project Payara by payara.

the class UtilHandlers method foreach.

/**
 *	<p> This handler provides the foreach loop functionality.  You should
 *	    specify a request attribute 'var' that will be used as the key for
 *	    storing each token in the list.  You can then retrieve each value
 *	    as the loop iterates by requesting the request scoped attribute
 *	    keyed by the value you supplied for 'var'.  You must also specify
 *	    the <code>List&lt;Object&gt;</code> to iterate over.</p>
 * @param handlerCtx
 * @return
 */
@Handler(id = "foreach", input = { @HandlerInput(name = "var", type = String.class, required = false, defaultValue = "idx"), @HandlerInput(name = "list", type = Collection.class, required = true) })
public static boolean foreach(HandlerContext handlerCtx) {
    String var = (String) handlerCtx.getInputValue("var");
    Collection<Object> list = (Collection<Object>) handlerCtx.getInputValue("list");
    List<com.sun.jsftemplating.layout.descriptors.handler.Handler> handlers = handlerCtx.getHandler().getChildHandlers();
    if (handlers.size() > 0) {
        // We have child handlers in the loop... execute while we iterate
        LayoutElement elt = handlerCtx.getLayoutElement();
        Map<String, Object> requestMap = handlerCtx.getFacesContext().getExternalContext().getRequestMap();
        if (list != null) {
            for (Object obj : list) {
                requestMap.put(var, obj);
                // Ignore whats returned by the handler... we need to return
                // false anyway to prevent children from being executed again
                // FIXME: Consider supporting a "break" type of functionality
                elt.dispatchHandlers(handlerCtx, handlers);
            }
        }
    }
    // This will prevent the child handlers from executing again
    return false;
}
Also used : LayoutElement(com.sun.jsftemplating.layout.descriptors.LayoutElement) Collection(java.util.Collection) Handler(com.sun.jsftemplating.annotation.Handler) Handler(com.sun.jsftemplating.annotation.Handler)

Example 2 with LayoutElement

use of com.sun.jsftemplating.layout.descriptors.LayoutElement in project Payara by payara.

the class UtilHandlers method forLoop.

/**
 *	<p> A utility handler that resembles the for() method in Java.  Handler inside the for loop will be executed
 *  in a loop.  start index is specified by "start",  till less than "end".
 * eg. forLoop(start="1"  end="3" varName="foo"){}, handler inside the {} will be executed 2 times.
 *
 *  <p> Input value: "start" -- Type: <code>Integer</code> Start index, default to Zero is not specified
 *  <p> Input value: "end" -- Type: <code>Integer</code> End index.
 *  <p> Input value: "varName" -- Type: <code>String</code>  Variable to be replaced in the for loop by the index.
 *	@param	handlerCtx	The HandlerContext.
 * @return
 */
@Handler(id = "forLoop", input = { @HandlerInput(name = "start", type = String.class), @HandlerInput(name = "end", type = Integer.class, required = true), @HandlerInput(name = "varName", type = String.class, required = true) })
public static boolean forLoop(HandlerContext handlerCtx) {
    String startInt = (String) handlerCtx.getInputValue("start");
    int start = (startInt == null) ? 0 : Integer.parseInt(startInt);
    int end = ((Integer) handlerCtx.getInputValue("end"));
    String varName = ((String) handlerCtx.getInputValue("varName"));
    List<com.sun.jsftemplating.layout.descriptors.handler.Handler> handlers = handlerCtx.getHandler().getChildHandlers();
    if (handlers.size() > 0) {
        LayoutElement elt = handlerCtx.getLayoutElement();
        Map<String, Object> requestMap = handlerCtx.getFacesContext().getExternalContext().getRequestMap();
        for (int ix = start; ix <= end; ix++) {
            requestMap.put(varName, ix);
            // ignore whats returned by the handler.
            elt.dispatchHandlers(handlerCtx, handlers);
        }
    }
    return false;
}
Also used : LayoutElement(com.sun.jsftemplating.layout.descriptors.LayoutElement) Handler(com.sun.jsftemplating.annotation.Handler) Handler(com.sun.jsftemplating.annotation.Handler)

Aggregations

Handler (com.sun.jsftemplating.annotation.Handler)2 LayoutElement (com.sun.jsftemplating.layout.descriptors.LayoutElement)2 Collection (java.util.Collection)1