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;
035:
036: import com.icesoft.faces.context.effects.JavascriptContext;
037: import com.icesoft.faces.component.CSS_DEFAULT;
038: import com.icesoft.faces.component.ext.renderkit.TableRenderer;
039: import com.icesoft.faces.component.ext.taglib.Util;
040:
041: import javax.faces.component.UIComponentBase;
042: import javax.faces.component.UIComponent;
043: import javax.faces.context.FacesContext;
044: import javax.faces.el.MethodBinding;
045: import javax.faces.el.ValueBinding;
046: import javax.faces.event.FacesEvent;
047: import javax.faces.event.PhaseId;
048: import java.io.IOException;
049: import java.util.StringTokenizer;
050:
051: /**
052: * Created by IntelliJ IDEA. User: rmayhew Date: Aug 28, 2006 Time: 12:45:26 PM
053: * To change this template use File | Settings | File Templates.
054: */
055: public class RowSelector extends UIComponentBase {
056: private Boolean value;
057: private Boolean toggleOnClick;
058: // private Listener
059: private Boolean multiple;
060: private String mouseOverClass;
061: private String selectedClass;
062: private String selectedMouseOverClass;
063: private MethodBinding selectionListener;
064: private MethodBinding selectionAction;
065: private Integer clickedRow;
066:
067: public static final String COMPONENT_TYPE = "com.icesoft.faces.RowSelector";
068: public static final String RENDERER_TYPE = "com.icesoft.faces.RowSelectorRenderer";
069: public static final String COMPONENT_FAMILY = "com.icesoft.faces.RowSelectorFamily";
070:
071: public RowSelector() {
072: JavascriptContext.includeLib(JavascriptContext.ICE_EXTRAS,
073: getFacesContext());
074: }
075:
076: public String getFamily() {
077: return COMPONENT_FAMILY;
078: }
079:
080: public Boolean getValue() {
081: ValueBinding vb = getValueBinding("value");
082: if (vb != null) {
083: return (Boolean) vb.getValue(getFacesContext());
084: }
085: if (value != null) {
086: return value;
087: }
088: return Boolean.FALSE;
089: }
090:
091: public void setValue(Boolean value) {
092: ValueBinding vb = getValueBinding("value");
093: if (vb != null) {
094: vb.setValue(getFacesContext(), value);
095: } else {
096: this .value = value;
097: }
098: }
099:
100: public Integer getClickedRow() {
101: ValueBinding vb = getValueBinding("clickedRow");
102: if (vb != null) {
103: return (Integer) vb.getValue(getFacesContext());
104: }
105: if (clickedRow != null) {
106: return clickedRow;
107: }
108: return new Integer(-1);
109: }
110:
111: public void setClickedRow(Integer clickedRow) {
112: ValueBinding vb = getValueBinding("clickedRow");
113: if (vb != null) {
114: vb.setValue(getFacesContext(), clickedRow);
115: } else {
116: this .clickedRow = clickedRow;
117: }
118: }
119:
120: public Boolean getMultiple() {
121: ValueBinding vb = getValueBinding("multiple");
122: if (vb != null) {
123: return (Boolean) vb.getValue(getFacesContext());
124: }
125: if (multiple != null) {
126: return multiple;
127: }
128: return Boolean.FALSE;
129: }
130:
131: public void setMultiple(Boolean multiple) {
132: this .multiple = multiple;
133: }
134:
135: public Boolean getToggleOnClick() {
136: ValueBinding vb = getValueBinding("toggleOnClick");
137: if (vb != null) {
138: return (Boolean) vb.getValue(getFacesContext());
139: }
140: if (toggleOnClick != null) {
141: return toggleOnClick;
142: }
143: return Boolean.TRUE;
144: }
145:
146: public void setToggleOnClick(Boolean toggleOnClick) {
147: this .toggleOnClick = toggleOnClick;
148: }
149:
150: public String getMouseOverClass() {
151: return Util.getQualifiedStyleClass(this , mouseOverClass,
152: CSS_DEFAULT.ROW_SELECTION_MOUSE_OVER, "mouseOverClass");
153: }
154:
155: public void setMouseOverClass(String mouseOverClass) {
156: this .mouseOverClass = mouseOverClass;
157: }
158:
159: public String getSelectedClass() {
160: return Util.getQualifiedStyleClass(this , selectedClass,
161: CSS_DEFAULT.ROW_SELECTION_SELECTED, "selectedClass");
162: }
163:
164: public void setSelectedClass(String selectedClass) {
165: this .selectedClass = selectedClass;
166: }
167:
168: public String getSelectedMouseOverClass() {
169: return Util.getQualifiedStyleClass(this ,
170: selectedMouseOverClass,
171: CSS_DEFAULT.ROW_SELECTION_SELECTED_MOUSE_OVER,
172: "selectedMouseOverClass");
173: }
174:
175: public void setSelectedMouseOverClass(String selectedMouseOverClass) {
176: this .selectedMouseOverClass = selectedMouseOverClass;
177: }
178:
179: public MethodBinding getSelectionListener() {
180: return selectionListener;
181: }
182:
183: public void setSelectionListener(MethodBinding selectionListener) {
184: this .selectionListener = selectionListener;
185: }
186:
187: public MethodBinding getSelectionAction() {
188: return selectionAction;
189: }
190:
191: public void setSelectionAction(MethodBinding selectionListener) {
192: this .selectionAction = selectionListener;
193: }
194:
195: public void processDecodes(FacesContext facesContext) {
196: // Check for row selection in its parent table hidden field
197: HtmlDataTable dataTable = getParentDataTable(this );
198:
199: String dataTableId = dataTable.getClientId(facesContext);
200: String selectedRowsParameter = TableRenderer
201: .getSelectedRowParameterName(dataTableId);
202: String selectedRows = (String) facesContext
203: .getExternalContext().getRequestParameterMap().get(
204: selectedRowsParameter);
205:
206: if (selectedRows == null || selectedRows.trim().length() == 0) {
207: return;
208: }
209:
210: // What row number am I, was I clicked?
211: int rowIndex = dataTable.getRowIndex();
212: StringTokenizer st = new StringTokenizer(selectedRows, ",");
213: boolean rowClicked = false;
214: while (st.hasMoreTokens()) {
215: int row = Integer.parseInt(st.nextToken());
216: if (row == rowIndex) {
217: rowClicked = true;
218: break;
219: }
220: }
221: RowSelector rowSelector = (RowSelector) this ;
222:
223: try {
224: if (rowClicked) {
225: // Toggle the row selection if multiple
226: boolean b = rowSelector.getValue().booleanValue();
227: b = !b;
228: rowSelector.setValue(new Boolean(b));
229: setClickedRow(new Integer(rowIndex));
230: if (rowSelector.getSelectionListener() != null) {
231: RowSelectorEvent evt = new RowSelectorEvent(
232: rowSelector, rowIndex, b);
233: evt.setPhaseId(PhaseId.APPLY_REQUEST_VALUES);
234:
235: rowSelector.queueEvent(evt);
236: }
237: if (rowSelector.getSelectionAction() != null) {
238: rowSelector.getSelectionAction().invoke(
239: facesContext, null);
240: }
241:
242: } else {
243: if (Boolean.FALSE.equals(rowSelector.getMultiple())) {
244: // Clear all other selections
245: rowSelector.setValue(Boolean.FALSE);
246: }
247: }
248: } catch (Exception e) {
249: e.printStackTrace();
250: }
251:
252: }
253:
254: private static HtmlDataTable getParentDataTable(
255: UIComponent uiComponenent) {
256: UIComponent parentComp = uiComponenent.getParent();
257: if (parentComp == null) {
258: throw new RuntimeException(
259: "RowSelectorRenderer: decode. Could not find an Ice:dataTable as a parent componenent");
260: }
261: if (parentComp instanceof com.icesoft.faces.component.ext.HtmlDataTable) {
262: return (HtmlDataTable) parentComp;
263: }
264: return getParentDataTable(parentComp);
265: }
266:
267: public void encodeEnd(FacesContext facesContext) throws IOException {
268:
269: //super.encodeEnd(facesContext, uiComponent);
270:
271: // Nothing is rendered
272: }
273:
274: public void encodeBegin(FacesContext facesContext)
275: throws IOException {
276:
277: //super.encodeBegin(facesContext, uiComponent);
278: //uiComponent.setRendered(true);
279: // Mothing is rendered
280: }
281:
282: public void broadcast(FacesEvent event) {
283:
284: super .broadcast(event);
285: if (event instanceof RowSelectorEvent
286: && selectionListener != null) {
287:
288: selectionListener.invoke(getFacesContext(),
289: new Object[] { (RowSelectorEvent) event });
290:
291: }
292: }
293:
294: public Object saveState(FacesContext context) {
295: Object[] state = new Object[12];
296: state[0] = super .saveState(context);
297: state[1] = value;
298: state[2] = multiple;
299: state[3] = mouseOverClass;
300: state[4] = selectedClass;
301: state[5] = selectionListener;
302: return state;
303: }
304:
305: public void restoreState(FacesContext context, Object stateIn) {
306: Object[] state = (Object[]) stateIn;
307: super .restoreState(context, state[0]);
308: value = (Boolean) state[1];
309: multiple = (Boolean) state[2];
310: mouseOverClass = (String) state[3];
311: selectedClass = (String) state[4];
312: selectionListener = (MethodBinding) state[5];
313: }
314:
315: String styleClass;
316:
317: /**
318: * <p>Set the value of the <code>styleClass</code> property.</p>
319: */
320: public void setStyleClass(String styleClass) {
321: this .styleClass = styleClass;
322: }
323:
324: /**
325: * <p>Return the value of the <code>styleClass</code> property.</p>
326: */
327: public String getStyleClass() {
328: return Util.getQualifiedStyleClass(this , styleClass,
329: CSS_DEFAULT.ROW_SELECTION_BASE, "styleClass");
330: }
331: }
|