Search in sources :

Example 1 with VisitCallback

use of jakarta.faces.component.visit.VisitCallback in project myfaces by apache.

the class IdSearchKeywordResolver method resolve.

@Override
public void resolve(SearchKeywordContext expressionContext, UIComponent current, String keyword) {
    FacesContext facesContext = expressionContext.getSearchExpressionContext().getFacesContext();
    final String targetId = extractId(keyword);
    if (expressionContext.getSearchExpressionContext().getExpressionHints() != null && expressionContext.getSearchExpressionContext().getExpressionHints().contains(SearchExpressionHint.SKIP_VIRTUAL_COMPONENTS)) {
        // Avoid visit tree because in this case we need real component instances.
        // This means components inside UIData will not be scanned.
        withId(facesContext, targetId, current, expressionContext.getCallback());
        expressionContext.setKeywordResolved(true);
    } else {
        current.visitTree(VisitContext.createVisitContext(facesContext, null, expressionContext.getSearchExpressionContext().getVisitHints()), new VisitCallback() {

            @Override
            public VisitResult visit(VisitContext context, UIComponent target) {
                if (targetId.equals(target.getId())) {
                    expressionContext.invokeContextCallback(target);
                    if (expressionContext.getSearchExpressionContext().getExpressionHints() != null && expressionContext.getSearchExpressionContext().getExpressionHints().contains(SearchExpressionHint.RESOLVE_SINGLE_COMPONENT)) {
                        return VisitResult.COMPLETE;
                    }
                    return VisitResult.ACCEPT;
                } else {
                    return VisitResult.ACCEPT;
                }
            }
        });
    }
}
Also used : FacesContext(jakarta.faces.context.FacesContext) VisitCallback(jakarta.faces.component.visit.VisitCallback) VisitContext(jakarta.faces.component.visit.VisitContext) UIComponent(jakarta.faces.component.UIComponent) VisitResult(jakarta.faces.component.visit.VisitResult)

Example 2 with VisitCallback

use of jakarta.faces.component.visit.VisitCallback in project myfaces by apache.

the class UISelectOne method processValidators.

/**
 * Verify that when ever there is a ValueExpression and submitted value is not empty, then
 * visit all the UISelectItem elements within the UISelectOne radio components to check if
 * the submitted value exists in any of the select items.
 *
 * @see jakarta.faces.component.UIInput#processValidators(jakarta.faces.context.FacesContext)
 */
@Override
public void processValidators(FacesContext context) {
    String group = getGroup();
    ValueExpression ve = getValueExpression("value");
    String submittedValue = (String) getSubmittedValue();
    if (group != null && !group.isEmpty() && ve != null && !isEmpty(submittedValue)) {
        final UIComponent form = getRadioNestingForm(context, this);
        form.visitTree(VisitContext.createVisitContext(context), new VisitCallback() {

            @Override
            public VisitResult visit(VisitContext visitContext, UIComponent target) {
                if (target instanceof UISelectOne && ((UISelectOne) target).getGroup().equals(group)) {
                    UISelectOne radio = (UISelectOne) target;
                    // and verify if the submitted value exists
                    for (Iterator<UIComponent> iter = radio.getChildren().iterator(); iter.hasNext(); ) {
                        UIComponent component = iter.next();
                        if (component instanceof UISelectItem) {
                            UISelectItem item = (UISelectItem) component;
                            if (item.getItemValue().equals(submittedValue)) {
                                selectItemValueFound = true;
                                return VisitResult.COMPLETE;
                            }
                        }
                    }
                    return VisitResult.REJECT;
                }
                return VisitResult.ACCEPT;
            }
        });
    }
    super.processValidators(context);
}
Also used : VisitCallback(jakarta.faces.component.visit.VisitCallback) VisitContext(jakarta.faces.component.visit.VisitContext) ValueExpression(jakarta.el.ValueExpression) Iterator(java.util.Iterator) SelectItemsIterator(org.apache.myfaces.core.api.shared.SelectItemsIterator) VisitResult(jakarta.faces.component.visit.VisitResult) UISelectItem(jakarta.faces.component.UISelectItem)

Example 3 with VisitCallback

use of jakarta.faces.component.visit.VisitCallback in project myfaces by apache.

the class PartialStateManagementStrategy method saveStateOnMapVisitTree.

