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