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.shared.renderkit.html;
20  
21  import java.io.IOException;
22  import java.util.List;
23  import java.util.Map;
24  
25  import javax.faces.component.UIComponent;
26  import javax.faces.component.UIOutcomeTarget;
27  import javax.faces.component.behavior.ClientBehavior;
28  import javax.faces.component.behavior.ClientBehaviorHolder;
29  import javax.faces.component.html.HtmlOutcomeTargetButton;
30  import javax.faces.context.ExternalContext;
31  import javax.faces.context.FacesContext;
32  import javax.faces.context.ResponseWriter;
33  
34  import org.apache.myfaces.shared.renderkit.ClientBehaviorEvents;
35  import org.apache.myfaces.shared.renderkit.JSFAttr;
36  import org.apache.myfaces.shared.renderkit.RendererUtils;
37  import org.apache.myfaces.shared.renderkit.html.util.ResourceUtils;
38  
39  /**
40   * @since 2.0
41   */
42  public class HtmlOutcomeTargetButtonRendererBase extends HtmlRenderer
43  {
44  
45      public boolean getRendersChildren()
46      {
47          return true;
48      }
49  
50      public void encodeBegin(FacesContext facesContext, UIComponent uiComponent)
51              throws IOException
52      {
53          super.encodeBegin(facesContext, uiComponent); //check for NP
54  
55          String clientId = uiComponent.getClientId(facesContext);
56  
57          ResponseWriter writer = facesContext.getResponseWriter();
58  
59          Map<String, List<ClientBehavior>> behaviors = null;
60          if (uiComponent instanceof ClientBehaviorHolder)
61          {
62              behaviors = ((ClientBehaviorHolder) uiComponent).getClientBehaviors();
63              if (!behaviors.isEmpty())
64              {
65                  ResourceUtils.renderDefaultJsfJsInlineIfNecessary(facesContext, facesContext.getResponseWriter());
66              }
67          }
68  
69          writer.startElement(HTML.INPUT_ELEM, uiComponent);
70  
71          writer.writeAttribute(HTML.ID_ATTR, clientId, JSFAttr.ID_ATTR);
72          writer.writeAttribute(HTML.NAME_ATTR, clientId, JSFAttr.ID_ATTR);
73  
74          String image = getImage(uiComponent);
75          ExternalContext externalContext = facesContext.getExternalContext();
76  
77          if (image != null)
78          {
79              // type="image"
80              writer.writeAttribute(HTML.TYPE_ATTR, HTML.INPUT_TYPE_IMAGE, JSFAttr.TYPE_ATTR);
81              String src = facesContext.getApplication().getViewHandler().getResourceURL(facesContext, image);
82              writer.writeURIAttribute(HTML.SRC_ATTR, externalContext.encodeResourceURL(src), JSFAttr.IMAGE_ATTR);
83          }
84          else
85          {
86              // type="button"
87              writer.writeAttribute(HTML.TYPE_ATTR, HTML.INPUT_TYPE_BUTTON, JSFAttr.TYPE_ATTR);
88              Object value = RendererUtils.getStringValue(facesContext, uiComponent);
89              if (value != null)
90              {
91                  writer.writeAttribute(HTML.VALUE_ATTR, value, JSFAttr.VALUE_ATTR);
92              }
93          }
94  
95          String outcomeTargetHref = HtmlRendererUtils.getOutcomeTargetHref(
96                      facesContext, (UIOutcomeTarget) uiComponent);
97  
98          if (HtmlRendererUtils.isDisabled(uiComponent) || outcomeTargetHref == null)
99          {
100             // disable the button - if disabled is true or no fitting NavigationCase was found
101             HtmlRendererUtils.renderHTMLAttribute(writer, HTML.DISABLED_ATTR, HTML.DISABLED_ATTR, true);
102         }
103         else
104         {
105             // render onClick attribute
106             String href = facesContext.getExternalContext().encodeResourceURL(outcomeTargetHref);
107 
108             String commandOnClick = (String) uiComponent.getAttributes().get(HTML.ONCLICK_ATTR);
109             String navigationJavaScript = "window.location.href = '" + href + "'";
110 
111             if (behaviors != null && !behaviors.isEmpty())
112             {
113                 HtmlRendererUtils.renderBehaviorizedAttribute(facesContext, writer, HTML.ONCLICK_ATTR, 
114                         uiComponent, ClientBehaviorEvents.CLICK, null, behaviors, HTML.ONCLICK_ATTR, 
115                         commandOnClick, navigationJavaScript);
116             }
117             else
118             {
119                 StringBuilder onClick = new StringBuilder();
120     
121                 if (commandOnClick != null)
122                 {
123                     onClick.append(commandOnClick);
124                     onClick.append(';');
125                 }
126                 onClick.append(navigationJavaScript);
127     
128                 writer.writeAttribute(HTML.ONCLICK_ATTR, onClick.toString(), null);
129             }
130         }
131 
132         if (isCommonPropertiesOptimizationEnabled(facesContext))
133         {
134             long commonPropertiesMarked = CommonPropertyUtils.getCommonPropertiesMarked(uiComponent);
135             
136             if (behaviors != null && !behaviors.isEmpty() && uiComponent instanceof ClientBehaviorHolder)
137             {
138                 HtmlRendererUtils.renderBehaviorizedEventHandlersWithoutOnclick(
139                         facesContext, writer, uiComponent, behaviors);
140                 HtmlRendererUtils.renderBehaviorizedFieldEventHandlersWithoutOnchangeAndOnselect(
141                         facesContext, writer, uiComponent, behaviors);
142             }
143             else
144             {
145                 CommonPropertyUtils.renderEventPropertiesWithoutOnclick(
146                         writer, commonPropertiesMarked, uiComponent);
147                 CommonPropertyUtils.renderFocusBlurEventProperties(writer, commonPropertiesMarked, uiComponent);
148             }
149             
150             CommonPropertyUtils.renderCommonFieldPassthroughPropertiesWithoutDisabledAndEvents(
151                     writer, commonPropertiesMarked, uiComponent);
152             if ((commonPropertiesMarked & CommonPropertyConstants.ALT_PROP) != 0)
153             {
154                 HtmlRendererUtils.renderHTMLStringAttribute(writer, uiComponent,
155                         HTML.ALT_ATTR, HTML.ALT_ATTR);
156             }
157         }
158         else
159         {
160             if (uiComponent instanceof ClientBehaviorHolder)
161             {
162                 HtmlRendererUtils.renderBehaviorizedEventHandlersWithoutOnclick(
163                         facesContext, writer, uiComponent, behaviors);
164                 HtmlRendererUtils.renderBehaviorizedFieldEventHandlersWithoutOnchangeAndOnselect(
165                         facesContext, writer, uiComponent, behaviors);
166             }
167             else
168             {
169                 HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent,
170                         HTML.EVENT_HANDLER_ATTRIBUTES_WITHOUT_ONCLICK);
171                 HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent,
172                         HTML.COMMON_FIELD_EVENT_ATTRIBUTES_WITHOUT_ONSELECT_AND_ONCHANGE);
173     
174             }
175             HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent,
176                     HTML.COMMON_FIELD_PASSTROUGH_ATTRIBUTES_WITHOUT_DISABLED_AND_EVENTS);
177             HtmlRendererUtils.renderHTMLStringAttribute(writer, uiComponent,
178                     HTML.ALT_ATTR, HTML.ALT_ATTR);
179         }
180 
181         writer.flush();
182     }
183 
184     private String getImage(UIComponent uiComponent)
185     {
186         if (uiComponent instanceof HtmlOutcomeTargetButton)
187         {
188             return ((HtmlOutcomeTargetButton) uiComponent).getImage();
189         }
190         return (String) uiComponent.getAttributes().get(JSFAttr.IMAGE_ATTR);
191     }
192 
193     public void encodeChildren(FacesContext facesContext, UIComponent component)
194             throws IOException
195     {
196         RendererUtils.renderChildren(facesContext, component);
197     }
198 
199     public void encodeEnd(FacesContext facesContext, UIComponent component)
200             throws IOException
201     {
202         super.encodeEnd(facesContext, component); //check for NP
203 
204         ResponseWriter writer = facesContext.getResponseWriter();
205 
206         writer.endElement(HTML.INPUT_ELEM);
207     }
208 }