Search in sources :

Example 1 with ClientBehaviorHolder

use of javax.faces.component.behavior.ClientBehaviorHolder in project ART-TIME by Artezio.

the class SheetRenderer method decodeBehaviors.

/**
 * Decodes client behaviors (ajax events).
 * <p>
 * @param context
 *            the FacesContext
 * @param component
 *            the Component being decodes
 */
protected void decodeBehaviors(FacesContext context, UIComponent component) {
    // get current behaviors
    Map<String, List<ClientBehavior>> behaviors = ((ClientBehaviorHolder) component).getClientBehaviors();
    // if empty, done
    if (behaviors.isEmpty())
        return;
    // get the parameter map and the behaviorEvent fired
    Map<String, String> params = context.getExternalContext().getRequestParameterMap();
    String behaviorEvent = params.get("javax.faces.behavior.event");
    // if no event, done
    if (behaviorEvent == null)
        return;
    // get behaviors for the event
    List<ClientBehavior> behaviorsForEvent = behaviors.get(behaviorEvent);
    if (behaviorsForEvent == null || behaviorsForEvent.isEmpty())
        return;
    // decode event if we are the source
    String behaviorSource = params.get("javax.faces.source");
    String clientId = component.getClientId();
    if (behaviorSource != null && clientId.equals(behaviorSource)) {
        for (ClientBehavior behavior : behaviorsForEvent) {
            behavior.decode(context, component);
        }
    }
}
Also used : List(java.util.List) ClientBehaviorHolder(javax.faces.component.behavior.ClientBehaviorHolder) ClientBehavior(javax.faces.component.behavior.ClientBehavior)

Example 2 with ClientBehaviorHolder

use of javax.faces.component.behavior.ClientBehaviorHolder in project primefaces by primefaces.

the class AbstractBehaviorHandler method apply.

@Override
public void apply(FaceletContext faceletContext, UIComponent parent) throws IOException {
    if (!ComponentHandler.isNew(parent)) {
        return;
    }
    String eventName = getEventName();
    if (UIComponent.isCompositeComponent(parent)) {
        boolean tagApplied = false;
        if (parent instanceof ClientBehaviorHolder) {
            applyAttachedObject(faceletContext, parent);
            tagApplied = true;
        }
        BeanInfo componentBeanInfo = (BeanInfo) parent.getAttributes().get(UIComponent.BEANINFO_KEY);
        if (null == componentBeanInfo) {
            throw new TagException(tag, "Composite component does not have BeanInfo attribute");
        }
        BeanDescriptor componentDescriptor = componentBeanInfo.getBeanDescriptor();
        if (null == componentDescriptor) {
            throw new TagException(tag, "Composite component BeanInfo does not have BeanDescriptor");
        }
        List<AttachedObjectTarget> targetList = (List<AttachedObjectTarget>) componentDescriptor.getValue(AttachedObjectTarget.ATTACHED_OBJECT_TARGETS_KEY);
        if (null == targetList && !tagApplied) {
            throw new TagException(tag, "Composite component does not support behavior events");
        }
        boolean supportedEvent = false;
        if (targetList != null) {
            for (int i = 0; i < targetList.size(); i++) {
                AttachedObjectTarget target = targetList.get(i);
                if (target instanceof BehaviorHolderAttachedObjectTarget) {
                    BehaviorHolderAttachedObjectTarget behaviorTarget = (BehaviorHolderAttachedObjectTarget) target;
                    if ((null != eventName && eventName.equals(behaviorTarget.getName())) || (null == eventName && behaviorTarget.isDefaultEvent())) {
                        supportedEvent = true;
                        break;
                    }
                }
            }
        }
        if (supportedEvent) {
            // Workaround to implementation specific composite component handlers
            FacesContext context = FacesContext.getCurrentInstance();
            PrimeEnvironment environment = PrimeApplicationContext.getCurrentInstance(context).getEnvironment();
            if (environment.isMojarra()) {
                addAttachedObjectHandlerToMojarra(environment, parent);
            } else {
                addAttachedObjectHandlerToMyFaces(parent, faceletContext);
            }
        } else {
            if (!tagApplied) {
                throw new TagException(tag, "Composite component does not support event " + eventName);
            }
        }
    } else if (parent instanceof ClientBehaviorHolder) {
        applyAttachedObject(faceletContext, parent);
    } else {
        throw new TagException(tag, "Unable to attach behavior to non-ClientBehaviorHolder parent");
    }
}
Also used : FacesContext(javax.faces.context.FacesContext) BehaviorHolderAttachedObjectTarget(javax.faces.view.BehaviorHolderAttachedObjectTarget) AttachedObjectTarget(javax.faces.view.AttachedObjectTarget) BeanInfo(java.beans.BeanInfo) BehaviorHolderAttachedObjectTarget(javax.faces.view.BehaviorHolderAttachedObjectTarget) ClientBehaviorHolder(javax.faces.component.behavior.ClientBehaviorHolder) BeanDescriptor(java.beans.BeanDescriptor) PrimeEnvironment(org.primefaces.config.PrimeEnvironment) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with ClientBehaviorHolder

