1 5 package org.exoplatform.faces.core.component; 6 7 import java.util.ArrayList ; 8 import java.util.List ; 9 import javax.faces.component.UIComponent; 10 import javax.faces.validator.Validator; 11 import org.exoplatform.container.PortalContainer; 12 import org.exoplatform.faces.ValidatorManager; 13 14 19 public class ComponentUtil { 20 21 static public List findDescendantsOfType(UIComponent comp, Class type) { 22 List list = new ArrayList () ; 23 findDescendantsOfType(comp, type, list) ; 24 return list ; 25 } 26 27 static private void findDescendantsOfType(UIComponent uiComp, Class type, List list) { 28 if(uiComp.getChildCount() == 0) return ; 29 List children = uiComp.getChildren() ; 30 for(int i = 0; i < children.size(); i++) { 31 UIComponent child = (UIComponent) children.get(i) ; 32 if(type.isInstance(child)) list.add(child) ; 33 findDescendantsOfType(child, type,list) ; 34 } 35 } 36 37 static public UIExoComponent findComponentById(UIExoComponent comp, String id) { 38 if(id.equals(comp.getId())) return comp ; 39 List children = comp.getChildren(); 40 for (int i = 0; i < children.size() ; i++) { 41 UIExoComponent uiChild = (UIExoComponent) children.get(i) ; 42 UIExoComponent result = findComponentById(uiChild, id) ; 43 if (result != null) return result ; 44 } 45 return null ; 46 } 47 48 static public UIExoComponent findRenderedComponentById(UIExoComponent uiComp, String id) { 49 if(id.equals(uiComp.getId())) return uiComp ; 50 List children = uiComp.getChildren(); 51 for (int i = 0; i < children.size() ; i++) { 52 UIExoComponent uiChild = (UIExoComponent) children.get(i) ; 53 if(uiChild.isRendered()) { 54 UIExoComponent result = findRenderedComponentById(uiChild, id) ; 55 if (result != null) return result ; 56 } 57 } 58 return null ; 59 } 60 61 static public Object getChildComponentOfType(UIComponent comp, Class classType) { 62 List children = comp.getChildren() ; 63 for (int i = 0; i < children.size(); i++) { 64 Object o = children.get(i) ; 65 if( classType.isInstance(o)) return o ; 66 } 67 return null ; 68 } 69 70 static public UIComponent getAncestorOfType(UIComponent comp, Class classType) { 71 UIComponent parent = comp.getParent() ; 72 while(parent != null ) { 73 if( classType.isInstance(parent)) return parent ; 74 parent = parent.getParent() ; 75 } 76 return null ; 77 } 78 79 static public void setRenderedComponent(UIExoComponent comp, String id) { 80 List children = comp.getChildren() ; 81 for(int i = 0 ; i < children.size(); i++) { 82 UIComponent child = (UIComponent) children.get(i); 83 if(child.getId().equals(id)) child.setRendered(true); 84 else child.setRendered(false) ; 85 } 86 } 87 88 static public void setRenderedComponent(UIExoComponent comp, Class type) { 89 List children = comp.getChildren() ; 90 for(int i = 0 ; i < children.size(); i++) { 91 UIComponent child = (UIComponent)children.get(i); 92 if(type.isInstance(child)) child.setRendered(true); 93 else child.setRendered(false) ; 94 } 95 } 96 97 static public void setRenderedSibling(UIExoComponent comp, Class type) { 98 List children = comp.getParent().getChildren() ; 99 for(int i = 0 ; i < children.size(); i++) { 100 UIComponent child = (UIComponent)children.get(i); 101 if(type.isInstance(child)) child.setRendered(true); 102 else child.setRendered(false) ; 103 } 104 } 105 106 static public UIComponent getSibling(UIComponent comp, Class type) { 107 List children = comp.getParent().getChildren() ; 108 for(int i = 0 ; i < children.size(); i++) { 109 UIComponent child = (UIComponent)children.get(i); 110 if(type.isInstance(child)) return child ; 111 } 112 return null ; 113 } 114 115 static public void addValidator(List list, Class clazz) { 116 PortalContainer pcontainer = PortalContainer.getInstance() ; 117 ValidatorManager manager = 118 (ValidatorManager) pcontainer.getComponentInstanceOfType(ValidatorManager.class) ; 119 Validator result = manager.getValidator(clazz) ; 120 list.add(result) ; 121 } 122 123 static public Validator getValidator(Class clazz) { 124 PortalContainer pcontainer = PortalContainer.getInstance() ; 125 ValidatorManager manager = 126 (ValidatorManager) pcontainer.getComponentInstanceOfType(ValidatorManager.class) ; 127 return manager.getValidator(clazz) ; 128 } 129 } | Popular Tags |