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  
20  package org.apache.myfaces.tobago.internal.renderkit.renderer;
21  
22  import org.apache.myfaces.tobago.internal.component.AbstractUILinks;
23  import org.apache.myfaces.tobago.layout.Orientation;
24  import org.apache.myfaces.tobago.renderkit.RendererBase;
25  import org.apache.myfaces.tobago.renderkit.css.BootstrapClass;
26  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
27  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
28  
29  import javax.faces.component.UIComponent;
30  import javax.faces.context.FacesContext;
31  import java.io.IOException;
32  
33  public class LinksRenderer<T extends AbstractUILinks> extends RendererBase<T> {
34  
35    @Override
36    public void encodeBeginInternal(final FacesContext facesContext, final T component) throws IOException {
37  
38  //    final boolean insideBar = ComponentUtils.findAncestor(component, AbstractUIBar.class) != null;
39  //    final boolean insideBar = facesContext.getAttributes().get("inside-bar") != null;
40      final boolean insideBar = isInside(facesContext, HtmlElements.TOBAGO_BAR);
41      final TobagoResponseWriter writer = getResponseWriter(facesContext);
42  
43      writer.startElement(HtmlElements.TOBAGO_LINKS);
44      writer.writeIdAttribute(component.getClientId(facesContext));
45      writer.writeClassAttribute(
46          Orientation.vertical.equals(component.getOrientation()) ? BootstrapClass.FLEX_COLUMN : null,
47          component.getCustomClass());
48      writer.startElement(HtmlElements.UL);
49      writer.writeClassAttribute(
50          insideBar ? BootstrapClass.NAVBAR_NAV : BootstrapClass.NAV);
51    }
52  
53    @Override
54    public boolean getRendersChildren() {
55      return true;
56    }
57  
58    @Override
59    public void encodeChildrenInternal(final FacesContext facesContext, final T component) throws IOException {
60      final TobagoResponseWriter writer = getResponseWriter(facesContext);
61  
62      insideBegin(facesContext, HtmlElements.TOBAGO_LINKS);
63      for (final UIComponent child : component.getChildren()) {
64        if (child.isRendered()) {
65          writer.startElement(HtmlElements.LI);
66          writer.writeClassAttribute(BootstrapClass.NAV_ITEM);
67          child.encodeAll(facesContext);
68          writer.endElement(HtmlElements.LI);
69        }
70      }
71      insideEnd(facesContext, HtmlElements.TOBAGO_LINKS);
72    }
73  
74    @Override
75    public void encodeEndInternal(final FacesContext facesContext, final T component) throws IOException {
76      final TobagoResponseWriter writer = getResponseWriter(facesContext);
77      writer.endElement(HtmlElements.UL);
78      writer.endElement(HtmlElements.TOBAGO_LINKS);
79    }
80  }