1 16 package org.apache.myfaces.application; 17 18 import org.apache.myfaces.util.ClassUtils; 19 20 import javax.faces.component.UIComponent; 21 import javax.faces.component.UIViewRoot; 22 import java.io.Serializable ; 23 import java.util.ArrayList ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.Map ; 27 28 32 public class TreeStructureManager 33 { 34 36 38 public TreeStructureManager() 39 { 40 } 42 43 public Object buildTreeStructureToSave(UIViewRoot viewRoot) 44 { 45 return internalBuildTreeStructureToSave(viewRoot); 46 } 47 48 private TreeStructComponent internalBuildTreeStructureToSave(UIComponent component) 49 { 50 TreeStructComponent structComp = new TreeStructComponent(component.getClass().getName(), 51 component.getId()); 52 53 if (component.getChildCount() > 0) 55 { 56 List childList = component.getChildren(); 57 List structChildList = new ArrayList (); 58 for (int i = 0, len = childList.size(); i < len; i++) 59 { 60 UIComponent child = (UIComponent)childList.get(i); 61 if (!child.isTransient()) 62 { 63 TreeStructComponent structChild = internalBuildTreeStructureToSave(child); 64 structChildList.add(structChild); 65 } 66 } 67 TreeStructComponent[] childArray = (TreeStructComponent[])structChildList.toArray(new TreeStructComponent[structChildList.size()]); 68 structComp.setChildren(childArray); 69 } 70 71 Map facetMap = component.getFacets(); 73 if (!facetMap.isEmpty()) 74 { 75 List structFacetList = new ArrayList (); 76 for (Iterator it = facetMap.entrySet().iterator(); it.hasNext(); ) 77 { 78 Map.Entry entry = (Map.Entry )it.next(); 79 UIComponent child = (UIComponent)entry.getValue(); 80 if (!child.isTransient()) 81 { 82 String facetName = (String )entry.getKey(); 83 TreeStructComponent structChild = internalBuildTreeStructureToSave(child); 84 structFacetList.add(new Object [] {facetName, structChild}); 85 } 86 } 87 Object [] facetArray = structFacetList.toArray(new Object [structFacetList.size()]); 88 structComp.setFacets(facetArray); 89 } 90 91 return structComp; 92 } 93 94 95 public UIViewRoot restoreTreeStructure(Object treeStructRoot) 96 { 97 if (treeStructRoot instanceof TreeStructComponent) 98 { 99 return (UIViewRoot)internalRestoreTreeStructure((TreeStructComponent)treeStructRoot); 100 } 101 else 102 { 103 throw new IllegalArgumentException ("TreeStructure of type " + treeStructRoot.getClass().getName() + " is not supported."); 104 } 105 } 106 107 private UIComponent internalRestoreTreeStructure(TreeStructComponent treeStructComp) 108 { 109 String compClass = treeStructComp.getComponentClass(); 110 String compId = treeStructComp.getComponentId(); 111 UIComponent component = (UIComponent)ClassUtils.newInstance(compClass); 112 component.setId(compId); 113 114 TreeStructComponent[] childArray = treeStructComp.getChildren(); 116 if (childArray != null) 117 { 118 List childList = component.getChildren(); 119 for (int i = 0, len = childArray.length; i < len; i++) 120 { 121 UIComponent child = internalRestoreTreeStructure(childArray[i]); 122 childList.add(child); 123 } 124 } 125 126 Object [] facetArray = treeStructComp.getFacets(); 128 if (facetArray != null) 129 { 130 Map facetMap = component.getFacets(); 131 for (int i = 0, len = facetArray.length; i < len; i++) 132 { 133 Object [] tuple = (Object [])facetArray[i]; 134 String facetName = (String )tuple[0]; 135 TreeStructComponent structChild = (TreeStructComponent)tuple[1]; 136 UIComponent child = internalRestoreTreeStructure(structChild); 137 facetMap.put(facetName, child); 138 } 139 } 140 141 return component; 142 } 143 144 145 public static class TreeStructComponent 146 implements Serializable 147 { 148 private String _componentClass; 149 private String _componentId; 150 private TreeStructComponent[] _children = null; private Object [] _facets = null; 153 TreeStructComponent(String componentClass, String componentId) 154 { 155 _componentClass = componentClass; 156 _componentId = componentId; 157 } 158 159 public String getComponentClass() 160 { 161 return _componentClass; 162 } 163 164 public String getComponentId() 165 { 166 return _componentId; 167 } 168 169 void setChildren(TreeStructComponent[] children) 170 { 171 _children = children; 172 } 173 174 TreeStructComponent[] getChildren() 175 { 176 return _children; 177 } 178 179 Object [] getFacets() 180 { 181 return _facets; 182 } 183 184 void setFacets(Object [] facets) 185 { 186 _facets = facets; 187 } 188 } 189 190 } 191 | Popular Tags |