Coverage Report - org.seasar.teeda.core.application.impl.TreeStructureManagerImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
TreeStructureManagerImpl
85%
39/46
75%
12/16
2.429
TreeStructureManagerImpl$UIComponentTreeStructure
100%
2/2
N/A
2.429
 
 1  
 /*
 2  
  * Copyright 2004-2011 the Seasar Foundation and the Others.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 13  
  * either express or implied. See the License for the specific language
 14  
  * governing permissions and limitations under the License.
 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  
  * @author shot
 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  
 }