KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > faces > core > component > ComponentUtil


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.faces.core.component;
6
7 import java.util.ArrayList JavaDoc;
8 import java.util.List JavaDoc;
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 /**
15  * @author Tuan Nguyen (tuan08@users.sourceforge.net)
16  * @since Jan 19, 2005
17  * @version $Id$
18  */

19 public class ComponentUtil {
20   
21   static public List JavaDoc findDescendantsOfType(UIComponent comp, Class JavaDoc type) {
22     List JavaDoc list = new ArrayList JavaDoc() ;
23     findDescendantsOfType(comp, type, list) ;
24     return list ;
25   }
26   
27   static private void findDescendantsOfType(UIComponent uiComp, Class JavaDoc type, List JavaDoc list) {
28     if(uiComp.getChildCount() == 0) return ;
29     List JavaDoc 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 JavaDoc id) {
38     if(id.equals(comp.getId())) return comp ;
39     List JavaDoc 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 JavaDoc id) {
49     if(id.equals(uiComp.getId())) return uiComp ;
50     List JavaDoc 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 JavaDoc getChildComponentOfType(UIComponent comp, Class JavaDoc classType) {
62     List JavaDoc children = comp.getChildren() ;
63     for (int i = 0; i < children.size(); i++) {
64       Object JavaDoc 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 JavaDoc 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 JavaDoc id) {
80     List JavaDoc 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 JavaDoc type) {
89     List JavaDoc 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 JavaDoc type) {
98     List JavaDoc 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 JavaDoc type) {
107     List JavaDoc 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 JavaDoc list, Class JavaDoc 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 JavaDoc clazz) {
124     PortalContainer pcontainer = PortalContainer.getInstance() ;
125     ValidatorManager manager =
126       (ValidatorManager) pcontainer.getComponentInstanceOfType(ValidatorManager.class) ;
127     return manager.getValidator(clazz) ;
128   }
129 }
Popular Tags