1 package org.exoplatform.portlet.faces.application; 2 3 import java.util.HashMap ; 4 import javax.faces.component.UIViewRoot; 5 import javax.faces.context.FacesContext; 6 import javax.portlet.*; 7 import javax.portlet.PortletConfig; 8 import javax.portlet.PortletMode ; 9 import org.exoplatform.services.portletcontainer.pci.ExoWindowID; 10 import org.exoplatform.container.SessionContainer; 11 import org.exoplatform.portlet.faces.context.ExternalContextImpl; 12 17 public class PortletFacesData { 18 private static PortletMode CONFIG = new PortletMode("config") ; 19 private static PortletMode MONITOR = new PortletMode("monitor") ; 20 21 private HashMap views_ ; 22 private String lastView_ ; 23 private PortletMode lastMode_ ; 24 25 public PortletFacesData() { 26 views_ = new HashMap (5) ; 27 } 28 29 public String getLastView(PortletMode mode) throws Exception { 30 if(mode.equals(lastMode_) && lastMode_ != null) { 31 return lastView_ ; 32 } 33 ExternalContextImpl impl = 34 (ExternalContextImpl) FacesContext.getCurrentInstance().getExternalContext(); 35 String view = getDefaultView(mode, impl.getConfig()) ; 36 if(PortletMode.VIEW == mode) views_.remove(view) ; 39 40 return view ; 41 } 42 43 public void setLastView(PortletMode mode, String viewId) { 44 lastView_ = viewId ; 45 lastMode_ = mode ; 46 } 47 48 public void saveView(FacesContext context, UIViewRoot uiViewRoot) { 49 views_.put(uiViewRoot.getViewId(), uiViewRoot) ; 50 } 51 52 public UIViewRoot restoreView(FacesContext context, String viewId) { 53 return (UIViewRoot) views_.get(viewId) ; 54 } 55 56 public String getDefaultView(PortletMode mode, PortletConfig config) throws Exception { 57 if (PortletMode.VIEW == mode) { 58 return config.getInitParameter("default-view") ; 59 } else if (PortletMode.EDIT == mode) { 60 return config.getInitParameter("default-edit") ; 61 } else if (PortletMode.HELP == mode) { 62 return config.getInitParameter("default-help") ; 63 } else if (CONFIG.equals(mode)) { 64 return config.getInitParameter("default-config") ; 65 } else if (MONITOR.equals(mode)) { 66 return config.getInitParameter("default-monitor") ; 67 } else { 68 throw new PortletException("unknown portlet mode: " + mode); 69 } 70 } 71 72 static public PortletFacesData getPortletFacesData(FacesContext context) { 73 SessionContainer scontainer = SessionContainer.getInstance() ; 74 ExternalContextImpl impl = (ExternalContextImpl) context.getExternalContext(); 75 ExoWindowID windowId = impl.getWindowID() ; 76 String id = "faces:" + windowId.getUniqueID() ; 77 PortletFacesData data = (PortletFacesData) scontainer.getComponentInstance(id) ; 78 if(data == null) { 79 data = new PortletFacesData() ; 80 scontainer.registerComponentInstance(id, data) ; 81 } 82 return data ; 83 } 84 85 static public void destroy(FacesContext context) { 86 SessionContainer scontainer = SessionContainer.getInstance() ; 87 ExternalContextImpl impl = (ExternalContextImpl) context.getExternalContext(); 88 ExoWindowID windowId = impl.getWindowID() ; 89 String id = "faces:" + windowId.getUniqueID() ; 90 scontainer.unregisterComponent(id) ; 91 } 92 } | Popular Tags |