| 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.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 |  |  import org.kuali.student.common.ui.client.widgets.menus.KSMenu.MenuImageLocation; | 
  | 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.user.client.ui.Composite; | 
  | 33 |  |  import com.google.gwt.user.client.ui.HTMLPanel; | 
  | 34 |  |  import com.google.gwt.user.client.ui.HasHorizontalAlignment; | 
  | 35 |  |  import com.google.gwt.user.client.ui.HorizontalPanel; | 
  | 36 |  |  import com.google.gwt.user.client.ui.Image; | 
  | 37 |  |  import com.google.gwt.user.client.ui.PopupPanel; | 
  | 38 |  |  import com.google.gwt.user.client.ui.Widget; | 
  | 39 |  |   | 
  | 40 | 0 |  public class StylishDropDown extends Composite{ | 
  | 41 |  |           | 
  | 42 | 0 |          private ClickablePanel namePanel = new ClickablePanel(); | 
  | 43 | 0 |          private SpanPanel parentPanel = new SpanPanel(); | 
  | 44 | 0 |          private boolean showSelectedItem = false; | 
  | 45 | 0 |          private boolean showTitleIcon = false; | 
  | 46 | 0 |          private PopupPanel menuPanel = new PopupPanel(); | 
  | 47 | 0 |          private KSListMenuImpl menu = new KSListMenuImpl(); | 
  | 48 | 0 |          private HorizontalPanel layout = new HorizontalPanel(); | 
  | 49 | 0 |          private KSLabel titleLabel = new KSLabel(); | 
  | 50 | 0 |          private Image titleImage = Theme.INSTANCE.getCommonImages().getSpacer(); | 
  | 51 | 0 |          private HorizontalPanel titleLayout = new HorizontalPanel(); | 
  | 52 | 0 |          private Image defaultArrow = Theme.INSTANCE.getCommonImages().getDropDownIconBlack(); | 
  | 53 | 0 |          private boolean mouseOver = false; | 
  | 54 | 0 |          private MenuImageLocation imgLoc = MenuImageLocation.RIGHT; | 
  | 55 | 0 |          private boolean makeButton = false; | 
  | 56 | 0 |          private boolean enabled = true; | 
  | 57 |  |           | 
  | 58 |  |           | 
  | 59 |  |          private KSButton button; | 
  | 60 |  |           | 
  | 61 | 0 |          private ClickHandler panelHandler = new ClickHandler(){ | 
  | 62 |  |   | 
  | 63 |  |                  @Override | 
  | 64 |  |                  public void onClick(ClickEvent event) { | 
  | 65 | 0 |                          if(enabled){ | 
  | 66 | 0 |                                  if(!menuPanel.isShowing()){ | 
  | 67 | 0 |                                          StylishDropDown.this.showMenu(); | 
  | 68 |  |                                  } | 
  | 69 |  |                                  else{ | 
  | 70 | 0 |                                          StylishDropDown.this.hideMenu(); | 
  | 71 |  |                                  } | 
  | 72 |  |                          } | 
  | 73 |  |                           | 
  | 74 | 0 |                  } | 
  | 75 |  |                   | 
  | 76 |  |          }; | 
  | 77 |  |           | 
  | 78 | 0 |          private MenuEventHandler menuHandler = new MenuEventHandler(){ | 
  | 79 |  |   | 
  | 80 |  |                  @Override | 
  | 81 |  |                  public void onChange(MenuChangeEvent e) { | 
  | 82 |  |                           | 
  | 83 |  |                           | 
  | 84 | 0 |                  } | 
  | 85 |  |   | 
  | 86 |  |                  @Override | 
  | 87 |  |                  public void onSelect(MenuSelectEvent e) { | 
  | 88 | 0 |                          KSMenuItemData i = (KSMenuItemData) e.getSource(); | 
  | 89 | 0 |                          StylishDropDown.this.hideMenu(); | 
  | 90 | 0 |                          if(showSelectedItem){ | 
  | 91 | 0 |                                  titleLayout.clear(); | 
  | 92 | 0 |                                  titleLabel.setText(i.getLabel()); | 
  | 93 | 0 |                                  titleLayout.add(titleLabel); | 
  | 94 | 0 |                                  if(i.getShownIcon() != null){ | 
  | 95 | 0 |                                          titleLayout.add(i.getShownIcon()); | 
  | 96 |  |                                  } | 
  | 97 |  |                          } | 
  | 98 |  |                           | 
  | 99 | 0 |                  } | 
  | 100 |  |          }; | 
  | 101 |  |           | 
  | 102 | 0 |          public StylishDropDown(String title){ | 
  | 103 | 0 |                  titleLayout.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT); | 
  | 104 | 0 |                  titleLabel.setText(title); | 
  | 105 | 0 |                  titleLayout.add(titleLabel); | 
  | 106 | 0 |                  titleLayout.add(titleImage); | 
  | 107 | 0 |                  init(); | 
  | 108 | 0 |          } | 
  | 109 |  |           | 
  | 110 | 0 |          public StylishDropDown(String title, Image image, MenuImageLocation imgLoc){ | 
  | 111 | 0 |                  titleLayout.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT); | 
  | 112 | 0 |                  titleLabel.setText(title); | 
  | 113 | 0 |                  titleImage = image; | 
  | 114 | 0 |                  titleLayout.add(titleLabel); | 
  | 115 | 0 |                  if(imgLoc == MenuImageLocation.RIGHT){ | 
  | 116 | 0 |                          titleLayout.add(titleImage); | 
  | 117 |  |                  } | 
  | 118 |  |                  else{ | 
  | 119 | 0 |                          titleLayout.insert(titleImage, 0); | 
  | 120 |  |                  } | 
  | 121 | 0 |                  menu.setImageLocation(imgLoc); | 
  | 122 | 0 |                  init(); | 
  | 123 | 0 |          } | 
  | 124 |  |           | 
  | 125 | 0 |          public StylishDropDown(Widget widget){ | 
  | 126 | 0 |                  titleLayout.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT); | 
  | 127 | 0 |                  titleLayout.add(widget); | 
  | 128 | 0 |                  init(); | 
  | 129 | 0 |          } | 
  | 130 |  |           | 
  | 131 |  |           | 
  | 132 |  |           | 
  | 133 |  |   | 
  | 134 |  |   | 
  | 135 |  |   | 
  | 136 |  |          public void makeAButtonWhenOneItem(boolean makeButton){ | 
  | 137 | 0 |                  this.makeButton = makeButton; | 
  | 138 | 0 |          } | 
  | 139 |  |           | 
  | 140 |  |          private void init(){ | 
  | 141 | 0 |                  layout.clear(); | 
  | 142 | 0 |                  layout.setWidth("100%"); | 
  | 143 | 0 |                  layout.add(titleLayout); | 
  | 144 | 0 |                  layout.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT); | 
  | 145 | 0 |                  layout.add(defaultArrow); | 
  | 146 |  |   | 
  | 147 | 0 |                  namePanel.setWidget(layout); | 
  | 148 | 0 |                  menu.addGlobalMenuItemSelectCallback(new Callback<KSMenuItemData>(){ | 
  | 149 |  |   | 
  | 150 |  |                          @Override | 
  | 151 |  |                          public void exec(KSMenuItemData item) { | 
  | 152 | 0 |                                  if(item.getClickHandler() != null){ | 
  | 153 | 0 |                                          StylishDropDown.this.hideMenu(); | 
  | 154 | 0 |                                          if(showSelectedItem){ | 
  | 155 | 0 |                                                  titleLabel.setText(item.getLabel()); | 
  | 156 | 0 |                                                  if(item.getShownIcon() != null && showTitleIcon){ | 
  | 157 | 0 |                                                          titleLayout.remove(titleImage); | 
  | 158 | 0 |                                                          Image image = item.getShownIcon(); | 
  | 159 | 0 |                                                          titleImage = new Image(image.getUrl(), image.getOriginLeft(),  | 
  | 160 |  |                                                                          image.getOriginTop(), image.getWidth(), image.getHeight()); | 
  | 161 | 0 |                                                          if(imgLoc == MenuImageLocation.RIGHT){ | 
  | 162 | 0 |                                                                  titleLayout.add(titleImage); | 
  | 163 |  |                                                          } | 
  | 164 |  |                                                          else{ | 
  | 165 | 0 |                                                                  titleLayout.insert(titleImage, 0); | 
  | 166 |  |                                                          } | 
  | 167 |  |                                                           | 
  | 168 |  |                                                  } | 
  | 169 |  |                                          } | 
  | 170 |  |                                  } | 
  | 171 | 0 |                          } | 
  | 172 |  |                  }); | 
  | 173 | 0 |                  menuPanel.setWidget(menu); | 
  | 174 | 0 |                  namePanel.addClickHandler(panelHandler); | 
  | 175 | 0 |                  menuPanel.setAutoHideEnabled(true); | 
  | 176 | 0 |                  menuPanel.addAutoHidePartner(namePanel.getElement()); | 
  | 177 | 0 |                  namePanel.getElement().setAttribute("id", HTMLPanel.createUniqueId()); | 
  | 178 | 0 |                  parentPanel.add(namePanel); | 
  | 179 | 0 |                  this.initWidget(parentPanel); | 
  | 180 | 0 |                  titleLabel.addStyleName("KS-CutomDropDown-TitleLabel"); | 
  | 181 | 0 |                  layout.addStyleName("KS-CustomDropDown-TitlePanel"); | 
  | 182 | 0 |                  defaultArrow.addStyleName("KS-CustomDropDown-Arrow"); | 
  | 183 | 0 |          } | 
  | 184 |  |           | 
  | 185 |  |          public void showMenu(){ | 
  | 186 | 0 |                  menuPanel.setPopupPosition(this.getAbsoluteLeft(), this.getAbsoluteTop() + this.getOffsetHeight()); | 
  | 187 | 0 |                  menuPanel.show(); | 
  | 188 | 0 |          } | 
  | 189 |  |           | 
  | 190 |  |          public void hideMenu(){ | 
  | 191 | 0 |                  menuPanel.hide(); | 
  | 192 | 0 |          } | 
  | 193 |  |           | 
  | 194 |  |          public void setArrowImage(Image arrow){ | 
  | 195 | 0 |                  layout.remove(defaultArrow); | 
  | 196 | 0 |                  arrow.addStyleName("KS-CustomDropDown-Arrow"); | 
  | 197 | 0 |                  layout.add(arrow); | 
  | 198 | 0 |          } | 
  | 199 |  |           | 
  | 200 |  |          public void setItems(List<KSMenuItemData> items){ | 
  | 201 | 0 |                  if(makeButton && items.size() == 1){ | 
  | 202 | 0 |                          KSMenuItemData item = items.get(0); | 
  | 203 | 0 |                          button = new KSButton(); | 
  | 204 | 0 |                          button.addStyleName("ks-button-spacing"); | 
  | 205 | 0 |                          button.addClickHandler(item.getClickHandler()); | 
  | 206 | 0 |                          button.setText(item.getLabel()); | 
  | 207 | 0 |                          parentPanel.clear(); | 
  | 208 | 0 |                          parentPanel.add(button); | 
  | 209 | 0 |                  } | 
  | 210 |  |                  else{ | 
  | 211 | 0 |                          if(!namePanel.isAttached()){ | 
  | 212 | 0 |                                  parentPanel.clear(); | 
  | 213 | 0 |                                  parentPanel.add(namePanel); | 
  | 214 |  |                          } | 
  | 215 | 0 |                          for(KSMenuItemData item: items){ | 
  | 216 | 0 |                                  item.addMenuEventHandler(MenuSelectEvent.TYPE, menuHandler); | 
  | 217 | 0 |                                  item.addMenuEventHandler(MenuChangeEvent.TYPE, menuHandler); | 
  | 218 |  |                          } | 
  | 219 | 0 |                          menu.setItems(items); | 
  | 220 |  |                  } | 
  | 221 |  |                   | 
  | 222 | 0 |          } | 
  | 223 |  |           | 
  | 224 |  |          public void setEnabled(boolean enabled){ | 
  | 225 | 0 |                  this.enabled = enabled; | 
  | 226 | 0 |                  if(!enabled){ | 
  | 227 | 0 |                          layout.addStyleName("KS-CustomDropDown-TitlePanel-Disabled"); | 
  | 228 |  |                  } | 
  | 229 |  |                  else{ | 
  | 230 | 0 |                          layout.removeStyleName("KS-CustomDropDown-TitlePanel-Disabled"); | 
  | 231 |  |                  } | 
  | 232 | 0 |                  if(button != null){ | 
  | 233 | 0 |                          button.setEnabled(enabled); | 
  | 234 |  |                  } | 
  | 235 | 0 |          } | 
  | 236 |  |           | 
  | 237 |  |          public void setImageLocation(MenuImageLocation loc){ | 
  | 238 | 0 |                  imgLoc = loc; | 
  | 239 | 0 |                  menu.setImageLocation(loc); | 
  | 240 | 0 |          } | 
  | 241 |  |           | 
  | 242 |  |          @Override | 
  | 243 |  |          public void addStyleName(String style){ | 
  | 244 | 0 |                  namePanel.addStyleName(style); | 
  | 245 | 0 |                  menu.addStyleName(style); | 
  | 246 | 0 |          } | 
  | 247 |  |           | 
  | 248 |  |          public boolean isShowingSelectedItem() { | 
  | 249 | 0 |                  return showSelectedItem; | 
  | 250 |  |          } | 
  | 251 |  |   | 
  | 252 |  |          public void setShowSelectedItem(boolean showSelectedItem) { | 
  | 253 | 0 |                  this.showSelectedItem = showSelectedItem; | 
  | 254 | 0 |          } | 
  | 255 |  |           | 
  | 256 |  |          public void setShowTitleIcon(boolean showTitleIcon){ | 
  | 257 | 0 |                  this.showTitleIcon = showTitleIcon; | 
  | 258 | 0 |          } | 
  | 259 |  |           | 
  | 260 |  |          public boolean isShowingTitleIcon(){ | 
  | 261 | 0 |                  return showTitleIcon; | 
  | 262 |  |          } | 
  | 263 |  |           | 
  | 264 |  |           | 
  | 265 |  |  } |