View Javadoc

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  public class BreadcrumbManager extends Composite{
24  	
25  	public static List<Hyperlink> links = new ArrayList<Hyperlink>();
26  	
27  	private static List<String> names = new ArrayList<String>();
28  	
29  	private static Controller root;
30  	private static ComplexPanel panel = new SpanPanel();
31  	private static boolean panelEmpty = true;
32  	
33  	private static Panel parentPanel;
34  	
35  	private static class BreadcrumbData{
36  		private String name;
37  		private String path;
38  		public BreadcrumbData(String name, String path) {
39  			super();
40  			this.name = name;
41  			this.path = path;
42  		}
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  		root = controller;
52  	}
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  	    if (root == null){
62  	        return;
63  	    }
64  	    
65  		links.clear();
66  		panel.clear();
67  		panelEmpty = true;
68  		names.clear();
69  		root.collectBreadcrumbNames(names);
70  		
71  		String[] arr = HistoryManager.splitHistoryStack(historyStack);
72  		List<BreadcrumbData> breadcrumbs = new ArrayList<BreadcrumbData>();
73  
74  		if(arr.length == names.size()){
75  			String path = "";
76  			//account for applicationController - skip first item from both
77  			for(int i = 1; i < names.size(); i++){
78  				path = path + "/" + arr[i];
79  				String name = names.get(i);
80  				//Views with empty names do not appear on the breadcrumbs
81  				if(name != null && !name.isEmpty()){
82  					breadcrumbs.add(new BreadcrumbData(name, path));
83  				}
84  			}
85  		}
86  		//Special link, a controller is adding a breadcrumb outside the scope of the current controller
87  		//in format name@path
88  		else if(names.size() > arr.length){
89  			String path = "";
90  			int j = 1;
91  			//account for applicationController - skip first item from both
92  			for(int i = 1; i < names.size(); i++){
93  				String name = names.get(i);
94  				if(name.contains("@")){
95  					String[] split = name.split("@");
96  					name = split[0];
97  					if(name != null && !name.isEmpty()){
98  						//In the special case the path is the second part of the split
99  						breadcrumbs.add(new BreadcrumbData(name, split[1]));
100 					}
101 				}
102 				else{
103 					if(j == arr.length){
104 						break;
105 					}
106 					path = path + "/" + arr[j];
107 					j++;
108 					if(name != null && !name.isEmpty()){
109 						breadcrumbs.add(new BreadcrumbData(name, path));
110 					}
111 				}
112 			}
113 		}
114 		
115 		if(parentPanel != null){
116 			if(breadcrumbs.size() == 1){
117 				panel.getParent().setVisible(false);
118 			}
119 			else{
120 				panel.getParent().setVisible(true);
121 
122 			}
123 		}
124 		
125 		for(int i = 0; i < breadcrumbs.size(); i++){
126 			if(i < breadcrumbs.size() - 1){
127 				createLink(breadcrumbs.get(i).name, breadcrumbs.get(i).path);
128 			}
129 			else{
130 				createLabel(breadcrumbs.get(i).name);
131 				//WindowTitleUtils.setSubtitle(breadcrumbs.get(i).name);
132 			}
133 		}
134 	}
135 	
136 	private static void createLabel(String name){
137 		addToPanel(new InlineLabel(name));
138 	}
139 	
140 	private static void createLink(String name, final String viewPath){
141 		Hyperlink link = new Hyperlink(name, viewPath);
142 		links.add(link);
143 		addToPanel(link);
144 	}
145 	
146 	private static void addToPanel(Widget w){
147 		if(panelEmpty){
148 			panel.add(w);
149 			panelEmpty = false;
150 		}
151 		else{
152 			panel.add(new InlineLabel(" \u00BB "));
153 			panel.add(w);
154 		}
155 	}
156 	
157 	/** 
158 	 * @return the breadcrumb panel which contains the breadcrumb links dynamically updated by the 
159 	 * BreadcrumbManager
160 	 */
161 	public static ComplexPanel getBreadcrumbPanel(){
162 		return panel;
163 	}
164 	
165 	public static void setParentPanel(Panel panel){
166 		parentPanel = panel;
167 		parentPanel.setVisible(false);
168 	}
169 
170 }