Coverage Report - org.kuali.student.common.ui.client.mvc.breadcrumb.BreadcrumbManager
 
Classes in this File Line Coverage Branch Coverage Complexity
BreadcrumbManager
0%
0/65
0%
0/34
3.125
BreadcrumbManager$BreadcrumbData
0%
0/5
N/A
3.125
 
 1  
 package org.kuali.student.common.ui.client.mvc.breadcrumb;
 2  
 
 3  
 import java.util.ArrayList;
 4  
 import java.util.List;
 5  
 
 6  
 import org.kuali.student.common.ui.client.mvc.Controller;
 7  
 import org.kuali.student.common.ui.client.mvc.history.HistoryManager;
 8  
 import org.kuali.student.common.ui.client.widgets.field.layout.element.SpanPanel;
 9  
 
 10  
 import com.google.gwt.user.client.ui.ComplexPanel;
 11  
 import com.google.gwt.user.client.ui.Composite;
 12  
 import com.google.gwt.user.client.ui.Hyperlink;
 13  
 import com.google.gwt.user.client.ui.InlineLabel;
 14  
 import com.google.gwt.user.client.ui.Panel;
 15  
 import com.google.gwt.user.client.ui.Widget;
 16  
 
 17  
 /**
 18  
  * Manages breadcrumbs for the application
 19  
  * 
 20  
  * @author Kuali Student Team
 21  
  *
 22  
  */
 23  0
 public class BreadcrumbManager extends Composite{
 24  
         
 25  0
         public static List<Hyperlink> links = new ArrayList<Hyperlink>();
 26  
         
 27  0
         private static List<String> names = new ArrayList<String>();
 28  
         
 29  
         private static Controller root;
 30  0
         private static ComplexPanel panel = new SpanPanel();
 31  0
         private static boolean panelEmpty = true;
 32  
         
 33  
         private static Panel parentPanel;
 34  
         
 35  0
         private static class BreadcrumbData{
 36  
                 private String name;
 37  
                 private String path;
 38  
                 public BreadcrumbData(String name, String path) {
 39  0
                         super();
 40  0
                         this.name = name;
 41  0
                         this.path = path;
 42  0
                 }
 43  
         }
 44  
         
 45  
         /**
 46  
          * Binds the controller as the top level controller to call collectBreadcrumbNames on.
 47  
          * 
 48  
          * @param controller
 49  
          */
 50  
         public static void bind(Controller controller){
 51  0
                 root = controller;
 52  0
         }
 53  
         
 54  
         /**
 55  
          * Updates the breadcrumb panel with the current breadcrumb by walking the controller hierarchy by calling
 56  
          * collectBreadcrumbNames on the root controller bound to the BreadcrumbManager.
 57  
          * 
 58  
          * @param historyStack
 59  
          */
 60  
         public static void updateLinks(String historyStack){
 61  0
                 links.clear();
 62  0
                 panel.clear();
 63  0
                 panelEmpty = true;
 64  0
                 names.clear();
 65  0
                 root.collectBreadcrumbNames(names);
 66  
                 
 67  0
                 String[] arr = HistoryManager.splitHistoryStack(historyStack);
 68  0
                 List<BreadcrumbData> breadcrumbs = new ArrayList<BreadcrumbData>();
 69  
 
 70  0
                 if(arr.length == names.size()){
 71  0
                         String path = "";
 72  
                         //account for applicationController - skip first item from both
 73  0
                         for(int i = 1; i < names.size(); i++){
 74  0
                                 path = path + "/" + arr[i];
 75  0
                                 String name = names.get(i);
 76  
                                 //Views with empty names do not appear on the breadcrumbs
 77  0
                                 if(name != null && !name.isEmpty()){
 78  0
                                         breadcrumbs.add(new BreadcrumbData(name, path));
 79  
                                 }
 80  
                         }
 81  0
                 }
 82  
                 //Special link, a controller is adding a breadcrumb outside the scope of the current controller
 83  
                 //in format name@path
 84  0
                 else if(names.size() > arr.length){
 85  0
                         String path = "";
 86  0
                         int j = 1;
 87  
                         //account for applicationController - skip first item from both
 88  0
                         for(int i = 1; i < names.size(); i++){
 89  0
                                 String name = names.get(i);
 90  0
                                 if(name.contains("@")){
 91  0
                                         String[] split = name.split("@");
 92  0
                                         name = split[0];
 93  0
                                         if(name != null && !name.isEmpty()){
 94  
                                                 //In the special case the path is the second part of the split
 95  0
                                                 breadcrumbs.add(new BreadcrumbData(name, split[1]));
 96  
                                         }
 97  0
                                 }
 98  
                                 else{
 99  0
                                         if(j == arr.length){
 100  0
                                                 break;
 101  
                                         }
 102  0
                                         path = path + "/" + arr[j];
 103  0
                                         j++;
 104  0
                                         if(name != null && !name.isEmpty()){
 105  0
                                                 breadcrumbs.add(new BreadcrumbData(name, path));
 106  
                                         }
 107  
                                 }
 108  
                         }
 109  
                 }
 110  
                 
 111  0
                 if(parentPanel != null){
 112  0
                         if(breadcrumbs.size() == 1){
 113  0
                                 panel.getParent().setVisible(false);
 114  
                         }
 115  
                         else{
 116  0
                                 panel.getParent().setVisible(true);
 117  
 
 118  
                         }
 119  
                 }
 120  
                 
 121  0
                 for(int i = 0; i < breadcrumbs.size(); i++){
 122  0
                         if(i < breadcrumbs.size() - 1){
 123  0
                                 createLink(breadcrumbs.get(i).name, breadcrumbs.get(i).path);
 124  
                         }
 125  
                         else{
 126  0
                                 createLabel(breadcrumbs.get(i).name);
 127  
                                 //WindowTitleUtils.setSubtitle(breadcrumbs.get(i).name);
 128  
                         }
 129  
                 }
 130  0
         }
 131  
         
 132  
         private static void createLabel(String name){
 133  0
                 addToPanel(new InlineLabel(name));
 134  0
         }
 135  
         
 136  
         private static void createLink(String name, final String viewPath){
 137  0
                 Hyperlink link = new Hyperlink(name, viewPath);
 138  0
                 links.add(link);
 139  0
                 addToPanel(link);
 140  0
         }
 141  
         
 142  
         private static void addToPanel(Widget w){
 143  0
                 if(panelEmpty){
 144  0
                         panel.add(w);
 145  0
                         panelEmpty = false;
 146  
                 }
 147  
                 else{
 148  0
                         panel.add(new InlineLabel(" \u00BB "));
 149  0
                         panel.add(w);
 150  
                 }
 151  0
         }
 152  
         
 153  
         /** 
 154  
          * @return the breadcrumb panel which contains the breadcrumb links dynamically updated by the 
 155  
          * BreadcrumbManager
 156  
          */
 157  
         public static ComplexPanel getBreadcrumbPanel(){
 158  0
                 return panel;
 159  
         }
 160  
         
 161  
         public static void setParentPanel(Panel panel){
 162  0
                 parentPanel = panel;
 163  0
                 parentPanel.setVisible(false);
 164  0
         }
 165  
 
 166  
 }