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.UITreeIcon;
23 import org.apache.myfaces.tobago.component.UITreeNode;
24 import org.apache.myfaces.tobago.context.Markup;
25 import org.apache.myfaces.tobago.context.ResourceManagerUtils;
26 import org.apache.myfaces.tobago.context.ResourceUtils;
27 import org.apache.myfaces.tobago.internal.component.AbstractUIData;
28 import org.apache.myfaces.tobago.renderkit.LayoutComponentRendererBase;
29 import org.apache.myfaces.tobago.renderkit.css.Classes;
30 import org.apache.myfaces.tobago.renderkit.html.DataAttributes;
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.HtmlRendererUtils;
34 import org.apache.myfaces.tobago.util.ComponentUtils;
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 TreeIconRenderer extends LayoutComponentRendererBase {
42
43 protected static final String OPEN_FOLDER
44 = ResourceUtils.createString("image", "treeNode", "icon", "open", ResourceUtils.GIF);
45 protected static final String CLOSED_FOLDER
46 = ResourceUtils.createString("image", "treeNode", "icon", ResourceUtils.GIF);
47 protected static final String LEAF
48 = ResourceUtils.createString("image", "treeNode", "icon", "leaf", ResourceUtils.GIF);
49
50 @Override
51 public void encodeBegin(FacesContext facesContext, UIComponent component) throws IOException {
52
53 final UITreeIcon image = (UITreeIcon) component;
54 final AbstractUIData data = ComponentUtils.findAncestor(image, AbstractUIData.class);
55 final UITreeNode node = ComponentUtils.findAncestor(image, UITreeNode.class);
56 final boolean folder = node.isFolder();
57 final boolean expanded = folder && data.getExpandedState().isExpanded(node.getPath());
58
59 String source;
60 final String openSource;
61 final String closedSource;
62
63 final String imageUrl = (String) image.getValue();
64 if (imageUrl != null) {
65 closedSource = ResourceManagerUtils.getImageWithPath(facesContext, imageUrl);
66 } else {
67 closedSource = ResourceManagerUtils.getImageWithPath(facesContext, CLOSED_FOLDER);
68 }
69 if (folder) {
70 if (imageUrl != null) {
71 openSource = ResourceManagerUtils.getImageWithPath(facesContext,
72 ResourceUtils.addPostfixToFilename(imageUrl, "-open"), true);
73 } else {
74 openSource = ResourceManagerUtils.getImageWithPath(facesContext, OPEN_FOLDER);
75 }
76 source = expanded ? openSource : closedSource;
77 } else {
78 openSource = null;
79 if (imageUrl != null) {
80 source = ResourceManagerUtils.getImageWithPath(facesContext,
81 ResourceUtils.addPostfixToFilename(imageUrl, "-leaf"), true);
82 } else {
83 source = ResourceManagerUtils.getImageWithPath(facesContext, LEAF);
84 }
85 }
86 if (source == null) {
87 source = closedSource;
88 }
89 if (source == null) {
90 source = openSource;
91 }
92
93 TobagoResponseWriter writer = HtmlRendererUtils.getTobagoResponseWriter(facesContext);
94
95 writer.startElement(HtmlElements.IMG, null);
96 writer.writeClassAttribute(Classes.create(node, "toggle", Markup.NULL));
97 HtmlRendererUtils.writeDataAttributes(facesContext, writer, image);
98 writer.writeAttribute(HtmlAttributes.SRC, source, true);
99 if (folder) {
100 writer.writeAttribute(DataAttributes.SRCOPEN, openSource, true);
101 writer.writeAttribute(DataAttributes.SRCCLOSE, closedSource, true);
102 }
103 writer.writeAttribute(HtmlAttributes.ALT, "", false);
104 writer.endElement(HtmlElements.IMG);
105 }
106 }