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