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