1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.student.common.ui.client.widgets.impl;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.kuali.student.common.dto.Idable;
22 import org.kuali.student.common.ui.client.mvc.Callback;
23 import org.kuali.student.common.ui.client.widgets.list.KSSelectItemWidgetAbstract;
24 import org.kuali.student.common.ui.client.widgets.list.ListItems;
25 import org.kuali.student.common.ui.client.widgets.list.ModelListItems;
26
27 import com.google.gwt.event.dom.client.BlurEvent;
28 import com.google.gwt.event.dom.client.BlurHandler;
29 import com.google.gwt.event.dom.client.ChangeEvent;
30 import com.google.gwt.event.dom.client.ChangeHandler;
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.HasBlurHandlers;
36 import com.google.gwt.event.dom.client.HasFocusHandlers;
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.event.shared.HandlerRegistration;
42 import com.google.gwt.user.client.ui.ListBox;
43
44 public class KSDropDownImpl extends KSSelectItemWidgetAbstract implements HasFocusHandlers, HasBlurHandlers{
45
46 private ListBox listBox;
47 private boolean blankFirstItem = true;
48 private final String EMPTY_ITEM = "";
49
50 public KSDropDownImpl() {
51 init();
52 }
53
54 public void redraw(){
55 String selectedItem = getSelectedItem();
56
57 listBox.clear();
58
59 if(blankFirstItem){
60 listBox.addItem(EMPTY_ITEM);
61 }
62 for (String id: super.getListItems().getItemIds()){
63 listBox.addItem(super.getListItems().getItemText(id),id);
64 }
65
66 selectItem(selectedItem);
67 setInitialized(true);
68 }
69
70 protected void init() {
71 listBox = new ListBox(false);
72 this.initWidget(listBox);
73 setupDefaultStyle();
74
75 listBox.addChangeHandler(new ChangeHandler(){
76 public void onChange(ChangeEvent event) {
77 fireChangeEvent(true);
78 }
79 });
80 }
81
82 private void setupDefaultStyle() {
83 listBox.addStyleName("KS-Dropdown");
84
85 listBox.addBlurHandler(new BlurHandler(){
86 public void onBlur(BlurEvent event) {
87 listBox.removeStyleName("KS-Dropdown-Focus");
88 }
89 });
90
91 listBox.addFocusHandler(new FocusHandler(){
92 public void onFocus(FocusEvent event) {
93 listBox.addStyleName("KS-Dropdown-Focus");
94 }
95 });
96
97 listBox.addMouseOverHandler(new MouseOverHandler(){
98 public void onMouseOver(MouseOverEvent event) {
99 listBox.addStyleName("KS-Dropdown-Hover");
100 }
101 });
102
103 listBox.addMouseOutHandler(new MouseOutHandler(){
104 public void onMouseOut(MouseOutEvent event) {
105 listBox.removeStyleName("KS-Dropdown-Hover");
106 }
107 });
108
109 listBox.addClickHandler(new ClickHandler(){
110 public void onClick(ClickEvent event) {
111 if(listBox.getSelectedIndex() != -1){
112 listBox.addStyleName("KS-Dropdown-Selected");
113 }
114 else{
115 listBox.removeStyleName("KS-Dropdown-Selected");
116 }
117 }
118 });
119 }
120
121 public void selectItem(String id){
122 if (id != null){
123 boolean matched = false;
124 for(int i = 0; i < listBox.getItemCount(); i++){
125 if(id.equals(listBox.getValue(i))){
126 matched = true;
127 listBox.setSelectedIndex(i);
128 fireChangeEvent(false);
129 break;
130 }
131 }
132 if(!matched){
133
134 if(blankFirstItem && listBox.getSelectedIndex() != 0){
135 listBox.setSelectedIndex(0);
136 fireChangeEvent(false);
137 }
138 }
139 }
140 }
141
142
143
144
145 @Override
146 public void deSelectItem(String id) {
147 int i = listBox.getSelectedIndex();
148 if (i >= 0 && listBox.getValue(i).equals(id)){
149 listBox.setItemSelected(i, false);
150 listBox.setItemSelected(0, true);
151 fireChangeEvent(false);
152 }
153 }
154
155 @Override
156 public <T extends Idable> void setListItems(ListItems listItems) {
157 if(listItems instanceof ModelListItems){
158 Callback<T> redrawCallback = new Callback<T>(){
159
160 @Override
161 public void exec(T result){
162 KSDropDownImpl.this.redraw();
163 }
164 };
165 ((ModelListItems<T>)listItems).addOnAddCallback(redrawCallback);
166 ((ModelListItems<T>)listItems).addOnRemoveCallback(redrawCallback);
167 ((ModelListItems<T>)listItems).addOnUpdateCallback(redrawCallback);
168 ((ModelListItems<T>)listItems).addOnBulkUpdateCallback(redrawCallback);
169 }
170
171 super.setListItems(listItems);
172
173 this.redraw();
174 }
175
176
177
178
179 @Override
180 public List<String> getSelectedItems() {
181 List<String> result = new ArrayList<String>();
182
183 int selectedIdx = listBox.getSelectedIndex();
184
185 if((blankFirstItem && selectedIdx > 0) || (!blankFirstItem && selectedIdx >= 0)){
186 String id = listBox.getValue(selectedIdx);
187 result.add(id);
188 }
189 return result;
190 }
191
192
193
194
195 @Override
196 public void onLoad() {
197
198 }
199
200 @Override
201 public void setEnabled(boolean b) {
202 listBox.setEnabled(b);
203 }
204
205 @Override
206 public boolean isEnabled() {
207 return listBox.isEnabled();
208 }
209
210 public boolean isBlankFirstItem() {
211 return blankFirstItem;
212 }
213
214 public void setBlankFirstItem(boolean blankFirstItem) {
215 this.blankFirstItem = blankFirstItem;
216 }
217
218 @Override
219 public HandlerRegistration addFocusHandler(FocusHandler handler) {
220 return listBox.addFocusHandler(handler);
221 }
222
223 @Override
224 public HandlerRegistration addBlurHandler(BlurHandler handler) {
225 return listBox.addBlurHandler(handler);
226 }
227
228 @Override
229 public void clear() {
230
231 if (super.getListItems() != null) {
232 listBox.clear();
233
234 if(blankFirstItem){
235 listBox.addItem(EMPTY_ITEM);
236 }
237 for (String id: super.getListItems().getItemIds()){
238 listBox.addItem(super.getListItems().getItemText(id),id);
239 }
240 fireChangeEvent(false);
241 }
242 }
243 }