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