View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.trinidad.component.visit;
20  
21  import java.util.Collection;
22  import java.util.Collections;
23  import java.util.EnumSet;
24  import java.util.Set;
25  
26  import javax.faces.component.visit.VisitCallback;
27  import javax.faces.component.visit.VisitContext;
28  import javax.faces.component.visit.VisitHint;
29  import javax.faces.context.FacesContext;
30  
31  import org.apache.myfaces.trinidad.component.UIXComponent;
32  import org.apache.myfaces.trinidad.context.RequestContext;
33  
34  
35  /**
36   * Utility methods to make usage of the visit tree functionality more user friendly.
37   */
38  public final class VisitTreeUtils
39  {
40    /**
41     * Set of {@link VisitHint#SKIP_UNRENDERED} and {@link VisitHint#SKIP_TRANSIENT} provided
42     * for convenience as it is a common use case.
43     */
44    public static final Set<VisitHint> NON_TRANSIENT_RENDERED_HINTS =
45      EnumSet.of(VisitHint.SKIP_UNRENDERED, VisitHint.SKIP_TRANSIENT);
46  
47    private VisitTreeUtils() {}
48  
49    /**
50     * <p>Creates a VisitContext instance for use with
51     * {@link javax.faces.component.UIComponent#visitTree}.</p>
52     *
53     * @param context the FacesContext for the current request
54     * @param ids the client ids of the components to visit.  If null,
55     *   all components will be visited.
56     * @param hints the VisitHints to apply to the visit
57     * @return a VisitContext instance that is initialized with the
58     *   specified ids and hints.
59     * @deprecated Method is no longer needed and duplicates the functionality provided by
60     *   {@link VisitContext#createVisitContext(FacesContext, Collection<String>, Set<VisitHint>)}
61     */
62    @Deprecated
63    public static VisitContext createVisitContext(
64      FacesContext context,
65      Collection<String> ids,
66      Set<VisitHint> hints)
67    {
68      return VisitContext.createVisitContext(context, ids, hints);
69    }
70  
71    /**
72     * Visit a single component in the component tree starting from the view root.
73     * <p>Method assumes the {@link RequestContext} is available and a view root must be set on the faces context.</p>
74     *
75     * @param facesContext the faces context
76     * @param clientId the client ID of the component to visit
77     * @param visitCallback the callback to be invoked if the component is found
78     * @return true if a component was visited
79     */
80    public static boolean visitSingleComponent(
81      FacesContext  facesContext,
82      String        clientId,
83      VisitCallback visitCallback)
84    {
85      VisitContext visitContext = VisitContext.createVisitContext(facesContext,
86                                    Collections.singleton(clientId), null);
87      return UIXComponent.visitTree(visitContext, facesContext.getViewRoot(), visitCallback);
88    }
89  }