Search in sources :

Example 1 with FaceletViewDeclarationLanguage

use of org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage in project myfaces by apache.

the class CreateDynamicCompositeComponentListener method processEvent.

@Override
public void processEvent(ComponentSystemEvent event) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    FaceletViewDeclarationLanguage vdl = (FaceletViewDeclarationLanguage) facesContext.getApplication().getViewHandler().getViewDeclarationLanguage(facesContext, facesContext.getViewRoot().getViewId());
    Facelet componentFacelet;
    FaceletFactory faceletFactory = vdl.getFaceletFactory();
    FaceletFactory.setInstance(faceletFactory);
    try {
        componentFacelet = faceletFactory.compileComponentFacelet(taglibURI, tagName, attributes);
    } finally {
        FaceletFactory.setInstance(null);
    }
    UIComponent component = event.getComponent();
    // The execution of this listener activated another call to PostAddToViewEvent, because
    // ComponentTagHandlerDelegate removes and add the component again. This is necessary because
    // the inner components also require the propagation of PostAddToViewEvent to refresh themselves.
    // but this check avoids the duplicate call to the facelet, even if the duplicate call does not
    // have any side effect (counts as a refresh).
    Integer step = (Integer) component.getAttributes().get(CompositeComponentResourceTagHandler.CREATE_CC_ON_POST_ADD_TO_VIEW);
    if (step != null && step == 0) {
        component.getAttributes().put(CompositeComponentResourceTagHandler.CREATE_CC_ON_POST_ADD_TO_VIEW, 1);
    } else {
        return;
    }
    try {
        facesContext.getAttributes().put(FaceletViewDeclarationLanguage.REFRESHING_TRANSIENT_BUILD, Boolean.TRUE);
        // Detect the relationship between parent and child, to ensure the component is properly created
        // and refreshed. In facelets this is usually done by core.FacetHandler, but since it is a
        // dynamic component, we need to do it here before apply the handler
        UIComponent parent = component.getParent();
        String facetName = null;
        if (parent.getFacetCount() > 0 && !parent.getChildren().contains(component)) {
            facetName = ComponentSupport.findFacetNameByComponentInstance(parent, component);
        }
        try {
            if (facetName != null) {
                parent.getAttributes().put(org.apache.myfaces.view.facelets.tag.faces.core.FacetHandler.KEY, facetName);
            }
            // The trick here is restore MARK_CREATED, just to allow ComponentTagHandlerDelegate to
            // find the component. Then we reset it to exclude it from facelets refresh algorithm.
            String markId = (String) component.getAttributes().get(FaceletViewDeclarationLanguage.GEN_MARK_ID);
            if (markId == null) {
                ((AbstractFacelet) componentFacelet).applyDynamicComponentHandler(facesContext, component, baseKey);
            } else {
                try {
                    component.getAttributes().put(ComponentSupport.MARK_CREATED, markId);
                    ((AbstractFacelet) componentFacelet).applyDynamicComponentHandler(facesContext, component.getParent(), baseKey);
                } finally {
                    component.getAttributes().put(ComponentSupport.MARK_CREATED, null);
                }
            }
            if (FaceletViewDeclarationLanguageBase.isDynamicComponentNeedsRefresh(facesContext)) {
                FaceletCompositionContext fcc = FaceletCompositionContext.getCurrentInstance(facesContext);
                if (fcc == null) {
                    FaceletViewDeclarationLanguageBase.activateDynamicComponentRefreshTransientBuild(facesContext);
                    FaceletViewDeclarationLanguageBase.resetDynamicComponentNeedsRefreshFlag(facesContext);
                    component.subscribeToEvent(DynamicComponentRefreshTransientBuildEvent.class, new RefreshDynamicComponentListener(taglibURI, tagName, attributes, baseKey));
                    component.getAttributes().put(DynamicComponentRefreshTransientBuildEvent.DYN_COMP_REFRESH_FLAG, Boolean.TRUE);
                } else {
                    component.subscribeToEvent(FaceletDynamicComponentRefreshTransientBuildEvent.class, new RefreshDynamicComponentListener(taglibURI, tagName, attributes, baseKey));
                }
            }
        } finally {
            if (facetName != null) {
                parent.getAttributes().remove(org.apache.myfaces.view.facelets.tag.faces.core.FacetHandler.KEY);
            }
        }
    } catch (IOException e) {
        throw new LocationAwareFacesException(e, component);
    } finally {
        facesContext.getAttributes().remove(FaceletViewDeclarationLanguage.REFRESHING_TRANSIENT_BUILD);
    }
}
Also used : FacesContext(jakarta.faces.context.FacesContext) FaceletCompositionContext(org.apache.myfaces.view.facelets.FaceletCompositionContext) AbstractFacelet(org.apache.myfaces.view.facelets.AbstractFacelet) Facelet(jakarta.faces.view.facelets.Facelet) FaceletFactory(org.apache.myfaces.view.facelets.FaceletFactory) AbstractFacelet(org.apache.myfaces.view.facelets.AbstractFacelet) UIComponent(jakarta.faces.component.UIComponent) FaceletViewDeclarationLanguage(org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage) IOException(java.io.IOException) RefreshDynamicComponentListener(org.apache.myfaces.view.facelets.compiler.RefreshDynamicComponentListener) LocationAwareFacesException(org.apache.myfaces.view.facelets.LocationAwareFacesException)

