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