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.ui.client.mvc.Callback; |
22 | |
import org.kuali.student.common.ui.client.widgets.list.KSSelectItemWidgetAbstract; |
23 | |
import org.kuali.student.common.ui.client.widgets.list.ListItems; |
24 | |
import org.kuali.student.common.ui.client.widgets.list.ModelListItems; |
25 | |
import org.kuali.student.core.dto.Idable; |
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 | for(int i = 0; i < listBox.getItemCount(); i++){ |
124 | 0 | if(id.equals(listBox.getValue(i))){ |
125 | 0 | listBox.setSelectedIndex(i); |
126 | 0 | fireChangeEvent(false); |
127 | |
} |
128 | |
} |
129 | |
} |
130 | 0 | } |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
@Override |
136 | |
public void deSelectItem(String id) { |
137 | 0 | int i = listBox.getSelectedIndex(); |
138 | 0 | if (i >= 0 && listBox.getValue(i).equals(id)){ |
139 | 0 | listBox.setItemSelected(i, false); |
140 | 0 | listBox.setItemSelected(0, true); |
141 | 0 | fireChangeEvent(false); |
142 | |
} |
143 | 0 | } |
144 | |
|
145 | |
@Override |
146 | |
public <T extends Idable> void setListItems(ListItems listItems) { |
147 | 0 | if(listItems instanceof ModelListItems){ |
148 | 0 | Callback<T> redrawCallback = new Callback<T>(){ |
149 | |
|
150 | |
@Override |
151 | |
public void exec(T result){ |
152 | 0 | KSDropDownImpl.this.redraw(); |
153 | 0 | } |
154 | |
}; |
155 | 0 | ((ModelListItems<T>)listItems).addOnAddCallback(redrawCallback); |
156 | 0 | ((ModelListItems<T>)listItems).addOnRemoveCallback(redrawCallback); |
157 | 0 | ((ModelListItems<T>)listItems).addOnUpdateCallback(redrawCallback); |
158 | 0 | ((ModelListItems<T>)listItems).addOnBulkUpdateCallback(redrawCallback); |
159 | |
} |
160 | |
|
161 | 0 | super.setListItems(listItems); |
162 | |
|
163 | 0 | this.redraw(); |
164 | 0 | } |
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
@Override |
170 | |
public List<String> getSelectedItems() { |
171 | 0 | List<String> result = new ArrayList<String>(); |
172 | |
|
173 | 0 | int selectedIdx = listBox.getSelectedIndex(); |
174 | |
|
175 | 0 | if((blankFirstItem && selectedIdx > 0) || (!blankFirstItem && selectedIdx >= 0)){ |
176 | 0 | String id = listBox.getValue(selectedIdx); |
177 | 0 | result.add(id); |
178 | |
} |
179 | 0 | return result; |
180 | |
} |
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
@Override |
186 | |
public void onLoad() { |
187 | |
|
188 | 0 | } |
189 | |
|
190 | |
@Override |
191 | |
public void setEnabled(boolean b) { |
192 | 0 | listBox.setEnabled(b); |
193 | 0 | } |
194 | |
|
195 | |
@Override |
196 | |
public boolean isEnabled() { |
197 | 0 | return listBox.isEnabled(); |
198 | |
} |
199 | |
|
200 | |
public boolean isBlankFirstItem() { |
201 | 0 | return blankFirstItem; |
202 | |
} |
203 | |
|
204 | |
public void setBlankFirstItem(boolean blankFirstItem) { |
205 | 0 | this.blankFirstItem = blankFirstItem; |
206 | 0 | } |
207 | |
|
208 | |
@Override |
209 | |
public HandlerRegistration addFocusHandler(FocusHandler handler) { |
210 | 0 | return listBox.addFocusHandler(handler); |
211 | |
} |
212 | |
|
213 | |
@Override |
214 | |
public HandlerRegistration addBlurHandler(BlurHandler handler) { |
215 | 0 | return listBox.addBlurHandler(handler); |
216 | |
} |
217 | |
|
218 | |
@Override |
219 | |
public void clear() { |
220 | |
|
221 | 0 | if (super.getListItems() != null) { |
222 | 0 | listBox.clear(); |
223 | |
|
224 | 0 | if(blankFirstItem){ |
225 | 0 | listBox.addItem(EMPTY_ITEM); |
226 | |
} |
227 | 0 | for (String id: super.getListItems().getItemIds()){ |
228 | 0 | listBox.addItem(super.getListItems().getItemText(id),id); |
229 | |
} |
230 | 0 | fireChangeEvent(false); |
231 | |
} |
232 | 0 | } |
233 | |
} |