use of javax.faces.component.behavior.ClientBehaviorHolder in project primefaces by primefaces.

the class AbstractBehaviorHandler method applyAttachedObject.

public void applyAttachedObject(FaceletContext faceletContext, UIComponent parent) {
    ClientBehaviorHolder holder = (ClientBehaviorHolder) parent;
    String eventName = getEventName();
    if (null == eventName) {
        eventName = holder.getDefaultEventName();
        if (null == eventName) {
            throw new TagException(tag, "Event attribute could not be determined: " + eventName);
        }
    } else {
        Collection<String> eventNames = holder.getEventNames();
        if (!eventNames.contains(eventName)) {
            throw new TagException(tag, "Event:" + eventName + " is not supported.");
        }
    }
    Application application = faceletContext.getFacesContext().getApplication();
    E behavior = (E) application.createBehavior(getBehaviorId());
    init(faceletContext, behavior, eventName, parent);
    holder.addClientBehavior(eventName, behavior);
}
Also used : ClientBehaviorHolder(javax.faces.component.behavior.ClientBehaviorHolder) Application(javax.faces.application.Application)

Example 4 with ClientBehaviorHolder

use of javax.faces.component.behavior.ClientBehaviorHolder in project primefaces by primefaces.

the class CoreRenderer method buildDomEvent.

protected String buildDomEvent(FacesContext context, UIComponent component, String domEvent, String behaviorEvent, String behaviorEventAlias, String command) {
    StringBuilder builder = null;
    boolean hasCommand = (command != null);
    Map<String, List<ClientBehavior>> allBehaviors = null;
    if (component instanceof ClientBehaviorHolder) {
        allBehaviors = ((ClientBehaviorHolder) component).getClientBehaviors();
    }
    Object event = component.getAttributes().get(domEvent);
    boolean hasEvent = (event != null);
    String behaviorEventName = behaviorEventAlias;
    if (allBehaviors != null && allBehaviors.containsKey(behaviorEvent)) {
        behaviorEventName = behaviorEvent;
    }
    List<ClientBehavior> behaviors = (allBehaviors == null) ? null : allBehaviors.get(behaviorEventName);
    boolean hasBehaviors = (behaviors != null && !behaviors.isEmpty());
    if (hasEvent || hasBehaviors || hasCommand) {
        if (builder == null) {
            builder = SharedStringBuilder.get(context, SB_RENDER_DOM_EVENTS);
        }
        int commandSize = 0;
        if (hasBehaviors) {
            commandSize += behaviors.size();
        }
        if (hasEvent) {
            commandSize++;
        }
        if (hasCommand) {
            commandSize++;
        }
        if (commandSize > 1) {
            boolean first = true;
            builder.append("PrimeFaces.bcn(this,event,[");
            if (hasEvent) {
                builder.append("function(event){").append(event).append("}");
                first = false;
            }
            if (hasBehaviors) {
                ClientBehaviorContext cbc = null;
                for (int i = 0; i < behaviors.size(); i++) {
                    ClientBehavior behavior = behaviors.get(i);
                    if (cbc == null) {
                        cbc = ClientBehaviorContext.createClientBehaviorContext(context, component, behaviorEventName, component.getClientId(context), Collections.<ClientBehaviorContext.Parameter>emptyList());
                    }
                    String script = behavior.getScript(cbc);
                    if (script != null) {
                        if (!first) {
                            builder.append(",");
                        }
                        builder.append("function(event){").append(script).append("}");
                        first = false;
                    }
                }
            }
            if (hasCommand) {
                if (!first) {
                    builder.append(",");
                }
                builder.append("function(event){").append(command).append("}");
                first = false;
            }
            builder.append("]);");
        } else {
            if (hasBehaviors) {
                ClientBehaviorContext cbc = ClientBehaviorContext.createClientBehaviorContext(context, component, behaviorEventName, component.getClientId(context), Collections.<ClientBehaviorContext.Parameter>emptyList());
                ClientBehavior behavior = behaviors.get(0);
                String script = behavior.getScript(cbc);
                if (script != null) {
                    builder.append(script);
                }
            } else if (hasCommand) {
                builder.append(command);
            } else if (hasEvent) {
                builder.append(event);
            }
        }
    }
    return (builder == null) ? null : builder.toString();
}
Also used : ClientBehaviorHolder(javax.faces.component.behavior.ClientBehaviorHolder) MixedClientBehaviorHolder(org.primefaces.component.api.MixedClientBehaviorHolder) ClientBehavior(javax.faces.component.behavior.ClientBehavior) ClientBehaviorContext(javax.faces.component.behavior.ClientBehaviorContext)

