001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package com.sun.rave.web.ui.component.util.descriptors;
042:
043: import com.sun.rave.web.ui.component.util.event.AfterLoopEvent;
044: import com.sun.rave.web.ui.component.util.event.BeforeLoopEvent;
045: import com.sun.rave.web.ui.util.PermissionChecker;
046:
047: import java.io.IOException;
048:
049: import javax.faces.context.FacesContext;
050: import javax.faces.component.UIComponent;
051:
052: /**
053: * <P> This class defines a LayoutWhile {@link LayoutElement}. The
054: * LayoutWhile provides the functionality necessary to iteratively
055: * display a portion of the layout tree. The condition is a boolean
056: * equation and may use "$...{...}" type expressions to substitute
057: * values.</P>
058: *
059: * @see com.sun.rave.web.ui.util.VariableResolver
060: * @see com.sun.rave.web.ui.util.PermissionChecker
061: *
062: * @author Ken Paulsen (ken.paulsen@sun.com)
063: */
064: public class LayoutWhile extends LayoutIf implements LayoutElement {
065:
066: /**
067: * Constructor
068: */
069: public LayoutWhile(LayoutElement parent, String condition) {
070: super (parent, condition);
071: }
072:
073: /**
074: * <P> This method always returns true. The condition is checked in
075: * {@link #shouldContinue(UIComponent)} instead of here because
076: * the {@link #encode(FacesContext, UIComponent)} method
077: * evaluates the condition and calls the super. Performing the check
078: * here would cause the condition to be evaluated twice.</P>
079: *
080: * @param context The FacesContext
081: * @param component The UIComponent
082: *
083: * @return true
084: */
085: protected boolean encodeThis(FacesContext context,
086: UIComponent component) {
087: return true;
088: }
089:
090: /**
091: * <P> This method returns true if the condition of this LayoutWhile is
092: * met, false otherwise. This provides the functionality for
093: * iteratively displaying a portion of the layout tree.</P>
094: *
095: * @param component The UIComponent
096: *
097: * @return true if children are to be rendered, false otherwise.
098: */
099: protected boolean shouldContinue(UIComponent component) {
100: PermissionChecker checker = new PermissionChecker(this ,
101: component, getCondition());
102: // return checker.evaluate();
103: return checker.hasPermission();
104: }
105:
106: /**
107: * <P> This implementation overrides the parent <code>encode</code>
108: * method. It does this to cause the encode process to loop while
109: * {@link #shouldContinue(UIComponent)} returns
110: * true. Currently there is no infinite loop checking, so be
111: * careful.</P>
112: *
113: * @param context The FacesContext
114: * @param component The UIComponent
115: */
116: public void encode(FacesContext context, UIComponent component)
117: throws IOException {
118: Object result = dispatchHandlers(context, BEFORE_LOOP,
119: new BeforeLoopEvent((UIComponent) component));
120: while (shouldContinue(component)) {
121: super .encode(context, component);
122: }
123: result = dispatchHandlers(context, AFTER_LOOP,
124: new AfterLoopEvent((UIComponent) component));
125: }
126:
127: /**
128: * <P> This is the event "type" for
129: * {@link com.sun.rave.web.ui.component.util.event.Handler} elements to be
130: * invoked after this LayoutWhile is processed (outside loop).</P>
131: */
132: public static final String AFTER_LOOP = "afterLoop";
133:
134: /**
135: * <P> This is the event "type" for
136: * {@link com.sun.rave.web.ui.component.util.event.Handler} elements to be
137: * invoked before this LayoutWhile is processed (outside loop).</P>
138: */
139: public static final String BEFORE_LOOP = "beforeLoop";
140: }
|