Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   *
   * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
   *
   * The contents of this file are subject to the terms of either the GNU
   * General Public License Version 2 only ("GPL") or the Common Development
   * and Distribution License("CDDL") (collectively, the "License").  You
   * may not use this file except in compliance with the License.  You can
  * obtain a copy of the License at
  * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
  * or packager/legal/LICENSE.txt.  See the License for the specific
  * language governing permissions and limitations under the License.
  *
  * When distributing the software, include this License Header Notice in each
  * file and include the License file at packager/legal/LICENSE.txt.
  *
  * GPL Classpath Exception:
  * Oracle designates this particular file as subject to the "Classpath"
  * exception as provided by Oracle in the GPL Version 2 section of the License
  * file that accompanied this code.
  *
  * Modifications:
  * If applicable, add the following below the License Header, with the fields
  * enclosed by brackets [] replaced by your own identifying information:
  * "Portions Copyright [year] [name of copyright owner]"
  *
  * Contributor(s):
  * If you wish your version of this file to be governed by only the CDDL or
  * only the GPL Version 2, indicate your decision by adding "[Contributor]
  * elects to include this software in this distribution under the [CDDL or GPL
  * Version 2] license."  If you don't indicate a single choice of license, a
  * recipient has the option to distribute your version of this file under
  * either the CDDL, the GPL Version 2 or to extend the choice of license to
  * its licensees as provided above.  However, if you add GPL Version 2 code
  * and therefore, elected the GPL Version 2 license, then the option applies
  * only if the new code is made subject to such option by the copyright
  * holder.
  */
 
 package javax.faces.component.behavior;
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 

An instance of this class is added as a ClientBehavior to a component using the ClientBehaviorHolder.addClientBehavior(java.lang.String,javax.faces.component.behavior.ClientBehavior) contract that components implement. The presence of this ClientBehavior will cause the rendering of JavaScript that produces an Ajax request using the specification public JavaScript API when the component is rendered.

If the component is an instance of javax.faces.component.EditableValueHolder, Where at all possible, the component must have the UI register the ajax event when the initial value is changed, not when focus is lost on the component.

