View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * @author Manfred Geiler (latest modification by $Author$)
34   * @version $Revision$ $Date$
35   */
36  public class TreeStructureManager
37  {
38      public TreeStructureManager()
39      {
40          //_facesContext = facesContext;
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          //children
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          //facets
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         //children
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         //facets
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;    // Array of children
158         private Object[] _facets = null;            // Array of Array-tuples with Facetname and TreeStructComponent
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 }