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  
20  package org.apache.myfaces.custom.util;
21  
22  import javax.faces.component.UIColumn;
23  import javax.faces.component.UIComponent;
24  import javax.faces.component.UIParameter;
25  import javax.faces.component.html.HtmlDataTable;
26  import javax.faces.component.html.HtmlMessages;
27  import javax.faces.context.FacesContext;
28  import javax.faces.lifecycle.LifecycleFactory;
29  import javax.faces.webapp.FacesServlet;
30  import javax.servlet.ServletContext;
31  
32  import java.util.ArrayList;
33  import java.util.HashMap;
34  import java.util.Iterator;
35  import java.util.List;
36  import java.util.Map;
37  
38  /**
39   * User: treeder
40   * Date: Nov 21, 2005
41   * Time: 9:20:14 PM
42   */
43  public final class ComponentUtils
44  {
45  
46      private ComponentUtils() {
47  
48      }
49  
50      /**
51       * TR- This was moved from AjaxPhaseListenere on checkin 344383
52       *
53       * @param context
54       * @param root
55       * @param clientId
56       * @return component referenced by clientId or null if not found
57       */
58      public static UIComponent findComponentByClientId(FacesContext context,
59              UIComponent root, String clientId) {
60          UIComponent component = null;
61          for (int i = 0; i < root.getChildCount() && component == null; i++) {
62              UIComponent child = (UIComponent) root.getChildren().get(i);
63              component = findComponentByClientId(context, child, clientId);
64          }
65          if (root.getId() != null) {
66              if (component == null && root.getClientId(context).equals(clientId)) {
67                  component = root;
68              }
69          }
70          return component;
71      }
72  
73      /**
74       * Useful if you don't know the clientId <p/> TR- This was moved from
75       * AjaxPhaseListenere on checkin 344383 Seems like this could be made more
76       * efficient
77       * 
78       * @param context
79       * @param root
80       * @param id
81       * @return component referenced by id or null if not found
82       */
83      public static UIComponent findComponentById(FacesContext context,
84              UIComponent root, String id) {
85          UIComponent component = null;
86          for (int i = 0; i < root.getChildCount() && component == null; i++) {
87              UIComponent child = (UIComponent) root.getChildren().get(i);
88              component = findComponentById(context, child, id);
89          }
90          // System.out.println("component looking for: " + id + " - rootid: " +
91          // root.getId() + " " + root);
92          if (root.getId() != null) {
93              if (component == null && root.getId().equals(id)) {
94                  component = root;
95              }
96          }
97          return component;
98      }
99  
100     public static UIComponent findFirstMessagesComponent(FacesContext context,
101             UIComponent base) {
102         if (base == null) {
103             return null;
104         }
105 
106         if (base instanceof HtmlMessages) {
107             return base;
108         }
109 
110         Iterator iterChildren = base.getFacetsAndChildren();
111         while (iterChildren.hasNext()) {
112             UIComponent child = (UIComponent) iterChildren.next();
113 
114             UIComponent found = findFirstMessagesComponent(context, child);
115             if (found != null) {
116                 return found;
117             }
118         }
119 
120         return null;
121     }
122 
123     private static boolean isDecorated(UIComponent component, String attribute,
124             String value) {
125         String attributeValue = (String) component.getAttributes().get(
126                 attribute);
127 
128         if (attributeValue == null || attributeValue.indexOf(value) == -1)
129             return false;
130         else
131             return true;
132     }
133     
134     /**
135      * Changes the event attributes like onclick by appending the given value
136      * 
137      * @param component
138      *            UIComponent instance that the attribute belongs to
139      * @param attribute
140      *            Attribute to be changed
141      * @param value
142      *            Value to be appended
143      */
144     public static void decorateEventAttribute(UIComponent component,
145             String attribute, String value) {
146         if (isDecorated(component, attribute, value))
147             return;
148 
149         String attributeValue = (String) component.getAttributes().get(
150                 attribute);
151 
152         if (attributeValue == null)
153             component.getAttributes().put(attribute, value);
154         else if (attributeValue.endsWith(";"))
155             component.getAttributes().put(attribute, attributeValue + value);
156         else
157             component.getAttributes().put(attribute,
158                     attributeValue + ";" + value);
159     }
160     
161     /**
162      * The getParameterMap() is used for getting the parameters
163      * of a specific component.
164      * @param component
165      * @return the Map of the component.
166      */
167     public static Map getParameterMap(UIComponent component) {
168         Map result = new HashMap();
169         for (Iterator iter = component.getChildren().iterator(); iter.hasNext();) {
170             UIComponent child = (UIComponent) iter.next();
171             if (child instanceof UIParameter) {
172                 UIParameter uiparam = (UIParameter) child;
173                 Object value = uiparam.getValue();
174                 if (value != null) {
175                     result.put(uiparam.getName(), value);
176                 }
177             }
178         }
179         return result;
180     }    
181     
182     /**
183      * The getLifecycleId() is used for getting the id of 
184      * the Lifecycle from the ServletContext.
185      * @param context
186      * @return the id of the life cycle.
187      */    
188     public static String getLifecycleId(ServletContext context) {
189         String lifecycleId = context
190                 .getInitParameter(FacesServlet.LIFECYCLE_ID_ATTR);
191         return lifecycleId != null ? lifecycleId
192                 : LifecycleFactory.DEFAULT_LIFECYCLE;
193     }    
194     
195     /**
196      * This method is used for getting the columns of 
197      * a specific HTMLDataTable.
198      * @param table
199      * @return the List of UIColumns
200      */
201     public static List getHTMLDataTableColumns(HtmlDataTable table) {
202         List columns = new ArrayList();
203         
204         for (int i = 0; i < table.getChildCount(); i++) {
205             UIComponent child = (UIComponent) table.getChildren().get( i );
206             if (child instanceof UIColumn) {                
207                 columns.add( child );
208             }
209         }
210         return columns;
211     }    
212 }