Example 2 with FaceletViewDeclarationLanguage

use of org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage in project myfaces by apache.

the class RefreshDynamicComponentListener method processEvent.

@Override
public void processEvent(ComponentSystemEvent event) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    FaceletViewDeclarationLanguage vdl = (FaceletViewDeclarationLanguage) facesContext.getApplication().getViewHandler().getViewDeclarationLanguage(facesContext, facesContext.getViewRoot().getViewId());
    Facelet componentFacelet;
    FaceletFactory faceletFactory = vdl.getFaceletFactory();
    FaceletFactory.setInstance(faceletFactory);
    try {
        componentFacelet = faceletFactory.compileComponentFacelet(taglibURI, tagName, attributes);
    } finally {
        FaceletFactory.setInstance(null);
    }
    try {
        UIComponent component = event.getComponent();
        facesContext.getAttributes().put(FaceletViewDeclarationLanguage.REFRESHING_TRANSIENT_BUILD, Boolean.TRUE);
        // Detect the relationship between parent and child, to ensure the component is properly created
        // and refreshed. In facelets this is usually done by core.FacetHandler, but since it is a
        // dynamic component, we need to do it here before apply the handler
        UIComponent parent = component.getParent();
        String facetName = null;
        if (parent.getFacetCount() > 0 && !parent.getChildren().contains(component)) {
            facetName = ComponentSupport.findFacetNameByComponentInstance(parent, component);
        }
        try {
            if (facetName != null) {
                parent.getAttributes().put(org.apache.myfaces.view.facelets.tag.faces.core.FacetHandler.KEY, facetName);
            }
            // The trick here is restore MARK_CREATED, just to allow ComponentTagHandlerDelegate to
            // find the component. Then we reset it to exclude it from facelets refresh algorithm.
            // Note oam.vf.GEN_MARK_ID helps to identify when there is a wrapper component or not,
            // and in that way identify which component is the parent.
            String markId = (String) component.getAttributes().get(FaceletViewDeclarationLanguage.GEN_MARK_ID);
            if (markId == null) {
                ((AbstractFacelet) componentFacelet).applyDynamicComponentHandler(facesContext, component, baseKey);
            } else {
                try {
                    component.getAttributes().put(ComponentSupport.MARK_CREATED, markId);
                    ((AbstractFacelet) componentFacelet).applyDynamicComponentHandler(facesContext, component.getParent(), baseKey);
                } finally {
                    component.getAttributes().put(ComponentSupport.MARK_CREATED, null);
                }
            }
        } finally {
            if (facetName != null) {
                parent.getAttributes().remove(org.apache.myfaces.view.facelets.tag.faces.core.FacetHandler.KEY);
            }
        }
    } catch (IOException e) {
        throw new FacesException(e);
    } finally {
        facesContext.getAttributes().remove(FaceletViewDeclarationLanguage.REFRESHING_TRANSIENT_BUILD);
    }
}
Also used : FacesContext(jakarta.faces.context.FacesContext) AbstractFacelet(org.apache.myfaces.view.facelets.AbstractFacelet) Facelet(jakarta.faces.view.facelets.Facelet) FaceletFactory(org.apache.myfaces.view.facelets.FaceletFactory) AbstractFacelet(org.apache.myfaces.view.facelets.AbstractFacelet) UIComponent(jakarta.faces.component.UIComponent) FaceletViewDeclarationLanguage(org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage) IOException(java.io.IOException) FacesException(jakarta.faces.FacesException)

Aggregations

UIComponent (jakarta.faces.component.UIComponent)2 FacesContext (jakarta.faces.context.FacesContext)2 Facelet (jakarta.faces.view.facelets.Facelet)2 IOException (java.io.IOException)2 AbstractFacelet (org.apache.myfaces.view.facelets.AbstractFacelet)2 FaceletFactory (org.apache.myfaces.view.facelets.FaceletFactory)2 FaceletViewDeclarationLanguage (org.apache.myfaces.view.facelets.FaceletViewDeclarationLanguage)2 FacesException (jakarta.faces.FacesException)1 FaceletCompositionContext (org.apache.myfaces.view.facelets.FaceletCompositionContext)1 LocationAwareFacesException (org.apache.myfaces.view.facelets.LocationAwareFacesException)1 RefreshDynamicComponentListener (org.apache.myfaces.view.facelets.compiler.RefreshDynamicComponentListener)1