1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.common.ui.client.widgets; |
17 | |
|
18 | |
import java.util.List; |
19 | |
|
20 | |
import org.kuali.student.common.ui.client.mvc.Callback; |
21 | |
import org.kuali.student.common.ui.client.theme.Theme; |
22 | |
import org.kuali.student.common.ui.client.widgets.field.layout.element.SpanPanel; |
23 | |
import org.kuali.student.common.ui.client.widgets.menus.KSMenu.MenuImageLocation; |
24 | |
import org.kuali.student.common.ui.client.widgets.menus.KSMenuItemData; |
25 | |
import org.kuali.student.common.ui.client.widgets.menus.MenuChangeEvent; |
26 | |
import org.kuali.student.common.ui.client.widgets.menus.MenuEventHandler; |
27 | |
import org.kuali.student.common.ui.client.widgets.menus.MenuSelectEvent; |
28 | |
import org.kuali.student.common.ui.client.widgets.menus.impl.KSListMenuImpl; |
29 | |
|
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.KeyDownEvent; |
35 | |
import com.google.gwt.event.dom.client.KeyDownHandler; |
36 | |
import com.google.gwt.event.dom.client.MouseOutEvent; |
37 | |
import com.google.gwt.event.dom.client.MouseOutHandler; |
38 | |
import com.google.gwt.event.dom.client.MouseOverEvent; |
39 | |
import com.google.gwt.event.dom.client.MouseOverHandler; |
40 | |
import com.google.gwt.user.client.ui.Composite; |
41 | |
import com.google.gwt.user.client.ui.HTMLPanel; |
42 | |
import com.google.gwt.user.client.ui.HasHorizontalAlignment; |
43 | |
import com.google.gwt.user.client.ui.HorizontalPanel; |
44 | |
import com.google.gwt.user.client.ui.Image; |
45 | |
import com.google.gwt.user.client.ui.KeyboardListener; |
46 | |
import com.google.gwt.user.client.ui.PopupPanel; |
47 | |
import com.google.gwt.user.client.ui.Widget; |
48 | |
|
49 | 0 | public class StylishDropDown extends Composite{ |
50 | |
|
51 | 0 | private ClickablePanel namePanel = new ClickablePanel(); |
52 | 0 | private SpanPanel parentPanel = new SpanPanel(); |
53 | 0 | private boolean showSelectedItem = false; |
54 | 0 | private boolean showTitleIcon = false; |
55 | 0 | private PopupPanel menuPanel = new PopupPanel(); |
56 | 0 | private KSListMenuImpl menu = new KSListMenuImpl(); |
57 | 0 | private HorizontalPanel layout = new HorizontalPanel(); |
58 | 0 | private KSLabel titleLabel = new KSLabel(); |
59 | 0 | private Image titleImage = Theme.INSTANCE.getCommonImages().getSpacer(); |
60 | 0 | private HorizontalPanel titleLayout = new HorizontalPanel(); |
61 | 0 | private Image defaultArrow = Theme.INSTANCE.getCommonImages().getDropDownIconBlack(); |
62 | 0 | private boolean mouseOver = false; |
63 | 0 | private MenuImageLocation imgLoc = MenuImageLocation.RIGHT; |
64 | 0 | private boolean makeButton = false; |
65 | 0 | private boolean enabled = true; |
66 | |
|
67 | |
|
68 | |
private KSButton button; |
69 | |
|
70 | 0 | private ClickHandler panelHandler = new ClickHandler(){ |
71 | |
|
72 | |
@Override |
73 | |
public void onClick(ClickEvent event) { |
74 | 0 | if(enabled){ |
75 | 0 | if(!menuPanel.isShowing()){ |
76 | 0 | StylishDropDown.this.showMenu(); |
77 | |
} |
78 | |
else{ |
79 | 0 | StylishDropDown.this.hideMenu(); |
80 | |
} |
81 | |
} |
82 | |
|
83 | 0 | } |
84 | |
|
85 | |
}; |
86 | |
|
87 | 0 | private KeyDownHandler downHandler = new KeyDownHandler(){ |
88 | |
|
89 | |
@Override |
90 | |
public void onKeyDown(KeyDownEvent event) { |
91 | 0 | if(enabled){ |
92 | 0 | if (event.getNativeKeyCode() == KeyboardListener.KEY_DOWN || event.getNativeKeyCode() == KeyboardListener.KEY_ENTER) |
93 | 0 | StylishDropDown.this.showMenu(); |
94 | 0 | else if (event.getNativeKeyCode() == KeyboardListener.KEY_UP) |
95 | 0 | StylishDropDown.this.hideMenu(); |
96 | 0 | else if (event.getNativeKeyCode() == KeyboardListener.KEY_TAB) |
97 | |
{ |
98 | 0 | StylishDropDown.this.showMenu(); |
99 | 0 | titleLayout.removeStyleName("KS-Basic-Menu-Item-Panel-Main-Hover"); |
100 | |
} |
101 | |
} |
102 | 0 | } |
103 | |
}; |
104 | |
|
105 | 0 | private FocusHandler focusHandler = new FocusHandler(){ |
106 | |
|
107 | |
@Override |
108 | |
public void onFocus(FocusEvent event) { |
109 | 0 | if(enabled) |
110 | 0 | titleLayout.addStyleName("KS-Basic-Menu-Item-Panel-Main-Hover"); |
111 | 0 | } |
112 | |
}; |
113 | |
|
114 | 0 | private MouseOverHandler mouseOverHandler = new MouseOverHandler() { |
115 | |
|
116 | |
@Override |
117 | |
public void onMouseOver(MouseOverEvent event) { |
118 | 0 | titleLayout.addStyleName("KS-Basic-Menu-Item-Panel-Main-Hover"); |
119 | 0 | } |
120 | |
|
121 | |
}; |
122 | |
|
123 | 0 | private MouseOutHandler mouseOutHandler = new MouseOutHandler() { |
124 | |
|
125 | |
@Override |
126 | |
public void onMouseOut(MouseOutEvent event) { |
127 | 0 | titleLayout.removeStyleName("KS-Basic-Menu-Item-Panel-Main-Hover"); |
128 | 0 | } |
129 | |
|
130 | |
}; |
131 | |
|
132 | 0 | private MenuEventHandler menuHandler = new MenuEventHandler(){ |
133 | |
|
134 | |
@Override |
135 | |
public void onChange(MenuChangeEvent e) { |
136 | |
|
137 | |
|
138 | 0 | } |
139 | |
|
140 | |
@Override |
141 | |
public void onSelect(MenuSelectEvent e) { |
142 | 0 | KSMenuItemData i = (KSMenuItemData) e.getSource(); |
143 | 0 | StylishDropDown.this.hideMenu(); |
144 | 0 | if(showSelectedItem){ |
145 | 0 | titleLayout.clear(); |
146 | 0 | titleLabel.setText(i.getLabel()); |
147 | 0 | titleLayout.add(titleLabel); |
148 | 0 | if(i.getShownIcon() != null){ |
149 | 0 | titleLayout.add(i.getShownIcon()); |
150 | |
} |
151 | |
} |
152 | |
|
153 | 0 | } |
154 | |
}; |
155 | |
|
156 | 0 | public StylishDropDown(String title){ |
157 | 0 | titleLayout.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT); |
158 | 0 | titleLabel.setText(title); |
159 | 0 | titleLayout.add(titleLabel); |
160 | 0 | titleLayout.add(titleImage); |
161 | 0 | init(); |
162 | 0 | } |
163 | |
|
164 | 0 | public StylishDropDown(String title, Image image, MenuImageLocation imgLoc){ |
165 | 0 | titleLayout.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT); |
166 | 0 | titleLabel.setText(title); |
167 | 0 | titleImage = image; |
168 | 0 | titleLayout.add(titleLabel); |
169 | 0 | if(imgLoc == MenuImageLocation.RIGHT){ |
170 | 0 | titleLayout.add(titleImage); |
171 | |
} |
172 | |
else{ |
173 | 0 | titleLayout.insert(titleImage, 0); |
174 | |
} |
175 | 0 | menu.setImageLocation(imgLoc); |
176 | 0 | init(); |
177 | 0 | } |
178 | |
|
179 | 0 | public StylishDropDown(Widget widget){ |
180 | 0 | titleLayout.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT); |
181 | 0 | titleLayout.add(widget); |
182 | 0 | init(); |
183 | 0 | } |
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
public void makeAButtonWhenOneItem(boolean makeButton){ |
191 | 0 | this.makeButton = makeButton; |
192 | 0 | } |
193 | |
|
194 | |
private void init(){ |
195 | 0 | layout.clear(); |
196 | 0 | layout.setWidth("100%"); |
197 | 0 | layout.add(titleLayout); |
198 | 0 | layout.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT); |
199 | 0 | layout.add(defaultArrow); |
200 | |
|
201 | 0 | namePanel.setWidget(layout); |
202 | 0 | menu.addGlobalMenuItemSelectCallback(new Callback<KSMenuItemData>(){ |
203 | |
|
204 | |
@Override |
205 | |
public void exec(KSMenuItemData item) { |
206 | 0 | if(item.getClickHandler() != null){ |
207 | 0 | StylishDropDown.this.hideMenu(); |
208 | 0 | if(showSelectedItem){ |
209 | 0 | titleLabel.setText(item.getLabel()); |
210 | 0 | if(item.getShownIcon() != null && showTitleIcon){ |
211 | 0 | titleLayout.remove(titleImage); |
212 | 0 | Image image = item.getShownIcon(); |
213 | 0 | titleImage = new Image(image.getUrl(), image.getOriginLeft(), |
214 | |
image.getOriginTop(), image.getWidth(), image.getHeight()); |
215 | 0 | if(imgLoc == MenuImageLocation.RIGHT){ |
216 | 0 | titleLayout.add(titleImage); |
217 | |
} |
218 | |
else{ |
219 | 0 | titleLayout.insert(titleImage, 0); |
220 | |
} |
221 | |
|
222 | |
} |
223 | |
} |
224 | |
} |
225 | 0 | } |
226 | |
}); |
227 | 0 | menuPanel.setWidget(menu); |
228 | 0 | namePanel.addClickHandler(panelHandler); |
229 | 0 | namePanel.addKeyDownHandler(downHandler); |
230 | 0 | namePanel.addFocusHandler(focusHandler); |
231 | 0 | namePanel.addMouseOverHandler(mouseOverHandler); |
232 | 0 | namePanel.addMouseOutHandler(mouseOutHandler); |
233 | 0 | namePanel.setTabIndex(1); |
234 | 0 | menuPanel.setAutoHideEnabled(true); |
235 | 0 | menuPanel.addAutoHidePartner(namePanel.getElement()); |
236 | 0 | namePanel.getElement().setAttribute("id", HTMLPanel.createUniqueId()); |
237 | 0 | parentPanel.add(namePanel); |
238 | 0 | this.initWidget(parentPanel); |
239 | 0 | titleLabel.addStyleName("KS-CutomDropDown-TitleLabel"); |
240 | 0 | layout.addStyleName("KS-CustomDropDown-TitlePanel"); |
241 | 0 | defaultArrow.addStyleName("KS-CustomDropDown-Arrow"); |
242 | 0 | } |
243 | |
|
244 | |
public void showMenu(){ |
245 | 0 | menuPanel.setPopupPosition(layout.getAbsoluteLeft(), layout.getAbsoluteTop() + layout.getOffsetHeight()); |
246 | 0 | menuPanel.show(); |
247 | 0 | } |
248 | |
|
249 | |
public void hideMenu(){ |
250 | 0 | menuPanel.hide(); |
251 | 0 | } |
252 | |
|
253 | |
public void setArrowImage(Image arrow){ |
254 | 0 | layout.remove(defaultArrow); |
255 | 0 | arrow.addStyleName("KS-CustomDropDown-Arrow"); |
256 | 0 | layout.add(arrow); |
257 | 0 | } |
258 | |
|
259 | |
public void setItems(List<KSMenuItemData> items){ |
260 | 0 | if(makeButton && items.size() == 1){ |
261 | 0 | KSMenuItemData item = items.get(0); |
262 | 0 | button = new KSButton(); |
263 | 0 | button.addStyleName("ks-button-spacing"); |
264 | 0 | button.addClickHandler(item.getClickHandler()); |
265 | 0 | button.setText(item.getLabel()); |
266 | 0 | parentPanel.clear(); |
267 | 0 | parentPanel.add(button); |
268 | 0 | } |
269 | |
else{ |
270 | 0 | if(!namePanel.isAttached()){ |
271 | 0 | parentPanel.clear(); |
272 | 0 | parentPanel.add(namePanel); |
273 | |
} |
274 | 0 | for(KSMenuItemData item: items){ |
275 | 0 | item.addMenuEventHandler(MenuSelectEvent.TYPE, menuHandler); |
276 | 0 | item.addMenuEventHandler(MenuChangeEvent.TYPE, menuHandler); |
277 | |
} |
278 | 0 | menu.setItems(items); |
279 | |
} |
280 | |
|
281 | 0 | } |
282 | |
|
283 | |
public void setEnabled(boolean enabled){ |
284 | 0 | this.enabled = enabled; |
285 | 0 | if(!enabled){ |
286 | 0 | layout.addStyleName("KS-CustomDropDown-TitlePanel-Disabled"); |
287 | |
} |
288 | |
else{ |
289 | 0 | layout.removeStyleName("KS-CustomDropDown-TitlePanel-Disabled"); |
290 | |
} |
291 | 0 | if(button != null){ |
292 | 0 | button.setEnabled(enabled); |
293 | |
} |
294 | 0 | } |
295 | |
|
296 | |
public void setImageLocation(MenuImageLocation loc){ |
297 | 0 | imgLoc = loc; |
298 | 0 | menu.setImageLocation(loc); |
299 | 0 | } |
300 | |
|
301 | |
@Override |
302 | |
public void addStyleName(String style){ |
303 | 0 | namePanel.addStyleName(style); |
304 | 0 | menu.addStyleName(style); |
305 | 0 | } |
306 | |
|
307 | |
public boolean isShowingSelectedItem() { |
308 | 0 | return showSelectedItem; |
309 | |
} |
310 | |
|
311 | |
public void setShowSelectedItem(boolean showSelectedItem) { |
312 | 0 | this.showSelectedItem = showSelectedItem; |
313 | 0 | } |
314 | |
|
315 | |
public void setShowTitleIcon(boolean showTitleIcon){ |
316 | 0 | this.showTitleIcon = showTitleIcon; |
317 | 0 | } |
318 | |
|
319 | |
public boolean isShowingTitleIcon(){ |
320 | 0 | return showTitleIcon; |
321 | |
} |
322 | |
|
323 | |
|
324 | |
} |