private void saveStateOnMapVisitTree(final FacesContext facesContext, final Map<String, Object> states, final UIViewRoot uiViewRoot) {
    facesContext.getAttributes().put(MyFacesVisitHints.SKIP_ITERATION_HINT, Boolean.TRUE);
    try {
        uiViewRoot.visitTree(getVisitContextFactory().getVisitContext(facesContext, null, MyFacesVisitHints.SET_SKIP_ITERATION), new VisitCallback() {

            @Override
            public VisitResult visit(VisitContext context, UIComponent target) {
                FacesContext facesContext = context.getFacesContext();
                Object state;
                if ((target == null) || target.isTransient()) {
                    return VisitResult.REJECT;
                }
                ComponentState componentAddedAfterBuildView = (ComponentState) target.getAttributes().get(COMPONENT_ADDED_AFTER_BUILD_VIEW);
                // Note if UIViewRoot has this marker, JSF 1.2 like state saving is used.
                if (componentAddedAfterBuildView != null && (target.getParent() != null)) {
                    if (ComponentState.REMOVE_ADD.equals(componentAddedAfterBuildView)) {
                        registerOnAddRemoveList(facesContext, target.getClientId(facesContext));
                        target.getAttributes().put(COMPONENT_ADDED_AFTER_BUILD_VIEW, ComponentState.ADDED);
                    } else if (ComponentState.ADD.equals(componentAddedAfterBuildView)) {
                        registerOnAddList(facesContext, target.getClientId(facesContext));
                        target.getAttributes().put(COMPONENT_ADDED_AFTER_BUILD_VIEW, ComponentState.ADDED);
                    } else if (ComponentState.ADDED.equals(componentAddedAfterBuildView)) {
                        registerOnAddList(facesContext, target.getClientId(facesContext));
                    }
                    ensureClearInitialState(target);
                    // Save all required info to restore the subtree.
                    // This includes position, structure and state of subtree
                    int childIndex = target.getParent().getChildren().indexOf(target);
                    if (childIndex >= 0) {
                        states.put(target.getClientId(facesContext), new AttachedFullStateWrapper(new Object[] { target.getParent().getClientId(facesContext), null, childIndex, internalBuildTreeStructureToSave(target), target.processSaveState(facesContext) }));
                    } else {
                        String facetName = null;
                        if (target.getParent().getFacetCount() > 0) {
                            for (Map.Entry<String, UIComponent> entry : target.getParent().getFacets().entrySet()) {
                                if (target.equals(entry.getValue())) {
                                    facetName = entry.getKey();
                                    break;
                                }
                            }
                        }
                        states.put(target.getClientId(facesContext), new AttachedFullStateWrapper(new Object[] { target.getParent().getClientId(facesContext), facetName, null, internalBuildTreeStructureToSave(target), target.processSaveState(facesContext) }));
                    }
                    return VisitResult.REJECT;
                } else if (target.getParent() != null) {
                    state = target.saveState(facesContext);
                    if (state != null) {
                        // Save by client ID into our map.
                        states.put(target.getClientId(facesContext), state);
                    }
                    return VisitResult.ACCEPT;
                } else {
                    // Only UIViewRoot has no parent in a component tree.
                    return VisitResult.ACCEPT;
                }
            }
        });
    } finally {
        facesContext.getAttributes().remove(MyFacesVisitHints.SKIP_ITERATION_HINT);
    }
    if (!uiViewRoot.isTransient()) {
        Object state = uiViewRoot.saveState(facesContext);
        if (state != null) {
            // Save by client ID into our map.
            states.put(uiViewRoot.getClientId(facesContext), state);
        }
    }
}
Also used : FacesContext(jakarta.faces.context.FacesContext) ViewEntry(org.apache.myfaces.view.facelets.pool.ViewEntry) VisitCallback(jakarta.faces.component.visit.VisitCallback) VisitContext(jakarta.faces.component.visit.VisitContext) UIComponent(jakarta.faces.component.UIComponent) VisitResult(jakarta.faces.component.visit.VisitResult)

Example 4 with VisitCallback

use of jakarta.faces.component.visit.VisitCallback in project myfaces by apache.

the class MockMyFacesClient method internalSubmit.

