KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > faces > component > ext > HtmlDataTable


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.ext;
35
36 import com.icesoft.faces.component.CSS_DEFAULT;
37 import com.icesoft.faces.component.ext.taglib.Util;
38 import com.icesoft.faces.component.panelseries.UISeries;
39
40 import javax.faces.component.UIColumn;
41 import javax.faces.component.UIComponent;
42 import javax.faces.component.UIData;
43 import javax.faces.context.FacesContext;
44 import javax.faces.el.ValueBinding;
45 import javax.faces.event.PhaseId;
46 import java.util.Iterator JavaDoc;
47
48 /**
49  * This is an extension of javax.faces.component.html.HtmlDataTable, which
50  * provides some additional behavior to this component such as: <ul> <li>changes
51  * the component's rendered state based on the authentication</li> <li>maintain
52  * the sorting order, for a column within the dataTable</li> <ul>
53  */

54 public class HtmlDataTable
55         extends UISeries {
56
57     public static final String JavaDoc COMPONENT_TYPE =
58             "com.icesoft.faces.HtmlDataTable";
59     public static final String JavaDoc RENDERER_TYPE = "com.icesoft.faces.Table";
60     private String JavaDoc renderedOnUserRole = null;
61     private String JavaDoc sortColumn = null;
62     private Boolean JavaDoc sortAscending = null;
63     private static final boolean DEFAULT_SORTASCENDING = true;
64     
65     private Boolean JavaDoc scrollable = null;
66     private String JavaDoc columnWidths = null;
67     private String JavaDoc scrollHeight = null;
68     private String JavaDoc headerClasses = null;
69
70     public HtmlDataTable() {
71         super();
72         setRendererType(RENDERER_TYPE);
73     }
74
75     /**
76      * <p>Set the value of the <code>renderedOnUserRole</code> property.</p>
77      */

78     public void setRenderedOnUserRole(String JavaDoc renderedOnUserRole) {
79         this.renderedOnUserRole = renderedOnUserRole;
80     }
81
82     /**
83      * <p>Return the value of the <code>renderedOnUserRole</code> property.</p>
84      */

85     public String JavaDoc getRenderedOnUserRole() {
86         if (renderedOnUserRole != null) {
87             return renderedOnUserRole;
88         }
89         ValueBinding vb = getValueBinding("renderedOnUserRole");
90         return vb != null ? (String JavaDoc) vb.getValue(getFacesContext()) : null;
91     }
92
93     /**
94      * <p>Return the value of the <code>rendered</code> property.</p>
95      */

96     public boolean isRendered() {
97         if (!Util.isRenderedOnUserRole(this)) {
98             return false;
99         }
100         return super.isRendered();
101     }
102
103     /**
104      * <p>Return the value of the <code>sortColumn</code> property.</p>
105      */

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

117     public void setSortAscending(boolean sortAscending) {
118         this.sortAscending = new Boolean JavaDoc(sortAscending);
119         ValueBinding vb = getValueBinding("sortAscending");
120         if (vb != null) {
121             vb.setValue(getFacesContext(), this.sortAscending);
122             this.sortAscending = null;
123         }
124     }
125
126     /**
127      * <p>Set the value of the <code>sortColumn</code> property.</p>
128      */

129     public void setSortColumn(String JavaDoc sortColumn) {
130         this.sortColumn = sortColumn;
131         ValueBinding vb = getValueBinding("sortColumn");
132         if (vb != null) {
133             vb.setValue(getFacesContext(), this.sortColumn);
134             sortColumn = null;
135         }
136     }
137
138     /**
139      * <p>Return the value of the <code>sortAscending</code> property.</p>
140      */

141     public boolean isSortAscending() {
142         if (sortAscending != null) {
143             return sortAscending.booleanValue();
144         }
145         ValueBinding vb = getValueBinding("sortAscending");
146         Boolean JavaDoc v =
147                 vb != null ? (Boolean JavaDoc) vb.getValue(getFacesContext()) : null;
148         return v != null ? v.booleanValue() : DEFAULT_SORTASCENDING;
149     }
150
151     /**
152      * <p>Gets the state of the instance as a <code>Serializable</code>
153      * Object.</p>
154      */

155     public Object JavaDoc saveState(FacesContext context) {
156         Object JavaDoc values[] = new Object JavaDoc[4];
157         values[0] = super.saveState(context);
158         values[1] = renderedOnUserRole;
159         return ((Object JavaDoc) (values));
160     }
161
162     /**
163      * <p>Perform any processing required to restore the state from the entries
164      * in the state Object.</p>
165      */

166     public void restoreState(FacesContext context, Object JavaDoc state) {
167         Object JavaDoc values[] = (Object JavaDoc[]) state;
168         super.restoreState(context, values[0]);
169         renderedOnUserRole = (String JavaDoc) values[1];
170     }
171
172     public String JavaDoc getComponentType() {
173         return COMPONENT_TYPE;
174     }
175
176     protected void iterate(FacesContext facesContext, PhaseId phase) {
177         // clear row index
178
setRowIndex(-1);
179         // process component facets once
180
Iterator JavaDoc facets = getFacets().keySet().iterator();
181         while (facets.hasNext()) {
182             UIComponent facet = (UIComponent) getFacets().get(facets.next());
183             processKids(facesContext, phase, facet);
184         }
185         // reset row index
186
setRowIndex(-1);
187         // process each child column and it's facets once
188
Iterator JavaDoc columns = getChildren().iterator();
189         while (columns.hasNext()) {
190             UIComponent column = (UIComponent) columns.next();
191             if (!(column instanceof UIColumn) &&
192                 !(column instanceof UIColumns)) {
193                 continue;
194             }
195             if (!column.isRendered()) {
196                 continue;
197             }
198             if (column instanceof UIColumn) {
199                 Iterator JavaDoc columnFacets = column.getFacets().keySet().iterator();
200                 while (columnFacets.hasNext()) {
201                     UIComponent columnFacet = (UIComponent) column.getFacets()
202                             .get(columnFacets.next());
203                     processKids(facesContext, phase, columnFacet);
204                 }
205
206             } else if (column instanceof UIColumns) {
207                 processKids(facesContext, phase, column);
208             }
209         }
210
211         // clear rowIndex
212
setRowIndex(-1);
213
214         int rowsProcessed = 0;
215         int currentRowIndex = getFirst() - 1;
216         int displayedRows = getRows();
217         // loop over dataModel processing each row once
218
while (1 == 1) {
219             // break if we have processed the number of rows requested
220
if ((++currentRowIndex >= getRowCount()) ||
221                     ((displayedRows > 0) && (++rowsProcessed > displayedRows))) {
222                 break;
223             }
224             // process the row at currentRowIndex
225
setRowIndex(currentRowIndex);
226             // break if we've moved past the last row
227
if (!isRowAvailable()) {
228                 break;
229             }
230             // loop over children
231
Iterator JavaDoc children = getChildren().iterator();
232             while (children.hasNext()) {
233                 UIComponent child = (UIComponent) children.next();
234                 if (!(child instanceof UIColumn) &&
235                     !(child instanceof UIColumns)) {
236                     continue;
237                 }
238                 if (child instanceof UIColumn) {
239                     Iterator JavaDoc granchildren = child.getChildren().iterator();
240                     while (granchildren.hasNext()) {
241                         UIComponent granchild =
242                                 (UIComponent) granchildren.next();
243                         if (!granchild.isRendered()) {
244                             continue;
245                         }
246                         processKids(facesContext, phase, granchild);
247                     }
248                 } else if (child instanceof UIColumns) {
249                     processKids(facesContext, phase, child);
250                 }
251             }
252         }
253
254         // clear rowIndex
255
setRowIndex(-1);
256     }
257
258     protected void restoreChildrenState(FacesContext facesContext) {
259         Iterator JavaDoc kids = getChildren().iterator();
260         while (kids.hasNext()) {
261             UIComponent kid = (UIComponent) kids.next();
262             if (kid instanceof UIColumn) {
263                 restoreChildState(facesContext, kid);
264             }
265         }
266
267     }
268
269     /**
270      * <p>Save state information for all descendant components, as described for
271      * <code>setRowIndex()</code>.</p>
272      */

273     protected void saveChildrenState(FacesContext facesContext) {
274         Iterator JavaDoc kids = getChildren().iterator();
275         while (kids.hasNext()) {
276             UIComponent kid = (UIComponent) kids.next();
277             if (kid instanceof UIColumn) {
278                 saveChildState(facesContext, kid);
279             }
280         }
281     }
282
283     public void processKids(FacesContext context, PhaseId phaseId,
284                             UIComponent kid) {
285         if (phaseId == PhaseId.APPLY_REQUEST_VALUES) {
286             kid.processDecodes(context);
287         } else if (phaseId == PhaseId.PROCESS_VALIDATIONS) {
288             kid.processValidators(context);
289         } else if (phaseId == PhaseId.UPDATE_MODEL_VALUES) {
290             kid.processUpdates(context);
291         } else {
292             throw new IllegalArgumentException JavaDoc();
293         }
294     }
295
296     private int colNumber = 0;
297
298     public int getColNumber() {
299         return colNumber;
300     }
301
302     public void setColNumber(int colNumber) {
303         this.colNumber = colNumber;
304     }
305
306     public Boolean JavaDoc getScrollable(){
307         return isScrollable();
308     }
309
310     public Boolean JavaDoc isScrollable() {
311         if (scrollable != null) {
312             return scrollable;
313         }
314         ValueBinding vb = getValueBinding("scrollable");
315         Boolean JavaDoc v =
316                 vb != null ? (Boolean JavaDoc) vb.getValue(getFacesContext()) : null;
317         return v != null ? v : Boolean.FALSE;
318     }
319
320     public void setScrollable(Boolean JavaDoc scrollable) {
321         this.scrollable = scrollable;
322
323     }
324
325     public String JavaDoc getColumnWidths() {
326         if (columnWidths != null) {
327             return columnWidths;
328         }
329         ValueBinding vb = getValueBinding("columnWidths");
330         return vb != null ? (String JavaDoc) vb.getValue(getFacesContext()) : null;
331     }
332
333     public void setColumnWidths(String JavaDoc columnWidths) {
334         this.columnWidths = columnWidths;
335
336     }
337
338     public String JavaDoc getScrollHeight() {
339         if (scrollHeight != null) {
340             return scrollHeight;
341         }
342         ValueBinding vb = getValueBinding("scrollHeight");
343         return vb != null ? (String JavaDoc) vb.getValue(getFacesContext()) : null;
344     }
345
346     public void setScrollHeight(String JavaDoc scrollHeight) {
347         this.scrollHeight = scrollHeight;
348
349     }
350
351     /**
352      * <p>Set the value of the <code>headerClasses</code> property.</p>
353      */

354     public void setHeaderClasses(String JavaDoc headerClasses) {
355         this.headerClasses = headerClasses;
356     }
357
358     /**
359      * <p>Return the value of the <code>headerClasses</code> property.</p>
360      */

361     public String JavaDoc getHeaderClasses() {
362         if (headerClasses != null) {
363             return headerClasses;
364         }
365         ValueBinding vb = getValueBinding("headerClasses");
366         return vb != null ? (String JavaDoc) vb.getValue(getFacesContext()) :null;
367     }
368
369     String JavaDoc[] headerClassesArray = null;
370
371     public String JavaDoc getHeaderClassAtIndex(int index) {
372         if (headerClassesArray == null) {
373             headerClassesArray = getHeaderClasses().split(",");
374         }
375         if (headerClassesArray.length == 1) {
376             return headerClassesArray[0];
377         }
378         try {
379             return headerClassesArray[index];
380         } catch (ArrayIndexOutOfBoundsException JavaDoc e) {
381             return headerClassesArray[0];
382         }
383     }
384
385
386     protected void restoreChild(FacesContext facesContext,
387                                 UIComponent uiComponent) {
388         super.restoreChild(facesContext, uiComponent);
389         if (uiComponent instanceof UIData) {
390             String JavaDoc clientId = uiComponent.getClientId(facesContext);
391             Object JavaDoc value = savedChildren.get(clientId);
392             ((UIData) uiComponent).setValue(value);
393         }
394     }
395
396     protected void saveChild(FacesContext facesContext,
397                              UIComponent uiComponent) {
398         super.saveChild(facesContext, uiComponent);
399         if (uiComponent instanceof UIData) {
400             String JavaDoc clientId = uiComponent.getClientId(facesContext);
401             savedChildren.put(clientId, ((UIData) uiComponent).getValue());
402         }
403     }
404     
405     public String JavaDoc getStyleClass() {
406         return Util.getQualifiedStyleClass(this,
407                 super.getStyleClass(),
408                 CSS_DEFAULT.TABLE_STYLE_CLASS,
409                 "styleClass");
410     }
411     
412     public String JavaDoc getHeaderClass() {
413         return Util.getQualifiedStyleClass(this,
414                 super.getHeaderClass(),
415                 CSS_DEFAULT.TABLE_HEADER_CLASS,
416                 "headerClass");
417     }
418
419     public String JavaDoc getFooterClass() {
420         return Util.getQualifiedStyleClass(this,
421                 super.getFooterClass(),
422                 CSS_DEFAULT.TABLE_FOOTER_CLASS,
423                 "footerClass");
424     }
425
426 }
427    
428
429   
430
Popular Tags