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: lu4242 $)
34   * @version $Revision: 1470767 $ $Date: 2013-04-22 20:07:29 -0500 (Mon, 22 Apr 2013) $
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          
77          if (component.getFacetCount() > 0)
78          {
79              Map<String, UIComponent> facetMap = component.getFacets();
80              List<Object[]> structFacetList = new ArrayList<Object[]>();
81              for (Map.Entry<String, UIComponent> entry : facetMap.entrySet())
82              {
83                  UIComponent child = entry.getValue();
84                  if (!child.isTransient())
85                  {
86                      String facetName = entry.getKey();
87                      TreeStructComponent structChild = internalBuildTreeStructureToSave(child);
88                      structFacetList.add(new Object[] {facetName, structChild});
89                  }
90              }
91              
92              Object[] facetArray = structFacetList.toArray(new Object[structFacetList.size()]);
93              structComp.setFacets(facetArray);
94          }
95  
96          return structComp;
97      }
98  
99  
100     public UIViewRoot restoreTreeStructure(Object treeStructRoot)
101     {
102         if (treeStructRoot instanceof TreeStructComponent)
103         {
104             return (UIViewRoot)internalRestoreTreeStructure((TreeStructComponent)treeStructRoot, true);
105         }
106         
107         
108         throw new IllegalArgumentException("TreeStructure of type " + treeStructRoot.getClass().getName() + 
109                                            " is not supported.");
110         
111     }
112 
113     private UIComponent internalRestoreTreeStructure(TreeStructComponent treeStructComp, boolean checkViewRoot)
114     {
115         String compClass = treeStructComp.getComponentClass();
116         String compId = treeStructComp.getComponentId();
117         UIComponent component = (UIComponent)ClassUtils.newInstance(compClass);
118         component.setId(compId);
119 
120         if (checkViewRoot && component instanceof UIViewRoot)
121         {
122             FacesContext.getCurrentInstance().setViewRoot((UIViewRoot) component);
123         }
124         //children
125         TreeStructComponent[] childArray = treeStructComp.getChildren();
126         if (childArray != null)
127         {
128             List<UIComponent> childList = component.getChildren();
129             for (int i = 0, len = childArray.length; i < len; i++)
130             {
131                 UIComponent child = internalRestoreTreeStructure(childArray[i], false);
132                 childList.add(child);
133             }
134         }
135 
136         //facets
137         Object[] facetArray = treeStructComp.getFacets();
138         if (facetArray != null)
139         {
140             Map<String, UIComponent> facetMap = component.getFacets();
141             for (int i = 0, len = facetArray.length; i < len; i++)
142             {
143                 Object[] tuple = (Object[])facetArray[i];
144                 String facetName = (String)tuple[0];
145                 TreeStructComponent structChild = (TreeStructComponent)tuple[1];
146                 UIComponent child = internalRestoreTreeStructure(structChild, false);
147                 facetMap.put(facetName, child);
148             }
149         }
150 
151         return component;
152     }
153 
154 
155     public static class TreeStructComponent
156             implements Serializable
157     {
158         private static final long serialVersionUID = 5069109074684737231L;
159         private String _componentClass;
160         private String _componentId;
161         private TreeStructComponent[] _children = null;    // Array of children
162         private Object[] _facets = null;                   // Array of Array-tuples with Facetname and TreeStructComponent
163 
164         TreeStructComponent(String componentClass, String componentId)
165         {
166             _componentClass = componentClass;
167             _componentId = componentId;
168         }
169 
170         public String getComponentClass()
171         {
172             return _componentClass;
173         }
174 
175         public String getComponentId()
176         {
177             return _componentId;
178         }
179 
180         void setChildren(TreeStructComponent[] children)
181         {
182             _children = children;
183         }
184 
185         TreeStructComponent[] getChildren()
186         {
187             return _children;
188         }
189 
190         Object[] getFacets()
191         {
192             return _facets;
193         }
194 
195         void setFacets(Object[] facets)
196         {
197             _facets = facets;
198         }
199     }
200 
201 }