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.Attributes;
23  import org.apache.myfaces.tobago.component.UITreeCommand;
24  import org.apache.myfaces.tobago.component.UITreeNode;
25  import org.apache.myfaces.tobago.internal.component.AbstractUICommand;
26  import org.apache.myfaces.tobago.internal.util.AccessKeyMap;
27  import org.apache.myfaces.tobago.renderkit.CommandRendererBase;
28  import org.apache.myfaces.tobago.renderkit.LabelWithAccessKey;
29  import org.apache.myfaces.tobago.renderkit.css.Classes;
30  import org.apache.myfaces.tobago.renderkit.css.Style;
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.CommandRendererHelper;
34  import org.apache.myfaces.tobago.renderkit.html.util.HtmlRendererUtils;
35  import org.apache.myfaces.tobago.util.ComponentUtils;
36  import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
37  import org.slf4j.Logger;
38  import org.slf4j.LoggerFactory;
39  
40  import javax.faces.component.UIComponent;
41  import javax.faces.context.FacesContext;
42  import javax.faces.context.ResponseWriter;
43  import java.io.IOException;
44  
45  public class TreeCommandRenderer extends CommandRendererBase {
46  
47    private static final Logger LOG = LoggerFactory.getLogger(TreeCommandRenderer.class);
48  
49    @Override
50    public void prepareRender(FacesContext facesContext, UIComponent component) throws IOException {
51      final UITreeCommand command = (UITreeCommand) component;
52      final UITreeNode node = ComponentUtils.findAncestor(command, UITreeNode.class);
53      // Todo: use an expression?
54      command.setDisabled(node.isDisabled());
55      super.prepareRender(facesContext, component);
56    }
57  
58    @Override
59    public void encodeBegin(FacesContext facesContext, UIComponent component) throws IOException {
60  
61      UITreeCommand command = (UITreeCommand) component;
62      String clientId = command.getClientId(facesContext);
63      CommandRendererHelper helper = new CommandRendererHelper(facesContext, command, CommandRendererHelper.Tag.ANCHOR);
64      String href = helper.getHref();
65      TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
66  
67      LabelWithAccessKey label = new LabelWithAccessKey(command);
68  
69      if (helper.isDisabled()) {
70        writer.startElement(HtmlElements.SPAN, command);
71      } else {
72        writer.startElement(HtmlElements.A, command);
73        writer.writeAttribute(HtmlAttributes.HREF, href, true);
74        if (helper.getOnclick() != null) {
75          writer.writeAttribute(HtmlAttributes.ONCLICK, helper.getOnclick(), true);
76        }
77        if (helper.getTarget() != null) {
78          writer.writeAttribute(HtmlAttributes.TARGET, helper.getTarget(), true);
79        }
80        writer.writeNameAttribute(clientId);
81      }
82      writer.writeStyleAttribute(createStyle(facesContext, command));
83      writer.writeClassAttribute(Classes.create(command));
84      writer.writeIdAttribute(clientId);
85      HtmlRendererUtils.writeDataAttributes(facesContext, writer, command);
86      if (label.getAccessKey() != null) {
87        writer.writeAttribute(HtmlAttributes.ACCESSKEY, Character.toString(label.getAccessKey()), false);
88      }
89      HtmlRendererUtils.renderTip(command, writer);
90      writer.flush();
91  
92  //  label
93      if (label.getText() != null) {
94        HtmlRendererUtils.writeLabelWithAccessKey(writer, label);
95      }
96  
97      if (label.getAccessKey() != null) {
98        if (LOG.isInfoEnabled()
99            && !AccessKeyMap.addAccessKey(facesContext, label.getAccessKey())) {
100         LOG.info("duplicated accessKey : " + label.getAccessKey());
101       }
102 
103       HtmlRendererUtils.addClickAcceleratorKey(facesContext, clientId, label.getAccessKey());
104     }
105   }
106 
107   protected Style createStyle(FacesContext facesContext, AbstractUICommand link) {
108     return new Style(facesContext, link);
109   }
110 
111   @Override
112   public void encodeEnd(FacesContext facesContext, UIComponent component)
113       throws IOException {
114     ResponseWriter writer = facesContext.getResponseWriter();
115     if (ComponentUtils.getBooleanAttribute(component, Attributes.DISABLED)) {
116       writer.endElement(HtmlElements.SPAN);
117     } else {
118       writer.endElement(HtmlElements.A);
119     }
120   }
121 }