KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > faces > FacesUtil


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.faces;
6 import javax.faces.application.Application;
7 import javax.faces.component.UIComponent;
8 import javax.faces.context.ExternalContext;
9 import javax.faces.context.FacesContext;
10 import javax.faces.el.ValueBinding;
11 import javax.portlet.PortletPreferences;
12 import javax.portlet.PortletRequest;
13 import javax.servlet.ServletContext JavaDoc;
14 /**
15  * Fri, May 30, 2003 @
16  * @author: Tuan Nguyen
17  * @author: Ove Ranheim
18  * @version: $Id: FacesUtil.java,v 1.6 2004/11/03 04:24:51 tuan08 Exp $
19  * @email: tuan08@yahoo.com
20  * @email: oranheim@users.sourceforge.net
21  */

22 public class FacesUtil {
23
24   static public PortletPreferences getPortletPreferences() {
25     PortletRequest request =
26       (PortletRequest) FacesContext.getCurrentInstance().
27                              getExternalContext().getRequest() ;
28     return request.getPreferences() ;
29   }
30   
31   static public String JavaDoc getApplicationRealPath(String JavaDoc s) {
32     FacesContext context = FacesContext.getCurrentInstance() ;
33         ExternalContext econtext = context.getExternalContext() ;
34         ServletContext JavaDoc scontext = (ServletContext JavaDoc) econtext.getContext() ;
35     return scontext.getRealPath(s) ;
36   }
37   
38   static public ServletContext JavaDoc getServletContext() {
39     FacesContext context = FacesContext.getCurrentInstance() ;
40     ExternalContext econtext = context.getExternalContext() ;
41     return (ServletContext JavaDoc) econtext.getContext() ;
42   }
43   
44   // Simple is el expr test
45
public static boolean isValueReference(String JavaDoc elexpr) {
46         return (elexpr != null && elexpr.startsWith("#{") && elexpr.endsWith("}"));
47     }
48
49     // Create valuebinding by UI-field/property for given el-expr
50
public static void createValueBinding(FacesContext context,
51                                                                                 UIComponent uiComp, String JavaDoc key,
52                                                                                 String JavaDoc elexpr) {
53         if (isValueReference(elexpr)) {
54             Application app = context.getApplication();
55             if (uiComp.getValueBinding(key) == null) {
56                 uiComp.setValueBinding(key, app.createValueBinding(elexpr));
57             }
58         }
59     }
60   
61   // Update the value for a UI-field/property
62
public static boolean updateBoundValueBinding(FacesContext context,
63                                                                                                 UIComponent uiComp, String JavaDoc key,
64                                                                                                 Object JavaDoc value) {
65         boolean b = false;
66         ValueBinding vb = uiComp.getValueBinding(key);
67         //Map paramMap = context.getExternalContext().getRequestParameterMap();
68
if (vb != null) {
69             vb.setValue(context, value);
70             b = true;
71         }
72         return b;
73     }
74   
75   // Resolve the placehold el-exprssed value
76
public static Object JavaDoc resolveBoundValueBinding(FacesContext context,
77                                                                                                 UIComponent uiComp, String JavaDoc key) {
78         Object JavaDoc value = null;
79         ValueBinding vb = uiComp.getValueBinding(key); // "text"
80
if (vb != null)
81             value = vb.getValue(context);
82         return value;
83     }
84 }
Popular Tags