01: package com.sun.portal.app.sharedevents.faces;
02:
03: import com.sun.data.provider.RowKey;
04: import com.sun.data.provider.impl.TableRowDataProvider;
05: import com.sun.web.ui.event.TableSelectPhaseListener;
06:
07: import java.util.Map;
08: import javax.faces.context.FacesContext;
09:
10: /**
11: * This class provides functionality for select tables.
12: */
13: public class Select {
14: private TableSelectPhaseListener tspl = null; // Phase listener.
15: private EventsTableContainer data = null; // Table data.
16:
17: /** Default constructor. */
18: public Select(EventsTableContainer data) {
19: this .data = data;
20: tspl = new TableSelectPhaseListener();
21: }
22:
23: // UI guidelines recomend items should be unselected when no longer in view. // For example, if a user has selected rows of the table and navigates to
24: // another page. If the user then deletes selected rows, they may
25: // inadvertently remove rows not displayed on the current page. Using the
26: // TableSelect utility class ensures that this scenario does not occur
27: // because state is cleared after the rendering phase.
28:
29: /**
30: * Get current table row.
31: *
32: * Note: To obtain the current table row, the request key must match the
33: * value given to the table's sourceVar attribute.
34: */
35: private RowKey getTableRow() {
36: FacesContext context = FacesContext.getCurrentInstance();
37: Map map = context.getExternalContext().getRequestMap();
38: TableRowDataProvider provider = (TableRowDataProvider) map
39: .get("testEvent");
40: return (provider != null) ? provider.getTableRow() : null;
41: }
42:
43: /** Get selected property. */
44:
45: /** Get selected property. */
46: public Object getSelected() {
47: return (String) tspl.getSelected(getTableRow());
48: }
49:
50: /** Set selected property. */
51: public void setSelected(Object value) {
52: RowKey rowKey = getTableRow();
53: if (rowKey != null) {
54: tspl.setSelected(getTableRow(), value);
55: }
56: }
57:
58: /** Get selected value property. */
59: public Object getSelectedValue() {
60:
61: RowKey rowKey = getTableRow();
62: return (rowKey != null) ? rowKey.getRowId() : null;
63:
64: }
65:
66: /** Get the selected state -- Sort on checked state only. */
67: public Boolean getSelectedState() {
68: Object selectedValue = getSelectedValue();
69: return new Boolean((selectedValue != null) ? selectedValue
70: .equals(getSelected()) : false);
71: }
72:
73: // When implementing the UI guidelines, selected items can be retrieved for
74: // the current page using the getSelected method of Checkbox (e.g.,
75: // Checkbox.getSelected("selectGroup")). However, there are cases when
76: // maintaining state across pages is necessary. When maintaining state, set
77: // the selectedRows property for the Table component according to UI
78: // guidelines. That is, even when there are currently no hidden selections, // the number zero should still be shown.
79:
80: /** Used only when maintaining state across pages. */
81:
82: /** Used only when maintaining state across pages. */
83: public int getSelectedRows() {
84: int result = 0;
85:
86: // Get selected rows.
87: CalEvent[] evt = (CalEvent[]) data.getEventsListProvider()
88: .getArray();
89:
90: for (int i = 0; i < evt.length; i++) {
91: if (evt[i].getSelected()) {
92: result++;
93: }
94: }
95: return result;
96: }
97: }
|