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.selectOneRow;
20  
21  import java.io.IOException;
22  import java.util.List;
23  import java.util.Map;
24  
25  import javax.el.ValueExpression;
26  import javax.faces.component.UIComponent;
27  import javax.faces.component.UIData;
28  import javax.faces.component.UIInput;
29  import javax.faces.component.behavior.ClientBehavior;
30  import javax.faces.context.FacesContext;
31  import javax.faces.context.ResponseWriter;
32  
33  import org.apache.myfaces.shared_tomahawk.config.MyfacesConfig;
34  import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
35  import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRenderer;
36  import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRendererUtils;
37  import org.apache.myfaces.shared_tomahawk.renderkit.html.util.ResourceUtils;
38  
39  /**
40   * 
41   * @JSFRenderer
42   *   renderKitId = "HTML_BASIC" 
43   *   family = "org.apache.myfaces.SelectOneRow"
44   *   type = "org.apache.myfaces.SelectOneRow"
45   * @since 1.1.7
46   */
47  public class SelectOneRowRenderer extends HtmlRenderer
48  {
49  
50      public void encodeBegin(FacesContext facesContext, UIComponent component) throws IOException
51      {
52          if ((component instanceof SelectOneRow) && component.isRendered())
53          {
54              SelectOneRow row = (SelectOneRow) component;
55              String clientId = row.getClientId(facesContext);
56  
57              ResponseWriter writer = facesContext.getResponseWriter();
58              
59              Map<String, List<ClientBehavior>> behaviors = null;
60              behaviors = row.getClientBehaviors();
61              if (!behaviors.isEmpty())
62              {
63                  ResourceUtils.renderDefaultJsfJsInlineIfNecessary(facesContext, facesContext.getResponseWriter());
64              }
65  
66              writer.startElement(HTML.INPUT_ELEM, row);
67              writer.writeAttribute(HTML.TYPE_ATTR, HTML.INPUT_TYPE_RADIO, null);
68              writer.writeAttribute(HTML.NAME_ATTR, row.getGroupName(), null);
69  
70              if (isDisabled(facesContext, row))
71              {
72                  writer.writeAttribute(HTML.DISABLED_ATTR, HTML.DISABLED_ATTR, null);
73              }
74              
75              writer.writeAttribute(HTML.ID_ATTR, clientId, null);
76  
77              if (isRowSelected(row))
78              {
79                  writer.writeAttribute(HTML.CHECKED_ATTR, HTML.CHECKED_ATTR, null);
80              }
81  
82              writer.writeAttribute(HTML.VALUE_ATTR, clientId, null);
83  
84              if (!behaviors.isEmpty())
85              {
86                  HtmlRendererUtils.renderBehaviorizedOnchangeEventHandler(facesContext, writer, row, behaviors);
87                  HtmlRendererUtils.renderBehaviorizedEventHandlers(facesContext, writer, row, behaviors);
88                  HtmlRendererUtils.renderBehaviorizedFieldEventHandlersWithoutOnchange(facesContext, writer, row, behaviors);
89                  HtmlRendererUtils.renderHTMLAttributes(writer, row, HTML.INPUT_PASSTHROUGH_ATTRIBUTES_WITHOUT_DISABLED_AND_EVENTS);
90              }
91              else
92              {
93                  HtmlRendererUtils.renderHTMLAttributes(writer, row, HTML.INPUT_PASSTHROUGH_ATTRIBUTES_WITHOUT_DISABLED);
94              }
95  
96              writer.endElement(HTML.INPUT_ELEM);
97          }
98      }
99      
100     /**
101      * Check if the component is disabled or not, taking into account
102      * the config init param org.apache.myfaces.READONLY_AS_DISABLED_FOR_SELECTS 
103      */
104     protected boolean isDisabled(FacesContext facesContext, SelectOneRow row)
105     {
106         boolean disabled = row.isDisabled();
107         boolean readonly = row.isReadonly();
108         if (!disabled && readonly)
109         {
110             disabled = MyfacesConfig.getCurrentInstance(facesContext
111                             .getExternalContext()).isReadonlyAsDisabledForSelect();
112         }
113         return disabled;        
114     }
115 
116     private boolean isRowSelected(UIComponent component)
117     {
118         UIInput input = (UIInput) component;
119         Object value = input.getValue();
120 
121         int currentRowIndex = getCurrentRowIndex(component);
122 
123         return (value != null)
124                 && (currentRowIndex == ((Number) value).intValue());
125 
126     }
127 
128     private int getCurrentRowIndex(UIComponent component)
129     {
130         UIData uidata = findUIData(component);
131         if (uidata == null)
132             return -1;
133         else
134             return uidata.getRowIndex();
135     }
136 
137     protected UIData findUIData(UIComponent uicomponent)
138     {
139         if (uicomponent == null)
140             return null;
141         if (uicomponent instanceof UIData)
142             return (UIData) uicomponent;
143         else
144             return findUIData(uicomponent.getParent());
145     }
146 
147     public void decode(FacesContext context, UIComponent uiComponent)
148     {
149         if (! (uiComponent instanceof SelectOneRow))
150         {
151             return;
152         }
153 
154         if (!uiComponent.isRendered())
155         {
156             return;
157         }
158         SelectOneRow row = (SelectOneRow) uiComponent;
159 
160         Map requestMap = context.getExternalContext().getRequestParameterMap();
161         String postedValue;
162 
163         if (requestMap.containsKey(row.getGroupName()))
164         {
165             postedValue = (String) requestMap.get(row.getGroupName());
166             String clientId = row.getClientId(context);
167             if (clientId.equals(postedValue))
168             {
169                 String[] postedValueArray = postedValue.split(":");
170                 String rowIndex = postedValueArray[postedValueArray.length - 2];
171 
172                 ValueExpression vb = row.getValueExpression("value");
173                 Class type = vb.getType(context.getELContext());
174                 if (type == null)
175                 {
176                     type = (vb.getValue(context.getELContext()) != null) ? vb.getValue(context.getELContext()).getClass() : null;
177                 }
178                 Object newValue = null;
179                 if (type != null)
180                 {
181                     if (type.isAssignableFrom(Long.class))
182                     {
183                         newValue = Long.valueOf(rowIndex);
184                     }
185                     else
186                     {
187                         newValue = Integer.valueOf(rowIndex);
188                     }
189                 }
190                 else
191                 {
192                     newValue = Integer.valueOf(rowIndex);
193                 }
194                 //the value to go in conversion&validation
195                 row.setSubmittedValue(newValue);
196                 row.setValid(true);
197             }
198         }
199         
200         HtmlRendererUtils.decodeClientBehaviors(context, uiComponent);
201     }
202 }