001: package com.sun.portal.app.sharedtasks.faces.beans;
002:
003: import com.sun.data.provider.RowKey;
004: import com.sun.data.provider.FieldKey;
005: import com.sun.data.provider.impl.TableRowDataProvider;
006: import com.sun.web.ui.event.TableSelectPhaseListener;
007:
008: import java.util.Map;
009: import javax.faces.context.FacesContext;
010:
011: import com.sun.portal.app.sharedtasks.util.CalTaskListItem;
012: import com.sun.portal.app.sharedtasks.util.CalTask;
013: import com.sun.portal.app.sharedtasks.util.SharedConstants;
014:
015: /**
016: * This class provides functionality for select tables.
017: */
018: public class TaskTableSelect {
019:
020: private TableSelectPhaseListener tspl; // Phase listener.
021: private TaskTableContainer tableContainer; // Table data.
022:
023: /** Default constructor. */
024: public TaskTableSelect(TaskTableContainer tableContainer) {
025: this .tableContainer = tableContainer;
026: tspl = new TableSelectPhaseListener();
027: }
028:
029: // UI guidelines recomend items should be unselected when no longer in view.
030: // For example, if a user has selected rows of the table and navigates to
031: // another page. If the user then deletes selected rows, they may
032: // inadvertently remove rows not displayed on the current page. Using the
033: // TableSelect utility class ensures that this scenario does not occur
034: // because state is cleared after the rendering phase.
035:
036: /**
037: * Get current table row.
038: *
039: * Note: To obtain the current table row, the request key must match the
040: * value given to the table's sourceVar attribute.
041: */
042: private RowKey getTableRow() {
043: FacesContext context = FacesContext.getCurrentInstance();
044: Map map = context.getExternalContext().getRequestMap();
045: TableRowDataProvider provider = (TableRowDataProvider) map
046: .get("calendarTask");
047:
048: return (provider != null) ? provider.getTableRow() : null;
049: }
050:
051: /** Get selected property. */
052:
053: /** Get selected property. */
054: public Object getSelected() {
055: return (String) tspl.getSelected(getTableRow());
056: }
057:
058: /** Set selected property. */
059: public void setSelected(Object value) {
060: RowKey rowKey = getTableRow();
061: if (rowKey != null) {
062: tspl.setSelected(getTableRow(), value);
063: }
064: }
065:
066: /** Get selected value property. */
067: public Object getSelectedValue() {
068: FacesContext context = FacesContext.getCurrentInstance();
069: Map map = context.getExternalContext().getRequestMap();
070: TableRowDataProvider provider = (TableRowDataProvider) map
071: .get("calendarTask");
072:
073: FieldKey taskKey = provider.getFieldKey("task");
074: CalTask task = null;
075: if (taskKey != null) {
076: task = (CalTask) provider.getValue(taskKey);
077: }
078:
079: return (task == null) ? null : task.getId()
080: + SharedConstants.ID_RID_SEPARATOR + task.getRid();
081:
082: //RowKey rowKey = getTableRow();
083: //return (rowKey != null) ? rowKey.getRowId() : null;
084: }
085:
086: /** Get the selected state -- Sort on checked state only. */
087: public Boolean getSelectedState() {
088: Object selectedValue = getSelectedValue();
089: return new Boolean((selectedValue != null) ? selectedValue
090: .equals(getSelected()) : false);
091: }
092:
093: // When implementing the UI guidelines, selected items can be retrieved for
094: // the current page using the getSelected method of Checkbox (e.g.,
095: // Checkbox.getSelected("selectGroup")). However, there are cases when
096: // maintaining state across pages is necessary. When maintaining state, set
097: // the selectedRows property for the Table component according to UI
098: // guidelines. That is, even when there are currently no hidden selections, // the number zero should still be shown.
099:
100: /** Used only when maintaining state across pages. */
101:
102: /** Used only when maintaining state across pages. */
103: public int getSelectedRows() {
104: int result = 0;
105:
106: // Get selected rows.
107: CalTaskListItem[] tasks = (CalTaskListItem[]) tableContainer
108: .getDataProvider().getArray();
109:
110: for (int i = 0; i < tasks.length; i++) {
111: if (tasks[i].getSelected()) {
112: result++;
113: }
114: }
115:
116: return result;
117: }
118: }
|