Example 5 with ClientBehaviorHolder

use of javax.faces.component.behavior.ClientBehaviorHolder in project primefaces by primefaces.

the class MenuItemAwareRenderer method setConfirmationScript.

protected void setConfirmationScript(FacesContext context, MenuItem item) {
    if (item instanceof ClientBehaviorHolder) {
        Map<String, List<ClientBehavior>> behaviors = ((ClientBehaviorHolder) item).getClientBehaviors();
        List<ClientBehavior> clickBehaviors = (behaviors == null) ? null : behaviors.get("click");
        if (clickBehaviors != null && !clickBehaviors.isEmpty()) {
            for (int i = 0; i < clickBehaviors.size(); i++) {
                ClientBehavior clientBehavior = clickBehaviors.get(i);
                if (clientBehavior instanceof ConfirmBehavior) {
                    ClientBehaviorContext cbc = ClientBehaviorContext.createClientBehaviorContext(context, (UIComponent) item, "click", item.getClientId(), Collections.emptyList());
                    clientBehavior.getScript(cbc);
                    break;
                }
            }
        }
    }
}
Also used : ClientBehaviorHolder(javax.faces.component.behavior.ClientBehaviorHolder) ConfirmBehavior(org.primefaces.behavior.confirm.ConfirmBehavior) ClientBehavior(javax.faces.component.behavior.ClientBehavior) ClientBehaviorContext(javax.faces.component.behavior.ClientBehaviorContext)

Aggregations

ClientBehaviorHolder (javax.faces.component.behavior.ClientBehaviorHolder)8 ClientBehavior (javax.faces.component.behavior.ClientBehavior)4 List (java.util.List)2 UIComponent (javax.faces.component.UIComponent)2 UIComponentBase (javax.faces.component.UIComponentBase)2 UIInput (javax.faces.component.UIInput)2 ClientBehaviorContext (javax.faces.component.behavior.ClientBehaviorContext)2 BeanDescriptor (java.beans.BeanDescriptor)1 BeanInfo (java.beans.BeanInfo)1 ArrayList (java.util.ArrayList)1 Application (javax.faces.application.Application)1 FacesContext (javax.faces.context.FacesContext)1 AttachedObjectTarget (javax.faces.view.AttachedObjectTarget)1 BehaviorHolderAttachedObjectTarget (javax.faces.view.BehaviorHolderAttachedObjectTarget)1 ConfirmBehavior (org.primefaces.behavior.confirm.ConfirmBehavior)1 MixedClientBehaviorHolder (org.primefaces.component.api.MixedClientBehaviorHolder)1 PrimeEnvironment (org.primefaces.config.PrimeEnvironment)1