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.custom.tree2;
20  
21  import java.util.Map;
22  
23  import javax.faces.component.UIComponent;
24  import javax.faces.component.UIGraphic;
25  import javax.faces.component.UIViewRoot;
26  import javax.faces.view.facelets.ComponentConfig;
27  import javax.faces.view.facelets.ComponentHandler;
28  import javax.faces.view.facelets.FaceletContext;
29  
30  /**
31   * 
32   * @since 1.1.10
33   * @author Leonardo Uribe
34   */
35  public class HtmlTreeTagHandler extends ComponentHandler
36  {
37      private static final String IMAGE_PREFIX = "t2";
38      private static final String NODE_STATE_EXPANDED = "x";
39      private static final String NODE_STATE_CLOSED = "c";
40      
41      public HtmlTreeTagHandler(ComponentConfig config)
42      {
43          super(config);
44      }
45  
46      @Override
47      public void onComponentPopulated(FaceletContext ctx, UIComponent c,
48              UIComponent parent)
49      {
50          if (ComponentHandler.isNew(c))
51          {
52              // t:tree2 requires to force id rendering of UIGraphic images on "expand" and "collapse" facets for
53              // a nodeTypeFacet. Long time ago, this was done on HtmlTreeRenderer.encodeNavigation, but that
54              // hack will not work well with jsf 2 PSS, because it requires ids to be generated in a "stable" way,
55              // and that hack used a counter to generate it.
56              for (Map.Entry<String, UIComponent> entry : c.getFacets().entrySet())
57              {
58                  UIComponent nodeTypeFacet = entry.getValue();
59                  UIComponent expandFacet = nodeTypeFacet.getFacet("expand");
60                  if (expandFacet != null)
61                  {
62                      //if (! (expandFacet instanceof UIGraphic))
63                      //{
64                      //    expandFacet = expandFacet.getChildren().get(0);
65                      //    nodeTypeFacet.getFacets().put("expand", expandFacet);
66                      //}
67                      UIGraphic expandImg = (UIGraphic)expandFacet;
68                      String id = expandImg.getId();
69                      if (id.startsWith(UIViewRoot.UNIQUE_ID_PREFIX))
70                      {
71                          expandImg.setId(IMAGE_PREFIX + id.substring(UIViewRoot.UNIQUE_ID_PREFIX.length()) + NODE_STATE_EXPANDED);
72                      }
73                      
74                  }
75                  
76                  UIComponent collapseFacet = nodeTypeFacet.getFacet("collapse");
77                  if (collapseFacet != null)
78                  {
79                      //if (! (collapseFacet instanceof UIGraphic))
80                      //{
81                      //    collapseFacet = collapseFacet.getChildren().get(0);
82                      //    nodeTypeFacet.getFacets().put("collapse", collapseFacet);
83                      //}
84                      UIGraphic collapseImg = (UIGraphic)collapseFacet;
85                      String id = collapseImg.getId();
86                      if (id.startsWith(UIViewRoot.UNIQUE_ID_PREFIX))
87                      {
88                          collapseImg.setId(IMAGE_PREFIX + id.substring(UIViewRoot.UNIQUE_ID_PREFIX.length()) + NODE_STATE_CLOSED);
89                      }
90                  }
91              }
92          }
93      }
94  }