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