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