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.component.UIComponent;
24  import javax.faces.context.FacesContext;
25  import javax.faces.context.ResponseWriter;
26  
27  import org.apache.myfaces.custom.tree.HtmlTreeImageCommandLink;
28  import org.apache.myfaces.custom.tree.HtmlTreeNode;
29  import org.apache.myfaces.renderkit.html.jsf.ExtendedHtmlLinkRenderer;
30  import org.apache.myfaces.renderkit.html.util.DummyFormUtils;
31  import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
32  import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRendererUtils;
33  
34  
35  /**
36   * @JSFRenderer
37   *   renderKitId = "HTML_BASIC" 
38   *   family = "org.apache.myfaces.HtmlTree"
39   *   type = "org.apache.myfaces.HtmlTreeImageCommandLink"
40   * 
41   * @author <a href="mailto:oliver@rossmueller.com">Oliver Rossmueller</a>
42   * @version $Revision: 659874 $ $Date: 2008-05-24 15:59:15 -0500 (Sat, 24 May 2008) $
43   */
44  public class HtmlTreeImageCommandLinkRenderer
45      extends ExtendedHtmlLinkRenderer {
46  
47      private static final Integer ZERO = new Integer(0);
48  
49  
50      public void decode(FacesContext facesContext, UIComponent component) {
51          super.decode(facesContext, component);
52          String clientId = component.getClientId(facesContext);
53          String reqValue = (String) facesContext
54              .getExternalContext()
55              .getRequestParameterMap().get(HtmlRendererUtils
56              .getHiddenCommandLinkFieldName(DummyFormUtils.findNestingForm(component, facesContext)));
57          if (reqValue != null && reqValue.equals(clientId)) {
58              HtmlTreeNode node = (HtmlTreeNode) component.getParent();
59  
60              node.toggleExpanded();
61          }
62      }
63  
64  
65      protected void renderCommandLinkStart(FacesContext facesContext,
66                                            UIComponent component,
67                                            String clientId,
68                                            Object value,
69                                            String style,
70                                            String styleClass) throws IOException {
71  
72          super.renderCommandLinkStart(facesContext, component, clientId, value, style, styleClass);
73  
74          String url = ((HtmlTreeImageCommandLink) component).getImage();
75  
76          if ((url != null) && (url.length() > 0)) {
77              ResponseWriter writer = facesContext.getResponseWriter();
78              writer.startElement(HTML.IMG_ELEM, component);
79              String src = facesContext.getApplication().getViewHandler().getResourceURL(facesContext, url);
80              //String src;
81              //if (url.startsWith(HTML.HREF_PATH_SEPARATOR))
82              //{
83              //    String path = facesContext.getExternalContext().getRequestContextPath();
84              //    src = path + url;
85              //}
86              //else
87              //{
88              //    src = url;
89              //}
90              //Encode URL
91              //Although this is an url url, encodeURL is no nonsense, because the
92              //actual url url could also be a dynamic servlet request:
93              //src = facesContext.getExternalContext().encodeResourceURL(src);
94              writer.writeAttribute(HTML.SRC_ATTR, src, null);
95              writer.writeAttribute(HTML.BORDER_ATTR, ZERO, null);
96  
97              HtmlRendererUtils.renderHTMLAttributes(writer, component, HTML.IMG_PASSTHROUGH_ATTRIBUTES);
98  
99              writer.endElement(HTML.IMG_ELEM);
100         }
101     }
102 }