001: /*
002: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
003: *
004: * "The contents of this file are subject to the Mozilla Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License at
007: * http://www.mozilla.org/MPL/
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
011: * License for the specific language governing rights and limitations under
012: * the License.
013: *
014: * The Original Code is ICEfaces 1.5 open source software code, released
015: * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
016: * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
017: * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
018: *
019: * Contributor(s): _____________________.
020: *
021: * Alternatively, the contents of this file may be used under the terms of
022: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
023: * License), in which case the provisions of the LGPL License are
024: * applicable instead of those above. If you wish to allow use of your
025: * version of this file only under the terms of the LGPL License and not to
026: * allow others to use your version of this file under the MPL, indicate
027: * your decision by deleting the provisions above and replace them with
028: * the notice and other provisions required by the LGPL License. If you do
029: * not delete the provisions above, a recipient may use your version of
030: * this file under either the MPL or the LGPL License."
031: *
032: */
033:
034: package com.icesoft.faces.component.ext.renderkit;
035:
036: import com.icesoft.faces.component.ext.HtmlDataTable;
037: import com.icesoft.faces.component.ext.RowSelector;
038: import com.icesoft.faces.component.ext.RowSelectorEvent;
039: import com.icesoft.faces.context.DOMContext;
040: import com.icesoft.faces.renderkit.dom_html_basic.DomBasicRenderer;
041: import com.icesoft.faces.renderkit.dom_html_basic.HTML;
042: import org.w3c.dom.Element;
043:
044: import javax.faces.component.UIComponent;
045: import javax.faces.context.FacesContext;
046: import javax.faces.event.PhaseId;
047: import java.io.IOException;
048: import java.util.StringTokenizer;
049:
050: /**
051: * Created by IntelliJ IDEA. User: rmayhew Date: Aug 28, 2006 Time: 12:48:09 PM
052: * To change this template use File | Settings | File Templates.
053: */
054: public class RowSelectorRenderer extends DomBasicRenderer {
055:
056: // Decode Method
057: public void decode(FacesContext facesContext,
058: UIComponent uiComponent) {
059: //super.decode(facesContext, uiComponent);
060:
061: // Check for row selection in its parent table hidden field
062: HtmlDataTable dataTable = getParentDataTable(uiComponent);
063:
064: String dataTableId = dataTable.getClientId(facesContext);
065: String selectedRowsParameter = TableRenderer
066: .getSelectedRowParameterName(dataTableId);
067: String selectedRows = (String) facesContext
068: .getExternalContext().getRequestParameterMap().get(
069: selectedRowsParameter);
070:
071: if (selectedRows == null || selectedRows.trim().length() == 0) {
072: selectedRows = (String) facesContext.getExternalContext()
073: .getRequestMap().get(
074: RowSelectorRenderer.class.getName());
075: if (selectedRows == null
076: || selectedRows.trim().length() == 0)
077: return;
078: } else {
079: //Myfaces 1.1.0 Work around
080: facesContext.getExternalContext().getRequestMap().put(
081: RowSelectorRenderer.class.getName(), selectedRows);
082: }
083: // What row number am I, was I clicked?
084: int rowIndex = dataTable.getRowIndex();
085: StringTokenizer st = new StringTokenizer(selectedRows, ",");
086: boolean rowClicked = false;
087: while (st.hasMoreTokens()) {
088: int row = Integer.parseInt(st.nextToken());
089: if (row == rowIndex) {
090: rowClicked = true;
091: break;
092: }
093: }
094: RowSelector rowSelector = (RowSelector) uiComponent;
095:
096: try {
097: if (rowClicked) {
098: // Toggle the row selection if multiple
099: boolean b = rowSelector.getValue().booleanValue();
100: b = !b;
101: rowSelector.setValue(new Boolean(b));
102:
103: if (rowSelector.getSelectionListener() != null) {
104: RowSelectorEvent evt = new RowSelectorEvent(
105: rowSelector, rowIndex, b);
106: evt.setPhaseId(PhaseId.APPLY_REQUEST_VALUES);
107:
108: rowSelector.queueEvent(evt);
109: }
110:
111: } else {
112: if (Boolean.FALSE.equals(rowSelector.getMultiple())) {
113: // Clear all other selections
114: rowSelector.setValue(Boolean.FALSE);
115: }
116: }
117: } catch (Exception e) {
118: e.printStackTrace();
119: }
120: }
121:
122: public void encodeEnd(FacesContext facesContext,
123: UIComponent uiComponent) throws IOException {
124:
125: //super.encodeEnd(facesContext, uiComponent);
126:
127: // Nothing is rendered
128: }
129:
130: public void encodeBegin(FacesContext facesContext,
131: UIComponent uiComponent) throws IOException {
132: //super.encodeBegin(facesContext, uiComponent);
133: //uiComponent.setRendered(true);
134: // Mothing is rendered
135: }
136:
137: private static HtmlDataTable getParentDataTable(
138: UIComponent uiComponenent) {
139: UIComponent parentComp = uiComponenent.getParent();
140: if (parentComp == null) {
141: throw new RuntimeException(
142: "RowSelectorRenderer: decode. Could not find an Ice:dataTable as a parent componenent");
143: }
144: if (parentComp instanceof com.icesoft.faces.component.ext.HtmlDataTable) {
145: return (HtmlDataTable) parentComp;
146: }
147: return getParentDataTable(parentComp);
148: }
149:
150: }
|