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