View Javadoc

1   /*
2    * $Id: TreeTools.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.Set;
8   
9   import javax.faces.FacesException;
10  import javax.faces.component.UIComponent;
11  import javax.faces.component.UISelectItem;
12  import javax.faces.context.FacesContext;
13  
14  import org.rcfaces.core.component.TreeComponent;
15  import org.rcfaces.core.component.TreeNodeComponent;
16  import org.rcfaces.core.internal.tools.CollectionTools.IComponentValueType;
17  import org.rcfaces.core.lang.OrderedSet;
18  
19  /**
20   * 
21   * @author Olivier Oeuillot (latest modification by $Author: jbmeslin $)
22   * @version $Revision: 1.18 $ $Date: 2011/06/16 09:29:41 $
23   */
24  public class TreeTools {
25      private static final String REVISION = "$Revision: 1.18 $";
26  
27      private static final Object OBJECT_EMPTY_ARRAY[] = new Object[0];
28  
29      public static void setExpanded(FacesContext facesContext,
30              TreeComponent treeComponent, Object itemValue, boolean expanded) {
31  
32          if (expanded) {
33              ExpansionTools.expand(facesContext, treeComponent, itemValue);
34              return;
35          }
36  
37          ExpansionTools.collapse(facesContext, treeComponent, itemValue);
38      }
39  
40      public static boolean isExpanded(FacesContext facesContext,
41              TreeComponent treeComponent, Object itemValue) {
42  
43          if (itemValue == null) {
44              return false;
45          }
46  
47          Set values = ExpansionTools.expansionValuesToSet(facesContext,
48                  treeComponent, true);
49  
50          if (values == null || values.isEmpty()) {
51              return false;
52          }
53  
54          return values.contains(itemValue);
55      }
56  
57      public static TreeComponent getTree(TreeNodeComponent component) {
58          UIComponent parent = component.getParent();
59  
60          for (; parent != null; parent = parent.getParent()) {
61              if (parent instanceof TreeComponent) {
62                  return (TreeComponent) parent;
63              }
64  
65              if (parent instanceof UISelectItem) {
66                  continue;
67              }
68  
69              throw new FacesException(
70                      "Invalid parent of TreeNode component. (Parent must be a Tree or a UISelectItem).");
71          }
72  
73          throw new FacesException("Parent of TreeNode component not found !");
74      }
75  
76      public static final IComponentValueType TREE_VALUE_TYPE = new IComponentValueType() {
77          private static final String REVISION = "$Revision: 1.18 $";
78  
79          public Object createNewValue(UIComponent component) {
80              return new OrderedSet();
81          }
82      };
83  
84  }