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 | |
|
240 | 0 | contentPanel.add(itemLabel); |
241 | 0 | if(item.getShownIcon() != null){ |
242 | 0 | if(imgLoc == MenuImageLocation.RIGHT){ |
243 | 0 | contentPanel.add(item.getShownIcon()); |
244 | |
} |
245 | |
else{ |
246 | 0 | contentPanel.insert(item.getShownIcon(), 0); |
247 | |
} |
248 | |
} |
249 | |
|
250 | 0 | if(indent == 1 && !(item.getSubItems().isEmpty())){ |
251 | 0 | itemLabel.setText(item.getLabel()); |
252 | 0 | itemLabel.addStyleName("KS-Basic-Menu-Toplevel-Item-Label"); |
253 | 0 | this.addStyleName("KS-Basic-Menu-Toplevel-Item-Panel"); |
254 | 0 | this.add(contentPanel); |
255 | |
} else { |
256 | 0 | itemLabel.setText(item.getLabel()); |
257 | 0 | anchorPanel.add(contentPanel, id + "anchor"); |
258 | 0 | this.add(anchorPanel); |
259 | |
} |
260 | 0 | } |
261 | |
|
262 | |
public void addImage(Image shownIcon) { |
263 | 0 | shownIcon.addStyleName("KS-Basic-Menu-Item-Image"); |
264 | 0 | contentPanel.add(shownIcon); |
265 | |
|
266 | 0 | } |
267 | |
|
268 | |
public void deSelect(){ |
269 | 0 | this.removeStyleName("KS-Basic-Menu-Item-Panel-Selected"); |
270 | 0 | itemLabel.removeStyleName("KS-Basic-Menu-Item-Label-Selected"); |
271 | 0 | selected = false; |
272 | 0 | } |
273 | |
|
274 | |
public void select(){ |
275 | 0 | this.addStyleName("KS-Basic-Menu-Item-Panel-Selected"); |
276 | 0 | itemLabel.addStyleName("KS-Basic-Menu-Item-Label-Selected"); |
277 | 0 | selected = true; |
278 | 0 | } |
279 | |
|
280 | |
public KSLabel getItemLabel() { |
281 | 0 | return itemLabel; |
282 | |
} |
283 | |
|
284 | |
public boolean isSelectable() { |
285 | 0 | return selectable; |
286 | |
} |
287 | |
|
288 | |
public void setSelectable(boolean selectable) { |
289 | 0 | this.selectable = selectable; |
290 | 0 | } |
291 | |
|
292 | |
public KSMenuItemData getItem() { |
293 | 0 | return item; |
294 | |
} |
295 | |
|
296 | |
public boolean isSelected() { |
297 | 0 | return selected; |
298 | |
} |
299 | |
|
300 | |
public void setSelected(boolean selected) { |
301 | 0 | this.selected = selected; |
302 | 0 | } |
303 | |
|
304 | |
} |
305 | |
|
306 | |
@Override |
307 | |
protected void populateMenu() { |
308 | 0 | menuPanel.clear(); |
309 | 0 | menuItems.clear(); |
310 | 0 | createMenuItems(items, 1); |
311 | 0 | } |
312 | |
|
313 | |
private void createMenuItems(List<KSMenuItemData> theItems, int currentDepth){ |
314 | 0 | int itemNum = 0; |
315 | 0 | for(KSMenuItemData i: theItems){ |
316 | 0 | itemNum++; |
317 | 0 | addMenuItem(new MenuItemPanel(i, currentDepth, itemNum)); |
318 | 0 | if(!(i.getSubItems().isEmpty())){ |
319 | 0 | createMenuItems(i.getSubItems(), currentDepth + 1); |
320 | |
} |
321 | 0 | i.addMenuEventHandler(MenuSelectEvent.TYPE, menuHandler); |
322 | 0 | i.addMenuEventHandler(MenuChangeEvent.TYPE, menuHandler); |
323 | |
} |
324 | 0 | } |
325 | |
|
326 | |
private void addMenuItem(MenuItemPanel panel){ |
327 | 0 | menuPanel.add(panel); |
328 | 0 | menuItems.add(panel); |
329 | 0 | } |
330 | |
|
331 | |
public boolean isNumberAllItems() { |
332 | 0 | return numberAllItems; |
333 | |
} |
334 | |
|
335 | |
public void setNumberAllItems(boolean numberAllItems) { |
336 | 0 | this.numberAllItems = numberAllItems; |
337 | 0 | } |
338 | |
|
339 | |
@Override |
340 | |
public boolean selectMenuItem(String[] hierarchy) { |
341 | 0 | List<KSMenuItemData> currentItems = items; |
342 | 0 | KSMenuItemData itemToSelect = null; |
343 | 0 | for(String s: hierarchy){ |
344 | 0 | s = s.trim(); |
345 | 0 | for(KSMenuItemData i: currentItems){ |
346 | 0 | if(s.equalsIgnoreCase(i.getLabel().trim())){ |
347 | 0 | itemToSelect = i; |
348 | 0 | currentItems = i.getSubItems(); |
349 | 0 | break; |
350 | |
} |
351 | |
} |
352 | |
} |
353 | |
|
354 | 0 | if(itemToSelect != null){ |
355 | |
|
356 | 0 | for(MenuItemPanel p: menuItems){ |
357 | 0 | if(itemToSelect.equals(p.getItem())){ |
358 | 0 | p.getItem().setSelected(true); |
359 | 0 | return true; |
360 | |
} |
361 | |
} |
362 | |
|
363 | |
} |
364 | |
|
365 | 0 | return false; |
366 | |
} |
367 | |
|
368 | |
|
369 | |
|
370 | |
|
371 | |
@Override |
372 | |
public void clearSelected() { |
373 | 0 | for(MenuItemPanel m : menuItems){ |
374 | 0 | m.deSelect(); |
375 | 0 | m.getItem().unhandledSetSelected(false); |
376 | |
} |
377 | 0 | } |
378 | |
|
379 | |
public void addGlobalMenuItemSelectCallback(Callback<KSMenuItemData> callback){ |
380 | 0 | globalCallbacks.add(callback); |
381 | 0 | } |
382 | |
|
383 | |
public MenuImageLocation getImageLocation() { |
384 | 0 | return imgLoc; |
385 | |
} |
386 | |
|
387 | |
public void setImageLocation(MenuImageLocation imgLoc) { |
388 | 0 | this.imgLoc = imgLoc; |
389 | 0 | } |
390 | |
|
391 | |
} |