KickJava   Java API By Example, From Geeks To Geeks.

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


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.context.effects.JavascriptContext;
37 import com.icesoft.faces.component.CSS_DEFAULT;
38 import com.icesoft.faces.component.ext.renderkit.TableRenderer;
39 import com.icesoft.faces.component.ext.taglib.Util;
40
41 import javax.faces.component.UIComponentBase;
42 import javax.faces.component.UIComponent;
43 import javax.faces.context.FacesContext;
44 import javax.faces.el.MethodBinding;
45 import javax.faces.el.ValueBinding;
46 import javax.faces.event.FacesEvent;
47 import javax.faces.event.PhaseId;
48 import java.io.IOException JavaDoc;
49 import java.util.StringTokenizer JavaDoc;
50
51 /**
52  * Created by IntelliJ IDEA. User: rmayhew Date: Aug 28, 2006 Time: 12:45:26 PM
53  * To change this template use File | Settings | File Templates.
54  */

55 public class RowSelector extends UIComponentBase {
56     private Boolean JavaDoc value;
57     private Boolean JavaDoc toggleOnClick;
58     // private Listener
59
private Boolean JavaDoc multiple;
60     private String JavaDoc mouseOverClass;
61     private String JavaDoc selectedClass;
62     private MethodBinding selectionListener;
63     private MethodBinding selectionAction;
64     private Integer JavaDoc clickedRow;
65     
66
67     public static final String JavaDoc COMPONENT_TYPE = "com.icesoft.faces.RowSelector";
68     public static final String JavaDoc RENDERER_TYPE =
69             "com.icesoft.faces.RowSelectorRenderer";
70     public static final String JavaDoc COMPONENT_FAMILY =
71             "com.icesoft.faces.RowSelectorFamily";
72
73     public RowSelector(){
74        JavascriptContext
75                .includeLib(JavascriptContext.ICE_EXTRAS, getFacesContext());
76     }
77
78     public String JavaDoc getFamily() {
79         return COMPONENT_FAMILY;
80     }
81
82     public Boolean JavaDoc getValue() {
83         ValueBinding vb = getValueBinding("value");
84         if (vb != null) {
85             return (Boolean JavaDoc) vb.getValue(getFacesContext());
86         }
87         if (value != null) {
88             return value;
89         }
90         return Boolean.FALSE;
91     }
92
93     public void setValue(Boolean JavaDoc value) {
94         ValueBinding vb = getValueBinding("value");
95         if (vb != null) {
96             vb.setValue(getFacesContext(), value);
97         } else {
98             this.value = value;
99         }
100     }
101
102     public Integer JavaDoc getClickedRow() {
103         ValueBinding vb = getValueBinding("clickedRow");
104         if (vb != null) {
105             return (Integer JavaDoc) vb.getValue(getFacesContext());
106         }
107         if (clickedRow != null) {
108             return clickedRow;
109         }
110         return new Integer JavaDoc(-1);
111     }
112
113     public void setClickedRow(Integer JavaDoc clickedRow) {
114         ValueBinding vb = getValueBinding("clickedRow");
115         if (vb != null) {
116             vb.setValue(getFacesContext(), clickedRow);
117         } else {
118             this.clickedRow = clickedRow;
119         }
120     }
121
122     public Boolean JavaDoc getMultiple() {
123         ValueBinding vb = getValueBinding("multiple");
124         if (vb != null) {
125             return (Boolean JavaDoc) vb.getValue(getFacesContext());
126         }
127         if (multiple != null) {
128             return multiple;
129         }
130         return Boolean.FALSE;
131     }
132
133     public void setMultiple(Boolean JavaDoc multiple) {
134         this.multiple = multiple;
135     }
136     
137     public Boolean JavaDoc getToggleOnClick() {
138         ValueBinding vb = getValueBinding("toggleOnClick");
139         if (vb != null) {
140             return (Boolean JavaDoc) vb.getValue(getFacesContext());
141         }
142         if (toggleOnClick != null) {
143             return toggleOnClick;
144         }
145         return Boolean.TRUE;
146     }
147
148     public void setToggleOnClick(Boolean JavaDoc toggleOnClick) {
149         this.toggleOnClick = toggleOnClick;
150     }
151     
152
153     public String JavaDoc getMouseOverClass() {
154         return Util.getQualifiedStyleClass(this,
155                 mouseOverClass,
156                 CSS_DEFAULT.ROW_SELECTION_MOUSE_OVER,
157                 "mouseOverClass");
158     }
159
160     public void setMouseOverClass(String JavaDoc mouseOverClass) {
161         this.mouseOverClass = mouseOverClass;
162     }
163
164     public String JavaDoc getSelectedClass() {
165         return Util.getQualifiedStyleClass(this,
166                 selectedClass,
167                 CSS_DEFAULT.ROW_SELECTION_SELECTED,
168                 "selectedClass");
169     }
170
171     public void setSelectedClass(String JavaDoc selectedClass) {
172         this.selectedClass = selectedClass;
173     }
174
175     public MethodBinding getSelectionListener() {
176         return selectionListener;
177     }
178
179     public void setSelectionListener(MethodBinding selectionListener) {
180         this.selectionListener = selectionListener;
181     }
182
183     public MethodBinding getSelectionAction() {
184          return selectionAction;
185      }
186
187      public void setSelectionAction(MethodBinding selectionListener) {
188          this.selectionAction = selectionListener;
189      }
190
191
192
193     public void processDecodes(FacesContext facesContext){
194         // Check for row selection in its parent table hidden field
195
HtmlDataTable dataTable = getParentDataTable(this);
196
197         String JavaDoc dataTableId = dataTable.getClientId(facesContext);
198         String JavaDoc selectedRowsParameter =
199                 TableRenderer.getSelectedRowParameterName(dataTableId);
200         String JavaDoc selectedRows = (String JavaDoc) facesContext.getExternalContext()
201                 .getRequestParameterMap().get(selectedRowsParameter);
202
203         if (selectedRows == null || selectedRows.trim().length() == 0) {
204             return;
205         }
206
207         // What row number am I, was I clicked?
208
int rowIndex = dataTable.getRowIndex();
209         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(selectedRows, ",");
210         boolean rowClicked = false;
211         while (st.hasMoreTokens()) {
212             int row = Integer.parseInt(st.nextToken());
213             if (row == rowIndex) {
214                 rowClicked = true;
215                 break;
216             }
217         }
218         RowSelector rowSelector = (RowSelector) this;
219
220         try {
221             if (rowClicked) {
222                 // Toggle the row selection if multiple
223
boolean b = rowSelector.getValue().booleanValue();
224                 b = !b;
225                 rowSelector.setValue(new Boolean JavaDoc(b));
226                 setClickedRow(new Integer JavaDoc(rowIndex));
227                 if (rowSelector.getSelectionListener() != null) {
228                     RowSelectorEvent evt =
229                             new RowSelectorEvent(rowSelector, rowIndex, b);
230                     evt.setPhaseId(PhaseId.APPLY_REQUEST_VALUES);
231
232                     rowSelector.queueEvent(evt);
233                 } if(rowSelector.getSelectionAction() != null){
234                     rowSelector.getSelectionAction().invoke(facesContext, null);
235                 }
236          
237
238             } else {
239                 if (Boolean.FALSE.equals(rowSelector.getMultiple())) {
240                     // Clear all other selections
241
rowSelector.setValue(Boolean.FALSE);
242                 }
243             }
244         } catch (Exception JavaDoc e) {
245             e.printStackTrace();
246         }
247
248
249
250     }
251      private static HtmlDataTable getParentDataTable(UIComponent uiComponenent) {
252         UIComponent parentComp = uiComponenent.getParent();
253         if (parentComp == null) {
254             throw new RuntimeException JavaDoc(
255                     "RowSelectorRenderer: decode. Could not find an Ice:dataTable as a parent componenent");
256         }
257         if (parentComp instanceof com.icesoft.faces.component.ext.HtmlDataTable) {
258             return (HtmlDataTable) parentComp;
259         }
260         return getParentDataTable(parentComp);
261     }
262
263     public void encodeEnd(FacesContext facesContext)
264             throws IOException JavaDoc {
265                
266         //super.encodeEnd(facesContext, uiComponent);
267

268         // Nothing is rendered
269
}
270
271     public void encodeBegin(FacesContext facesContext)
272             throws IOException JavaDoc {
273     
274         //super.encodeBegin(facesContext, uiComponent);
275
//uiComponent.setRendered(true);
276
// Mothing is rendered
277
}
278
279     public void broadcast(FacesEvent event) {
280
281         super.broadcast(event);
282         if (event instanceof RowSelectorEvent && selectionListener != null) {
283
284             selectionListener.invoke(getFacesContext(),
285                                      new Object JavaDoc[]{(RowSelectorEvent) event});
286
287         }
288     }
289
290     public Object JavaDoc saveState(FacesContext context) {
291         Object JavaDoc[] state = new Object JavaDoc[12];
292         state[0] = super.saveState(context);
293         state[1] = value;
294         state[2] = multiple;
295         state[3] = mouseOverClass;
296         state[4] = selectedClass;
297         state[5] = selectionListener;
298         return state;
299     }
300
301     public void restoreState(FacesContext context, Object JavaDoc stateIn) {
302         Object JavaDoc[] state = (Object JavaDoc[]) stateIn;
303         super.restoreState(context, state[0]);
304         value = (Boolean JavaDoc) state[1];
305         multiple = (Boolean JavaDoc) state[2];
306         mouseOverClass = (String JavaDoc) state[3];
307         selectedClass = (String JavaDoc) state[4];
308         selectionListener = (MethodBinding) state[5];
309     }
310     
311     String JavaDoc styleClass;
312     /**
313      * <p>Set the value of the <code>styleClass</code> property.</p>
314      */

315     public void setStyleClass(String JavaDoc styleClass) {
316         this.styleClass = styleClass;
317     }
318
319     /**
320      * <p>Return the value of the <code>styleClass</code> property.</p>
321      */

322     public String JavaDoc getStyleClass() {
323         return Util.getQualifiedStyleClass(this,
324                 styleClass,
325                 CSS_DEFAULT.ROW_SELECTION_BASE,
326                 "styleClass");
327     }
328 }
329
Popular Tags