Coverage Report - org.kuali.rice.krad.uif.container.TreeGroup
 
Classes in this File Line Coverage Branch Coverage Complexity
TreeGroup
0%
0/95
0%
0/30
1.857
 
 1  
 package org.kuali.rice.krad.uif.container;
 2  
 
 3  
 import org.kuali.rice.core.api.util.tree.Node;
 4  
 import org.kuali.rice.core.api.util.tree.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.core.DataBinding;
 9  
 import org.kuali.rice.krad.uif.field.MessageField;
 10  
 import org.kuali.rice.krad.uif.util.ComponentUtils;
 11  
 import org.kuali.rice.krad.uif.util.ObjectPropertyUtils;
 12  
 import org.kuali.rice.krad.uif.widget.TreeWidget;
 13  
 
 14  
 import java.util.ArrayList;
 15  
 import java.util.List;
 16  
 import java.util.Map;
 17  
 
 18  
 /**
 19  
  * Group component that is backed by a <code>Tree</code> data structure and typically
 20  
  * rendered as a tree in the user interface
 21  
  *
 22  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 23  
  */
 24  
 public class TreeGroup extends Group implements DataBinding{
 25  
     private static final long serialVersionUID = 5841343037089286740L;
 26  
 
 27  
     private String propertyName;
 28  
     private BindingInfo bindingInfo;
 29  
 
 30  
     private Map<Class<?>, NodePrototype> nodePrototypeMap;
 31  
     private NodePrototype defaultNodePrototype;
 32  
 
 33  
     private Tree<Group, MessageField> treeGroups;
 34  
 
 35  
     private TreeWidget treeWidget;
 36  
 
 37  
     public TreeGroup() {
 38  0
         super();
 39  
 
 40  0
         treeGroups = new Tree<Group, MessageField>();
 41  0
     }
 42  
 
 43  
     /**
 44  
      * The following actions are performed:
 45  
      *
 46  
      * <ul>
 47  
      * <li>Set fieldBindModelPath to the collection model path (since the fields
 48  
      * have to belong to the same model as the collection)</li>
 49  
      * <li>Set defaults for binding</li>
 50  
      * <li>Calls view helper service to initialize prototypes</li>
 51  
      * </ul>
 52  
      *
 53  
      * @see org.kuali.rice.kns.uif.core.ComponentBase#performInitialization(org.kuali.rice.kns.uif.container.View)
 54  
      */
 55  
     @Override
 56  
     public void performInitialization(View view) {
 57  0
         setFieldBindingObjectPath(getBindingInfo().getBindingObjectPath());
 58  
 
 59  0
         super.performInitialization(view);
 60  
 
 61  0
         if (bindingInfo != null) {
 62  0
             bindingInfo.setDefaults(view, getPropertyName());
 63  
         }
 64  
 
 65  
         // TODO: set object path for prototypes equal to the tree group object path?
 66  
 
 67  0
         initializeNodePrototypeComponents(view);
 68  0
     }
 69  
 
 70  
     /**
 71  
      * This method initializes {@link org.kuali.rice.kns.uif.core.Component}s within the {@link
 72  
      * org.kuali.rice.krad.uif.container.NodePrototype.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 (nodePrototypeMap != null)
 81  0
             for (Map.Entry<Class<?>, NodePrototype> prototypeEntry : nodePrototypeMap.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, Component parent) {
 109  0
         super.performApplyModel(view, model, parent);
 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 =
 137  
                 buildTreeNode(treeData.getRootElement(), bindingPrefix + /* TODO: hack */ ".rootElement", 0);
 138  0
         treeGroups.setRootElement(rootNode);
 139  
 
 140  0
         setTreeGroups(treeGroups);
 141  0
     }
 142  
 
 143  
     protected Node<Group, MessageField> buildTreeNode(Node<Object, String> nodeData, String bindingPrefix,
 144  
             int nodeCounter) {
 145  0
         Node<Group, MessageField> node = new Node<Group, MessageField>();
 146  0
         node.setNodeType(nodeData.getNodeType());
 147  
 
 148  0
         String idSuffix = "_n" + nodeCounter;
 149  
 
 150  0
         NodePrototype prototype = getNodePrototype(nodeData);
 151  
 
 152  0
         MessageField messageField = ComponentUtils.copy(prototype.getLabelPrototype(), idSuffix);
 153  0
         ComponentUtils.pushObjectToContext(messageField, UifConstants.ContextVariableNames.NODE, nodeData);
 154  0
         messageField.setMessageText(nodeData.getNodeLabel());
 155  0
         node.setNodeLabel(messageField);
 156  
 
 157  0
         Group nodeGroup =
 158  
                 ComponentUtils.copyComponent(prototype.getDataGroupPrototype(), bindingPrefix + ".data", idSuffix);
 159  0
         ComponentUtils.pushObjectToContext(nodeGroup, UifConstants.ContextVariableNames.NODE, nodeData);
 160  0
         node.setData(nodeGroup);
 161  
 
 162  0
         List<Node<Group, MessageField>> nodeChildren = new ArrayList<Node<Group, MessageField>>();
 163  
 
 164  0
         int childIndex = 0;
 165  0
         for (Node<Object, String> childDataNode : nodeData.getChildren()) {
 166  0
             String nextBindingPrefix = bindingPrefix + ".children[" + childIndex + "]";
 167  0
             Node<Group, MessageField> childNode = buildTreeNode(childDataNode, nextBindingPrefix, nodeCounter++);
 168  
 
 169  0
             nodeChildren.add(childNode);
 170  
 
 171  
             // Don't forget about me:
 172  0
             ++childIndex;
 173  0
         }
 174  0
         node.setChildren(nodeChildren);
 175  
 
 176  0
         return node;
 177  
     }
 178  
 
 179  
     /**
 180  
      * This method gets the NodePrototype to use for the given Node
 181  
      */
 182  
     private NodePrototype getNodePrototype(Node<Object, String> nodeData) {
 183  0
         NodePrototype result = null;
 184  0
         if (nodeData != null && nodeData.getData() != null) {
 185  0
             Class<?> dataClass = nodeData.getData().getClass();
 186  0
             result = nodePrototypeMap.get(dataClass);
 187  
 
 188  
             // somewhat lame fallback - to do this right we'd find all entries that are assignable from the data class
 189  
             // and then figure out which one is the closest relative
 190  0
             if (result == null)
 191  0
                 for (Map.Entry<Class<?>, NodePrototype> prototypeEntry : nodePrototypeMap.entrySet()) {
 192  0
                     if (prototypeEntry.getKey().isAssignableFrom(dataClass)) {
 193  0
                         result = prototypeEntry.getValue();
 194  0
                         break;
 195  
                     }
 196  
                 }
 197  
         }
 198  0
         if (result == null)
 199  0
             result = defaultNodePrototype;
 200  0
         return result;
 201  
     }
 202  
 
 203  
     /**
 204  
      * @see org.kuali.rice.kns.uif.container.ContainerBase#getNestedComponents()
 205  
      */
 206  
     @Override
 207  
     public List<Component> getNestedComponents() {
 208  0
         List<Component> components = super.getNestedComponents();
 209  
 
 210  0
         components.add(treeWidget);
 211  0
         addNodeComponents(treeGroups.getRootElement(), components);
 212  
 
 213  0
         return components;
 214  
     }
 215  
 
 216  
     /**
 217  
      * Retrieves the <code>Component</code> instances from the node for building the nested
 218  
      * components list
 219  
      *
 220  
      * @param node - node to pull components from
 221  
      * @param components - list to add components to
 222  
      */
 223  
     protected void addNodeComponents(Node<Group, MessageField> node, List<Component> components) {
 224  0
         if (node != null) {
 225  0
             components.add(node.getNodeLabel());
 226  0
             components.add(node.getData());
 227  
 
 228  0
             for (Node<Group, MessageField> nodeChild : node.getChildren()) {
 229  0
                 addNodeComponents(nodeChild, components);
 230  
             }
 231  
         }
 232  0
     }
 233  
 
 234  
     public String getPropertyName() {
 235  0
         return propertyName;
 236  
     }
 237  
 
 238  
     public void setPropertyName(String propertyName) {
 239  0
         this.propertyName = propertyName;
 240  0
     }
 241  
 
 242  
     public BindingInfo getBindingInfo() {
 243  0
         return bindingInfo;
 244  
     }
 245  
 
 246  
     public void setBindingInfo(BindingInfo bindingInfo) {
 247  0
         this.bindingInfo = bindingInfo;
 248  0
     }
 249  
 
 250  
     /**
 251  
      * @return the defaultNodePrototype
 252  
      */
 253  
     public NodePrototype getDefaultNodePrototype() {
 254  0
         return this.defaultNodePrototype;
 255  
     }
 256  
 
 257  
     /**
 258  
      * @param defaultNodePrototype the defaultNodePrototype to set
 259  
      */
 260  
     public void setDefaultNodePrototype(NodePrototype defaultNodePrototype) {
 261  0
         this.defaultNodePrototype = defaultNodePrototype;
 262  0
     }
 263  
 
 264  
     /**
 265  
      * @return the nodePrototypeMap
 266  
      */
 267  
     public Map<Class<?>, NodePrototype> getNodePrototypeMap() {
 268  0
         return this.nodePrototypeMap;
 269  
     }
 270  
 
 271  
     /**
 272  
      * @param nodePrototypeMap the nodePrototypeMap to set
 273  
      */
 274  
     public void setNodePrototypeMap(Map<Class<?>, NodePrototype> nodePrototypeMap) {
 275  0
         this.nodePrototypeMap = nodePrototypeMap;
 276  0
     }
 277  
 
 278  
     public Tree<Group, MessageField> getTreeGroups() {
 279  0
         return treeGroups;
 280  
     }
 281  
 
 282  
     public void setTreeGroups(Tree<Group, MessageField> treeGroups) {
 283  0
         this.treeGroups = treeGroups;
 284  0
     }
 285  
 
 286  
     public TreeWidget getTreeWidget() {
 287  0
         return treeWidget;
 288  
     }
 289  
 
 290  
     public void setTreeWidget(TreeWidget treeWidget) {
 291  0
         this.treeWidget = treeWidget;
 292  0
     }
 293  
 }