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