View Javadoc

1   /*
2    * $Id: CardBoxTools.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.Collections;
8   import java.util.List;
9   
10  import javax.faces.FacesException;
11  import javax.faces.component.UIComponent;
12  import javax.faces.model.SelectItem;
13  
14  import org.apache.commons.logging.Log;
15  import org.apache.commons.logging.LogFactory;
16  import org.rcfaces.core.component.CardBoxComponent;
17  import org.rcfaces.core.component.CardComponent;
18  import org.rcfaces.core.component.iterator.ICardIterator;
19  import org.rcfaces.core.internal.listener.CameliaPhaseListener;
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 CardBoxTools {
27      private static final String REVISION = "$Revision: 1.18 $";
28  
29      private static final Log LOG = LogFactory.getLog(CardBoxTools.class);
30  
31      private static final ICardIterator EMPTY_COMPONENT_ITERATOR = new CardListIterator(
32              Collections.EMPTY_LIST);
33  
34      public static ICardIterator listCards(CardBoxComponent component) {
35          List list = ComponentIterators.list(component, CardComponent.class);
36          if (list.isEmpty()) {
37              return EMPTY_COMPONENT_ITERATOR;
38          }
39  
40          return new CardListIterator(list);
41      }
42  
43      public static CardComponent getSelectedCard(CardBoxComponent component) {
44          Object value = component.getSubmittedExternalValue();
45  
46          if (value == null) {
47              value = component.getValue();
48          }
49  
50          if (value instanceof SelectItem) {
51              value = ((SelectItem) value).getValue();
52          }
53  
54          if (value != null) {
55              CardComponent byId = null;
56  
57              ICardIterator iterator = listCards(component);
58              for (; iterator.hasNext();) {
59                  CardComponent card = iterator.next();
60  
61                  if (value.equals(card.getValue())) {
62                      return card;
63                  }
64  
65                  if (value.equals(card.getId())) {
66                      byId = card;
67                  }
68              }
69  
70              return byId; // On retourne NULL si on trouve pas l'onglet
71              /*
72               * if (byId != null) { return byId; }
73               */
74  
75          }
76  
77          // On prend le premier tab dans ce cas !
78  
79          ICardIterator iterator = listCards(component);
80          if (iterator.hasNext() == false) {
81              return null;
82          }
83  
84          return iterator.next();
85      }
86  
87      private static final class CardListIterator extends
88              ComponentIterators.ComponentListIterator implements ICardIterator {
89          private static final String REVISION = "$Revision: 1.18 $";
90  
91          public CardListIterator(List list) {
92              super(list);
93          }
94  
95          public final CardComponent next() {
96              return (CardComponent) nextComponent();
97          }
98  
99          public CardComponent[] toArray() {
100             return (CardComponent[]) toArray(new CardComponent[count()]);
101         }
102     }
103 
104     public static void selectCard(CardBoxComponent component, CardComponent card) {
105         boolean applyingRequestValues = CameliaPhaseListener
106                 .isApplyingRequestValues();
107 
108         if (card == null) {
109             if (applyingRequestValues) {
110                 component.setSubmittedExternalValue(null);
111                 return;
112             }
113             component.setValue(null);
114             return;
115         }
116 
117         Object value = card.getValue();
118         if (value == null) {
119             value = card.getId();
120         }
121 
122         if (applyingRequestValues) {
123             component.setSubmittedExternalValue(value);
124             return;
125         }
126         component.setValue(value);
127     }
128 
129     public static CardBoxComponent getCardBox(CardComponent component) {
130         UIComponent parent = component.getParent();
131 
132         if (parent == null || (parent instanceof CardBoxComponent) == false) {
133             throw new FacesException(
134                     "Invalid parent of Tab component. (Must be a CardBox component) parent='"
135                             + parent + "'");
136         }
137 
138         return (CardBoxComponent) parent;
139     }
140 }