protected void internalSubmit(UICommand command) {
    if (command instanceof HtmlCommandButton) {
        final FacesContext facesContext = testCase.getFacesContext();
        UIForm form = getParentForm(command);
        VisitContext visitContext = VisitContext.createVisitContext(facesContext);
        form.visitTree(visitContext, new VisitCallback() {

            public VisitResult visit(VisitContext context, UIComponent target) {
                if (target instanceof UIInput) {
                    if (!parameters.containsKey(target.getClientId(facesContext))) {
                        parameters.put(target.getClientId(facesContext), RendererUtils.getStringValue(facesContext, target));
                    }
                }
                return VisitResult.ACCEPT;
            }
        });
        parameters.put(form.getClientId(facesContext) + "_SUBMIT", "1");
        Object value = command.getValue();
        parameters.put(command.getClientId(), value == null ? "" : value.toString());
        applyStateFromPreviousRequest();
    /*
            parameters.put(ResponseStateManager.VIEW_STATE_PARAM, 
                facesContext.getApplication().getStateManager().getViewState(facesContext));
            if (facesContext.getExternalContext().getClientWindow() != null)
            {
                parameters.put(ResponseStateManager.CLIENT_WINDOW_URL_PARAM, 
                facesContext.getExternalContext().getClientWindow().getId());
            }
            MockHttpServletResponse response = (MockHttpServletResponse) 
                facesContext.getExternalContext().getResponse(); 
            Cookie cookie = response.getCookie("oam.Flash.RENDERMAP.TOKEN");
            getCookies().put("oam.Flash.RENDERMAP.TOKEN", cookie);*/
    }
}
Also used : FacesContext(jakarta.faces.context.FacesContext) VisitCallback(jakarta.faces.component.visit.VisitCallback) VisitContext(jakarta.faces.component.visit.VisitContext) UIComponent(jakarta.faces.component.UIComponent) HtmlCommandButton(jakarta.faces.component.html.HtmlCommandButton) UIForm(jakarta.faces.component.UIForm) VisitResult(jakarta.faces.component.visit.VisitResult) UIInput(jakarta.faces.component.UIInput)

Example 5 with VisitCallback

use of jakarta.faces.component.visit.VisitCallback in project myfaces by apache.

the class UIDataTest method testVisitTree.

/**
 * Test method for
 * {@link jakarta.faces.component.UIData#visitTree(jakarta.faces.component.visit.VisitContext, jakarta.faces.component.visit.VisitCallback)}.
 */
