1 5 package org.exoplatform.portlet.faces.application; 6 7 import java.io.IOException ; 8 import java.util.Locale ; 9 import javax.faces.FacesException; 10 import javax.faces.application.StateManager; 11 import javax.faces.application.ViewHandler; 12 import javax.faces.component.UIViewRoot; 13 import javax.faces.context.FacesContext; 14 import org.apache.commons.logging.Log; 15 import org.exoplatform.services.log.LogUtil; 16 import org.exoplatform.portlet.faces.component.UIExoViewRoot; 17 23 public class FacesPortletViewHandler extends ViewHandler { 24 protected static Log log_ = LogUtil.getLog("org.exoplatform.portlet.faces.faces" ); 25 26 protected StateManager stateManager_ ; 27 28 public FacesPortletViewHandler(StateManager stateManager) { 29 stateManager_ = stateManager ; 30 } 31 32 public void renderView(FacesContext context, UIViewRoot viewToRender) throws IOException , FacesException { 33 if(null == context || null == viewToRender) 34 throw new NullPointerException ("Faces context cannot be null"); 35 UIExoViewRoot exoViewToRender = (UIExoViewRoot) viewToRender ; 36 String viewId = exoViewToRender.getViewId(); 37 if (exoViewToRender.isComponentView()) { 38 exoViewToRender.renderChildren(context) ; 40 } else { 41 context.getExternalContext().dispatch(viewId); 43 } 44 } 47 48 public StateManager getStateManager() { return stateManager_; } 49 50 public UIViewRoot restoreView(FacesContext context, String viewId) { 51 if(context == null) 52 throw new NullPointerException ("faces context cannot be null"); 53 PortletFacesData data = PortletFacesData.getPortletFacesData(context) ; 54 UIViewRoot viewRoot = data.restoreView(context, viewId) ; 55 return viewRoot; 57 } 58 59 public UIViewRoot createView(FacesContext context, String viewId) { 60 UIViewRoot result = null ; 61 if (result == null) { 62 result = new UIExoViewRoot(viewId); 63 Locale locale = null; 64 if(context.getViewRoot() != null) locale = context.getViewRoot().getLocale(); 65 if(locale == null) { 66 locale = calculateLocale(context); 67 } 68 result.setLocale(locale); 69 } 70 result.setRenderKitId(calculateRenderKitId(context)) ; 71 PortletFacesData data = PortletFacesData.getPortletFacesData(context) ; 72 data.saveView(context, result) ; 73 return result; 74 } 75 76 public String calculateRenderKitId(FacesContext context) { 77 if(context == null) throw new NullPointerException ("Faces Context is null"); 78 String result = context.getApplication().getDefaultRenderKitId() ; 79 if(null == result ) result = "HTML_BASIC"; 80 return result; 81 } 82 83 public Locale calculateLocale(FacesContext context) { 84 return context.getExternalContext().getRequestLocale() ; 85 } 86 87 public void writeState(FacesContext context) throws IOException { 88 93 } 94 95 public String getViewIdPath(FacesContext context, String viewId) { 96 if(context == null || viewId == null) 97 throw new NullPointerException ("faces context or viewId cannot be bull"); 98 if(viewId.charAt(0) != '/') { 99 throw new IllegalArgumentException ("expect viewId start with /"); 100 } 101 return viewId; 102 } 103 104 public String getResourceURL(FacesContext context, String path) { 105 if(path.startsWith("/")) 106 return context.getExternalContext().getRequestContextPath() + path; 107 return path; 108 } 109 110 public String getActionURL(FacesContext context, String viewId) { 111 return viewId ; 112 } 113 } | Popular Tags |