1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.portals.bridges.jsf;
18
19 import javax.faces.component.NamingContainer;
20 import javax.faces.component.UIViewRoot;
21 import javax.faces.context.FacesContext;
22 import javax.portlet.RenderResponse;
23
24 /***
25 * A portlet view root that implements a naming container to creates unique
26 * client ids.
27 * @author Matthew Bruzek
28 */
29 public class PortletUIViewRoot extends UIViewRoot implements NamingContainer
30 {
31 /*** A portlet view id constant to prepend to the namespace. */
32 public static final String VIEW_PREFIX = "view";
33
34 private String _namespace;
35
36 /*** The default constructor calls the UIViewRoot default constructor. */
37 public PortletUIViewRoot()
38 {
39 super();
40 }
41
42 /***
43 * The convenience constructor creates a PortletUIViewRoot from a UIViewRoot.
44 * @param viewRoot The UIViewRoot to use when creating this object.
45 */
46 public PortletUIViewRoot( UIViewRoot viewRoot )
47 {
48 setLocale( viewRoot.getLocale() );
49 setRenderKitId( viewRoot.getRenderKitId() );
50 setViewId( viewRoot.getViewId() );
51 }
52
53 /***
54 * Return a string which can be used as output to the response which uniquely
55 * identifies a component within the current view.
56 * @param context The FacesContext object for the current request.
57 */
58 public String getClientId( FacesContext context )
59 {
60 if ( context == null )
61 throw new NullPointerException( "context can not be null." );
62
63 if (_namespace == null && context.getExternalContext().getResponse() instanceof RenderResponse) {
64 String nameSpace = context.getExternalContext().encodeNamespace( "" );
65 _namespace = VIEW_PREFIX + nameSpace;
66 }
67 return _namespace;
68 }
69
70
71 public Object saveState(FacesContext context) {
72 Object values[] = new Object[2];
73 values[0] = super.saveState(context);
74 values[1] = _namespace;
75 return values;
76 }
77
78
79 public void restoreState(FacesContext context, Object state) {
80 Object values[] = (Object[])state;
81 super.restoreState(context, values[0]);
82 _namespace = (String) values[1];
83 }
84 }
85