1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
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 }