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: bommel $)
34   * @version $Revision: 1187700 $ $Date: 2011-10-22 07:19:37 -0500 (Sat, 22 Oct 2011) $
35   */
36  public class TreeStructureManager
37  {
38      //private static final Log log = LogFactory.getLog(TreeStructureManager.class);
39  
40      //private FacesContext _facesContext;
41  
42      public TreeStructureManager()
43      {
44          //_facesContext = facesContext;
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          //children
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          //facets
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         //children
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         //facets
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;    // Array of children
161         private Object[] _facets = null;                   // Array of Array-tuples with Facetname and TreeStructComponent
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 }