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.focus;
20  
21  import java.io.IOException;
22  import java.util.List;
23  
24  import javax.faces.component.NamingContainer;
25  import javax.faces.component.UIComponent;
26  import javax.faces.component.UISelectMany;
27  import javax.faces.component.UISelectOne;
28  import javax.faces.component.html.HtmlSelectManyCheckbox;
29  import javax.faces.component.html.HtmlSelectOneRadio;
30  import javax.faces.context.FacesContext;
31  import javax.faces.context.ResponseWriter;
32  import javax.faces.render.Renderer;
33  
34  import org.apache.myfaces.custom.date.HtmlDateRenderer;
35  import org.apache.myfaces.custom.date.HtmlInputDate;
36  import org.apache.myfaces.custom.dojo.DojoConfig;
37  import org.apache.myfaces.custom.dojo.DojoUtils;
38  import org.apache.myfaces.shared_tomahawk.renderkit.JSFAttr;
39  import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
40  import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
41  
42  /**
43   * 
44   * @JSFRenderer
45   *   renderKitId = "HTML_BASIC" 
46   *   family = "javax.faces.Output"
47   *   type = "org.apache.myfaces.Focus"
48   *
49   * @author Rogerio Pereira Araujo (latest modification by $Author: lu4242 $)
50   * @version $Revision: 681488 $ $Date: 2008-07-31 15:59:58 -0500 (Thu, 31 Jul 2008) $
51   */
52  public class HtmlFocusRenderer extends Renderer
53  {
54  
55      public void decode(FacesContext facesContext, UIComponent component)
56      {
57          RendererUtils.checkParamValidity(facesContext, component, HtmlFocus.class);
58  
59          HtmlFocus focus = (HtmlFocus) component;
60  
61          if(focus.isRememberClientFocus())
62          {
63              focus.setSubmittedValue(RendererUtils.getStringValue(facesContext, component));
64          }
65      }
66  
67      public void encodeEnd(FacesContext facesContext, UIComponent uiComponent)
68              throws IOException
69      {
70          RendererUtils.checkParamValidity(facesContext, uiComponent,
71                                           HtmlFocus.class);
72  
73          HtmlFocus focus = (HtmlFocus) uiComponent;
74  
75          UIComponent targetComponent = focus.findUIComponent();
76  
77          if(targetComponent != null)
78          {
79              if (focus.isRememberClientFocus())
80              {
81                  String javascriptLocation = (String)uiComponent.getAttributes().get(JSFAttr.JAVASCRIPT_LOCATION);
82                  DojoUtils.addMainInclude(facesContext, uiComponent, javascriptLocation, new DojoConfig());
83                  DojoUtils.addRequire(facesContext, uiComponent, "dojo.event.*");
84              }
85  
86              String clientId = targetComponent.getClientId(facesContext);
87              if(targetComponent instanceof HtmlInputDate)
88              {
89                  clientId = HtmlDateRenderer.getClientIdForDaySubcomponent(clientId);
90              } 
91              else if(targetComponent instanceof HtmlSelectOneRadio)
92              {
93                  UISelectOne selectOne = (UISelectOne)targetComponent;
94                  List selectItemList = org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils.getSelectItemList(selectOne);
95                  clientId += getFirstChildId(selectItemList);
96              }
97              else if(targetComponent instanceof HtmlSelectManyCheckbox)
98              {
99                  UISelectMany selectMany = (UISelectMany)targetComponent;
100                 List selectItemList = org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils.getSelectItemList(selectMany);
101                 clientId += getFirstChildId(selectItemList);
102             }
103             ResponseWriter writer = facesContext.getResponseWriter();
104 
105             writer.startElement(HTML.SCRIPT_ELEM, uiComponent);
106             writer.writeAttribute(HTML.SCRIPT_TYPE_ATTR, HTML.SCRIPT_TYPE_TEXT_JAVASCRIPT, null);
107             writer.writeText("setTimeout(\"document.getElementById('" + clientId + "').focus()\", 100)", null);
108             writer.endElement(HTML.SCRIPT_ELEM);
109 
110             if (focus.isRememberClientFocus())
111             {
112                 writer.startElement(HTML.INPUT_ELEM, uiComponent);
113                 writer.writeAttribute(HTML.TYPE_ATTR,HTML.INPUT_TYPE_HIDDEN,null);
114                 writer.writeAttribute(HTML.ID_ATTR,uiComponent.getClientId(facesContext), JSFAttr.ID_ATTR);
115                 writer.writeAttribute(HTML.VALUE_ATTR,clientId,JSFAttr.VALUE_ATTR);
116                 writer.endElement(HTML.INPUT_ELEM);
117             }
118         }
119     }
120     
121     private String getFirstChildId(List selectItems) 
122     {
123         if(selectItems.size() > 0) 
124         {
125             return NamingContainer.SEPARATOR_CHAR + "0";
126         }
127         return "";
128     }
129 }