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  package org.apache.myfaces.custom.tree.renderkit.html;
20  
21  import java.io.IOException;
22  
23  import javax.faces.application.Resource;
24  import javax.faces.application.ResourceHandler;
25  import javax.faces.component.UIComponent;
26  import javax.faces.context.FacesContext;
27  import javax.faces.context.ResponseWriter;
28  
29  import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFRenderer;
30  import org.apache.myfaces.custom.tree.HtmlTreeImageCommandLink;
31  import org.apache.myfaces.custom.tree.HtmlTreeNode;
32  import org.apache.myfaces.renderkit.html.jsf.ExtendedHtmlLinkRenderer;
33  import org.apache.myfaces.renderkit.html.util.DummyFormUtils;
34  import org.apache.myfaces.shared_tomahawk.renderkit.JSFAttr;
35  import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
36  import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRendererUtils;
37  
38  
39  /**
40   * 
41   * @author <a href="mailto:oliver@rossmueller.com">Oliver Rossmueller</a>
42   * @version $Revision: 659874 $ $Date: 2008-05-24 15:59:15 -0500 (sáb, 24 may 2008) $
43   */
44  @JSFRenderer(
45     renderKitId = "HTML_BASIC", 
46     family = "org.apache.myfaces.HtmlTree",
47     type = "org.apache.myfaces.HtmlTreeImageCommandLink")
48  public class HtmlTreeImageCommandLinkRenderer
49      extends ExtendedHtmlLinkRenderer {
50  
51      private static final Integer ZERO = new Integer(0);
52  
53  
54      public void decode(FacesContext facesContext, UIComponent component) {
55          super.decode(facesContext, component);
56          String clientId = component.getClientId(facesContext);
57          String reqValue = (String) facesContext
58              .getExternalContext()
59              .getRequestParameterMap().get(HtmlRendererUtils
60              .getHiddenCommandLinkFieldName(DummyFormUtils.findNestingForm(component, facesContext)));
61          if (reqValue != null && reqValue.equals(clientId)) {
62              HtmlTreeNode node = (HtmlTreeNode) component.getParent();
63  
64              node.toggleExpanded();
65          }
66      }
67  
68  
69      protected void renderCommandLinkStart(FacesContext facesContext,
70                                            UIComponent component,
71                                            String clientId,
72                                            Object value,
73                                            String style,
74                                            String styleClass) throws IOException {
75  
76          super.renderCommandLinkStart(facesContext, component, clientId, value, style, styleClass);
77  
78          String url = ((HtmlTreeImageCommandLink) component).getImage();
79          String libraryName = (String) component.getAttributes().get(JSFAttr.LIBRARY_ATTR);
80          String resourceName = (String) component.getAttributes().get(JSFAttr.NAME_ATTR);
81  
82          if ((url != null) && (url.length() > 0)) {
83              ResponseWriter writer = facesContext.getResponseWriter();
84              writer.startElement(HTML.IMG_ELEM, component);
85              String src = facesContext.getApplication().getViewHandler().getResourceURL(facesContext, url);
86              //String src;
87              //if (url.startsWith(HTML.HREF_PATH_SEPARATOR))
88              //{
89              //    String path = facesContext.getExternalContext().getRequestContextPath();
90              //    src = path + url;
91              //}
92              //else
93              //{
94              //    src = url;
95              //}
96              //Encode URL
97              //Although this is an url url, encodeURL is no nonsense, because the
98              //actual url url could also be a dynamic servlet request:
99              //src = facesContext.getExternalContext().encodeResourceURL(src);
100             writer.writeAttribute(HTML.SRC_ATTR, src, null);
101             writer.writeAttribute(HTML.BORDER_ATTR, ZERO, null);
102 
103             HtmlRendererUtils.renderHTMLAttributes(writer, component, HTML.IMG_PASSTHROUGH_ATTRIBUTES);
104 
105             writer.endElement(HTML.IMG_ELEM);
106         }
107         else if ((resourceName != null) && (resourceName.length() > 0))
108         {
109             ResponseWriter writer = facesContext.getResponseWriter();
110             writer.startElement(HTML.IMG_ELEM, component);
111             ResourceHandler resourceHandler = facesContext.getApplication().getResourceHandler();
112             Resource resource = null;
113             if ((libraryName != null) && (libraryName.length() > 0))
114             {
115                 resource = resourceHandler.createResource(resourceName, libraryName);
116             }
117             else
118             {
119                 resource = resourceHandler.createResource(resourceName);    
120             }
121             writer.writeAttribute(HTML.SRC_ATTR, resource.getRequestPath(), null);
122             writer.writeAttribute(HTML.BORDER_ATTR, ZERO, null);
123 
124             HtmlRendererUtils.renderHTMLAttributes(writer, component, HTML.IMG_PASSTHROUGH_ATTRIBUTES);
125 
126             writer.endElement(HTML.IMG_ELEM);
127         }
128     }
129 }