public void testVisitTree() {
    UIData uidata = new UIData();
    // value
    Collection<String> value = new ArrayList<String>();
    value.add("value#1");
    value.add("value#2");
    uidata.setValue(value);
    // header facet
    UIComponent headerFacet = new HtmlPanelGroup();
    headerFacet.setId("headerFacet");
    uidata.setHeader(headerFacet);
    // footer facet
    UIComponent footerFacet = new HtmlPanelGroup();
    footerFacet.setId("footerFacet");
    uidata.setFooter(footerFacet);
    // first child
    UIComponent child1 = new UIColumn();
    // facet of first child
    UIComponent facetOfChild1 = new HtmlPanelGroup();
    child1.getFacets().put("someFacet", facetOfChild1);
    // child of first child
    UIOutput childOfChild1 = new UIOutput();
    childOfChild1.setId("childOfColumn");
    child1.getChildren().add(childOfChild1);
    uidata.getChildren().add(child1);
    // second child (should not be processed --> != UIColumn)
    UIComponent child2 = new HtmlPanelGroup();
    uidata.getChildren().add(child2);
    VisitCallback callback = null;
    IMocksControl control = EasyMock.createControl();
    VisitContext visitContextMock = control.createMock(VisitContext.class);
    EasyMock.expect(visitContextMock.getFacesContext()).andReturn(facesContext).anyTimes();
    EasyMock.expect(visitContextMock.getHints()).andReturn(Collections.<VisitHint>emptySet()).anyTimes();
    Collection<String> subtreeIdsToVisit = new ArrayList<String>();
    subtreeIdsToVisit.add("1");
    EasyMock.expect(visitContextMock.getSubtreeIdsToVisit(uidata)).andReturn(subtreeIdsToVisit);
    EasyMock.expect(visitContextMock.invokeVisitCallback(uidata, callback)).andReturn(VisitResult.ACCEPT);
    EasyMock.expect(visitContextMock.invokeVisitCallback(headerFacet, callback)).andReturn(VisitResult.ACCEPT);
    EasyMock.expect(visitContextMock.invokeVisitCallback(footerFacet, callback)).andReturn(VisitResult.ACCEPT);
    EasyMock.expect(visitContextMock.invokeVisitCallback(facetOfChild1, callback)).andReturn(VisitResult.ACCEPT);
    EasyMock.expect(visitContextMock.invokeVisitCallback(child1, callback)).andReturn(VisitResult.ACCEPT);
    EasyMock.expect(visitContextMock.invokeVisitCallback(childOfChild1, callback)).andReturn(VisitResult.ACCEPT).times(2);
    control.replay();
    uidata.visitTree(visitContextMock, callback);
    control.verify();
    // VisitHint.SKIP_ITERATION test:
    // (1) uiData with two rows - should iterate over row twice
    MockVisitContext mockVisitContext = new MockVisitContext(facesContext, null);
    CountingVisitCallback countingVisitCallback = new CountingVisitCallback(2);
    uidata.visitTree(mockVisitContext, countingVisitCallback);
    countingVisitCallback.verify();
    // (2) uiData with two values - should iterate over row ones - SKIP_ITERATION is used
    mockVisitContext = new MockVisitContext(facesContext, EnumSet.of(VisitHint.SKIP_ITERATION));
    countingVisitCallback = new CountingVisitCallback(1);
    uidata.visitTree(mockVisitContext, countingVisitCallback);
    countingVisitCallback.verify();
    // (3) uiData with five values - should iterate over row five times
    value = new ArrayList<String>();
    value.add("1");
    value.add("2");
    value.add("3");
    value.add("4");
    value.add("5");
    uidata.setValue(value);
    mockVisitContext = new MockVisitContext(facesContext, null);
    countingVisitCallback = new CountingVisitCallback(5);
    uidata.visitTree(mockVisitContext, countingVisitCallback);
    countingVisitCallback.verify();
    // (4) uiData with five values - should iterate over child ones - SKIP_ITERATION is used
    mockVisitContext = new MockVisitContext(facesContext, EnumSet.of(VisitHint.SKIP_ITERATION));
    countingVisitCallback = new CountingVisitCallback(1);
    uidata.visitTree(mockVisitContext, countingVisitCallback);
    countingVisitCallback.verify();
}
Also used : MockVisitContext(org.apache.myfaces.test.mock.visit.MockVisitContext) UIColumn(jakarta.faces.component.UIColumn) VisitCallback(jakarta.faces.component.visit.VisitCallback) VisitHint(jakarta.faces.component.visit.VisitHint) MockVisitContext(org.apache.myfaces.test.mock.visit.MockVisitContext) VisitContext(jakarta.faces.component.visit.VisitContext) ArrayList(java.util.ArrayList) UIComponent(jakarta.faces.component.UIComponent) UIData(jakarta.faces.component.UIData) IMocksControl(org.easymock.classextension.IMocksControl) UIOutput(jakarta.faces.component.UIOutput) HtmlPanelGroup(jakarta.faces.component.html.HtmlPanelGroup)

Aggregations

VisitCallback (jakarta.faces.component.visit.VisitCallback)5 VisitContext (jakarta.faces.component.visit.VisitContext)5 UIComponent (jakarta.faces.component.UIComponent)4 VisitResult (jakarta.faces.component.visit.VisitResult)4 FacesContext (jakarta.faces.context.FacesContext)3 ValueExpression (jakarta.el.ValueExpression)1 UIColumn (jakarta.faces.component.UIColumn)1 UIData (jakarta.faces.component.UIData)1 UIForm (jakarta.faces.component.UIForm)1 UIInput (jakarta.faces.component.UIInput)1 UIOutput (jakarta.faces.component.UIOutput)1 UISelectItem (jakarta.faces.component.UISelectItem)1 HtmlCommandButton (jakarta.faces.component.html.HtmlCommandButton)1 HtmlPanelGroup (jakarta.faces.component.html.HtmlPanelGroup)1 VisitHint (jakarta.faces.component.visit.VisitHint)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 SelectItemsIterator (org.apache.myfaces.core.api.shared.SelectItemsIterator)1 MockVisitContext (org.apache.myfaces.test.mock.visit.MockVisitContext)1 ViewEntry (org.apache.myfaces.view.facelets.pool.ViewEntry)1