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.UICommand;
24 import org.apache.myfaces.tobago.context.ResourceManagerUtils;
25 import org.apache.myfaces.tobago.internal.component.AbstractUIImage;
26 import org.apache.myfaces.tobago.renderkit.LayoutComponentRendererBase;
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.HtmlAttributes;
30 import org.apache.myfaces.tobago.renderkit.html.HtmlElements;
31 import org.apache.myfaces.tobago.renderkit.html.util.HtmlRendererUtils;
32 import org.apache.myfaces.tobago.webapp.TobagoResponseWriter;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 import javax.faces.component.UIComponent;
37 import javax.faces.context.FacesContext;
38 import java.io.IOException;
39
40 public class ImageRenderer extends LayoutComponentRendererBase {
41
42 private static final Logger LOG = LoggerFactory.getLogger(ImageRenderer.class);
43
44 public void prepareRender(FacesContext facesContext, UIComponent component) throws IOException {
45 super.prepareRender(facesContext, component);
46 HtmlRendererUtils.renderDojoDndSource(facesContext, component);
47 }
48
49 public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException {
50
51 TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
52
53 AbstractUIImage image = (AbstractUIImage) component;
54 final String value = image.getUrl();
55 String src = value;
56 if (src != null) {
57 if (ResourceManagerUtils.isAbsoluteResource(src)) {
58
59 } else {
60 src = null;
61 if (isDisabled(image)) {
62 src = ResourceManagerUtils.getImageWithPath(facesContext,
63 HtmlRendererUtils.createSrc(value, "Disabled"), true);
64 }
65 if (src == null) {
66 src = ResourceManagerUtils.getImageWithPath(facesContext, value);
67 }
68 }
69 }
70
71 String border = (String) image.getAttributes().get(Attributes.BORDER);
72 if (border == null) {
73 border = "0";
74 }
75 String alt = (String) image.getAttributes().get(Attributes.ALT);
76 if (alt == null) {
77 alt = "";
78 }
79
80 writer.startElement(HtmlElements.IMG, image);
81 final String clientId = image.getClientId(facesContext);
82 writer.writeIdAttribute(clientId);
83 HtmlRendererUtils.writeDataAttributes(facesContext, writer, image);
84 if (src != null) {
85 writer.writeAttribute(HtmlAttributes.SRC, src, true);
86 }
87 writer.writeAttribute(HtmlAttributes.ALT, alt, true);
88 HtmlRendererUtils.renderTip(image, writer);
89 writer.writeAttribute(HtmlAttributes.BORDER, border, false);
90 Style style = new Style(facesContext, image);
91 writer.writeStyleAttribute(style);
92 HtmlRendererUtils.renderDojoDndItem(image, writer, true);
93 writer.writeClassAttribute(Classes.create(image));
94 writer.endElement(HtmlElements.IMG);
95 }
96
97 private String createSrc(String src, String ext) {
98 int dot = src.lastIndexOf('.');
99 if (dot == -1) {
100 LOG.warn("Image src without extension: '" + src + "'");
101 return src;
102 } else {
103 return src.substring(0, dot) + ext + src.substring(dot);
104 }
105 }
106
107 private boolean isDisabled(AbstractUIImage graphic) {
108 return graphic.isDisabled()
109 || (graphic.getParent() instanceof UICommand && ((UICommand) graphic.getParent()).isDisabled());
110 }
111 }