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.context.ResourceUtils;
23  import org.apache.myfaces.tobago.internal.component.AbstractUIData;
24  import org.apache.myfaces.tobago.internal.component.AbstractUITreeNode;
25  import org.apache.myfaces.tobago.layout.Display;
26  import org.apache.myfaces.tobago.layout.LayoutBase;
27  import org.apache.myfaces.tobago.renderkit.css.Classes;
28  import org.apache.myfaces.tobago.renderkit.css.Style;
29  import org.apache.myfaces.tobago.renderkit.html.DataAttributes;
30  import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
31  import org.apache.myfaces.tobago.renderkit.html.util.HtmlRendererUtils;
32  import org.apache.myfaces.tobago.util.ComponentUtils;
33  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
34  import org.slf4j.Logger;
35  import org.slf4j.LoggerFactory;
36  
37  import javax.faces.component.UIComponent;
38  import javax.faces.context.FacesContext;
39  import java.io.IOException;
40  
41  public class TreeNodeRenderer extends TreeNodeRendererBase {
42  
43    private static final Logger LOG = LoggerFactory.getLogger(TreeNodeRenderer.class);
44  
45    protected static final String OPEN_FOLDER
46        = ResourceUtils.createString("image", "treeNode", "icon", "open", ResourceUtils.GIF);
47    protected static final String CLOSED_FOLDER
48        = ResourceUtils.createString("image", "treeNode", "icon", ResourceUtils.GIF);
49    protected static final String LEAF
50        = ResourceUtils.createString("image", "treeNode", "icon", "leaf", ResourceUtils.GIF);
51  
52    @Override
53    public void encodeBegin(FacesContext facesContext, UIComponent component) throws IOException {
54  
55      final AbstractUITreeNode node = (AbstractUITreeNode) component;
56      final AbstractUIData data = ComponentUtils.findAncestor(node, AbstractUIData.class);
57  
58      final boolean dataRendersRowContainer = data.isRendersRowContainer();
59      final boolean folder = node.isFolder();
60      final String clientId = node.getClientId(facesContext);
61      final String parentId = data.getRowParentClientId();
62      final boolean visible = data.isRowVisible();
63  
64      final TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
65  
66      writer.startElement(HtmlElements.DIV, null);
67  
68      // div id
69      writer.writeIdAttribute(clientId);
70      if (!folder) {
71        HtmlRendererUtils.renderDojoDndItem(node, writer, true);
72      }
73      writer.writeClassAttribute(Classes.create(node));
74      HtmlRendererUtils.writeDataAttributes(facesContext, writer, node);
75      if (parentId != null) {
76        writer.writeAttribute(DataAttributes.TREE_PARENT, parentId, false);
77      }
78  
79      Style style = new Style();
80      // In the case of a sheet, we need not hiding the node, because the whole TR will be hidden.
81      if (!dataRendersRowContainer && !visible) {
82        style.setDisplay(Display.NONE);
83      }
84  
85      // div style (width)
86      Style widthStyle = new Style(facesContext, (LayoutBase) data);
87      if (widthStyle.getWidth() != null) {
88        style.setWidth(widthStyle.getWidth().subtract(22)); // fixme: 4 + 18 for scrollbar
89      }
90      writer.writeStyleAttribute(style);
91    }
92  
93    @Override
94    public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException {
95      final TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
96      writer.endElement(HtmlElements.DIV);
97    }
98  }