View Javadoc

1   /*
2    * $Id: RadioButtonTools.java,v 1.18 2011/06/16 09:29:41 jbmeslin Exp $
3    * 
4    */
5   package org.rcfaces.core.internal.tools;
6   
7   import java.util.ArrayList;
8   import java.util.Collections;
9   import java.util.Iterator;
10  import java.util.List;
11  
12  import javax.faces.component.UIComponent;
13  
14  import org.rcfaces.core.component.ImageRadioButtonComponent;
15  import org.rcfaces.core.component.RadioButtonComponent;
16  import org.rcfaces.core.component.capability.IRadioGroupCapability;
17  import org.rcfaces.core.component.capability.ISelectedCapability;
18  import org.rcfaces.core.component.iterator.IImageRadioButtonIterator;
19  import org.rcfaces.core.component.iterator.IRadioButtonIterator;
20  import org.rcfaces.core.internal.util.ComponentIterators;
21  
22  /**
23   * @author Olivier Oeuillot (latest modification by $Author: jbmeslin $)
24   * @version $Revision: 1.18 $ $Date: 2011/06/16 09:29:41 $
25   */
26  public class RadioButtonTools {
27      private static final String REVISION = "$Revision: 1.18 $";
28  
29      private static final IRadioButtonIterator EMPTY_RADIO_BUTTON_COMPONENT_ITERATOR = new RadioButtonListIterator(
30              Collections.EMPTY_LIST);
31  
32      private static final IImageRadioButtonIterator EMPTY_IMAGE_RADIO_BUTTON_COMPONENT_ITERATOR = new ImageRadioButtonListIterator(
33              Collections.EMPTY_LIST);
34  
35      public static RadioButtonComponent getSelectedRadioButtonFromSameGroup(
36              IRadioGroupCapability radioButtonComponent) {
37  
38          return (RadioButtonComponent) getSelectedRadioGroupFromSameGroup(radioButtonComponent);
39      }
40  
41      public static ImageRadioButtonComponent getSelectedImageRadioButtonFromSameGroup(
42              IRadioGroupCapability radioButtonComponent) {
43  
44          return (ImageRadioButtonComponent) getSelectedRadioGroupFromSameGroup(radioButtonComponent);
45      }
46  
47      public static IRadioGroupCapability getSelectedRadioGroupFromSameGroup(
48              IRadioGroupCapability radioButtonComponent) {
49  
50          UIComponent component = (UIComponent) radioButtonComponent;
51          for (; component.getParent() != null;) {
52              component = component.getParent();
53          }
54  
55          return findRadioButtonSelectedWithSameGroupBox(component,
56                  radioButtonComponent.getGroupName());
57      }
58  
59      public static IRadioButtonIterator listRadioButtonSameGroup(
60              IRadioGroupCapability radioButtonComponent) {
61  
62          UIComponent component = (UIComponent) radioButtonComponent;
63          for (; component.getParent() != null;) {
64              component = component.getParent();
65          }
66  
67          List result = new ArrayList(8);
68  
69          listRadioWithSameGroupBox(component, radioButtonComponent
70                  .getGroupName(), result);
71  
72          if (result.isEmpty()) {
73              return EMPTY_RADIO_BUTTON_COMPONENT_ITERATOR;
74          }
75  
76          return new RadioButtonListIterator(result);
77      }
78  
79      public static IImageRadioButtonIterator listImageRadioButtonSameGroup(
80              IRadioGroupCapability radioButtonComponent) {
81  
82          UIComponent component = (UIComponent) radioButtonComponent;
83          for (; component.getParent() != null;) {
84              component = component.getParent();
85          }
86  
87          List result = new ArrayList(8);
88  
89          listRadioWithSameGroupBox(component, radioButtonComponent
90                  .getGroupName(), result);
91  
92          if (result.isEmpty()) {
93              return EMPTY_IMAGE_RADIO_BUTTON_COMPONENT_ITERATOR;
94          }
95  
96          return new ImageRadioButtonListIterator(result);
97      }
98  
99      /**
100      * 
101      * @author Olivier Oeuillot (latest modification by $Author: jbmeslin $)
102      * @version $Revision: 1.18 $ $Date: 2011/06/16 09:29:41 $
103      */
104     private static final class RadioButtonListIterator extends
105             ComponentIterators.ComponentListIterator implements
106             IRadioButtonIterator {
107         private static final String REVISION = "$Revision: 1.18 $";
108 
109         public RadioButtonListIterator(List list) {
110             super(list);
111         }
112 
113         public final RadioButtonComponent next() {
114             return (RadioButtonComponent) nextComponent();
115         }
116 
117         public RadioButtonComponent[] toArray() {
118             return (RadioButtonComponent[]) toArray(new RadioButtonComponent[count()]);
119         }
120     }
121 
122     /**
123      * 
124      * @author Olivier Oeuillot (latest modification by $Author: jbmeslin $)
125      * @version $Revision: 1.18 $ $Date: 2011/06/16 09:29:41 $
126      */
127     private static final class ImageRadioButtonListIterator extends
128             ComponentIterators.ComponentListIterator implements
129             IImageRadioButtonIterator {
130         private static final String REVISION = "$Revision: 1.18 $";
131 
132         public ImageRadioButtonListIterator(List list) {
133             super(list);
134         }
135 
136         public final ImageRadioButtonComponent next() {
137             return (ImageRadioButtonComponent) nextComponent();
138         }
139 
140         public ImageRadioButtonComponent[] toArray() {
141             return (ImageRadioButtonComponent[]) toArray(new ImageRadioButtonComponent[count()]);
142         }
143     }
144 
145     private static final IRadioGroupCapability findRadioButtonSelectedWithSameGroupBox(
146             UIComponent container, String groupName) {
147 
148         for (Iterator i = container.getChildren().iterator(); i.hasNext();) {
149             UIComponent e = (UIComponent) i.next();
150 
151             if (e instanceof IRadioGroupCapability) {
152                 IRadioGroupCapability rb = (IRadioGroupCapability) e;
153 
154                 if (groupName.equals(rb.getGroupName()) == false) {
155                     continue;
156                 }
157 
158                 if (e instanceof ISelectedCapability) {
159                     ISelectedCapability selectedCapability = (ISelectedCapability) e;
160 
161                     if (selectedCapability.isSelected()) {
162                         return rb;
163                     }
164                 }
165 
166                 continue;
167             }
168 
169             if (e.getChildCount() == 0) {
170                 continue;
171             }
172 
173             IRadioGroupCapability rb = findRadioButtonSelectedWithSameGroupBox(
174                     e, groupName);
175             if (rb == null) {
176                 continue;
177             }
178 
179             return rb;
180         }
181 
182         return null;
183     }
184 
185     private static final void listRadioWithSameGroupBox(UIComponent container,
186             String groupName, List list) {
187 
188         for (Iterator i = container.getChildren().iterator(); i.hasNext();) {
189             UIComponent e = (UIComponent) i.next();
190 
191             if (e instanceof IRadioGroupCapability) {
192                 IRadioGroupCapability rb = (IRadioGroupCapability) e;
193 
194                 if (groupName.equals(rb.getGroupName()) == false) {
195                     continue;
196                 }
197 
198                 list.add(e);
199                 continue;
200             }
201 
202             if (e.getChildCount() == 0) {
203                 continue;
204             }
205 
206             listRadioWithSameGroupBox(e, groupName, list);
207         }
208     }
209 
210     /*
211      * public static Object getGroupValue(IRadioValueCapability component) {
212      * IRadioValueCapability radioGroupCapability = (IRadioValueCapability)
213      * getSelectedRadioGroupFromSameGroup(component); if (radioGroupCapability ==
214      * null) { return null; }
215      * 
216      * return getValue(radioGroupCapability); }
217      * 
218      * 
219      * public static void setGroupValue(IComponentEngine engine,
220      * IRadioValueCapability component, Object value) {
221      * 
222      * if (value instanceof ValueExpression) {
223      * engine.setProperty(Properties.GROUP_VALUE, value); return; }
224      * 
225      * List list = new ArrayList(8);
226      * 
227      * listRadioWithSameGroupBox((UIComponent) component, component
228      * .getGroupName(), list); if (list.isEmpty()) { return; }
229      * 
230      * for (Iterator it = list.iterator(); it.hasNext();) {
231      * IRadioValueCapability radio = (IRadioValueCapability) it.next();
232      * 
233      * boolean state = false;
234      * 
235      * if (value != null && value.equals(getValue(radio))) { state = true; }
236      * 
237      * select(radio, state); }
238      * 
239      * engine.setProperty(Properties.GROUP_VALUE, value); }
240      * 
241      * private static Object getValue(IRadioValueCapability
242      * groupValueCapability) { if (groupValueCapability instanceof ValueHolder) {
243      * return ((ValueHolder) groupValueCapability).getValue(); }
244      * 
245      * if (groupValueCapability instanceof UISelectItem) { return
246      * ((UISelectItem) groupValueCapability).getItemValue(); }
247      * 
248      * throw new FacesException("Unknown type of groupValueCapability: " +
249      * groupValueCapability); }
250      * 
251      * private static void select(IRadioValueCapability groupValueCapability,
252      * boolean state) {
253      * 
254      * if (groupValueCapability instanceof ISelectedCapability) {
255      * ISelectedCapability selectedCapability = (ISelectedCapability)
256      * groupValueCapability;
257      * 
258      * if (selectedCapability.isSelected() != state) {
259      * selectedCapability.setSelected(state); } return; }
260      * 
261      * if (groupValueCapability instanceof ICheckedCapability) {
262      * ICheckedCapability checkedCapability = (ICheckedCapability)
263      * groupValueCapability; if (checkedCapability.isChecked() != state) {
264      * checkedCapability.setChecked(state); } return; }
265      * 
266      * throw new FacesException("Unknown to select groupValueCapability: " +
267      * groupValueCapability); }
268      */
269 
270 }