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.UITreeNode;
23  import org.apache.myfaces.tobago.context.ResourceManagerUtils;
24  import org.apache.myfaces.tobago.context.UserAgent;
25  import org.apache.myfaces.tobago.internal.component.AbstractUIData;
26  import org.apache.myfaces.tobago.layout.Display;
27  import org.apache.myfaces.tobago.layout.Measure;
28  import org.apache.myfaces.tobago.renderkit.css.Classes;
29  import org.apache.myfaces.tobago.renderkit.css.Style;
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.util.VariableResolverUtils;
36  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
37  
38  import javax.faces.component.UIComponent;
39  import javax.faces.context.FacesContext;
40  import java.io.IOException;
41  
42  public class TreeMenuNodeRenderer extends TreeNodeRendererBase {
43  
44    @Override
45    public void encodeBegin(FacesContext facesContext, UIComponent component) throws IOException {
46  
47      final UITreeNode node = (UITreeNode) component;
48      final AbstractUIData data = ComponentUtils.findAncestor(node, AbstractUIData.class);
49  
50      final boolean dataRendersRowContainer = data.isRendersRowContainer();
51      final boolean folder = node.isFolder();
52      final String clientId = node.getClientId(facesContext);
53      final boolean ie6
54          = VariableResolverUtils.resolveClientProperties(facesContext).getUserAgent().equals(UserAgent.MSIE_6_0);
55      final String parentId = data.getRowParentClientId();
56      final boolean visible = data.isRowVisible();
57  
58      final TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
59  
60      writer.startElement(HtmlElements.DIV, null);
61      writer.writeIdAttribute(clientId);
62      writer.writeClassAttribute(Classes.create(node));
63      HtmlRendererUtils.writeDataAttributes(facesContext, writer, node);
64      if (parentId != null) {
65        writer.writeAttribute(DataAttributes.TREE_PARENT, parentId, false);
66      }
67  
68      // In the case of a sheet, we need not hiding the node, because the whole TR will be hidden.
69      if (!dataRendersRowContainer && !visible) {
70        Style style = new Style();
71        style.setDisplay(Display.NONE);
72        writer.writeStyleAttribute(style);
73      }
74  
75  
76      if (!folder && ie6) { // XXX IE6: without this hack, we can't click beside the label text. Why?
77        final String src = ResourceManagerUtils.getImageWithPath(facesContext, "image/1x1.gif");
78        writer.startElement(HtmlElements.IMG, null);
79        writer.writeClassAttribute(Classes.create(node, "icon"));
80        writer.writeAttribute(HtmlAttributes.SRC, src, false);
81        writer.writeAttribute(HtmlAttributes.ALT, "", false);
82        Style style = new Style();
83        style.setWidth(Measure.ZERO);
84        writer.writeStyleAttribute(style);
85        writer.endElement(HtmlElements.IMG);
86      }
87    }
88  
89    @Override
90    public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException {
91      final UITreeNode node = (UITreeNode) component;
92      final AbstractUIData data = ComponentUtils.findAncestor(node, AbstractUIData.class);
93      final int level = node.getLevel();
94      final boolean folder = node.isFolder();
95      final boolean expanded = folder && data.getExpandedState().isExpanded(node.getPath()) || level == 0;
96  
97      final TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
98  
99      if (folder) {
100       encodeIcon(facesContext, writer, expanded, node);
101     }
102     writer.endElement(HtmlElements.DIV);
103   }
104 
105   private void encodeIcon(FacesContext facesContext, TobagoResponseWriter writer, boolean expanded, UITreeNode node)
106       throws IOException {
107     final String srcOpen = ResourceManagerUtils.getImageWithPath(facesContext, "image/treeMenuOpen.gif");
108     final String srcClose = ResourceManagerUtils.getImageWithPath(facesContext, "image/treeMenuClose.gif");
109     final String src = expanded ? srcOpen : srcClose;
110     writer.startElement(HtmlElements.IMG, null);
111     writer.writeClassAttribute(Classes.create(node, "toggle"));
112     writer.writeAttribute(HtmlAttributes.SRC, src, false);
113     writer.writeAttribute(DataAttributes.SRC_OPEN, srcOpen, false);
114     writer.writeAttribute(DataAttributes.SRC_CLOSE, srcClose, false);
115     writer.writeAttribute(HtmlAttributes.ALT, "", false);
116     writer.endElement(HtmlElements.IMG);
117   }
118 
119 }