Coverage Report - org.trails.finder.BaseBlockFinder
 
Classes in this File Line Coverage Branch Coverage Complexity
BaseBlockFinder
90% 
100% 
0
 
 1  
 package org.trails.finder;
 2  
 
 3  
 import java.util.Map;
 4  
 
 5  
 import ognl.Ognl;
 6  
 import ognl.OgnlException;
 7  
 import org.apache.tapestry.IRequestCycle;
 8  
 import org.apache.tapestry.components.Block;
 9  
 import org.apache.tapestry.util.ComponentAddress;
 10  
 import org.trails.page.IEditorBlockPage;
 11  
 import org.trails.descriptor.IPropertyDescriptor;
 12  
 
 13  16
 public class BaseBlockFinder implements BlockFinder
 14  
 {
 15  
 
 16  
         private Map<String, ComponentAddress> editorMap;
 17  
         private ComponentAddress defaultEditor;
 18  
 
 19  
         public Map<String, ComponentAddress> getEditorMap()
 20  
         {
 21  0
                 return editorMap;
 22  
         }
 23  
 
 24  
         /**
 25  
          * This a map where the keys are ognl expressions and the values are component address.
 26  
          *
 27  
          * @param editorMap
 28  
          */
 29  
         public void setEditorMap(Map<String, ComponentAddress> editorMap)
 30  
         {
 31  8
                 this.editorMap = editorMap;
 32  8
         }
 33  
 
 34  
         /**
 35  
          * @param descriptor
 36  
          * @return The first component address in the editorMap whose key evaluates to true for descriptor. This will be used
 37  
          *         to load an editor for the descriptor. Returns default editor if no match is found.
 38  
          * @see BlockFinder#findBlockAddress(org.trails.descriptor.IPropertyDescriptor)
 39  
          */
 40  
         public ComponentAddress findBlockAddress(IPropertyDescriptor descriptor)
 41  
         {
 42  16
                 ComponentAddress componentAddress = findBlockAddress(editorMap, descriptor);
 43  16
                 return componentAddress != null ? componentAddress : getDefaultBlockAddress();
 44  
         }
 45  
 
 46  
         protected ComponentAddress findBlockAddress(Map<String, ComponentAddress> map, IPropertyDescriptor descriptor)
 47  
         {
 48  44
                 for (Map.Entry<String, ComponentAddress> entry : map.entrySet())
 49  
                 {
 50  
                         try
 51  
                         {
 52  24
                                 if ((Boolean) Ognl.getValue(entry.getKey(), descriptor))
 53  
                                 {
 54  12
                                         return entry.getValue();
 55  
                                 }
 56  0
                         } catch (OgnlException e)
 57  
                         {
 58  
                         }
 59  
                 }
 60  4
                 return null;
 61  
         }
 62  
 
 63  
         public Block findBlock(IRequestCycle cycle, IPropertyDescriptor descriptor)
 64  
         {
 65  8
                 if (cycle.getPage().getComponents().containsKey(descriptor.getName()))
 66  
                 {
 67  4
                         Block block = (Block) cycle.getPage().getComponent(descriptor.getName());
 68  4
                         return block;
 69  
                 } else
 70  
                 {
 71  
                         // since it came from a block container page, we need to set
 72  
                         // the model and descriptor on the container page so its visible to the
 73  
                         // block
 74  4
                         ComponentAddress blockAddress = findBlockAddress(descriptor);
 75  4
                         Block block = (Block) blockAddress.findComponent(cycle);
 76  4
                         ((IEditorBlockPage) block.getPage()).setDescriptor(descriptor);
 77  4
                         return block;
 78  
                 }
 79  
         }
 80  
 
 81  
         public ComponentAddress getDefaultBlockAddress()
 82  
         {
 83  4
                 return defaultEditor;
 84  
         }
 85  
 
 86  
         public void setDefaultBlockAddress(ComponentAddress defaultEditor)
 87  
         {
 88  8
                 this.defaultEditor = defaultEditor;
 89  8
         }
 90  
 
 91  
 }