1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.teeda.core.application.impl; |
17 | |
|
18 | |
import java.io.Serializable; |
19 | |
import java.util.ArrayList; |
20 | |
import java.util.Iterator; |
21 | |
import java.util.List; |
22 | |
import java.util.Map; |
23 | |
|
24 | |
import javax.faces.component.UIComponent; |
25 | |
|
26 | |
import org.seasar.framework.util.ClassUtil; |
27 | |
import org.seasar.teeda.core.application.TreeStructure; |
28 | |
import org.seasar.teeda.core.application.TreeStructureManager; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | 16 | public class TreeStructureManagerImpl implements TreeStructureManager, |
34 | |
Serializable { |
35 | |
|
36 | |
private static final long serialVersionUID = 1L; |
37 | |
|
38 | |
public TreeStructure buildTreeStructure(UIComponent component) { |
39 | 11 | TreeStructure struct = new UIComponentTreeStructure(component); |
40 | 11 | struct.setChildren(buildChildrenTreeStructure(component)); |
41 | 11 | struct.setFacets(buildFacetsTreeStructure(component)); |
42 | 11 | return struct; |
43 | |
} |
44 | |
|
45 | |
public UIComponent restoreTreeStructure(TreeStructure structure) { |
46 | 5 | String className = structure.getComponentClassName(); |
47 | 5 | String id = structure.getComponentId(); |
48 | 5 | UIComponent component = (UIComponent) ClassUtil.newInstance(className); |
49 | 5 | component.setId(id); |
50 | 5 | restoreChildrenTreeStructure(component, structure.getChildren()); |
51 | 5 | restoreFacetsTreeStructure(component, structure.getFacets()); |
52 | 5 | return component; |
53 | |
} |
54 | |
|
55 | |
protected TreeStructure[] buildChildrenTreeStructure(UIComponent component) { |
56 | 13 | if (component.getChildCount() > 0) { |
57 | 2 | List structs = new ArrayList(); |
58 | 2 | for (Iterator itr = component.getChildren().iterator(); itr |
59 | 4 | .hasNext();) { |
60 | 2 | UIComponent child = (UIComponent) itr.next(); |
61 | 2 | if (!child.isTransient()) { |
62 | 2 | TreeStructure childStruct = buildTreeStructure(child); |
63 | 2 | structs.add(childStruct); |
64 | |
} |
65 | |
} |
66 | 2 | return (TreeStructure[]) structs.toArray(new TreeStructure[structs |
67 | |
.size()]); |
68 | |
} |
69 | 11 | return null; |
70 | |
} |
71 | |
|
72 | |
protected Object[] buildFacetsTreeStructure(UIComponent component) { |
73 | 13 | Map facets = component.getFacets(); |
74 | 13 | if (!facets.isEmpty()) { |
75 | 2 | List structs = new ArrayList(); |
76 | 2 | for (Iterator it = facets.entrySet().iterator(); it.hasNext();) { |
77 | 2 | Map.Entry entry = (Map.Entry) it.next(); |
78 | 2 | UIComponent child = (UIComponent) entry.getValue(); |
79 | 2 | if (!child.isTransient()) { |
80 | 2 | String facetName = (String) entry.getKey(); |
81 | 2 | TreeStructure struct = buildTreeStructure(child); |
82 | 2 | structs.add(new Object[] { facetName, struct }); |
83 | |
} |
84 | |
} |
85 | 2 | return structs.toArray(); |
86 | |
} |
87 | 11 | return null; |
88 | |
} |
89 | |
|
90 | |
protected void restoreChildrenTreeStructure(UIComponent component, |
91 | |
TreeStructure[] structs) { |
92 | 5 | for (int i = 0; i < structs.length; ++i) { |
93 | 0 | UIComponent child = restoreTreeStructure(structs[i]); |
94 | 0 | component.getChildren().add(child); |
95 | |
} |
96 | 5 | } |
97 | |
|
98 | |
protected void restoreFacetsTreeStructure(UIComponent component, |
99 | |
Object[] facets) { |
100 | 5 | for (int i = 0, len = facets.length; i < len; i++) { |
101 | 0 | Object[] array = (Object[]) facets[i]; |
102 | 0 | String facetName = (String) array[0]; |
103 | 0 | TreeStructure struct = (TreeStructure) array[1]; |
104 | 0 | UIComponent child = restoreTreeStructure(struct); |
105 | 0 | component.getFacets().put(facetName, child); |
106 | |
} |
107 | 5 | } |
108 | |
|
109 | 16 | private static class UIComponentTreeStructure extends TreeStructure { |
110 | |
|
111 | |
private static final long serialVersionUID = 1L; |
112 | |
|
113 | |
public UIComponentTreeStructure(UIComponent component) { |
114 | 11 | super(component.getClass().getName(), component.getId()); |
115 | 11 | } |
116 | |
} |
117 | |
|
118 | |
} |