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.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) { // application image
65        closedSource = ResourceManagerUtils.getImageWithPath(facesContext, imageUrl);
66      } else { // theme image
67        closedSource = ResourceManagerUtils.getImageWithPath(facesContext, CLOSED_FOLDER);
68      }
69      if (folder) {
70        if (imageUrl != null) { // application image
71          openSource = ResourceManagerUtils.getImageWithPath(facesContext,
72              ResourceUtils.addPostfixToFilename(imageUrl, "-open"), true);
73        } else { // theme image
74          openSource = ResourceManagerUtils.getImageWithPath(facesContext, OPEN_FOLDER);
75        }
76        source = expanded ? openSource : closedSource;
77      } else {
78        openSource = null;
79        if (imageUrl != null) { // application image
80          source = ResourceManagerUtils.getImageWithPath(facesContext,
81              ResourceUtils.addPostfixToFilename(imageUrl, "-leaf"), true);
82        } else { // theme image
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.SRC_OPEN, openSource, true);
101       writer.writeAttribute(DataAttributes.SRC_CLOSE, closedSource, true);
102     }
103     writer.writeAttribute(HtmlAttributes.ALT, "", false);
104     writer.endElement(HtmlElements.IMG);
105   }
106 }