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.AbstractUIData;
23  import org.apache.myfaces.tobago.internal.component.AbstractUITreeIndent;
24  import org.apache.myfaces.tobago.internal.component.AbstractUITreeNodeBase;
25  import org.apache.myfaces.tobago.internal.util.HtmlRendererUtils;
26  import org.apache.myfaces.tobago.renderkit.RendererBase;
27  import org.apache.myfaces.tobago.renderkit.css.BootstrapClass;
28  import org.apache.myfaces.tobago.renderkit.css.Icons;
29  import org.apache.myfaces.tobago.renderkit.css.TobagoClass;
30  import org.apache.myfaces.tobago.renderkit.html.DataAttributes;
31  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
32  import org.apache.myfaces.tobago.util.ComponentUtils;
33  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
34  
35  import javax.faces.context.FacesContext;
36  import java.io.IOException;
37  
38  public class TreeIndentRenderer<T extends AbstractUITreeIndent> extends RendererBase<T> {
39  
40    @Override
41    public void encodeBeginInternal(final FacesContext facesContext, final T component) throws IOException {
42  
43      final AbstractUITreeNodeBase node = ComponentUtils.findAncestor(component, AbstractUITreeNodeBase.class);
44      final AbstractUIData data = ComponentUtils.findAncestor(component, AbstractUIData.class);
45  
46      if (node == null) {
47        throw new NullPointerException(
48            "No AbstractUITreeNodeBase as ancestor found from '" + component.getClientId() + "'");
49      }
50      if (data == null) {
51        throw new NullPointerException("No AbstractUIData as ancestor found from '" + component.getClientId() + "'");
52      }
53  
54      final boolean folder = node.isFolder();
55      final boolean showJunctions = component.isShowJunctions();
56      final boolean expanded = folder && data.getExpandedState().isExpanded(node.getPath());
57  
58      final TobagoResponseWriter writer = getResponseWriter(facesContext);
59  
60      writer.startElement(HtmlElements.SPAN);
61      writer.writeIdAttribute(component.getClientId(facesContext));
62      HtmlRendererUtils.writeDataAttributes(facesContext, writer, component);
63      writer.writeClassAttribute(
64          TobagoClass.TREE_NODE__TOGGLE,
65          !folder ? BootstrapClass.INVISIBLE : null,
66          component.getCustomClass());
67  
68      // encode tree junction
69      if (!showJunctions) {
70        return;
71      }
72      writer.startElement(HtmlElements.I);
73      if (folder) {
74        writer.writeClassAttribute(Icons.FA, expanded ? Icons.MINUS_SQUARE_O : Icons.PLUS_SQUARE_O);
75        writer.writeAttribute(DataAttributes.OPEN, Icons.MINUS_SQUARE_O.getName(), false);
76        writer.writeAttribute(DataAttributes.CLOSED, Icons.PLUS_SQUARE_O.getName(), false);
77      } else {
78        writer.writeClassAttribute(Icons.FA, Icons.SQUARE_O, BootstrapClass.INVISIBLE);
79      }
80      writer.endElement(HtmlElements.I);
81    }
82  
83    @Override
84    public void encodeEndInternal(final FacesContext facesContext, final T component) throws IOException {
85      final TobagoResponseWriter writer = getResponseWriter(facesContext);
86      writer.endElement(HtmlElements.SPAN);
87    }
88  
89  }