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.renderkit.html.ext;
20  
21  import java.io.IOException;
22  
23  import javax.faces.component.UIComponent;
24  import javax.faces.component.UIOutput;
25  import javax.faces.context.FacesContext;
26  import javax.faces.context.ResponseWriter;
27  
28  import org.apache.myfaces.component.UserRoleUtils;
29  import org.apache.myfaces.component.html.ext.HtmlCommandLink;
30  import org.apache.myfaces.renderkit.html.jsf.ExtendedHtmlLinkRenderer;
31  import org.apache.myfaces.shared_tomahawk.renderkit.JSFAttr;
32  import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
33  import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRendererUtils;
34  
35  /**
36   * @JSFRenderer
37   *   renderKitId = "HTML_BASIC"
38   *   family = "javax.faces.Command"
39   *   type = "org.apache.myfaces.Link" 
40   * 
41   * @author Manfred Geiler (latest modification by $Author: lu4242 $)
42   * @version $Revision: 951635 $ $Date: 2010-06-04 21:19:57 -0500 (Fri, 04 Jun 2010) $
43   */
44  public class HtmlLinkRenderer
45          extends ExtendedHtmlLinkRenderer
46  {
47      //private static final Log log = LogFactory.getLog(HtmlLinkRenderer.class);
48  
49      protected void renderCommandLinkStart(FacesContext facesContext,
50                                            UIComponent component,
51                                            String clientId,
52                                            Object value,
53                                            String style,
54                                            String styleClass) throws IOException
55      {
56          //if link is disabled we render the nested components without the anchor
57          if (UserRoleUtils.isEnabledOnUserRole(component) &&
58                          !((HtmlCommandLink) component).isDisabled() )
59          {
60              super.renderCommandLinkStart(facesContext, component, clientId, value, style, styleClass);
61          }
62          else
63          {
64              //For a disabled HtmlCommandLink we want to
65              //render a span-element instead of the Anchor
66  
67              //set disabledStyle if available
68              if(((HtmlCommandLink) component).getDisabledStyle() != null)
69              {
70                  style = ((HtmlCommandLink) component).getDisabledStyle();
71              }
72  
73              if(((HtmlCommandLink) component).getDisabledStyleClass() != null)
74              {
75                  styleClass = ((HtmlCommandLink) component).getDisabledStyleClass();
76              }
77  
78              renderSpanStart(facesContext, component, clientId, value, style, styleClass);
79              /*// render value as required by JSF 1.1 renderkitdocs
80              if(value != null)
81              {
82                  ResponseWriter writer = facesContext.getResponseWriter();
83  
84                  writer.writeText(value.toString(), JSFAttr.VALUE_ATTR);
85              }*/
86          }
87      }
88  
89      protected void renderOutputLinkStart(FacesContext facesContext, UIOutput output) throws IOException
90      {
91          //if link is disabled we render the nested components without the anchor
92          if (UserRoleUtils.isEnabledOnUserRole(output))
93          {
94              super.renderOutputLinkStart(facesContext, output);
95          }
96      }
97  
98      protected void renderOutputLinkEnd(FacesContext facesContext,
99              UIComponent component) throws IOException
100     {
101         if (UserRoleUtils.isEnabledOnUserRole(component))
102         {
103             super.renderOutputLinkEnd(facesContext, component);
104         }
105     }
106 
107     protected void renderCommandLinkEnd(FacesContext facesContext, UIComponent component) throws IOException
108     {
109         //if link is disabled we render the nested components without the anchor
110         if (UserRoleUtils.isEnabledOnUserRole(component) &&
111                         !((HtmlCommandLink) component).isDisabled() )
112         {
113             super.renderCommandLinkEnd(facesContext, component);
114         }
115         else
116         {
117             renderSpanEnd(facesContext, component);
118         }
119     }
120 
121     protected void renderSpanStart(FacesContext facesContext,
122                                           UIComponent component,
123                                           String clientId,
124                                           Object value,
125                                           String style,
126                                           String styleClass) throws IOException
127     {
128     ResponseWriter writer = facesContext.getResponseWriter();
129 
130         String[] spanAttrsToRender;
131 
132         writer.startElement(HTML.SPAN_ELEM, component);
133 
134         spanAttrsToRender = HTML.COMMON_PASSTROUGH_ATTRIBUTES_WITHOUT_STYLE;
135 
136         HtmlRendererUtils.renderHTMLAttribute(writer, HTML.ID_ATTR, HTML.ID_ATTR, clientId);
137 
138         HtmlRendererUtils.renderHTMLAttributes(writer, component,
139                                                spanAttrsToRender);
140         HtmlRendererUtils.renderHTMLAttribute(writer, HTML.STYLE_ATTR, HTML.STYLE_ATTR,
141                                               style);
142         HtmlRendererUtils.renderHTMLAttribute(writer, HTML.STYLE_CLASS_ATTR, HTML.STYLE_CLASS_ATTR,
143                                               styleClass);
144 
145         // render value as required by JSF 1.1 renderkitdocs
146         if(value != null)
147         {
148             writer.writeText(value.toString(), JSFAttr.VALUE_ATTR);
149         }
150     }
151 
152     protected void renderSpanEnd(FacesContext facesContext, UIComponent component) throws IOException
153     {
154         ResponseWriter writer = facesContext.getResponseWriter();
155         // force separate end tag
156         writer.writeText("", null);
157         writer.endElement(HTML.SPAN_ELEM);
158     }
159 }