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 public TreeStructureManager()
39 {
40
41 }
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
54 if (component.getChildCount() > 0)
55 {
56 List<TreeStructComponent> structChildList = new ArrayList<TreeStructComponent>();
57 for (int i = 0, childCount = component.getChildCount(); i < childCount; i++)
58 {
59 UIComponent child = component.getChildren().get(i);
60 if (!child.isTransient())
61 {
62 TreeStructComponent structChild = internalBuildTreeStructureToSave(child);
63 structChildList.add(structChild);
64 }
65 }
66
67 TreeStructComponent[] childArray = structChildList.toArray(new TreeStructComponent[structChildList.size()]);
68 structComp.setChildren(childArray);
69 }
70
71
72
73 if (component.getFacetCount() > 0)
74 {
75 Map<String, UIComponent> facetMap = component.getFacets();
76 List<Object[]> structFacetList = new ArrayList<Object[]>();
77 for (Map.Entry<String, UIComponent> entry : facetMap.entrySet())
78 {
79 UIComponent child = entry.getValue();
80 if (!child.isTransient())
81 {
82 String facetName = entry.getKey();
83 TreeStructComponent structChild = internalBuildTreeStructureToSave(child);
84 structFacetList.add(new Object[] {facetName, structChild});
85 }
86 }
87
88 Object[] facetArray = structFacetList.toArray(new Object[structFacetList.size()]);
89 structComp.setFacets(facetArray);
90 }
91
92 return structComp;
93 }
94
95
96 public UIViewRoot restoreTreeStructure(Object treeStructRoot)
97 {
98 if (treeStructRoot instanceof TreeStructComponent)
99 {
100 return (UIViewRoot)internalRestoreTreeStructure((TreeStructComponent)treeStructRoot, true);
101 }
102
103
104 throw new IllegalArgumentException("TreeStructure of type " + treeStructRoot.getClass().getName() +
105 " is not supported.");
106
107 }
108
109 private UIComponent internalRestoreTreeStructure(TreeStructComponent treeStructComp, boolean checkViewRoot)
110 {
111 String compClass = treeStructComp.getComponentClass();
112 String compId = treeStructComp.getComponentId();
113 UIComponent component = (UIComponent)ClassUtils.newInstance(compClass);
114 component.setId(compId);
115
116 if (checkViewRoot && component instanceof UIViewRoot)
117 {
118 FacesContext.getCurrentInstance().setViewRoot((UIViewRoot) component);
119 }
120
121 TreeStructComponent[] childArray = treeStructComp.getChildren();
122 if (childArray != null)
123 {
124 List<UIComponent> childList = component.getChildren();
125 for (int i = 0, len = childArray.length; i < len; i++)
126 {
127 UIComponent child = internalRestoreTreeStructure(childArray[i], false);
128 childList.add(child);
129 }
130 }
131
132
133 Object[] facetArray = treeStructComp.getFacets();
134 if (facetArray != null)
135 {
136 Map<String, UIComponent> facetMap = component.getFacets();
137 for (int i = 0, len = facetArray.length; i < len; i++)
138 {
139 Object[] tuple = (Object[])facetArray[i];
140 String facetName = (String)tuple[0];
141 TreeStructComponent structChild = (TreeStructComponent)tuple[1];
142 UIComponent child = internalRestoreTreeStructure(structChild, false);
143 facetMap.put(facetName, child);
144 }
145 }
146
147 return component;
148 }
149
150
151 public static class TreeStructComponent
152 implements Serializable
153 {
154 private static final long serialVersionUID = 5069109074684737231L;
155 private String _componentClass;
156 private String _componentId;
157 private TreeStructComponent[] _children = null;
158 private Object[] _facets = null;
159
160 TreeStructComponent(String componentClass, String componentId)
161 {
162 _componentClass = componentClass;
163 _componentId = componentId;
164 }
165
166 public String getComponentClass()
167 {
168 return _componentClass;
169 }
170
171 public String getComponentId()
172 {
173 return _componentId;
174 }
175
176 void setChildren(TreeStructComponent[] children)
177 {
178 _children = children;
179 }
180
181 TreeStructComponent[] getChildren()
182 {
183 return _children;
184 }
185
186 Object[] getFacets()
187 {
188 return _facets;
189 }
190
191 void setFacets(Object[] facets)
192 {
193 _facets = facets;
194 }
195 }
196
197 }