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