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