View Javadoc

1   /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
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  public class StylishDropDown extends Composite{
50  	
51  	private ClickablePanel namePanel = new ClickablePanel();
52  	private SpanPanel parentPanel = new SpanPanel();
53  	private boolean showSelectedItem = false;
54  	private boolean showTitleIcon = false;
55  	private PopupPanel menuPanel = new PopupPanel();
56  	private KSListMenuImpl menu = new KSListMenuImpl();
57  	private HorizontalPanel layout = new HorizontalPanel();
58  	private KSLabel titleLabel = new KSLabel();
59  	private Image titleImage = Theme.INSTANCE.getCommonImages().getSpacer();
60  	private HorizontalPanel titleLayout = new HorizontalPanel();
61  	private Image defaultArrow = Theme.INSTANCE.getCommonImages().getDropDownIconBlack();
62  	private boolean mouseOver = false;
63  	private MenuImageLocation imgLoc = MenuImageLocation.RIGHT;
64  	private boolean makeButton = false;
65  	private boolean enabled = true;
66  	
67  	//optional button
68  	private KSButton button;
69  	
70  	private ClickHandler panelHandler = new ClickHandler(){
71  
72  		@Override
73  		public void onClick(ClickEvent event) {
74  			if(enabled){
75  				if(!menuPanel.isShowing()){
76  					StylishDropDown.this.showMenu();
77  				}
78  				else{
79  					StylishDropDown.this.hideMenu();
80  				}
81  			}
82  			
83  		}
84  		
85  	};
86  
87  	private KeyDownHandler downHandler = new KeyDownHandler(){
88  
89  		@Override
90  		public void onKeyDown(KeyDownEvent event) {
91  			if(enabled){
92  				if (event.getNativeKeyCode() == KeyboardListener.KEY_DOWN || event.getNativeKeyCode() == KeyboardListener.KEY_ENTER) 
93  					StylishDropDown.this.showMenu();
94  				else if (event.getNativeKeyCode() == KeyboardListener.KEY_UP)
95  					StylishDropDown.this.hideMenu();
96  				else if (event.getNativeKeyCode() == KeyboardListener.KEY_TAB)
97  				{
98  					StylishDropDown.this.showMenu();
99  					titleLayout.removeStyleName("KS-Basic-Menu-Item-Panel-Main-Hover");
100 				}					
101 			}	
102 		} 
103 	}; 
104 
105 	private FocusHandler focusHandler = new FocusHandler(){
106 
107 		@Override
108 		public void onFocus(FocusEvent event) {
109 			if(enabled) 
110 				titleLayout.addStyleName("KS-Basic-Menu-Item-Panel-Main-Hover");
111 		}
112 	}; 
113 
114 	private MouseOverHandler mouseOverHandler = new MouseOverHandler() {
115 
116 		@Override
117 		public void onMouseOver(MouseOverEvent event) {
118 			titleLayout.addStyleName("KS-Basic-Menu-Item-Panel-Main-Hover");
119 		}
120 		
121 	};
122 
123 	private MouseOutHandler mouseOutHandler = new MouseOutHandler() {
124 
125 		@Override
126 		public void onMouseOut(MouseOutEvent event) {
127 			titleLayout.removeStyleName("KS-Basic-Menu-Item-Panel-Main-Hover");
128 		}
129 		
130 	};
131 
132 	private MenuEventHandler menuHandler = new MenuEventHandler(){
133 
134 		@Override
135 		public void onChange(MenuChangeEvent e) {
136 			//Not needed?
137 			
138 		}
139 
140 		@Override
141 		public void onSelect(MenuSelectEvent e) {
142 			KSMenuItemData i = (KSMenuItemData) e.getSource();
143 			StylishDropDown.this.hideMenu();
144 			if(showSelectedItem){
145 				titleLayout.clear();
146 				titleLabel.setText(i.getLabel());
147 				titleLayout.add(titleLabel);
148 				if(i.getShownIcon() != null){
149 					titleLayout.add(i.getShownIcon());
150 				}
151 			}
152 			
153 		}
154 	};
155 	
156 	public StylishDropDown(String title){
157 		titleLayout.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
158 		titleLabel.setText(title);
159 		titleLayout.add(titleLabel);
160 		titleLayout.add(titleImage);
161 		init();
162 	}
163 	
164 	public StylishDropDown(String title, Image image, MenuImageLocation imgLoc){
165 		titleLayout.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
166 		titleLabel.setText(title);
167 		titleImage = image;
168 		titleLayout.add(titleLabel);
169 		if(imgLoc == MenuImageLocation.RIGHT){
170 			titleLayout.add(titleImage);
171 		}
172 		else{
173 			titleLayout.insert(titleImage, 0);
174 		}
175 		menu.setImageLocation(imgLoc);
176 		init();
177 	}
178 	
179 	public StylishDropDown(Widget widget){
180 		titleLayout.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
181 		titleLayout.add(widget);
182 		init();
183 	}
184 	
185 	
186 	/**
187 	 * This method will make the stylish drop down just a button when a list of 1 item is passed in
188 	 * @param makeButton
189 	 */
190 	public void makeAButtonWhenOneItem(boolean makeButton){
191 		this.makeButton = makeButton;
192 	}
193 	
194 	private void init(){
195 		layout.clear();
196 		layout.setWidth("100%");
197 		layout.add(titleLayout);
198 		layout.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
199 		layout.add(defaultArrow);
200 
201 		namePanel.setWidget(layout);
202 		menu.addGlobalMenuItemSelectCallback(new Callback<KSMenuItemData>(){
203 
204 			@Override
205 			public void exec(KSMenuItemData item) {
206 				if(item.getClickHandler() != null){
207 					StylishDropDown.this.hideMenu();
208 					if(showSelectedItem){
209 						titleLabel.setText(item.getLabel());
210 						if(item.getShownIcon() != null && showTitleIcon){
211 							titleLayout.remove(titleImage);
212 							Image image = item.getShownIcon();
213 							titleImage = new Image(image.getUrl(), image.getOriginLeft(), 
214 									image.getOriginTop(), image.getWidth(), image.getHeight());
215 							if(imgLoc == MenuImageLocation.RIGHT){
216 								titleLayout.add(titleImage);
217 							}
218 							else{
219 								titleLayout.insert(titleImage, 0);
220 							}
221 							
222 						}
223 					}
224 				}
225 			}
226 		});
227 		menuPanel.setWidget(menu);
228 		namePanel.addClickHandler(panelHandler);
229 		namePanel.addKeyDownHandler(downHandler);
230 		namePanel.addFocusHandler(focusHandler);
231 		namePanel.addMouseOverHandler(mouseOverHandler);
232 		namePanel.addMouseOutHandler(mouseOutHandler);
233 		namePanel.setTabIndex(1);
234 		menuPanel.setAutoHideEnabled(true);
235 		menuPanel.addAutoHidePartner(namePanel.getElement());
236 		namePanel.getElement().setAttribute("id", HTMLPanel.createUniqueId());
237 		parentPanel.add(namePanel);
238 		this.initWidget(parentPanel);
239 		titleLabel.addStyleName("KS-CutomDropDown-TitleLabel");
240 		layout.addStyleName("KS-CustomDropDown-TitlePanel");
241 		defaultArrow.addStyleName("KS-CustomDropDown-Arrow");
242 	}
243 	
244 	public void showMenu(){
245 		menuPanel.setPopupPosition(layout.getAbsoluteLeft(), layout.getAbsoluteTop() + layout.getOffsetHeight());
246 		menuPanel.show();
247 	}
248 	
249 	public void hideMenu(){
250 		menuPanel.hide();
251 	}
252 	
253 	public void setArrowImage(Image arrow){
254 		layout.remove(defaultArrow);
255 		arrow.addStyleName("KS-CustomDropDown-Arrow");
256 		layout.add(arrow);
257 	}
258 	
259 	public void setItems(List<KSMenuItemData> items){
260 		if(makeButton && items.size() == 1){
261 			KSMenuItemData item = items.get(0);
262 			button = new KSButton();
263 			button.addStyleName("ks-button-spacing");
264 			button.addClickHandler(item.getClickHandler());
265 			button.setText(item.getLabel());
266 			parentPanel.clear();
267 			parentPanel.add(button);
268 		}
269 		else{
270 			if(!namePanel.isAttached()){
271 				parentPanel.clear();
272 				parentPanel.add(namePanel);
273 			}
274 			for(KSMenuItemData item: items){
275 				item.addMenuEventHandler(MenuSelectEvent.TYPE, menuHandler);
276 				item.addMenuEventHandler(MenuChangeEvent.TYPE, menuHandler);
277 			}
278 			menu.setItems(items);
279 		}
280 		
281 	}
282 	
283 	public void setEnabled(boolean enabled){
284 		this.enabled = enabled;
285 		if(!enabled){
286 			layout.addStyleName("KS-CustomDropDown-TitlePanel-Disabled");
287 		}
288 		else{
289 			layout.removeStyleName("KS-CustomDropDown-TitlePanel-Disabled");
290 		}
291 		if(button != null){
292 			button.setEnabled(enabled);
293 		}
294 	}
295 	
296 	public void setImageLocation(MenuImageLocation loc){
297 		imgLoc = loc;
298 		menu.setImageLocation(loc);
299 	}
300 	
301 	@Override
302 	public void addStyleName(String style){
303 		namePanel.addStyleName(style);
304 		menu.addStyleName(style);
305 	}
306 	
307 	public boolean isShowingSelectedItem() {
308 		return showSelectedItem;
309 	}
310 
311 	public void setShowSelectedItem(boolean showSelectedItem) {
312 		this.showSelectedItem = showSelectedItem;
313 	}
314 	
315 	public void setShowTitleIcon(boolean showTitleIcon){
316 		this.showTitleIcon = showTitleIcon;
317 	}
318 	
319 	public boolean isShowingTitleIcon(){
320 		return showTitleIcon;
321 	}
322 	
323 	
324 }