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