1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.common.ui.client.widgets.list.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.util.DebugIdUtils; |
24 | |
import org.kuali.student.common.ui.client.widgets.KSLabel; |
25 | |
import org.kuali.student.common.ui.client.widgets.KSRadioButton; |
26 | |
import org.kuali.student.common.ui.client.widgets.focus.FocusGroup; |
27 | |
import org.kuali.student.common.ui.client.widgets.list.KSSelectItemWidgetAbstract; |
28 | |
import org.kuali.student.common.ui.client.widgets.list.ListItems; |
29 | |
import org.kuali.student.common.ui.client.widgets.list.ModelListItems; |
30 | |
import org.kuali.student.common.ui.client.widgets.list.SearchResultListItems; |
31 | |
|
32 | |
import com.google.gwt.event.dom.client.BlurHandler; |
33 | |
import com.google.gwt.event.dom.client.FocusHandler; |
34 | |
import com.google.gwt.event.dom.client.HasBlurHandlers; |
35 | |
import com.google.gwt.event.dom.client.HasFocusHandlers; |
36 | |
import com.google.gwt.event.dom.client.KeyUpEvent; |
37 | |
import com.google.gwt.event.dom.client.KeyUpHandler; |
38 | |
import com.google.gwt.event.logical.shared.ValueChangeEvent; |
39 | |
import com.google.gwt.event.logical.shared.ValueChangeHandler; |
40 | |
import com.google.gwt.event.shared.HandlerRegistration; |
41 | |
import com.google.gwt.user.client.ui.FlexTable; |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
public class KSRadioButtonListImpl extends KSSelectItemWidgetAbstract implements KeyUpHandler, ValueChangeHandler<Boolean>, HasBlurHandlers, HasFocusHandlers { |
57 | 0 | private final FocusGroup focus = new FocusGroup(this); |
58 | 0 | private FlexTable layout = new FlexTable(); |
59 | 0 | private List<String> selectedItems = new ArrayList<String>(); |
60 | 0 | private static int groupNumber = 0; |
61 | |
private String groupName; |
62 | |
|
63 | 0 | private int maxCols = 1; |
64 | 0 | private boolean enabled = true; |
65 | 0 | private boolean ignoreMultipleAttributes = false; |
66 | |
|
67 | 0 | public KSRadioButtonListImpl() { |
68 | 0 | groupName = "radio" + groupNumber; |
69 | 0 | groupNumber++; |
70 | 0 | initWidget(layout); |
71 | 0 | } |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
public void deSelectItem(String id) { |
77 | 0 | for (int i=0; i < layout.getRowCount(); i++){ |
78 | 0 | for (int j=0; j < layout.getCellCount(i); j++){ |
79 | 0 | KSRadioButton radiobutton = (KSRadioButton)layout.getWidget(i, j); |
80 | 0 | if (radiobutton.getFormValue().equals(id)){ |
81 | 0 | this.selectedItems.remove(id); |
82 | 0 | radiobutton.setValue(false); |
83 | 0 | fireChangeEvent(false); |
84 | 0 | break; |
85 | |
} |
86 | |
} |
87 | |
} |
88 | 0 | } |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
public List<String> getSelectedItems() { |
94 | 0 | return selectedItems; |
95 | |
} |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
public void selectItem(String id) { |
103 | 0 | if (!selectedItems.contains(id)){ |
104 | 0 | for (int i=0; i < layout.getRowCount(); i++){ |
105 | 0 | for (int j=0; j < layout.getCellCount(i); j++){ |
106 | 0 | if(layout.getWidget(i, j) instanceof KSRadioButton){ |
107 | 0 | KSRadioButton radiobutton = (KSRadioButton)layout.getWidget(i, j); |
108 | 0 | if (radiobutton.getFormValue().equals(id)){ |
109 | 0 | selectedItems.clear(); |
110 | 0 | this.selectedItems.add(id); |
111 | 0 | radiobutton.setValue(true); |
112 | 0 | fireChangeEvent(false); |
113 | 0 | break; |
114 | |
} |
115 | |
} |
116 | |
} |
117 | |
} |
118 | |
} |
119 | 0 | } |
120 | |
|
121 | |
public void redraw(){ |
122 | 0 | layout.clear(); |
123 | 0 | int itemCount = super.getListItems().getItemCount(); |
124 | 0 | int currCount = 0; |
125 | 0 | int row = 0; |
126 | 0 | int col = 0; |
127 | |
|
128 | |
|
129 | 0 | if (!ignoreMultipleAttributes && super.getListItems().getAttrKeys() != null && super.getListItems().getAttrKeys().size() > 1) { |
130 | 0 | layout.addStyleName("KS-Checkbox-Table"); |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | 0 | for (String id:super.getListItems().getItemIds()){ |
139 | 0 | SearchResultListItems searchList = (SearchResultListItems)super.getListItems(); |
140 | 0 | String value = searchList.getItemAttribute(id, searchList.getAttrKeys().get(searchList.getItemTextAttrNdx())); |
141 | 0 | layout.setWidget(row, col, createRadioButton(id, value)); |
142 | 0 | layout.setWidget(row,++col, new KSLabel(value)); |
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | 0 | row++; |
149 | 0 | col = 0; |
150 | 0 | } |
151 | |
|
152 | |
} |
153 | 0 | else if (maxCols <= 2){ |
154 | |
|
155 | 0 | int maxRows = (itemCount / maxCols) + (itemCount % 2); |
156 | 0 | for (String id:super.getListItems().getItemIds()){ |
157 | 0 | currCount++; |
158 | 0 | row = (currCount % maxRows); |
159 | 0 | row = ((row == 0) ? maxRows:row) - 1; |
160 | |
|
161 | 0 | layout.setWidget(row, col, createRadioButtonWithLabel(id)); |
162 | |
|
163 | 0 | col += ((row + 1)/ maxRows) * 1; |
164 | |
|
165 | |
} |
166 | 0 | } else { |
167 | |
|
168 | 0 | for (String id:super.getListItems().getItemIds()){ |
169 | 0 | currCount++; |
170 | 0 | col = currCount % maxCols; |
171 | 0 | col = ((col == 0) ? maxCols:col) - 1; |
172 | |
|
173 | 0 | layout.setWidget(row, col, createRadioButtonWithLabel(id)); |
174 | |
|
175 | 0 | row += ((col + 1 )/ maxCols) * 1; |
176 | |
} |
177 | |
} |
178 | |
|
179 | 0 | setInitialized(true); |
180 | 0 | } |
181 | |
|
182 | |
@Override |
183 | |
public <T extends Idable> void setListItems(ListItems listItems) { |
184 | 0 | if(listItems instanceof ModelListItems){ |
185 | 0 | Callback<T> redrawCallback = new Callback<T>(){ |
186 | |
|
187 | |
@Override |
188 | |
public void exec(T result){ |
189 | 0 | KSRadioButtonListImpl.this.redraw(); |
190 | 0 | } |
191 | |
}; |
192 | 0 | ((ModelListItems<T>)listItems).addOnAddCallback(redrawCallback); |
193 | 0 | ((ModelListItems<T>)listItems).addOnRemoveCallback(redrawCallback); |
194 | 0 | ((ModelListItems<T>)listItems).addOnUpdateCallback(redrawCallback); |
195 | 0 | ((ModelListItems<T>)listItems).addOnBulkUpdateCallback(redrawCallback); |
196 | |
} |
197 | |
|
198 | 0 | super.setListItems(listItems); |
199 | |
|
200 | 0 | redraw(); |
201 | 0 | } |
202 | |
|
203 | |
private KSRadioButton createRadioButton(String id, String debugId){ |
204 | 0 | KSRadioButton radiobutton = new KSRadioButton(groupName); |
205 | 0 | radiobutton.ensureDebugId(DebugIdUtils.createWebDriverSafeDebugId(debugId)); |
206 | 0 | radiobutton.setFormValue(id); |
207 | 0 | radiobutton.addValueChangeHandler(this); |
208 | 0 | radiobutton.addKeyUpHandler(this); |
209 | 0 | focus.addWidget(radiobutton); |
210 | 0 | return radiobutton; |
211 | |
} |
212 | |
|
213 | |
private KSRadioButton createRadioButtonWithLabel(String id){ |
214 | 0 | KSRadioButton radiobutton = new KSRadioButton(groupName, getListItems().getItemText(id)); |
215 | 0 | radiobutton.ensureDebugId(DebugIdUtils.createWebDriverSafeDebugId(getListItems().getItemText(id))); |
216 | 0 | radiobutton.setFormValue(id); |
217 | 0 | radiobutton.addValueChangeHandler(this); |
218 | 0 | radiobutton.addKeyUpHandler(this); |
219 | 0 | focus.addWidget(radiobutton); |
220 | 0 | return radiobutton; |
221 | |
} |
222 | |
|
223 | 0 | public void onLoad() {} |
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
@Override |
229 | |
public void setColumnSize(int col) { |
230 | 0 | maxCols = col; |
231 | 0 | } |
232 | |
|
233 | |
@Override |
234 | |
public void setEnabled(boolean b) { |
235 | 0 | enabled = b; |
236 | 0 | for (int i=0; i < layout.getRowCount(); i++){ |
237 | 0 | for (int j=0; j < layout.getCellCount(i); j++){ |
238 | 0 | ((KSRadioButton)layout.getWidget(i, j)).setEnabled(b); |
239 | |
} |
240 | |
} |
241 | 0 | } |
242 | |
|
243 | |
@Override |
244 | |
public boolean isEnabled() { |
245 | 0 | return enabled; |
246 | |
} |
247 | |
|
248 | |
@Override |
249 | |
public boolean isMultipleSelect(){ |
250 | 0 | return false; |
251 | |
} |
252 | |
|
253 | |
public void setIgnoreMultipleAttributes(boolean ignoreMultiple){ |
254 | 0 | this.ignoreMultipleAttributes = ignoreMultiple; |
255 | 0 | } |
256 | |
|
257 | |
|
258 | |
@Override |
259 | |
public void clear(){ |
260 | 0 | selectedItems.clear(); |
261 | 0 | fireChangeEvent(false); |
262 | 0 | redraw(); |
263 | 0 | } |
264 | |
|
265 | |
@Override |
266 | |
public HandlerRegistration addBlurHandler(BlurHandler handler) { |
267 | 0 | return focus.addBlurHandler(handler); |
268 | |
} |
269 | |
|
270 | |
@Override |
271 | |
public HandlerRegistration addFocusHandler(FocusHandler handler) { |
272 | 0 | return focus.addFocusHandler(handler); |
273 | |
} |
274 | |
|
275 | |
@Override |
276 | |
public void onValueChange(ValueChangeEvent<Boolean> event) { |
277 | 0 | KSRadioButton radiobutton = (KSRadioButton)(event.getSource()); |
278 | 0 | String value = radiobutton.getFormValue(); |
279 | 0 | if (event.getValue()){ |
280 | 0 | if (!selectedItems.contains(value)){ |
281 | 0 | selectedItems.clear(); |
282 | 0 | selectedItems.add(value); |
283 | 0 | fireChangeEvent(true); |
284 | |
} |
285 | |
} |
286 | 0 | } |
287 | |
|
288 | |
@Override |
289 | |
public void setName(String name) { |
290 | 0 | super.setName(name); |
291 | 0 | groupName = name; |
292 | 0 | } |
293 | |
|
294 | |
@Override |
295 | |
public void onKeyUp(KeyUpEvent event) { |
296 | 0 | KSRadioButton radiobutton = (KSRadioButton)(event.getSource()); |
297 | 0 | String value = radiobutton.getFormValue(); |
298 | 0 | if (radiobutton.getValue()){ |
299 | 0 | if (!selectedItems.contains(value)){ |
300 | 0 | selectedItems.clear(); |
301 | 0 | selectedItems.add(value); |
302 | 0 | fireChangeEvent(true); |
303 | |
} |
304 | |
} |
305 | 0 | } |
306 | |
} |