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