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