KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > faces > component > panelstack > PanelStack


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

33
34 package com.icesoft.faces.component.panelstack;
35
36 import com.icesoft.faces.component.CSS_DEFAULT;
37 import com.icesoft.faces.component.ext.taglib.Util;
38
39 import javax.faces.component.html.HtmlPanelGroup;
40 import javax.faces.component.UIComponent;
41 import javax.faces.context.FacesContext;
42 import javax.faces.el.ValueBinding;
43 import javax.faces.event.PhaseId;
44 import java.util.Iterator JavaDoc;
45
46
47 /**
48  * Manage a stack of JSF components and allow for one child component to be
49  * choosen for rendering. The behaviour is similar to the CardLayout of Java
50  * Swing. Property <code>selectedPanel</code> defines the id of the child to be
51  * rendered. If no child panel is selected or if the selected panel can not be
52  * found the first child is rendered.
53  *
54  * @version beta 1.0
55  */

56 public class PanelStack extends HtmlPanelGroup {
57
58     public static final String JavaDoc COMPONENT_TYPE = "com.icesoft.faces.PanelStack";
59     public static final String JavaDoc COMPONENT_FAMILY = "javax.faces.Panel";
60     private static final String JavaDoc DEFAULT_RENDERER_TYPE =
61             "com.icesoft.faces.PanelStack";
62     private String JavaDoc style = null;
63     private String JavaDoc selectedPanel = null;
64     private String JavaDoc styleClass = null;
65     private String JavaDoc renderedOnUserRole = null;
66
67     public PanelStack() {
68         setRendererType(DEFAULT_RENDERER_TYPE);
69     }
70
71     /**
72      * <p>Return the value of the <code>COMPONENT_FAMILY</code> of this
73      * component.</p>
74      */

75     public String JavaDoc getFamily() {
76         return COMPONENT_FAMILY;
77     }
78
79     /**
80      * <p>Set the value of the <code>style</code> property.</p>
81      */

82     public void setStyle(String JavaDoc style) {
83         this.style = style;
84     }
85
86     /**
87      * <p>Return the value of the <code>style</code> property.</p>
88      */

89     public String JavaDoc getStyle() {
90         if (style != null) {
91             return style;
92         }
93         ValueBinding vb = getValueBinding("style");
94         return vb != null ? (String JavaDoc) vb.getValue(getFacesContext()) : null;
95     }
96
97     /**
98      * <p>Set the value of the <code>selectedPanel</code> property.</p>
99      */

100     public void setSelectedPanel(String JavaDoc selectedPanel) {
101         this.selectedPanel = selectedPanel;
102     }
103
104     /**
105      * <p>Return the value of the <code>selectedPanel</code> property.</p>
106      */

107     public String JavaDoc getSelectedPanel() {
108         if (selectedPanel != null) {
109             return selectedPanel;
110         }
111         ValueBinding vb = getValueBinding("selectedPanel");
112         return vb != null ? (String JavaDoc) vb.getValue(getFacesContext()) : null;
113     }
114
115     /**
116      * <p>Return the value of the <code>rendered</code> property.</p>
117      */

118     public boolean isRendered() {
119         if (!Util.isRenderedOnUserRole(this)) {
120             return false;
121         }
122         return super.isRendered();
123     }
124
125     /**
126      * <p>Set the value of the <code>styleClass</code> property.</p>
127      */

128     public void setStyleClass(String JavaDoc styleClass) {
129         this.styleClass = styleClass;
130     }
131
132     /**
133      * <p>Return the value of the <code>styleClass</code> property.</p>
134      */

135     public String JavaDoc getStyleClass() {
136         return Util.getQualifiedStyleClass(this,
137                         styleClass,
138                         CSS_DEFAULT.PANEL_STACK_BASE,
139                         "styleClass");
140     }
141
142     /**
143      * <p>Set the value of the <code>renderedOnUserRole</code> property.</p>
144      */

145     public void setRenderedOnUserRole(String JavaDoc renderedOnUserRole) {
146         this.renderedOnUserRole = renderedOnUserRole;
147     }
148
149     /**
150      * <p>Return the value of the <code>renderedOnUserRole</code> property.</p>
151      */

152     public String JavaDoc getRenderedOnUserRole() {
153         if (renderedOnUserRole != null) {
154             return renderedOnUserRole;
155         }
156         ValueBinding vb = getValueBinding("renderedOnUserRole");
157         return vb != null ? (String JavaDoc) vb.getValue(getFacesContext()) : null;
158     }
159
160     /**
161      * <p>Return the value of the <code>rowClass</code> property.</p>
162      */

163     public String JavaDoc getRowClass() {
164         return Util.getQualifiedStyleClass(this, CSS_DEFAULT.PANEL_STACK_ROW);
165     }
166
167     /**
168      * <p>Return the value of the <code>columnName</code> property.</p>
169      */

170     public String JavaDoc getColumnClass() {
171         return Util.getQualifiedStyleClass(this, CSS_DEFAULT.PANEL_STACK_COL);
172     }
173
174     /**
175      * <p>Gets the state of the instance as a <code>Serializable</code>
176      * Object.</p>
177      */

178     public Object JavaDoc saveState(FacesContext context) {
179         Object JavaDoc values[] = new Object JavaDoc[4];
180         values[0] = super.saveState(context);
181         values[1] = selectedPanel;
182         values[2] = styleClass;
183         values[3] = style;
184         return ((Object JavaDoc) (values));
185     }
186
187     /**
188      * <p>Perform any processing required to restore the state from the entries
189      * in the state Object.</p>
190      */

191     public void restoreState(FacesContext context, Object JavaDoc state) {
192         Object JavaDoc values[] = (Object JavaDoc[]) state;
193         super.restoreState(context, values[0]);
194         selectedPanel = (String JavaDoc) values[1];
195         styleClass = (String JavaDoc) values[2];
196         style = (String JavaDoc) values[3];
197     }
198
199
200     public static final String JavaDoc LAST_SELECTED_PANEL = "PanelStack-lastPanel";
201     /**
202         * @param context
203         * @param phaseId
204         */

205     public void applyPhase(FacesContext context, PhaseId phaseId) {
206         if (context == null) {
207             throw new NullPointerException JavaDoc("Null context in PanelTabSet");
208         }
209         Iterator JavaDoc it = getFacetsAndChildren();
210
211         while (it.hasNext()) {
212             UIComponent childOrFacet =
213                     (UIComponent) it.next();
214             String JavaDoc selectedPanel = getSelectedPanel();
215             String JavaDoc lastSelectedPanel = (String JavaDoc) context.getExternalContext().getRequestMap().get(LAST_SELECTED_PANEL + getClientId(context));
216             boolean changed = !selectedPanel.equals(lastSelectedPanel);
217             if (lastSelectedPanel == null) changed = false;
218             if (selectedPanel.equals(childOrFacet.getId())) {
219                 if (!(changed && phaseId == PhaseId.APPLY_REQUEST_VALUES)) {
220                     applyPhase(context, childOrFacet, phaseId);
221                 }
222             }
223         }
224
225     }
226
227     /**
228         * @param context
229         * @param component
230         * @param phaseId
231         */

232        public void applyPhase(FacesContext context, UIComponent component,
233                               PhaseId phaseId) {
234            if (phaseId == PhaseId.APPLY_REQUEST_VALUES) {
235                component.processDecodes(context);
236            } else if (phaseId == PhaseId.PROCESS_VALIDATIONS) {
237                component.processValidators(context);
238            } else if (phaseId == PhaseId.UPDATE_MODEL_VALUES) {
239                component.processUpdates(context);
240            } else {
241                throw new IllegalArgumentException JavaDoc();
242            }
243        }
244
245        /* (non-Javadoc)
246        * @see javax.faces.component.UIComponent#processDecodes(javax.faces.context.FacesContext)
247        */

248        public void processDecodes(javax.faces.context.FacesContext context) {
249          
250            if (context == null) {
251                throw new NullPointerException JavaDoc("context");
252            }
253
254            if (!isRendered()) {
255                return;
256            }
257
258            decode(context);
259            applyPhase(context, PhaseId.APPLY_REQUEST_VALUES);
260        }
261
262        /* (non-Javadoc)
263        * @see javax.faces.component.UIComponent#processValidators(javax.faces.context.FacesContext)
264        */

265        public void processValidators(FacesContext context) {
266
267            if (context == null) {
268                throw new NullPointerException JavaDoc();
269            }
270            if (!isRendered()) {
271                return;
272            }
273            applyPhase(context, PhaseId.PROCESS_VALIDATIONS);
274        }
275
276
277        /* (non-Javadoc)
278         * @see javax.faces.component.UIComponent#processUpdates(javax.faces.context.FacesContext)
279         */

280        public void processUpdates(FacesContext context) {
281
282            if (context == null) {
283                throw new NullPointerException JavaDoc();
284            }
285            if (!isRendered()) {
286                return;
287            }
288            applyPhase(context, PhaseId.UPDATE_MODEL_VALUES);
289        }
290     
291 }
292
Popular Tags