| 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.widgets.KSLabel; |
| 24 | |
import org.kuali.student.common.ui.client.widgets.KSRadioButton; |
| 25 | |
import org.kuali.student.common.ui.client.widgets.focus.FocusGroup; |
| 26 | |
import org.kuali.student.common.ui.client.widgets.list.KSSelectItemWidgetAbstract; |
| 27 | |
import org.kuali.student.common.ui.client.widgets.list.ListItems; |
| 28 | |
import org.kuali.student.common.ui.client.widgets.list.ModelListItems; |
| 29 | |
import org.kuali.student.common.ui.client.widgets.list.SearchResultListItems; |
| 30 | |
|
| 31 | |
import com.google.gwt.event.dom.client.BlurHandler; |
| 32 | |
import com.google.gwt.event.dom.client.FocusHandler; |
| 33 | |
import com.google.gwt.event.dom.client.HasBlurHandlers; |
| 34 | |
import com.google.gwt.event.dom.client.HasFocusHandlers; |
| 35 | |
import com.google.gwt.event.dom.client.KeyUpEvent; |
| 36 | |
import com.google.gwt.event.dom.client.KeyUpHandler; |
| 37 | |
import com.google.gwt.event.logical.shared.ValueChangeEvent; |
| 38 | |
import com.google.gwt.event.logical.shared.ValueChangeHandler; |
| 39 | |
import com.google.gwt.event.shared.HandlerRegistration; |
| 40 | |
import com.google.gwt.user.client.ui.FlexTable; |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
public class KSRadioButtonListImpl extends KSSelectItemWidgetAbstract implements KeyUpHandler, ValueChangeHandler<Boolean>, HasBlurHandlers, HasFocusHandlers { |
| 56 | 0 | private final FocusGroup focus = new FocusGroup(this); |
| 57 | 0 | private FlexTable layout = new FlexTable(); |
| 58 | 0 | private List<String> selectedItems = new ArrayList<String>(); |
| 59 | 0 | private static int groupNumber = 0; |
| 60 | |
private String groupName; |
| 61 | |
|
| 62 | 0 | private int maxCols = 1; |
| 63 | 0 | private boolean enabled = true; |
| 64 | 0 | private boolean ignoreMultipleAttributes = false; |
| 65 | |
|
| 66 | 0 | public KSRadioButtonListImpl() { |
| 67 | 0 | groupName = "radio" + groupNumber; |
| 68 | 0 | groupNumber++; |
| 69 | 0 | initWidget(layout); |
| 70 | 0 | } |
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
public void deSelectItem(String id) { |
| 76 | 0 | for (int i=0; i < layout.getRowCount(); i++){ |
| 77 | 0 | for (int j=0; j < layout.getCellCount(i); j++){ |
| 78 | 0 | KSRadioButton radiobutton = (KSRadioButton)layout.getWidget(i, j); |
| 79 | 0 | if (radiobutton.getFormValue().equals(id)){ |
| 80 | 0 | this.selectedItems.remove(id); |
| 81 | 0 | radiobutton.setValue(false); |
| 82 | 0 | fireChangeEvent(false); |
| 83 | 0 | break; |
| 84 | |
} |
| 85 | |
} |
| 86 | |
} |
| 87 | 0 | } |
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
public List<String> getSelectedItems() { |
| 93 | 0 | return selectedItems; |
| 94 | |
} |
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
public void selectItem(String id) { |
| 102 | 0 | if (!selectedItems.contains(id)){ |
| 103 | 0 | for (int i=0; i < layout.getRowCount(); i++){ |
| 104 | 0 | for (int j=0; j < layout.getCellCount(i); j++){ |
| 105 | 0 | if(layout.getWidget(i, j) instanceof KSRadioButton){ |
| 106 | 0 | KSRadioButton radiobutton = (KSRadioButton)layout.getWidget(i, j); |
| 107 | 0 | if (radiobutton.getFormValue().equals(id)){ |
| 108 | 0 | selectedItems.clear(); |
| 109 | 0 | this.selectedItems.add(id); |
| 110 | 0 | radiobutton.setValue(true); |
| 111 | 0 | fireChangeEvent(false); |
| 112 | 0 | break; |
| 113 | |
} |
| 114 | |
} |
| 115 | |
} |
| 116 | |
} |
| 117 | |
} |
| 118 | 0 | } |
| 119 | |
|
| 120 | |
public void redraw(){ |
| 121 | 0 | layout.clear(); |
| 122 | 0 | int itemCount = super.getListItems().getItemCount(); |
| 123 | 0 | int currCount = 0; |
| 124 | 0 | int row = 0; |
| 125 | 0 | int col = 0; |
| 126 | |
|
| 127 | |
|
| 128 | 0 | if (!ignoreMultipleAttributes && super.getListItems().getAttrKeys() != null && super.getListItems().getAttrKeys().size() > 1) { |
| 129 | 0 | layout.addStyleName("KS-Checkbox-Table"); |
| 130 | |
|
| 131 | |
|
| 132 | |
|
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | 0 | for (String id:super.getListItems().getItemIds()){ |
| 138 | |
|
| 139 | 0 | layout.setWidget(row, col, createCheckbox(id)); |
| 140 | 0 | SearchResultListItems searchList = (SearchResultListItems)super.getListItems(); |
| 141 | 0 | String value = searchList.getItemAttribute(id, searchList.getAttrKeys().get(searchList.getItemTextAttrNdx())); |
| 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, createCheckboxWithLabel(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, createCheckboxWithLabel(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 createCheckbox(String id){ |
| 204 | 0 | KSRadioButton radiobutton = new KSRadioButton(groupName); |
| 205 | 0 | radiobutton.setFormValue(id); |
| 206 | 0 | radiobutton.addValueChangeHandler(this); |
| 207 | 0 | radiobutton.addKeyUpHandler(this); |
| 208 | 0 | focus.addWidget(radiobutton); |
| 209 | 0 | return radiobutton; |
| 210 | |
} |
| 211 | |
|
| 212 | |
private KSRadioButton createCheckboxWithLabel(String id){ |
| 213 | 0 | KSRadioButton radiobutton = new KSRadioButton(groupName, getListItems().getItemText(id)); |
| 214 | 0 | radiobutton.setFormValue(id); |
| 215 | 0 | radiobutton.addValueChangeHandler(this); |
| 216 | 0 | radiobutton.addKeyUpHandler(this); |
| 217 | 0 | focus.addWidget(radiobutton); |
| 218 | 0 | return radiobutton; |
| 219 | |
} |
| 220 | |
|
| 221 | 0 | public void onLoad() {} |
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
@Override |
| 227 | |
public void setColumnSize(int col) { |
| 228 | 0 | maxCols = col; |
| 229 | 0 | } |
| 230 | |
|
| 231 | |
@Override |
| 232 | |
public void setEnabled(boolean b) { |
| 233 | 0 | enabled = b; |
| 234 | 0 | for (int i=0; i < layout.getRowCount(); i++){ |
| 235 | 0 | for (int j=0; j < layout.getCellCount(i); j++){ |
| 236 | 0 | ((KSRadioButton)layout.getWidget(i, j)).setEnabled(b); |
| 237 | |
} |
| 238 | |
} |
| 239 | 0 | } |
| 240 | |
|
| 241 | |
@Override |
| 242 | |
public boolean isEnabled() { |
| 243 | 0 | return enabled; |
| 244 | |
} |
| 245 | |
|
| 246 | |
@Override |
| 247 | |
public boolean isMultipleSelect(){ |
| 248 | 0 | return false; |
| 249 | |
} |
| 250 | |
|
| 251 | |
public void setIgnoreMultipleAttributes(boolean ignoreMultiple){ |
| 252 | 0 | this.ignoreMultipleAttributes = ignoreMultiple; |
| 253 | 0 | } |
| 254 | |
|
| 255 | |
|
| 256 | |
@Override |
| 257 | |
public void clear(){ |
| 258 | 0 | selectedItems.clear(); |
| 259 | 0 | fireChangeEvent(false); |
| 260 | 0 | redraw(); |
| 261 | 0 | } |
| 262 | |
|
| 263 | |
@Override |
| 264 | |
public HandlerRegistration addBlurHandler(BlurHandler handler) { |
| 265 | 0 | return focus.addBlurHandler(handler); |
| 266 | |
} |
| 267 | |
|
| 268 | |
@Override |
| 269 | |
public HandlerRegistration addFocusHandler(FocusHandler handler) { |
| 270 | 0 | return focus.addFocusHandler(handler); |
| 271 | |
} |
| 272 | |
|
| 273 | |
@Override |
| 274 | |
public void onValueChange(ValueChangeEvent<Boolean> event) { |
| 275 | 0 | KSRadioButton radiobutton = (KSRadioButton)(event.getSource()); |
| 276 | 0 | String value = radiobutton.getFormValue(); |
| 277 | 0 | if (event.getValue()){ |
| 278 | 0 | if (!selectedItems.contains(value)){ |
| 279 | 0 | selectedItems.clear(); |
| 280 | 0 | selectedItems.add(value); |
| 281 | 0 | fireChangeEvent(true); |
| 282 | |
} |
| 283 | |
} |
| 284 | 0 | } |
| 285 | |
|
| 286 | |
@Override |
| 287 | |
public void setName(String name) { |
| 288 | 0 | super.setName(name); |
| 289 | 0 | groupName = name; |
| 290 | 0 | } |
| 291 | |
|
| 292 | |
@Override |
| 293 | |
public void onKeyUp(KeyUpEvent event) { |
| 294 | 0 | KSRadioButton radiobutton = (KSRadioButton)(event.getSource()); |
| 295 | 0 | String value = radiobutton.getFormValue(); |
| 296 | 0 | if (radiobutton.getValue()){ |
| 297 | 0 | if (!selectedItems.contains(value)){ |
| 298 | 0 | selectedItems.clear(); |
| 299 | 0 | selectedItems.add(value); |
| 300 | 0 | fireChangeEvent(true); |
| 301 | |
} |
| 302 | |
} |
| 303 | 0 | } |
| 304 | |
} |