Coverage Report - org.kuali.rice.kns.uif.container.TreeGroup
 
Classes in this File Line Coverage Branch Coverage Complexity
TreeGroup
0%
0/91
0%
0/30
1.72
TreeGroup$NodePrototype
0%
0/7
N/A
1.72
 
 1  
 package org.kuali.rice.kns.uif.container;
 2  
 
 3  
 import java.io.Serializable;
 4  
 import java.util.ArrayList;
 5  
 import java.util.List;
 6  
 import java.util.Map;
 7  
 import java.util.Map.Entry;
 8  
 
 9  
 import org.kuali.rice.core.util.Node;
 10  
 import org.kuali.rice.core.util.Tree;
 11  
 import org.kuali.rice.kns.uif.UifConstants;
 12  
 import org.kuali.rice.kns.uif.core.BindingInfo;
 13  
 import org.kuali.rice.kns.uif.core.Component;
 14  
 import org.kuali.rice.kns.uif.field.MessageField;
 15  
 import org.kuali.rice.kns.uif.util.ComponentUtils;
 16  
 import org.kuali.rice.kns.uif.util.ObjectPropertyUtils;
 17  
 import org.kuali.rice.kns.uif.widget.TreeWidget;
 18  
 
 19  
 /**
 20  
  * Group component that is backed by a <code>Tree</code> data structure and typically
 21  
  * rendered as a tree in the user interface
 22  
  *
 23  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 24  
  */
 25  
 public class TreeGroup extends Group {
 26  
     private static final long serialVersionUID = 5841343037089286740L;
 27  
 
 28  
     private String propertyName;
 29  
     private BindingInfo bindingInfo;
 30  
 
 31  
     private Map<Class<?>, NodePrototype> nodeProtypeMap;
 32  
     private NodePrototype defaultNodePrototype;
 33  
     
 34  
     private Tree<Group, MessageField> treeGroups;
 35  
 
 36  
     private TreeWidget treeWidget;
 37  
 
 38  
     public TreeGroup() {
 39  0
         super();
 40  
 
 41  0
         treeGroups = new Tree<Group, MessageField>();
 42  0
     }
 43  
 
 44  
     /**
 45  
      * The following actions are performed:
 46  
      *
 47  
      * <ul>
 48  
      * <li>Set fieldBindModelPath to the collection model path (since the fields
 49  
      * have to belong to the same model as the collection)</li>
 50  
      * <li>Set defaults for binding</li>
 51  
      * <li>Calls view helper service to initialize prototypes</li>
 52  
      * </ul>
 53  
      *
 54  
      * @see org.kuali.rice.kns.uif.core.ComponentBase#performInitialization(org.kuali.rice.kns.uif.container.View)
 55  
      */
 56  
     @Override
 57  
     public void performInitialization(View view) {
 58  0
         setFieldBindingObjectPath(getBindingInfo().getBindingObjectPath());
 59  
 
 60  0
         super.performInitialization(view);
 61  
 
 62  0
         if (bindingInfo != null) {
 63  0
             bindingInfo.setDefaults(view, getPropertyName());
 64  
         }
 65  
 
 66  
         // TODO: set object path for prototypes equal to the tree group object path?
 67  
 
 68  0
         initializeNodePrototypeComponents(view);
 69  0
     }
 70  
 
 71  
     /**
 72  
      * This method initializes {@link org.kuali.rice.kns.uif.core.Component}s within the {@link NodePrototype}s
 73  
      * 
 74  
      * @param view
 75  
      */
 76  
     private void initializeNodePrototypeComponents(View view) {
 77  0
         view.getViewHelperService().performComponentInitialization(view, defaultNodePrototype.getLabelPrototype());
 78  0
         view.getViewHelperService().performComponentInitialization(view, defaultNodePrototype.getDataGroupPrototype());
 79  
         
 80  0
         if (nodeProtypeMap != null) for (Entry<Class<?>, NodePrototype> prototypeEntry : nodeProtypeMap.entrySet()) {
 81  0
             NodePrototype prototype = prototypeEntry.getValue();
 82  0
             if (prototype != null) {
 83  
                 
 84  0
                 if (prototype.getLabelPrototype() != null) {
 85  0
                     view.getViewHelperService().performComponentInitialization(view, prototype.getLabelPrototype());
 86  
                 } else {
 87  0
                     throw new IllegalStateException("encountered null NodePrototype.labelPrototype");
 88  
                 }
 89  
                 
 90  0
                 if (prototype.getDataGroupPrototype() != null) {
 91  0
                     view.getViewHelperService().performComponentInitialization(view, prototype.getDataGroupPrototype());
 92  
                 } else {
 93  0
                     throw new IllegalStateException("encountered null NodePrototype.dataGroupPrototype");
 94  
                 }
 95  
                 
 96  
             } else {
 97  0
                 throw new IllegalStateException("encountered null NodePrototype");
 98  
             }
 99  0
         }
 100  0
     }
 101  
 
 102  
     /**
 103  
      * @see org.kuali.rice.kns.uif.container.ContainerBase#performApplyModel(org.kuali.rice.kns.uif.container.View,
 104  
      *      java.lang.Object)
 105  
      */
 106  
     @Override
 107  
     public void performApplyModel(View view, Object model) {
 108  0
         super.performApplyModel(view, model);
 109  
 
 110  0
         buildTreeGroups(view, model);
 111  0
     }
 112  
 
 113  
     /**
 114  
      * Builds the components that will be rendered as part of the tree group
 115  
      *
 116  
      * <p>
 117  
      *  The component tree group mirrors the tree data structure on the model. For each node of
 118  
      *  the data structure, a corresponding <code>MessageField</code>  will be created for the node
 119  
      *  label, and a <code>Group</code> component for the node data. These are placed into a new
 120  
      *  node for the component tree. After the tree is built it is set as a property on the tree group
 121  
      *  to be read by the renderer
 122  
      * </p>
 123  
      *
 124  
      * @param view - view instance the tree group belongs to
 125  
      * @param model - object containing the view data from which the tree data will be retrieved
 126  
      */
 127  
     protected void buildTreeGroups(View view, Object model) {
 128  
         // get Tree data property
 129  0
         Tree<Object, String> treeData = ObjectPropertyUtils.getPropertyValue(model, getBindingInfo().getBindingPath());
 130  
 
 131  
         // build component tree that corresponds with tree data
 132  0
         Tree<Group, MessageField> treeGroups = new Tree<Group, MessageField>();
 133  
 
 134  0
         String bindingPrefix = getBindingInfo().getBindingPrefixForNested();
 135  0
         Node<Group, MessageField> rootNode = buildTreeNode(treeData.getRootElement(), bindingPrefix, 0);
 136  0
         treeGroups.setRootElement(rootNode);
 137  
 
 138  0
         setTreeGroups(treeGroups);
 139  0
     }
 140  
 
 141  
     protected Node<Group, MessageField> buildTreeNode(Node<Object, String> nodeData, String bindingPrefix, int nodeCounter) {
 142  0
         Node<Group, MessageField> node = new Node<Group, MessageField>();
 143  0
         node.setNodeType(nodeData.getNodeType());
 144  
 
 145  0
         String idSuffix = "_n" + nodeCounter;
 146  
         
 147  0
         NodePrototype prototype = getNodePrototype(nodeData);
 148  
 
 149  0
         MessageField messageField = ComponentUtils.copy(prototype.getLabelPrototype(), idSuffix);
 150  0
         ComponentUtils.pushObjectToContext(messageField, UifConstants.ContextVariableNames.NODE, nodeData);
 151  0
         messageField.setMessageText(nodeData.getNodeLabel());
 152  0
         node.setNodeLabel(messageField);
 153  
         
 154  0
         Group nodeGroup = ComponentUtils.copyComponent(prototype.getDataGroupPrototype(), bindingPrefix + ".data", idSuffix);
 155  0
         ComponentUtils.pushObjectToContext(nodeGroup, UifConstants.ContextVariableNames.NODE, nodeData);
 156  0
         node.setData(nodeGroup);
 157  
 
 158  0
         List<Node<Group, MessageField>> nodeChildren = new ArrayList<Node<Group, MessageField>>();
 159  
 
 160  0
         int childIndex = -1;
 161  0
         for (Node<Object, String> childDataNode : nodeData.getChildren()) {
 162  0
             String nextBindingPrefix = bindingPrefix + ".children[" + childIndex + "]";
 163  0
             Node<Group, MessageField> childNode = buildTreeNode(childDataNode, nextBindingPrefix, nodeCounter++);
 164  
 
 165  0
             nodeChildren.add(childNode);
 166  0
         }
 167  0
         node.setChildren(nodeChildren);
 168  
 
 169  0
         return node;
 170  
     }
 171  
 
 172  
     
 173  
     /**
 174  
      * This method gets the NodePrototype to use for the given Node
 175  
      */
 176  
     private NodePrototype getNodePrototype(Node<Object, String> nodeData) {
 177  0
         NodePrototype result = null;
 178  0
         if (nodeData != null && nodeData.getData() != null) {
 179  0
             Class<?> dataClass = nodeData.getData().getClass();
 180  0
             result = nodeProtypeMap.get(dataClass);
 181  
             
 182  
             // somewhat lame fallback - to do this right we'd find all entries that are assignable from the data class 
 183  
             // and then figure out which one is the closest relative
 184  0
             if (result == null) for (Entry<Class<?>, NodePrototype> prototypeEntry : nodeProtypeMap.entrySet()) {
 185  0
                 if (prototypeEntry.getKey().isAssignableFrom(dataClass)) {
 186  0
                     result = prototypeEntry.getValue();
 187  0
                     break;
 188  
                 }
 189  
             }
 190  
         } 
 191  0
         if (result == null) result = defaultNodePrototype;
 192  0
         return result;
 193  
     }
 194  
 
 195  
     /**
 196  
      * @see org.kuali.rice.kns.uif.container.ContainerBase#getNestedComponents()
 197  
      */
 198  
     @Override
 199  
     public List<Component> getNestedComponents() {
 200  0
         List<Component> components = super.getNestedComponents();
 201  
 
 202  0
         components.add(treeWidget);
 203  0
         addNodeComponents(treeGroups.getRootElement(), components);
 204  
 
 205  0
         return components;
 206  
     }
 207  
 
 208  
     /**
 209  
      * Retrieves the <code>Component</code> instances from the node for building the nested
 210  
      * components list
 211  
      *
 212  
      * @param node - node to pull components from
 213  
      * @param components - list to add components to
 214  
      */
 215  
     protected void addNodeComponents(Node<Group, MessageField> node, List<Component> components) {
 216  0
         if (node != null) {
 217  0
             components.add(node.getNodeLabel());
 218  0
             components.add(node.getData());
 219  
 
 220  0
             for (Node<Group, MessageField> nodeChild : node.getChildren()) {
 221  0
                 addNodeComponents(nodeChild, components);
 222  
             }
 223  
         }
 224  0
     }
 225  
 
 226  
     public String getPropertyName() {
 227  0
         return propertyName;
 228  
     }
 229  
 
 230  
     public void setPropertyName(String propertyName) {
 231  0
         this.propertyName = propertyName;
 232  0
     }
 233  
 
 234  
     public BindingInfo getBindingInfo() {
 235  0
         return bindingInfo;
 236  
     }
 237  
 
 238  
     public void setBindingInfo(BindingInfo bindingInfo) {
 239  0
         this.bindingInfo = bindingInfo;
 240  0
     }
 241  
 
 242  
     /**
 243  
      * @return the defaultNodePrototype
 244  
      */
 245  
     public NodePrototype getDefaultNodePrototype() {
 246  0
         return this.defaultNodePrototype;
 247  
     }
 248  
     
 249  
     /**
 250  
      * @param defaultNodePrototype the defaultNodePrototype to set
 251  
      */
 252  
     public void setDefaultNodePrototype(NodePrototype defaultNodePrototype) {
 253  0
         this.defaultNodePrototype = defaultNodePrototype;
 254  0
     }
 255  
     
 256  
     /**
 257  
      * @return the nodeProtypeMap
 258  
      */
 259  
     public Map<Class<?>, NodePrototype> getNodeProtypeMap() {
 260  0
         return this.nodeProtypeMap;
 261  
     }
 262  
     
 263  
     /**
 264  
      * @param nodeProtypeMap the nodeProtypeMap to set
 265  
      */
 266  
     public void setNodeProtypeMap(Map<Class<?>, NodePrototype> nodeProtypeMap) {
 267  0
         this.nodeProtypeMap = nodeProtypeMap;
 268  0
     }
 269  
 
 270  
     public Tree<Group, MessageField> getTreeGroups() {
 271  0
         return treeGroups;
 272  
     }
 273  
 
 274  
     public void setTreeGroups(Tree<Group, MessageField> treeGroups) {
 275  0
         this.treeGroups = treeGroups;
 276  0
     }
 277  
 
 278  
     public TreeWidget getTreeWidget() {
 279  0
         return treeWidget;
 280  
     }
 281  
 
 282  
     public void setTreeWidget(TreeWidget treeWidget) {
 283  0
         this.treeWidget = treeWidget;
 284  0
     }
 285  
     
 286  0
     public static class NodePrototype implements Serializable {
 287  
         
 288  
         private static final long serialVersionUID = 1L;
 289  
         
 290  
         MessageField labelPrototype;
 291  
         Group dataGroupPrototype;
 292  
         
 293  
         /**
 294  
          * @param labelPrototype the labelPrototype to set
 295  
          */
 296  
         public void setLabelPrototype(MessageField labelPrototype) {
 297  0
             this.labelPrototype = labelPrototype;
 298  0
         }
 299  
         
 300  
         /**
 301  
          * @return the labelPrototype
 302  
          */
 303  
         public MessageField getLabelPrototype() {
 304  0
             return this.labelPrototype;
 305  
         }
 306  
         
 307  
         /**
 308  
          * @param dataGroupPrototype the dataGroupPrototype to set
 309  
          */
 310  
         public void setDataGroupPrototype(Group dataGroupPrototype) {
 311  0
             this.dataGroupPrototype = dataGroupPrototype;
 312  0
         }
 313  
         
 314  
         /**
 315  
          * @return the dataGroupPrototype
 316  
          */
 317  
         public Group getDataGroupPrototype() {
 318  0
             return this.dataGroupPrototype;
 319  
         }
 320  
     }
 321  
 }