1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.common.ui.client.widgets.menus.impl; |
17 | |
|
18 | |
import java.util.ArrayList; |
19 | |
import java.util.List; |
20 | |
|
21 | |
import org.kuali.student.common.ui.client.mvc.Callback; |
22 | |
import org.kuali.student.common.ui.client.util.DebugIdUtils; |
23 | |
import org.kuali.student.common.ui.client.widgets.ClickablePanel; |
24 | |
import org.kuali.student.common.ui.client.widgets.KSLabel; |
25 | |
import org.kuali.student.common.ui.client.widgets.layout.HorizontalBlockFlowPanel; |
26 | |
import org.kuali.student.common.ui.client.widgets.menus.KSBasicMenuAbstract; |
27 | |
import org.kuali.student.common.ui.client.widgets.menus.KSListPanel; |
28 | |
import org.kuali.student.common.ui.client.widgets.menus.KSMenuItemData; |
29 | |
import org.kuali.student.common.ui.client.widgets.menus.MenuChangeEvent; |
30 | |
import org.kuali.student.common.ui.client.widgets.menus.MenuEventHandler; |
31 | |
import org.kuali.student.common.ui.client.widgets.menus.MenuSelectEvent; |
32 | |
|
33 | |
import com.google.gwt.user.client.ui.*; |
34 | |
import com.google.gwt.event.dom.client.BlurEvent; |
35 | |
import com.google.gwt.event.dom.client.BlurHandler; |
36 | |
import com.google.gwt.event.dom.client.ClickEvent; |
37 | |
import com.google.gwt.event.dom.client.ClickHandler; |
38 | |
import com.google.gwt.event.dom.client.FocusEvent; |
39 | |
import com.google.gwt.event.dom.client.FocusHandler; |
40 | |
import com.google.gwt.event.dom.client.MouseOutEvent; |
41 | |
import com.google.gwt.event.dom.client.MouseOutHandler; |
42 | |
import com.google.gwt.event.dom.client.MouseOverEvent; |
43 | |
import com.google.gwt.event.dom.client.MouseOverHandler; |
44 | |
|
45 | 0 | public class KSListMenuImpl extends KSBasicMenuAbstract{ |
46 | 0 | private KSListPanel menuPanel = new KSListPanel(); |
47 | 0 | private VerticalPanel menuTitlePanel = new VerticalPanel(); |
48 | 0 | private KSLabel menuTitle = new KSLabel(); |
49 | 0 | private KSLabel menuDescription = new KSLabel(); |
50 | 0 | private List<MenuItemPanel> menuItems = new ArrayList<MenuItemPanel>(); |
51 | 0 | private boolean numberAllItems = false; |
52 | 0 | private List<Callback<KSMenuItemData>> globalCallbacks = new ArrayList<Callback<KSMenuItemData>>(); |
53 | 0 | private MenuImageLocation imgLoc = MenuImageLocation.RIGHT; |
54 | |
|
55 | 0 | private EventHandler handler = new EventHandler(); |
56 | |
|
57 | 0 | private MenuEventHandler menuHandler = new MenuEventHandler(){ |
58 | |
|
59 | |
@Override |
60 | |
public void onChange(MenuChangeEvent e) { |
61 | 0 | KSMenuItemData i = (KSMenuItemData) e.getSource(); |
62 | 0 | MenuItemPanel itemToChange = null; |
63 | 0 | for(MenuItemPanel p: menuItems){ |
64 | 0 | if(i.equals(p.getItem())){ |
65 | 0 | itemToChange = p; |
66 | |
} |
67 | |
} |
68 | 0 | if(itemToChange != null){ |
69 | 0 | if(!(i.getLabel().equals(itemToChange.getItemLabel().getText()))){ |
70 | 0 | itemToChange.getItemLabel().setText(i.getLabel()); |
71 | |
} |
72 | 0 | else if(i.getShownIcon() != null){ |
73 | 0 | itemToChange.addImage(i.getShownIcon()); |
74 | |
} |
75 | |
} |
76 | |
|
77 | 0 | } |
78 | |
|
79 | |
@Override |
80 | |
public void onSelect(MenuSelectEvent e) { |
81 | |
|
82 | 0 | KSMenuItemData i = (KSMenuItemData) e.getSource(); |
83 | 0 | MenuItemPanel itemToSelect = null; |
84 | 0 | for(MenuItemPanel p: menuItems){ |
85 | 0 | if(i.equals(p.getItem())){ |
86 | 0 | itemToSelect = p; |
87 | |
} |
88 | |
} |
89 | 0 | if(itemToSelect != null){ |
90 | 0 | if(e.firesClickEvent()){ |
91 | 0 | itemToSelect.fireEvent(new ClickEvent(){}); |
92 | |
} |
93 | |
else{ |
94 | 0 | selectMenuItemPanel(itemToSelect); |
95 | 0 | itemToSelect.removeStyleName("KS-Basic-Menu-Item-Panel-Hover"); |
96 | 0 | itemToSelect.getItemLabel().removeStyleName("KS-Basic-Menu-Item-Label-Hover"); |
97 | |
} |
98 | |
} |
99 | 0 | } |
100 | |
}; |
101 | |
|
102 | 0 | public KSListMenuImpl(){ |
103 | 0 | menuTitlePanel.add(menuTitle); |
104 | 0 | menuTitlePanel.add(menuDescription); |
105 | 0 | menuTitlePanel.setVisible(false); |
106 | 0 | menuPanel.add(menuTitlePanel); |
107 | |
|
108 | 0 | menuTitlePanel.addStyleName("KS-Basic-Menu-Title-Panel"); |
109 | 0 | menuPanel.addStyleName( "KS-Basic-Menu-Panel"); |
110 | |
|
111 | 0 | menuTitle.addStyleName("KS-Basic-Menu-Title-Label"); |
112 | 0 | menuTitle.addStyleName("KS-Indent" + "-1"); |
113 | |
|
114 | 0 | menuDescription.addStyleName("KS-Basic-Menu-Desc-Label"); |
115 | 0 | menuDescription.addStyleName("KS-Indent" + "-1"); |
116 | |
|
117 | 0 | this.initWidget(menuPanel); |
118 | 0 | } |
119 | |
|
120 | |
public void setTitle(String title){ |
121 | 0 | menuTitlePanel.setVisible(true); |
122 | 0 | menuTitle.setText(title); |
123 | 0 | menuTitle.setWordWrap(true); |
124 | |
|
125 | 0 | } |
126 | |
|
127 | |
public void setDescription(String description){ |
128 | 0 | menuTitlePanel.setVisible(true); |
129 | 0 | menuDescription.setText(description); |
130 | 0 | menuDescription.setWordWrap(true); |
131 | |
|
132 | 0 | } |
133 | |
|
134 | 0 | private class EventHandler implements ClickHandler, MouseOverHandler, MouseOutHandler{ |
135 | |
|
136 | |
@Override |
137 | |
public void onClick(ClickEvent event) { |
138 | 0 | Widget sender = (Widget) event.getSource(); |
139 | 0 | if(sender instanceof MenuItemPanel){ |
140 | 0 | selectMenuItemPanel((MenuItemPanel)sender); |
141 | 0 | sender.removeStyleName("KS-Basic-Menu-Item-Panel-Hover"); |
142 | 0 | ((MenuItemPanel) sender).getItemLabel().removeStyleName("KS-Basic-Menu-Item-Label-Hover"); |
143 | |
} |
144 | 0 | } |
145 | |
|
146 | |
@Override |
147 | |
public void onMouseOver(MouseOverEvent event) { |
148 | 0 | Widget sender = (Widget) event.getSource(); |
149 | 0 | if(sender instanceof MenuItemPanel){ |
150 | 0 | if(((MenuItemPanel) sender).isSelectable() && !((MenuItemPanel) sender).isSelected()){ |
151 | 0 | sender.addStyleName("KS-Basic-Menu-Item-Panel-Hover"); |
152 | 0 | ((MenuItemPanel) sender).getItemLabel().addStyleName("KS-Basic-Menu-Item-Label-Hover"); |
153 | |
} |
154 | |
} |
155 | 0 | } |
156 | |
|
157 | |
@Override |
158 | |
public void onMouseOut(MouseOutEvent event) { |
159 | 0 | Widget sender = (Widget) event.getSource(); |
160 | 0 | if(sender instanceof MenuItemPanel){ |
161 | 0 | if(((MenuItemPanel) sender).isSelectable()){ |
162 | 0 | sender.removeStyleName("KS-Basic-Menu-Item-Panel-Hover"); |
163 | 0 | ((MenuItemPanel) sender).getItemLabel().removeStyleName("KS-Basic-Menu-Item-Label-Hover"); |
164 | |
} |
165 | |
} |
166 | 0 | } |
167 | |
} |
168 | |
|
169 | |
private void selectMenuItemPanel(MenuItemPanel toBeSelected) { |
170 | 0 | for(Callback<KSMenuItemData> c: globalCallbacks){ |
171 | 0 | c.exec(toBeSelected.getItem()); |
172 | |
} |
173 | 0 | if(toBeSelected.isSelectable()){ |
174 | 0 | clearSelected(); |
175 | 0 | toBeSelected.select(); |
176 | 0 | toBeSelected.getItem().unhandledSetSelected(true); |
177 | |
} |
178 | |
|
179 | 0 | } |
180 | |
|
181 | |
private class MenuItemPanel extends ClickablePanel{ |
182 | 0 | KSLabel itemLabel = new KSLabel(); |
183 | 0 | HorizontalBlockFlowPanel contentPanel = new HorizontalBlockFlowPanel(); |
184 | 0 | boolean selectable = false; |
185 | 0 | boolean selected = false; |
186 | |
KSMenuItemData item; |
187 | |
int indent; |
188 | |
int itemNum; |
189 | 0 | private String id = HTMLPanel.createUniqueId(); |
190 | 0 | HTMLPanel anchorPanel = new HTMLPanel("<span id='" + id + "'></span>"); |
191 | 0 | Anchor a = new Anchor(); |
192 | |
|
193 | 0 | public MenuItemPanel(KSMenuItemData item, int indent, int itemNum){ |
194 | 0 | this.item = item; |
195 | 0 | this.indent = indent; |
196 | 0 | this.itemNum = itemNum; |
197 | 0 | this.getElement().setTabIndex(-1); |
198 | 0 | a.setHref("javascript:return false;"); |
199 | 0 | a.getElement().setId(id + "anchor"); |
200 | 0 | anchorPanel.add(a, id); |
201 | |
|
202 | 0 | itemLabel.setWordWrap(true); |
203 | 0 | if(item.getSpecialStyle() != null){ |
204 | 0 | this.addStyleName(item.getSpecialStyle()); |
205 | |
} |
206 | |
else{ |
207 | 0 | this.addStyleName("KS-Basic-Menu-Item-Panel"); |
208 | |
} |
209 | 0 | itemLabel.ensureDebugId(DebugIdUtils.createWebDriverSafeDebugId(item.getLabel() + "-label")); |
210 | 0 | itemLabel.addStyleName("KS-Basic-Menu-Item-Label"); |
211 | 0 | itemLabel.getElement().setAttribute("style", "white-space: nowrap"); |
212 | 0 | if(item.getClickHandler() != null){ |
213 | 0 | this.addClickHandler(item.getClickHandler()); |
214 | 0 | itemLabel.addStyleName("KS-Basic-Menu-Clickable-Item-Label"); |
215 | 0 | selectable = true; |
216 | |
} |
217 | 0 | this.addClickHandler(handler); |
218 | 0 | this.addMouseOverHandler(handler); |
219 | 0 | this.addMouseOutHandler(handler); |
220 | |
|
221 | 0 | a.addFocusHandler(new FocusHandler() { |
222 | |
@Override |
223 | |
public void onFocus(FocusEvent event) { |
224 | 0 | if (MenuItemPanel.this.isSelectable() && !MenuItemPanel.this.isSelected()) { |
225 | 0 | MenuItemPanel.this.addStyleName("KS-Basic-Menu-Item-Panel-Hover"); |
226 | 0 | MenuItemPanel.this.getItemLabel().addStyleName("KS-Basic-Menu-Item-Label-Hover"); |
227 | |
} |
228 | 0 | } |
229 | |
}); |
230 | |
|
231 | 0 | a.addBlurHandler(new BlurHandler() { |
232 | |
@Override |
233 | |
public void onBlur(BlurEvent event) { |
234 | 0 | if (MenuItemPanel.this.isSelectable()) { |
235 | 0 | MenuItemPanel.this.removeStyleName("KS-Basic-Menu-Item-Panel-Hover"); |
236 | 0 | MenuItemPanel.this.getItemLabel().removeStyleName("KS-Basic-Menu-Item-Label-Hover"); |
237 | |
} |
238 | 0 | } |
239 | |
}); |
240 | |
|
241 | 0 | a.setTabIndex(itemNum + 1); |
242 | 0 | contentPanel.ensureDebugId(DebugIdUtils.createWebDriverSafeDebugId(item.getLabel() + "-panel")); |
243 | 0 | contentPanel.add(itemLabel); |
244 | 0 | if(item.getShownIcon() != null){ |
245 | 0 | if(imgLoc == MenuImageLocation.RIGHT){ |
246 | 0 | contentPanel.add(item.getShownIcon()); |
247 | |
} |
248 | |
else{ |
249 | 0 | contentPanel.insert(item.getShownIcon(), 0); |
250 | |
} |
251 | |
} |
252 | |
|
253 | 0 | if(indent == 1 && !(item.getSubItems().isEmpty())){ |
254 | 0 | itemLabel.setText(item.getLabel()); |
255 | 0 | itemLabel.addStyleName("KS-Basic-Menu-Toplevel-Item-Label"); |
256 | 0 | this.addStyleName("KS-Basic-Menu-Toplevel-Item-Panel"); |
257 | 0 | this.add(contentPanel); |
258 | |
} else { |
259 | 0 | itemLabel.setText(item.getLabel()); |
260 | 0 | anchorPanel.add(contentPanel, id + "anchor"); |
261 | 0 | this.add(anchorPanel); |
262 | |
} |
263 | 0 | a.ensureDebugId(DebugIdUtils.createWebDriverSafeDebugId(item.getLabel() + "-anchor")); |
264 | 0 | anchorPanel.ensureDebugId(DebugIdUtils.createWebDriverSafeDebugId(item.getLabel() + "-anchorPanel")); |
265 | 0 | } |
266 | |
|
267 | |
public void addImage(Image shownIcon) { |
268 | 0 | shownIcon.addStyleName("KS-Basic-Menu-Item-Image"); |
269 | 0 | contentPanel.add(shownIcon); |
270 | |
|
271 | 0 | } |
272 | |
|
273 | |
public void deSelect(){ |
274 | 0 | this.removeStyleName("KS-Basic-Menu-Item-Panel-Selected"); |
275 | 0 | itemLabel.removeStyleName("KS-Basic-Menu-Item-Label-Selected"); |
276 | 0 | selected = false; |
277 | 0 | } |
278 | |
|
279 | |
public void select(){ |
280 | 0 | this.addStyleName("KS-Basic-Menu-Item-Panel-Selected"); |
281 | 0 | itemLabel.addStyleName("KS-Basic-Menu-Item-Label-Selected"); |
282 | 0 | selected = true; |
283 | 0 | } |
284 | |
|
285 | |
public KSLabel getItemLabel() { |
286 | 0 | return itemLabel; |
287 | |
} |
288 | |
|
289 | |
public boolean isSelectable() { |
290 | 0 | return selectable; |
291 | |
} |
292 | |
|
293 | |
public void setSelectable(boolean selectable) { |
294 | 0 | this.selectable = selectable; |
295 | 0 | } |
296 | |
|
297 | |
public KSMenuItemData getItem() { |
298 | 0 | return item; |
299 | |
} |
300 | |
|
301 | |
public boolean isSelected() { |
302 | 0 | return selected; |
303 | |
} |
304 | |
|
305 | |
public void setSelected(boolean selected) { |
306 | 0 | this.selected = selected; |
307 | 0 | } |
308 | |
|
309 | |
} |
310 | |
|
311 | |
@Override |
312 | |
protected void populateMenu() { |
313 | 0 | menuPanel.clear(); |
314 | 0 | menuItems.clear(); |
315 | 0 | createMenuItems(items, 1); |
316 | 0 | } |
317 | |
|
318 | |
private void createMenuItems(List<KSMenuItemData> theItems, int currentDepth){ |
319 | 0 | int itemNum = 0; |
320 | 0 | for(KSMenuItemData i: theItems){ |
321 | 0 | itemNum++; |
322 | 0 | addMenuItem(new MenuItemPanel(i, currentDepth, itemNum)); |
323 | 0 | if(!(i.getSubItems().isEmpty())){ |
324 | 0 | createMenuItems(i.getSubItems(), currentDepth + 1); |
325 | |
} |
326 | 0 | i.addMenuEventHandler(MenuSelectEvent.TYPE, menuHandler); |
327 | 0 | i.addMenuEventHandler(MenuChangeEvent.TYPE, menuHandler); |
328 | |
} |
329 | 0 | } |
330 | |
|
331 | |
private void addMenuItem(MenuItemPanel panel){ |
332 | 0 | menuPanel.add(panel); |
333 | 0 | menuItems.add(panel); |
334 | 0 | } |
335 | |
|
336 | |
public boolean isNumberAllItems() { |
337 | 0 | return numberAllItems; |
338 | |
} |
339 | |
|
340 | |
public void setNumberAllItems(boolean numberAllItems) { |
341 | 0 | this.numberAllItems = numberAllItems; |
342 | 0 | } |
343 | |
|
344 | |
@Override |
345 | |
public boolean selectMenuItem(String[] hierarchy) { |
346 | 0 | List<KSMenuItemData> currentItems = items; |
347 | 0 | KSMenuItemData itemToSelect = null; |
348 | 0 | for(String s: hierarchy){ |
349 | 0 | s = s.trim(); |
350 | 0 | for(KSMenuItemData i: currentItems){ |
351 | 0 | if(s.equalsIgnoreCase(i.getLabel().trim())){ |
352 | 0 | itemToSelect = i; |
353 | 0 | currentItems = i.getSubItems(); |
354 | 0 | break; |
355 | |
} |
356 | |
} |
357 | |
} |
358 | |
|
359 | 0 | if(itemToSelect != null){ |
360 | |
|
361 | 0 | for(MenuItemPanel p: menuItems){ |
362 | 0 | if(itemToSelect.equals(p.getItem())){ |
363 | 0 | p.getItem().setSelected(true); |
364 | 0 | return true; |
365 | |
} |
366 | |
} |
367 | |
|
368 | |
} |
369 | |
|
370 | 0 | return false; |
371 | |
} |
372 | |
|
373 | |
|
374 | |
|
375 | |
|
376 | |
@Override |
377 | |
public void clearSelected() { |
378 | 0 | for(MenuItemPanel m : menuItems){ |
379 | 0 | m.deSelect(); |
380 | 0 | m.getItem().unhandledSetSelected(false); |
381 | |
} |
382 | 0 | } |
383 | |
|
384 | |
public void addGlobalMenuItemSelectCallback(Callback<KSMenuItemData> callback){ |
385 | 0 | globalCallbacks.add(callback); |
386 | 0 | } |
387 | |
|
388 | |
public MenuImageLocation getImageLocation() { |
389 | 0 | return imgLoc; |
390 | |
} |
391 | |
|
392 | |
public void setImageLocation(MenuImageLocation imgLoc) { |
393 | 0 | this.imgLoc = imgLoc; |
394 | 0 | } |
395 | |
|
396 | |
} |