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.renderkit.html.scarborough.standard.tag;
21  
22  import org.apache.myfaces.tobago.component.UITree;
23  import org.apache.myfaces.tobago.component.UITreeIndent;
24  import org.apache.myfaces.tobago.context.Markup;
25  import org.apache.myfaces.tobago.context.ResourceManagerUtils;
26  import org.apache.myfaces.tobago.internal.component.AbstractUIData;
27  import org.apache.myfaces.tobago.internal.component.AbstractUITreeNode;
28  import org.apache.myfaces.tobago.renderkit.LayoutComponentRendererBase;
29  import org.apache.myfaces.tobago.renderkit.css.Classes;
30  import org.apache.myfaces.tobago.renderkit.html.DataAttributes;
31  import org.apache.myfaces.tobago.renderkit.html.HtmlAttributes;
32  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
33  import org.apache.myfaces.tobago.renderkit.html.util.HtmlRendererUtils;
34  import org.apache.myfaces.tobago.util.ComponentUtils;
35  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
36  
37  import javax.faces.component.UIComponent;
38  import javax.faces.context.FacesContext;
39  import java.io.IOException;
40  import java.util.List;
41  
42  public class TreeIndentRenderer extends LayoutComponentRendererBase {
43  
44    @Override
45    public void encodeBegin(FacesContext facesContext, UIComponent component) throws IOException {
46  
47      final UITreeIndent indent = (UITreeIndent) component;
48      final AbstractUITreeNode node = ComponentUtils.findAncestor(indent, AbstractUITreeNode.class);
49      final AbstractUIData data = ComponentUtils.findAncestor(indent, AbstractUIData.class);
50  
51      final boolean folder = node.isFolder();
52      final int level = node.getLevel();
53      final List<Boolean> junctions = node.getJunctions();
54  
55      final boolean showRoot = data.isShowRoot();
56      final boolean showJunctions = indent.isShowJunctions();
57      final boolean showRootJunction = data.isShowRootJunction();
58      final boolean expanded = folder && data.getExpandedState().isExpanded(node.getPath());
59      final boolean showLines = showJunctions && data instanceof UITree; // sheet should not show lines
60      final boolean showIcons = showJunctions;
61  
62      final TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
63  
64      writer.startElement(HtmlElements.SPAN, indent);
65      writer.writeIdAttribute(indent.getClientId(facesContext));
66      HtmlRendererUtils.writeDataAttributes(facesContext, writer, indent);
67  
68      encodeIndent(
69          facesContext, writer, node, showLines, showIcons, showRootJunction, showRoot, junctions);
70  
71      encodeTreeJunction(
72          facesContext, writer, node, showLines, showIcons, showRootJunction, junctions, expanded, folder, level == 0);
73    }
74  
75    @Override
76    public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException {
77      final TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
78      writer.endElement(HtmlElements.SPAN);
79    }
80  
81    private void encodeIndent(
82        final FacesContext facesContext, final TobagoResponseWriter writer, final AbstractUITreeNode node,
83        final boolean showLines, final boolean showIcons, final boolean showRootJunction, final boolean showRoot,
84        final List<Boolean> junctions)
85        throws IOException {
86  
87      final boolean dropFirst = !showRoot || !showRootJunction && (showLines || showIcons);
88      final String blank = ResourceManagerUtils.getImageWithPath(facesContext, "image/blank.gif");
89      final String perpendicular = ResourceManagerUtils.getImageWithPath(facesContext, "image/I.gif");
90  
91      for (int i = dropFirst ? 1 : 0; i < junctions.size() - 1; i++) {
92        Boolean junction = junctions.get(i);
93        writer.startElement(HtmlElements.IMG, null);
94        writer.writeClassAttribute(Classes.create(node, "junction"));
95        writer.writeAttribute(HtmlAttributes.ALT, "", false);
96        if (junction && showLines) {
97          writer.writeAttribute("src", perpendicular, true);
98        } else {
99          writer.writeAttribute("src", blank, true);
100       }
101       writer.endElement(HtmlElements.IMG);
102     }
103   }
104 
105   private void encodeTreeJunction(
106       final FacesContext facesContext, final TobagoResponseWriter writer, final AbstractUITreeNode node,
107       final boolean showLines, final boolean showIcons, final boolean showRootJunction, final List<Boolean> junctions,
108       final boolean expanded, final boolean folder, final boolean root)
109       throws IOException {
110     if (!showIcons || !showRootJunction && root) {
111       return;
112     }
113     final boolean hasNextSibling = junctions.get(junctions.size() - 1); // last element
114     writer.startElement(HtmlElements.IMG, null);
115     writer.writeClassAttribute(Classes.create(node, "toggle", Markup.NULL));
116 
117     final String open;
118     final String close;
119     if (showLines) {
120       if (root) {
121         open = "Rminus.gif";
122         close = "Rplus.gif";
123       } else {
124         if (hasNextSibling) {
125           if (folder) {
126             open = "Tminus.gif";
127             close = "Tplus.gif";
128           } else {
129             open = "T.gif";
130             close = "T.gif";
131           }
132         } else {
133           if (folder) {
134             open = "Lminus.gif";
135             close = "Lplus.gif";
136           } else {
137             open = "L.gif";
138             close = "L.gif";
139           }
140         }
141       }
142     } else {
143       if (folder) {
144         open = "minus.gif";
145         close = "plus.gif";
146       } else {
147         open = "blank.gif";
148         close = "blank.gif";
149       }
150     }
151 
152     final String srcOpen = ResourceManagerUtils.getImageWithPath(facesContext, "image/" + open);
153     final String srcClose = ResourceManagerUtils.getImageWithPath(facesContext, "image/" + close);
154     final String src = expanded ? srcOpen : srcClose;
155     writer.writeAttribute(HtmlAttributes.SRC, src, true);
156     if (folder) {
157       writer.writeAttribute(DataAttributes.SRC_OPEN, srcOpen, true);
158       writer.writeAttribute(DataAttributes.SRC_CLOSE, srcClose, true);
159     }
160     writer.writeAttribute(HtmlAttributes.ALT, "", false);
161     writer.endElement(HtmlElements.IMG);
162   }
163 
164 }