Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2009, Red Hat, Inc. and individual contributors
   * by the @authors tag. See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 package org.ajax4jsf.component.behavior;
 
 import java.util.Set;
 
 import  javax.faces.FacesException;
 import  javax.faces.component.behavior.ClientBehaviorHint;
 import  javax.faces.context.FacesContext;
 import  javax.faces.event.AbortProcessingException;
 import  javax.faces.event.AjaxBehaviorEvent;
 import  javax.faces.event.AjaxBehaviorListener;
 import  javax.faces.event.BehaviorEvent;
 
 import  org.ajax4jsf.component.AjaxClientBehavior;
 import  org.richfaces.renderkit.util.CoreAjaxRendererUtils;

The <a4j:ajax> behavior allows Ajax capability to be added to a non-Ajax component. The non-Ajax component must implement the ClientBehaviorHolder interface for all the event attributes that support behavior rendering.

Author(s):
Anton Belevich
 
 @JsfBehavior(id = "org.ajax4jsf.behavior.Ajax", tag = @Tag(name = "ajax", handler = "org.richfaces.view.facelets.html.AjaxHandler", type = .),
         attributes = { "ajax-props.xml""ajaxBehavior-prop.xml" })
 public class AjaxBehavior extends ClientBehavior implements AjaxClientBehavior {
     public static final String BEHAVIOR_ID = "org.ajax4jsf.behavior.Ajax";
     private static final Set<ClientBehaviorHint> HINTS = Collections.unmodifiableSet(EnumSet.of(ClientBehaviorHint.SUBMITTING));
 
     enum PropertyKeys {
         data,
         execute,
         onbeforedomupdate,
         onbegin,
         oncomplete,
         onerror,
         queueId,
         render,
         status,
         disabled,
         limitRender,
         immediate,
         bypassUpdates,
         onbeforesubmit
     }
 
     private Set<Stringexecute;
     private Set<Stringrender;
     @SuppressWarnings("unused")
     @Attribute(generate = false, signature = @Signature(returnType = Void.class, parameters = AjaxBehaviorEvent.class))
     private MethodExpression listener;
 
     @Override
     public void setLiteralAttribute(String nameObject value) {
 
         ExpressionFactory expFactory = getFacesContext().getApplication().getExpressionFactory();
 
         if (compare(.name)) {
             setData(value);
         } else if (compare(.name)) {
             setExecute(toSet(.value));
         } else if (compare(.name)) {
             setRender(toSet(.value));
         } else if (compare(.name)) {
            setOnbeforedomupdate((Stringvalue);
        } else if (compare(.name)) {
            setOnbegin((Stringvalue);
        } else if (compare(.name)) {
            setOncomplete((Stringvalue);
        } else if (compare(.name)) {
            setOnerror((Stringvalue);
        } else if (compare(.name)) {
            setQueueId((Stringvalue);
        } else if (compare(.name)) {
            setStatus((Stringvalue);
        } else if (compare(.name)) {
            value = expFactory.coerceToType(valueBoolean.class);
            setDisabled((Booleanvalue);
        } else if (compare(.name)) {
            value = expFactory.coerceToType(valueBoolean.class);
            setLimitRender((Booleanvalue);
        } else if (compare(.name)) {
            value = expFactory.coerceToType(valueBoolean.class);
            setImmediate((Booleanvalue);
        } else if (compare(.name)) {
            value = expFactory.coerceToType(valueBoolean.class);
            setBypassUpdates((Booleanvalue);
        } else if (compare(.name)) {
            setOnbeforesubmit((Stringvalue);
        }
    }
    private Set<StringtoSet(Serializable propertyNameObject value) {
        Set<Stringresult = null;
        result = CoreAjaxRendererUtils.asIdsSet(value);
        if (result == null) {
            throw new FacesException(
                propertyName.toString()
                    + "' attribute value must be Collection, List, array, String, comma-separated String, whitespace-separate String'");
        }
        return result;
    }

    
Serialized (on default with JSON) data passed to the client by a developer on an AJAX request. It's accessible via "event.data" syntax. Both primitive types and complex types such as arrays and collections can be serialized and used with data
    @Attribute
    public Object getData() {
        return getStateHelper().eval(.);
    }
    public void setData(Object data) {
        getStateHelper().put(.data);
    }

    
Ids of components that will participate in the "execute" portion of the Request Processing Lifecycle. Can be a single id, a space or comma separated list of Id's, or an EL Expression evaluating to an array or Collection. Any of the keywords "@this", "@form", "@all", "@none", "@region" may be specified in the identifier list. Some components make use of additional keywords
    @Attribute
    public Collection<StringgetExecute() {
    }
    public void setExecute(Collection<Stringexecute) {
        this. = copyToSet(execute);
        clearInitialState();
    }

    
The client-side script method to be called after the ajax response comes back, but before the DOM is updated
    @Attribute
    public String getOnbeforedomupdate() {
        return (StringgetStateHelper().eval(.);
    }
    public void setOnbeforedomupdate(String onbeforedomupdate) {
        getStateHelper().put(.onbeforedomupdate);
    }

    
The client-side script method to be called before an ajax request.
    @Attribute
    public String getOnbegin() {
        return (StringgetStateHelper().eval(.);
    }
    public void setOnbegin(String onbegin) {
        getStateHelper().put(.onbegin);
    }

    
The client-side script method to be called before the AJAX request is submitted
    @Attribute
    public String getOnbeforesubmit() {
        return (StringgetStateHelper().eval(.);
    }
    public void setOnbeforesubmit(String onbeforesubmit) {
        getStateHelper().put(.onbeforesubmit);
    }

    
The client-side script method to be called after the DOM is updated
    @Attribute
    public String getOncomplete() {
        return (StringgetStateHelper().eval(.);
    }
    public void setOncomplete(String oncomplete) {
        getStateHelper().put(.oncomplete);
    }

    
The client-side script method to be called when an error has occurred during Ajax communications
    @Attribute
    public String getOnerror() {
        return (StringgetStateHelper().eval(.);
    }
    public void setOnerror(String onerror) {
        getStateHelper().put(.onerror);
    }

    
Identify the name of the destination queue
    @Attribute
    public String getQueueId() {
        return (StringgetStateHelper().eval(.);
    }
    public void setQueueId(String queueId) {
        getStateHelper().put(.queueId);
    }

    
Ids of components that will participate in the "render" portion of the Request Processing Lifecycle. Can be a single id, a space or comma separated list of Id's, or an EL Expression evaluating to an array or Collection. Any of the keywords "@this", "@form", "@all", "@none", "@region" may be specified in the identifier list. Some components make use of additional keywords
    @Attribute
    public Collection<StringgetRender() {
        return getCollectionValue(.);
    }
    public void setRender(Collection<Stringrender) {
        this. = copyToSet(render);
        clearInitialState();
    }

    
ID of the request status component
    @Attribute
    public String getStatus() {
        return (StringgetStateHelper().eval(.);
    }
    public void setStatus(String status) {
        getStateHelper().put(.status);
    }

    
If "true", do not initiate an ajax request when the associated event is observed
    @Attribute
    public boolean isDisabled() {
        return (BooleangetStateHelper().eval(.false);
    }
    public void setDisabled(boolean disabled) {
        getStateHelper().put(.disabled);
    }

    
If "true", render only those ids specified in the "render" attribute, forgoing the render of the auto-rendered panels
    @Attribute
    public boolean isLimitRender() {
        return (BooleangetStateHelper().eval(.false);
    }
    public void setLimitRender(boolean limitRender) {
        getStateHelper().put(.limitRender);
    }

    
Flag indicating that, if this component is activated by the user, notifications should be delivered to interested listeners and actions immediately (that is, during Apply Request Values phase) rather than waiting until Invoke Application phase.
    @Attribute
    public boolean isImmediate() {
        return (BooleangetStateHelper().eval(.false);
    }
    public void setImmediate(boolean immediate) {
        getStateHelper().put(.immediate);
    }

    
If "true", after process validations phase it skips updates of model beans on a force render response. It can be used for validating components input
    @Attribute
    public boolean isBypassUpdates() {
        return (BooleangetStateHelper().eval(.false);
    }
    public void setBypassUpdates(boolean bypassUpdates) {
        getStateHelper().put(.bypassUpdates);
    }
    @Override
    public String getRendererType() {
        return ;
    }
    @Override
    public Set<ClientBehaviorHint> getHints() {
        return ;
    }
    public void addAjaxBehaviorListener(AjaxBehaviorListener listener) {
        addBehaviorListener(listener);
    }
    public void removeAjaxBehaviorListener(AjaxBehaviorListener listener) {
        removeBehaviorListener(listener);
    }
    @Override
    public void broadcast(BehaviorEvent eventthrows AbortProcessingException {
        if (this.equals(event.getBehavior()) && event instanceof BypassUpdatesAjaxBehaviorEvent) {
            FacesContext.getCurrentInstance().renderResponse();
        }
        super.broadcast(event);
    }
    private Object saveSet(Serializable propertyNameSet<Stringset) {
        if ((set == null) || set.isEmpty()) {
            return null;
        }
        int size = set.size();
        if (size == 1) {
            return set.toArray(new String[1])[0];
        }
        return set.toArray(new String[size]);
    }
    private Set<StringrestoreSet(Serializable propertyNameObject state) {
        if (state == null) {
            return null;
        }
        Set<Stringset = toSet(propertyNamestate);
        return set;
    }
    private Set<StringcopyToSet(Collection<Stringcollection) {
        return Collections.unmodifiableSet(new HashSet<String>(collection));
    }
    private Collection<StringgetCollectionValue(Serializable propertyNameCollection<Stringcollection) {
        if (collection != null) {
            return collection;
        }
        Collection<Stringresult = null;
        ValueExpression expression = getValueExpression(propertyName.toString());
        if (expression != null) {
            FacesContext facesContext = FacesContext.getCurrentInstance();
            Object value = expression.getValue(facesContext.getELContext());
            if (value != null) {
                if (value instanceof Collection) {
                    return (Collection<String>) value;
                }
                result = toSet(propertyNamevalue);
            }
        }
        return result == null ? Collections.<String>emptyList() : result;
    }
    @Override
    public void restoreState(FacesContext contextObject state) {
        if (state != null) {
            Object[] values = (Object[]) state;
            super.restoreState(context, values[0]);
            if (values.length != 1) {
                 = restoreSet(.values[1]);
                 = restoreSet(.values[2]);
                clearInitialState();
            }
        }
    }
    @Override
    public Object saveState(FacesContext context) {
        Object parentState = super.saveState(context);
        Object state = null;
        if (initialStateMarked()) {
            if (parentState != null) {
                state = new Object[] { parentState };
            }
        } else {
            Object[] values = new Object[3];
            values[0] = parentState;
            values[1] = saveSet(.);
            values[2] = saveSet(.);
            state = values;
        }
        return state;
    }
New to GrepCode? Check out our FAQ X