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