1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
package org.kuali.student.common.ui.client.widgets.tabs.impl; |
10 | |
|
11 | |
import java.util.ArrayList; |
12 | |
import java.util.HashMap; |
13 | |
import java.util.List; |
14 | |
import java.util.Map; |
15 | |
|
16 | |
import org.kuali.student.common.ui.client.mvc.Callback; |
17 | |
import org.kuali.student.common.ui.client.mvc.history.HistoryManager; |
18 | |
import org.kuali.student.common.ui.client.widgets.ClickablePanel; |
19 | |
import org.kuali.student.common.ui.client.widgets.KSLabel; |
20 | |
import org.kuali.student.common.ui.client.widgets.layout.VerticalFlowPanel; |
21 | |
import org.kuali.student.common.ui.client.widgets.menus.KSListPanel; |
22 | |
import org.kuali.student.common.ui.client.widgets.tabs.KSTabPanelAbstract; |
23 | |
import org.kuali.student.common.ui.client.widgets.tabs.KSTabPanel.TabPanelStyle; |
24 | |
import org.kuali.student.common.ui.client.widgets.tabs.KSTabPanel.TabPosition; |
25 | |
|
26 | |
import com.google.gwt.event.dom.client.ClickEvent; |
27 | |
import com.google.gwt.event.dom.client.ClickHandler; |
28 | |
import com.google.gwt.event.dom.client.MouseOutEvent; |
29 | |
import com.google.gwt.event.dom.client.MouseOutHandler; |
30 | |
import com.google.gwt.event.dom.client.MouseOverEvent; |
31 | |
import com.google.gwt.event.dom.client.MouseOverHandler; |
32 | |
import com.google.gwt.user.client.ui.ComplexPanel; |
33 | |
import com.google.gwt.user.client.ui.Composite; |
34 | |
import com.google.gwt.user.client.ui.FlowPanel; |
35 | |
import com.google.gwt.user.client.ui.HasText; |
36 | |
import com.google.gwt.user.client.ui.HorizontalPanel; |
37 | |
import com.google.gwt.user.client.ui.Image; |
38 | |
import com.google.gwt.user.client.ui.Label; |
39 | |
import com.google.gwt.user.client.ui.SimplePanel; |
40 | |
import com.google.gwt.user.client.ui.Widget; |
41 | |
|
42 | 0 | public class KSTabPanelImpl extends KSTabPanelAbstract { |
43 | |
|
44 | 0 | private VerticalFlowPanel container = new VerticalFlowPanel(); |
45 | 0 | private FlowPanel tabRow = new FlowPanel(); |
46 | |
|
47 | |
|
48 | 0 | private KSListPanel left = new KSListPanel(); |
49 | 0 | private KSListPanel right = new KSListPanel(); |
50 | 0 | private SimplePanel content = new SimplePanel(); |
51 | |
private Tab selectedTab; |
52 | 0 | private Map<String, Tab> tabMap = new HashMap<String, Tab>(); |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | 0 | protected class Tab extends Composite { |
71 | 0 | private boolean selected = false; |
72 | |
private Widget tab; |
73 | 0 | private ClickablePanel tabPanel = new ClickablePanel(); |
74 | |
private Widget displayContent; |
75 | 0 | private int labelIndex = -1; |
76 | 0 | private String tabKey = ""; |
77 | 0 | private List<Callback<String>> callbacks = new ArrayList<Callback<String>>(); |
78 | |
|
79 | 0 | public Tab(String key, String label, Image image, Widget displayContent) { |
80 | 0 | tabKey = key; |
81 | 0 | HorizontalPanel tabContent = new HorizontalPanel(); |
82 | 0 | tabContent.add(image); |
83 | 0 | KSLabel labelWidget = new KSLabel(label); |
84 | 0 | tabContent.add(labelWidget); |
85 | 0 | labelIndex = tabContent.getWidgetIndex(labelWidget); |
86 | 0 | init(tabContent, displayContent); |
87 | |
|
88 | 0 | } |
89 | |
|
90 | 0 | public Tab(String key, String label, Widget displayContent) { |
91 | 0 | tabKey = key; |
92 | 0 | KSLabel labelWidget = new KSLabel(label); |
93 | 0 | init(labelWidget, displayContent); |
94 | 0 | } |
95 | |
|
96 | 0 | public Tab(String key, Widget tab, Widget displayContent) { |
97 | 0 | tabKey = key; |
98 | 0 | init(tab, displayContent); |
99 | 0 | } |
100 | |
|
101 | |
public void addCallback(Callback<String> callback) { |
102 | 0 | callbacks.add(callback); |
103 | 0 | } |
104 | |
|
105 | |
public void emptyCallbacks() { |
106 | 0 | callbacks = new ArrayList<Callback<String>>(); |
107 | 0 | } |
108 | |
|
109 | |
public String getTabKey() { |
110 | 0 | return tabKey; |
111 | |
} |
112 | |
|
113 | |
public void init(Widget tab, Widget displayContent) { |
114 | 0 | this.tab = tab; |
115 | 0 | this.tabPanel.setWidget(tab); |
116 | 0 | tabPanel.addMouseOverHandler(new MouseOverHandler() { |
117 | |
|
118 | |
@Override |
119 | |
public void onMouseOver(MouseOverEvent event) { |
120 | 0 | Tab.this.onMouseOver(); |
121 | |
|
122 | 0 | } |
123 | |
}); |
124 | 0 | tabPanel.addClickHandler(new ClickHandler() { |
125 | |
|
126 | |
@Override |
127 | |
public void onClick(ClickEvent event) { |
128 | 0 | Tab.this.onSelect(); |
129 | 0 | if (!callbacks.isEmpty()) { |
130 | 0 | for (Callback<String> callback : callbacks) { |
131 | 0 | callback.exec(tabKey); |
132 | |
} |
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | 0 | HistoryManager.logHistoryChange(); |
138 | |
} |
139 | 0 | } |
140 | |
}); |
141 | 0 | tabPanel.addMouseOutHandler(new MouseOutHandler() { |
142 | |
|
143 | |
@Override |
144 | |
public void onMouseOut(MouseOutEvent event) { |
145 | 0 | Tab.this.onMouseOut(); |
146 | |
|
147 | 0 | } |
148 | |
}); |
149 | 0 | tabPanel.addStyleName("KS-TabPanel-Tab"); |
150 | 0 | this.initWidget(tabPanel); |
151 | 0 | this.displayContent = displayContent; |
152 | 0 | } |
153 | |
|
154 | |
public void addTabStyleName(String style) { |
155 | 0 | tabPanel.addStyleName(style); |
156 | 0 | } |
157 | |
|
158 | |
public void onSelect() { |
159 | 0 | KSTabPanelImpl.this.deselectCurrentTab(); |
160 | 0 | KSTabPanelImpl.this.selectedTab = Tab.this; |
161 | 0 | selected = true; |
162 | 0 | this.onMouseOut(); |
163 | 0 | tabPanel.addStyleName("KS-TabPanel-Tab-Selected"); |
164 | 0 | if (tab instanceof Label || tab instanceof KSLabel) { |
165 | 0 | tab.addStyleName("KS-TabPanel-Tab-Label-Selected"); |
166 | 0 | } else if (tab instanceof ComplexPanel) { |
167 | 0 | if (labelIndex != -1) { |
168 | 0 | Widget w = ((ComplexPanel) tab).getWidget(labelIndex); |
169 | 0 | w.addStyleName("KS-TabPanel-Tab-Label-Selected"); |
170 | |
} |
171 | |
} |
172 | 0 | KSTabPanelImpl.this.content.clear(); |
173 | 0 | KSTabPanelImpl.this.content.setWidget(displayContent); |
174 | |
|
175 | 0 | } |
176 | |
|
177 | |
public void onDeselect() { |
178 | 0 | selected = false; |
179 | 0 | tabPanel.removeStyleName("KS-TabPanel-Tab-Selected"); |
180 | 0 | if (tab instanceof Label || tab instanceof KSLabel) { |
181 | 0 | tab.removeStyleName("KS-TabPanel-Tab-Label-Selected"); |
182 | 0 | } else if (tab instanceof ComplexPanel) { |
183 | 0 | if (labelIndex != -1) { |
184 | 0 | Widget w = ((ComplexPanel) tab).getWidget(labelIndex); |
185 | 0 | w.removeStyleName("KS-TabPanel-Tab-Label-Selected"); |
186 | |
} |
187 | |
} |
188 | 0 | } |
189 | |
|
190 | |
public void onMouseOver() { |
191 | 0 | if (!selected) { |
192 | 0 | tabPanel.addStyleName("KS-TabPanel-Tab-Hover"); |
193 | 0 | if (tab instanceof Label || tab instanceof KSLabel) { |
194 | 0 | tab.addStyleName("KS-TabPanel-Tab-Label-Hover"); |
195 | 0 | } else if (tab instanceof ComplexPanel) { |
196 | 0 | if (labelIndex != -1) { |
197 | 0 | Widget w = ((ComplexPanel) tab).getWidget(labelIndex); |
198 | 0 | w.addStyleName("KS-TabPanel-Tab-Label-Hover"); |
199 | |
} |
200 | |
} |
201 | |
} |
202 | 0 | } |
203 | |
|
204 | |
public void onMouseOut() { |
205 | 0 | tabPanel.removeStyleName("KS-TabPanel-Tab-Hover"); |
206 | 0 | if (tab instanceof Label || tab instanceof KSLabel) { |
207 | 0 | tab.removeStyleName("KS-TabPanel-Tab-Label-Hover"); |
208 | 0 | } else if (tab instanceof ComplexPanel) { |
209 | 0 | if (labelIndex != -1) { |
210 | 0 | Widget w = ((ComplexPanel) tab).getWidget(labelIndex); |
211 | 0 | w.removeStyleName("KS-TabPanel-Tab-Label-Hover"); |
212 | |
} |
213 | |
} |
214 | 0 | } |
215 | |
} |
216 | |
|
217 | 0 | public KSTabPanelImpl() { |
218 | |
|
219 | |
|
220 | |
|
221 | 0 | tabRow.add(left); |
222 | 0 | tabRow.add(right); |
223 | 0 | tabRow.addStyleName("KS-TabPanel-TabRow"); |
224 | 0 | left.addStyleName("KS-TabPanel-Left-Panel"); |
225 | 0 | right.addStyleName("KS-TabPanel-Right-Panel"); |
226 | 0 | container.addStyleName("KS-TabPanel-Full"); |
227 | 0 | content.addStyleName("KS-TabPanel-Content"); |
228 | 0 | container.add(tabRow); |
229 | 0 | container.add(content); |
230 | 0 | this.initWidget(container); |
231 | 0 | } |
232 | |
|
233 | 0 | public KSTabPanelImpl(TabPanelStyle style) { |
234 | |
|
235 | |
|
236 | |
|
237 | 0 | tabRow.add(left); |
238 | 0 | tabRow.add(right); |
239 | 0 | tabRow.addStyleName("KS-TabPanel-TabRow"); |
240 | 0 | left.addStyleName("KS-TabPanel-Left-Panel"); |
241 | 0 | right.addStyleName("KS-TabPanel-Right-Panel"); |
242 | |
|
243 | 0 | container.addStyleName("KS-TabPanel"); |
244 | 0 | content.addStyleName("KS-TabPanel-Content"); |
245 | 0 | container.add(tabRow); |
246 | 0 | container.add(content); |
247 | 0 | this.initWidget(container); |
248 | 0 | } |
249 | |
|
250 | |
public void setTabPanelStyle(TabPanelStyle style) { |
251 | 0 | if (style == TabPanelStyle.FULL_PAGE) { |
252 | 0 | container.setStyleName("KS-TabPanel-Full"); |
253 | 0 | } else if (style == TabPanelStyle.SMALL) { |
254 | 0 | container.setStyleName("KS-TabPanel-Small"); |
255 | |
} |
256 | 0 | } |
257 | |
|
258 | |
private void deselectCurrentTab() { |
259 | 0 | if (selectedTab != null) { |
260 | 0 | selectedTab.onDeselect(); |
261 | |
} |
262 | 0 | } |
263 | |
|
264 | |
@Override |
265 | |
public void selectTab(String key) { |
266 | 0 | Tab tab = tabMap.get(key); |
267 | 0 | tab.onSelect(); |
268 | 0 | } |
269 | |
|
270 | |
@Override |
271 | |
public void removeTab(String key) { |
272 | 0 | Tab tab = tabMap.get(key); |
273 | 0 | tab.removeFromParent(); |
274 | 0 | } |
275 | |
|
276 | |
private void positionTab(Tab tab, TabPosition position) { |
277 | 0 | if (position == TabPosition.LEFT) { |
278 | 0 | tab.addStyleName("KS-TabPanel-Tab-Left"); |
279 | 0 | left.add(tab); |
280 | |
} else { |
281 | 0 | tab.addStyleName("KS-TabPanel-Tab-Right"); |
282 | 0 | right.add(tab); |
283 | |
} |
284 | 0 | } |
285 | |
|
286 | |
@Override |
287 | |
public void addTab(String key, Widget tabWidget, Widget content, TabPosition position) { |
288 | 0 | Tab tab = new Tab(key, tabWidget, content); |
289 | 0 | tabMap.put(key, tab); |
290 | 0 | positionTab(tab, position); |
291 | 0 | } |
292 | |
|
293 | |
@Override |
294 | |
public void addTab(String key, String label, Widget content, TabPosition position) { |
295 | 0 | Tab tab = new Tab(key, label, content); |
296 | 0 | tabMap.put(key, tab); |
297 | 0 | positionTab(tab, position); |
298 | 0 | } |
299 | |
|
300 | |
@Override |
301 | |
public void addTab(String key, String label, Image image, Widget content, TabPosition position) { |
302 | 0 | Tab tab = new Tab(key, label, image, content); |
303 | 0 | tabMap.put(key, tab); |
304 | 0 | positionTab(tab, position); |
305 | 0 | } |
306 | |
|
307 | |
@Override |
308 | |
public void addTab(String key, String label, Image image, Widget content) { |
309 | 0 | Tab tab = new Tab(key, label, image, content); |
310 | 0 | tabMap.put(key, tab); |
311 | 0 | positionTab(tab, TabPosition.LEFT); |
312 | 0 | } |
313 | |
|
314 | |
@Override |
315 | |
public void addTab(String key, String label, Widget content) { |
316 | 0 | Tab tab = new Tab(key, label, content); |
317 | 0 | tabMap.put(key, tab); |
318 | 0 | positionTab(tab, TabPosition.LEFT); |
319 | 0 | } |
320 | |
|
321 | |
@Override |
322 | |
public void addTab(String key, Widget tabWidget, Widget content) { |
323 | 0 | Tab tab = new Tab(key, tabWidget, content); |
324 | 0 | tabMap.put(key, tab); |
325 | 0 | positionTab(tab, TabPosition.LEFT); |
326 | 0 | } |
327 | |
|
328 | |
@Override |
329 | |
public void addStyleName(String style) { |
330 | 0 | container.addStyleName(style); |
331 | 0 | } |
332 | |
|
333 | |
@Override |
334 | |
public void addTabCustomCallback(final String key, final Callback<String> callback) { |
335 | 0 | Tab tab = tabMap.get(key); |
336 | 0 | if (tab != null) { |
337 | 0 | tab.addCallback(callback); |
338 | |
} |
339 | |
|
340 | 0 | } |
341 | |
|
342 | |
@Override |
343 | |
public String getSelectedTabKey() { |
344 | 0 | if (selectedTab != null) { |
345 | 0 | return selectedTab.getTabKey(); |
346 | |
} else { |
347 | 0 | return ""; |
348 | |
} |
349 | |
|
350 | |
} |
351 | |
|
352 | |
@Override |
353 | |
public Widget getSelectedTab() { |
354 | 0 | return selectedTab.displayContent; |
355 | |
} |
356 | |
|
357 | |
@Override |
358 | |
public String getSelectedTabName() { |
359 | 0 | Widget widget = selectedTab.tab; |
360 | 0 | if (widget instanceof HasText) { |
361 | 0 | return ((HasText) widget).getText(); |
362 | |
} |
363 | 0 | return ""; |
364 | |
} |
365 | |
|
366 | |
@Override |
367 | |
public int getTabCount() { |
368 | 0 | return tabMap.size(); |
369 | |
} |
370 | |
|
371 | |
@Override |
372 | |
public void removeTabCustomCallbacks(String key) { |
373 | 0 | Tab tab = tabMap.get(key); |
374 | 0 | if (tab != null) { |
375 | 0 | tab.emptyCallbacks(); |
376 | |
} |
377 | |
|
378 | 0 | } |
379 | |
|
380 | |
@Override |
381 | |
public boolean hasTabKey(String key) { |
382 | 0 | return tabMap.containsKey(key); |
383 | |
} |
384 | |
|
385 | |
} |