KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > portlet > faces > application > PortletFacesData


1 package org.exoplatform.portlet.faces.application;
2
3 import java.util.HashMap JavaDoc ;
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 /**
13  * @author Tuan Nguyen (tuan08@users.sourceforge.net)
14  * @since Nov 1, 2004
15  * @version $Id: PortletFacesData.java,v 1.2 2004/11/03 04:24:53 tuan08 Exp $
16  */

17 public class PortletFacesData {
18   private static PortletMode CONFIG = new PortletMode("config") ;
19   private static PortletMode MONITOR = new PortletMode("monitor") ;
20   
21   private HashMap JavaDoc views_ ;
22   private String JavaDoc lastView_ ;
23   private PortletMode lastMode_ ;
24   
25   public PortletFacesData() {
26     views_ = new HashMap JavaDoc(5) ;
27   }
28   
29   public String JavaDoc getLastView(PortletMode mode) throws Exception JavaDoc {
30     if(mode.equals(lastMode_) && lastMode_ != null) {
31       return lastView_ ;
32     }
33     ExternalContextImpl impl =
34       (ExternalContextImpl) FacesContext.getCurrentInstance().getExternalContext();
35     String JavaDoc view = getDefaultView(mode, impl.getConfig()) ;
36     //reinit view mode since user can change the configuration in edit mode
37
//or config mode
38
if(PortletMode.VIEW == mode) views_.remove(view) ;
39
40     return view ;
41   }
42   
43   public void setLastView(PortletMode mode, String JavaDoc 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 JavaDoc viewId) {
53     return (UIViewRoot) views_.get(viewId) ;
54   }
55   
56   public String JavaDoc getDefaultView(PortletMode mode, PortletConfig config) throws Exception JavaDoc {
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 JavaDoc 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 JavaDoc id = "faces:" + windowId.getUniqueID() ;
90     scontainer.unregisterComponent(id) ;
91   }
92 }
Popular Tags