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