Package javax.faces.component.behavior

Examples of javax.faces.component.behavior.ClientBehaviorHolder


     * Test method for {@link javax.faces.component.UIComponentBase#saveState(javax.faces.context.FacesContext)}.
     */
    public void testSaveState() {
        BehaviorComponent comp = new BehaviorComponent();
        // Cast component to the interface, to be sure about method definition.
        ClientBehaviorHolder holder = (ClientBehaviorHolder) comp;
        TestBehavior behavior = new TestBehavior();
        holder.addClientBehavior(ONCLICK, behavior);
        TestBehavior behavior2 = new TestBehavior();
        holder.addClientBehavior(ONCLICK, behavior2);
        TestBehavior behavior3 = new TestBehavior();
        holder.addClientBehavior(ONCHANGE, behavior3);
        Object state = comp.saveState(facesContext);
        BehaviorComponent restoredComp = new BehaviorComponent();
        restoredComp.restoreState(facesContext, state);
        Map<String, List<ClientBehavior>> behaviors = restoredComp.getClientBehaviors();
        assertFalse(behaviors.isEmpty());
View Full Code Here


     * Test method for {@link javax.faces.component.UIComponentBase#addClientBehavior(java.lang.String, javax.faces.component.behavior.Behavior)}.
     */
    public void testAddBehavior() {
        BehaviorComponent comp = new BehaviorComponent();
        // Cast component to the interface, to be sure about method definition.
        ClientBehaviorHolder holder = (ClientBehaviorHolder) comp;
        holder.addClientBehavior(ONCLICK, new TestBehavior());
        holder.addClientBehavior(ONCLICK, new TestBehavior());
        holder.addClientBehavior(ONCHANGE, new TestBehavior());
        try {
            holder.addClientBehavior("foo", new TestBehavior());
        } catch (IllegalArgumentException e) {
            return;
        }
        assertFalse(true);
    }
View Full Code Here

    /**
     * Test method for {@link javax.faces.component.UIComponentBase#getEventNames()}.
     */
    public void testGetEventNames() {
        BehaviorComponent comp = new BehaviorComponent();
        ClientBehaviorHolder holder = (ClientBehaviorHolder) comp;
        assertEquals(EVENTS, holder.getEventNames());
    }
View Full Code Here

     * Test method for {@link javax.faces.component.UIComponentBase#getClientBehaviors()}.
     */
    public void testGetBehaviors() {
        BehaviorComponent comp = new BehaviorComponent();
        // Cast component to the interface, to be sure about method definition.
        ClientBehaviorHolder holder = (ClientBehaviorHolder) comp;
        Map<String, List<ClientBehavior>> behaviors = holder.getClientBehaviors();
        assertTrue(behaviors.isEmpty());
        assertFalse(behaviors.containsKey(ONCLICK));
        assertFalse(behaviors.containsValue(new TestBehavior()));
        assertEquals(0, behaviors.entrySet().size());
        holder.addClientBehavior(ONCLICK, new TestBehavior());
        holder.addClientBehavior(ONCLICK, new TestBehavior());
        holder.addClientBehavior(ONCHANGE, new TestBehavior());
        assertFalse(behaviors.isEmpty());
        assertTrue(behaviors.containsKey(ONCLICK));
        assertTrue(behaviors.containsKey(ONCHANGE));
        assertFalse(behaviors.containsKey(ONTEST));
        assertEquals(2, behaviors.entrySet().size());
View Full Code Here

     * Test method for {@link javax.faces.component.UIComponentBase#getDefaultEventName()}.
     */
    public void testGetDefaultEventName() {
        BehaviorComponent comp = new BehaviorComponent();
        // Cast component to the interface, to be sure about method definition.
        ClientBehaviorHolder holder = (ClientBehaviorHolder) comp;
        assertEquals(ONTEST, holder.getDefaultEventName());
    }
View Full Code Here

    }
   
    public void applyAttachedObject(FacesContext context, UIComponent parent) {
        FaceletContext ctx = (FaceletContext) context.getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY);
        // cast to the ClientBehaviorHolder.
        ClientBehaviorHolder behaviorHolder = (ClientBehaviorHolder) parent;
        ValueExpression bindingExpr=null;
        Behavior behavior=null;
        if (null != owner.getBinding()){
            bindingExpr = owner.getBinding().getValueExpression(ctx, Behavior.class);
            behavior = (Behavior) bindingExpr.getValue(ctx);
        }
        if (null == behavior){
            if (null != owner.getBehaviorId()){
                behavior = ctx.getFacesContext().getApplication().createBehavior(owner.getBehaviorId());
                if (null == behavior){
                    throw new TagException(owner.getTag(),
                            "No Faces behavior defined for Id "+owner.getBehaviorId());
                }
                if (null != bindingExpr){
                    bindingExpr.setValue(ctx, behavior);
                }
            } else {
                throw new TagException(owner.getTag(),"No behaviorId defined");
            }
        }
        owner.setAttributes(ctx, behavior);

        if (behavior instanceof ClientBehavior) {
            behaviorHolder.addClientBehavior(getEventName(behaviorHolder), (ClientBehavior)behavior);
        }
    }
View Full Code Here

        if (!(component instanceof ClientBehaviorHolder)) {
            return null;
        }

        ClientBehaviorHolder holder = (ClientBehaviorHolder)component;
        Map<String, List<ClientBehavior>> behaviors = holder.getClientBehaviors();
        if (behaviors.isEmpty()) {
            return null;
        }

        ExternalContext external = context.getExternalContext();
View Full Code Here

        _delegate.setAttributes(faceletContext, behavior);
       
        if (behavior instanceof ClientBehavior)
        {
            // cast to a ClientBehaviorHolder
            ClientBehaviorHolder cvh = (ClientBehaviorHolder) parent;
           
            // TODO: check if the behavior could be applied to the current parent
            // For run tests it is not necessary, so we let this one pending.

            // It is necessary to obtain a event name for add it, so we have to
            // look first to the defined event name, otherwise take the default from
            // the holder
            String eventName = getEventName();
            if (eventName == null)
            {
                eventName = cvh.getDefaultEventName();
            }
            if (eventName == null)
            {
                throw new TagAttributeException(_delegate.getEvent(), "eventName could not be defined for client behavior "+ behavior.toString());
            }
            else if (!cvh.getEventNames().contains(eventName))
            {
                throw new TagAttributeException(_delegate.getEvent(), "eventName "+eventName+" not found on component instance");
            }
            else
            {
                cvh.addClientBehavior(eventName, (ClientBehavior) behavior);
            }
           
            AjaxHandler.registerJsfAjaxDefaultResource(faceletContext, parent);
        }
    }
View Full Code Here

        // Retrieve the current FaceletContext from FacesContext object
        FaceletContext faceletContext = (FaceletContext) context
                .getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY);

        // cast to a ClientBehaviorHolder
        ClientBehaviorHolder cvh = (ClientBehaviorHolder) parent;
       
        String eventName = getEventName();
        if (eventName == null)
        {
            eventName = cvh.getDefaultEventName();
            if (eventName == null)
            {
                if (_wrapMode)
                {
                    // No eventName defined, we can't apply this tag to this component, because
                    // there is no event defined to attach it, but since we are in wrap mode
                    // we have here the case that the component could not be the target
                    // for this attached object.
                    return;
                }
                else
                {
                    throw new TagAttributeException(_event, "eventName could not be defined for f:ajax tag with no wrap mode.");
                }
            }
        }
        else if (!cvh.getEventNames().contains(eventName))
        {
            if (_wrapMode)
            {
                // The current component does not implement the event selected,
                // this ajax behavior cannot be applied, but we can't throw any exception
                // since we are in wrap mode and we have here the case that the
                // component could not be the target for this attached object.
                return;
            }
            else
            {
                throw new TagAttributeException(_event, "event it is not a valid eventName defined for this component");
            }
        }
       
        Map<String, List<ClientBehavior>> clientBehaviors = cvh.getClientBehaviors();

        List<ClientBehavior> clientBehaviorList = clientBehaviors.get(eventName);
        if (clientBehaviorList != null && !clientBehaviorList.isEmpty())
        {
            for (ClientBehavior cb : clientBehaviorList)
            {
                if (cb instanceof AjaxBehavior)
                {
                    // The most inner one has been applied, so according to
                    // jsf 2.0 spec section 10.4.1.1 it is not necessary to apply
                    // this one, because the inner one has precendece over
                    // the outer one.
                    return;
                }
            }
        }

        AjaxBehavior ajaxBehavior = (AjaxBehavior) context.getApplication()
                .createBehavior(AjaxBehavior.BEHAVIOR_ID);

        if (_disabled != null)
        {
            if (_disabled.isLiteral())
            {
                ajaxBehavior.setDisabled(_disabled.getBoolean(faceletContext));
            }
            else
            {
                ajaxBehavior.setValueExpression("disabled", _disabled
                        .getValueExpression(faceletContext, Boolean.class));
            }
        }
        if (_execute != null)
        {
            ajaxBehavior.setValueExpression("execute", _execute
                    .getValueExpression(faceletContext, Object.class));
        }
        if (_immediate != null)
        {
            if (_immediate.isLiteral())
            {
                ajaxBehavior
                        .setImmediate(_immediate.getBoolean(faceletContext));
            }
            else
            {
                ajaxBehavior.setValueExpression("immediate", _immediate
                        .getValueExpression(faceletContext, Boolean.class));
            }
        }
        if (_listener != null)
        {
            MethodExpression expr = _listener.getMethodExpression(
                    faceletContext, Void.TYPE, AJAX_BEHAVIOR_LISTENER_SIG);
            AjaxBehaviorListener abl = new AjaxBehaviorListenerImpl(expr);
            ajaxBehavior.addAjaxBehaviorListener(abl);
        }
        if (_onerror != null)
        {
            if (_onerror.isLiteral())
            {
                ajaxBehavior.setOnerror(_onerror.getValue(faceletContext));
            }
            else
            {
                ajaxBehavior.setValueExpression("onerror", _onerror
                        .getValueExpression(faceletContext, String.class));
            }
        }
        if (_onevent != null)
        {
            if (_onevent.isLiteral())
            {
                ajaxBehavior.setOnevent(_onevent.getValue(faceletContext));
            }
            else
            {
                ajaxBehavior.setValueExpression("onevent", _onevent
                        .getValueExpression(faceletContext, String.class));
            }
        }
        if (_render != null)
        {
            ajaxBehavior.setValueExpression("render", _render
                    .getValueExpression(faceletContext, Object.class));
        }
       
        cvh.addClientBehavior(eventName, ajaxBehavior);
    }
View Full Code Here

     * @since 4.0.0
     */
    public static void decodeClientBehaviors(FacesContext facesContext,
                                             UIComponent component) {
        if (component instanceof ClientBehaviorHolder) {
            ClientBehaviorHolder clientBehaviorHolder = (ClientBehaviorHolder) component;

            Map<String, List<ClientBehavior>> clientBehaviors =
                    clientBehaviorHolder.getClientBehaviors();

            if (clientBehaviors != null && !clientBehaviors.isEmpty()) {
                Map<String, String> paramMap = facesContext.getExternalContext().
                        getRequestParameterMap();

View Full Code Here

TOP

Related Classes of javax.faces.component.behavior.ClientBehaviorHolder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.