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