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