Since:
2.0
 
 
 public class AjaxBehavior extends ClientBehaviorBase {

    

The standard id for this behavior.

 
     public static final String BEHAVIOR_ID = "javax.faces.behavior.Ajax";
 
     private static final Set<ClientBehaviorHintHINTS = 
         Collections.unmodifiableSet(EnumSet.of(.));
 
     private String onerror;
     private String onevent;
     private List<Stringexecute;
     private List<Stringrender;
     private Boolean disabled;
     private Boolean immediate;
    private Map<StringValueExpressionbindings;
    // ---------------------------------------------------------- Public Methods
    @Override
    public String getRendererType() {
        // We use the same sring for both the behavior id and the renderer 
        // type.
        return  ;
    }


    

This method returns an unmodifiable Set containing the ClientBehaviorHint SUBMITTING.

Returns:
unmodifiable set containing the hint ClientBehaviorHint SUBMITTING.
Since:
2.0
    public Set<ClientBehaviorHintgetHints() {
        return ;
    }

    

Return the String of JavaScript function name that will be used to identify the client callback function that should be run in the event of an error.

Since:
2.0
    public String getOnerror() {
        return (Stringeval();
    }

    

Sets the JavaScript function name that will be used to identify the client callback function that should be run in the event of an error.

Parameters:
onerror the error handling function name
Since:
2.0
    public void setOnerror(String onerror) {
        this. = onerror;
        clearInitialState();
    }

    

Return the String of JavaScript function name that will be used to identify the client callback function that should be run on the occurance of a client-side event.

Since:
2.0
    public String getOnevent() {
        return (Stringeval();
    }

    

Sets the JavaScript function name that will be used to identify the client callback function that should be run in response to event activity.

Parameters:
onevent the event handling function name
Since:
2.0
    public void setOnevent(String onevent) {
        this. = onevent;
        clearInitialState();
   }

    

Return a non-empty Collection<String> of component identifiers that will be used to identify components that should be processed during the execute phase of the request processing lifecycle.

Note that the returned collection may be unmodifiable. Modifications should be performed by calling setExecute(java.util.Collection).

Since:
2.0
    public Collection<StringgetExecute() {
        return getCollectionValue();
    }

    

Sets the component identifiers that will be used to identify components that should be processed during the execute phase of the request processing lifecycle.

Parameters:
execute the ids of components to execute
Since:
2.0
    public void setExecute(Collection<Stringexecute) {
        this. = copyToList(execute);
        clearInitialState();
    }

    

Return a non-empty Collection<String> of component identifiers that will be used to identify components that should be processed during the render phase of the request processing lifecycle.

Note that the returned collection may be unmodifiable. Modifications should be performed by calling setRender(java.util.Collection).

Since:
2.0
    public Collection<StringgetRender() {
        return getCollectionValue();
    }

    

Sets the component identifiers that will be used to identify components that should be processed during the render phase of the request processing lifecycle.

Parameters:
render the ids of components to render
Since:
2.0
    public void setRender(Collection<Stringrender) {
        this. = copyToList(render);
        clearInitialState();
    }

    

Return the disabled status of this behavior.

Since:
2.0
    public boolean isDisabled() {
        Boolean result = (Booleaneval();
        return ((result != null) ? result : false);
    }

    

Sets the disabled status of this behavior.

Since:
2.0
    public void setDisabled(boolean disabled) {
        this. = disabled;
        clearInitialState();
    }

    

Return the immediate status of this behaivor.

Since:
2.0
    public boolean isImmediate() {
        Boolean result = (Booleaneval();
        return ((result != null) ? result : false);
    }


    

Sets the immediate status of this behavior.

Since:
2.0
    public void setImmediate(boolean immediate) {
        this. = immediate;
        clearInitialState();
    }

    

Tests whether the immediate attribute is specified. Returns true if the immediate attribute is specified, either as a locally set property or as a value expression. This information allows an associated client behavior renderer to fall back on the parent component's immediate status when immediate is not explicitly specified on the AjaxBehavior.

Since:
2.0
    public boolean isImmediateSet() {
        return (( != null) || (getValueExpression() != null));
    }

    

Returns the javax.el.ValueExpression used to calculate the value for the specified property name, if any.

Parameters:
name Name of the property for which to retrieve a javax.el.ValueExpression
Throws:
java.lang.NullPointerException if name is null
    public ValueExpression getValueExpression(String name) {
        if (name == null) {
            throw new NullPointerException();
        }
        return (( == null) ? null : .get(name));
    }

    

Sets the javax.el.ValueExpression used to calculate the value for the specified property name.

Parameters:
name Name of the property for which to set a javax.el.ValueExpression
binding The javax.el.ValueExpression to set, or null to remove any currently set javax.el.ValueExpression
Throws:
java.lang.NullPointerException if name is null
    public void setValueExpression(String nameValueExpression binding) {
        if (name == null) {
            throw new NullPointerException();
        }
        if (binding != null) {
            if (binding.isLiteralText()) {
                setLiteralValue(namebinding);
            } else {
                if ( == null) {
                    // We use a very small initial capacity on this HashMap.
                    // The goal is not to reduce collisions, but to keep the
                    // memory footprint small.  It is very unlikely that an
                    // an AjaxBehavior would have more than 1 or 2 bound 
                    // properties - and even if more are present, it's okay
                    // if we have some collisions - will still be fast.
                     = new HashMap<StringValueExpression>(6,1.0f);
                }
                .put(namebinding);
            }
        } else {
            if ( != null) {
                .remove(name);
                if (.isEmpty()) {
                     = null;
                }
            }
        }
        clearInitialState();
    }

    

Add the specified javax.faces.event.AjaxBehaviorListener to the set of listeners registered to receive event notifications from this AjaxBehavior.

Parameters:
listener The javax.faces.event.AjaxBehaviorListener to be registered
Throws:
java.lang.NullPointerException if listener is null
Since:
2.0
    public void addAjaxBehaviorListener(AjaxBehaviorListener listener) {
        addBehaviorListener(listener);
    }

    

Remove the specified javax.faces.event.AjaxBehaviorListener from the set of listeners registered to receive event notifications from this AjaxBehavior.

Parameters:
listener The javax.faces.event.AjaxBehaviorListener to be removed
Throws:
java.lang.NullPointerException if listener is null
Since:
2.0
    public void removeAjaxBehaviorListener(AjaxBehaviorListener listener) {
        removeBehaviorListener(listener);
    }
    @Override
    public Object saveState(FacesContext context) {
        if (context == null) {
            throw new NullPointerException();
        }
        Object[] values;
        Object superState = super.saveState(context);
        if (initialStateMarked()) {
            if (superState == null) {
                values = null;
            } else {
                values = new Object[] { superState };
            }
        } else {
            values = new Object[8];
      
            values[0] = superState;
            values[1] = ;
            values[2] = ;
            values[3] = ;
            values[4] = ;
            values[5] = saveList();
            values[6] = saveList();
            values[7] = saveBindings(context);
        }
        return values;
    }
    @Override
    public void restoreState(FacesContext contextObject state) {
        if (context == null) {
            throw new NullPointerException();
        }
        if (state != null) {
            Object[] values = (Object[]) state;
            super.restoreState(context, values[0]);
            if (values.length != 1) {
                 = (String)values[1];
                 = (String)values[2];
                 = (Boolean)values[3];
                 = (Boolean)values[4];
                 = restoreList(values[5]);
                 = restoreList(values[6]);
                 = restoreBindings(contextvalues[7]);
                // If we saved state last time, save state again next time.
                clearInitialState();
            }
        }
    }
    // --------------------------------------------------------- Private Methods
    // Utility for saving bindings state
    private static Object saveBindings(FacesContext context,
                                       Map<StringValueExpressionbindings) {
        // Note: This code is copied from UIComponentBase.  In a future
        // version of the JSF spec, it would be useful to define a
        // attribute/property/bindings/state helper object that can be
        // shared across components/behaviors/validaters/converters.
        
        if (bindings == null) {
            return (null);
        }
        Object values[] = new Object[2];
        values[0] = bindings.keySet().toArray(new String[bindings.size()]);
        Object[] bindingValues = bindings.values().toArray();
        for (int i = 0; i < bindingValues.lengthi++) {
            bindingValues[i] = UIComponentBase.saveAttachedState(contextbindingValues[i]);
        }
        values[1] = bindingValues;
        return (values);
    }
    // Utility for restoring bindings from state
    private static Map<StringValueExpressionrestoreBindings(FacesContext context,
                                                                Object state) {
        // Note: This code is copied from UIComponentBase.  See note above
        // in saveBindings().
        if (state == null) {
            return (null);
        }
        Object values[] = (Object[]) state;
        String names[] = (String[]) values[0];
        Object states[] = (Object[]) values[1];
        Map<StringValueExpressionbindings = new HashMap<StringValueExpression>(names.length);
        for (int i = 0; i < names.lengthi++) {
            bindings.put(names[i],
                    (ValueExpression) UIComponentBase.restoreAttachedState(contextstates[i]));
        }
        return (bindings);
    }
    // Save the List<String>, either as a String (single element) or as
    // a String[] (multiple elements.
    private static Object saveList(List<Stringlist) {
        if ((list == null) || list.isEmpty()) {
            return null;
        }
        int size = list.size();
        if (size == 1) {
            return list.get(0);
        }
        return list.toArray(new String[size]);
    }
    // Restore the list from a String (single element) or a String[]
    // (multiple elements)
    private static List<StringrestoreList(String propertyName
                                            Object state) {
        if (state == null) {
            return null;
        }
        List<Stringlist = null;
        if (state instanceof String) {
            list = toSingletonList(propertyName, (String)state);
        } else if (state instanceof String[]) {
            list = Collections.unmodifiableList(Arrays.asList((String[])state));
        }
        return list;
    }
      
    private Object eval(String propertyNameObject value) {
        if (value != null) {
            return value;
        }
        ValueExpression expression = getValueExpression(propertyName);
        if (expression != null) {
            FacesContext ctx = FacesContext.getCurrentInstance();
            return expression.getValue(ctx.getELContext());
        }
        return null;
    }
    @SuppressWarnings("unchecked")
    private Collection<StringgetCollectionValue(String propertyName,
                                                  Collection<Stringcollection) {
        if (collection!= null) {
            return collection;
        }
        Collection<Stringresult = null;
        ValueExpression expression = getValueExpression(propertyName);
        if (expression != null) {
            FacesContext ctx = FacesContext.getCurrentInstance();
            Object value = expression.getValue(ctx.getELContext());
            if (value != null) {
                if (value instanceof Collection) {
                    // Unchecked cast to Collection<String>
                    return (Collection<String>)value;
                }
                result = toList(propertyNameexpressionvalue);
            }
        }
        return result == null ? Collections.<String>emptyList() : result;
    }
    // Sets a property, converting it from a literal
    private void setLiteralValue(String propertyName,
                                 ValueExpression expression) {
        assert(expression.isLiteralText());
        Object value;
        ELContext context = FacesContext.getCurrentInstance().getELContext();
        try {
            value = expression.getValue(context);
        } catch (ELException ele) {
            throw new FacesException(ele);
        }
        if (.equals(propertyName)) {
             = (String)value;
        } else if (.equals(propertyName)) {
             = (String)value;
        } else if (.equals(propertyName)) {
             = (Boolean)value;
        } else if (.equals(propertyName)) {
             = (Boolean)value;
        } else if (.equals(propertyName)) {
             = toList(propertyNameexpressionvalue);
        } else if (.equals(propertyName)) {
             = toList(propertyNameexpressionvalue);
        }
    }
    // Converts the specified object to a List<String>
    private static List<StringtoList(String propertyName,
                                ValueExpression expression,
                                Object value) {
        if (value instanceof String) {
            String strValue = (String)value;
            // If the value contains no spaces, we can optimize.
            // This is worthwhile, since the execute/render lists
            // will often only contain a single value.
            if (strValue.indexOf(' ') == -1) {
                return toSingletonList(propertyNamestrValue);
            }
            // We're stuck splitting up the string.
            String[] values = .split(strValue);
            if ((values == null) || (values.length == 0)) {
                return null;
            }
            // Note that we could create a Set out of the values if
            // we care about removing duplicates.  However, the
            // presence of duplicates does not real harm.  They will
            // be consolidated during the partial view traversal.  So,
            // just create an list - garbage in, garbage out.
            return Collections.unmodifiableList(Arrays.asList(values));
        }
        // RELEASE_PENDING i18n ;
        throw new FacesException(expression.toString()
                                 + " : '"
                                 + propertyName
                                 + "' attribute value must be either a String or a Collection");
    }
    // Converts a String with no spaces to a singleton list
    private static List<StringtoSingletonList(String propertyName,
                                         String value) {
        if ((null == value) || (value.length() == 0)) {
            return null;
        }
        if (value.charAt(0) == '@') {
            // These are very common, so we use shared copies
            // of these collections instead of re-creating.
            List<Stringlist;
            if (.equals(value)) {
                list = ;
            } else if (.equals(value)){
                list = ;
            } else if (.equals(value)) {
                list = 
            } else if (.equals(value)) {
                list = ;
            } else {
                // RELEASE_PENDING i18n ;
                throw new FacesException(value
                                     + " : Invalid id keyword specified for '"
                                     + propertyName
                                     + "' attribute");
            }
            
            return list;
        }
         
        return Collections.singletonList(value);
    }
    // Makes a defensive copy of the collection, converting to a List
    // (to make state saving a bit easier).
    private List<StringcopyToList(Collection<Stringcollection) {
 
        if ((collection == null) || collection.isEmpty()) {
            return null;
        }
       return Collections.unmodifiableList(new ArrayList<String>(collection));
    }
    // Property name constants
    private static final String ONEVENT = "onevent";
    private static final String ONERROR = "onerror";
    private static final String IMMEDIATE = "immediate";
    private static final String DISABLED = "disabled";
    private static final String EXECUTE = "execute";
    private static final String RENDER = "render";
    // Id keyword constants
    private static String ALL = "@all";
    private static String FORM = "@form";
    private static String THIS = "@this";
    private static String NONE = "@none";
    // Shared execute/render collections
    private static List<StringALL_LIST = Collections.singletonList("@all");
    private static List<StringFORM_LIST = Collections.singletonList("@form");
    private static List<StringTHIS_LIST = Collections.singletonList("@this");
    private static List<StringNONE_LIST = Collections.singletonList("@none");
    // Pattern used for execute/render string splitting
    private static Pattern SPLIT_PATTERN = Pattern.compile(" ");
New to GrepCode? Check out our FAQ X